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