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