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