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