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