no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / gfx / thebes / FontPaletteCache.h
blobfd2b3cc058ec53133a0ee1898d70346f4d7934fc
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"
13 #include "nsAtom.h"
14 #include "nsTArray.h"
15 #include <utility>
17 class gfxFontEntry;
19 namespace mozilla::gfx {
21 class FontPaletteValueSet;
23 // A resolved font palette as an array of colors.
24 class FontPalette {
25 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FontPalette);
27 public:
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; }
34 private:
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>>;
43 struct CacheData {
44 CacheKey mKey;
45 RefPtr<FontPalette> mPalette;
48 class PaletteCache
49 : public mozilla::MruCache<CacheKey, CacheData, PaletteCache> {
50 public:
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;
66 protected:
67 const FontPaletteValueSet* mPaletteValueSet = nullptr;
70 } // namespace mozilla::gfx
72 #endif