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