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