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