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