joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 8 | #ifndef GrDrawOpAtlas_DEFINED |
| 9 | #define GrDrawOpAtlas_DEFINED |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 10 | |
Mike Reed | 1fda024 | 2018-04-04 15:39:46 -0400 | [diff] [blame] | 11 | #include "SkIPoint16.h" |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 12 | #include "SkTDArray.h" |
| 13 | #include "SkTInternalLList.h" |
| 14 | |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 15 | #include "ops/GrDrawOp.h" |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 16 | |
Robert Phillips | cd5099c | 2018-02-09 09:56:56 -0500 | [diff] [blame] | 17 | class GrOnFlushResourceProvider; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 18 | class GrRectanizer; |
| 19 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 20 | struct GrDrawOpAtlasConfig { |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 21 | int numPlotsX() const { return fWidth / fPlotWidth; } |
| 22 | int numPlotsY() const { return fHeight / fPlotWidth; } |
| 23 | int fWidth; |
| 24 | int fHeight; |
| 25 | int fPlotWidth; |
| 26 | int fPlotHeight; |
| 27 | }; |
| 28 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 29 | /** |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 30 | * This class manages one or more atlas textures on behalf of GrDrawOps. The draw ops that use the |
| 31 | * atlas perform texture uploads when preparing their draws during flush. The class provides |
| 32 | * facilities for using GrDrawOpUploadToken to detect data hazards. Op's uploads are performed in |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 33 | * "ASAP" mode until it is impossible to add data without overwriting texels read by draws that |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 34 | * have not yet executed on the gpu. At that point, the atlas will attempt to allocate a new |
| 35 | * atlas texture (or "page") of the same size, up to a maximum number of textures, and upload |
| 36 | * to that texture. If that's not possible, the uploads are performed "inline" between draws. If a |
| 37 | * single draw would use enough subimage space to overflow the atlas texture then the atlas will |
| 38 | * fail to add a subimage. This gives the op the chance to end the draw and begin a new one. |
| 39 | * Additional uploads will then succeed in inline mode. |
| 40 | * |
| 41 | * When the atlas has multiple pages, new uploads are prioritized to the lower index pages, i.e., |
| 42 | * it will try to upload to page 0 before page 1 or 2. To keep the atlas from continually using |
| 43 | * excess space, periodic garbage collection is needed to shift data from the higher index pages to |
| 44 | * the lower ones, and then eventually remove any pages that are no longer in use. "In use" is |
| 45 | * determined by using the GrDrawUploadToken system: After a flush each subarea of the page |
| 46 | * is checked to see whether it was used in that flush; if it is not, a counter is incremented. |
| 47 | * Once that counter reaches a threshold that subarea is considered to be no longer in use. |
| 48 | * |
| 49 | * Garbage collection is initiated by the GrDrawOpAtlas's client via the compact() method. One |
| 50 | * solution is to make the client a subclass of GrOnFlushCallbackObject, register it with the |
| 51 | * GrContext via addOnFlushCallbackObject(), and the client's postFlush() method calls compact() |
| 52 | * and passes in the given GrDrawUploadToken. |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 53 | */ |
| 54 | class GrDrawOpAtlas { |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 55 | private: |
| 56 | static constexpr auto kMaxMultitexturePages = 4; |
| 57 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 58 | public: |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 59 | /** Is the atlas allowed to use more than one texture? */ |
| 60 | enum class AllowMultitexturing : bool { kNo, kYes }; |
| 61 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 62 | /** |
| 63 | * An AtlasID is an opaque handle which callers can use to determine if the atlas contains |
| 64 | * a specific piece of data. |
| 65 | */ |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 66 | typedef uint64_t AtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 67 | static const uint32_t kInvalidAtlasID = 0; |
| 68 | static const uint64_t kInvalidAtlasGeneration = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 69 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 70 | /** |
| 71 | * A function pointer for use as a callback during eviction. Whenever GrDrawOpAtlas evicts a |
| 72 | * specific AtlasID, it will call all of the registered listeners so they can process the |
| 73 | * eviction. |
| 74 | */ |
| 75 | typedef void (*EvictionFunc)(GrDrawOpAtlas::AtlasID, void*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 76 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 77 | /** |
| 78 | * Returns a GrDrawOpAtlas. This function can be called anywhere, but the returned atlas |
| 79 | * should only be used inside of GrMeshDrawOp::onPrepareDraws. |
| 80 | * @param GrPixelConfig The pixel config which this atlas will store |
| 81 | * @param width width in pixels of the atlas |
| 82 | * @param height height in pixels of the atlas |
| 83 | * @param numPlotsX The number of plots the atlas should be broken up into in the X |
| 84 | * direction |
| 85 | * @param numPlotsY The number of plots the atlas should be broken up into in the Y |
| 86 | * direction |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 87 | * @param allowMultitexturing Can the atlas use more than one texture. |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 88 | * @param func An eviction function which will be called whenever the atlas has to |
| 89 | * evict data |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 90 | * @param data User supplied data which will be passed into func whenever an |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 91 | * eviction occurs |
| 92 | * @return An initialized GrDrawOpAtlas, or nullptr if creation fails |
| 93 | */ |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 94 | static std::unique_ptr<GrDrawOpAtlas> Make(GrProxyProvider*, GrPixelConfig, |
| 95 | int width, int height, |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 96 | int numPlotsX, int numPlotsY, |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 97 | AllowMultitexturing allowMultitexturing, |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 98 | GrDrawOpAtlas::EvictionFunc func, void* data); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 99 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 100 | /** |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 101 | * Adds a width x height subimage to the atlas. Upon success it returns 'kSucceeded' and returns |
| 102 | * the ID and the subimage's coordinates in the backing texture. 'kTryAgain' is returned if |
| 103 | * the subimage cannot fit in the atlas without overwriting texels that will be read in the |
| 104 | * current draw. This indicates that the op should end its current draw and begin another |
| 105 | * before adding more data. Upon success, an upload of the provided image data will have |
| 106 | * been added to the GrDrawOp::Target, in "asap" mode if possible, otherwise in "inline" mode. |
| 107 | * Successive uploads in either mode may be consolidated. |
| 108 | * 'kError' will be returned when some unrecoverable error was encountered while trying to |
| 109 | * add the subimage. In this case the op being created should be discarded. |
| 110 | * |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 111 | * NOTE: When the GrDrawOp prepares a draw that reads from the atlas, it must immediately call |
| 112 | * 'setUseToken' with the currentToken from the GrDrawOp::Target, otherwise the next call to |
| 113 | * addToAtlas might cause the previous data to be overwritten before it has been read. |
| 114 | */ |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 115 | |
| 116 | enum class ErrorCode { |
| 117 | kError, |
| 118 | kSucceeded, |
| 119 | kTryAgain |
| 120 | }; |
| 121 | |
| 122 | ErrorCode addToAtlas(GrResourceProvider*, AtlasID*, GrDeferredUploadTarget*, |
| 123 | int width, int height, |
| 124 | const void* image, SkIPoint16* loc); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 125 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 126 | const sk_sp<GrTextureProxy>* getProxies() const { return fProxies; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 127 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 128 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 129 | |
| 130 | inline bool hasID(AtlasID id) { |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 131 | if (kInvalidAtlasID == id) { |
| 132 | return false; |
| 133 | } |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 134 | uint32_t plot = GetPlotIndexFromID(id); |
| 135 | SkASSERT(plot < fNumPlots); |
| 136 | uint32_t page = GetPageIndexFromID(id); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 137 | SkASSERT(page < fNumActivePages); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 138 | return fPages[page].fPlotArray[plot]->genID() == GetGenerationFromID(id); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 139 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 140 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 141 | /** To ensure the atlas does not evict a given entry, the client must set the last use token. */ |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 142 | inline void setLastUseToken(AtlasID id, GrDeferredUploadToken token) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 143 | SkASSERT(this->hasID(id)); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 144 | uint32_t plotIdx = GetPlotIndexFromID(id); |
| 145 | SkASSERT(plotIdx < fNumPlots); |
| 146 | uint32_t pageIdx = GetPageIndexFromID(id); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 147 | SkASSERT(pageIdx < fNumActivePages); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 148 | Plot* plot = fPages[pageIdx].fPlotArray[plotIdx].get(); |
| 149 | this->makeMRU(plot, pageIdx); |
| 150 | plot->setLastUseToken(token); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | inline void registerEvictionCallback(EvictionFunc func, void* userData) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 154 | EvictionData* data = fEvictionCallbacks.append(); |
| 155 | data->fFunc = func; |
| 156 | data->fData = userData; |
| 157 | } |
| 158 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 159 | uint32_t numActivePages() { return fNumActivePages; } |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 160 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 161 | /** |
| 162 | * A class which can be handed back to GrDrawOpAtlas for updating last use tokens in bulk. The |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 163 | * current max number of plots per page the GrDrawOpAtlas can handle is 32. If in the future |
| 164 | * this is insufficient then we can move to a 64 bit int. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 165 | */ |
| 166 | class BulkUseTokenUpdater { |
| 167 | public: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 168 | BulkUseTokenUpdater() { |
| 169 | memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated)); |
| 170 | } |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 171 | BulkUseTokenUpdater(const BulkUseTokenUpdater& that) |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 172 | : fPlotsToUpdate(that.fPlotsToUpdate) { |
| 173 | memcpy(fPlotAlreadyUpdated, that.fPlotAlreadyUpdated, sizeof(fPlotAlreadyUpdated)); |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 174 | } |
| 175 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 176 | void add(AtlasID id) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 177 | int index = GrDrawOpAtlas::GetPlotIndexFromID(id); |
| 178 | int pageIdx = GrDrawOpAtlas::GetPageIndexFromID(id); |
| 179 | if (!this->find(pageIdx, index)) { |
| 180 | this->set(pageIdx, index); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | void reset() { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 185 | fPlotsToUpdate.reset(); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 186 | memset(fPlotAlreadyUpdated, 0, sizeof(fPlotAlreadyUpdated)); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 189 | struct PlotData { |
| 190 | PlotData(int pageIdx, int plotIdx) : fPageIndex(pageIdx), fPlotIndex(plotIdx) {} |
| 191 | uint32_t fPageIndex; |
| 192 | uint32_t fPlotIndex; |
| 193 | }; |
| 194 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 195 | private: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 196 | bool find(int pageIdx, int index) const { |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 197 | SkASSERT(index < kMaxPlots); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 198 | return (fPlotAlreadyUpdated[pageIdx] >> index) & 1; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 201 | void set(int pageIdx, int index) { |
| 202 | SkASSERT(!this->find(pageIdx, index)); |
| 203 | fPlotAlreadyUpdated[pageIdx] |= (1 << index); |
| 204 | fPlotsToUpdate.push_back(PlotData(pageIdx, index)); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 207 | static constexpr int kMinItems = 4; |
| 208 | static constexpr int kMaxPlots = 32; |
| 209 | SkSTArray<kMinItems, PlotData, true> fPlotsToUpdate; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 210 | uint32_t fPlotAlreadyUpdated[kMaxMultitexturePages]; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 211 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 212 | friend class GrDrawOpAtlas; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 215 | void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrDeferredUploadToken token) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 216 | int count = updater.fPlotsToUpdate.count(); |
| 217 | for (int i = 0; i < count; i++) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 218 | const BulkUseTokenUpdater::PlotData& pd = updater.fPlotsToUpdate[i]; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 219 | // it's possible we've added a plot to the updater and subsequently the plot's page |
| 220 | // was deleted -- so we check to prevent a crash |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 221 | if (pd.fPageIndex < fNumActivePages) { |
Jim Van Verth | 6ca9c6f | 2017-09-27 18:04:34 -0400 | [diff] [blame] | 222 | Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get(); |
| 223 | this->makeMRU(plot, pd.fPageIndex); |
| 224 | plot->setLastUseToken(token); |
| 225 | } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 228 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 229 | void compact(GrDeferredUploadToken startTokenForNextFlush); |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 230 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 231 | static constexpr auto kGlyphMaxDim = 256; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 232 | static bool GlyphTooLargeForAtlas(int width, int height) { |
| 233 | return width > kGlyphMaxDim || height > kGlyphMaxDim; |
| 234 | } |
| 235 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 236 | static uint32_t GetPageIndexFromID(AtlasID id) { |
| 237 | return id & 0xff; |
| 238 | } |
| 239 | |
Robert Phillips | cd5099c | 2018-02-09 09:56:56 -0500 | [diff] [blame] | 240 | void instantiate(GrOnFlushResourceProvider*); |
| 241 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 242 | uint32_t maxPages() const { |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 243 | return fMaxPages; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 246 | int numAllocated_TestingOnly() const; |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 247 | void setMaxPages_TestingOnly(uint32_t maxPages); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 248 | |
| 249 | private: |
| 250 | GrDrawOpAtlas(GrProxyProvider*, GrPixelConfig, int width, int height, int numPlotsX, |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 251 | int numPlotsY, AllowMultitexturing allowMultitexturing); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 252 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 253 | /** |
| 254 | * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots |
| 255 | * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its |
| 256 | * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e. |
| 257 | * there is no room for the new subimage according to the GrRectanizer), it can no longer be |
| 258 | * used unless the last use of the Plot has already been flushed through to the gpu. |
| 259 | */ |
| 260 | class Plot : public SkRefCnt { |
| 261 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 262 | |
| 263 | public: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 264 | /** index() is a unique id for the plot relative to the owning GrAtlas and page. */ |
| 265 | uint32_t index() const { return fPlotIndex; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 266 | /** |
| 267 | * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know |
| 268 | * if a particular subimage is still present in the atlas. |
| 269 | */ |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 270 | uint64_t genID() const { return fGenID; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 271 | GrDrawOpAtlas::AtlasID id() const { |
| 272 | SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 273 | return fID; |
| 274 | } |
| 275 | SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; }) |
| 276 | |
| 277 | bool addSubImage(int width, int height, const void* image, SkIPoint16* loc); |
| 278 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 279 | /** |
| 280 | * To manage the lifetime of a plot, we use two tokens. We use the last upload token to |
| 281 | * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to |
| 282 | * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We |
| 283 | * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use |
| 284 | * has already flushed through the gpu then we can reuse the plot. |
| 285 | */ |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 286 | GrDeferredUploadToken lastUploadToken() const { return fLastUpload; } |
| 287 | GrDeferredUploadToken lastUseToken() const { return fLastUse; } |
| 288 | void setLastUploadToken(GrDeferredUploadToken token) { fLastUpload = token; } |
| 289 | void setLastUseToken(GrDeferredUploadToken token) { fLastUse = token; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 290 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 291 | void uploadToTexture(GrDeferredTextureUploadWritePixelsFn&, GrTextureProxy*); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 292 | void resetRects(); |
| 293 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 294 | int flushesSinceLastUsed() { return fFlushesSinceLastUse; } |
| 295 | void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; } |
| 296 | void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; } |
| 297 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 298 | private: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 299 | Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY, int width, int height, |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 300 | GrPixelConfig config); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 301 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 302 | ~Plot() override; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 303 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 304 | /** |
| 305 | * Create a clone of this plot. The cloned plot will take the place of the current plot in |
| 306 | * the atlas |
| 307 | */ |
| 308 | Plot* clone() const { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 309 | return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 312 | static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx, |
| 313 | uint64_t generation) { |
| 314 | SkASSERT(pageIdx < (1 << 8)); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 315 | SkASSERT(pageIdx < kMaxMultitexturePages); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 316 | SkASSERT(plotIdx < (1 << 8)); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 317 | SkASSERT(generation < ((uint64_t)1 << 48)); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 318 | return generation << 16 | plotIdx << 8 | pageIdx; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 321 | GrDeferredUploadToken fLastUpload; |
| 322 | GrDeferredUploadToken fLastUse; |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 323 | // the number of flushes since this plot has been last used |
| 324 | int fFlushesSinceLastUse; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 325 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 326 | struct { |
| 327 | const uint32_t fPageIndex : 16; |
| 328 | const uint32_t fPlotIndex : 16; |
| 329 | }; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 330 | uint64_t fGenID; |
| 331 | GrDrawOpAtlas::AtlasID fID; |
| 332 | unsigned char* fData; |
| 333 | const int fWidth; |
| 334 | const int fHeight; |
| 335 | const int fX; |
| 336 | const int fY; |
| 337 | GrRectanizer* fRects; |
| 338 | const SkIPoint16 fOffset; // the offset of the plot in the backing texture |
| 339 | const GrPixelConfig fConfig; |
| 340 | const size_t fBytesPerPixel; |
| 341 | SkIRect fDirtyRect; |
| 342 | SkDEBUGCODE(bool fDirty); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 343 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 344 | friend class GrDrawOpAtlas; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 345 | |
| 346 | typedef SkRefCnt INHERITED; |
| 347 | }; |
| 348 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 349 | typedef SkTInternalLList<Plot> PlotList; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 350 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 351 | static uint32_t GetPlotIndexFromID(AtlasID id) { |
| 352 | return (id >> 8) & 0xff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 353 | } |
| 354 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 355 | // top 48 bits are reserved for the generation ID |
| 356 | static uint64_t GetGenerationFromID(AtlasID id) { |
| 357 | return (id >> 16) & 0xffffffffffff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 360 | inline bool updatePlot(GrDeferredUploadTarget*, AtlasID*, Plot*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 361 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 362 | inline void makeMRU(Plot* plot, int pageIdx) { |
| 363 | if (fPages[pageIdx].fPlotList.head() == plot) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 367 | fPages[pageIdx].fPlotList.remove(plot); |
| 368 | fPages[pageIdx].fPlotList.addToHead(plot); |
| 369 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 370 | // No MRU update for pages -- since we will always try to add from |
| 371 | // the front and remove from the back there is no need for MRU. |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 372 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 373 | |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 374 | bool uploadToPage(unsigned int pageIdx, AtlasID* id, GrDeferredUploadTarget* target, |
| 375 | int width, int height, const void* image, SkIPoint16* loc); |
| 376 | |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 377 | bool createPages(GrProxyProvider*); |
| 378 | bool activateNewPage(GrResourceProvider*); |
| 379 | void deactivateLastPage(); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 380 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 381 | void processEviction(AtlasID); |
| 382 | inline void processEvictionAndResetRects(Plot* plot) { |
| 383 | this->processEviction(plot->id()); |
| 384 | plot->resetRects(); |
| 385 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 386 | |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 387 | GrPixelConfig fPixelConfig; |
| 388 | int fTextureWidth; |
| 389 | int fTextureHeight; |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 390 | int fPlotWidth; |
| 391 | int fPlotHeight; |
Jim Van Verth | 06f593c | 2018-02-20 11:30:10 -0500 | [diff] [blame] | 392 | unsigned int fNumPlots; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 393 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 394 | uint64_t fAtlasGeneration; |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 395 | // nextTokenToFlush() value at the end of the previous flush |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame] | 396 | GrDeferredUploadToken fPrevFlushToken; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 397 | |
| 398 | struct EvictionData { |
| 399 | EvictionFunc fFunc; |
| 400 | void* fData; |
| 401 | }; |
| 402 | |
| 403 | SkTDArray<EvictionData> fEvictionCallbacks; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 404 | |
| 405 | struct Page { |
| 406 | // allocated array of Plots |
| 407 | std::unique_ptr<sk_sp<Plot>[]> fPlotArray; |
| 408 | // LRU list of Plots (MRU at head - LRU at tail) |
| 409 | PlotList fPlotList; |
| 410 | }; |
| 411 | // proxies kept separate to make it easier to pass them up to client |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 412 | sk_sp<GrTextureProxy> fProxies[kMaxMultitexturePages]; |
| 413 | Page fPages[kMaxMultitexturePages]; |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 414 | uint32_t fMaxPages; |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 415 | |
| 416 | uint32_t fNumActivePages; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 417 | }; |
| 418 | |
| 419 | #endif |