FontPaletteCache.h (2082B)
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/. */ 5 6 #ifndef FONT_PALETTE_CACHE_H 7 #define FONT_PALETTE_CACHE_H 8 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> 16 17 class gfxFontEntry; 18 19 namespace mozilla::gfx { 20 21 class FontPaletteValueSet; 22 23 // A resolved font palette as an array of colors. 24 class FontPalette { 25 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FontPalette); 26 27 public: 28 FontPalette() = default; 29 explicit FontPalette(nsTArray<mozilla::gfx::sRGBColor>&& aColors) 30 : mColors(std::move(aColors)) {} 31 32 const nsTArray<mozilla::gfx::sRGBColor>* Colors() const { return &mColors; } 33 34 private: 35 ~FontPalette() = default; 36 37 nsTArray<mozilla::gfx::sRGBColor> mColors; 38 }; 39 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; 46 }; 47 48 class PaletteCache 49 : public mozilla::MruCache<CacheKey, CacheData, PaletteCache> { 50 public: 51 explicit PaletteCache(const FontPaletteValueSet* aPaletteValueSet = nullptr) 52 : mPaletteValueSet(aPaletteValueSet) {} 53 54 void SetPaletteValueSet(const FontPaletteValueSet* aSet); 55 56 already_AddRefed<FontPalette> GetPaletteFor(gfxFontEntry* aFontEntry, 57 nsAtom* aPaletteName); 58 59 static mozilla::HashNumber Hash(const CacheKey& aKey) { 60 return mozilla::HashGeneric(aKey.first.get(), aKey.second.get()); 61 } 62 static bool Match(const CacheKey& aKey, const CacheData& aVal) { 63 return aVal.mKey == aKey; 64 } 65 66 protected: 67 const FontPaletteValueSet* mPaletteValueSet = nullptr; 68 }; 69 70 } // namespace mozilla::gfx 71 72 #endif