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]; |
| 198 | Plot* plot = fPages[pd.fPageIndex].fPlotArray[pd.fPlotIndex].get(); |
| 199 | this->makeMRU(plot, pd.fPageIndex); |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 200 | plot->setLastUseToken(token); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 203 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame^] | 204 | void compact(GrDrawOpUploadToken startTokenForNextFlush); |
| 205 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 206 | static constexpr auto kGlyphMaxDim = 256; |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 207 | static bool GlyphTooLargeForAtlas(int width, int height) { |
| 208 | return width > kGlyphMaxDim || height > kGlyphMaxDim; |
| 209 | } |
| 210 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 211 | static uint32_t GetPageIndexFromID(AtlasID id) { |
| 212 | return id & 0xff; |
| 213 | } |
| 214 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 215 | private: |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 216 | GrDrawOpAtlas(GrContext*, GrPixelConfig config, int width, int height, |
| 217 | int numPlotsX, int numPlotsY); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 218 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 219 | /** |
| 220 | * The backing GrTexture for a GrDrawOpAtlas is broken into a spatial grid of Plots. The Plots |
| 221 | * keep track of subimage placement via their GrRectanizer. A Plot manages the lifetime of its |
| 222 | * data using two tokens, a last use token and a last upload token. Once a Plot is "full" (i.e. |
| 223 | * there is no room for the new subimage according to the GrRectanizer), it can no longer be |
| 224 | * used unless the last use of the Plot has already been flushed through to the gpu. |
| 225 | */ |
| 226 | class Plot : public SkRefCnt { |
| 227 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Plot); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 228 | |
| 229 | public: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 230 | /** index() is a unique id for the plot relative to the owning GrAtlas and page. */ |
| 231 | uint32_t index() const { return fPlotIndex; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 232 | /** |
| 233 | * genID() is incremented when the plot is evicted due to a atlas spill. It is used to know |
| 234 | * if a particular subimage is still present in the atlas. |
| 235 | */ |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 236 | uint64_t genID() const { return fGenID; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 237 | GrDrawOpAtlas::AtlasID id() const { |
| 238 | SkASSERT(GrDrawOpAtlas::kInvalidAtlasID != fID); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 239 | return fID; |
| 240 | } |
| 241 | SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; }) |
| 242 | |
| 243 | bool addSubImage(int width, int height, const void* image, SkIPoint16* loc); |
| 244 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 245 | /** |
| 246 | * To manage the lifetime of a plot, we use two tokens. We use the last upload token to |
| 247 | * know when we can 'piggy back' uploads, i.e. if the last upload hasn't been flushed to |
| 248 | * the gpu, we don't need to issue a new upload even if we update the cpu backing store. We |
| 249 | * use lastUse to determine when we can evict a plot from the cache, i.e. if the last use |
| 250 | * has already flushed through the gpu then we can reuse the plot. |
| 251 | */ |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 252 | GrDrawOpUploadToken lastUploadToken() const { return fLastUpload; } |
| 253 | GrDrawOpUploadToken lastUseToken() const { return fLastUse; } |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 254 | void setLastUploadToken(GrDrawOpUploadToken token) { fLastUpload = token; } |
| 255 | void setLastUseToken(GrDrawOpUploadToken token) { fLastUse = token; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 256 | |
Robert Phillips | acaa607 | 2017-07-28 10:54:53 -0400 | [diff] [blame] | 257 | void uploadToTexture(GrDrawOp::WritePixelsFn&, GrTextureProxy*); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 258 | void resetRects(); |
| 259 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame^] | 260 | int flushesSinceLastUsed() { return fFlushesSinceLastUse; } |
| 261 | void resetFlushesSinceLastUsed() { fFlushesSinceLastUse = 0; } |
| 262 | void incFlushesSinceLastUsed() { fFlushesSinceLastUse++; } |
| 263 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 264 | private: |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 265 | 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] | 266 | GrPixelConfig config); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 267 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 268 | ~Plot() override; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 269 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 270 | /** |
| 271 | * Create a clone of this plot. The cloned plot will take the place of the current plot in |
| 272 | * the atlas |
| 273 | */ |
| 274 | Plot* clone() const { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 275 | return new Plot(fPageIndex, fPlotIndex, fGenID + 1, fX, fY, fWidth, fHeight, fConfig); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 278 | static GrDrawOpAtlas::AtlasID CreateId(uint32_t pageIdx, uint32_t plotIdx, |
| 279 | uint64_t generation) { |
| 280 | SkASSERT(pageIdx < (1 << 8)); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 281 | SkASSERT(pageIdx < kMaxPages); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 282 | SkASSERT(plotIdx < (1 << 8)); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 283 | SkASSERT(generation < ((uint64_t)1 << 48)); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 284 | return generation << 16 | plotIdx << 8 | pageIdx; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 287 | GrDrawOpUploadToken fLastUpload; |
| 288 | GrDrawOpUploadToken fLastUse; |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame^] | 289 | // the number of flushes since this plot has been last used |
| 290 | int fFlushesSinceLastUse; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 291 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 292 | struct { |
| 293 | const uint32_t fPageIndex : 16; |
| 294 | const uint32_t fPlotIndex : 16; |
| 295 | }; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 296 | uint64_t fGenID; |
| 297 | GrDrawOpAtlas::AtlasID fID; |
| 298 | unsigned char* fData; |
| 299 | const int fWidth; |
| 300 | const int fHeight; |
| 301 | const int fX; |
| 302 | const int fY; |
| 303 | GrRectanizer* fRects; |
| 304 | const SkIPoint16 fOffset; // the offset of the plot in the backing texture |
| 305 | const GrPixelConfig fConfig; |
| 306 | const size_t fBytesPerPixel; |
| 307 | SkIRect fDirtyRect; |
| 308 | SkDEBUGCODE(bool fDirty); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 309 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 310 | friend class GrDrawOpAtlas; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 311 | |
| 312 | typedef SkRefCnt INHERITED; |
| 313 | }; |
| 314 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 315 | typedef SkTInternalLList<Plot> PlotList; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 316 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 317 | static uint32_t GetPlotIndexFromID(AtlasID id) { |
| 318 | return (id >> 8) & 0xff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 319 | } |
| 320 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 321 | // top 48 bits are reserved for the generation ID |
| 322 | static uint64_t GetGenerationFromID(AtlasID id) { |
| 323 | return (id >> 16) & 0xffffffffffff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 326 | inline bool updatePlot(GrDrawOp::Target*, AtlasID*, Plot*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 327 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 328 | inline void makeMRU(Plot* plot, int pageIdx) { |
| 329 | if (fPages[pageIdx].fPlotList.head() == plot) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 333 | fPages[pageIdx].fPlotList.remove(plot); |
| 334 | fPages[pageIdx].fPlotList.addToHead(plot); |
| 335 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame^] | 336 | // No MRU update for pages -- since we will always try to add from |
| 337 | // the front and remove from the back there is no need for MRU. |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 338 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 339 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 340 | bool createNewPage(); |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame^] | 341 | void deleteLastPage(); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 342 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 343 | inline void processEviction(AtlasID); |
| 344 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 345 | GrContext* fContext; |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 346 | GrPixelConfig fPixelConfig; |
| 347 | int fTextureWidth; |
| 348 | int fTextureHeight; |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 349 | int fPlotWidth; |
| 350 | int fPlotHeight; |
| 351 | SkDEBUGCODE(uint32_t fNumPlots;) |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 352 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 353 | uint64_t fAtlasGeneration; |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame^] | 354 | // nextTokenToFlush() value at the end of the previous flush |
| 355 | GrDrawOpUploadToken fPrevFlushToken; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 356 | |
| 357 | struct EvictionData { |
| 358 | EvictionFunc fFunc; |
| 359 | void* fData; |
| 360 | }; |
| 361 | |
| 362 | SkTDArray<EvictionData> fEvictionCallbacks; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 363 | |
| 364 | struct Page { |
| 365 | // allocated array of Plots |
| 366 | std::unique_ptr<sk_sp<Plot>[]> fPlotArray; |
| 367 | // LRU list of Plots (MRU at head - LRU at tail) |
| 368 | PlotList fPlotList; |
| 369 | }; |
| 370 | // proxies kept separate to make it easier to pass them up to client |
| 371 | sk_sp<GrTextureProxy> fProxies[kMaxPages]; |
| 372 | Page fPages[kMaxPages]; |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 373 | uint32_t fNumPages; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 374 | }; |
| 375 | |
| 376 | #endif |