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