blob: 9bf03c1f40a618d9c4ab39f2fa4969b7dff6e595 [file] [log] [blame]
Robert Phillipsc4039ea2018-03-01 11:36:45 -05001/*
2 * Copyright 2018 Google Inc.
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 GrAtlasManager_DEFINED
9#define GrAtlasManager_DEFINED
10
11#include "GrDrawOpAtlas.h"
12#include "GrOnFlushResourceProvider.h"
13
14class GrAtlasGlypCache;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050015class GrTextStrike;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050016struct GrGlyph;
17
Robert Phillips5a66efb2018-03-07 15:13:18 -050018//////////////////////////////////////////////////////////////////////////////////////////////////
19/** The GrAtlasManager manages the lifetime of and access to GrDrawOpAtlases.
20 * It is only available at flush and only via the GrOpFlushState.
21 *
22 * This implies that all of the advanced atlasManager functionality (i.e.,
23 * adding glyphs to the atlas) are only available at flush time.
24 */
25class GrAtlasManager : public GrOnFlushCallbackObject {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050026public:
Robert Phillips5a66efb2018-03-07 15:13:18 -050027 GrAtlasManager(GrProxyProvider*, GrGlyphCache*,
28 float maxTextureBytes, GrDrawOpAtlas::AllowMultitexturing);
29 ~GrAtlasManager() override;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050030
31 // if getProxies returns nullptr, the client must not try to use other functions on the
32 // GrGlyphCache which use the atlas. This function *must* be called first, before other
33 // functions which use the atlas.
34 const sk_sp<GrTextureProxy>* getProxies(GrMaskFormat format, unsigned int* numProxies) {
35 if (this->initAtlas(format)) {
36 *numProxies = this->getAtlas(format)->numActivePages();
37 return this->getAtlas(format)->getProxies();
38 }
39 *numProxies = 0;
40 return nullptr;
41 }
42
43 SkScalar getGlyphSizeLimit() const { return fGlyphSizeLimit; }
44
Robert Phillipsaf4adef2018-03-07 11:59:37 -050045 static void ComputeAtlasLimits(const GrCaps* caps, float maxTextureBytes,
46 int* maxDim, int* minDim, int* maxPlot, int* minPlot);
47
Robert Phillipsc4039ea2018-03-01 11:36:45 -050048 void freeAll();
49
50 bool hasGlyph(GrGlyph* glyph);
51
52 // To ensure the GrDrawOpAtlas does not evict the Glyph Mask from its texture backing store,
53 // the client must pass in the current op token along with the GrGlyph.
54 // A BulkUseTokenUpdater is used to manage bulk last use token updating in the Atlas.
55 // For convenience, this function will also set the use token for the current glyph if required
56 // NOTE: the bulk uploader is only valid if the subrun has a valid atlasGeneration
57 void addGlyphToBulkAndSetUseToken(GrDrawOpAtlas::BulkUseTokenUpdater*, GrGlyph*,
58 GrDeferredUploadToken);
59
60 void setUseTokenBulk(const GrDrawOpAtlas::BulkUseTokenUpdater& updater,
61 GrDeferredUploadToken token,
62 GrMaskFormat format) {
63 this->getAtlas(format)->setLastUseTokenBulk(updater, token);
64 }
65
66 // add to texture atlas that matches this format
Robert Phillipsd2e9f762018-03-07 11:54:37 -050067 GrDrawOpAtlas::ErrorCode addToAtlas(
68 GrResourceProvider*, GrGlyphCache*, GrTextStrike*,
Robert Phillipsc4039ea2018-03-01 11:36:45 -050069 GrDrawOpAtlas::AtlasID*, GrDeferredUploadTarget*, GrMaskFormat,
70 int width, int height, const void* image, SkIPoint16* loc);
71
72 // Some clients may wish to verify the integrity of the texture backing store of the
73 // GrDrawOpAtlas. The atlasGeneration returned below is a monotonically increasing number which
74 // changes every time something is removed from the texture backing store.
75 uint64_t atlasGeneration(GrMaskFormat format) const {
76 return this->getAtlas(format)->atlasGeneration();
77 }
78
79 // GrOnFlushCallbackObject overrides
80
81 void preFlush(GrOnFlushResourceProvider* onFlushResourceProvider, const uint32_t*, int,
82 SkTArray<sk_sp<GrRenderTargetContext>>*) override {
83 for (int i = 0; i < kMaskFormatCount; ++i) {
84 if (fAtlases[i]) {
85 fAtlases[i]->instantiate(onFlushResourceProvider);
86 }
87 }
88 }
89
90 void postFlush(GrDeferredUploadToken startTokenForNextFlush,
91 const uint32_t* opListIDs, int numOpListIDs) override {
92 for (int i = 0; i < kMaskFormatCount; ++i) {
93 if (fAtlases[i]) {
94 fAtlases[i]->compact(startTokenForNextFlush);
95 }
96 }
97 }
98
99 // The AtlasGlyph cache always survives freeGpuResources so we want it to remain in the active
100 // OnFlushCallbackObject list
101 bool retainOnFreeGpuResources() override { return true; }
102
103 ///////////////////////////////////////////////////////////////////////////
104 // Functions intended debug only
105#ifdef SK_DEBUG
106 void dump(GrContext* context) const;
107#endif
108
109 void setAtlasSizes_ForTesting(const GrDrawOpAtlasConfig configs[3]);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500110 void setMaxPages_TestingOnly(uint32_t maxPages);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500111
112private:
Robert Phillips5a66efb2018-03-07 15:13:18 -0500113 bool initAtlas(GrMaskFormat);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500114
Robert Phillips5a66efb2018-03-07 15:13:18 -0500115 // There is a 1:1 mapping between GrMaskFormats and atlas indices
116 static int MaskFormatToAtlasIndex(GrMaskFormat format) {
117 static const int sAtlasIndices[] = {
118 kA8_GrMaskFormat,
119 kA565_GrMaskFormat,
120 kARGB_GrMaskFormat,
121 };
122 static_assert(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, "array_size_mismatch");
123
124 SkASSERT(sAtlasIndices[format] < kMaskFormatCount);
125 return sAtlasIndices[format];
126 }
127
128 GrDrawOpAtlas* getAtlas(GrMaskFormat format) const {
129 int atlasIndex = MaskFormatToAtlasIndex(format);
130 SkASSERT(fAtlases[atlasIndex]);
131 return fAtlases[atlasIndex].get();
132 }
133
134 sk_sp<const GrCaps> fCaps;
135 GrDrawOpAtlas::AllowMultitexturing fAllowMultitexturing;
136 std::unique_ptr<GrDrawOpAtlas> fAtlases[kMaskFormatCount];
137 GrDrawOpAtlasConfig fAtlasConfigs[kMaskFormatCount];
138 SkScalar fGlyphSizeLimit;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500139 GrProxyProvider* fProxyProvider;
140 GrGlyphCache* fGlyphCache;
141
Robert Phillips5a66efb2018-03-07 15:13:18 -0500142 typedef GrOnFlushCallbackObject INHERITED;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500143};
144
145#endif // GrAtlasManager_DEFINED