Truly independent web browser
Compositor: Scope image filter caches to their resource storage
Andreas Klingba8c812a7203
diff --git a/Libraries/LibCompositing/CMakeLists.txt b/Libraries/LibCompositing/CMakeLists.txtindex 563c4d6b8a2..fce33010437 100644--- a/Libraries/LibCompositing/CMakeLists.txt+++ b/Libraries/LibCompositing/CMakeLists.txt@@ -60,6 +60,7 @@ import_rust_crate(CRATE_NAME libcompositing_rustFEATURES allocatorFFI_HEADERS+ TestBuilderRustFFI.hRustFFI.hTypesRustFFI.hDisplayList/DisplayListCommandsGenerated.hdiff --git a/Libraries/LibCompositing/DisplayList/VisualContextTreeTestBuilder.cpp b/Libraries/LibCompositing/DisplayList/VisualContextTreeTestBuilder.cppindex df3ee567772..e9e4ca32e39 100644--- a/Libraries/LibCompositing/DisplayList/VisualContextTreeTestBuilder.cpp+++ b/Libraries/LibCompositing/DisplayList/VisualContextTreeTestBuilder.cpp@@ -4,8 +4,10 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <LibCompositing/DisplayList/DisplayList.h>#include <LibCompositing/DisplayList/VisualContextTreeTestBuilder.h>#include <LibCompositing/RustFFI.h>+#include <LibCompositing/TestBuilderRustFFI.h>namespace Compositing {@@ -53,6 +55,16 @@ ClipNodeIndex VisualContextTreeTestBuilder::append_clip(ClipNodeIndex parent, Spreturn ClipNodeIndex { Compositing::RustFFI::visual_context_tree_test_builder_append_clip(m_builder, parent.value(), spatial.value(), rect, corner_radii, mode) };}+EffectNodeIndex VisualContextTreeTestBuilder::append_image_filter(u64 image_id, Gfx::IntRect rect)+{+ return EffectNodeIndex { RustFFI::visual_context_tree_test_builder_append_image_filter(m_builder, image_id, rect) };+}++NonnullRefPtr<DisplayList> VisualContextTreeTestBuilder::filled_rect_display_list(AccumulatedVisualContextTree const& tree, ContextRef context, Gfx::IntRect rect, Gfx::Color color)+{+ return DisplayList::adopt_rust_command_storage(tree, RustFFI::visual_context_tree_test_builder_filled_rect(rect, color, context));+}+AccumulatedVisualContextTree VisualContextTreeTestBuilder::finish(){VERIFY(m_builder);diff --git a/Libraries/LibCompositing/DisplayList/VisualContextTreeTestBuilder.h b/Libraries/LibCompositing/DisplayList/VisualContextTreeTestBuilder.hindex 96ff78b66f8..15996f18d1b 100644--- a/Libraries/LibCompositing/DisplayList/VisualContextTreeTestBuilder.h+++ b/Libraries/LibCompositing/DisplayList/VisualContextTreeTestBuilder.h@@ -11,6 +11,7 @@#include <LibCompositing/DisplayList/ContextRef.h>#include <LibCompositing/DisplayList/DisplayListCommand.h>#include <LibCompositing/Export.h>+#include <LibGfx/Color.h>#include <LibGfx/CornerRadii.h>#include <LibGfx/Forward.h>#include <LibGfx/Matrix4x4.h>@@ -49,6 +50,9 @@ public:SpatialNodeIndex append_sticky(SpatialNodeIndex parent, StickyConstraints const&);ClipNodeIndex append_clip(ClipNodeIndex parent, SpatialNodeIndex spatial, Gfx::FloatRect, Gfx::CornerRadii = {}, ClipMode = ClipMode::Intersect);+ EffectNodeIndex append_image_filter(u64 image_id, Gfx::IntRect);+ static NonnullRefPtr<DisplayList> filled_rect_display_list(AccumulatedVisualContextTree const&, ContextRef, Gfx::IntRect, Gfx::Color);+AccumulatedVisualContextTree finish();private:diff --git a/Libraries/LibCompositing/Rust/build.rs b/Libraries/LibCompositing/Rust/build.rsindex 0f091d5ead2..c673197668a 100644--- a/Libraries/LibCompositing/Rust/build.rs+++ b/Libraries/LibCompositing/Rust/build.rs@@ -298,6 +298,14 @@ fn main() -> Result<(), Box<dyn Error>> {].map(String::from).to_vec();+ let mut test_builder_config = ffi_config.clone();+ test_builder_config.export.include.clear();+ generate_ffi_header(+ test_builder_config,+ &[manifest_dir.join("src/test_builder.rs")],+ &out_dir,+ Path::new("TestBuilderRustFFI.h"),+ );generate_ffi_header(ffi_config,&[diff --git a/Libraries/LibCompositing/Rust/src/ffi.rs b/Libraries/LibCompositing/Rust/src/ffi.rsindex 7b860255603..d50bd611f1a 100644--- a/Libraries/LibCompositing/Rust/src/ffi.rs+++ b/Libraries/LibCompositing/Rust/src/ffi.rs@@ -596,7 +596,7 @@ pub unsafe extern "C" fn visual_context_tree_test_builder_create() -> *mut c_voi/// # Safety////// `builder` must be a live handle from `visual_context_tree_test_builder_create`.-unsafe fn test_builder_tree<'a>(builder: *mut c_void) -> &'a mut crate::visual_context::VisualContextTree {+pub(super) unsafe fn test_builder_tree<'a>(builder: *mut c_void) -> &'a mut crate::visual_context::VisualContextTree {// SAFETY: The caller guarantees `builder` is the live boxed tree the create call returned.unsafe { &mut *builder.cast::<crate::visual_context::VisualContextTree>() }}diff --git a/Libraries/LibCompositing/Rust/src/lib.rs b/Libraries/LibCompositing/Rust/src/lib.rsindex 07456d4b022..bad580ad4b2 100644--- a/Libraries/LibCompositing/Rust/src/lib.rs+++ b/Libraries/LibCompositing/Rust/src/lib.rs@@ -26,3 +26,5 @@ pub mod node_slot_id;mod test_stubs;pub mod visual_animation;pub mod visual_context;++mod test_builder;diff --git a/Libraries/LibCompositing/Rust/src/test_builder.rs b/Libraries/LibCompositing/Rust/src/test_builder.rsnew file mode 100644index 00000000000..5235d3854dc--- /dev/null+++ b/Libraries/LibCompositing/Rust/src/test_builder.rs@@ -0,0 +1,62 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++use crate::display_list::commands::{ClipNodeIndex, ContextRef, EffectNodeIndex};+use crate::ffi::test_builder_tree;+use std::ffi::c_void;++/// # Safety+///+/// `builder` must be a live handle from `visual_context_tree_test_builder_create`.+#[unsafe(no_mangle)]+pub unsafe extern "C" fn visual_context_tree_test_builder_append_image_filter(+ builder: *mut c_void,+ image_id: u64,+ rect: libgfx_rust::IntRect,+) -> u32 {+ let tree = unsafe { test_builder_tree(builder) };+ let filter = libgfx_rust::filter::Filter::Image {+ frame_id: image_id,+ src_rect: rect,+ dest_rect: rect,+ scaling_mode: libgfx_rust::ScalingMode::NearestNeighbor,+ };+ tree.append_effect(+ crate::visual_context::EffectNodeData::Effects(crate::visual_context::EffectsData {+ isolation: false,+ opacity: 1.0,+ blend_mode: libgfx_rust::CompositingAndBlendingOperator::Normal,+ filter: Some(std::sync::Arc::new(filter.serialize())),+ backdrop_filter: None,+ }),+ EffectNodeIndex::NONE,+ crate::display_list::commands::VISUAL_VIEWPORT_NODE_INDEX,+ ClipNodeIndex::NONE,+ )+ .0+}++/// Records a rectangle with the same canonical builder as production recordings. Returns one+/// owned Arc reference consumed by `display_list_storage_adopt_recorded`.+#[unsafe(no_mangle)]+pub extern "C" fn visual_context_tree_test_builder_filled_rect(+ rect: libgfx_rust::IntRect,+ color: libgfx_rust::Color,+ context: ContextRef,+) -> *const c_void {+ let mut builder = crate::display_list::builder::DisplayListBuilder::new();+ builder.append(+ &crate::display_list::commands::FillRect {+ rect,+ color,+ compositing_and_blending_operator: libgfx_rust::CompositingAndBlendingOperator::Normal,+ background_color_animation_effect: EffectNodeIndex::NONE,+ },+ &[],+ context,+ );+ std::sync::Arc::into_raw(std::sync::Arc::new(builder.finish())).cast()+}diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cppindex a072ae2140c..f36dc7a8901 100644--- a/Services/Compositor/DisplayListPlayerSkia.cpp+++ b/Services/Compositor/DisplayListPlayerSkia.cpp@@ -47,25 +47,6 @@ namespace Compositor {using namespace Compositing;-struct DisplayListPlayerSkia::LayerImageFilterCache {- AK_ALLOC_WITH_KMALLOC;-- struct Entry {- ByteBuffer filter_bytes;- sk_sp<SkImageFilter> image_filter;- };- struct BackdropEntry {- ByteBuffer filter_bytes;- Gfx::IntRect region;- Gfx::CornerRadii corner_radii;- bool limited_to_region { false };- sk_sp<SkImageFilter> image_filter;- };- HashMap<u64, HashMap<u32, Entry>> entries_by_tree_structural_epoch_and_effect;- HashMap<u64, HashMap<u32, BackdropEntry>> backdrop_entries_by_tree_structural_epoch_and_effect;- u64 tree_structural_epoch { 0 };-};-DisplayListPlayerSkia::DisplayListPlayerSkia(): DisplayListPlayerSkia(Gfx::SkiaBackendContext::the_main_thread_context()){@@ -73,7 +54,6 @@ DisplayListPlayerSkia::DisplayListPlayerSkia()DisplayListPlayerSkia::DisplayListPlayerSkia(RefPtr<Gfx::SkiaBackendContext> skia_backend_context): m_skia_backend_context(move(skia_backend_context))- , m_layer_image_filter_cache(make<LayerImageFilterCache>()){}@@ -96,11 +76,6 @@ void DisplayListPlayerSkia::execute(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();- m_layer_image_filter_cache->backdrop_entries_by_tree_structural_epoch_and_effect.clear();- m_layer_image_filter_cache->tree_structural_epoch = visual_context_tree.structural_epoch();- }DisplayListPlayer::execute(display_list,visual_context_tree,@@ -1583,42 +1558,31 @@ void DisplayListPlayerSkia::push_layer(ReplayLayer const& layer)sk_sp<SkImageFilter> DisplayListPlayerSkia::backdrop_image_filter(ReplayLayer const& layer, bool limited_to_region){ReadonlyBytes filter_bytes { layer.backdrop_filter_bytes, layer.backdrop_filter_bytes_size };- auto& entries_by_effect = m_layer_image_filter_cache->backdrop_entries_by_tree_structural_epoch_and_effect.ensure(active_visual_context_tree().structural_epoch());- if (auto cached = entries_by_effect.get(layer.effect.value());- cached.has_value()- && cached->filter_bytes.bytes() == filter_bytes- && cached->region == layer.backdrop_region- && cached->corner_radii == layer.backdrop_corner_radii- && cached->limited_to_region == limited_to_region)- return cached->image_filter;-- auto image_filter = image_filter_from_bytes(filter_bytes);- if (limited_to_region) {- auto region = to_skia_rect(layer.backdrop_region);- image_filter = SkImageFilters::Crop(region, SkTileMode::kDecal, move(image_filter));- if (layer.backdrop_corner_radii.has_any_radius()) {- SkPictureRecorder recorder;- auto* picture_canvas = recorder.beginRecording(region);- SkPaint mask_paint;- mask_paint.setAntiAlias(true);- mask_paint.setColor(SK_ColorWHITE);- picture_canvas->drawRRect(to_skia_rrect(layer.backdrop_region, layer.backdrop_corner_radii), mask_paint);- image_filter = SkImageFilters::Blend(SkBlendMode::kDstIn, move(image_filter), SkImageFilters::Picture(recorder.finishRecordingAsPicture()));+ return raster_cache().backdrop_image_filter(active_visual_context_tree().structural_epoch(), layer, limited_to_region, [&] {+ auto image_filter = image_filter_from_bytes(filter_bytes);+ if (limited_to_region) {+ auto region = to_skia_rect(layer.backdrop_region);+ image_filter = SkImageFilters::Crop(region, SkTileMode::kDecal, move(image_filter));+ if (layer.backdrop_corner_radii.has_any_radius()) {+ SkPictureRecorder recorder;+ auto* picture_canvas = recorder.beginRecording(region);+ SkPaint mask_paint;+ mask_paint.setAntiAlias(true);+ mask_paint.setColor(SK_ColorWHITE);+ picture_canvas->drawRRect(to_skia_rrect(layer.backdrop_region, layer.backdrop_corner_radii), mask_paint);+ image_filter = SkImageFilters::Blend(SkBlendMode::kDstIn, move(image_filter), SkImageFilters::Picture(recorder.finishRecordingAsPicture()));+ }}- }- entries_by_effect.set(layer.effect.value(), LayerImageFilterCache::BackdropEntry { MUST(ByteBuffer::copy(filter_bytes)), layer.backdrop_region, layer.backdrop_corner_radii, limited_to_region, image_filter });- return image_filter;+ return image_filter;+ });}sk_sp<SkImageFilter> DisplayListPlayerSkia::layer_image_filter(ReplayLayer const& layer){ReadonlyBytes filter_bytes { layer.filter_bytes, layer.filter_bytes_size };- auto& entries_by_effect = m_layer_image_filter_cache->entries_by_tree_structural_epoch_and_effect.ensure(active_visual_context_tree().structural_epoch());- if (auto cached = entries_by_effect.get(layer.effect.value()); cached.has_value() && cached->filter_bytes.bytes() == filter_bytes)- return cached->image_filter;- auto image_filter = image_filter_from_bytes(filter_bytes);- entries_by_effect.set(layer.effect.value(), LayerImageFilterCache::Entry { MUST(ByteBuffer::copy(filter_bytes)), image_filter });- return image_filter;+ return raster_cache().layer_image_filter(active_visual_context_tree().structural_epoch(), layer, [&] {+ return image_filter_from_bytes(filter_bytes);+ });}sk_sp<SkImageFilter> DisplayListPlayerSkia::image_filter_from_bytes(ReadonlyBytes filter_bytes)diff --git a/Services/Compositor/DisplayListPlayerSkia.h b/Services/Compositor/DisplayListPlayerSkia.hindex 5c603a8bd1d..34234eba3ae 100644--- a/Services/Compositor/DisplayListPlayerSkia.h+++ b/Services/Compositor/DisplayListPlayerSkia.h@@ -92,11 +92,6 @@ private:ScrollbarController const* m_scrollbar_controller { nullptr };DisplayListRasterCache* m_raster_cache { nullptr };DisplayListRasterCache& raster_cache() const { return *m_raster_cache; }-- // Layer filters are built from their bytes once per frame node and kept until the visual- // context tree's structure changes, so a replayed frame does not rebuild them.- struct LayerImageFilterCache;- NonnullOwnPtr<LayerImageFilterCache> m_layer_image_filter_cache;};}diff --git a/Services/Compositor/DisplayListRasterCache.cpp b/Services/Compositor/DisplayListRasterCache.cppindex 44180cddbca..f7413350983 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/SkImageFilter.h>#include <core/SkPath.h>#include <core/SkTextBlob.h>#include <core/SkYUVAPixmaps.h>@@ -34,6 +35,25 @@ namespace Compositor {using namespace Compositing;+struct DisplayListRasterCache::LayerImageFilterCache {+ AK_ALLOC_WITH_KMALLOC;++ struct Entry {+ ByteBuffer filter_bytes;+ sk_sp<SkImageFilter> image_filter;+ };+ struct BackdropEntry {+ ByteBuffer filter_bytes;+ Gfx::IntRect region;+ Gfx::CornerRadii corner_radii;+ bool limited_to_region { false };+ sk_sp<SkImageFilter> image_filter;+ };+ HashMap<u64, HashMap<u32, Entry>> entries_by_tree_structural_epoch_and_effect;+ HashMap<u64, HashMap<u32, BackdropEntry>> backdrop_entries_by_tree_structural_epoch_and_effect;+ u64 tree_structural_epoch { 0 };+};+struct DisplayListRasterCache::ImageFrameImage {AK_ALLOC_WITH_KMALLOC;@@ -147,6 +167,9 @@ DisplayListRasterCache::~DisplayListRasterCache() = default;void DisplayListRasterCache::evict(DisplayListResourceSet const& removed_resources){+ // Filters retain images resolved from this storage, including resources whose IDs were replaced.+ if (!removed_resources.image_frames.is_empty())+ m_layer_image_filter_cache = nullptr;for (auto id : removed_resources.image_frames)m_image_frame_images.remove(id.value());for (auto id : removed_resources.video_sinks)@@ -163,6 +186,41 @@ void DisplayListRasterCache::evict(DisplayListResourceSet const& removed_resourc}}+DisplayListRasterCache::LayerImageFilterCache& DisplayListRasterCache::ensure_layer_image_filter_cache(u64 tree_structural_epoch)+{+ if (!m_layer_image_filter_cache || m_layer_image_filter_cache->tree_structural_epoch != tree_structural_epoch) {+ m_layer_image_filter_cache = make<LayerImageFilterCache>();+ m_layer_image_filter_cache->tree_structural_epoch = tree_structural_epoch;+ }+ return *m_layer_image_filter_cache;+}++sk_sp<SkImageFilter> DisplayListRasterCache::layer_image_filter(u64 tree_structural_epoch, ReplayLayer const& layer, Function<sk_sp<SkImageFilter>()> const& create)+{+ ReadonlyBytes filter_bytes { layer.filter_bytes, layer.filter_bytes_size };+ auto& entries = ensure_layer_image_filter_cache(tree_structural_epoch).entries_by_tree_structural_epoch_and_effect.ensure(tree_structural_epoch);+ if (auto cached = entries.get(layer.effect.value()); cached.has_value() && cached->filter_bytes.bytes() == filter_bytes)+ return cached->image_filter;+ auto image_filter = create();+ entries.set(layer.effect.value(), LayerImageFilterCache::Entry { MUST(ByteBuffer::copy(filter_bytes)), image_filter });+ return image_filter;+}++sk_sp<SkImageFilter> DisplayListRasterCache::backdrop_image_filter(u64 tree_structural_epoch, ReplayLayer const& layer, bool limited_to_region, Function<sk_sp<SkImageFilter>()> const& create)+{+ ReadonlyBytes filter_bytes { layer.backdrop_filter_bytes, layer.backdrop_filter_bytes_size };+ auto& entries = ensure_layer_image_filter_cache(tree_structural_epoch).backdrop_entries_by_tree_structural_epoch_and_effect.ensure(tree_structural_epoch);+ if (auto cached = entries.get(layer.effect.value()); cached.has_value()+ && cached->filter_bytes.bytes() == filter_bytes+ && cached->region == layer.backdrop_region+ && cached->corner_radii == layer.backdrop_corner_radii+ && cached->limited_to_region == limited_to_region)+ return cached->image_filter;+ auto image_filter = create();+ entries.set(layer.effect.value(), LayerImageFilterCache::BackdropEntry { MUST(ByteBuffer::copy(filter_bytes)), layer.backdrop_region, layer.backdrop_corner_radii, limited_to_region, image_filter });+ return image_filter;+}+static sk_sp<SkImage> create_skia_image(Gfx::DecodedImageFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){auto raster_image = Gfx::sk_image_from_bitmap(frame.bitmap(), frame.color_space());diff --git a/Services/Compositor/DisplayListRasterCache.h b/Services/Compositor/DisplayListRasterCache.hindex 0d040388fc0..9a3d7984ddd 100644--- a/Services/Compositor/DisplayListRasterCache.h+++ b/Services/Compositor/DisplayListRasterCache.h@@ -10,6 +10,7 @@#include <AK/HashMap.h>#include <AK/Noncopyable.h>#include <AK/NonnullOwnPtr.h>+#include <AK/OwnPtr.h>#include <AK/RefPtr.h>#include <AK/Span.h>#include <AK/Time.h>@@ -21,12 +22,19 @@#include <LibGfx/Size.h>class SkImage;+class SkImageFilter;class SkPath;class SkTextBlob;template<typename T>class sk_sp;+namespace Compositing {++struct ReplayLayer;++}+namespace Compositor {struct TextBlobCacheKey {@@ -77,6 +85,9 @@ 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);+ sk_sp<SkImageFilter> layer_image_filter(u64 tree_structural_epoch, Compositing::ReplayLayer const&, Function<sk_sp<SkImageFilter>()> const& create);+ sk_sp<SkImageFilter> backdrop_image_filter(u64 tree_structural_epoch, Compositing::ReplayLayer const&, bool limited_to_region, Function<sk_sp<SkImageFilter>()> const& create);+// 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::PathData const&);@@ -90,6 +101,10 @@ private:struct NestedDisplayListRasters;struct TextBlob;struct ClipPath;+ struct LayerImageFilterCache;+ LayerImageFilterCache& ensure_layer_image_filter_cache(u64 tree_structural_epoch);++ OwnPtr<LayerImageFilterCache> m_layer_image_filter_cache;HashMap<u64, NonnullOwnPtr<ImageFrameImage>> m_image_frame_images;HashMap<u64, NonnullOwnPtr<VideoSinkImage>> m_video_sink_images;diff --git a/Tests/Compositor/CMakeLists.txt b/Tests/Compositor/CMakeLists.txtindex 0b2172e2e85..52cfd5c5e2b 100644--- a/Tests/Compositor/CMakeLists.txt+++ b/Tests/Compositor/CMakeLists.txt@@ -12,7 +12,7 @@ foreach(source IN LISTS TEST_SOURCES)endforeach()# These drive Skia directly, and the compositor keeps its Skia dependency private.-foreach(source IN ITEMS BenchmarkForceDarkImageFilter.cpp TestForceDarkImageFilter.cpp TestPaintingSurface.cpp TestYUVData.cpp)+foreach(source IN ITEMS TestDisplayListImageFilters.cpp BenchmarkForceDarkImageFilter.cpp TestForceDarkImageFilter.cpp TestPaintingSurface.cpp TestYUVData.cpp)ladybird_test("${source}" Compositor LIBS compositorservice LibCompositing LibGfx skia)endforeach()diff --git a/Tests/Compositor/TestDisplayListImageFilters.cpp b/Tests/Compositor/TestDisplayListImageFilters.cppnew file mode 100644index 00000000000..84952932dd1--- /dev/null+++ b/Tests/Compositor/TestDisplayListImageFilters.cpp@@ -0,0 +1,92 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <Compositor/DisplayListPlayerSkia.h>+#include <Compositor/DisplayListRasterCache.h>+#include <Compositor/PaintingSurface.h>+#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>+#include <LibCompositing/DisplayList/VisualContextTreeTestBuilder.h>+#include <LibGfx/Bitmap.h>+#include <LibTest/TestCase.h>+#include <new>++static Compositing::ImageFrameResourceId const image_id { 1 };+static Gfx::IntRect const rect { 0, 0, 1, 1 };++static NonnullRefPtr<Gfx::Bitmap> replace_image(Compositing::DisplayListResourceStorage& storage, Compositor::DisplayListRasterCache& cache, Gfx::Color color)+{+ auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, rect.size()));+ bitmap->set_pixel(0, 0, color);+ Compositing::DisplayListResourceTransaction transaction;+ transaction.image_frames.append({ image_id, Gfx::DecodedImageFrame { *bitmap } });+ cache.evict(storage.apply_transaction(move(transaction)));+ return bitmap;+}++static Gfx::Color render(Compositor::DisplayListPlayerSkia& player, Compositing::DisplayList const& list, Compositing::AccumulatedVisualContextTree const& tree, Compositing::DisplayListResourceStorage const& storage, Compositor::DisplayListRasterCache& cache)+{+ auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, rect.size()));+ auto surface = Gfx::PaintingSurface::wrap_bitmap(*bitmap);+ player.execute(list, tree, storage, cache, {}, surface, nullptr, nullptr);+ player.flush(*surface);+ return bitmap->get_pixel(0, 0);+}++TEST_CASE(image_filters_use_the_current_resource_storage)+{+ Compositing::VisualContextTreeTestBuilder builder;+ auto effect = builder.append_image_filter(image_id.value(), rect);+ auto tree = builder.finish();+ auto list = Compositing::VisualContextTreeTestBuilder::filled_rect_display_list(tree, { .effect = effect }, rect, Gfx::Color::White);+ Compositor::DisplayListPlayerSkia player { nullptr };+ Compositing::DisplayListResourceStorage blue_storage;+ Compositing::DisplayListResourceStorage green_storage;+ Compositor::DisplayListRasterCache blue_cache;+ Compositor::DisplayListRasterCache green_cache;+ (void)replace_image(blue_storage, blue_cache, Gfx::Color::Blue);+ (void)replace_image(green_storage, green_cache, Gfx::Color::Green);++ EXPECT_EQ(render(player, *list, tree, blue_storage, blue_cache), Gfx::Color::Blue);+ EXPECT_EQ(render(player, *list, tree, green_storage, green_cache), Gfx::Color::Green);+ EXPECT_EQ(render(player, *list, tree, blue_storage, blue_cache), Gfx::Color::Blue);+}++TEST_CASE(image_filters_drop_replaced_image_resources)+{+ Compositing::VisualContextTreeTestBuilder builder;+ auto effect = builder.append_image_filter(image_id.value(), rect);+ auto tree = builder.finish();+ auto list = Compositing::VisualContextTreeTestBuilder::filled_rect_display_list(tree, { .effect = effect }, rect, Gfx::Color::White);+ Compositor::DisplayListPlayerSkia player { nullptr };+ Compositing::DisplayListResourceStorage storage;+ Compositor::DisplayListRasterCache cache;+ auto original_bitmap = replace_image(storage, cache, Gfx::Color::Blue);+ EXPECT_EQ(render(player, *list, tree, storage, cache), Gfx::Color::Blue);+ (void)replace_image(storage, cache, Gfx::Color::Green);+ EXPECT_EQ(render(player, *list, tree, storage, cache), Gfx::Color::Green);+ EXPECT_EQ(original_bitmap->get_pixel(0, 0), Gfx::Color::Blue);+}++TEST_CASE(image_filters_do_not_keep_destroyed_resource_namespaces)+{+ Compositing::VisualContextTreeTestBuilder builder;+ auto effect = builder.append_image_filter(image_id.value(), rect);+ auto tree = builder.finish();+ auto list = Compositing::VisualContextTreeTestBuilder::filled_rect_display_list(tree, { .effect = effect }, rect, Gfx::Color::White);+ Compositor::DisplayListPlayerSkia player { nullptr };+ struct ResourceNamespace {+ Compositing::DisplayListResourceStorage storage;+ Compositor::DisplayListRasterCache cache;+ };+ alignas(ResourceNamespace) u8 memory[sizeof(ResourceNamespace)];+ Vector<NonnullRefPtr<Gfx::Bitmap>> kept_images;+ for (auto color : { Gfx::Color::Blue, Gfx::Color::Green }) {+ auto* resources = new (memory) ResourceNamespace;+ kept_images.append(replace_image(resources->storage, resources->cache, color));+ EXPECT_EQ(render(player, *list, tree, resources->storage, resources->cache), color);+ resources->~ResourceNamespace();+ }+}