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 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 32 | #ifdef DUMP_ATLAS_DATA |
| 33 | static bool gDumpAtlasData = false; |
| 34 | #endif |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 35 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 36 | //////////////////////////////////////////////////////////////////////////////// |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 37 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 38 | GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY, |
| 39 | int width, int height, GrPixelConfig config) |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame^] | 40 | : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken()) |
| 41 | , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken()) |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 42 | , fFlushesSinceLastUse(0) |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 43 | , fPageIndex(pageIndex) |
| 44 | , fPlotIndex(plotIndex) |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 45 | , fGenID(genID) |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 46 | , fID(CreateId(fPageIndex, fPlotIndex, fGenID)) |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 47 | , fData(nullptr) |
| 48 | , fWidth(width) |
| 49 | , fHeight(height) |
| 50 | , fX(offX) |
| 51 | , fY(offY) |
| 52 | , fRects(nullptr) |
| 53 | , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight)) |
| 54 | , fConfig(config) |
| 55 | , fBytesPerPixel(GrBytesPerPixel(config)) |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 56 | #ifdef SK_DEBUG |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 57 | , fDirty(false) |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 58 | #endif |
| 59 | { |
| 60 | fDirtyRect.setEmpty(); |
| 61 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 62 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 63 | GrDrawOpAtlas::Plot::~Plot() { |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 64 | sk_free(fData); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 65 | delete fRects; |
| 66 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 67 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 68 | bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 69 | SkASSERT(width <= fWidth && height <= fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 70 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 71 | if (!fRects) { |
| 72 | fRects = GrRectanizer::Factory(fWidth, fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 73 | } |
| 74 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 75 | if (!fRects->addRect(width, height, loc)) { |
| 76 | return false; |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 77 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 78 | |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 79 | if (!fData) { |
| 80 | fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth * |
| 81 | fHeight)); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 82 | } |
| 83 | size_t rowBytes = width * fBytesPerPixel; |
| 84 | const unsigned char* imagePtr = (const unsigned char*)image; |
| 85 | // point ourselves at the right starting spot |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 86 | unsigned char* dataPtr = fData; |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 87 | dataPtr += fBytesPerPixel * fWidth * loc->fY; |
| 88 | dataPtr += fBytesPerPixel * loc->fX; |
Brian Osman | cce3e58 | 2016-10-14 11:42:20 -0400 | [diff] [blame] | 89 | // copy into the data buffer, swizzling as we go if this is ARGB data |
| 90 | if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) { |
| 91 | for (int i = 0; i < height; ++i) { |
| 92 | SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width); |
| 93 | dataPtr += fBytesPerPixel * fWidth; |
| 94 | imagePtr += rowBytes; |
| 95 | } |
| 96 | } else { |
| 97 | for (int i = 0; i < height; ++i) { |
| 98 | memcpy(dataPtr, imagePtr, rowBytes); |
| 99 | dataPtr += fBytesPerPixel * fWidth; |
| 100 | imagePtr += rowBytes; |
| 101 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 104 | fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 105 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 106 | loc->fX += fOffset.fX; |
| 107 | loc->fY += fOffset.fY; |
| 108 | SkDEBUGCODE(fDirty = true;) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 109 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 110 | return true; |
| 111 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 112 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame^] | 113 | void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels, |
Robert Phillips | acaa607 | 2017-07-28 10:54:53 -0400 | [diff] [blame] | 114 | GrTextureProxy* proxy) { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 115 | // We should only be issuing uploads if we are in fact dirty |
Robert Phillips | acaa607 | 2017-07-28 10:54:53 -0400 | [diff] [blame] | 116 | SkASSERT(fDirty && fData && proxy && proxy->priv().peekTexture()); |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 117 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 118 | size_t rowBytes = fBytesPerPixel * fWidth; |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 119 | const unsigned char* dataPtr = fData; |
| 120 | dataPtr += rowBytes * fDirtyRect.fTop; |
| 121 | dataPtr += fBytesPerPixel * fDirtyRect.fLeft; |
Robert Phillips | acaa607 | 2017-07-28 10:54:53 -0400 | [diff] [blame] | 122 | writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop, |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 123 | fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 124 | fDirtyRect.setEmpty(); |
| 125 | SkDEBUGCODE(fDirty = false;) |
| 126 | } |
| 127 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 128 | void GrDrawOpAtlas::Plot::resetRects() { |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 129 | if (fRects) { |
| 130 | fRects->reset(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 131 | } |
| 132 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 133 | fGenID++; |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 134 | fID = CreateId(fPageIndex, fPlotIndex, fGenID); |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame^] | 135 | fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken(); |
| 136 | fLastUse = GrDeferredUploadToken::AlreadyFlushedToken(); |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 137 | |
| 138 | // zero out the plot |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 139 | if (fData) { |
| 140 | sk_bzero(fData, fBytesPerPixel * fWidth * fHeight); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 141 | } |
| 142 | |
joshualitt | 5df175e | 2015-11-18 13:37:54 -0800 | [diff] [blame] | 143 | fDirtyRect.setEmpty(); |
| 144 | SkDEBUGCODE(fDirty = false;) |
| 145 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 146 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 147 | /////////////////////////////////////////////////////////////////////////////// |
| 148 | |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 149 | GrDrawOpAtlas::GrDrawOpAtlas(GrContext* context, GrPixelConfig config, int width, int height, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 150 | int numPlotsX, int numPlotsY) |
| 151 | : fContext(context) |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 152 | , fPixelConfig(config) |
| 153 | , fTextureWidth(width) |
| 154 | , fTextureHeight(height) |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 155 | , fAtlasGeneration(kInvalidAtlasGeneration + 1) |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame^] | 156 | , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken()) |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 157 | , fNumPages(0) { |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 158 | fPlotWidth = fTextureWidth / numPlotsX; |
| 159 | fPlotHeight = fTextureHeight / numPlotsY; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 160 | SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots); |
Jim Van Verth | d74f3f2 | 2017-08-31 16:44:08 -0400 | [diff] [blame] | 161 | SkASSERT(fPlotWidth * numPlotsX == fTextureWidth); |
| 162 | SkASSERT(fPlotHeight * numPlotsY == fTextureHeight); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 163 | |
| 164 | SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 165 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 166 | this->createNewPage(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 169 | inline void GrDrawOpAtlas::processEviction(AtlasID id) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 170 | for (int i = 0; i < fEvictionCallbacks.count(); i++) { |
| 171 | (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData); |
| 172 | } |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 173 | ++fAtlasGeneration; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 176 | inline bool GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* plot) { |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 177 | int pageIdx = GetPageIndexFromID(plot->id()); |
| 178 | this->makeMRU(plot, pageIdx); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 179 | |
| 180 | // If our most recent upload has already occurred then we have to insert a new |
| 181 | // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred. |
| 182 | // This new update will piggy back on that previously scheduled update. |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 183 | if (target->hasDrawBeenFlushed(plot->lastUploadToken())) { |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 184 | // 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] | 185 | sk_sp<Plot> plotsp(SkRef(plot)); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 186 | |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 187 | // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated. |
| 188 | // 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] | 189 | if (!fProxies[pageIdx]->instantiate(fContext->resourceProvider())) { |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 190 | return false; |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 191 | } |
Robert Phillips | acaa607 | 2017-07-28 10:54:53 -0400 | [diff] [blame] | 192 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 193 | GrTextureProxy* proxy = fProxies[pageIdx].get(); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 194 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame^] | 195 | GrDeferredUploadToken lastUploadToken = target->addAsapUpload( |
| 196 | [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) { |
| 197 | plotsp->uploadToTexture(writePixels, proxy); |
| 198 | }); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 199 | plot->setLastUploadToken(lastUploadToken); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 200 | } |
| 201 | *id = plot->id(); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 202 | return true; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 205 | // Number of atlas-related flushes beyond which we consider a plot to no longer be in use. |
| 206 | // |
| 207 | // This value is somewhat arbitrary -- the idea is to keep it low enough that |
| 208 | // a page with unused plots will get removed reasonably quickly, but allow it |
| 209 | // to hang around for a bit in case it's needed. The assumption is that flushes |
| 210 | // are rare; i.e., we are not continually refreshing the frame. |
Derek Sollenberger | 90196cc | 2017-10-09 15:00:33 -0400 | [diff] [blame] | 211 | static constexpr auto kRecentlyUsedCount = 256; |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 212 | |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 213 | bool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height, |
| 214 | const void* image, SkIPoint16* loc) { |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 215 | if (width > fPlotWidth || height > fPlotHeight) { |
| 216 | return false; |
| 217 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 218 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 219 | // Look through each page to see if we can upload without having to flush |
| 220 | // We prioritize this upload to the first pages, not the most recently used, to make it easier |
| 221 | // to remove unused pages in reverse page order. |
| 222 | for (unsigned int pageIdx = 0; pageIdx < fNumPages; ++pageIdx) { |
| 223 | SkASSERT(fProxies[pageIdx]); |
| 224 | // look through all allocated plots for one we can share, in Most Recently Refed order |
| 225 | PlotList::Iter plotIter; |
| 226 | plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart); |
| 227 | Plot* plot; |
| 228 | while ((plot = plotIter.get())) { |
| 229 | SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == plot->bpp()); |
| 230 | if (plot->addSubImage(width, height, image, loc)) { |
| 231 | return this->updatePlot(target, id, plot); |
| 232 | } |
| 233 | plotIter.next(); |
| 234 | } |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 235 | } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 236 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 237 | // If the above fails, then see if the least recently used plot per page has already been |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 238 | // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise. |
| 239 | // We wait until we've grown to the full number of pages to begin evicting already flushed |
| 240 | // plots so that we can maximize the opportunity for reuse. |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 241 | // As before we prioritize this upload to the first pages, not the most recently used. |
| 242 | for (unsigned int pageIdx = 0; pageIdx < fNumPages; ++pageIdx) { |
| 243 | Plot* plot = fPages[pageIdx].fPlotList.tail(); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 244 | SkASSERT(plot); |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 245 | if ((fNumPages == kMaxPages && target->hasDrawBeenFlushed(plot->lastUseToken())) || |
| 246 | plot->flushesSinceLastUsed() >= kRecentlyUsedCount) { |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 247 | this->processEvictionAndResetRects(plot); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 248 | SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == plot->bpp()); |
| 249 | SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc); |
| 250 | SkASSERT(verify); |
| 251 | if (!this->updatePlot(target, id, plot)) { |
| 252 | return false; |
| 253 | } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 254 | return true; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // If the simple cases fail, try to create a new page and add to it |
| 259 | if (this->createNewPage()) { |
| 260 | unsigned int pageIdx = fNumPages-1; |
| 261 | SkASSERT(fProxies[pageIdx]); |
| 262 | Plot* plot = fPages[pageIdx].fPlotList.head(); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 263 | SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == plot->bpp()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 264 | if (plot->addSubImage(width, height, image, loc)) { |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 265 | return this->updatePlot(target, id, plot); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 266 | } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 267 | |
| 268 | // we shouldn't get here -- if so, something has gone terribly wrong |
| 269 | SkASSERT(false); |
| 270 | return false; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 273 | // Try to find a plot that we can perform an inline upload to. |
| 274 | // We prioritize this upload in reverse order of pages to counterbalance the order above. |
| 275 | Plot* plot = nullptr; |
| 276 | for (int pageIdx = (int)(fNumPages-1); pageIdx >= 0; --pageIdx) { |
| 277 | Plot* currentPlot = fPages[pageIdx].fPlotList.tail(); |
| 278 | if (currentPlot->lastUseToken() != target->nextDrawToken()) { |
| 279 | plot = currentPlot; |
| 280 | break; |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 281 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 284 | // If we can't find a plot that is not used in a draw currently being prepared by an op, then |
| 285 | // we have to fail. This gives the op a chance to enqueue the draw, and call back into this |
| 286 | // function. When that draw is enqueued, the draw token advances, and the subsequent call will |
| 287 | // continue past this branch and prepare an inline upload that will occur after the enqueued |
| 288 | //draw which references the plot's pre-upload content. |
| 289 | if (!plot) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 290 | return false; |
| 291 | } |
| 292 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 293 | this->processEviction(plot->id()); |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 294 | int pageIdx = GetPageIndexFromID(plot->id()); |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 295 | fPages[pageIdx].fPlotList.remove(plot); |
| 296 | sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()]; |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 297 | newPlot.reset(plot->clone()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 298 | |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 299 | fPages[pageIdx].fPlotList.addToHead(newPlot.get()); |
| 300 | SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == newPlot->bpp()); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 301 | SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 302 | SkASSERT(verify); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 303 | |
robertphillips | 1f0e350 | 2015-11-10 10:19:50 -0800 | [diff] [blame] | 304 | // Note that this plot will be uploaded inline with the draws whereas the |
| 305 | // one it displaced most likely was uploaded asap. |
Brian Salomon | 2ee084e | 2016-12-16 18:59:19 -0500 | [diff] [blame] | 306 | // With c+14 we could move sk_sp into lambda to only ref once. |
| 307 | sk_sp<Plot> plotsp(SkRef(newPlot.get())); |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 308 | // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated. |
| 309 | // 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] | 310 | if (!fProxies[pageIdx]->instantiate(fContext->resourceProvider())) { |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 311 | return false; |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 312 | } |
Jim Van Verth | a950b63 | 2017-09-12 11:54:11 -0400 | [diff] [blame] | 313 | GrTextureProxy* proxy = fProxies[pageIdx].get(); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 314 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame^] | 315 | GrDeferredUploadToken lastUploadToken = target->addInlineUpload( |
| 316 | [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) { |
| 317 | plotsp->uploadToTexture(writePixels, proxy); |
| 318 | }); |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 319 | newPlot->setLastUploadToken(lastUploadToken); |
| 320 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 321 | *id = newPlot->id(); |
robertphillips | 2b0536f | 2015-11-06 14:10:42 -0800 | [diff] [blame] | 322 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 323 | return true; |
| 324 | } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 325 | |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame^] | 326 | void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) { |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 327 | if (fNumPages <= 1) { |
| 328 | fPrevFlushToken = startTokenForNextFlush; |
| 329 | return; |
| 330 | } |
| 331 | |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 332 | // For all plots, reset number of flushes since used if used this frame. |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 333 | PlotList::Iter plotIter; |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 334 | bool atlasUsedThisFlush = false; |
| 335 | for (uint32_t pageIndex = 0; pageIndex < fNumPages; ++pageIndex) { |
| 336 | plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart); |
| 337 | while (Plot* plot = plotIter.get()) { |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 338 | // Reset number of flushes since used |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 339 | if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) { |
| 340 | plot->resetFlushesSinceLastUsed(); |
| 341 | atlasUsedThisFlush = true; |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | plotIter.next(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // We only try to compact if the atlas was used in the recently completed flush. |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 349 | // This is to handle the case where a lot of text or path rendering has occurred but then just |
| 350 | // a blinking cursor is drawn. |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 351 | // TODO: consider if we should also do this if it's been a long time since the last atlas use |
| 352 | if (atlasUsedThisFlush) { |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 353 | int availablePlots = 0; |
| 354 | uint32_t lastPageIndex = fNumPages - 1; |
| 355 | |
| 356 | // For all plots but the last one, update number of flushes since used, and check to see |
| 357 | // if there are any in the first pages that the last page can safely upload to. |
| 358 | for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) { |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 359 | #ifdef DUMP_ATLAS_DATA |
| 360 | if (gDumpAtlasData) { |
| 361 | SkDebugf("page %d: ", pageIndex); |
| 362 | } |
| 363 | #endif |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 364 | plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart); |
| 365 | while (Plot* plot = plotIter.get()) { |
| 366 | // Update number of flushes since plot was last used |
| 367 | // We only increment the 'sinceLastUsed' count for flushes where the atlas was used |
| 368 | // to avoid deleting everything when we return to text drawing in the blinking |
| 369 | // cursor case |
| 370 | if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) { |
| 371 | plot->incFlushesSinceLastUsed(); |
| 372 | } |
| 373 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 374 | #ifdef DUMP_ATLAS_DATA |
| 375 | if (gDumpAtlasData) { |
| 376 | SkDebugf("%d ", plot->flushesSinceLastUsed()); |
| 377 | } |
| 378 | #endif |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 379 | // Count plots we can potentially upload to in all pages except the last one |
| 380 | // (the potential compactee). |
| 381 | if (plot->flushesSinceLastUsed() > kRecentlyUsedCount) { |
| 382 | ++availablePlots; |
| 383 | } |
| 384 | |
| 385 | plotIter.next(); |
| 386 | } |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 387 | #ifdef DUMP_ATLAS_DATA |
| 388 | if (gDumpAtlasData) { |
| 389 | SkDebugf("\n"); |
| 390 | } |
| 391 | #endif |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 392 | } |
| 393 | |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 394 | // Count recently used plots in the last page and evict them if there's available space |
| 395 | // in earlier pages. Since we prioritize uploading to the first pages, this will eventually |
| 396 | // clear out usage of this page unless we have a large need. |
| 397 | plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart); |
| 398 | int usedPlots = 0; |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 399 | #ifdef DUMP_ATLAS_DATA |
| 400 | if (gDumpAtlasData) { |
| 401 | SkDebugf("page %d: ", lastPageIndex); |
| 402 | } |
| 403 | #endif |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 404 | while (Plot* plot = plotIter.get()) { |
Jim Van Verth | 62ea0cd | 2017-09-27 12:59:45 -0400 | [diff] [blame] | 405 | // Update number of flushes since plot was last used |
| 406 | if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) { |
| 407 | plot->incFlushesSinceLastUsed(); |
| 408 | } |
| 409 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 410 | #ifdef DUMP_ATLAS_DATA |
| 411 | if (gDumpAtlasData) { |
| 412 | SkDebugf("%d ", plot->flushesSinceLastUsed()); |
| 413 | } |
| 414 | #endif |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 415 | // If this plot was used recently |
| 416 | if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) { |
| 417 | usedPlots++; |
| 418 | // see if there's room in an earlier page and if so evict. |
| 419 | // We need to be somewhat harsh here so that one plot that is consistently in use |
| 420 | // doesn't end up locking the page in memory. |
| 421 | if (availablePlots) { |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 422 | this->processEvictionAndResetRects(plot); |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 423 | --availablePlots; |
| 424 | } |
Brian Salomon | 943ed79 | 2017-10-30 09:37:55 -0400 | [diff] [blame^] | 425 | } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) { |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 426 | // otherwise if aged out just evict it. |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 427 | this->processEvictionAndResetRects(plot); |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 428 | } |
| 429 | plotIter.next(); |
| 430 | } |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 431 | #ifdef DUMP_ATLAS_DATA |
| 432 | if (gDumpAtlasData) { |
| 433 | SkDebugf("\n"); |
| 434 | } |
| 435 | #endif |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 436 | // If none of the plots in the last page have been used recently, delete it. |
| 437 | if (!usedPlots) { |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 438 | #ifdef DUMP_ATLAS_DATA |
| 439 | if (gDumpAtlasData) { |
| 440 | SkDebugf("delete %d\n", fNumPages-1); |
| 441 | } |
| 442 | #endif |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 443 | this->deleteLastPage(); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | fPrevFlushToken = startTokenForNextFlush; |
| 448 | } |
| 449 | |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 450 | bool GrDrawOpAtlas::createNewPage() { |
| 451 | if (fNumPages == kMaxPages) { |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | GrSurfaceDesc desc; |
| 456 | desc.fFlags = kNone_GrSurfaceFlags; |
| 457 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 458 | desc.fWidth = fTextureWidth; |
| 459 | desc.fHeight = fTextureHeight; |
| 460 | desc.fConfig = fPixelConfig; |
| 461 | |
| 462 | // We don't want to flush the context so we claim we're in the middle of flushing so as to |
| 463 | // guarantee we do not recieve a texture with pending IO |
| 464 | // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156) |
| 465 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
| 466 | sk_sp<GrTexture> texture(fContext->resourceProvider()->createApproxTexture(desc, kFlags)); |
| 467 | if (texture) { |
| 468 | // MDB TODO: for now, wrap an instantiated texture. Having the deferred instantiation |
| 469 | // possess the correct properties (e.g., no pendingIO) should fall out of the system but |
| 470 | // should receive special attention. |
| 471 | // Note: When switching over to the deferred proxy, use the kExact flag to create |
| 472 | // the atlas and assert that the width & height are powers of 2. |
| 473 | fProxies[fNumPages] = GrSurfaceProxy::MakeWrapped(std::move(texture), |
| 474 | kTopLeft_GrSurfaceOrigin); |
| 475 | } |
| 476 | if (!fProxies[fNumPages]) { |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | int numPlotsX = fTextureWidth/fPlotWidth; |
| 481 | int numPlotsY = fTextureHeight/fPlotHeight; |
| 482 | |
| 483 | // set up allocated plots |
| 484 | fPages[fNumPages].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]); |
| 485 | |
| 486 | sk_sp<Plot>* currPlot = fPages[fNumPages].fPlotArray.get(); |
| 487 | for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) { |
| 488 | for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) { |
| 489 | uint32_t plotIndex = r * numPlotsX + c; |
| 490 | currPlot->reset(new Plot(fNumPages, plotIndex, 1, x, y, fPlotWidth, fPlotHeight, |
| 491 | fPixelConfig)); |
| 492 | |
| 493 | // build LRU list |
| 494 | fPages[fNumPages].fPlotList.addToHead(currPlot->get()); |
| 495 | ++currPlot; |
| 496 | } |
| 497 | } |
| 498 | |
Jim Van Verth | c3269ae | 2017-09-28 15:04:00 -0400 | [diff] [blame] | 499 | #ifdef DUMP_ATLAS_DATA |
| 500 | if (gDumpAtlasData) { |
| 501 | SkDebugf("created %d\n", fNumPages); |
| 502 | } |
| 503 | #endif |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 504 | fNumPages++; |
| 505 | return true; |
| 506 | } |
Jim Van Verth | 106b5c4 | 2017-09-26 12:45:29 -0400 | [diff] [blame] | 507 | |
| 508 | inline void GrDrawOpAtlas::deleteLastPage() { |
| 509 | uint32_t lastPageIndex = fNumPages - 1; |
| 510 | // clean out the plots |
| 511 | fPages[lastPageIndex].fPlotList.reset(); |
| 512 | fPages[lastPageIndex].fPlotArray.reset(nullptr); |
| 513 | // remove ref to texture proxy |
| 514 | fProxies[lastPageIndex].reset(nullptr); |
| 515 | --fNumPages; |
| 516 | } |