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 | #include "GrBatchAtlas.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 9 | #include "GrBatchFlushState.h" |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 10 | #include "GrRectanizer.h" |
| 11 | #include "GrTracing.h" |
bsalomon | 72e3ae4 | 2015-04-28 08:08:46 -0700 | [diff] [blame] | 12 | #include "GrVertexBuffer.h" |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 13 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 14 | static inline void adjust_for_offset(SkIPoint16* loc, const SkIPoint16& offset) { |
| 15 | loc->fX += offset.fX; |
| 16 | loc->fY += offset.fY; |
| 17 | } |
| 18 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 19 | static GrBatchAtlas::AtlasID create_id(uint32_t index, uint64_t generation) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 20 | SkASSERT(index < (1 << 16)); |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 21 | SkASSERT(generation < ((uint64_t)1 << 48)); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 22 | return generation << 16 | index; |
| 23 | } |
| 24 | |
| 25 | // The backing GrTexture for a GrBatchAtlas is broken into a spatial grid of GrBatchPlots. |
| 26 | // The GrBatchPlots keep track of subimage placement via their GrRectanizer. In turn, a GrBatchPlot |
| 27 | // manages the lifetime of its data using two tokens, a last ref toke and a last upload token. |
| 28 | // Once a GrBatchPlot is "full" (i.e. there is no room for the new subimage according to the |
| 29 | // GrRectanizer), it can no longer be used unless the last ref on the GrPlot has already been |
| 30 | // flushed through to the gpu. |
| 31 | |
| 32 | class BatchPlot : public SkRefCnt { |
| 33 | public: |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 34 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(BatchPlot); |
| 35 | |
| 36 | // index() refers to the index of the plot in the owning GrAtlas's plot array. genID() is a |
| 37 | // monotonically incrementing number which is bumped every time the cpu backing store is |
| 38 | // wiped, or when the plot itself is evicted from the atlas(ie, there is continuity in genID() |
| 39 | // across atlas spills) |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 40 | uint32_t index() const { return fIndex; } |
| 41 | uint64_t genID() const { return fGenID; } |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 42 | GrBatchAtlas::AtlasID id() { |
| 43 | SkASSERT(GrBatchAtlas::kInvalidAtlasID != fID); |
| 44 | return fID; |
| 45 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 46 | |
| 47 | GrTexture* texture() const { return fTexture; } |
| 48 | |
| 49 | bool addSubImage(int width, int height, const void* image, SkIPoint16* loc, size_t rowBytes) { |
| 50 | if (!fRects->addRect(width, height, loc)) { |
| 51 | return false; |
| 52 | } |
| 53 | |
joshualitt | e062db9 | 2015-04-24 08:33:21 -0700 | [diff] [blame] | 54 | if (!fData) { |
| 55 | fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth * |
| 56 | fHeight)); |
| 57 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 58 | const unsigned char* imagePtr = (const unsigned char*)image; |
| 59 | // point ourselves at the right starting spot |
| 60 | unsigned char* dataPtr = fData; |
| 61 | dataPtr += fBytesPerPixel * fWidth * loc->fY; |
| 62 | dataPtr += fBytesPerPixel * loc->fX; |
| 63 | // copy into the data buffer |
| 64 | for (int i = 0; i < height; ++i) { |
| 65 | memcpy(dataPtr, imagePtr, rowBytes); |
| 66 | dataPtr += fBytesPerPixel * fWidth; |
| 67 | imagePtr += rowBytes; |
| 68 | } |
| 69 | |
| 70 | fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height); |
| 71 | adjust_for_offset(loc, fOffset); |
| 72 | SkDEBUGCODE(fDirty = true;) |
| 73 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
| 77 | // to manage the lifetime of a plot, we use two tokens. We use last upload token to know when |
| 78 | // we can 'piggy back' uploads, ie if the last upload hasn't been flushed to gpu, we don't need |
| 79 | // to issue a new upload even if we update the cpu backing store. We use lastref to determine |
| 80 | // when we can evict a plot from the cache, ie if the last ref has already flushed through |
| 81 | // the gpu then we can reuse the plot |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 82 | GrBatchToken lastUploadToken() const { return fLastUpload; } |
| 83 | GrBatchToken lastUseToken() const { return fLastUse; } |
| 84 | void setLastUploadToken(GrBatchToken batchToken) { |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 85 | SkASSERT(batchToken >= fLastUpload); |
| 86 | fLastUpload = batchToken; |
| 87 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 88 | void setLastUseToken(GrBatchToken batchToken) { |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 89 | SkASSERT(batchToken >= fLastUse); |
| 90 | fLastUse = batchToken; |
| 91 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 92 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 93 | void uploadToTexture(GrBatchUploader::TextureUploader* uploader) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 94 | // We should only be issuing uploads if we are in fact dirty |
joshualitt | e062db9 | 2015-04-24 08:33:21 -0700 | [diff] [blame] | 95 | SkASSERT(fDirty && fData && fTexture); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 96 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture"); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 97 | size_t rowBytes = fBytesPerPixel * fRects->width(); |
| 98 | const unsigned char* dataPtr = fData; |
| 99 | dataPtr += rowBytes * fDirtyRect.fTop; |
| 100 | dataPtr += fBytesPerPixel * fDirtyRect.fLeft; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 101 | uploader->writeTexturePixels(fTexture, |
| 102 | fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop, |
| 103 | fDirtyRect.width(), fDirtyRect.height(), |
| 104 | fTexture->config(), dataPtr, rowBytes); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 105 | fDirtyRect.setEmpty(); |
| 106 | SkDEBUGCODE(fDirty = false;) |
| 107 | } |
| 108 | |
| 109 | void resetRects() { |
| 110 | SkASSERT(fRects); |
| 111 | fRects->reset(); |
| 112 | fGenID++; |
| 113 | fID = create_id(fIndex, fGenID); |
| 114 | |
| 115 | // zero out the plot |
joshualitt | e062db9 | 2015-04-24 08:33:21 -0700 | [diff] [blame] | 116 | if (fData) { |
| 117 | sk_bzero(fData, fBytesPerPixel * fWidth * fHeight); |
| 118 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 119 | |
| 120 | fDirtyRect.setEmpty(); |
| 121 | SkDEBUGCODE(fDirty = false;) |
| 122 | } |
| 123 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 124 | uint32_t x() const { return fX; } |
| 125 | uint32_t y() const { return fY; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 126 | |
| 127 | private: |
| 128 | BatchPlot() |
| 129 | : fLastUpload(0) |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 130 | , fLastUse(0) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 131 | , fIndex(-1) |
| 132 | , fGenID(-1) |
| 133 | , fID(0) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | , fData(nullptr) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 135 | , fWidth(0) |
| 136 | , fHeight(0) |
| 137 | , fX(0) |
| 138 | , fY(0) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | , fTexture(nullptr) |
| 140 | , fRects(nullptr) |
| 141 | , fAtlas(nullptr) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 142 | , fBytesPerPixel(1) |
| 143 | #ifdef SK_DEBUG |
| 144 | , fDirty(false) |
| 145 | #endif |
| 146 | { |
| 147 | fOffset.set(0, 0); |
| 148 | } |
| 149 | |
| 150 | ~BatchPlot() { |
joshualitt | 8e71d38 | 2015-04-24 18:47:11 -0700 | [diff] [blame] | 151 | sk_free(fData); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | fData = nullptr; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 153 | delete fRects; |
| 154 | } |
| 155 | |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 156 | void init(GrBatchAtlas* atlas, GrTexture* texture, int index, uint64_t generation, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 157 | int offX, int offY, int width, int height, size_t bpp) { |
| 158 | fIndex = index; |
| 159 | fGenID = generation; |
| 160 | fID = create_id(index, generation); |
| 161 | fWidth = width; |
| 162 | fHeight = height; |
| 163 | fX = offX; |
| 164 | fY = offY; |
| 165 | fRects = GrRectanizer::Factory(width, height); |
| 166 | fAtlas = atlas; |
| 167 | fOffset.set(offX * width, offY * height); |
| 168 | fBytesPerPixel = bpp; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 169 | fData = nullptr; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 170 | fDirtyRect.setEmpty(); |
| 171 | SkDEBUGCODE(fDirty = false;) |
| 172 | fTexture = texture; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 173 | } |
| 174 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 175 | GrBatchToken fLastUpload; |
| 176 | GrBatchToken fLastUse; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 177 | |
| 178 | uint32_t fIndex; |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 179 | uint64_t fGenID; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 180 | GrBatchAtlas::AtlasID fID; |
| 181 | unsigned char* fData; |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 182 | uint32_t fWidth; |
| 183 | uint32_t fHeight; |
| 184 | uint32_t fX; |
| 185 | uint32_t fY; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 186 | GrTexture* fTexture; |
| 187 | GrRectanizer* fRects; |
| 188 | GrBatchAtlas* fAtlas; |
| 189 | SkIPoint16 fOffset; // the offset of the plot in the backing texture |
| 190 | size_t fBytesPerPixel; |
| 191 | SkIRect fDirtyRect; |
| 192 | SkDEBUGCODE(bool fDirty;) |
| 193 | |
| 194 | friend class GrBatchAtlas; |
| 195 | |
| 196 | typedef SkRefCnt INHERITED; |
| 197 | }; |
| 198 | |
| 199 | //////////////////////////////////////////////////////////////////////////////// |
| 200 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 201 | class GrPlotUploader : public GrBatchUploader { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 202 | public: |
| 203 | GrPlotUploader(BatchPlot* plot) |
| 204 | : INHERITED(plot->lastUploadToken()) |
| 205 | , fPlot(SkRef(plot)) { |
| 206 | SkASSERT(plot); |
| 207 | } |
| 208 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 209 | void upload(TextureUploader* uploader) override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 210 | fPlot->uploadToTexture(uploader); |
| 211 | } |
| 212 | |
| 213 | private: |
| 214 | SkAutoTUnref<BatchPlot> fPlot; |
| 215 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 216 | typedef GrBatchUploader INHERITED; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | /////////////////////////////////////////////////////////////////////////////// |
| 220 | |
| 221 | GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY) |
| 222 | : fTexture(texture) |
| 223 | , fNumPlotsX(numPlotsX) |
| 224 | , fNumPlotsY(numPlotsY) |
| 225 | , fPlotWidth(texture->width() / numPlotsX) |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 226 | , fPlotHeight(texture->height() / numPlotsY) |
| 227 | , fAtlasGeneration(kInvalidAtlasGeneration + 1) { |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 228 | SkASSERT(fNumPlotsX * fNumPlotsY <= BulkUseTokenUpdater::kMaxPlots); |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 229 | SkASSERT(fPlotWidth * fNumPlotsX == static_cast<uint32_t>(texture->width())); |
| 230 | SkASSERT(fPlotHeight * fNumPlotsY == static_cast<uint32_t>(texture->height())); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 231 | |
| 232 | // We currently do not support compressed atlases... |
| 233 | SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig)); |
| 234 | |
| 235 | // set up allocated plots |
| 236 | fBPP = GrBytesPerPixel(texture->desc().fConfig); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 237 | fPlotArray = new SkAutoTUnref<BatchPlot>[(fNumPlotsX * fNumPlotsY)]; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 238 | |
| 239 | SkAutoTUnref<BatchPlot>* currPlot = fPlotArray; |
| 240 | for (int y = fNumPlotsY - 1, r = 0; y >= 0; --y, ++r) { |
| 241 | for (int x = fNumPlotsX - 1, c = 0; x >= 0; --x, ++c) { |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 242 | uint32_t id = r * fNumPlotsX + c; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 243 | currPlot->reset(new BatchPlot); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 244 | (*currPlot)->init(this, texture, id, 1, x, y, fPlotWidth, fPlotHeight, fBPP); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 245 | |
| 246 | // build LRU list |
| 247 | fPlotList.addToHead(currPlot->get()); |
| 248 | ++currPlot; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | GrBatchAtlas::~GrBatchAtlas() { |
| 254 | SkSafeUnref(fTexture); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 255 | delete[] fPlotArray; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void GrBatchAtlas::processEviction(AtlasID id) { |
| 259 | for (int i = 0; i < fEvictionCallbacks.count(); i++) { |
| 260 | (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void GrBatchAtlas::makeMRU(BatchPlot* plot) { |
| 265 | if (fPlotList.head() == plot) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | fPlotList.remove(plot); |
| 270 | fPlotList.addToHead(plot); |
| 271 | } |
| 272 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 273 | inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, BatchPlot* plot) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 274 | this->makeMRU(plot); |
| 275 | |
| 276 | // If our most recent upload has already occurred then we have to insert a new |
| 277 | // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred. |
| 278 | // This new update will piggy back on that previously scheduled update. |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 279 | if (target->hasTokenBeenFlushed(plot->lastUploadToken())) { |
| 280 | plot->setLastUploadToken(target->asapToken()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 281 | SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(plot)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 282 | target->upload(uploader); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 283 | } |
| 284 | *id = plot->id(); |
| 285 | } |
| 286 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 287 | bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* batchTarget, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 288 | int width, int height, const void* image, SkIPoint16* loc) { |
| 289 | // We should already have a texture, TODO clean this up |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 290 | SkASSERT(fTexture && |
| 291 | static_cast<uint32_t>(width) <= fPlotWidth && |
| 292 | static_cast<uint32_t>(height) <= fPlotHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 293 | |
| 294 | // now look through all allocated plots for one we can share, in Most Recently Refed order |
| 295 | GrBatchPlotList::Iter plotIter; |
| 296 | plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart); |
| 297 | BatchPlot* plot; |
| 298 | while ((plot = plotIter.get())) { |
| 299 | if (plot->addSubImage(width, height, image, loc, fBPP * width)) { |
| 300 | this->updatePlot(batchTarget, id, plot); |
| 301 | return true; |
| 302 | } |
| 303 | plotIter.next(); |
| 304 | } |
| 305 | |
| 306 | // If the above fails, then see if the least recently refed plot has already been flushed to the |
| 307 | // gpu |
| 308 | plotIter.init(fPlotList, GrBatchPlotList::Iter::kTail_IterStart); |
| 309 | plot = plotIter.get(); |
| 310 | SkASSERT(plot); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 311 | if (batchTarget->hasTokenBeenFlushed(plot->lastUseToken())) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 312 | this->processEviction(plot->id()); |
| 313 | plot->resetRects(); |
| 314 | SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc, fBPP * width); |
| 315 | SkASSERT(verify); |
| 316 | this->updatePlot(batchTarget, id, plot); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 317 | fAtlasGeneration++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 318 | return true; |
| 319 | } |
| 320 | |
| 321 | // The least recently refed plot hasn't been flushed to the gpu yet, however, if we have flushed |
| 322 | // it to the batch target than we can reuse it. Our last ref token is guaranteed to be less |
| 323 | // than or equal to the current token. If its 'less than' the current token, than we can spin |
| 324 | // off the plot(ie let the batch target manage it) and create a new plot in its place in our |
| 325 | // array. If it is equal to the currentToken, then the caller has to flush draws to the batch |
| 326 | // target so we can spin off the plot |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 327 | if (plot->lastUseToken() == batchTarget->currentToken()) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 328 | return false; |
| 329 | } |
| 330 | |
| 331 | // We take an extra ref here so our plot isn't deleted when we reset its index in the array. |
| 332 | plot->ref(); |
| 333 | int index = plot->index(); |
| 334 | int x = plot->x(); |
| 335 | int y = plot->y(); |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 336 | uint64_t generation = plot->genID(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 337 | |
| 338 | this->processEviction(plot->id()); |
| 339 | fPlotList.remove(plot); |
| 340 | SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()]; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 341 | newPlot.reset(new BatchPlot); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 342 | newPlot->init(this, fTexture, index, ++generation, x, y, fPlotWidth, fPlotHeight, fBPP); |
| 343 | |
| 344 | fPlotList.addToHead(newPlot.get()); |
| 345 | SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc, fBPP * width); |
| 346 | SkASSERT(verify); |
| 347 | newPlot->setLastUploadToken(batchTarget->currentToken()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 348 | SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(newPlot)); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 349 | batchTarget->upload(uploader); |
| 350 | *id = newPlot->id(); |
| 351 | plot->unref(); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 352 | fAtlasGeneration++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
| 356 | bool GrBatchAtlas::hasID(AtlasID id) { |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 357 | uint32_t index = GetIndexFromID(id); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 358 | SkASSERT(index < fNumPlotsX * fNumPlotsY); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 359 | return fPlotArray[index]->genID() == GetGenerationFromID(id); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 360 | } |
| 361 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 362 | void GrBatchAtlas::setLastUseToken(AtlasID id, GrBatchToken batchToken) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 363 | SkASSERT(this->hasID(id)); |
joshualitt | 8db6fdc | 2015-07-31 08:25:07 -0700 | [diff] [blame] | 364 | uint32_t index = GetIndexFromID(id); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 365 | SkASSERT(index < fNumPlotsX * fNumPlotsY); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 366 | this->makeMRU(fPlotArray[index]); |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 367 | fPlotArray[index]->setLastUseToken(batchToken); |
| 368 | } |
| 369 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 370 | void GrBatchAtlas::setLastUseTokenBulk(const BulkUseTokenUpdater& updater, |
| 371 | GrBatchToken batchToken) { |
joshualitt | 4314e08 | 2015-04-23 08:03:35 -0700 | [diff] [blame] | 372 | int count = updater.fPlotsToUpdate.count(); |
| 373 | for (int i = 0; i < count; i++) { |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 374 | BatchPlot* plot = fPlotArray[updater.fPlotsToUpdate[i]]; |
| 375 | this->makeMRU(plot); |
| 376 | plot->setLastUseToken(batchToken); |
| 377 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 378 | } |