Debian Codegraph @

src/gpu/graphite/text/TextAtlasManager.h

raw
1 /*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef skgpu_graphite_TextAtlasManager_DEFINED
9 #define skgpu_graphite_TextAtlasManager_DEFINED
10
11 #include "include/core/SkRefCnt.h" // IWYU pragma: keep
12 #include "include/private/base/SkAssert.h"
13 #include "src/gpu/AtlasTypes.h"
14 #include "src/gpu/graphite/DrawAtlas.h"
15
16 #include <cstdint>
17 #include <memory>
18
19 class SkGlyph;
20
21 namespace sktext::gpu {
22 class Glyph;
23 }
24
25 namespace skgpu::graphite {
26
27 class DrawContext;
28 class Recorder;
29 class TextureProxy;
30
31 //////////////////////////////////////////////////////////////////////////////////////////////////
32 /** The TextAtlasManager manages the lifetime of and access to DrawAtlases used in glyph rendering.
33 */
34 class TextAtlasManager : public AtlasGenerationCounter {
35 public:
36 TextAtlasManager(Recorder*);
37 ~TextAtlasManager();
38
39 // If getProxies returns nullptr, the client must not try to use other functions on the
40 // StrikeCache which use the atlas. This function *must* be called first, before other
41 // functions which use the atlas.
42 const sk_sp<TextureProxy>* getProxies(MaskFormat format,
43 unsigned int* numActiveProxies) {
44 format = this->resolveMaskFormat(format);
45 if (this->initAtlas(format)) {
46 *numActiveProxies = this->getAtlas(format)->numActivePages();
47 return this->getAtlas(format)->getProxies();
48 }
49 *numActiveProxies = 0;
50 return nullptr;
51 }
52
53 void freeGpuResources();
54
55 bool hasGlyph(MaskFormat, sktext::gpu::Glyph*);
56
57 DrawAtlas::ErrorCode addGlyphToAtlas(const SkGlyph&,
58 sktext::gpu::Glyph*,
59 int srcPadding);
60
61 // To ensure the DrawAtlas does not evict the Glyph Mask from its texture backing store,
62 // the client must pass in the current draw token along with the sktext::gpu::Glyph.
63 // A BulkUsePlotUpdater is used to manage bulk last use token updating in the Atlas.
64 // For convenience, this function will also set the use token for the current glyph if required
65 // NOTE: the bulk uploader is only valid if the subrun has a valid atlasGeneration
66 void addGlyphToBulkAndSetUseToken(BulkUsePlotUpdater*, MaskFormat,
67 sktext::gpu::Glyph*, Token);
68
69 void setUseTokenBulk(const BulkUsePlotUpdater& updater,
70 Token token,
71 MaskFormat format) {
72 this->getAtlas(format)->setLastUseTokenBulk(updater, token);
73 }
74
75 bool recordUploads(DrawContext* dc);
76
77 void evictAtlases() {
78 for (int i = 0; i < kMaskFormatCount; ++i) {
79 if (fAtlases[i]) {
80 fAtlases[i]->evictAllPlots();
81 }
82 }
83 }
84
85 void compact();
86
87 // Some clients may wish to verify the integrity of the texture backing store of the
88 // DrawAtlas. The atlasGeneration returned below is a monotonically increasing number which
89 // changes every time something is removed from the texture backing store.
90 uint64_t atlasGeneration(skgpu::MaskFormat format) const {
91 return this->getAtlas(format)->atlasGeneration();
92 }
93
94 ///////////////////////////////////////////////////////////////////////////
95 // Functions intended debug only
96
97 void setAtlasDimensionsToMinimum_ForTesting();
98 void setMaxPages_TestingOnly(uint32_t maxPages);
99
100 private:
101 bool initAtlas(MaskFormat);
102 // Change an expected 565 mask format to 8888 if 565 is not supported (will happen when using
103 // Metal on Intel MacOS). The actual conversion of the data is handled in
104 // get_packed_glyph_image() in StrikeCache.cpp
105 MaskFormat resolveMaskFormat(MaskFormat format) const;
106
107 // There is a 1:1 mapping between skgpu::MaskFormats and atlas indices
108 static int MaskFormatToAtlasIndex(skgpu::MaskFormat format) {
109 return static_cast<int>(format);
110 }
111 static skgpu::MaskFormat AtlasIndexToMaskFormat(int idx) {
112 return static_cast<skgpu::MaskFormat>(idx);
113 }
114
115 DrawAtlas* getAtlas(skgpu::MaskFormat format) const {
116 format = this->resolveMaskFormat(format);
117 int atlasIndex = MaskFormatToAtlasIndex(format);
118 SkASSERT(fAtlases[atlasIndex]);
119 return fAtlases[atlasIndex].get();
120 }
121
122 Recorder* fRecorder;
123 DrawAtlas::AllowMultitexturing fAllowMultitexturing;
124 std::unique_ptr<DrawAtlas> fAtlases[kMaskFormatCount];
125 static_assert(kMaskFormatCount == 3);
126 bool fSupportBilerpAtlas;
127 DrawAtlasConfig fAtlasConfig;
128 };
129
130 } // namespace skgpu::graphite
131
132 #endif // skgpu_graphite_TextAtlasManager_DEFINED