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