summaryrefslogtreecommitdiffstats
path: root/gfx/thebes/FontPaletteCache.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /gfx/thebes/FontPaletteCache.h
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gfx/thebes/FontPaletteCache.h')
-rw-r--r--gfx/thebes/FontPaletteCache.h72
1 files changed, 72 insertions, 0 deletions
diff --git a/gfx/thebes/FontPaletteCache.h b/gfx/thebes/FontPaletteCache.h
new file mode 100644
index 0000000000..fd2b3cc058
--- /dev/null
+++ b/gfx/thebes/FontPaletteCache.h
@@ -0,0 +1,72 @@
+/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef FONT_PALETTE_CACHE_H
+#define FONT_PALETTE_CACHE_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;
+ RefPtr<FontPalette> mPalette;
+};
+
+class PaletteCache
+ : public mozilla::MruCache<CacheKey, CacheData, PaletteCache> {
+ public:
+ explicit PaletteCache(const FontPaletteValueSet* aPaletteValueSet = nullptr)
+ : mPaletteValueSet(aPaletteValueSet) {}
+
+ void SetPaletteValueSet(const FontPaletteValueSet* aSet);
+
+ already_AddRefed<FontPalette> GetPaletteFor(gfxFontEntry* aFontEntry,
+ nsAtom* aPaletteName);
+
+ static mozilla::HashNumber Hash(const CacheKey& aKey) {
+ return mozilla::HashGeneric(aKey.first.get(), aKey.second.get());
+ }
+ static bool Match(const CacheKey& aKey, const CacheData& aVal) {
+ return aVal.mKey == aKey;
+ }
+
+ protected:
+ const FontPaletteValueSet* mPaletteValueSet = nullptr;
+};
+
+} // namespace mozilla::gfx
+
+#endif