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