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, |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 16 | int height, GrPixelConfig config) |
| 17 | : fLastUpload(GrBatchDrawToken::AlreadyFlushedToken()) |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 18 | , fLastUse(GrBatchDrawToken::AlreadyFlushedToken()) |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 19 | , fIndex(index) |
| 20 | , fGenID(genID) |
| 21 | , fID(CreateId(fIndex, fGenID)) |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 22 | , fData(nullptr) |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 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() { |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 39 | sk_free(fData); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 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 | |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 55 | if (!fData) { |
| 56 | fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth * |
| 57 | fHeight)); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 58 | } |
| 59 | size_t rowBytes = width * fBytesPerPixel; |
| 60 | const unsigned char* imagePtr = (const unsigned char*)image; |
| 61 | // point ourselves at the right starting spot |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 62 | unsigned char* dataPtr = fData; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 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 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 81 | void GrBatchAtlas::BatchPlot::uploadToTexture(GrDrawBatch::WritePixelsFn& writePixels, |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 82 | GrTexture* texture) { |
| 83 | // We should only be issuing uploads if we are in fact dirty |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 84 | SkASSERT(fDirty && fData && texture); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 85 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture"); |
| 86 | size_t rowBytes = fBytesPerPixel * fWidth; |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 87 | const unsigned char* dataPtr = fData; |
| 88 | dataPtr += rowBytes * fDirtyRect.fTop; |
| 89 | dataPtr += fBytesPerPixel * fDirtyRect.fLeft; |
| 90 | writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop, |
| 91 | fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 92 | fDirtyRect.setEmpty(); |
| 93 | SkDEBUGCODE(fDirty = false;) |
| 94 | } |
| 95 | |
| 96 | void GrBatchAtlas::BatchPlot::resetRects() { |
| 97 | if (fRects) { |
| 98 | fRects->reset(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 99 | } |
| 100 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 101 | fGenID++; |
| 102 | fID = CreateId(fIndex, fGenID); |
| 103 | |
| 104 | // zero out the plot |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 105 | if (fData) { |
| 106 | sk_bzero(fData, fBytesPerPixel * fWidth * fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 107 | } |
| 108 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 109 | fDirtyRect.setEmpty(); |
| 110 | SkDEBUGCODE(fDirty = false;) |
| 111 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 112 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 113 | /////////////////////////////////////////////////////////////////////////////// |
| 114 | |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 115 | GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 116 | : fTexture(texture) |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 117 | , fAtlasGeneration(kInvalidAtlasGeneration + 1) { |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 118 | |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 119 | fPlotWidth = texture->width() / numPlotsX; |
| 120 | fPlotHeight = texture->height() / numPlotsY; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 121 | SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 122 | SkASSERT(fPlotWidth * numPlotsX == texture->width()); |
| 123 | SkASSERT(fPlotHeight * numPlotsY == texture->height()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 124 | |
| 125 | SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 126 | |
| 127 | // We currently do not support compressed atlases... |
| 128 | SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig)); |
| 129 | |
| 130 | // set up allocated plots |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 131 | fPlotArray = new SkAutoTUnref<BatchPlot>[numPlotsX * numPlotsY]; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 132 | |
| 133 | SkAutoTUnref<BatchPlot>* currPlot = fPlotArray; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 134 | for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) { |
| 135 | for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) { |
| 136 | uint32_t index = r * numPlotsX + c; |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 137 | currPlot->reset(new BatchPlot(index, 1, x, y, fPlotWidth, fPlotHeight, |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 138 | texture->desc().fConfig)); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 139 | |
| 140 | // build LRU list |
| 141 | fPlotList.addToHead(currPlot->get()); |
| 142 | ++currPlot; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | GrBatchAtlas::~GrBatchAtlas() { |
| 148 | SkSafeUnref(fTexture); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 149 | delete[] fPlotArray; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void GrBatchAtlas::processEviction(AtlasID id) { |
| 153 | for (int i = 0; i < fEvictionCallbacks.count(); i++) { |
| 154 | (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData); |
| 155 | } |
| 156 | } |
| 157 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 158 | inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, BatchPlot* plot) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 159 | this->makeMRU(plot); |
| 160 | |
| 161 | // If our most recent upload has already occurred then we have to insert a new |
| 162 | // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred. |
| 163 | // This new update will piggy back on that previously scheduled update. |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 164 | if (target->hasDrawBeenFlushed(plot->lastUploadToken())) { |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 165 | // With c+14 we could move sk_sp into lamba to only ref once. |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 166 | sk_sp<BatchPlot> plotsp(SkRef(plot)); |
| 167 | GrTexture* texture = fTexture; |
| 168 | GrBatchDrawToken lastUploadToken = target->addAsapUpload( |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 169 | [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) { |
| 170 | plotsp->uploadToTexture(writePixels, texture); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 171 | } |
| 172 | ); |
| 173 | plot->setLastUploadToken(lastUploadToken); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 174 | } |
| 175 | *id = plot->id(); |
| 176 | } |
| 177 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 178 | bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* target, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 179 | int width, int height, const void* image, SkIPoint16* loc) { |
| 180 | // We should already have a texture, TODO clean this up |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 181 | SkASSERT(fTexture); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 182 | if (width > fPlotWidth || height > fPlotHeight) { |
| 183 | return false; |
| 184 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 185 | |
| 186 | // now look through all allocated plots for one we can share, in Most Recently Refed order |
| 187 | GrBatchPlotList::Iter plotIter; |
| 188 | plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart); |
| 189 | BatchPlot* plot; |
| 190 | while ((plot = plotIter.get())) { |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 191 | SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp()); |
| 192 | if (plot->addSubImage(width, height, image, loc)) { |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 193 | this->updatePlot(target, id, plot); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 194 | return true; |
| 195 | } |
| 196 | plotIter.next(); |
| 197 | } |
| 198 | |
| 199 | // If the above fails, then see if the least recently refed plot has already been flushed to the |
| 200 | // gpu |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 201 | plot = fPlotList.tail(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 202 | SkASSERT(plot); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 203 | if (target->hasDrawBeenFlushed(plot->lastUseToken())) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 204 | this->processEviction(plot->id()); |
| 205 | plot->resetRects(); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 206 | SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp()); |
| 207 | SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 208 | SkASSERT(verify); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 209 | this->updatePlot(target, id, plot); |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 210 | fAtlasGeneration++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 214 | // If this plot has been used in a draw that is currently being prepared by a batch, then we |
| 215 | // have to fail. This gives the batch a chance to enqueue the draw, and call back into this |
| 216 | // function. When that draw is enqueued, the draw token advances, and the subsequent call will |
| 217 | // continue past this branch and prepare an inline upload that will occur after the enqueued |
| 218 | // draw which references the plot's pre-upload content. |
| 219 | if (plot->lastUseToken() == target->nextDrawToken()) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 223 | SkASSERT(!plot->unique()); // The GrPlotUpdater should have a ref too |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 224 | |
| 225 | this->processEviction(plot->id()); |
| 226 | fPlotList.remove(plot); |
| 227 | SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()]; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 228 | newPlot.reset(plot->clone()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 229 | |
| 230 | fPlotList.addToHead(newPlot.get()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 231 | SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp()); |
| 232 | SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 233 | SkASSERT(verify); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 234 | |
robertphillips | 1f0e350 | 2015-11-10 10:19:50 -0800 | [diff] [blame] | 235 | // Note that this plot will be uploaded inline with the draws whereas the |
| 236 | // one it displaced most likely was uploaded asap. |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 237 | // With c+14 we could move sk_sp into lamba to only ref once. |
| 238 | sk_sp<BatchPlot> plotsp(SkRef(newPlot.get())); |
| 239 | GrTexture* texture = fTexture; |
| 240 | GrBatchDrawToken lastUploadToken = target->addInlineUpload( |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 241 | [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) { |
| 242 | plotsp->uploadToTexture(writePixels, texture); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 243 | } |
| 244 | ); |
| 245 | newPlot->setLastUploadToken(lastUploadToken); |
| 246 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 247 | *id = newPlot->id(); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 248 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 249 | fAtlasGeneration++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 250 | return true; |
| 251 | } |