Truly independent web browser
LibGfx+Compositor: Draw canvas text without Skia's text blob cache
Aliaksandr Kalenikb1ffc14841d2
diff --git a/Libraries/LibGfx/CanvasCommandPlayer.cpp b/Libraries/LibGfx/CanvasCommandPlayer.cppindex a29241a430d..fa9dc57b6f7 100644--- a/Libraries/LibGfx/CanvasCommandPlayer.cpp+++ b/Libraries/LibGfx/CanvasCommandPlayer.cpp@@ -13,15 +13,14 @@#include <LibGfx/SkiaUtils.h>#include <core/SkCanvas.h>#include <core/SkPaint.h>-#include <core/SkTextBlob.h>namespace Gfx {-CanvasCommandPlayer::CanvasCommandPlayer(RefPtr<SkiaBackendContext> skia_backend_context, IntSize size, BitmapFormat format, AlphaType alpha_type, CanvasSurfaceResolver canvas_surface_resolver, TextBlobResolver text_blob_resolver)+CanvasCommandPlayer::CanvasCommandPlayer(RefPtr<SkiaBackendContext> skia_backend_context, IntSize size, BitmapFormat format, AlphaType alpha_type, CanvasSurfaceResolver canvas_surface_resolver, FontResolver font_resolver): m_surface(PaintingSurface::create_with_size(size, format, alpha_type, move(skia_backend_context))), m_painter(make<PainterSkia>(*m_surface)), m_canvas_surface_resolver(move(canvas_surface_resolver))- , m_text_blob_resolver(move(text_blob_resolver))+ , m_font_resolver(move(font_resolver)){}@@ -45,12 +44,12 @@ void CanvasCommandPlayer::play(CanvasCommandList const& command_list)void CanvasCommandPlayer::play_command(CanvasCommands::DrawGlyphRun const& command){- if (!m_text_blob_resolver)+ if (!m_font_resolver)return;- auto blob = m_text_blob_resolver(command.font_id, command.glyphs);- if (!blob)+ auto const* font = m_font_resolver(command.font_id);+ if (!font)return;- m_painter->draw_text_blob(*blob, command.translation, resolve_paint_style(command.style), command.filter, command.global_alpha, command.compositing_and_blending_operator);+ m_painter->draw_glyph_run(*font, command.glyphs, command.translation, resolve_paint_style(command.style), command.filter, command.global_alpha, command.compositing_and_blending_operator);}void CanvasCommandPlayer::play_command(CanvasCommands::ClearRect const& command)diff --git a/Libraries/LibGfx/CanvasCommandPlayer.h b/Libraries/LibGfx/CanvasCommandPlayer.hindex d92e557f7e2..da04aa58b34 100644--- a/Libraries/LibGfx/CanvasCommandPlayer.h+++ b/Libraries/LibGfx/CanvasCommandPlayer.h@@ -14,10 +14,6 @@#include <LibGfx/CanvasCommandList.h>#include <LibGfx/Forward.h>-class SkTextBlob;-template<typename T>-class sk_sp;-namespace Gfx {class CanvasCommandPlayer {@@ -27,10 +23,10 @@ class CanvasCommandPlayer {public:AK_ALLOC_WITH_KMALLOC;- using TextBlobResolver = Function<sk_sp<SkTextBlob>(u64, ReadonlySpan<CanvasGlyph>)>;+ using FontResolver = Function<Font const*(u64)>;using CanvasSurfaceResolver = Function<PaintingSurface const*(u64)>;- CanvasCommandPlayer(RefPtr<SkiaBackendContext>, IntSize, BitmapFormat, AlphaType, CanvasSurfaceResolver = {}, TextBlobResolver = {});+ CanvasCommandPlayer(RefPtr<SkiaBackendContext>, IntSize, BitmapFormat, AlphaType, CanvasSurfaceResolver = {}, FontResolver = {});~CanvasCommandPlayer();NonnullRefPtr<PaintingSurface> surface() const;@@ -59,7 +55,7 @@ private:NonnullRefPtr<PaintingSurface> m_surface;NonnullOwnPtr<PainterSkia> m_painter;CanvasSurfaceResolver m_canvas_surface_resolver;- TextBlobResolver m_text_blob_resolver;+ FontResolver m_font_resolver;};}diff --git a/Libraries/LibGfx/PainterSkia.cpp b/Libraries/LibGfx/PainterSkia.cppindex 99906d30f6e..2b06d2f8f39 100644--- a/Libraries/LibGfx/PainterSkia.cpp+++ b/Libraries/LibGfx/PainterSkia.cpp@@ -12,13 +12,16 @@#include <AK/GenericShorthands.h>#include <AK/String.h>#include <AK/TypeCasts.h>+#include <LibGfx/CanvasCommandList.h>#include <LibGfx/DecodedImageFrame.h>#include <LibGfx/Filter.h>+#include <LibGfx/Font/Font.h>#include <LibGfx/PainterSkia.h>#include <LibGfx/PathSkia.h>#include <LibGfx/SkiaUtils.h>#include <core/SkCanvas.h>+#include <core/SkFont.h>#include <core/SkImage.h>#include <core/SkPath.h>#include <effects/SkBlurMaskFilter.h>@@ -277,12 +280,35 @@ void PainterSkia::fill_path(Gfx::Path const& path, Gfx::PaintStyle const& paint_canvas.drawPath(sk_path, paint);}-void PainterSkia::draw_text_blob(SkTextBlob const& blob, FloatPoint translation, PaintStyle const& style, Optional<Filter> filter, float global_alpha, CompositingAndBlendingOperator compositing_and_blending_operator)+void PainterSkia::draw_glyph_run(Font const& font, ReadonlySpan<CanvasGlyph> glyphs, FloatPoint translation, PaintStyle const& style, Optional<Filter> filter, float global_alpha, CompositingAndBlendingOperator compositing_and_blending_operator){+ if (glyphs.is_empty() || font.is_invisible())+ return;++ auto sk_font = font.skia_font(1);+ // NB: Preserve canvas text's fractional baseline and unhinted outline geometry.+ sk_font.setHinting(SkFontHinting::kNone);+ sk_font.setForceAutoHinting(false);+ sk_font.setBaselineSnap(false);++ Vector<SkGlyphID, 32> glyph_ids;+ Vector<SkPoint, 32> glyph_positions;+ glyph_ids.ensure_capacity(glyphs.size());+ glyph_positions.ensure_capacity(glyphs.size());+ auto font_ascent = font.pixel_metrics().ascent;+ for (auto const& glyph : glyphs) {+ glyph_ids.unchecked_append(static_cast<SkGlyphID>(glyph.glyph_id));+ glyph_positions.unchecked_append({ glyph.position.x(), glyph.position.y() + font_ascent });+ }+auto paint = to_skia_paint(style, filter);paint.setAlphaf(paint.getAlphaf() * global_alpha);paint.setBlender(to_skia_blender(compositing_and_blending_operator));- m_painting_surface->canvas().drawTextBlob(&blob, translation.x(), translation.y(), paint);++ // NB: Not drawTextBlob(). Skia caches a drawn SkTextBlob once per subpixel origin, and finds a cached entry by+ // scanning every entry of that blob. Canvas text is often redrawn at moving fractional positions, which+ // piles up thousands of entries per blob and makes each draw scan all of them.+ m_painting_surface->canvas().drawGlyphs({ glyph_ids.data(), glyph_ids.size() }, { glyph_positions.data(), glyph_positions.size() }, to_skia_point(translation), sk_font, paint);}void PainterSkia::save()diff --git a/Libraries/LibGfx/PainterSkia.h b/Libraries/LibGfx/PainterSkia.hindex 2bd4f3cc8b2..17cbfc1da54 100644--- a/Libraries/LibGfx/PainterSkia.h+++ b/Libraries/LibGfx/PainterSkia.h@@ -8,18 +8,20 @@#include <AK/NonnullRefPtr.h>#include <AK/Optional.h>+#include <AK/Span.h>#include <LibGfx/CompositingAndBlendingOperator.h>#include <LibGfx/Filter.h>+#include <LibGfx/Forward.h>#include <LibGfx/PaintStyle.h>#include <LibGfx/PaintingSurface.h>#include <LibGfx/Path.h>#include <LibGfx/ScalingMode.h>#include <LibGfx/WindingRule.h>-class SkTextBlob;-namespace Gfx {+struct CanvasGlyph;+class PainterSkia final {public:AK_ALLOC_WITH_KMALLOC;@@ -34,7 +36,7 @@ public:void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional<Gfx::Filter>, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle const&, Gfx::Path::JoinStyle const&, float miter_limit, Vector<float> const&, float dash_offset);void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator);void fill_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional<Gfx::Filter>, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::WindingRule);- void draw_text_blob(SkTextBlob const&, FloatPoint, PaintStyle const&, Optional<Filter>, float global_alpha, CompositingAndBlendingOperator);+ void draw_glyph_run(Font const&, ReadonlySpan<CanvasGlyph>, FloatPoint translation, PaintStyle const&, Optional<Filter>, float global_alpha, CompositingAndBlendingOperator);void set_transform(Gfx::AffineTransform const&);void save();void restore();diff --git a/Services/Compositor/CanvasHost.cpp b/Services/Compositor/CanvasHost.cppindex daacd6d4d32..fcd35001c15 100644--- a/Services/Compositor/CanvasHost.cpp+++ b/Services/Compositor/CanvasHost.cpp@@ -13,7 +13,6 @@#include <LibGfx/CanvasCommandPlayer.h>#include <LibGfx/PaintingSurface.h>#include <LibGfx/SkiaBackendContext.h>-#include <core/SkTextBlob.h>namespace Compositor {@@ -48,16 +47,12 @@ OwnPtr<Gfx::CanvasCommandPlayer> CanvasHost::create_2d_command_player(Gfx::IntSi// WebGL sources are presented separately and resolve via the registry.return m_canvas_surface_registry.canvas_surface(m_canvas_surface_owner_id, Compositing::CanvasId { canvas_id });};- auto text_blob_resolver = [this](u64 font_id, ReadonlySpan<Gfx::CanvasGlyph> glyphs) {+ auto font_resolver = [this](u64 font_id) -> Gfx::Font const* {if (!m_text_resources.has_font(Compositing::FontResourceId { font_id }))- return sk_sp<SkTextBlob> {};- Vector<Compositing::DisplayListGlyph> display_list_glyphs;- display_list_glyphs.ensure_capacity(glyphs.size());- for (auto const& glyph : glyphs)- display_list_glyphs.unchecked_append({ .position = glyph.position, .glyph_id = glyph.glyph_id });- return m_text_blobs.text_blob(m_text_resources, Compositing::FontResourceId { font_id }, 1, display_list_glyphs, 0, TextRasterizationMode::Unhinted);+ return nullptr;+ return &m_text_resources.font(Compositing::FontResourceId { font_id });};- auto player = make<Gfx::CanvasCommandPlayer>(m_skia_backend_context, size, format, Gfx::AlphaType::Premultiplied, move(canvas_surface_resolver), move(text_blob_resolver));+ auto player = make<Gfx::CanvasCommandPlayer>(m_skia_backend_context, size, format, Gfx::AlphaType::Premultiplied, move(canvas_surface_resolver), move(font_resolver));// https://html.spec.whatwg.org/multipage/canvas.html#the-canvas-settings:concept-canvas-alpha// "Thus, the bitmap of such a context starts off as opaque black instead of transparent black"@@ -149,12 +144,12 @@ void CanvasHost::execute_canvas_2d_stream(Vector<Compositing::Canvas2DCommandStr{Compositing::DisplayListResourceSet resources;for (auto const& font : fonts) {- // NB: Font IDs are immutable. Preserve the backing storage used by cached text blobs.+ // NB: Font IDs are immutable. Keep the typeface that Skia's glyph caches were built for.if (!m_text_resources.has_font(font.id))m_text_resources.set_font(font.id, font.font);resources.fonts.set(font.id);}- m_text_blobs.evict(m_text_resources.retain_only(resources));+ m_text_resources.retain_only(resources);for (auto const& segment : segments) {// The canvas may have been destroyed while this segment was pending in// WebContent, so a missing context is not a protocol violation.diff --git a/Services/Compositor/CanvasHost.h b/Services/Compositor/CanvasHost.hindex 3fc16f3c5f6..fab68a3f41a 100644--- a/Services/Compositor/CanvasHost.h+++ b/Services/Compositor/CanvasHost.h@@ -19,7 +19,6 @@#include <AK/String.h>#include <AK/Variant.h>#include <AK/Vector.h>-#include <Compositor/DisplayListRasterCache.h>#include <LibCompositing/DisplayList/DisplayListResourceIds.h>#include <LibCompositing/Forward.h>#include <LibCompositing/Types.h>@@ -91,7 +90,6 @@ private:Compositing::CanvasSurfaceOwnerId m_canvas_surface_owner_id;HashMap<Compositing::CanvasId, Context> m_contexts;Compositing::DisplayListResourceStorage m_text_resources;- DisplayListRasterCache m_text_blobs;};}diff --git a/Services/Compositor/DisplayListRasterCache.cpp b/Services/Compositor/DisplayListRasterCache.cppindex d1293966fd6..c154f4a5ca8 100644--- a/Services/Compositor/DisplayListRasterCache.cpp+++ b/Services/Compositor/DisplayListRasterCache.cpp@@ -393,7 +393,7 @@ static u64 text_blob_glyph_hash(ReadonlySpan<DisplayListGlyph> glyphs)return hash;}-static sk_sp<SkTextBlob> make_text_blob(Gfx::Font const& font, float scale, ReadonlySpan<DisplayListGlyph> glyphs, [[maybe_unused]] u8 font_smoothing, TextRasterizationMode rasterization_mode)+static sk_sp<SkTextBlob> make_text_blob(Gfx::Font const& font, float scale, ReadonlySpan<DisplayListGlyph> glyphs, [[maybe_unused]] u8 font_smoothing){if (font.is_invisible())return nullptr;@@ -416,12 +416,6 @@ static sk_sp<SkTextBlob> make_text_blob(Gfx::Font const& font, float scale, Readbreak;}#endif- if (rasterization_mode == TextRasterizationMode::Unhinted) {- // NB: Preserve canvas text's fractional baseline and unhinted outline geometry.- sk_font.setHinting(SkFontHinting::kNone);- sk_font.setForceAutoHinting(false);- sk_font.setBaselineSnap(false);- }SkTextBlobBuilder builder;auto const& run = builder.allocRunPos(sk_font, glyphs.size());@@ -434,13 +428,13 @@ static sk_sp<SkTextBlob> make_text_blob(Gfx::Font const& font, float scale, Readreturn builder.make();}-sk_sp<SkTextBlob> DisplayListRasterCache::text_blob(DisplayListResourceStorage const& resource_storage, FontResourceId font_id, float scale, ReadonlySpan<DisplayListGlyph> glyphs, u8 font_smoothing, TextRasterizationMode rasterization_mode)+sk_sp<SkTextBlob> DisplayListRasterCache::text_blob(DisplayListResourceStorage const& resource_storage, FontResourceId font_id, float scale, ReadonlySpan<DisplayListGlyph> glyphs, u8 font_smoothing){constexpr size_t max_text_blob_cache_bytes = 16 * MiB;constexpr auto text_blob_idle_duration = AK::Duration::from_milliseconds(250);ReadonlyBytes glyph_bytes { reinterpret_cast<u8 const*>(glyphs.data()), glyphs.size() * sizeof(DisplayListGlyph) };- TextBlobCacheKey key { font_id.value(), bit_cast<u32>(scale), font_smoothing, text_blob_glyph_hash(glyphs), rasterization_mode };+ TextBlobCacheKey key { font_id.value(), bit_cast<u32>(scale), font_smoothing, text_blob_glyph_hash(glyphs) };auto now = MonotonicTime::now();if (auto cached = m_text_blobs.find(key); cached != m_text_blobs.end()) {@@ -453,7 +447,7 @@ sk_sp<SkTextBlob> DisplayListRasterCache::text_blob(DisplayListResourceStorage cm_text_blobs.remove(cached);}- auto blob = make_text_blob(resource_storage.font(font_id), scale, glyphs, font_smoothing, rasterization_mode);+ auto blob = make_text_blob(resource_storage.font(font_id), scale, glyphs, font_smoothing);if (!blob)return nullptr;diff --git a/Services/Compositor/DisplayListRasterCache.h b/Services/Compositor/DisplayListRasterCache.hindex 6a63bd746d0..72c6ee04db6 100644--- a/Services/Compositor/DisplayListRasterCache.h+++ b/Services/Compositor/DisplayListRasterCache.h@@ -27,17 +27,11 @@ class sk_sp;namespace Compositor {-enum class TextRasterizationMode : u8 {- Normal,- Unhinted,-};-struct TextBlobCacheKey {u64 font_id { 0 };u32 scale_bits { 0 };u8 font_smoothing { 0 };u64 glyph_hash { 0 };- TextRasterizationMode rasterization_mode { TextRasterizationMode::Normal };bool operator==(TextBlobCacheKey const&) const = default;};@@ -50,7 +44,7 @@ template<>struct Traits<Compositor::TextBlobCacheKey> : public DefaultTraits<Compositor::TextBlobCacheKey> {static unsigned hash(Compositor::TextBlobCacheKey const& key){- return pair_int_hash(pair_int_hash(u64_hash(key.font_id ^ key.glyph_hash), key.scale_bits), pair_int_hash(key.font_smoothing, static_cast<u8>(key.rasterization_mode)));+ return pair_int_hash(pair_int_hash(u64_hash(key.font_id ^ key.glyph_hash), key.scale_bits), key.font_smoothing);}};@@ -78,7 +72,7 @@ public:sk_sp<SkImage> nested_display_list_raster(Compositing::DisplayListResourceId, RefPtr<Gfx::SkiaBackendContext> const&, Gfx::IntRect visible_rect_in_list_space, Gfx::IntRect& raster_rect_in_list_space);bool has_rasterized_nested_display_list(Compositing::DisplayListResourceId) const;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, TextRasterizationMode = TextRasterizationMode::Normal);+ sk_sp<SkTextBlob> text_blob(Compositing::DisplayListResourceStorage const&, Compositing::FontResourceId, float scale, ReadonlySpan<Compositing::DisplayListGlyph>, u8 font_smoothing);private:struct ImageFrameImage;