1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

LibGfx: Remove unused, deprecated code

This commit is contained in:
Jelle Raaijmakers 2024-10-18 14:08:52 +02:00 committed by Alexander Kalenik
parent 11e10d0532
commit 8419a5f60f
Notes: github-actions[bot] 2024-10-18 16:13:38 +00:00
6 changed files with 1 additions and 307 deletions

View file

@ -1,54 +0,0 @@
/*
* Copyright (c) 2021, Oleg Sikorskiy <olegsik@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/DeprecatedPainter.h>
#include <LibGfx/Font/FontDatabase.h>
#include <stdio.h>
BENCHMARK_CASE(diagonal_lines)
{
int const run_count = 50;
int const bitmap_size = 2000;
auto bitmap = TRY_OR_FAIL(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { bitmap_size, bitmap_size }));
Gfx::DeprecatedPainter painter(bitmap);
for (int run = 0; run < run_count; run++) {
for (int i = 0; i < bitmap_size; i++) {
painter.draw_line({ 0, 0 }, { i, bitmap_size - 1 }, Color::Blue);
painter.draw_line({ 0, 0 }, { bitmap_size - 1, i }, Color::Blue);
}
}
}
BENCHMARK_CASE(fill)
{
int const run_count = 1000;
int const bitmap_size = 2000;
auto bitmap = TRY_OR_FAIL(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { bitmap_size, bitmap_size }));
Gfx::DeprecatedPainter painter(bitmap);
for (int run = 0; run < run_count; run++) {
painter.fill_rect(bitmap->rect(), Color::Blue);
}
}
BENCHMARK_CASE(fill_with_gradient)
{
int const run_count = 50;
int const bitmap_size = 2000;
auto bitmap = TRY_OR_FAIL(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { bitmap_size, bitmap_size }));
Gfx::DeprecatedPainter painter(bitmap);
for (int run = 0; run < run_count; run++) {
painter.fill_rect_with_gradient(bitmap->rect(), Color::Blue, Color::Red);
}
}

View file

@ -1,5 +1,4 @@
set(TEST_SOURCES
BenchmarkGfxPainter.cpp
BenchmarkJPEGLoader.cpp
TestColor.cpp
TestDeltaE.cpp

View file

@ -48,7 +48,6 @@ DeprecatedPainter::DeprecatedPainter(Gfx::Bitmap& bitmap)
VERIFY(bitmap.format() == Gfx::BitmapFormat::BGRx8888 || bitmap.format() == Gfx::BitmapFormat::BGRA8888);
m_state_stack.append(State());
state().clip_rect = { { 0, 0 }, bitmap.size() };
m_clip_origin = state().clip_rect;
}
void DeprecatedPainter::clear_rect(IntRect const& a_rect, Color color)
@ -117,20 +116,6 @@ void DeprecatedPainter::fill_rect(IntRect const& rect, PaintStyle const& paint_s
});
}
void DeprecatedPainter::fill_rect_with_gradient(Orientation orientation, IntRect const& a_rect, Color gradient_start, Color gradient_end)
{
if (gradient_start == gradient_end) {
fill_rect(a_rect, gradient_start);
return;
}
return fill_rect_with_linear_gradient(a_rect, Array { ColorStop { gradient_start, 0 }, ColorStop { gradient_end, 1 } }, orientation == Orientation::Horizontal ? 90.0f : 0.0f);
}
void DeprecatedPainter::fill_rect_with_gradient(IntRect const& a_rect, Color gradient_start, Color gradient_end)
{
return fill_rect_with_gradient(Orientation::Horizontal, a_rect, gradient_start, gradient_end);
}
void DeprecatedPainter::fill_rect_with_rounded_corners(IntRect const& a_rect, Color color, int radius)
{
return fill_rect_with_rounded_corners(a_rect, color, radius, radius, radius, radius);
@ -790,63 +775,6 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con
}
}
void DeprecatedPainter::draw_scaled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& source, IntRect const& a_src_rect, float opacity, ScalingMode scaling_mode)
{
draw_scaled_bitmap(a_dst_rect, source, FloatRect { a_src_rect }, opacity, scaling_mode);
}
void DeprecatedPainter::draw_scaled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& source, FloatRect const& a_src_rect, float opacity, ScalingMode scaling_mode)
{
IntRect int_src_rect = enclosing_int_rect(a_src_rect);
if (a_src_rect == int_src_rect && a_dst_rect.size() == int_src_rect.size())
return blit(a_dst_rect.location(), source, int_src_rect, opacity);
if (scaling_mode == ScalingMode::None) {
IntRect clipped_draw_rect { (int)a_src_rect.location().x(), (int)a_src_rect.location().y(), a_dst_rect.size().width(), a_dst_rect.size().height() };
return blit(a_dst_rect.location(), source, clipped_draw_rect, opacity);
}
auto dst_rect = to_physical(a_dst_rect);
auto src_rect = a_src_rect;
auto clipped_rect = dst_rect.intersected(clip_rect());
if (clipped_rect.is_empty())
return;
if (source.has_alpha_channel() || opacity != 1.0f) {
switch (source.format()) {
case BitmapFormat::BGRx8888:
do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::BGRx8888>, opacity, scaling_mode);
break;
case BitmapFormat::BGRA8888:
do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::BGRA8888>, opacity, scaling_mode);
break;
default:
do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::Invalid>, opacity, scaling_mode);
break;
}
} else {
switch (source.format()) {
case BitmapFormat::BGRx8888:
do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::BGRx8888>, opacity, scaling_mode);
break;
default:
do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel<BitmapFormat::Invalid>, opacity, scaling_mode);
break;
}
}
}
void DeprecatedPainter::set_pixel(IntPoint p, Color color, bool blend)
{
auto point = p;
point.translate_by(state().translation);
// Use the scale only to avoid clipping pixels set in drawing functions that handle
// scaling and call set_pixel() -- do not scale the pixel.
if (!clip_rect().contains(point))
return;
set_physical_pixel(point, color, blend);
}
void DeprecatedPainter::set_physical_pixel(IntPoint physical_point, Color color, bool blend)
{
// This function should only be called after translation, clipping, etc has been handled elsewhere
@ -867,14 +795,6 @@ Optional<Color> DeprecatedPainter::get_pixel(IntPoint p)
return target().get_pixel(point);
}
ErrorOr<NonnullRefPtr<Bitmap>> DeprecatedPainter::get_region_bitmap(IntRect const& region, BitmapFormat format, Optional<IntRect&> actual_region)
{
auto bitmap_region = region.translated(state().translation).intersected(target().rect());
if (actual_region.has_value())
actual_region.value() = bitmap_region.translated(-state().translation);
return target().cropped(bitmap_region, format);
}
ALWAYS_INLINE void DeprecatedPainter::set_physical_pixel(u32& pixel, Color color)
{
// This always sets a single physical pixel, independent of scale().
@ -1059,25 +979,6 @@ void DeprecatedPainter::draw_line(IntPoint a_p1, IntPoint a_p2, Color color, int
}
}
void DeprecatedPainter::draw_triangle_wave(IntPoint a_p1, IntPoint a_p2, Color color, int amplitude, int thickness)
{
// FIXME: Support more than horizontal waves
VERIFY(a_p1.y() == a_p2.y());
auto const p1 = thickness > 1 ? a_p1.translated(-(thickness / 2), -(thickness / 2)) : a_p1;
auto const p2 = thickness > 1 ? a_p2.translated(-(thickness / 2), -(thickness / 2)) : a_p2;
auto point1 = to_physical(p1);
auto point2 = to_physical(p2);
auto y = point1.y();
for (int x = 0; x <= point2.x() - point1.x(); ++x) {
auto y_offset = abs(x % (2 * amplitude) - amplitude) - amplitude;
draw_physical_pixel({ point1.x() + x, y + y_offset }, color, thickness);
}
}
static bool can_approximate_bezier_curve(FloatPoint p1, FloatPoint p2, FloatPoint control)
{
// TODO: Somehow calculate the required number of splits based on the curve (and its size).
@ -1211,22 +1112,6 @@ void DeprecatedPainter::add_clip_rect(IntRect const& rect)
state().clip_rect.intersect(target().rect()); // FIXME: This shouldn't be necessary?
}
void DeprecatedPainter::clear_clip_rect()
{
state().clip_rect = m_clip_origin;
}
DeprecatedPainterStateSaver::DeprecatedPainterStateSaver(DeprecatedPainter& painter)
: m_painter(painter)
{
m_painter.save();
}
DeprecatedPainterStateSaver::~DeprecatedPainterStateSaver()
{
m_painter.restore();
}
void DeprecatedPainter::stroke_path(DeprecatedPath const& path, Color color, int thickness)
{
if (thickness <= 0)
@ -1234,66 +1119,4 @@ void DeprecatedPainter::stroke_path(DeprecatedPath const& path, Color color, int
fill_path(path.stroke_to_fill(thickness), color);
}
void DeprecatedPainter::draw_scaled_bitmap_with_transform(IntRect const& dst_rect, Bitmap const& bitmap, FloatRect const& src_rect, AffineTransform const& transform, float opacity, ScalingMode scaling_mode)
{
if (transform.is_identity_or_translation_or_scale()) {
draw_scaled_bitmap(transform.map(dst_rect.to_type<float>()).to_rounded<int>(), bitmap, src_rect, opacity, scaling_mode);
} else {
// The painter has an affine transform, we have to draw through it!
// FIXME: This is kinda inefficient.
// What we currently do, roughly:
// - Map the destination rect through the context's transform.
// - Compute the bounding rect of the destination quad.
// - For each point in the clipped bounding rect, reverse-map it to a point in the source image.
// - Sample the source image at the computed point.
// - Set or blend (depending on alpha values) one pixel in the canvas.
// - Loop.
// FIXME: DeprecatedPainter should have an affine transform as part of its state and handle all of this instead.
if (opacity == 0.0f)
return;
auto inverse_transform = transform.inverse();
if (!inverse_transform.has_value())
return;
auto destination_quad = transform.map_to_quad(dst_rect.to_type<float>());
auto destination_bounding_rect = destination_quad.bounding_rect().to_rounded<int>();
auto source_rect = enclosing_int_rect(src_rect).intersected(bitmap.rect());
Gfx::AffineTransform source_transform;
source_transform.translate(src_rect.x(), src_rect.y());
source_transform.scale(src_rect.width() / dst_rect.width(), src_rect.height() / dst_rect.height());
source_transform.translate(-dst_rect.x(), -dst_rect.y());
auto translated_dest_rect = destination_bounding_rect.translated(translation());
auto clipped_bounding_rect = translated_dest_rect.intersected(clip_rect());
if (clipped_bounding_rect.is_empty())
return;
auto sample_transform = source_transform.multiply(*inverse_transform);
auto start_offset = destination_bounding_rect.location() + (clipped_bounding_rect.location() - translated_dest_rect.location());
for (int y = 0; y < clipped_bounding_rect.height(); ++y) {
for (int x = 0; x < clipped_bounding_rect.width(); ++x) {
auto point = Gfx::IntPoint { x, y };
auto sample_point = point + start_offset;
// AffineTransform::map(IntPoint) rounds internally, which is wrong here. So explicitly call the FloatPoint version, and then truncate the result.
auto source_point = Gfx::IntPoint { sample_transform.map(Gfx::FloatPoint { sample_point }) };
if (!source_rect.contains(source_point))
continue;
auto source_color = bitmap.get_pixel(source_point);
if (source_color.alpha() == 0)
continue;
if (opacity != 1.0f)
source_color = source_color.with_opacity(opacity);
set_physical_pixel(point + clipped_bounding_rect.location(), source_color, true);
}
}
}
}
}

View file

@ -12,7 +12,6 @@
#include <AK/Vector.h>
#include <LibGfx/Color.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Gradients.h>
#include <LibGfx/LineStyle.h>
#include <LibGfx/PaintStyle.h>
#include <LibGfx/Point.h>
@ -44,24 +43,12 @@ public:
void clear_rect(IntRect const&, Color);
void fill_rect(IntRect const&, Color);
void fill_rect(IntRect const&, PaintStyle const&);
void fill_rect_with_gradient(Orientation, IntRect const&, Color gradient_start, Color gradient_end);
void fill_rect_with_gradient(IntRect const&, Color gradient_start, Color gradient_end);
void fill_rect_with_linear_gradient(IntRect const&, ReadonlySpan<ColorStop>, float angle, Optional<float> repeat_length = {});
void fill_rect_with_conic_gradient(IntRect const&, ReadonlySpan<ColorStop>, IntPoint center, float start_angle, Optional<float> repeat_length = {});
void fill_rect_with_radial_gradient(IntRect const&, ReadonlySpan<ColorStop>, IntPoint center, IntSize size, Optional<float> repeat_length = {}, Optional<float> rotation_angle = {});
void fill_rect_with_rounded_corners(IntRect const&, Color, int radius);
void fill_rect_with_rounded_corners(IntRect const&, Color, int top_left_radius, int top_right_radius, int bottom_right_radius, int bottom_left_radius);
void fill_ellipse(IntRect const&, Color);
void draw_rect(IntRect const&, Color, bool rough = false);
void draw_scaled_bitmap(IntRect const& dst_rect, Gfx::Bitmap const&, IntRect const& src_rect, float opacity = 1.0f, ScalingMode = ScalingMode::NearestNeighbor);
void draw_scaled_bitmap(IntRect const& dst_rect, Gfx::Bitmap const&, FloatRect const& src_rect, float opacity = 1.0f, ScalingMode = ScalingMode::NearestNeighbor);
void draw_scaled_bitmap_with_transform(IntRect const& dst_rect, Gfx::Bitmap const&, FloatRect const& src_rect, Gfx::AffineTransform const&, float opacity = 1.0f, ScalingMode = ScalingMode::NearestNeighbor);
void set_pixel(IntPoint, Color, bool blend = false);
void set_pixel(int x, int y, Color color, bool blend = false) { set_pixel({ x, y }, color, blend); }
Optional<Color> get_pixel(IntPoint);
ErrorOr<NonnullRefPtr<Bitmap>> get_region_bitmap(IntRect const&, BitmapFormat format, Optional<IntRect&> actual_region = {});
void draw_line(IntPoint, IntPoint, Color, int thickness = 1, LineStyle style = LineStyle::Solid, Color alternate_color = Color::Transparent);
void draw_triangle_wave(IntPoint, IntPoint, Color color, int amplitude, int thickness = 1);
void blit(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, float opacity = 1.0f, bool apply_alpha = true);
void blit_filtered(IntPoint, Gfx::Bitmap const&, IntRect const& src_rect, Function<Color(Color)> const&, bool apply_alpha = true);
@ -85,10 +72,8 @@ public:
void fill_path(DeprecatedPath const&, PaintStyle const& paint_style, float opacity = 1.0f, WindingRule rule = WindingRule::Nonzero);
void add_clip_rect(IntRect const& rect);
void clear_clip_rect();
void translate(int dx, int dy) { translate({ dx, dy }); }
void translate(IntPoint delta) { state().translation.translate_by(delta); }
void translate(int dx, int dy) { state().translation.translate_by({ dx, dy }); }
IntPoint translation() const { return state().translation; }
@ -104,12 +89,10 @@ public:
IntRect clip_rect() const { return state().clip_rect; }
protected:
friend GradientLine;
friend AntiAliasingPainter;
template<unsigned SamplesPerPixel>
friend class EdgeFlagPathRasterizer;
IntRect to_physical(IntRect const& r) const { return r.translated(translation()); }
IntPoint to_physical(IntPoint p) const { return p.translated(translation()); }
void set_physical_pixel(u32& pixel, Color);
void fill_physical_scanline(int y, int x, int width, Color color);
@ -127,18 +110,8 @@ protected:
void fill_physical_rect(IntRect const&, Color);
IntRect m_clip_origin;
NonnullRefPtr<Gfx::Bitmap> m_target;
Vector<State, 4> m_state_stack;
};
class DeprecatedPainterStateSaver {
public:
explicit DeprecatedPainterStateSaver(DeprecatedPainter&);
~DeprecatedPainterStateSaver();
private:
DeprecatedPainter& m_painter;
};
}

View file

@ -155,16 +155,6 @@ public:
invalidate_split_lines();
}
void horizontal_line_to(float x)
{
line_to({ x, last_point().y() });
}
void vertical_line_to(float y)
{
line_to({ last_point().x(), y });
}
void quadratic_bezier_curve_to(FloatPoint through, FloatPoint point)
{
append_segment<DeprecatedPathSegment::QuadraticBezierCurveTo>(through, point);
@ -211,13 +201,6 @@ public:
return m_split_lines->bounding_box;
}
void append_path(DeprecatedPath const& path)
{
m_commands.extend(path.m_commands);
m_points.extend(path.m_points);
invalidate_split_lines();
}
ByteString to_byte_string() const;
PathSegmentIterator begin() const

View file

@ -271,41 +271,11 @@ static auto create_radial_gradient(IntRect const& physical_rect, ReadonlySpan<Co
};
}
void DeprecatedPainter::fill_rect_with_linear_gradient(IntRect const& rect, ReadonlySpan<ColorStop> color_stops, float angle, Optional<float> repeat_length)
{
auto a_rect = to_physical(rect);
if (a_rect.intersected(clip_rect()).is_empty())
return;
auto linear_gradient = create_linear_gradient(a_rect, color_stops, angle, repeat_length);
linear_gradient.paint(*this, a_rect);
}
static FloatPoint pixel_center(IntPoint point)
{
return point.to_type<float>().translated(0.5f, 0.5f);
}
void DeprecatedPainter::fill_rect_with_conic_gradient(IntRect const& rect, ReadonlySpan<ColorStop> color_stops, IntPoint center, float start_angle, Optional<float> repeat_length)
{
auto a_rect = to_physical(rect);
if (a_rect.intersected(clip_rect()).is_empty())
return;
// Translate position/center to the center of the pixel (avoids some funky painting)
auto center_point = pixel_center(center);
auto conic_gradient = create_conic_gradient(color_stops, center_point, start_angle, repeat_length);
conic_gradient.paint(*this, a_rect);
}
void DeprecatedPainter::fill_rect_with_radial_gradient(IntRect const& rect, ReadonlySpan<ColorStop> color_stops, IntPoint center, IntSize size, Optional<float> repeat_length, Optional<float> rotation_angle)
{
auto a_rect = to_physical(rect);
if (a_rect.intersected(clip_rect()).is_empty())
return;
auto radial_gradient = create_radial_gradient(a_rect, color_stops, center, size, repeat_length, rotation_angle);
radial_gradient.paint(*this, a_rect);
}
// TODO: Figure out how to handle scale() here... Not important while not supported by fill_path()
void LinearGradientPaintStyle::paint(IntRect physical_bounding_box, PaintFunction paint) const