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" |
| 12 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 13 | //////////////////////////////////////////////////////////////////////////////// |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 14 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 15 | GrBatchAtlas::BatchPlot::BatchPlot(int index, uint64_t genID, int offX, int offY, int width, |
| 16 | int height, GrPixelConfig config) |
| 17 | : fLastUpload(0) |
| 18 | , fLastUse(0) |
| 19 | , fIndex(index) |
| 20 | , fGenID(genID) |
| 21 | , fID(CreateId(fIndex, fGenID)) |
| 22 | , fData(nullptr) |
| 23 | , fWidth(width) |
| 24 | , fHeight(height) |
| 25 | , fX(offX) |
| 26 | , fY(offY) |
| 27 | , fRects(nullptr) |
| 28 | , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight)) |
| 29 | , fConfig(config) |
| 30 | , fBytesPerPixel(GrBytesPerPixel(config)) |
| 31 | #ifdef SK_DEBUG |
| 32 | , fDirty(false) |
| 33 | #endif |
| 34 | { |
| 35 | fDirtyRect.setEmpty(); |
| 36 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 37 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 38 | GrBatchAtlas::BatchPlot::~BatchPlot() { |
| 39 | sk_free(fData); |
| 40 | delete fRects; |
| 41 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 42 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 43 | bool GrBatchAtlas::BatchPlot::addSubImage(int width, int height, const void* image, |
| 44 | SkIPoint16* loc) { |
| 45 | SkASSERT(width <= fWidth && height <= fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 46 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 47 | if (!fRects) { |
| 48 | fRects = GrRectanizer::Factory(fWidth, fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 49 | } |
| 50 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 51 | if (!fRects->addRect(width, height, loc)) { |
| 52 | return false; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 53 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 54 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 55 | if (!fData) { |
| 56 | fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth * |
| 57 | fHeight)); |
| 58 | } |
| 59 | size_t rowBytes = width * fBytesPerPixel; |
| 60 | const unsigned char* imagePtr = (const unsigned char*)image; |
| 61 | // point ourselves at the right starting spot |
| 62 | unsigned char* dataPtr = fData; |
| 63 | dataPtr += fBytesPerPixel * fWidth * loc->fY; |
| 64 | dataPtr += fBytesPerPixel * loc->fX; |
| 65 | // copy into the data buffer |
| 66 | for (int i = 0; i < height; ++i) { |
| 67 | memcpy(dataPtr, imagePtr, rowBytes); |
| 68 | dataPtr += fBytesPerPixel * fWidth; |
| 69 | imagePtr += rowBytes; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 70 | } |
| 71 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 72 | fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 73 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 74 | loc->fX += fOffset.fX; |
| 75 | loc->fY += fOffset.fY; |
| 76 | SkDEBUGCODE(fDirty = true;) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 77 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 78 | return true; |
| 79 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 80 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 81 | void GrBatchAtlas::BatchPlot::uploadToTexture(GrBatchUploader::TextureUploader* uploader, |
| 82 | GrTexture* texture) { |
| 83 | // We should only be issuing uploads if we are in fact dirty |
| 84 | SkASSERT(fDirty && fData && texture); |
| 85 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture"); |
| 86 | size_t rowBytes = fBytesPerPixel * fWidth; |
| 87 | const unsigned char* dataPtr = fData; |
| 88 | dataPtr += rowBytes * fDirtyRect.fTop; |
| 89 | dataPtr += fBytesPerPixel * fDirtyRect.fLeft; |
| 90 | uploader->writeTexturePixels(texture, |
| 91 | fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop, |
| 92 | fDirtyRect.width(), fDirtyRect.height(), |
| 93 | fConfig, dataPtr, rowBytes); |
| 94 | fDirtyRect.setEmpty(); |
| 95 | SkDEBUGCODE(fDirty = false;) |
| 96 | } |
| 97 | |
| 98 | void GrBatchAtlas::BatchPlot::resetRects() { |
| 99 | if (fRects) { |
| 100 | fRects->reset(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 101 | } |
| 102 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 103 | fGenID++; |
| 104 | fID = CreateId(fIndex, fGenID); |
| 105 | |
| 106 | // zero out the plot |
| 107 | if (fData) { |
| 108 | sk_bzero(fData, fBytesPerPixel * fWidth * fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 109 | } |
| 110 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 111 | fDirtyRect.setEmpty(); |
| 112 | SkDEBUGCODE(fDirty = false;) |
| 113 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 114 | |
| 115 | //////////////////////////////////////////////////////////////////////////////// |
| 116 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 117 | class GrPlotUploader : public GrBatchUploader { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 118 | public: |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 119 | GrPlotUploader(GrBatchAtlas::BatchPlot* plot, GrTexture* texture) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 120 | : INHERITED(plot->lastUploadToken()) |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 121 | , fPlot(SkRef(plot)) |
| 122 | , fTexture(texture) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 123 | SkASSERT(plot); |
| 124 | } |
| 125 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 126 | void upload(TextureUploader* uploader) override { |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 127 | fPlot->uploadToTexture(uploader, fTexture); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | private: |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 131 | SkAutoTUnref<GrBatchAtlas::BatchPlot> fPlot; |
| 132 | GrTexture* fTexture; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 133 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 134 | typedef GrBatchUploader INHERITED; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /////////////////////////////////////////////////////////////////////////////// |
| 138 | |
| 139 | GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY) |
| 140 | : fTexture(texture) |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 141 | , fAtlasGeneration(kInvalidAtlasGeneration + 1) { |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 142 | |
| 143 | int plotWidth = texture->width() / numPlotsX; |
| 144 | int plotHeight = texture->height() / numPlotsY; |
| 145 | SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots); |
| 146 | SkASSERT(plotWidth * numPlotsX == texture->width()); |
| 147 | SkASSERT(plotHeight * numPlotsY == texture->height()); |
| 148 | |
| 149 | SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 150 | |
| 151 | // We currently do not support compressed atlases... |
| 152 | SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig)); |
| 153 | |
| 154 | // set up allocated plots |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 155 | fPlotArray = new SkAutoTUnref<BatchPlot>[numPlotsX * numPlotsY]; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 156 | |
| 157 | SkAutoTUnref<BatchPlot>* currPlot = fPlotArray; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 158 | for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) { |
| 159 | for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) { |
| 160 | uint32_t index = r * numPlotsX + c; |
| 161 | currPlot->reset(new BatchPlot(index, 1, x, y, plotWidth, plotHeight, |
| 162 | texture->desc().fConfig)); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 163 | |
| 164 | // build LRU list |
| 165 | fPlotList.addToHead(currPlot->get()); |
| 166 | ++currPlot; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | GrBatchAtlas::~GrBatchAtlas() { |
| 172 | SkSafeUnref(fTexture); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 173 | delete[] fPlotArray; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void GrBatchAtlas::processEviction(AtlasID id) { |
| 177 | for (int i = 0; i < fEvictionCallbacks.count(); i++) { |
| 178 | (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData); |
| 179 | } |
| 180 | } |
| 181 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 182 | inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, BatchPlot* plot) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 183 | this->makeMRU(plot); |
| 184 | |
| 185 | // If our most recent upload has already occurred then we have to insert a new |
| 186 | // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred. |
| 187 | // This new update will piggy back on that previously scheduled update. |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 188 | if (target->hasTokenBeenFlushed(plot->lastUploadToken())) { |
| 189 | plot->setLastUploadToken(target->asapToken()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 190 | SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(plot, fTexture)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 191 | target->upload(uploader); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 192 | } |
| 193 | *id = plot->id(); |
| 194 | } |
| 195 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 196 | bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* batchTarget, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 197 | int width, int height, const void* image, SkIPoint16* loc) { |
| 198 | // We should already have a texture, TODO clean this up |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 199 | SkASSERT(fTexture); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 200 | |
| 201 | // now look through all allocated plots for one we can share, in Most Recently Refed order |
| 202 | GrBatchPlotList::Iter plotIter; |
| 203 | plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart); |
| 204 | BatchPlot* plot; |
| 205 | while ((plot = plotIter.get())) { |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 206 | SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp()); |
| 207 | if (plot->addSubImage(width, height, image, loc)) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 208 | this->updatePlot(batchTarget, id, plot); |
| 209 | return true; |
| 210 | } |
| 211 | plotIter.next(); |
| 212 | } |
| 213 | |
| 214 | // If the above fails, then see if the least recently refed plot has already been flushed to the |
| 215 | // gpu |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 216 | plot = fPlotList.tail(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 217 | SkASSERT(plot); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 218 | if (batchTarget->hasTokenBeenFlushed(plot->lastUseToken())) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 219 | this->processEviction(plot->id()); |
| 220 | plot->resetRects(); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 221 | SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp()); |
| 222 | SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 223 | SkASSERT(verify); |
| 224 | this->updatePlot(batchTarget, id, plot); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 225 | fAtlasGeneration++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 229 | // The least recently used plot hasn't been flushed to the gpu yet, however, if we have flushed |
| 230 | // 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] | 231 | // 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] | 232 | // 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] | 233 | // array. If it is equal to the currentToken, then the caller has to flush draws to the batch |
| 234 | // target so we can spin off the plot |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 235 | if (plot->lastUseToken() == batchTarget->currentToken()) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 239 | SkASSERT(plot->lastUseToken() < batchTarget->currentToken()); |
| 240 | SkASSERT(!batchTarget->hasTokenBeenFlushed(batchTarget->currentToken())); |
| 241 | |
| 242 | SkASSERT(!plot->unique()); // The GrPlotUpdater should have a ref too |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 243 | |
| 244 | this->processEviction(plot->id()); |
| 245 | fPlotList.remove(plot); |
| 246 | SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()]; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 247 | newPlot.reset(plot->clone()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 248 | |
| 249 | fPlotList.addToHead(newPlot.get()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 250 | SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp()); |
| 251 | SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 252 | SkASSERT(verify); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 253 | |
robertphillips | 1f0e350 | 2015-11-10 10:19:50 -0800 | [diff] [blame] | 254 | // Note that this plot will be uploaded inline with the draws whereas the |
| 255 | // one it displaced most likely was uploaded asap. |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 256 | newPlot->setLastUploadToken(batchTarget->currentToken()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 257 | SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(newPlot, fTexture)); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 258 | batchTarget->upload(uploader); |
| 259 | *id = newPlot->id(); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 260 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 261 | fAtlasGeneration++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 262 | return true; |
| 263 | } |