blob: 4b2272edcc791aa29024b6c2c086648e8fd36da1 [file] [log] [blame]
joshualitt5bf99f12015-03-13 11:47:42 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrDrawOpAtlas.h"
Robert Phillips32f28182017-02-28 16:20:03 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContext.h"
11#include "include/gpu/GrTexture.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050012#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040014#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrOnFlushResourceProvider.h"
16#include "src/gpu/GrOpFlushState.h"
17#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrResourceProvider.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040019#include "src/gpu/GrResourceProviderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrSurfaceProxyPriv.h"
21#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070022
Robert Phillipscd5099c2018-02-09 09:56:56 -050023// When proxy allocation is deferred until flush time the proxies acting as atlases require
24// special handling. This is because the usage that can be determined from the ops themselves
25// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
26// atlases. Extending the usage interval of any op that uses an atlas to the start of the
27// flush (as is done for proxies that are used for sw-generated masks) also won't work because
28// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
29// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
30// (which calls this method).
31void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050032 for (uint32_t i = 0; i < fNumActivePages; ++i) {
33 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050034 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050035 }
36}
37
Robert Phillips4bc70112018-03-01 10:24:02 -050038std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050039 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040040 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050041 int height, int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -050042 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -050043 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050044 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040045 if (!format.isValid()) {
46 return nullptr;
47 }
48
Herb Derby0ef780b2020-01-24 15:57:11 -050049 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType,
50 width, height, plotWidth, plotHeight,
51 generationCounter,
Robert Phillips4bc70112018-03-01 10:24:02 -050052 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050053 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040054 return nullptr;
55 }
56
Herb Derbya90ed952020-01-28 15:55:58 -050057 if (evictor != nullptr) {
58 atlas->fEvictionCallbacks.emplace_back(evictor);
59 }
Robert Phillips256c37b2017-03-01 14:32:46 -050060 return atlas;
61}
62
Jim Van Verth28a9b122020-01-27 16:00:19 +000063#ifdef DUMP_ATLAS_DATA
64static bool gDumpAtlasData = false;
65#endif
Robert Phillips256c37b2017-03-01 14:32:46 -050066
joshualitt5df175e2015-11-18 13:37:54 -080067////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -050068GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
69 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040070 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
71 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040072 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040073 , fPageIndex(pageIndex)
74 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -050075 , fGenerationCounter(generationCounter)
76 , fGenID(fGenerationCounter->next())
Herb Derby4d721712020-01-24 14:31:16 -050077 , fPlotLocator(CreatePlotLocator(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -050078 , fData(nullptr)
79 , fWidth(width)
80 , fHeight(height)
81 , fX(offX)
82 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -050083 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -050084 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -040085 , fColorType(colorType)
86 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -080087#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050088 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080089#endif
90{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -050091 // We expect the allocated dimensions to be a multiple of 4 bytes
92 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
93 // The padding for faster uploads only works for 1, 2 and 4 byte texels
94 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -080095 fDirtyRect.setEmpty();
96}
joshualitt5bf99f12015-03-13 11:47:42 -070097
Brian Salomon2ee084e2016-12-16 18:59:19 -050098GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070099 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800100}
joshualitt5bf99f12015-03-13 11:47:42 -0700101
Brian Salomon2ee084e2016-12-16 18:59:19 -0500102bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -0800103 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700104
Herb Derby73c75872020-01-22 18:09:16 -0500105 if (!fRectanizer.addRect(width, height, loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800106 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700107 }
joshualitt5bf99f12015-03-13 11:47:42 -0700108
jvanverthc3d706f2016-04-20 10:33:27 -0700109 if (!fData) {
110 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
111 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800112 }
113 size_t rowBytes = width * fBytesPerPixel;
114 const unsigned char* imagePtr = (const unsigned char*)image;
115 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700116 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800117 dataPtr += fBytesPerPixel * fWidth * loc->fY;
118 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400119 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500120 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400121 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400122 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400123 dataPtr += fBytesPerPixel * fWidth;
124 imagePtr += rowBytes;
125 }
126 } else {
127 for (int i = 0; i < height; ++i) {
128 memcpy(dataPtr, imagePtr, rowBytes);
129 dataPtr += fBytesPerPixel * fWidth;
130 imagePtr += rowBytes;
131 }
joshualitt5bf99f12015-03-13 11:47:42 -0700132 }
133
Mike Reed92b33352019-08-24 19:39:13 -0400134 fDirtyRect.join({loc->fX, loc->fY, loc->fX + width, loc->fY + height});
robertphillips2b0536f2015-11-06 14:10:42 -0800135
joshualitt5df175e2015-11-18 13:37:54 -0800136 loc->fX += fOffset.fX;
137 loc->fY += fOffset.fY;
138 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700139
joshualitt5df175e2015-11-18 13:37:54 -0800140 return true;
141}
joshualitt5bf99f12015-03-13 11:47:42 -0700142
Brian Salomon943ed792017-10-30 09:37:55 -0400143void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400144 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800145 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400146 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400147 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800148 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700149 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500150 // Clamp to 4-byte aligned boundaries
151 unsigned int clearBits = 0x3 / fBytesPerPixel;
152 fDirtyRect.fLeft &= ~clearBits;
153 fDirtyRect.fRight += clearBits;
154 fDirtyRect.fRight &= ~clearBits;
155 SkASSERT(fDirtyRect.fRight <= fWidth);
156 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700157 dataPtr += rowBytes * fDirtyRect.fTop;
158 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400159
Robert Phillipsacaa6072017-07-28 10:54:53 -0400160 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400161 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800162 fDirtyRect.setEmpty();
163 SkDEBUGCODE(fDirty = false;)
164}
165
Brian Salomon2ee084e2016-12-16 18:59:19 -0500166void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500167 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700168
Herb Derby0ef780b2020-01-24 15:57:11 -0500169 fGenID = fGenerationCounter->next();
Herb Derby4d721712020-01-24 14:31:16 -0500170 fPlotLocator = CreatePlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400171 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
172 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800173
174 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700175 if (fData) {
176 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700177 }
178
joshualitt5df175e2015-11-18 13:37:54 -0800179 fDirtyRect.setEmpty();
180 SkDEBUGCODE(fDirty = false;)
181}
joshualitt5bf99f12015-03-13 11:47:42 -0700182
joshualitt5bf99f12015-03-13 11:47:42 -0700183///////////////////////////////////////////////////////////////////////////////
184
Herb Derby0ef780b2020-01-24 15:57:11 -0500185GrDrawOpAtlas::GrDrawOpAtlas(
186 GrProxyProvider* proxyProvider, const GrBackendFormat& format,
187 GrColorType colorType, int width, int height, int plotWidth, int plotHeight,
188 GenerationCounter* generationCounter, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500189 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400190 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400191 , fTextureWidth(width)
192 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500193 , fPlotWidth(plotWidth)
194 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500195 , fGenerationCounter(generationCounter)
196 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400197 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500198 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500199 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500200 int numPlotsX = width/plotWidth;
201 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400202 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400203 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
204 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800205
Jim Van Verth06f593c2018-02-20 11:30:10 -0500206 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700207
Herb Derby0ef780b2020-01-24 15:57:11 -0500208 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700209}
210
Herb Derby4d721712020-01-24 14:31:16 -0500211inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
Herb Derby1a496c52020-01-22 17:26:56 -0500212 for (auto evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500213 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700214 }
Herb Derby1a496c52020-01-22 17:26:56 -0500215
Herb Derby0ef780b2020-01-24 15:57:11 -0500216 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700217}
218
Herb Derby4d721712020-01-24 14:31:16 -0500219inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
220 PlotLocator* plotLocator, Plot* plot) {
221 int pageIdx = GetPageIndexFromID(plot->plotLocator());
Jim Van Vertha950b632017-09-12 11:54:11 -0400222 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700223
224 // If our most recent upload has already occurred then we have to insert a new
225 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
226 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500227 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700228 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500229 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500230
Greg Daniel9715b6c2019-12-10 15:03:10 -0500231 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
232 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500233
Brian Salomon29b60c92017-10-31 14:42:10 -0400234 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400235 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
236 plotsp->uploadToTexture(writePixels, proxy);
237 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500238 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700239 }
Herb Derby4d721712020-01-24 14:31:16 -0500240 *plotLocator = plot->plotLocator();
Robert Phillips256c37b2017-03-01 14:32:46 -0500241 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700242}
243
Herb Derby4d721712020-01-24 14:31:16 -0500244bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx, PlotLocator* plotLocator,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400245 GrDeferredUploadTarget* target, int width, int height,
246 const void* image, SkIPoint16* loc) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500247 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500248
249 // look through all allocated plots for one we can share, in Most Recently Refed order
250 PlotList::Iter plotIter;
251 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
252
253 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500254 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500255
256 if (plot->addSubImage(width, height, image, loc)) {
Herb Derby4d721712020-01-24 14:31:16 -0500257 return this->updatePlot(target, plotLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500258 }
259 }
260
261 return false;
262}
263
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400264// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
265//
266// This value is somewhat arbitrary -- the idea is to keep it low enough that
267// a page with unused plots will get removed reasonably quickly, but allow it
268// to hang around for a bit in case it's needed. The assumption is that flushes
269// are rare; i.e., we are not continually refreshing the frame.
Derek Sollenberger90196cc2017-10-09 15:00:33 -0400270static constexpr auto kRecentlyUsedCount = 256;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400271
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500272GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500273 PlotLocator* plotLocator,
274 GrDeferredUploadTarget* target,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500275 int width, int height,
276 const void* image, SkIPoint16* loc) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700277 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500278 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700279 }
joshualitt5bf99f12015-03-13 11:47:42 -0700280
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400281 const GrCaps& caps = *resourceProvider->caps();
282
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400283 // Look through each page to see if we can upload without having to flush
284 // We prioritize this upload to the first pages, not the most recently used, to make it easier
285 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500286 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Herb Derby4d721712020-01-24 14:31:16 -0500287 if (this->uploadToPage(caps, pageIdx, plotLocator, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500288 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400289 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400290 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400291
Jim Van Verth712fe732017-09-25 16:53:49 -0400292 // If the above fails, then see if the least recently used plot per page has already been
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400293 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
294 // We wait until we've grown to the full number of pages to begin evicting already flushed
295 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400296 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500297 if (fNumActivePages == this->maxPages()) {
298 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
299 Plot* plot = fPages[pageIdx].fPlotList.tail();
300 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500301 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500302 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500303 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
304 plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500305 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
306 SkASSERT(verify);
Herb Derby4d721712020-01-24 14:31:16 -0500307 if (!this->updatePlot(target, plotLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500308 return ErrorCode::kError;
309 }
310 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400311 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500312 }
313 } else {
314 // If we haven't activated all the available pages, try to create a new one and add to it
315 if (!this->activateNewPage(resourceProvider)) {
316 return ErrorCode::kError;
317 }
318
Herb Derby4d721712020-01-24 14:31:16 -0500319 if (this->uploadToPage(
320 caps, fNumActivePages-1, plotLocator, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500321 return ErrorCode::kSucceeded;
322 } else {
323 // If we fail to upload to a newly activated page then something has gone terribly
324 // wrong - return an error
325 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400326 }
327 }
328
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500329 if (!fNumActivePages) {
330 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700331 }
332
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400333 // Try to find a plot that we can perform an inline upload to.
334 // We prioritize this upload in reverse order of pages to counterbalance the order above.
335 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500336 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400337 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500338 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400339 plot = currentPlot;
340 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500341 }
joshualitt5bf99f12015-03-13 11:47:42 -0700342 }
343
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400344 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
345 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
346 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
347 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500348 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400349 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500350 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700351 }
352
Herb Derby4d721712020-01-24 14:31:16 -0500353 this->processEviction(plot->plotLocator());
354 int pageIdx = GetPageIndexFromID(plot->plotLocator());
Jim Van Vertha950b632017-09-12 11:54:11 -0400355 fPages[pageIdx].fPlotList.remove(plot);
356 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800357 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700358
Jim Van Vertha950b632017-09-12 11:54:11 -0400359 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500360 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800361 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700362 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800363
robertphillips1f0e3502015-11-10 10:19:50 -0800364 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400365 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500366 // With c+14 we could move sk_sp into lambda to only ref once.
367 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500368
Greg Daniel9715b6c2019-12-10 15:03:10 -0500369 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
370 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700371
Brian Salomon943ed792017-10-30 09:37:55 -0400372 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
373 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
374 plotsp->uploadToTexture(writePixels, proxy);
375 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500376 newPlot->setLastUploadToken(lastUploadToken);
377
Herb Derby4d721712020-01-24 14:31:16 -0500378 *plotLocator = newPlot->plotLocator();
robertphillips2b0536f2015-11-06 14:10:42 -0800379
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500380 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700381}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400382
Brian Salomon943ed792017-10-30 09:37:55 -0400383void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500384 if (fNumActivePages <= 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400385 fPrevFlushToken = startTokenForNextFlush;
386 return;
387 }
388
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400389 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400390 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400391 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500392 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400393 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
394 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400395 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400396 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
397 plot->resetFlushesSinceLastUsed();
398 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400399 }
400
401 plotIter.next();
402 }
403 }
404
405 // We only try to compact if the atlas was used in the recently completed flush.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400406 // This is to handle the case where a lot of text or path rendering has occurred but then just
407 // a blinking cursor is drawn.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400408 // TODO: consider if we should also do this if it's been a long time since the last atlas use
409 if (atlasUsedThisFlush) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500410 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500411 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400412
413 // For all plots but the last one, update number of flushes since used, and check to see
414 // if there are any in the first pages that the last page can safely upload to.
415 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400416#ifdef DUMP_ATLAS_DATA
417 if (gDumpAtlasData) {
418 SkDebugf("page %d: ", pageIndex);
419 }
420#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400421 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
422 while (Plot* plot = plotIter.get()) {
423 // Update number of flushes since plot was last used
424 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
425 // to avoid deleting everything when we return to text drawing in the blinking
426 // cursor case
427 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
428 plot->incFlushesSinceLastUsed();
429 }
430
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400431#ifdef DUMP_ATLAS_DATA
432 if (gDumpAtlasData) {
433 SkDebugf("%d ", plot->flushesSinceLastUsed());
434 }
435#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400436 // Count plots we can potentially upload to in all pages except the last one
437 // (the potential compactee).
438 if (plot->flushesSinceLastUsed() > kRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500439 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400440 }
441
442 plotIter.next();
443 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400444#ifdef DUMP_ATLAS_DATA
445 if (gDumpAtlasData) {
446 SkDebugf("\n");
447 }
448#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400449 }
450
Jim Van Verth06f593c2018-02-20 11:30:10 -0500451 // Count recently used plots in the last page and evict any that are no longer in use.
452 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400453 // clear out usage of this page unless we have a large need.
454 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500455 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400456#ifdef DUMP_ATLAS_DATA
457 if (gDumpAtlasData) {
458 SkDebugf("page %d: ", lastPageIndex);
459 }
460#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400461 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400462 // Update number of flushes since plot was last used
463 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
464 plot->incFlushesSinceLastUsed();
465 }
466
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400467#ifdef DUMP_ATLAS_DATA
468 if (gDumpAtlasData) {
469 SkDebugf("%d ", plot->flushesSinceLastUsed());
470 }
471#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400472 // If this plot was used recently
473 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
474 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400475 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400476 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400477 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400478 }
479 plotIter.next();
480 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400481#ifdef DUMP_ATLAS_DATA
482 if (gDumpAtlasData) {
483 SkDebugf("\n");
484 }
485#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500486
487 // If recently used plots in the last page are using less than a quarter of the page, try
488 // to evict them if there's available space in earlier pages. Since we prioritize uploading
489 // to the first pages, this will eventually clear out usage of this page unless we have a
490 // large need.
491 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
492 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
493 while (Plot* plot = plotIter.get()) {
494 // If this plot was used recently
495 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
496 // See if there's room in an earlier page and if so evict.
497 // We need to be somewhat harsh here so that a handful of plots that are
498 // consistently in use don't end up locking the page in memory.
499 if (availablePlots.count() > 0) {
500 this->processEvictionAndResetRects(plot);
501 this->processEvictionAndResetRects(availablePlots.back());
502 availablePlots.pop_back();
503 --usedPlots;
504 }
505 if (!usedPlots || !availablePlots.count()) {
506 break;
507 }
508 }
509 plotIter.next();
510 }
511 }
512
Jim Van Verth106b5c42017-09-26 12:45:29 -0400513 // If none of the plots in the last page have been used recently, delete it.
514 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400515#ifdef DUMP_ATLAS_DATA
516 if (gDumpAtlasData) {
517 SkDebugf("delete %d\n", fNumPages-1);
518 }
519#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500520 this->deactivateLastPage();
Jim Van Verth106b5c42017-09-26 12:45:29 -0400521 }
522 }
523
524 fPrevFlushToken = startTokenForNextFlush;
525}
526
Herb Derby0ef780b2020-01-24 15:57:11 -0500527bool GrDrawOpAtlas::createPages(
528 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500529 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500530
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400531 GrSurfaceDesc desc;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400532 desc.fWidth = fTextureWidth;
533 desc.fHeight = fTextureHeight;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400534
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400535 int numPlotsX = fTextureWidth/fPlotWidth;
536 int numPlotsY = fTextureHeight/fPlotHeight;
537
Robert Phillips4bc70112018-03-01 10:24:02 -0500538 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500539 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500540 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Greg Daniel47c20e82020-01-21 14:29:57 -0500541 fFormat, desc, swizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
542 GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400543 GrInternalSurfaceFlags::kNone, GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500544 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500545 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400546 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500547 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500548
549 // set up allocated plots
550 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
551
552 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
553 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
554 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
555 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500556 currPlot->reset(new Plot(
557 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500558
559 // build LRU list
560 fPages[i].fPlotList.addToHead(currPlot->get());
561 ++currPlot;
562 }
563 }
564
565 }
566
567 return true;
568}
569
570
571bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500572 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500573
Greg Daniel9715b6c2019-12-10 15:03:10 -0500574 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500575 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400576 }
577
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400578#ifdef DUMP_ATLAS_DATA
579 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500580 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400581 }
582#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500583
584 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400585 return true;
586}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400587
Robert Phillips4bc70112018-03-01 10:24:02 -0500588
589inline void GrDrawOpAtlas::deactivateLastPage() {
590 SkASSERT(fNumActivePages);
591
592 uint32_t lastPageIndex = fNumActivePages - 1;
593
594 int numPlotsX = fTextureWidth/fPlotWidth;
595 int numPlotsY = fTextureHeight/fPlotHeight;
596
Jim Van Verth106b5c42017-09-26 12:45:29 -0400597 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500598 for (int r = 0; r < numPlotsY; ++r) {
599 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500600 uint32_t plotIndex = r * numPlotsX + c;
601
602 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
603 currPlot->resetRects();
604 currPlot->resetFlushesSinceLastUsed();
605
606 // rebuild the LRU list
607 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
608 SkDEBUGCODE(currPlot->fList = nullptr);
609 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
610 }
611 }
612
613 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500614 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500615 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400616}
Herb Derby15d9ef22018-10-18 13:41:32 -0400617
Jim Van Verthf6206f92018-12-14 08:22:24 -0500618GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
619 static const SkISize kARGBDimensions[] = {
620 {256, 256}, // maxBytes < 2^19
621 {512, 256}, // 2^19 <= maxBytes < 2^20
622 {512, 512}, // 2^20 <= maxBytes < 2^21
623 {1024, 512}, // 2^21 <= maxBytes < 2^22
624 {1024, 1024}, // 2^22 <= maxBytes < 2^23
625 {2048, 1024}, // 2^23 <= maxBytes
626 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400627
Jim Van Verthf6206f92018-12-14 08:22:24 -0500628 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
629 maxBytes >>= 18;
630 // Take the floor of the log to get the index
631 int index = maxBytes > 0
632 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
633 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400634
Jim Van Verthf6206f92018-12-14 08:22:24 -0500635 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
636 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
637 fARGBDimensions.set(SkTMin<int>(kARGBDimensions[index].width(), maxTextureSize),
638 SkTMin<int>(kARGBDimensions[index].height(), maxTextureSize));
639 fMaxTextureSize = SkTMin<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400640}
641
642SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500643 if (kA8_GrMaskFormat == type) {
644 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
645 return { SkTMin<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
646 SkTMin<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
647 } else {
648 return fARGBDimensions;
649 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400650}
651
Jim Van Verthf6206f92018-12-14 08:22:24 -0500652SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
653 if (kA8_GrMaskFormat == type) {
654 SkISize atlasDimensions = this->atlasDimensions(type);
655 // For A8 we want to grow the plots at larger texture sizes to accept more of the
656 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
657 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400658
Jim Van Verth578b0892018-12-20 20:48:55 +0000659 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
660 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500661 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000662 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400663
Jim Van Verthf6206f92018-12-14 08:22:24 -0500664 return { plotWidth, plotHeight };
665 } else {
666 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
667 return { 256, 256 };
668 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400669}
670
Jim Van Verthf6206f92018-12-14 08:22:24 -0500671constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;