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 | |
Brian Salomon | 903da79 | 2016-12-16 14:24:46 -0500 | [diff] [blame] | 8 | #include "GrDrawOpAtlas.h" |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 9 | |
| 10 | #include "GrContext.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 11 | #include "GrOpFlushState.h" |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 12 | #include "GrRectanizer.h" |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 13 | #include "GrResourceProvider.h" |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 14 | #include "GrTracing.h" |
| 15 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 16 | std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrContext* ctx, GrPixelConfig config, |
| 17 | int width, int height, |
| 18 | int numPlotsX, int numPlotsY, |
| 19 | GrDrawOpAtlas::EvictionFunc func, |
| 20 | void* data) { |
| 21 | GrSurfaceDesc desc; |
| 22 | desc.fFlags = kNone_GrSurfaceFlags; |
| 23 | desc.fWidth = width; |
| 24 | desc.fHeight = height; |
| 25 | desc.fConfig = config; |
| 26 | |
Brian Salomon | 8785df1 | 2017-05-24 10:07:30 -0400 | [diff] [blame] | 27 | if (ctx->caps()->mustClearAtlases()) { |
| 28 | desc.fFlags |= kPerformInitialClear_GrSurfaceFlag; |
| 29 | } |
| 30 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 31 | // We don't want to flush the context so we claim we're in the middle of flushing so as to |
| 32 | // guarantee we do not recieve a texture with pending IO |
| 33 | // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156) |
| 34 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 35 | sk_sp<GrTexture> texture(ctx->resourceProvider()->createApproxTexture(desc, kFlags)); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 36 | if (!texture) { |
| 37 | return nullptr; |
| 38 | } |
| 39 | |
| 40 | // MDB TODO: for now, wrap an instantiated texture. Having the deferred instantiation |
| 41 | // possess the correct properties (e.g., no pendingIO) should fall out of the system but |
| 42 | // should receive special attention. |
| 43 | // Note: When switching over to the deferred proxy, use the kExact flag to create |
| 44 | // the atlas and assert that the width & height are powers of 2. |
| 45 | sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(texture)); |
| 46 | if (!proxy) { |
| 47 | return nullptr; |
| 48 | } |
| 49 | |
| 50 | std::unique_ptr<GrDrawOpAtlas> atlas( |
| 51 | new GrDrawOpAtlas(ctx, std::move(proxy), numPlotsX, numPlotsY)); |
| 52 | atlas->registerEvictionCallback(func, data); |
| 53 | return atlas; |
| 54 | } |
| 55 | |
| 56 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 57 | //////////////////////////////////////////////////////////////////////////////// |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 58 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 59 | GrDrawOpAtlas::Plot::Plot(int index, uint64_t genID, int offX, int offY, int width, int height, |
| 60 | GrPixelConfig config) |
| 61 | : fLastUpload(GrDrawOpUploadToken::AlreadyFlushedToken()) |
| 62 | , fLastUse(GrDrawOpUploadToken::AlreadyFlushedToken()) |
| 63 | , fIndex(index) |
| 64 | , fGenID(genID) |
| 65 | , fID(CreateId(fIndex, fGenID)) |
| 66 | , fData(nullptr) |
| 67 | , fWidth(width) |
| 68 | , fHeight(height) |
| 69 | , fX(offX) |
| 70 | , fY(offY) |
| 71 | , fRects(nullptr) |
| 72 | , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight)) |
| 73 | , fConfig(config) |
| 74 | , fBytesPerPixel(GrBytesPerPixel(config)) |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 75 | #ifdef SK_DEBUG |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 76 | , fDirty(false) |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 77 | #endif |
| 78 | { |
| 79 | fDirtyRect.setEmpty(); |
| 80 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 81 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 82 | GrDrawOpAtlas::Plot::~Plot() { |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 83 | sk_free(fData); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 84 | delete fRects; |
| 85 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 86 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 87 | bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 88 | SkASSERT(width <= fWidth && height <= fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 89 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 90 | if (!fRects) { |
| 91 | fRects = GrRectanizer::Factory(fWidth, fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 92 | } |
| 93 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 94 | if (!fRects->addRect(width, height, loc)) { |
| 95 | return false; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 96 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 97 | |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 98 | if (!fData) { |
| 99 | fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth * |
| 100 | fHeight)); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 101 | } |
| 102 | size_t rowBytes = width * fBytesPerPixel; |
| 103 | const unsigned char* imagePtr = (const unsigned char*)image; |
| 104 | // point ourselves at the right starting spot |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 105 | unsigned char* dataPtr = fData; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 106 | dataPtr += fBytesPerPixel * fWidth * loc->fY; |
| 107 | dataPtr += fBytesPerPixel * loc->fX; |
Brian Osman | cce3e58 | 2016-10-14 11:42:20 -0400 | [diff] [blame] | 108 | // copy into the data buffer, swizzling as we go if this is ARGB data |
| 109 | if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) { |
| 110 | for (int i = 0; i < height; ++i) { |
| 111 | SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width); |
| 112 | dataPtr += fBytesPerPixel * fWidth; |
| 113 | imagePtr += rowBytes; |
| 114 | } |
| 115 | } else { |
| 116 | for (int i = 0; i < height; ++i) { |
| 117 | memcpy(dataPtr, imagePtr, rowBytes); |
| 118 | dataPtr += fBytesPerPixel * fWidth; |
| 119 | imagePtr += rowBytes; |
| 120 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 121 | } |
| 122 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 123 | fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 124 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 125 | loc->fX += fOffset.fX; |
| 126 | loc->fY += fOffset.fY; |
| 127 | SkDEBUGCODE(fDirty = true;) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 128 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 129 | return true; |
| 130 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 131 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 132 | void GrDrawOpAtlas::Plot::uploadToTexture(GrDrawOp::WritePixelsFn& writePixels, |
| 133 | GrTexture* texture) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 134 | // We should only be issuing uploads if we are in fact dirty |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 135 | SkASSERT(fDirty && fData && texture); |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 136 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrDrawOpAtlas::Plot::uploadToTexture"); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 137 | size_t rowBytes = fBytesPerPixel * fWidth; |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 138 | const unsigned char* dataPtr = fData; |
| 139 | dataPtr += rowBytes * fDirtyRect.fTop; |
| 140 | dataPtr += fBytesPerPixel * fDirtyRect.fLeft; |
| 141 | writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop, |
| 142 | fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 143 | fDirtyRect.setEmpty(); |
| 144 | SkDEBUGCODE(fDirty = false;) |
| 145 | } |
| 146 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 147 | void GrDrawOpAtlas::Plot::resetRects() { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 148 | if (fRects) { |
| 149 | fRects->reset(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 150 | } |
| 151 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 152 | fGenID++; |
| 153 | fID = CreateId(fIndex, fGenID); |
| 154 | |
| 155 | // zero out the plot |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 156 | if (fData) { |
| 157 | sk_bzero(fData, fBytesPerPixel * fWidth * fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 160 | fDirtyRect.setEmpty(); |
| 161 | SkDEBUGCODE(fDirty = false;) |
| 162 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 163 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 164 | /////////////////////////////////////////////////////////////////////////////// |
| 165 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 166 | GrDrawOpAtlas::GrDrawOpAtlas(GrContext* context, sk_sp<GrTextureProxy> proxy, |
| 167 | int numPlotsX, int numPlotsY) |
| 168 | : fContext(context) |
| 169 | , fProxy(std::move(proxy)) |
| 170 | , fAtlasGeneration(kInvalidAtlasGeneration + 1) { |
| 171 | fPlotWidth = fProxy->width() / numPlotsX; |
| 172 | fPlotHeight = fProxy->height() / numPlotsY; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 173 | SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots); |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 174 | SkASSERT(fPlotWidth * numPlotsX == fProxy->width()); |
| 175 | SkASSERT(fPlotHeight * numPlotsY == fProxy->height()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 176 | |
| 177 | SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 178 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 179 | // set up allocated plots |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 180 | fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 181 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 182 | sk_sp<Plot>* currPlot = fPlotArray.get(); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 183 | for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) { |
| 184 | for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) { |
| 185 | uint32_t index = r * numPlotsX + c; |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 186 | currPlot->reset( |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 187 | new Plot(index, 1, x, y, fPlotWidth, fPlotHeight, fProxy->config())); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 188 | |
| 189 | // build LRU list |
| 190 | fPlotList.addToHead(currPlot->get()); |
| 191 | ++currPlot; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 196 | void GrDrawOpAtlas::processEviction(AtlasID id) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 197 | for (int i = 0; i < fEvictionCallbacks.count(); i++) { |
| 198 | (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData); |
| 199 | } |
| 200 | } |
| 201 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 202 | inline bool GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* plot) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 203 | this->makeMRU(plot); |
| 204 | |
| 205 | // If our most recent upload has already occurred then we have to insert a new |
| 206 | // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred. |
| 207 | // This new update will piggy back on that previously scheduled update. |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 208 | if (target->hasDrawBeenFlushed(plot->lastUploadToken())) { |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 209 | // With c+14 we could move sk_sp into lamba to only ref once. |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 210 | sk_sp<Plot> plotsp(SkRef(plot)); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 211 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 212 | // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated. |
| 213 | // Once it is deferred more care must be taken upon instantiation failure. |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 214 | GrTexture* texture = fProxy->instantiateTexture(fContext->resourceProvider()); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 215 | if (!texture) { |
| 216 | return false; |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 217 | } |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 218 | |
| 219 | GrDrawOpUploadToken lastUploadToken = target->addAsapUpload( |
| 220 | [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) { |
| 221 | plotsp->uploadToTexture(writePixels, texture); |
| 222 | } |
| 223 | ); |
| 224 | plot->setLastUploadToken(lastUploadToken); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 225 | } |
| 226 | *id = plot->id(); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 227 | return true; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 230 | bool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height, |
| 231 | const void* image, SkIPoint16* loc) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 232 | // We should already have a texture, TODO clean this up |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 233 | SkASSERT(fProxy); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 234 | if (width > fPlotWidth || height > fPlotHeight) { |
| 235 | return false; |
| 236 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 237 | |
| 238 | // now look through all allocated plots for one we can share, in Most Recently Refed order |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 239 | PlotList::Iter plotIter; |
| 240 | plotIter.init(fPlotList, PlotList::Iter::kHead_IterStart); |
| 241 | Plot* plot; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 242 | while ((plot = plotIter.get())) { |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 243 | SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 244 | if (plot->addSubImage(width, height, image, loc)) { |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 245 | return this->updatePlot(target, id, plot); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 246 | } |
| 247 | plotIter.next(); |
| 248 | } |
| 249 | |
| 250 | // If the above fails, then see if the least recently refed plot has already been flushed to the |
| 251 | // gpu |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 252 | plot = fPlotList.tail(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 253 | SkASSERT(plot); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 254 | if (target->hasDrawBeenFlushed(plot->lastUseToken())) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 255 | this->processEviction(plot->id()); |
| 256 | plot->resetRects(); |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 257 | SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 258 | SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 259 | SkASSERT(verify); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 260 | if (!this->updatePlot(target, id, plot)) { |
| 261 | return false; |
| 262 | } |
| 263 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 264 | fAtlasGeneration++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 265 | return true; |
| 266 | } |
| 267 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 268 | // If this plot has been used in a draw that is currently being prepared by an op, then we have |
| 269 | // to fail. This gives the op a chance to enqueue the draw, and call back into this function. |
| 270 | // When that draw is enqueued, the draw token advances, and the subsequent call will continue |
| 271 | // past this branch and prepare an inline upload that will occur after the enqueued draw which |
| 272 | // references the plot's pre-upload content. |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 273 | if (plot->lastUseToken() == target->nextDrawToken()) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 277 | this->processEviction(plot->id()); |
| 278 | fPlotList.remove(plot); |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 279 | sk_sp<Plot>& newPlot = fPlotArray[plot->index()]; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 280 | newPlot.reset(plot->clone()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 281 | |
| 282 | fPlotList.addToHead(newPlot.get()); |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 283 | SkASSERT(GrBytesPerPixel(fProxy->config()) == newPlot->bpp()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 284 | SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 285 | SkASSERT(verify); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 286 | |
robertphillips | 1f0e350 | 2015-11-10 10:19:50 -0800 | [diff] [blame] | 287 | // Note that this plot will be uploaded inline with the draws whereas the |
| 288 | // one it displaced most likely was uploaded asap. |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 289 | // With c+14 we could move sk_sp into lambda to only ref once. |
| 290 | sk_sp<Plot> plotsp(SkRef(newPlot.get())); |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 291 | // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated. |
| 292 | // Once it is deferred more care must be taken upon instantiation failure. |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 293 | GrTexture* texture = fProxy->instantiateTexture(fContext->resourceProvider()); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 294 | if (!texture) { |
| 295 | return false; |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 296 | } |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 297 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 298 | GrDrawOpUploadToken lastUploadToken = target->addInlineUpload( |
| 299 | [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) { |
| 300 | plotsp->uploadToTexture(writePixels, texture); |
| 301 | } |
| 302 | ); |
| 303 | newPlot->setLastUploadToken(lastUploadToken); |
| 304 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 305 | *id = newPlot->id(); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 306 | |
joshualitt | 7c3a2f8 | 2015-03-31 13:32:05 -0700 | [diff] [blame] | 307 | fAtlasGeneration++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 308 | return true; |
| 309 | } |