blob: 1740d9d10295515ba39d850bd0efa040dd81fae9 [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 Derby1a496c52020-01-22 17:26:56 -050057 atlas->fEvictionCallbacks.emplace_back(evictor);
Robert Phillips256c37b2017-03-01 14:32:46 -050058 return atlas;
59}
60
Jim Van Verth28a9b122020-01-27 16:00:19 +000061#ifdef DUMP_ATLAS_DATA
62static bool gDumpAtlasData = false;
63#endif
Robert Phillips256c37b2017-03-01 14:32:46 -050064
joshualitt5df175e2015-11-18 13:37:54 -080065////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -050066GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
67 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040068 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
69 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040070 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040071 , fPageIndex(pageIndex)
72 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -050073 , fGenerationCounter(generationCounter)
74 , fGenID(fGenerationCounter->next())
Herb Derby4d721712020-01-24 14:31:16 -050075 , fPlotLocator(CreatePlotLocator(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -050076 , fData(nullptr)
77 , fWidth(width)
78 , fHeight(height)
79 , fX(offX)
80 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -050081 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -050082 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -040083 , fColorType(colorType)
84 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -080085#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050086 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080087#endif
88{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -050089 // We expect the allocated dimensions to be a multiple of 4 bytes
90 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
91 // The padding for faster uploads only works for 1, 2 and 4 byte texels
92 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -080093 fDirtyRect.setEmpty();
94}
joshualitt5bf99f12015-03-13 11:47:42 -070095
Brian Salomon2ee084e2016-12-16 18:59:19 -050096GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070097 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080098}
joshualitt5bf99f12015-03-13 11:47:42 -070099
Brian Salomon2ee084e2016-12-16 18:59:19 -0500100bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -0800101 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700102
Herb Derby73c75872020-01-22 18:09:16 -0500103 if (!fRectanizer.addRect(width, height, loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800104 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700105 }
joshualitt5bf99f12015-03-13 11:47:42 -0700106
jvanverthc3d706f2016-04-20 10:33:27 -0700107 if (!fData) {
108 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
109 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800110 }
111 size_t rowBytes = width * fBytesPerPixel;
112 const unsigned char* imagePtr = (const unsigned char*)image;
113 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700114 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800115 dataPtr += fBytesPerPixel * fWidth * loc->fY;
116 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400117 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500118 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400119 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400120 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400121 dataPtr += fBytesPerPixel * fWidth;
122 imagePtr += rowBytes;
123 }
124 } else {
125 for (int i = 0; i < height; ++i) {
126 memcpy(dataPtr, imagePtr, rowBytes);
127 dataPtr += fBytesPerPixel * fWidth;
128 imagePtr += rowBytes;
129 }
joshualitt5bf99f12015-03-13 11:47:42 -0700130 }
131
Mike Reed92b33352019-08-24 19:39:13 -0400132 fDirtyRect.join({loc->fX, loc->fY, loc->fX + width, loc->fY + height});
robertphillips2b0536f2015-11-06 14:10:42 -0800133
joshualitt5df175e2015-11-18 13:37:54 -0800134 loc->fX += fOffset.fX;
135 loc->fY += fOffset.fY;
136 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700137
joshualitt5df175e2015-11-18 13:37:54 -0800138 return true;
139}
joshualitt5bf99f12015-03-13 11:47:42 -0700140
Brian Salomon943ed792017-10-30 09:37:55 -0400141void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400142 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800143 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400144 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400145 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800146 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700147 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500148 // Clamp to 4-byte aligned boundaries
149 unsigned int clearBits = 0x3 / fBytesPerPixel;
150 fDirtyRect.fLeft &= ~clearBits;
151 fDirtyRect.fRight += clearBits;
152 fDirtyRect.fRight &= ~clearBits;
153 SkASSERT(fDirtyRect.fRight <= fWidth);
154 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700155 dataPtr += rowBytes * fDirtyRect.fTop;
156 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400157
Robert Phillipsacaa6072017-07-28 10:54:53 -0400158 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400159 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800160 fDirtyRect.setEmpty();
161 SkDEBUGCODE(fDirty = false;)
162}
163
Brian Salomon2ee084e2016-12-16 18:59:19 -0500164void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500165 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700166
Herb Derby0ef780b2020-01-24 15:57:11 -0500167 fGenID = fGenerationCounter->next();
Herb Derby4d721712020-01-24 14:31:16 -0500168 fPlotLocator = CreatePlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400169 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
170 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800171
172 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700173 if (fData) {
174 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700175 }
176
joshualitt5df175e2015-11-18 13:37:54 -0800177 fDirtyRect.setEmpty();
178 SkDEBUGCODE(fDirty = false;)
179}
joshualitt5bf99f12015-03-13 11:47:42 -0700180
joshualitt5bf99f12015-03-13 11:47:42 -0700181///////////////////////////////////////////////////////////////////////////////
182
Herb Derby0ef780b2020-01-24 15:57:11 -0500183GrDrawOpAtlas::GrDrawOpAtlas(
184 GrProxyProvider* proxyProvider, const GrBackendFormat& format,
185 GrColorType colorType, int width, int height, int plotWidth, int plotHeight,
186 GenerationCounter* generationCounter, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500187 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400188 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400189 , fTextureWidth(width)
190 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500191 , fPlotWidth(plotWidth)
192 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500193 , fGenerationCounter(generationCounter)
194 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400195 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500196 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500197 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500198 int numPlotsX = width/plotWidth;
199 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400200 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400201 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
202 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800203
Jim Van Verth06f593c2018-02-20 11:30:10 -0500204 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700205
Herb Derby0ef780b2020-01-24 15:57:11 -0500206 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700207}
208
Herb Derby4d721712020-01-24 14:31:16 -0500209inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
Herb Derby1a496c52020-01-22 17:26:56 -0500210 for (auto evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500211 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700212 }
Herb Derby1a496c52020-01-22 17:26:56 -0500213
Herb Derby0ef780b2020-01-24 15:57:11 -0500214 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700215}
216
Herb Derby4d721712020-01-24 14:31:16 -0500217inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
218 PlotLocator* plotLocator, Plot* plot) {
219 int pageIdx = GetPageIndexFromID(plot->plotLocator());
Jim Van Vertha950b632017-09-12 11:54:11 -0400220 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700221
222 // If our most recent upload has already occurred then we have to insert a new
223 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
224 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500225 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700226 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500227 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500228
Greg Daniel9715b6c2019-12-10 15:03:10 -0500229 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
230 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500231
Brian Salomon29b60c92017-10-31 14:42:10 -0400232 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400233 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
234 plotsp->uploadToTexture(writePixels, proxy);
235 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500236 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700237 }
Herb Derby4d721712020-01-24 14:31:16 -0500238 *plotLocator = plot->plotLocator();
Robert Phillips256c37b2017-03-01 14:32:46 -0500239 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700240}
241
Herb Derby4d721712020-01-24 14:31:16 -0500242bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx, PlotLocator* plotLocator,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400243 GrDeferredUploadTarget* target, int width, int height,
244 const void* image, SkIPoint16* loc) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500245 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500246
247 // look through all allocated plots for one we can share, in Most Recently Refed order
248 PlotList::Iter plotIter;
249 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
250
251 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500252 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500253
254 if (plot->addSubImage(width, height, image, loc)) {
Herb Derby4d721712020-01-24 14:31:16 -0500255 return this->updatePlot(target, plotLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500256 }
257 }
258
259 return false;
260}
261
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400262// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
263//
264// This value is somewhat arbitrary -- the idea is to keep it low enough that
265// a page with unused plots will get removed reasonably quickly, but allow it
266// to hang around for a bit in case it's needed. The assumption is that flushes
267// are rare; i.e., we are not continually refreshing the frame.
Derek Sollenberger90196cc2017-10-09 15:00:33 -0400268static constexpr auto kRecentlyUsedCount = 256;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400269
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500270GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500271 PlotLocator* plotLocator,
272 GrDeferredUploadTarget* target,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500273 int width, int height,
274 const void* image, SkIPoint16* loc) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700275 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500276 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700277 }
joshualitt5bf99f12015-03-13 11:47:42 -0700278
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400279 const GrCaps& caps = *resourceProvider->caps();
280
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400281 // Look through each page to see if we can upload without having to flush
282 // We prioritize this upload to the first pages, not the most recently used, to make it easier
283 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500284 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Herb Derby4d721712020-01-24 14:31:16 -0500285 if (this->uploadToPage(caps, pageIdx, plotLocator, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500286 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400287 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400288 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400289
Jim Van Verth712fe732017-09-25 16:53:49 -0400290 // 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 -0400291 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
292 // We wait until we've grown to the full number of pages to begin evicting already flushed
293 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400294 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500295 if (fNumActivePages == this->maxPages()) {
296 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
297 Plot* plot = fPages[pageIdx].fPlotList.tail();
298 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500299 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500300 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500301 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
302 plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500303 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
304 SkASSERT(verify);
Herb Derby4d721712020-01-24 14:31:16 -0500305 if (!this->updatePlot(target, plotLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500306 return ErrorCode::kError;
307 }
308 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400309 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500310 }
311 } else {
312 // If we haven't activated all the available pages, try to create a new one and add to it
313 if (!this->activateNewPage(resourceProvider)) {
314 return ErrorCode::kError;
315 }
316
Herb Derby4d721712020-01-24 14:31:16 -0500317 if (this->uploadToPage(
318 caps, fNumActivePages-1, plotLocator, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500319 return ErrorCode::kSucceeded;
320 } else {
321 // If we fail to upload to a newly activated page then something has gone terribly
322 // wrong - return an error
323 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400324 }
325 }
326
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500327 if (!fNumActivePages) {
328 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700329 }
330
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400331 // Try to find a plot that we can perform an inline upload to.
332 // We prioritize this upload in reverse order of pages to counterbalance the order above.
333 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500334 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400335 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500336 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400337 plot = currentPlot;
338 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500339 }
joshualitt5bf99f12015-03-13 11:47:42 -0700340 }
341
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400342 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
343 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
344 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
345 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500346 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400347 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500348 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700349 }
350
Herb Derby4d721712020-01-24 14:31:16 -0500351 this->processEviction(plot->plotLocator());
352 int pageIdx = GetPageIndexFromID(plot->plotLocator());
Jim Van Vertha950b632017-09-12 11:54:11 -0400353 fPages[pageIdx].fPlotList.remove(plot);
354 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800355 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700356
Jim Van Vertha950b632017-09-12 11:54:11 -0400357 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500358 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800359 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700360 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800361
robertphillips1f0e3502015-11-10 10:19:50 -0800362 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400363 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500364 // With c+14 we could move sk_sp into lambda to only ref once.
365 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500366
Greg Daniel9715b6c2019-12-10 15:03:10 -0500367 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
368 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700369
Brian Salomon943ed792017-10-30 09:37:55 -0400370 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
371 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
372 plotsp->uploadToTexture(writePixels, proxy);
373 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500374 newPlot->setLastUploadToken(lastUploadToken);
375
Herb Derby4d721712020-01-24 14:31:16 -0500376 *plotLocator = newPlot->plotLocator();
robertphillips2b0536f2015-11-06 14:10:42 -0800377
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500378 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700379}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400380
Brian Salomon943ed792017-10-30 09:37:55 -0400381void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500382 if (fNumActivePages <= 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400383 fPrevFlushToken = startTokenForNextFlush;
384 return;
385 }
386
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400387 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400388 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400389 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500390 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400391 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
392 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400393 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400394 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
395 plot->resetFlushesSinceLastUsed();
396 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400397 }
398
399 plotIter.next();
400 }
401 }
402
403 // We only try to compact if the atlas was used in the recently completed flush.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400404 // This is to handle the case where a lot of text or path rendering has occurred but then just
405 // a blinking cursor is drawn.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400406 // TODO: consider if we should also do this if it's been a long time since the last atlas use
407 if (atlasUsedThisFlush) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500408 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500409 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400410
411 // For all plots but the last one, update number of flushes since used, and check to see
412 // if there are any in the first pages that the last page can safely upload to.
413 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400414#ifdef DUMP_ATLAS_DATA
415 if (gDumpAtlasData) {
416 SkDebugf("page %d: ", pageIndex);
417 }
418#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400419 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
420 while (Plot* plot = plotIter.get()) {
421 // Update number of flushes since plot was last used
422 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
423 // to avoid deleting everything when we return to text drawing in the blinking
424 // cursor case
425 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
426 plot->incFlushesSinceLastUsed();
427 }
428
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400429#ifdef DUMP_ATLAS_DATA
430 if (gDumpAtlasData) {
431 SkDebugf("%d ", plot->flushesSinceLastUsed());
432 }
433#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400434 // Count plots we can potentially upload to in all pages except the last one
435 // (the potential compactee).
436 if (plot->flushesSinceLastUsed() > kRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500437 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400438 }
439
440 plotIter.next();
441 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400442#ifdef DUMP_ATLAS_DATA
443 if (gDumpAtlasData) {
444 SkDebugf("\n");
445 }
446#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400447 }
448
Jim Van Verth06f593c2018-02-20 11:30:10 -0500449 // Count recently used plots in the last page and evict any that are no longer in use.
450 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400451 // clear out usage of this page unless we have a large need.
452 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500453 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400454#ifdef DUMP_ATLAS_DATA
455 if (gDumpAtlasData) {
456 SkDebugf("page %d: ", lastPageIndex);
457 }
458#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400459 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400460 // Update number of flushes since plot was last used
461 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
462 plot->incFlushesSinceLastUsed();
463 }
464
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400465#ifdef DUMP_ATLAS_DATA
466 if (gDumpAtlasData) {
467 SkDebugf("%d ", plot->flushesSinceLastUsed());
468 }
469#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400470 // If this plot was used recently
471 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
472 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400473 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400474 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400475 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400476 }
477 plotIter.next();
478 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400479#ifdef DUMP_ATLAS_DATA
480 if (gDumpAtlasData) {
481 SkDebugf("\n");
482 }
483#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500484
485 // If recently used plots in the last page are using less than a quarter of the page, try
486 // to evict them if there's available space in earlier pages. Since we prioritize uploading
487 // to the first pages, this will eventually clear out usage of this page unless we have a
488 // large need.
489 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
490 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
491 while (Plot* plot = plotIter.get()) {
492 // If this plot was used recently
493 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
494 // See if there's room in an earlier page and if so evict.
495 // We need to be somewhat harsh here so that a handful of plots that are
496 // consistently in use don't end up locking the page in memory.
497 if (availablePlots.count() > 0) {
498 this->processEvictionAndResetRects(plot);
499 this->processEvictionAndResetRects(availablePlots.back());
500 availablePlots.pop_back();
501 --usedPlots;
502 }
503 if (!usedPlots || !availablePlots.count()) {
504 break;
505 }
506 }
507 plotIter.next();
508 }
509 }
510
Jim Van Verth106b5c42017-09-26 12:45:29 -0400511 // If none of the plots in the last page have been used recently, delete it.
512 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400513#ifdef DUMP_ATLAS_DATA
514 if (gDumpAtlasData) {
515 SkDebugf("delete %d\n", fNumPages-1);
516 }
517#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500518 this->deactivateLastPage();
Jim Van Verth106b5c42017-09-26 12:45:29 -0400519 }
520 }
521
522 fPrevFlushToken = startTokenForNextFlush;
523}
524
Herb Derby0ef780b2020-01-24 15:57:11 -0500525bool GrDrawOpAtlas::createPages(
526 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500527 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500528
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400529 GrSurfaceDesc desc;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400530 desc.fWidth = fTextureWidth;
531 desc.fHeight = fTextureHeight;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400532
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400533 int numPlotsX = fTextureWidth/fPlotWidth;
534 int numPlotsY = fTextureHeight/fPlotHeight;
535
Robert Phillips4bc70112018-03-01 10:24:02 -0500536 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500537 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500538 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Greg Daniel47c20e82020-01-21 14:29:57 -0500539 fFormat, desc, swizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
540 GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400541 GrInternalSurfaceFlags::kNone, GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500542 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500543 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400544 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500545 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500546
547 // set up allocated plots
548 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
549
550 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
551 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
552 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
553 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500554 currPlot->reset(new Plot(
555 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500556
557 // build LRU list
558 fPages[i].fPlotList.addToHead(currPlot->get());
559 ++currPlot;
560 }
561 }
562
563 }
564
565 return true;
566}
567
568
569bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500570 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500571
Greg Daniel9715b6c2019-12-10 15:03:10 -0500572 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500573 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400574 }
575
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400576#ifdef DUMP_ATLAS_DATA
577 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500578 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400579 }
580#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500581
582 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400583 return true;
584}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400585
Robert Phillips4bc70112018-03-01 10:24:02 -0500586
587inline void GrDrawOpAtlas::deactivateLastPage() {
588 SkASSERT(fNumActivePages);
589
590 uint32_t lastPageIndex = fNumActivePages - 1;
591
592 int numPlotsX = fTextureWidth/fPlotWidth;
593 int numPlotsY = fTextureHeight/fPlotHeight;
594
Jim Van Verth106b5c42017-09-26 12:45:29 -0400595 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500596 for (int r = 0; r < numPlotsY; ++r) {
597 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500598 uint32_t plotIndex = r * numPlotsX + c;
599
600 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
601 currPlot->resetRects();
602 currPlot->resetFlushesSinceLastUsed();
603
604 // rebuild the LRU list
605 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
606 SkDEBUGCODE(currPlot->fList = nullptr);
607 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
608 }
609 }
610
611 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500612 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500613 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400614}
Herb Derby15d9ef22018-10-18 13:41:32 -0400615
Jim Van Verthf6206f92018-12-14 08:22:24 -0500616GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
617 static const SkISize kARGBDimensions[] = {
618 {256, 256}, // maxBytes < 2^19
619 {512, 256}, // 2^19 <= maxBytes < 2^20
620 {512, 512}, // 2^20 <= maxBytes < 2^21
621 {1024, 512}, // 2^21 <= maxBytes < 2^22
622 {1024, 1024}, // 2^22 <= maxBytes < 2^23
623 {2048, 1024}, // 2^23 <= maxBytes
624 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400625
Jim Van Verthf6206f92018-12-14 08:22:24 -0500626 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
627 maxBytes >>= 18;
628 // Take the floor of the log to get the index
629 int index = maxBytes > 0
630 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
631 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400632
Jim Van Verthf6206f92018-12-14 08:22:24 -0500633 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
634 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
635 fARGBDimensions.set(SkTMin<int>(kARGBDimensions[index].width(), maxTextureSize),
636 SkTMin<int>(kARGBDimensions[index].height(), maxTextureSize));
637 fMaxTextureSize = SkTMin<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400638}
639
640SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500641 if (kA8_GrMaskFormat == type) {
642 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
643 return { SkTMin<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
644 SkTMin<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
645 } else {
646 return fARGBDimensions;
647 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400648}
649
Jim Van Verthf6206f92018-12-14 08:22:24 -0500650SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
651 if (kA8_GrMaskFormat == type) {
652 SkISize atlasDimensions = this->atlasDimensions(type);
653 // For A8 we want to grow the plots at larger texture sizes to accept more of the
654 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
655 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400656
Jim Van Verth578b0892018-12-20 20:48:55 +0000657 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
658 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500659 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000660 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400661
Jim Van Verthf6206f92018-12-14 08:22:24 -0500662 return { plotWidth, plotHeight };
663 } else {
664 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
665 return { 256, 256 };
666 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400667}
668
Jim Van Verthf6206f92018-12-14 08:22:24 -0500669constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;