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 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 16 | struct GrGlyph; |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 17 | class GrTextStrike; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 18 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 19 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 20 | /** The GrAtlasManager manages the lifetime of and access to GrDrawOpAtlases. |
| 21 | * It is only available at flush and only via the GrOpFlushState. |
| 22 | * |
| 23 | * This implies that all of the advanced atlasManager functionality (i.e., |
| 24 | * adding glyphs to the atlas) are only available at flush time. |
| 25 | */ |
| 26 | class GrAtlasManager : public GrOnFlushCallbackObject { |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 27 | public: |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 28 | GrAtlasManager(GrProxyProvider*, GrStrikeCache*, |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 29 | size_t maxTextureBytes, GrDrawOpAtlas::AllowMultitexturing); |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 30 | ~GrAtlasManager() override; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 31 | |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 32 | // Change an expected 565 mask format to 8888 if 565 is not supported (will happen when using |
| 33 | // Metal on macOS). The actual conversion of the data is handled in get_packed_glyph_image() in |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 34 | // GrStrikeCache.cpp |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 35 | GrMaskFormat resolveMaskFormat(GrMaskFormat format) const { |
| 36 | if (kA565_GrMaskFormat == format && |
| 37 | !fProxyProvider->caps()->isConfigTexturable(kRGB_565_GrPixelConfig)) { |
| 38 | format = kARGB_GrMaskFormat; |
| 39 | } |
| 40 | return format; |
| 41 | } |
| 42 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 43 | // if getProxies returns nullptr, the client must not try to use other functions on the |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 44 | // GrStrikeCache 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] | 45 | // functions which use the atlas. Note that we can have proxies available but none active |
| 46 | // (i.e., none instantiated). |
| 47 | const sk_sp<GrTextureProxy>* getProxies(GrMaskFormat format, unsigned int* numActiveProxies) { |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 48 | format = this->resolveMaskFormat(format); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 49 | if (this->initAtlas(format)) { |
Jim Van Verth | cbeae03 | 2018-05-16 14:54:41 -0400 | [diff] [blame] | 50 | *numActiveProxies = this->getAtlas(format)->numActivePages(); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 51 | return this->getAtlas(format)->getProxies(); |
| 52 | } |
Jim Van Verth | cbeae03 | 2018-05-16 14:54:41 -0400 | [diff] [blame] | 53 | *numActiveProxies = 0; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 54 | return nullptr; |
| 55 | } |
| 56 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 57 | void freeAll(); |
| 58 | |
| 59 | bool hasGlyph(GrGlyph* glyph); |
| 60 | |
| 61 | // To ensure the GrDrawOpAtlas does not evict the Glyph Mask from its texture backing store, |
| 62 | // the client must pass in the current op token along with the GrGlyph. |
| 63 | // A BulkUseTokenUpdater 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(GrDrawOpAtlas::BulkUseTokenUpdater*, GrGlyph*, |
| 67 | GrDeferredUploadToken); |
| 68 | |
| 69 | void setUseTokenBulk(const GrDrawOpAtlas::BulkUseTokenUpdater& updater, |
| 70 | GrDeferredUploadToken token, |
| 71 | GrMaskFormat format) { |
| 72 | this->getAtlas(format)->setLastUseTokenBulk(updater, token); |
| 73 | } |
| 74 | |
| 75 | // add to texture atlas that matches this format |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 76 | GrDrawOpAtlas::ErrorCode addToAtlas( |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 77 | GrResourceProvider*, GrStrikeCache*, GrTextStrike*, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 78 | GrDrawOpAtlas::AtlasID*, GrDeferredUploadTarget*, GrMaskFormat, |
| 79 | int width, int height, const void* image, SkIPoint16* loc); |
| 80 | |
| 81 | // Some clients may wish to verify the integrity of the texture backing store of the |
| 82 | // GrDrawOpAtlas. The atlasGeneration returned below is a monotonically increasing number which |
| 83 | // changes every time something is removed from the texture backing store. |
| 84 | uint64_t atlasGeneration(GrMaskFormat format) const { |
| 85 | return this->getAtlas(format)->atlasGeneration(); |
| 86 | } |
| 87 | |
| 88 | // GrOnFlushCallbackObject overrides |
| 89 | |
| 90 | void preFlush(GrOnFlushResourceProvider* onFlushResourceProvider, const uint32_t*, int, |
| 91 | SkTArray<sk_sp<GrRenderTargetContext>>*) override { |
| 92 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 93 | if (fAtlases[i]) { |
| 94 | fAtlases[i]->instantiate(onFlushResourceProvider); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void postFlush(GrDeferredUploadToken startTokenForNextFlush, |
| 100 | const uint32_t* opListIDs, int numOpListIDs) override { |
| 101 | for (int i = 0; i < kMaskFormatCount; ++i) { |
| 102 | if (fAtlases[i]) { |
| 103 | fAtlases[i]->compact(startTokenForNextFlush); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // The AtlasGlyph cache always survives freeGpuResources so we want it to remain in the active |
| 109 | // OnFlushCallbackObject list |
| 110 | bool retainOnFreeGpuResources() override { return true; } |
| 111 | |
| 112 | /////////////////////////////////////////////////////////////////////////// |
| 113 | // Functions intended debug only |
| 114 | #ifdef SK_DEBUG |
| 115 | void dump(GrContext* context) const; |
| 116 | #endif |
| 117 | |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 118 | void setAtlasSizesToMinimum_ForTesting(); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 119 | void setMaxPages_TestingOnly(uint32_t maxPages); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 120 | |
| 121 | private: |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 122 | bool initAtlas(GrMaskFormat); |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 123 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 124 | // There is a 1:1 mapping between GrMaskFormats and atlas indices |
| 125 | static int MaskFormatToAtlasIndex(GrMaskFormat format) { |
| 126 | static const int sAtlasIndices[] = { |
| 127 | kA8_GrMaskFormat, |
| 128 | kA565_GrMaskFormat, |
| 129 | kARGB_GrMaskFormat, |
| 130 | }; |
| 131 | static_assert(SK_ARRAY_COUNT(sAtlasIndices) == kMaskFormatCount, "array_size_mismatch"); |
| 132 | |
| 133 | SkASSERT(sAtlasIndices[format] < kMaskFormatCount); |
| 134 | return sAtlasIndices[format]; |
| 135 | } |
| 136 | |
| 137 | GrDrawOpAtlas* getAtlas(GrMaskFormat format) const { |
Timothy Liang | 91e260f | 2018-06-15 13:28:35 -0400 | [diff] [blame] | 138 | format = this->resolveMaskFormat(format); |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 139 | int atlasIndex = MaskFormatToAtlasIndex(format); |
| 140 | SkASSERT(fAtlases[atlasIndex]); |
| 141 | return fAtlases[atlasIndex].get(); |
| 142 | } |
| 143 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 144 | GrDrawOpAtlas::AllowMultitexturing fAllowMultitexturing; |
| 145 | std::unique_ptr<GrDrawOpAtlas> fAtlases[kMaskFormatCount]; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 146 | GrProxyProvider* fProxyProvider; |
Herb Derby | 3c4d533 | 2018-09-07 15:27:57 -0400 | [diff] [blame] | 147 | sk_sp<const GrCaps> fCaps; |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 148 | GrStrikeCache* fGlyphCache; |
Jim Van Verth | f6206f9 | 2018-12-14 08:22:24 -0500 | [diff] [blame] | 149 | GrDrawOpAtlasConfig fAtlasConfig; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 150 | |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 151 | typedef GrOnFlushCallbackObject INHERITED; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | #endif // GrAtlasManager_DEFINED |