Skip to content

Truly independent web browser

LibCompositing+Compositor: Keep a Skia path for each retained clip path

Aliaksandr Kalenika469c7663aa3
diff --git a/Libraries/LibCompositing/Rust/src/host/replay.rs b/Libraries/LibCompositing/Rust/src/host/replay.rs
index b99d5e92746..9c09769ebe2 100644
--- a/Libraries/LibCompositing/Rust/src/host/replay.rs
+++ b/Libraries/LibCompositing/Rust/src/host/replay.rs
@@ -18,7 +18,8 @@ pub struct FfiDisplayListReplayCallbacks {
pub set_matrix: unsafe extern "C" fn(*mut c_void, *const FloatMatrix4x4),
pub would_be_fully_clipped_by_painter: unsafe extern "C" fn(*mut c_void, IntRect) -> bool,
pub push_clip: unsafe extern "C" fn(*mut c_void, *const ReplayClip),
- pub push_clip_path: unsafe extern "C" fn(*mut c_void, *const c_void, WindingRule),
+ /// Takes the path, the identity of the path, and the fill rule.
+ pub push_clip_path: unsafe extern "C" fn(*mut c_void, *const c_void, u64, WindingRule),
pub push_layer: unsafe extern "C" fn(*mut c_void, *const ReplayLayer),
// Arguments: the mask, and the layer whose backdrop filter the mask's layer starts with, or null.
pub push_mask: unsafe extern "C" fn(*mut c_void, *const ReplayMask, *const ReplayLayer),
@@ -54,7 +55,7 @@ impl ReplayPainter for FfiDisplayListReplayCallbacks {
fn push_clip_path(&mut self, path: &OwnedPath, winding_rule: WindingRule) {
// SAFETY: The C++ painter reads the Gfx::Path synchronously; the tree keeps it alive.
- unsafe { (self.push_clip_path)(self.context, path.as_raw(), winding_rule) };
+ unsafe { (self.push_clip_path)(self.context, path.as_raw(), path.identity(), winding_rule) };
}
fn push_layer(&mut self, layer: &ReplayLayer) {
diff --git a/Services/Compositor/DisplayListPlayer.cpp b/Services/Compositor/DisplayListPlayer.cpp
index a628181b1ef..8845d039cfa 100644
--- a/Services/Compositor/DisplayListPlayer.cpp
+++ b/Services/Compositor/DisplayListPlayer.cpp
@@ -78,7 +78,7 @@ struct DisplayListPlayer::ReplayCallbacks {
return static_cast<DisplayListPlayer*>(context)->would_be_fully_clipped_by_painter(rect);
},
.push_clip = [](void* context, ReplayClip const* clip) { static_cast<DisplayListPlayer*>(context)->push_clip(*clip); },
- .push_clip_path = [](void* context, void const* path, Gfx::WindingRule winding_rule) { static_cast<DisplayListPlayer*>(context)->push_clip_path(*static_cast<Gfx::Path const*>(path), winding_rule); },
+ .push_clip_path = [](void* context, void const* path, u64 path_identity, Gfx::WindingRule winding_rule) { static_cast<DisplayListPlayer*>(context)->push_clip_path(*static_cast<Gfx::Path const*>(path), path_identity, winding_rule); },
.push_layer = [](void* context, ReplayLayer const* layer) { static_cast<DisplayListPlayer*>(context)->push_layer(*layer); },
.push_mask = [](void* context, ReplayMask const* mask, ReplayLayer const* backdrop) { static_cast<DisplayListPlayer*>(context)->push_mask(*mask, backdrop); },
.pop_mask = [](void* context, ReplayMask const* mask, EffectNodeIndex effect) { static_cast<DisplayListPlayer*>(context)->pop_mask(*mask, effect); },
@@ -91,7 +91,7 @@ struct DisplayListPlayer::ReplayCallbacks {
path.close();
static_cast<DisplayListPlayer*>(context)->push_device_space_plane_clip(path); },
.push_transform = [](void* context, Gfx::AffineTransform const* transform) { static_cast<DisplayListPlayer*>(context)->push_transform(*transform); },
- .push_clip_path_bytes = [](void* context, u8 const* path_bytes, size_t path_bytes_size, Gfx::WindingRule winding_rule) { static_cast<DisplayListPlayer*>(context)->push_clip_path(Gfx::Path::from_serialized_bytes({ path_bytes, path_bytes_size }), winding_rule); },
+ .push_clip_path_bytes = [](void* context, u8 const* path_bytes, size_t path_bytes_size, Gfx::WindingRule winding_rule) { static_cast<DisplayListPlayer*>(context)->push_clip_path_bytes({ path_bytes, path_bytes_size }, winding_rule); },
.play_command = [](void* context, DisplayListCommandType command_type, u8 const* command, u8 const* payload, size_t payload_size) { static_cast<DisplayListPlayer*>(context)->play_command_bytes(command_type, command, { payload, payload_size }); },
};
}
diff --git a/Services/Compositor/DisplayListPlayer.h b/Services/Compositor/DisplayListPlayer.h
index d2a0f8ab6eb..94069386b80 100644
--- a/Services/Compositor/DisplayListPlayer.h
+++ b/Services/Compositor/DisplayListPlayer.h
@@ -70,7 +70,9 @@ private:
virtual bool would_be_fully_clipped_by_painter(Gfx::IntRect) const = 0;
virtual void push_clip(Compositing::ReplayClip const&) = 0;
- virtual void push_clip_path(Gfx::Path const&, Gfx::WindingRule) = 0;
+ // A path stays the same for as long as it has its identity.
+ virtual void push_clip_path(Gfx::Path const&, u64 path_identity, Gfx::WindingRule) = 0;
+ virtual void push_clip_path_bytes(ReadonlyBytes, Gfx::WindingRule) = 0;
virtual void push_transform(Gfx::AffineTransform const&) = 0;
virtual void push_layer(Compositing::ReplayLayer const&) = 0;
virtual void push_mask(Compositing::ReplayMask const&, Compositing::ReplayLayer const* backdrop) = 0;
diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cpp
index 668839305ac..195270dda88 100644
--- a/Services/Compositor/DisplayListPlayerSkia.cpp
+++ b/Services/Compositor/DisplayListPlayerSkia.cpp
@@ -94,6 +94,7 @@ void DisplayListPlayerSkia::execute(
{
TemporaryChange composited_context_resolver_change { m_composited_context_resolver, composited_context_resolver };
TemporaryChange scrollbar_controller_change { m_scrollbar_controller, scrollbar_controller };
+ raster_cache.drop_clip_paths_not_used_since_last_frame();
TemporaryChange raster_cache_change { m_raster_cache, &raster_cache };
if (m_layer_image_filter_cache->tree_structural_epoch != visual_context_tree.structural_epoch()) {
m_layer_image_filter_cache->entries_by_tree_structural_epoch_and_effect.clear();
@@ -1527,10 +1528,17 @@ void DisplayListPlayerSkia::push_clip(ReplayClip const& clip)
clip_to_rounded_rect(canvas, clip.rect, clip.corner_radii, clip.mode == ClipMode::Difference ? SkClipOp::kDifference : SkClipOp::kIntersect);
}
-void DisplayListPlayerSkia::push_clip_path(Gfx::Path const& path, Gfx::WindingRule winding_rule)
+void DisplayListPlayerSkia::push_clip_path(Gfx::Path const& path, u64 path_identity, Gfx::WindingRule winding_rule)
{
surface().canvas().save();
- clip_path(path, winding_rule, true);
+ // A path that stays the same keeps one Skia path, so caches that are keyed on it work.
+ clip_path(raster_cache().clip_path(path_identity, path), winding_rule, true);
+}
+
+void DisplayListPlayerSkia::push_clip_path_bytes(ReadonlyBytes path_bytes, Gfx::WindingRule winding_rule)
+{
+ surface().canvas().save();
+ clip_path(Gfx::to_skia_path(Gfx::Path::from_serialized_bytes(path_bytes)), winding_rule, true);
}
void DisplayListPlayerSkia::push_transform(Gfx::AffineTransform const& transform)
@@ -1690,7 +1698,7 @@ void DisplayListPlayerSkia::push_device_space_plane_clip(Gfx::Path const& path)
auto& canvas = surface().canvas();
canvas.save();
canvas.setMatrix(SkM44());
- clip_path(path, Gfx::WindingRule::Nonzero, false);
+ clip_path(Gfx::to_skia_path(path), Gfx::WindingRule::Nonzero, false);
}
void DisplayListPlayerSkia::set_matrix(Gfx::FloatMatrix4x4 const& matrix)
@@ -1703,12 +1711,10 @@ Gfx::FloatMatrix4x4 DisplayListPlayerSkia::canvas_matrix() const
return to_gfx_matrix4x4(surface().canvas().getLocalToDevice());
}
-void DisplayListPlayerSkia::clip_path(Gfx::Path const& path, Gfx::WindingRule winding_rule, bool anti_aliased)
+void DisplayListPlayerSkia::clip_path(SkPath path, Gfx::WindingRule winding_rule, bool anti_aliased)
{
- auto& canvas = surface().canvas();
- auto sk_path = to_skia_path(path);
- sk_path.setFillType(to_skia_path_fill_type(winding_rule));
- canvas.clipPath(sk_path, anti_aliased);
+ path.setFillType(to_skia_path_fill_type(winding_rule));
+ surface().canvas().clipPath(path, anti_aliased);
}
bool DisplayListPlayerSkia::would_be_fully_clipped_by_painter(Gfx::IntRect rect) const
diff --git a/Services/Compositor/DisplayListPlayerSkia.h b/Services/Compositor/DisplayListPlayerSkia.h
index df14ed12d90..ddfd3da6c9b 100644
--- a/Services/Compositor/DisplayListPlayerSkia.h
+++ b/Services/Compositor/DisplayListPlayerSkia.h
@@ -23,6 +23,7 @@ class SkColorFilter;
class SkImage;
class SkImageFilter;
class SkPaint;
+class SkPath;
template<typename T>
class sk_sp;
@@ -66,7 +67,8 @@ private:
bool would_be_fully_clipped_by_painter(Gfx::IntRect) const override;
void push_clip(Compositing::ReplayClip const&) override;
- void push_clip_path(Gfx::Path const&, Gfx::WindingRule) override;
+ void push_clip_path(Gfx::Path const&, u64 path_identity, Gfx::WindingRule) override;
+ void push_clip_path_bytes(ReadonlyBytes, Gfx::WindingRule) override;
void push_transform(Gfx::AffineTransform const&) override;
void push_layer(Compositing::ReplayLayer const&) override;
void push_mask(Compositing::ReplayMask const&, Compositing::ReplayLayer const* backdrop) override;
@@ -74,7 +76,7 @@ private:
void pop() override;
void push_device_space_plane_clip(Gfx::Path const&) override;
- void clip_path(Gfx::Path const&, Gfx::WindingRule, bool anti_aliased);
+ void clip_path(SkPath, Gfx::WindingRule, bool anti_aliased);
SkPaint paint_style_to_skia_paint(Compositing::DisplayListPaintStyle const&, Gfx::FloatRect const& bounding_rect);
sk_sp<SkImageFilter> layer_image_filter(Compositing::ReplayLayer const&);
diff --git a/Services/Compositor/DisplayListRasterCache.cpp b/Services/Compositor/DisplayListRasterCache.cpp
index e71ea2ff8a0..2eabda2219e 100644
--- a/Services/Compositor/DisplayListRasterCache.cpp
+++ b/Services/Compositor/DisplayListRasterCache.cpp
@@ -24,6 +24,7 @@
#include <core/SkFont.h>
#include <core/SkImage.h>
+#include <core/SkPath.h>
#include <core/SkTextBlob.h>
#include <core/SkYUVAPixmaps.h>
#include <gpu/ganesh/GrDirectContext.h>
@@ -130,6 +131,17 @@ struct DisplayListRasterCache::TextBlob {
MonotonicTime last_used;
};
+struct DisplayListRasterCache::ClipPath {
+ AK_ALLOC_WITH_KMALLOC;
+
+ explicit ClipPath(SkPath path)
+ : path(move(path))
+ {
+ }
+
+ SkPath path;
+};
+
DisplayListRasterCache::DisplayListRasterCache() = default;
DisplayListRasterCache::~DisplayListRasterCache() = default;
@@ -430,4 +442,22 @@ sk_sp<SkTextBlob> DisplayListRasterCache::text_blob(DisplayListResourceStorage c
return blob;
}
+SkPath DisplayListRasterCache::clip_path(u64 path_identity, Gfx::Path const& path)
+{
+ if (auto it = m_clip_paths.find(path_identity); it != m_clip_paths.end())
+ return it->value->path;
+ auto entry = m_clip_paths_from_last_frame.take(path_identity).value_or_lazy_evaluated([&] {
+ return make<ClipPath>(Gfx::to_skia_path(path));
+ });
+ auto skia_path = entry->path;
+ m_clip_paths.set(path_identity, move(entry));
+ return skia_path;
+}
+
+void DisplayListRasterCache::drop_clip_paths_not_used_since_last_frame()
+{
+ swap(m_clip_paths, m_clip_paths_from_last_frame);
+ m_clip_paths.clear_with_capacity();
+}
+
}
diff --git a/Services/Compositor/DisplayListRasterCache.h b/Services/Compositor/DisplayListRasterCache.h
index 74d4e37e54b..f97ed8d4ac1 100644
--- a/Services/Compositor/DisplayListRasterCache.h
+++ b/Services/Compositor/DisplayListRasterCache.h
@@ -21,6 +21,7 @@
#include <LibGfx/Size.h>
class SkImage;
+class SkPath;
class SkTextBlob;
template<typename T>
@@ -76,12 +77,19 @@ public:
sk_sp<SkImage> create_nested_display_list_raster(Compositing::DisplayListResourceId, RefPtr<Gfx::SkiaBackendContext> const&, Gfx::IntRect rect_in_list_space, Function<sk_sp<SkImage>()> const& rasterize);
sk_sp<SkTextBlob> text_blob(Compositing::DisplayListResourceStorage const&, Compositing::FontResourceId, float scale, ReadonlySpan<Compositing::DisplayListGlyph>, u8 font_smoothing);
+ // The Skia path for a path that stays the same for as long as it has this identity. Keeping one Skia path lets
+ // the caches that are keyed on it work.
+ SkPath clip_path(u64 path_identity, Gfx::Path const&);
+ // Drops the clip paths that were not used since the last call.
+ void drop_clip_paths_not_used_since_last_frame();
+
private:
struct ImageFrameImage;
struct VideoSinkImage;
struct RepeatedTileRaster;
struct NestedDisplayListRasters;
struct TextBlob;
+ struct ClipPath;
HashMap<u64, NonnullOwnPtr<ImageFrameImage>> m_image_frame_images;
HashMap<u64, NonnullOwnPtr<VideoSinkImage>> m_video_sink_images;
@@ -92,6 +100,8 @@ private:
HashMap<TextBlobCacheKey, NonnullOwnPtr<TextBlob>> m_text_blobs;
size_t m_text_blob_cache_bytes { 0 };
MonotonicTime m_text_blob_cache_sweep_time { MonotonicTime::now() };
+ HashMap<u64, NonnullOwnPtr<ClipPath>> m_clip_paths;
+ HashMap<u64, NonnullOwnPtr<ClipPath>> m_clip_paths_from_last_frame;
};
}