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 | |
| 8 | #ifndef GrBatchAtlas_DEFINED |
| 9 | #define GrBatchAtlas_DEFINED |
| 10 | |
| 11 | #include "GrTexture.h" |
| 12 | #include "SkPoint.h" |
| 13 | #include "SkTDArray.h" |
| 14 | #include "SkTInternalLList.h" |
| 15 | |
joshualitt | ddd22d8 | 2016-02-16 06:47:52 -0800 | [diff] [blame] | 16 | #include "batches/GrDrawBatch.h" |
| 17 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 18 | class GrRectanizer; |
| 19 | |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 20 | struct GrBatchAtlasConfig { |
| 21 | int numPlotsX() const { return fWidth / fPlotWidth; } |
| 22 | int numPlotsY() const { return fHeight / fPlotWidth; } |
| 23 | int fWidth; |
| 24 | int fHeight; |
jvanverth | 7023a00 | 2016-02-22 11:25:32 -0800 | [diff] [blame] | 25 | int fLog2Width; |
| 26 | int fLog2Height; |
joshualitt | da04e0e | 2015-08-19 08:16:43 -0700 | [diff] [blame] | 27 | int fPlotWidth; |
| 28 | int fPlotHeight; |
| 29 | }; |
| 30 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 31 | class GrBatchAtlas { |
| 32 | public: |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 33 | // An AtlasID is an opaque handle which callers can use to determine if the atlas contains |
| 34 | // a specific piece of data |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 35 | typedef uint64_t AtlasID; |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 36 | static const uint32_t kInvalidAtlasID = 0; |
| 37 | static const uint64_t kInvalidAtlasGeneration = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 38 | |
| 39 | // A function pointer for use as a callback during eviction. Whenever GrBatchAtlas evicts a |
| 40 | // specific AtlasID, it will call all of the registered listeners so they can optionally process |
| 41 | // the eviction |
| 42 | typedef void (*EvictionFunc)(GrBatchAtlas::AtlasID, void*); |
| 43 | |
| 44 | GrBatchAtlas(GrTexture*, int numPlotsX, int numPlotsY); |
| 45 | ~GrBatchAtlas(); |
| 46 | |
| 47 | // Adds a width x height subimage to the atlas. Upon success it returns |
| 48 | // the containing GrPlot and absolute location in the backing texture. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 49 | // nullptr is returned if the subimage cannot fit in the atlas. |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 50 | // If provided, the image data will be written to the CPU-side backing bitmap. |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 51 | // NOTE: If the client intends to refer to the atlas, they should immediately call 'setUseToken' |
| 52 | // with the currentToken from the batch target, otherwise the next call to addToAtlas might |
| 53 | // cause an eviction |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 54 | bool addToAtlas(AtlasID*, GrDrawBatch::Target*, int width, int height, const void* image, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 55 | SkIPoint16* loc); |
| 56 | |
| 57 | GrTexture* getTexture() const { return fTexture; } |
| 58 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 59 | uint64_t atlasGeneration() const { return fAtlasGeneration; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 60 | |
| 61 | inline bool hasID(AtlasID id) { |
| 62 | uint32_t index = GetIndexFromID(id); |
| 63 | SkASSERT(index < fNumPlots); |
| 64 | return fPlotArray[index]->genID() == GetGenerationFromID(id); |
| 65 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 66 | |
| 67 | // To ensure the atlas does not evict a given entry, the client must set the last use token |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 68 | inline void setLastUseToken(AtlasID id, GrBatchDrawToken batchToken) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 69 | SkASSERT(this->hasID(id)); |
| 70 | uint32_t index = GetIndexFromID(id); |
| 71 | SkASSERT(index < fNumPlots); |
| 72 | this->makeMRU(fPlotArray[index]); |
| 73 | fPlotArray[index]->setLastUseToken(batchToken); |
| 74 | } |
| 75 | |
| 76 | inline void registerEvictionCallback(EvictionFunc func, void* userData) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 77 | EvictionData* data = fEvictionCallbacks.append(); |
| 78 | data->fFunc = func; |
| 79 | data->fData = userData; |
| 80 | } |
| 81 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 82 | /* |
| 83 | * A class which can be handed back to GrBatchAtlas for updating in bulk last use tokens. The |
| 84 | * current max number of plots the GrBatchAtlas can handle is 32, if in the future this is |
| 85 | * insufficient then we can move to a 64 bit int |
| 86 | */ |
| 87 | class BulkUseTokenUpdater { |
| 88 | public: |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 89 | BulkUseTokenUpdater() : fPlotAlreadyUpdated(0) {} |
joshualitt | 7e97b0b | 2015-07-31 15:18:08 -0700 | [diff] [blame] | 90 | BulkUseTokenUpdater(const BulkUseTokenUpdater& that) |
| 91 | : fPlotsToUpdate(that.fPlotsToUpdate) |
| 92 | , fPlotAlreadyUpdated(that.fPlotAlreadyUpdated) { |
| 93 | } |
| 94 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 95 | void add(AtlasID id) { |
| 96 | int index = GrBatchAtlas::GetIndexFromID(id); |
| 97 | if (!this->find(index)) { |
| 98 | this->set(index); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void reset() { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 103 | fPlotsToUpdate.reset(); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 104 | fPlotAlreadyUpdated = 0; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | bool find(int index) const { |
| 109 | SkASSERT(index < kMaxPlots); |
| 110 | return (fPlotAlreadyUpdated >> index) & 1; |
| 111 | } |
| 112 | |
| 113 | void set(int index) { |
| 114 | SkASSERT(!this->find(index)); |
| 115 | fPlotAlreadyUpdated = fPlotAlreadyUpdated | (1 << index); |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 116 | fPlotsToUpdate.push_back(index); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static const int kMinItems = 4; |
| 120 | static const int kMaxPlots = 32; |
joshualitt | 97202d2 | 2015-04-22 13:47:02 -0700 | [diff] [blame] | 121 | SkSTArray<kMinItems, int, true> fPlotsToUpdate; |
joshualitt | 8672f4d | 2015-04-21 08:03:04 -0700 | [diff] [blame] | 122 | uint32_t fPlotAlreadyUpdated; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 123 | |
| 124 | friend class GrBatchAtlas; |
| 125 | }; |
| 126 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 127 | void setLastUseTokenBulk(const BulkUseTokenUpdater& updater, GrBatchDrawToken batchToken) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 128 | int count = updater.fPlotsToUpdate.count(); |
| 129 | for (int i = 0; i < count; i++) { |
| 130 | BatchPlot* plot = fPlotArray[updater.fPlotsToUpdate[i]]; |
| 131 | this->makeMRU(plot); |
| 132 | plot->setLastUseToken(batchToken); |
| 133 | } |
| 134 | } |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 135 | |
joshualitt | 010db53 | 2015-04-21 10:07:26 -0700 | [diff] [blame] | 136 | static const int kGlyphMaxDim = 256; |
| 137 | static bool GlyphTooLargeForAtlas(int width, int height) { |
| 138 | return width > kGlyphMaxDim || height > kGlyphMaxDim; |
| 139 | } |
| 140 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 141 | private: |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 142 | // The backing GrTexture for a GrBatchAtlas is broken into a spatial grid of BatchPlots. |
| 143 | // The BatchPlots keep track of subimage placement via their GrRectanizer. A BatchPlot |
| 144 | // manages the lifetime of its data using two tokens, a last use token and a last upload token. |
| 145 | // Once a BatchPlot is "full" (i.e. there is no room for the new subimage according to the |
| 146 | // GrRectanizer), it can no longer be used unless the last use of the GrPlot has already been |
| 147 | // flushed through to the gpu. |
| 148 | class BatchPlot : public SkRefCnt { |
| 149 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(BatchPlot); |
| 150 | |
| 151 | public: |
| 152 | // index() is a unique id for the plot relative to the owning GrAtlas. genID() is a |
| 153 | // monotonically incremented number which is bumped every time this plot is |
| 154 | // evicted from the cache (i.e., there is continuity in genID() across atlas spills). |
| 155 | uint32_t index() const { return fIndex; } |
| 156 | uint64_t genID() const { return fGenID; } |
| 157 | GrBatchAtlas::AtlasID id() const { |
| 158 | SkASSERT(GrBatchAtlas::kInvalidAtlasID != fID); |
| 159 | return fID; |
| 160 | } |
| 161 | SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; }) |
| 162 | |
| 163 | bool addSubImage(int width, int height, const void* image, SkIPoint16* loc); |
| 164 | |
| 165 | // To manage the lifetime of a plot, we use two tokens. We use the last upload token to |
| 166 | // know when we can 'piggy back' uploads, ie if the last upload hasn't been flushed to gpu, |
| 167 | // we don't need to issue a new upload even if we update the cpu backing store. We use |
| 168 | // lastUse to determine when we can evict a plot from the cache, ie if the last use has |
| 169 | // already flushed through the gpu then we can reuse the plot. |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 170 | GrBatchDrawToken lastUploadToken() const { return fLastUpload; } |
| 171 | GrBatchDrawToken lastUseToken() const { return fLastUse; } |
| 172 | void setLastUploadToken(GrBatchDrawToken batchToken) { fLastUpload = batchToken; } |
| 173 | void setLastUseToken(GrBatchDrawToken batchToken) { fLastUse = batchToken; } |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 174 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 175 | void uploadToTexture(GrDrawBatch::WritePixelsFn&, GrTexture* texture); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 176 | void resetRects(); |
| 177 | |
| 178 | private: |
| 179 | BatchPlot(int index, uint64_t genID, int offX, int offY, int width, int height, |
| 180 | GrPixelConfig config); |
| 181 | |
| 182 | ~BatchPlot() override; |
| 183 | |
| 184 | // Create a clone of this plot. The cloned plot will take the place of the |
| 185 | // current plot in the atlas. |
| 186 | BatchPlot* clone() const { |
| 187 | return new BatchPlot(fIndex, fGenID+1, fX, fY, fWidth, fHeight, fConfig); |
| 188 | } |
| 189 | |
| 190 | static GrBatchAtlas::AtlasID CreateId(uint32_t index, uint64_t generation) { |
| 191 | SkASSERT(index < (1 << 16)); |
| 192 | SkASSERT(generation < ((uint64_t)1 << 48)); |
| 193 | return generation << 16 | index; |
| 194 | } |
| 195 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 196 | GrBatchDrawToken fLastUpload; |
| 197 | GrBatchDrawToken fLastUse; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 198 | |
| 199 | const uint32_t fIndex; |
| 200 | uint64_t fGenID; |
| 201 | GrBatchAtlas::AtlasID fID; |
| 202 | unsigned char* fData; |
| 203 | const int fWidth; |
| 204 | const int fHeight; |
| 205 | const int fX; |
| 206 | const int fY; |
| 207 | GrRectanizer* fRects; |
| 208 | const SkIPoint16 fOffset; // the offset of the plot in the backing texture |
| 209 | const GrPixelConfig fConfig; |
| 210 | const size_t fBytesPerPixel; |
| 211 | SkIRect fDirtyRect; |
| 212 | SkDEBUGCODE(bool fDirty;) |
| 213 | |
| 214 | friend class GrBatchAtlas; |
| 215 | |
| 216 | typedef SkRefCnt INHERITED; |
| 217 | }; |
| 218 | |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 219 | typedef SkTInternalLList<BatchPlot> GrBatchPlotList; |
| 220 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 221 | static uint32_t GetIndexFromID(AtlasID id) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 222 | return id & 0xffff; |
| 223 | } |
| 224 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 225 | // top 48 bits are reserved for the generation ID |
| 226 | static uint64_t GetGenerationFromID(AtlasID id) { |
| 227 | return (id >> 16) & 0xffffffffffff; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 228 | } |
| 229 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 230 | inline void updatePlot(GrDrawBatch::Target*, AtlasID*, BatchPlot*); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 231 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 232 | inline void makeMRU(BatchPlot* plot) { |
| 233 | if (fPlotList.head() == plot) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | fPlotList.remove(plot); |
| 238 | fPlotList.addToHead(plot); |
| 239 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 240 | |
| 241 | inline void processEviction(AtlasID); |
| 242 | |
| 243 | GrTexture* fTexture; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 244 | SkDEBUGCODE(uint32_t fNumPlots;) |
| 245 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 246 | uint64_t fAtlasGeneration; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 247 | |
| 248 | struct EvictionData { |
| 249 | EvictionFunc fFunc; |
| 250 | void* fData; |
| 251 | }; |
| 252 | |
| 253 | SkTDArray<EvictionData> fEvictionCallbacks; |
| 254 | // allocated array of GrBatchPlots |
| 255 | SkAutoTUnref<BatchPlot>* fPlotArray; |
| 256 | // LRU list of GrPlots (MRU at head - LRU at tail) |
| 257 | GrBatchPlotList fPlotList; |
| 258 | }; |
| 259 | |
| 260 | #endif |