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