1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef FONT_PALETTE_CACHE_H
7 #define FONT_PALETTE_CACHE_H
9 #include "mozilla/gfx/Types.h"
10 #include "mozilla/MruCache.h"
11 #include "mozilla/HashFunctions.h"
12 #include "mozilla/RefPtr.h"
19 namespace mozilla::gfx
{
21 class FontPaletteValueSet
;
23 // A resolved font palette as an array of colors.
25 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FontPalette
);
28 FontPalette() = default;
29 explicit FontPalette(nsTArray
<mozilla::gfx::sRGBColor
>&& aColors
)
30 : mColors(std::move(aColors
)) {}
32 const nsTArray
<mozilla::gfx::sRGBColor
>* Colors() const { return &mColors
; }
35 ~FontPalette() = default;
37 nsTArray
<mozilla::gfx::sRGBColor
> mColors
;
40 // MRU cache used for resolved color-font palettes, to avoid reconstructing
41 // the palette for each glyph rendered with a given font.
42 using CacheKey
= std::pair
<RefPtr
<gfxFontEntry
>, RefPtr
<nsAtom
>>;
45 RefPtr
<FontPalette
> mPalette
;
49 : public mozilla::MruCache
<CacheKey
, CacheData
, PaletteCache
> {
51 explicit PaletteCache(const FontPaletteValueSet
* aPaletteValueSet
= nullptr)
52 : mPaletteValueSet(aPaletteValueSet
) {}
54 void SetPaletteValueSet(const FontPaletteValueSet
* aSet
);
56 already_AddRefed
<FontPalette
> GetPaletteFor(gfxFontEntry
* aFontEntry
,
57 nsAtom
* aPaletteName
);
59 static mozilla::HashNumber
Hash(const CacheKey
& aKey
) {
60 return mozilla::HashGeneric(aKey
.first
.get(), aKey
.second
.get());
62 static bool Match(const CacheKey
& aKey
, const CacheData
& aVal
) {
63 return aVal
.mKey
== aKey
;
67 const FontPaletteValueSet
* mPaletteValueSet
= nullptr;
70 } // namespace mozilla::gfx