Bug 1875670 - Create a refcounted object for resolved color-font palette, instead of just a bare nsTArray. r=gfx-reviewers,lsalzman

This does not change behavior for users; it is preparation for the
following patch, which will want the ability to hold an additional
strong reference to a resolved palette. So we can no longer just own it
via a UniquePtr; we make it a RefPtr instead.

Differential Revision: https://phabricator.services.mozilla.com/D200138
This commit is contained in:
Jonathan Kew 2024-02-02 19:34:03 +00:00
parent 2a90e77ea0
commit df9da39b74
7 changed files with 42 additions and 20 deletions

View File

@ -2576,7 +2576,7 @@ uint16_t COLRFonts::GetColrTableVersion(hb_blob_t* aCOLR) {
return colr->version;
}
UniquePtr<nsTArray<sRGBColor>> COLRFonts::SetupColorPalette(
nsTArray<sRGBColor> 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<nsTArray<sRGBColor>> COLRFonts::SetupColorPalette(
hb_ot_color_palette_get_colors(aFace, paletteIndex, 0, &count,
colors.Elements());
auto palette = MakeUnique<nsTArray<sRGBColor>>();
palette->SetCapacity(count);
nsTArray<sRGBColor> 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<nsTArray<sRGBColor>> 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;
}
}
}

View File

@ -126,7 +126,7 @@ class COLRFonts {
static uint16_t GetColrTableVersion(hb_blob_t* aCOLR);
static UniquePtr<nsTArray<sRGBColor>> SetupColorPalette(
static nsTArray<sRGBColor> CreateColorPalette(
hb_face_t* aFace, const FontPaletteValueSet* aPaletteValueSet,
nsAtom* aFontPalette, const nsACString& aFamilyName);
};

View File

@ -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::sRGBColor>* gfx::PaletteCache::GetPaletteFor(
already_AddRefed<gfx::FontPalette> gfx::PaletteCache::GetPaletteFor(
gfxFontEntry* aFontEntry, nsAtom* aPaletteName) {
auto entry = Lookup(std::pair(aFontEntry, aPaletteName));
if (!entry) {
@ -22,10 +23,11 @@ nsTArray<gfx::sRGBColor>* 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();
}

View File

@ -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 <utility>
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<mozilla::gfx::sRGBColor>&& aColors)
: mColors(std::move(aColors)) {}
const nsTArray<mozilla::gfx::sRGBColor>* Colors() const { return &mColors; }
private:
~FontPalette() = default;
nsTArray<mozilla::gfx::sRGBColor> 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<gfxFontEntry>, RefPtr<nsAtom>>;
struct CacheData {
CacheKey mKey;
mozilla::UniquePtr<nsTArray<mozilla::gfx::sRGBColor>> mPalette;
RefPtr<FontPalette> mPalette;
};
class PaletteCache
@ -33,8 +53,8 @@ class PaletteCache
void SetPaletteValueSet(const FontPaletteValueSet* aSet);
nsTArray<mozilla::gfx::sRGBColor>* GetPaletteFor(gfxFontEntry* aFontEntry,
nsAtom* aPaletteName);
already_AddRefed<FontPalette> GetPaletteFor(gfxFontEntry* aFontEntry,
nsAtom* aPaletteName);
static mozilla::HashNumber Hash(const CacheKey& aKey) {
return mozilla::HashGeneric(aKey.first.get(), aKey.second.get());

View File

@ -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;
}

View File

@ -10,7 +10,6 @@
#include <new>
#include <utility>
#include <functional>
#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<mozilla::gfx::sRGBColor>* palette; // owned by TextRunDrawParams
RefPtr<mozilla::gfx::FontPalette> palette;
mozilla::gfx::Rect fontExtents;
bool isVerticalFont;
bool haveSVGGlyphs;

View File

@ -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",