blob: 6c168534ad255425b526a6b79394fc6d01fa23de [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
Timothy Liang91e260f2018-06-15 13:28:35 -040011#include "GrCaps.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050012#include "GrDrawOpAtlas.h"
13#include "GrOnFlushResourceProvider.h"
Timothy Liang91e260f2018-06-15 13:28:35 -040014#include "GrProxyProvider.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050015
16class GrAtlasGlypCache;
Robert Phillipscaf1ebb2018-03-01 14:28:44 -050017class GrTextStrike;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050018struct GrGlyph;
19
Robert Phillips5a66efb2018-03-07 15:13:18 -050020//////////////////////////////////////////////////////////////////////////////////////////////////
21/** The GrAtlasManager manages the lifetime of and access to GrDrawOpAtlases.
22 * It is only available at flush and only via the GrOpFlushState.
23 *
24 * This implies that all of the advanced atlasManager functionality (i.e.,
25 * adding glyphs to the atlas) are only available at flush time.
26 */
27class GrAtlasManager : public GrOnFlushCallbackObject {
Robert Phillipsc4039ea2018-03-01 11:36:45 -050028public:
Robert Phillips5a66efb2018-03-07 15:13:18 -050029 GrAtlasManager(GrProxyProvider*, GrGlyphCache*,
30 float maxTextureBytes, GrDrawOpAtlas::AllowMultitexturing);
31 ~GrAtlasManager() override;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050032
Timothy Liang91e260f2018-06-15 13:28:35 -040033 // Change an expected 565 mask format to 8888 if 565 is not supported (will happen when using
34 // Metal on macOS). The actual conversion of the data is handled in get_packed_glyph_image() in
35 // GrGlyphCache.cpp
36 GrMaskFormat resolveMaskFormat(GrMaskFormat format) const {
37 if (kA565_GrMaskFormat == format &&
38 !fProxyProvider->caps()->isConfigTexturable(kRGB_565_GrPixelConfig)) {
39 format = kARGB_GrMaskFormat;
40 }
41 return format;
42 }
43
Robert Phillipsc4039ea2018-03-01 11:36:45 -050044 // if getProxies returns nullptr, the client must not try to use other functions on the
45 // GrGlyphCache which use the atlas. This function *must* be called first, before other
Jim Van Verthcbeae032018-05-16 14:54:41 -040046 // functions which use the atlas. Note that we can have proxies available but none active
47 // (i.e., none instantiated).
48 const sk_sp<GrTextureProxy>* getProxies(GrMaskFormat format, unsigned int* numActiveProxies) {
Timothy Liang91e260f2018-06-15 13:28:35 -040049 format = this->resolveMaskFormat(format);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050050 if (this->initAtlas(format)) {
Jim Van Verthcbeae032018-05-16 14:54:41 -040051 *numActiveProxies = this->getAtlas(format)->numActivePages();
Robert Phillipsc4039ea2018-03-01 11:36:45 -050052 return this->getAtlas(format)->getProxies();
53 }
Jim Van Verthcbeae032018-05-16 14:54:41 -040054 *numActiveProxies = 0;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050055 return nullptr;
56 }
57
58 SkScalar getGlyphSizeLimit() const { return fGlyphSizeLimit; }
59
Khushalfa8ff092018-06-06 17:46:38 -070060 static void ComputeAtlasLimits(int maxTextureSize, size_t maxTextureBytes, int* maxDim,
61 int* minDim, int* maxPlot, int* minPlot);
Robert Phillipsaf4adef2018-03-07 11:59:37 -050062
Robert Phillipsc4039ea2018-03-01 11:36:45 -050063 void freeAll();
64
65 bool hasGlyph(GrGlyph* glyph);
66
67 // To ensure the GrDrawOpAtlas does not evict the Glyph Mask from its texture backing store,
68 // the client must pass in the current op token along with the GrGlyph.
69 // A BulkUseTokenUpdater is used to manage bulk last use token updating in the Atlas.
70 // For convenience, this function will also set the use token for the current glyph if required
71 // NOTE: the bulk uploader is only valid if the subrun has a valid atlasGeneration
72 void addGlyphToBulkAndSetUseToken(GrDrawOpAtlas::BulkUseTokenUpdater*, GrGlyph*,
73 GrDeferredUploadToken);
74
75 void setUseTokenBulk(const GrDrawOpAtlas::BulkUseTokenUpdater& updater,
76 GrDeferredUploadToken token,
77 GrMaskFormat format) {
78 this->getAtlas(format)->setLastUseTokenBulk(updater, token);
79 }
80
81 // add to texture atlas that matches this format
Robert Phillipsd2e9f762018-03-07 11:54:37 -050082 GrDrawOpAtlas::ErrorCode addToAtlas(
83 GrResourceProvider*, GrGlyphCache*, GrTextStrike*,
Robert Phillipsc4039ea2018-03-01 11:36:45 -050084 GrDrawOpAtlas::AtlasID*, GrDeferredUploadTarget*, GrMaskFormat,
85 int width, int height, const void* image, SkIPoint16* loc);
86
87 // Some clients may wish to verify the integrity of the texture backing store of the
88 // GrDrawOpAtlas. 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(GrMaskFormat format) const {
91 return this->getAtlas(format)->atlasGeneration();
92 }
93
94 // GrOnFlushCallbackObject overrides
95
96 void preFlush(GrOnFlushResourceProvider* onFlushResourceProvider, const uint32_t*, int,
97 SkTArray<sk_sp<GrRenderTargetContext>>*) override {
98 for (int i = 0; i < kMaskFormatCount; ++i) {
99 if (fAtlases[i]) {
100 fAtlases[i]->instantiate(onFlushResourceProvider);
101 }
102 }
103 }
104
105 void postFlush(GrDeferredUploadToken startTokenForNextFlush,
106 const uint32_t* opListIDs, int numOpListIDs) override {
107 for (int i = 0; i < kMaskFormatCount; ++i) {
108 if (fAtlases[i]) {
109 fAtlases[i]->compact(startTokenForNextFlush);
110 }
111 }
112 }
113
114 // The AtlasGlyph cache always survives freeGpuResources so we want it to remain in the active
115 // OnFlushCallbackObject list
116 bool retainOnFreeGpuResources() override { return true; }
117
118 ///////////////////////////////////////////////////////////////////////////
119 // Functions intended debug only
120#ifdef SK_DEBUG
121 void dump(GrContext* context) const;
122#endif
123
124 void setAtlasSizes_ForTesting(const GrDrawOpAtlasConfig configs[3]);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500125 void setMaxPages_TestingOnly(uint32_t maxPages);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500126
127private:
Robert Phillips5a66efb2018-03-07 15:13:18 -0500128 bool initAtlas(GrMaskFormat);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500129
Robert Phillips5a66efb2018-03-07 15:13:18 -0500130 // There is a 1:1 mapping between GrMaskFormats and atlas indices
131 static int MaskFormatToAtlasIndex(GrMaskFormat format) {
132 static const int sAtlasIndices[] = {
133 kA8_GrMaskFormat,
134 kA565_GrMaskFormat,
135 kARGB_GrMaskFormat,
136 };
137 static_assert(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, "array_size_mismatch");
138
139 SkASSERT(sAtlasIndices[format] < kMaskFormatCount);
140 return sAtlasIndices[format];
141 }
142
143 GrDrawOpAtlas* getAtlas(GrMaskFormat format) const {
Timothy Liang91e260f2018-06-15 13:28:35 -0400144 format = this->resolveMaskFormat(format);
Robert Phillips5a66efb2018-03-07 15:13:18 -0500145 int atlasIndex = MaskFormatToAtlasIndex(format);
146 SkASSERT(fAtlases[atlasIndex]);
147 return fAtlases[atlasIndex].get();
148 }
149
150 sk_sp<const GrCaps> fCaps;
151 GrDrawOpAtlas::AllowMultitexturing fAllowMultitexturing;
152 std::unique_ptr<GrDrawOpAtlas> fAtlases[kMaskFormatCount];
153 GrDrawOpAtlasConfig fAtlasConfigs[kMaskFormatCount];
154 SkScalar fGlyphSizeLimit;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500155 GrProxyProvider* fProxyProvider;
156 GrGlyphCache* fGlyphCache;
157
Robert Phillips5a66efb2018-03-07 15:13:18 -0500158 typedef GrOnFlushCallbackObject INHERITED;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500159};
160
161#endif // GrAtlasManager_DEFINED