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