Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 1 | /* |
| 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 Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame^] | 11 | #include "GrCaps.h" |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 12 | #include "GrDrawOpAtlas.h" |
| 13 | #include "GrOnFlushResourceProvider.h" |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame^] | 14 | #include "GrProxyProvider.h" |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 15 | |
| 16 | class GrAtlasGlypCache; |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 17 | class GrTextStrike; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 18 | struct GrGlyph; |
| 19 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 20 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 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 | */ |
| 27 | class GrAtlasManager : public GrOnFlushCallbackObject { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 28 | public: |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 29 | GrAtlasManager(GrProxyProvider*, GrGlyphCache*, |
| 30 | float maxTextureBytes, GrDrawOpAtlas::AllowMultitexturing); |
| 31 | ~GrAtlasManager() override; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 32 | |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame^] | 33 | // 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 Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 44 | // 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 Verth | cbeae03 | 2018-05-16 14:54:41 -0400 | [diff] [blame] | 46 | // 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 Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame^] | 49 | format = this->resolveMaskFormat(format); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 50 | if (this->initAtlas(format)) { |
Jim Van Verth | cbeae03 | 2018-05-16 14:54:41 -0400 | [diff] [blame] | 51 | *numActiveProxies = this->getAtlas(format)->numActivePages(); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 52 | return this->getAtlas(format)->getProxies(); |
| 53 | } |
Jim Van Verth | cbeae03 | 2018-05-16 14:54:41 -0400 | [diff] [blame] | 54 | *numActiveProxies = 0; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | SkScalar getGlyphSizeLimit() const { return fGlyphSizeLimit; } |
| 59 | |
Khushal | fa8ff09 | 2018-06-06 17:46:38 -0700 | [diff] [blame] | 60 | static void ComputeAtlasLimits(int maxTextureSize, size_t maxTextureBytes, int* maxDim, |
| 61 | int* minDim, int* maxPlot, int* minPlot); |
Robert Phillips | af4adef | 2018-03-07 11:59:37 -0500 | [diff] [blame] | 62 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 63 | 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 Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 82 | GrDrawOpAtlas::ErrorCode addToAtlas( |
| 83 | GrResourceProvider*, GrGlyphCache*, GrTextStrike*, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 84 | 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 Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 125 | void setMaxPages_TestingOnly(uint32_t maxPages); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 126 | |
| 127 | private: |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 128 | bool initAtlas(GrMaskFormat); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 129 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 130 | // 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 Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame^] | 144 | format = this->resolveMaskFormat(format); |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 145 | 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 Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 155 | GrProxyProvider* fProxyProvider; |
| 156 | GrGlyphCache* fGlyphCache; |
| 157 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 158 | typedef GrOnFlushCallbackObject INHERITED; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | #endif // GrAtlasManager_DEFINED |