Truly independent web browser
Compositor: Avoid repeated texture uploads in 2D canvas draws
Callum Law9c4a33296e8d
diff --git a/Libraries/LibGfx/DecodedImageFrame.h b/Libraries/LibGfx/DecodedImageFrame.hindex 25d2059e298..bac5eb2dc94 100644--- a/Libraries/LibGfx/DecodedImageFrame.h+++ b/Libraries/LibGfx/DecodedImageFrame.h@@ -17,6 +17,7 @@#include <LibGfx/Bitmap.h>#include <LibGfx/ColorSpace.h>#include <LibGfx/Forward.h>+#include <LibGfx/RasterizerData.h>#include <LibGfx/Rect.h>#include <LibIPC/Forward.h>@@ -26,6 +27,8 @@namespace Gfx {+struct DecodedImageFrameRasterizerData;+class DecodedImageFrame final {public:AK_ALLOC_WITH_KMALLOC;@@ -43,8 +46,11 @@ public:u64 id() const { return m_id; }+ auto const& rasterizer_data() const { return m_rasterizer_data; }+private:u64 m_id { 0 };+ RasterizerDataSlot<DecodedImageFrameRasterizerData> m_rasterizer_data;};using IdentityDestructionObserver = void (*)(SharedIdentity const&);diff --git a/Libraries/LibGfx/RasterizerData.h b/Libraries/LibGfx/RasterizerData.hindex c4b0ee96bb2..bb9ede725a1 100644--- a/Libraries/LibGfx/RasterizerData.h+++ b/Libraries/LibGfx/RasterizerData.h@@ -15,7 +15,7 @@namespace Gfx {-// What a rasterizer keeps with a typeface or a font, for as long as the typeface or the font lives.+// What a rasterizer keeps with a font, typeface, or decoded image frame for its lifetime.class RasterizerData {AK_ALLOC_WITH_KMALLOC;diff --git a/Services/Compositor/Canvas2DCommandPlayer.cpp b/Services/Compositor/Canvas2DCommandPlayer.cppindex 642fefe5681..cc967fa5931 100644--- a/Services/Compositor/Canvas2DCommandPlayer.cpp+++ b/Services/Compositor/Canvas2DCommandPlayer.cpp@@ -33,6 +33,12 @@#include <effects/SkGradient.h>#include <effects/SkImageFilters.h>+// NB: GPU canvases share the compositor's Skia context.+struct Gfx::DecodedImageFrameRasterizerData final : public Gfx::RasterizerData {+ sk_sp<SkImage> raster_image;+ sk_sp<SkImage> gpu_image;+};+namespace Compositor {using namespace Compositing;@@ -55,11 +61,21 @@ Canvas2DCommandPlayer::~Canvas2DCommandPlayer() = default;sk_sp<SkImage> Canvas2DCommandPlayer::image_of_frame(Gfx::DecodedImageFrame const& frame) const{- if (auto skia_backend_context = m_surface->skia_backend_context()) {- if (auto image = Gfx::sk_texture_image_sharing_pixels_of_decoded_image_frame(frame, *skia_backend_context))- return image;+ auto& images = frame.identity().rasterizer_data().get([] { return make<Gfx::DecodedImageFrameRasterizerData>(); });+ auto skia_backend_context = m_surface->skia_backend_context();+ auto& cached_image = skia_backend_context ? images.gpu_image : images.raster_image;+ if (cached_image)+ return cached_image;++ if (skia_backend_context) {+ if (auto texture_image = Gfx::sk_texture_image_sharing_pixels_of_decoded_image_frame(frame, *skia_backend_context)) {+ cached_image = texture_image;+ return texture_image;+ }}- return Gfx::sk_image_from_decoded_image_frame(frame);++ cached_image = Gfx::sk_image_adopting_bitmap(frame.bitmap_sharing_pixels(), frame.color_space());+ return cached_image;}NonnullRefPtr<Gfx::PaintingSurface> Canvas2DCommandPlayer::surface() constdiff --git a/Services/Compositor/SkiaUtils.cpp b/Services/Compositor/SkiaUtils.cppindex a4ee16e5b9b..b3046ffde05 100644--- a/Services/Compositor/SkiaUtils.cpp+++ b/Services/Compositor/SkiaUtils.cpp@@ -535,13 +535,13 @@ sk_sp<SkImage> sk_texture_image_sharing_pixels_of_decoded_image_frame([[maybe_un#endif}-sk_sp<SkImage> sk_image_adopting_bitmap(NonnullRefPtr<Bitmap> bitmap, ColorSpace const& color_space)+sk_sp<SkImage> sk_image_adopting_bitmap(NonnullRefPtr<Bitmap const> bitmap, ColorSpace const& color_space){auto info = SkImageInfo::Make(bitmap->width(), bitmap->height(), to_skia_color_type(bitmap->format()), to_skia_alpha_type(bitmap->format(), bitmap->alpha_type()), to_skia_color_space(color_space));auto row_bytes = bitmap->pitch();auto byte_count = bitmap->size_in_bytes();auto* pixels = bitmap->scanline_u8(0);- auto data = SkData::MakeWithProc(pixels, byte_count, [](void const*, void* context) { static_cast<Bitmap*>(context)->unref(); }, &bitmap.leak_ref());+ auto data = SkData::MakeWithProc(pixels, byte_count, [](void const*, void* context) { static_cast<Bitmap const*>(context)->unref(); }, const_cast<Bitmap*>(&bitmap.leak_ref()));return SkImages::RasterFromData(info, move(data), row_bytes);}diff --git a/Services/Compositor/SkiaUtils.h b/Services/Compositor/SkiaUtils.hindex 7c9452a645f..548961d46cc 100644--- a/Services/Compositor/SkiaUtils.h+++ b/Services/Compositor/SkiaUtils.h@@ -166,6 +166,6 @@ sk_sp<SkImage> sk_texture_image_sharing_pixels_of_decoded_image_frame(DecodedIma// Hands the source bitmap's pixel storage to the returned SkImage without copying: the image// holds a reference to the bitmap and drops it on destruction.-sk_sp<SkImage> sk_image_adopting_bitmap(NonnullRefPtr<Bitmap> bitmap, ColorSpace const& color_space);+sk_sp<SkImage> sk_image_adopting_bitmap(NonnullRefPtr<Bitmap const> bitmap, ColorSpace const& color_space);}diff --git a/Tests/Compositor/CMakeLists.txt b/Tests/Compositor/CMakeLists.txtindex 3e611e13388..2fb2e35d43c 100644--- a/Tests/Compositor/CMakeLists.txt+++ b/Tests/Compositor/CMakeLists.txt@@ -2,6 +2,7 @@ ladybird_test_suite(Compositor)set(TEST_SOURCESTestBitmapExport.cpp+ TestCanvas2DCommandPlayer.cppTestCanvasSurfaceRegistry.cppTestVideoFrameHolds.cppTestVSyncScheduler.cppdiff --git a/Tests/Compositor/TestCanvas2DCommandPlayer.cpp b/Tests/Compositor/TestCanvas2DCommandPlayer.cppnew file mode 100644index 00000000000..7a4b1199224--- /dev/null+++ b/Tests/Compositor/TestCanvas2DCommandPlayer.cpp@@ -0,0 +1,43 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <Compositor/Canvas2DCommandPlayer.h>+#include <Compositor/SkiaBackendContext.h>+#include <LibGfx/Bitmap.h>+#include <LibGfx/DecodedImageFrame.h>+#include <LibTest/TestCase.h>++TEST_CASE(drawing_strips_of_one_image_reuses_its_gpu_texture)+{+ auto context = Gfx::SkiaBackendContext::create_independent_gpu_backend();+ if (!context) {+ warnln("No GPU backend available, skipping");+ return;+ }++ auto source = MUST(Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 512, 512 }));+ auto frame = MUST(Gfx::DecodedImageFrame::create(*source));+ Compositor::Canvas2DCommandPlayer::Resolvers resolvers;+ resolvers.image_frame = [&](u64) { return &frame; };+ auto player = MUST(Compositor::Canvas2DCommandPlayer::create(context, { 2, 1 }, frame.format(), frame.alpha_type(), {}, move(resolvers)));++ Compositing::Canvas2DDrawImageFrame draw {};+ draw.frame_id = Compositing::ImageFrameResourceId { frame.id() };+ draw.scaling_mode = Gfx::ScalingMode::BilinearMipmap;+ draw.compositing.global_alpha = 1;+ draw.compositing.compositing_and_blending_operator = Gfx::CompositingAndBlendingOperator::SourceOver;+ auto cache_bytes_after_drawing_strip = [&](float x) {+ draw.src_rect = { x, 0, 1, static_cast<float>(frame.height()) };+ draw.dst_rect = { x, 0, 1, 1 };+ player->play_command(draw.command_type, { &draw, sizeof(draw) });+ return context->resource_cache_bytes();+ };++ auto before = context->resource_cache_bytes();+ auto after_first_strip = cache_bytes_after_drawing_strip(0);+ EXPECT(after_first_strip > before);+ EXPECT(cache_bytes_after_drawing_strip(1) < after_first_strip + frame.size_in_bytes());+}