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