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