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