LibGfx+LibWeb: Move HTML::CanvasPathClipper to Gfx::PathClipper

This does not depend on anything outside of LibGfx, and seems like it
may be useful elsewhere.

No behaviour change.
This commit is contained in:
MacDue 2024-03-16 22:12:59 +00:00 committed by Alexander Kalenik
parent a68b134e6d
commit 6c26ff567e
Notes: sideshowbarker 2024-07-16 22:34:39 +09:00
8 changed files with 84 additions and 84 deletions

View File

@ -70,6 +70,7 @@ set(SOURCES
Painter.cpp
Palette.cpp
Path.cpp
PathClipper.cpp
Point.cpp
Rect.cpp
ShareableBitmap.cpp

View File

@ -5,19 +5,19 @@
*/
#include <LibGfx/AntiAliasingPainter.h>
#include <LibWeb/HTML/Canvas/CanvasPathClipper.h>
#include <LibGfx/PathClipper.h>
namespace Web::HTML {
namespace Gfx {
// FIXME: This pretty naive, we should be able to cut down the allocations here
// (especially for the paint style which is a bit sad).
ErrorOr<CanvasPathClipper> CanvasPathClipper::create(Gfx::Painter& painter, CanvasClip const& canvas_clip)
ErrorOr<PathClipper> PathClipper::create(Painter& painter, ClipPath const& clip_path)
{
auto bounding_box = enclosing_int_rect(canvas_clip.path.bounding_box());
Gfx::IntRect actual_save_rect {};
auto maybe_bitmap = painter.get_region_bitmap(bounding_box, Gfx::BitmapFormat::BGRA8888, actual_save_rect);
RefPtr<Gfx::Bitmap> saved_clip_region;
auto bounding_box = enclosing_int_rect(clip_path.path.bounding_box());
IntRect actual_save_rect {};
auto maybe_bitmap = painter.get_region_bitmap(bounding_box, BitmapFormat::BGRA8888, actual_save_rect);
RefPtr<Bitmap> saved_clip_region;
if (!maybe_bitmap.is_error()) {
saved_clip_region = maybe_bitmap.release_value();
} else if (actual_save_rect.is_empty()) {
@ -27,20 +27,20 @@ ErrorOr<CanvasPathClipper> CanvasPathClipper::create(Gfx::Painter& painter, Canv
}
painter.save();
painter.add_clip_rect(bounding_box);
return CanvasPathClipper(move(saved_clip_region), bounding_box, canvas_clip);
return PathClipper(move(saved_clip_region), bounding_box, clip_path);
}
ErrorOr<void> CanvasPathClipper::apply_clip(Gfx::Painter& painter)
ErrorOr<void> PathClipper::apply_clip(Painter& painter)
{
painter.restore();
if (!m_saved_clip_region)
return {};
Gfx::IntRect actual_save_rect {};
auto clip_area = TRY(painter.get_region_bitmap(m_bounding_box, Gfx::BitmapFormat::BGRA8888, actual_save_rect));
IntRect actual_save_rect {};
auto clip_area = TRY(painter.get_region_bitmap(m_bounding_box, BitmapFormat::BGRA8888, actual_save_rect));
painter.blit(actual_save_rect.location(), *m_saved_clip_region, m_saved_clip_region->rect(), 1.0f, false);
Gfx::AntiAliasingPainter aa_painter { painter };
AntiAliasingPainter aa_painter { painter };
auto fill_offset = m_bounding_box.location() - actual_save_rect.location();
aa_painter.fill_path(m_canvas_clip.path, TRY(Gfx::BitmapPaintStyle::create(clip_area, fill_offset)), 1.0f, m_canvas_clip.winding_rule);
aa_painter.fill_path(m_clip_path.path, TRY(BitmapPaintStyle::create(clip_area, fill_offset)), 1.0f, m_clip_path.winding_rule);
return {};
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGfx/Painter.h>
#include <LibGfx/Path.h>
namespace Gfx {
struct ClipPath {
Path path;
Painter::WindingRule winding_rule;
};
class PathClipper {
public:
static ErrorOr<PathClipper> create(Painter&, ClipPath const& clip_path);
ErrorOr<void> apply_clip(Painter& painter);
private:
PathClipper(RefPtr<Bitmap const> saved_clip_region, IntRect bounding_box, ClipPath const& clip_path)
: m_saved_clip_region(saved_clip_region)
, m_bounding_box(bounding_box)
, m_clip_path(clip_path)
{
}
RefPtr<Bitmap const> m_saved_clip_region;
IntRect m_bounding_box;
ClipPath const& m_clip_path;
};
class ScopedPathClip {
AK_MAKE_NONMOVABLE(ScopedPathClip);
AK_MAKE_NONCOPYABLE(ScopedPathClip);
public:
ScopedPathClip(Painter& painter, Optional<ClipPath> const& clip_path)
: m_painter(painter)
{
if (clip_path.has_value()) {
auto clipper = PathClipper::create(painter, *clip_path);
if (!clipper.is_error())
m_path_clipper = clipper.release_value();
else
dbgln("Error: Failed to apply clip path: {}", clipper.error());
}
}
~ScopedPathClip()
{
if (m_path_clipper.has_value())
m_path_clipper->apply_clip(m_painter).release_value_but_fixme_should_propagate_errors();
}
private:
Painter& m_painter;
Optional<PathClipper> m_path_clipper;
};
}

View File

@ -243,7 +243,6 @@ set(SOURCES
HTML/BrowsingContextGroup.cpp
HTML/Canvas/CanvasDrawImage.cpp
HTML/Canvas/CanvasPath.cpp
HTML/Canvas/CanvasPathClipper.cpp
HTML/Canvas/CanvasState.cpp
HTML/CanvasGradient.cpp
HTML/CanvasPattern.cpp

View File

@ -1,65 +0,0 @@
/*
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGfx/Painter.h>
#include <LibGfx/Path.h>
namespace Web::HTML {
struct CanvasClip {
Gfx::Path path;
Gfx::Painter::WindingRule winding_rule;
};
class CanvasPathClipper {
public:
static ErrorOr<CanvasPathClipper> create(Gfx::Painter&, CanvasClip const& canvas_clip);
ErrorOr<void> apply_clip(Gfx::Painter& painter);
private:
CanvasPathClipper(RefPtr<Gfx::Bitmap const> saved_clip_region, Gfx::IntRect bounding_box, CanvasClip const& canvas_clip)
: m_saved_clip_region(saved_clip_region)
, m_bounding_box(bounding_box)
, m_canvas_clip(canvas_clip)
{
}
RefPtr<Gfx::Bitmap const> m_saved_clip_region;
Gfx::IntRect m_bounding_box;
CanvasClip const& m_canvas_clip;
};
class ScopedCanvasPathClip {
AK_MAKE_NONMOVABLE(ScopedCanvasPathClip);
AK_MAKE_NONCOPYABLE(ScopedCanvasPathClip);
public:
ScopedCanvasPathClip(Gfx::Painter& painter, Optional<CanvasClip> const& canvas_clip)
: m_painter(painter)
{
if (canvas_clip.has_value()) {
auto clipper = CanvasPathClipper::create(painter, *canvas_clip);
if (!clipper.is_error())
m_canvas_clipper = clipper.release_value();
else
dbgln("CRC2D Error: Failed to apply canvas clip path: {}", clipper.error());
}
}
~ScopedCanvasPathClip()
{
if (m_canvas_clipper.has_value())
m_canvas_clipper->apply_clip(m_painter).release_value_but_fixme_should_propagate_errors();
}
private:
Gfx::Painter& m_painter;
Optional<CanvasPathClipper> m_canvas_clipper;
};
}

View File

@ -12,8 +12,8 @@
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Color.h>
#include <LibGfx/PaintStyle.h>
#include <LibGfx/PathClipper.h>
#include <LibWeb/Bindings/CanvasRenderingContext2DPrototype.h>
#include <LibWeb/HTML/Canvas/CanvasPathClipper.h>
#include <LibWeb/HTML/CanvasGradient.h>
#include <LibWeb/HTML/CanvasPattern.h>
@ -79,7 +79,7 @@ public:
bool image_smoothing_enabled { true };
Bindings::ImageSmoothingQuality image_smoothing_quality { Bindings::ImageSmoothingQuality::Low };
float global_alpha = { 1 };
Optional<CanvasClip> clip;
Optional<Gfx::ClipPath> clip;
RefPtr<CSS::StyleValue> font_style_value { nullptr };
RefPtr<Gfx::Font const> current_font { nullptr };
Bindings::CanvasTextAlign text_align { Bindings::CanvasTextAlign::Start };

View File

@ -592,7 +592,7 @@ void CanvasRenderingContext2D::clip_internal(Gfx::Path& path, Gfx::Painter::Wind
if (drawing_state().clip.has_value()) {
dbgln("FIXME: CRC2D: Calculate the new clip path by intersecting the given path with the current one.");
}
drawing_state().clip = CanvasClip { path, winding_rule };
drawing_state().clip = Gfx::ClipPath { path, winding_rule };
}
void CanvasRenderingContext2D::clip(StringView fill_rule)

View File

@ -15,6 +15,7 @@
#include <LibGfx/Forward.h>
#include <LibGfx/Painter.h>
#include <LibGfx/Path.h>
#include <LibGfx/PathClipper.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/HTML/Canvas/CanvasCompositing.h>
#include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
@ -23,7 +24,6 @@
#include <LibWeb/HTML/Canvas/CanvasImageData.h>
#include <LibWeb/HTML/Canvas/CanvasImageSmoothing.h>
#include <LibWeb/HTML/Canvas/CanvasPath.h>
#include <LibWeb/HTML/Canvas/CanvasPathClipper.h>
#include <LibWeb/HTML/Canvas/CanvasPathDrawingStyles.h>
#include <LibWeb/HTML/Canvas/CanvasRect.h>
#include <LibWeb/HTML/Canvas/CanvasState.h>
@ -129,7 +129,7 @@ private:
auto painter = this->antialiased_painter();
if (!painter.has_value())
return;
ScopedCanvasPathClip clipper(painter->underlying_painter(), drawing_state().clip);
Gfx::ScopedPathClip clipper(painter->underlying_painter(), drawing_state().clip);
auto draw_rect = draw_function(*painter);
if (drawing_state().clip.has_value())
draw_rect.intersect(drawing_state().clip->path.bounding_box());