diff --git a/gfx/thebes/COLRFonts.cpp b/gfx/thebes/COLRFonts.cpp index 2916f9c93436..6369bf191dc9 100644 --- a/gfx/thebes/COLRFonts.cpp +++ b/gfx/thebes/COLRFonts.cpp @@ -2576,7 +2576,7 @@ uint16_t COLRFonts::GetColrTableVersion(hb_blob_t* aCOLR) { return colr->version; } -UniquePtr> COLRFonts::SetupColorPalette( +nsTArray COLRFonts::CreateColorPalette( hb_face_t* aFace, const FontPaletteValueSet* aPaletteValueSet, nsAtom* aFontPalette, const nsACString& aFamilyName) { // Find the base color palette to use, if there are multiple available; @@ -2631,10 +2631,10 @@ UniquePtr> COLRFonts::SetupColorPalette( hb_ot_color_palette_get_colors(aFace, paletteIndex, 0, &count, colors.Elements()); - auto palette = MakeUnique>(); - palette->SetCapacity(count); + nsTArray palette; + palette.SetCapacity(count); for (const auto c : colors) { - palette->AppendElement( + palette.AppendElement( sRGBColor(hb_color_get_red(c) / 255.0, hb_color_get_green(c) / 255.0, hb_color_get_blue(c) / 255.0, hb_color_get_alpha(c) / 255.0)); } @@ -2642,8 +2642,8 @@ UniquePtr> COLRFonts::SetupColorPalette( // Apply @font-palette-values overrides, if present. if (fpv) { for (const auto overrideColor : fpv->mOverrides) { - if (overrideColor.mIndex < palette->Length()) { - (*palette)[overrideColor.mIndex] = overrideColor.mColor; + if (overrideColor.mIndex < palette.Length()) { + palette[overrideColor.mIndex] = overrideColor.mColor; } } } diff --git a/gfx/thebes/COLRFonts.h b/gfx/thebes/COLRFonts.h index a073bb416462..57129f13645c 100644 --- a/gfx/thebes/COLRFonts.h +++ b/gfx/thebes/COLRFonts.h @@ -126,7 +126,7 @@ class COLRFonts { static uint16_t GetColrTableVersion(hb_blob_t* aCOLR); - static UniquePtr> SetupColorPalette( + static nsTArray CreateColorPalette( hb_face_t* aFace, const FontPaletteValueSet* aPaletteValueSet, nsAtom* aFontPalette, const nsACString& aFamilyName); }; diff --git a/gfx/thebes/FontPaletteCache.cpp b/gfx/thebes/FontPaletteCache.cpp index 5e5d857c5c8c..02ca74097794 100644 --- a/gfx/thebes/FontPaletteCache.cpp +++ b/gfx/thebes/FontPaletteCache.cpp @@ -5,6 +5,7 @@ #include "FontPaletteCache.h" #include "COLRFonts.h" +#include "gfxFontEntry.h" using namespace mozilla; @@ -14,7 +15,7 @@ void gfx::PaletteCache::SetPaletteValueSet( Clear(); } -nsTArray* gfx::PaletteCache::GetPaletteFor( +already_AddRefed gfx::PaletteCache::GetPaletteFor( gfxFontEntry* aFontEntry, nsAtom* aPaletteName) { auto entry = Lookup(std::pair(aFontEntry, aPaletteName)); if (!entry) { @@ -22,10 +23,11 @@ nsTArray* gfx::PaletteCache::GetPaletteFor( newData.mKey = std::pair(aFontEntry, aPaletteName); gfxFontEntry::AutoHBFace face = aFontEntry->GetHBFace(); - newData.mPalette = gfx::COLRFonts::SetupColorPalette( - face, mPaletteValueSet, aPaletteName, aFontEntry->FamilyName()); + newData.mPalette = new FontPalette(gfx::COLRFonts::CreateColorPalette( + face, mPaletteValueSet, aPaletteName, aFontEntry->FamilyName())); entry.Set(std::move(newData)); } - return entry.Data().mPalette.get(); + RefPtr result = entry.Data().mPalette; + return result.forget(); } diff --git a/gfx/thebes/FontPaletteCache.h b/gfx/thebes/FontPaletteCache.h index fee12e2f22c2..fd2b3cc058ec 100644 --- a/gfx/thebes/FontPaletteCache.h +++ b/gfx/thebes/FontPaletteCache.h @@ -6,23 +6,43 @@ #ifndef FONT_PALETTE_CACHE_H #define FONT_PALETTE_CACHE_H -#include "gfxFontEntry.h" #include "mozilla/gfx/Types.h" #include "mozilla/MruCache.h" #include "mozilla/HashFunctions.h" +#include "mozilla/RefPtr.h" #include "nsAtom.h" +#include "nsTArray.h" #include +class gfxFontEntry; + namespace mozilla::gfx { class FontPaletteValueSet; +// A resolved font palette as an array of colors. +class FontPalette { + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FontPalette); + + public: + FontPalette() = default; + explicit FontPalette(nsTArray&& aColors) + : mColors(std::move(aColors)) {} + + const nsTArray* Colors() const { return &mColors; } + + private: + ~FontPalette() = default; + + nsTArray mColors; +}; + // MRU cache used for resolved color-font palettes, to avoid reconstructing // the palette for each glyph rendered with a given font. using CacheKey = std::pair, RefPtr>; struct CacheData { CacheKey mKey; - mozilla::UniquePtr> mPalette; + RefPtr mPalette; }; class PaletteCache @@ -33,8 +53,8 @@ class PaletteCache void SetPaletteValueSet(const FontPaletteValueSet* aSet); - nsTArray* GetPaletteFor(gfxFontEntry* aFontEntry, - nsAtom* aPaletteName); + already_AddRefed GetPaletteFor(gfxFontEntry* aFontEntry, + nsAtom* aPaletteName); static mozilla::HashNumber Hash(const CacheKey& aKey) { return mozilla::HashGeneric(aKey.first.get(), aKey.second.get()); diff --git a/gfx/thebes/gfxFont.cpp b/gfx/thebes/gfxFont.cpp index 709040391e07..2b3e66bd2c06 100644 --- a/gfx/thebes/gfxFont.cpp +++ b/gfx/thebes/gfxFont.cpp @@ -2676,7 +2676,7 @@ bool gfxFont::RenderColorGlyph(DrawTarget* aDrawTarget, gfxContext* aContext, GetFontEntry()->GetCOLR(), hbShaper->GetHBFont(), paintGraph, aDrawTarget, aTextDrawer, aFontParams.scaledFont, aFontParams.drawOptions, aPoint, aFontParams.currentColor, - aFontParams.palette, aGlyphId, mFUnitsConvFactor); + aFontParams.palette->Colors(), aGlyphId, mFUnitsConvFactor); } } @@ -2686,7 +2686,7 @@ bool gfxFont::RenderColorGlyph(DrawTarget* aDrawTarget, gfxContext* aContext, bool ok = COLRFonts::PaintGlyphLayers( GetFontEntry()->GetCOLR(), face, layers, aDrawTarget, aTextDrawer, aFontParams.scaledFont, aFontParams.drawOptions, aPoint, - aFontParams.currentColor, aFontParams.palette); + aFontParams.currentColor, aFontParams.palette->Colors()); return ok; } diff --git a/gfx/thebes/gfxFont.h b/gfx/thebes/gfxFont.h index d475741c3ae9..8975fa0088fe 100644 --- a/gfx/thebes/gfxFont.h +++ b/gfx/thebes/gfxFont.h @@ -10,7 +10,6 @@ #include #include #include -#include "FontPaletteCache.h" #include "PLDHashTable.h" #include "ThebesRLBoxTypes.h" #include "gfxFontVariations.h" @@ -27,6 +26,7 @@ #include "mozilla/RWLock.h" #include "mozilla/TypedEnumBits.h" #include "mozilla/UniquePtr.h" +#include "mozilla/gfx/FontPaletteCache.h" #include "mozilla/gfx/MatrixFwd.h" #include "mozilla/gfx/Point.h" #include "mozilla/gfx/2D.h" @@ -2338,7 +2338,7 @@ struct MOZ_STACK_CLASS FontDrawParams { mozilla::gfx::DrawOptions drawOptions; gfxFloat advanceDirection; mozilla::gfx::sRGBColor currentColor; - nsTArray* palette; // owned by TextRunDrawParams + RefPtr palette; mozilla::gfx::Rect fontExtents; bool isVerticalFont; bool haveSVGGlyphs; diff --git a/gfx/thebes/moz.build b/gfx/thebes/moz.build index a7eca33ab141..fd1fcf236d81 100644 --- a/gfx/thebes/moz.build +++ b/gfx/thebes/moz.build @@ -19,7 +19,6 @@ XPIDL_MODULE = "gfx" EXPORTS += [ "COLRFonts.h", "DrawMode.h", - "FontPaletteCache.h", "gfx2DGlue.h", "gfxAlphaRecovery.h", "gfxASurface.h", @@ -71,6 +70,7 @@ EXPORTS.mozilla.gfx += [ "D3D11Checks.h", "DeviceManagerDx.h", "DisplayConfigWindows.h", + "FontPaletteCache.h", "PrintPromise.h", "PrintTarget.h", "PrintTargetThebes.h",