blob: 8f15db97c5169ce06f5a5f95975085a7525731d9 [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"
18#include "src/gpu/GrRectanizer.h"
19#include "src/gpu/GrResourceProvider.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040020#include "src/gpu/GrResourceProviderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrSurfaceProxyPriv.h"
22#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070023
Jim Van Verth3b9c5442020-01-16 14:52:16 -050024#ifdef DUMP_ATLAS_DATA
25static bool gDumpAtlasData = false;
26#endif
27
Robert Phillipscd5099c2018-02-09 09:56:56 -050028// When proxy allocation is deferred until flush time the proxies acting as atlases require
29// special handling. This is because the usage that can be determined from the ops themselves
30// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
31// atlases. Extending the usage interval of any op that uses an atlas to the start of the
32// flush (as is done for proxies that are used for sw-generated masks) also won't work because
33// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
34// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
35// (which calls this method).
36void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050037 for (uint32_t i = 0; i < fNumActivePages; ++i) {
38 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050039 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050040 }
41}
42
Robert Phillips4bc70112018-03-01 10:24:02 -050043std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050044 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040045 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050046 int height, int plotWidth, int plotHeight,
Brian Salomon9f545bc2017-11-06 10:36:57 -050047 AllowMultitexturing allowMultitexturing,
48 GrDrawOpAtlas::EvictionFunc func, void* data) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040049 if (!format.isValid()) {
50 return nullptr;
51 }
52
Robert Phillips42dda082019-05-14 13:29:45 -040053 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType, width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050054 height, plotWidth, plotHeight,
Robert Phillips4bc70112018-03-01 10:24:02 -050055 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050056 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040057 return nullptr;
58 }
59
Robert Phillips256c37b2017-03-01 14:32:46 -050060 atlas->registerEvictionCallback(func, data);
61 return atlas;
62}
63
Jim Van Verth3b9c5442020-01-16 14:52:16 -050064// The two bits that make up the texture index are packed into the u and v coordinate
65// respectively. To represent a '1', we negate the coordinate and subtract 1 (to handle 0).
66std::pair<int16_t, int16_t> GrDrawOpAtlas::PackIndexInTexCoords(int16_t u, int16_t v,
67 int texIndex) {
68 SkASSERT(texIndex >= 0 && texIndex < 4);
69 if (texIndex & 0x2) {
70 u = -u-1;
71 }
72 if (texIndex & 0x1) {
73 v = -v-1;
74 }
75 return std::make_pair(u, v);
76}
77
78std::tuple<int16_t, int16_t, int> GrDrawOpAtlas::UnpackIndexFromTexCoords(int16_t u, int16_t v) {
79 int texIndex = 0;
80 if (u < 0) {
81 u = -u-1;
82 texIndex |= 0x2;
83 }
84 if (v < 0) {
85 v = -v-1;
86 texIndex |= 0x1;
87 }
88 return std::make_tuple(u, v, texIndex);
89}
Robert Phillips256c37b2017-03-01 14:32:46 -050090
joshualitt5df175e2015-11-18 13:37:54 -080091////////////////////////////////////////////////////////////////////////////////
Jim Van Vertha950b632017-09-12 11:54:11 -040092GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY,
Robert Phillips42dda082019-05-14 13:29:45 -040093 int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040094 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
95 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040096 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040097 , fPageIndex(pageIndex)
98 , fPlotIndex(plotIndex)
Brian Salomon2ee084e2016-12-16 18:59:19 -050099 , fGenID(genID)
Jim Van Vertha950b632017-09-12 11:54:11 -0400100 , fID(CreateId(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -0500101 , fData(nullptr)
102 , fWidth(width)
103 , fHeight(height)
104 , fX(offX)
105 , fY(offY)
106 , fRects(nullptr)
107 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400108 , fColorType(colorType)
109 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800110#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500111 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800112#endif
113{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500114 // We expect the allocated dimensions to be a multiple of 4 bytes
115 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
116 // The padding for faster uploads only works for 1, 2 and 4 byte texels
117 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800118 fDirtyRect.setEmpty();
119}
joshualitt5bf99f12015-03-13 11:47:42 -0700120
Brian Salomon2ee084e2016-12-16 18:59:19 -0500121GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700122 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800123 delete fRects;
124}
joshualitt5bf99f12015-03-13 11:47:42 -0700125
Brian Salomon2ee084e2016-12-16 18:59:19 -0500126bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -0800127 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700128
joshualitt5df175e2015-11-18 13:37:54 -0800129 if (!fRects) {
130 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700131 }
132
joshualitt5df175e2015-11-18 13:37:54 -0800133 if (!fRects->addRect(width, height, loc)) {
134 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700135 }
joshualitt5bf99f12015-03-13 11:47:42 -0700136
jvanverthc3d706f2016-04-20 10:33:27 -0700137 if (!fData) {
138 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
139 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800140 }
141 size_t rowBytes = width * fBytesPerPixel;
142 const unsigned char* imagePtr = (const unsigned char*)image;
143 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700144 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800145 dataPtr += fBytesPerPixel * fWidth * loc->fY;
146 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400147 // copy into the data buffer, swizzling as we go if this is ARGB data
148 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
149 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400150 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400151 dataPtr += fBytesPerPixel * fWidth;
152 imagePtr += rowBytes;
153 }
154 } else {
155 for (int i = 0; i < height; ++i) {
156 memcpy(dataPtr, imagePtr, rowBytes);
157 dataPtr += fBytesPerPixel * fWidth;
158 imagePtr += rowBytes;
159 }
joshualitt5bf99f12015-03-13 11:47:42 -0700160 }
161
Mike Reed92b33352019-08-24 19:39:13 -0400162 fDirtyRect.join({loc->fX, loc->fY, loc->fX + width, loc->fY + height});
robertphillips2b0536f2015-11-06 14:10:42 -0800163
joshualitt5df175e2015-11-18 13:37:54 -0800164 loc->fX += fOffset.fX;
165 loc->fY += fOffset.fY;
166 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700167
joshualitt5df175e2015-11-18 13:37:54 -0800168 return true;
169}
joshualitt5bf99f12015-03-13 11:47:42 -0700170
Brian Salomon943ed792017-10-30 09:37:55 -0400171void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400172 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800173 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400174 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400175 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800176 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700177 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500178 // Clamp to 4-byte aligned boundaries
179 unsigned int clearBits = 0x3 / fBytesPerPixel;
180 fDirtyRect.fLeft &= ~clearBits;
181 fDirtyRect.fRight += clearBits;
182 fDirtyRect.fRight &= ~clearBits;
183 SkASSERT(fDirtyRect.fRight <= fWidth);
184 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700185 dataPtr += rowBytes * fDirtyRect.fTop;
186 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400187
Robert Phillipsacaa6072017-07-28 10:54:53 -0400188 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400189 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800190 fDirtyRect.setEmpty();
191 SkDEBUGCODE(fDirty = false;)
192}
193
Brian Salomon2ee084e2016-12-16 18:59:19 -0500194void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800195 if (fRects) {
196 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700197 }
198
joshualitt5df175e2015-11-18 13:37:54 -0800199 fGenID++;
Jim Van Vertha950b632017-09-12 11:54:11 -0400200 fID = CreateId(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400201 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
202 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800203
204 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700205 if (fData) {
206 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700207 }
208
joshualitt5df175e2015-11-18 13:37:54 -0800209 fDirtyRect.setEmpty();
210 SkDEBUGCODE(fDirty = false;)
211}
joshualitt5bf99f12015-03-13 11:47:42 -0700212
joshualitt5bf99f12015-03-13 11:47:42 -0700213///////////////////////////////////////////////////////////////////////////////
214
Greg Daniel4065d452018-11-16 15:43:41 -0500215GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -0400216 GrColorType colorType, int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500217 int plotWidth, int plotHeight, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500218 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400219 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400220 , fTextureWidth(width)
221 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500222 , fPlotWidth(plotWidth)
223 , fPlotHeight(plotHeight)
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400224 , fAtlasGeneration(kInvalidAtlasGeneration + 1)
Brian Salomon943ed792017-10-30 09:37:55 -0400225 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500226 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500227 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500228 int numPlotsX = width/plotWidth;
229 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400230 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400231 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
232 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800233
Jim Van Verth06f593c2018-02-20 11:30:10 -0500234 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700235
Robert Phillips4bc70112018-03-01 10:24:02 -0500236 this->createPages(proxyProvider);
joshualitt5bf99f12015-03-13 11:47:42 -0700237}
238
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400239inline void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700240 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
241 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
242 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400243 ++fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700244}
245
Brian Salomon29b60c92017-10-31 14:42:10 -0400246inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target, AtlasID* id, Plot* plot) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400247 int pageIdx = GetPageIndexFromID(plot->id());
248 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700249
250 // If our most recent upload has already occurred then we have to insert a new
251 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
252 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500253 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700254 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500255 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500256
Greg Daniel9715b6c2019-12-10 15:03:10 -0500257 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
258 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500259
Brian Salomon29b60c92017-10-31 14:42:10 -0400260 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400261 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
262 plotsp->uploadToTexture(writePixels, proxy);
263 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500264 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700265 }
266 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500267 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700268}
269
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400270bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx, AtlasID* id,
271 GrDeferredUploadTarget* target, int width, int height,
272 const void* image, SkIPoint16* loc) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500273 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500274
275 // look through all allocated plots for one we can share, in Most Recently Refed order
276 PlotList::Iter plotIter;
277 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
278
279 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500280 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500281
282 if (plot->addSubImage(width, height, image, loc)) {
283 return this->updatePlot(target, id, plot);
284 }
285 }
286
287 return false;
288}
289
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400290// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
291//
292// This value is somewhat arbitrary -- the idea is to keep it low enough that
293// a page with unused plots will get removed reasonably quickly, but allow it
294// to hang around for a bit in case it's needed. The assumption is that flushes
295// are rare; i.e., we are not continually refreshing the frame.
Derek Sollenberger90196cc2017-10-09 15:00:33 -0400296static constexpr auto kRecentlyUsedCount = 256;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400297
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500298GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
299 AtlasID* id, GrDeferredUploadTarget* target,
300 int width, int height,
301 const void* image, SkIPoint16* loc) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700302 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500303 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700304 }
joshualitt5bf99f12015-03-13 11:47:42 -0700305
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400306 const GrCaps& caps = *resourceProvider->caps();
307
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400308 // Look through each page to see if we can upload without having to flush
309 // We prioritize this upload to the first pages, not the most recently used, to make it easier
310 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500311 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400312 if (this->uploadToPage(caps, pageIdx, id, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500313 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400314 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400315 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400316
Jim Van Verth712fe732017-09-25 16:53:49 -0400317 // 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 -0400318 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
319 // We wait until we've grown to the full number of pages to begin evicting already flushed
320 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400321 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500322 if (fNumActivePages == this->maxPages()) {
323 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
324 Plot* plot = fPages[pageIdx].fPlotList.tail();
325 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500326 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500327 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500328 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
329 plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500330 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
331 SkASSERT(verify);
332 if (!this->updatePlot(target, id, plot)) {
333 return ErrorCode::kError;
334 }
335 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400336 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500337 }
338 } else {
339 // If we haven't activated all the available pages, try to create a new one and add to it
340 if (!this->activateNewPage(resourceProvider)) {
341 return ErrorCode::kError;
342 }
343
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400344 if (this->uploadToPage(caps, fNumActivePages-1, id, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500345 return ErrorCode::kSucceeded;
346 } else {
347 // If we fail to upload to a newly activated page then something has gone terribly
348 // wrong - return an error
349 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400350 }
351 }
352
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500353 if (!fNumActivePages) {
354 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700355 }
356
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400357 // Try to find a plot that we can perform an inline upload to.
358 // We prioritize this upload in reverse order of pages to counterbalance the order above.
359 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500360 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400361 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500362 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400363 plot = currentPlot;
364 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500365 }
joshualitt5bf99f12015-03-13 11:47:42 -0700366 }
367
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400368 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
369 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
370 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
371 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500372 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400373 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500374 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700375 }
376
joshualitt5bf99f12015-03-13 11:47:42 -0700377 this->processEviction(plot->id());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400378 int pageIdx = GetPageIndexFromID(plot->id());
Jim Van Vertha950b632017-09-12 11:54:11 -0400379 fPages[pageIdx].fPlotList.remove(plot);
380 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800381 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700382
Jim Van Vertha950b632017-09-12 11:54:11 -0400383 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500384 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800385 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700386 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800387
robertphillips1f0e3502015-11-10 10:19:50 -0800388 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400389 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500390 // With c+14 we could move sk_sp into lambda to only ref once.
391 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500392
Greg Daniel9715b6c2019-12-10 15:03:10 -0500393 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
394 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700395
Brian Salomon943ed792017-10-30 09:37:55 -0400396 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
397 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
398 plotsp->uploadToTexture(writePixels, proxy);
399 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500400 newPlot->setLastUploadToken(lastUploadToken);
401
joshualitt5bf99f12015-03-13 11:47:42 -0700402 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800403
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500404 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700405}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400406
Brian Salomon943ed792017-10-30 09:37:55 -0400407void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500408 if (fNumActivePages <= 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400409 fPrevFlushToken = startTokenForNextFlush;
410 return;
411 }
412
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400413 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400414 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400415 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500416 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400417 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
418 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400419 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400420 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
421 plot->resetFlushesSinceLastUsed();
422 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400423 }
424
425 plotIter.next();
426 }
427 }
428
429 // We only try to compact if the atlas was used in the recently completed flush.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400430 // This is to handle the case where a lot of text or path rendering has occurred but then just
431 // a blinking cursor is drawn.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400432 // TODO: consider if we should also do this if it's been a long time since the last atlas use
433 if (atlasUsedThisFlush) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500434 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500435 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400436
437 // For all plots but the last one, update number of flushes since used, and check to see
438 // if there are any in the first pages that the last page can safely upload to.
439 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400440#ifdef DUMP_ATLAS_DATA
441 if (gDumpAtlasData) {
442 SkDebugf("page %d: ", pageIndex);
443 }
444#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400445 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
446 while (Plot* plot = plotIter.get()) {
447 // Update number of flushes since plot was last used
448 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
449 // to avoid deleting everything when we return to text drawing in the blinking
450 // cursor case
451 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
452 plot->incFlushesSinceLastUsed();
453 }
454
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400455#ifdef DUMP_ATLAS_DATA
456 if (gDumpAtlasData) {
457 SkDebugf("%d ", plot->flushesSinceLastUsed());
458 }
459#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400460 // Count plots we can potentially upload to in all pages except the last one
461 // (the potential compactee).
462 if (plot->flushesSinceLastUsed() > kRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500463 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400464 }
465
466 plotIter.next();
467 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400468#ifdef DUMP_ATLAS_DATA
469 if (gDumpAtlasData) {
470 SkDebugf("\n");
471 }
472#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400473 }
474
Jim Van Verth06f593c2018-02-20 11:30:10 -0500475 // Count recently used plots in the last page and evict any that are no longer in use.
476 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400477 // clear out usage of this page unless we have a large need.
478 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500479 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400480#ifdef DUMP_ATLAS_DATA
481 if (gDumpAtlasData) {
482 SkDebugf("page %d: ", lastPageIndex);
483 }
484#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400485 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400486 // Update number of flushes since plot was last used
487 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
488 plot->incFlushesSinceLastUsed();
489 }
490
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400491#ifdef DUMP_ATLAS_DATA
492 if (gDumpAtlasData) {
493 SkDebugf("%d ", plot->flushesSinceLastUsed());
494 }
495#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400496 // If this plot was used recently
497 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
498 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400499 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400500 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400501 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400502 }
503 plotIter.next();
504 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400505#ifdef DUMP_ATLAS_DATA
506 if (gDumpAtlasData) {
507 SkDebugf("\n");
508 }
509#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500510
511 // If recently used plots in the last page are using less than a quarter of the page, try
512 // to evict them if there's available space in earlier pages. Since we prioritize uploading
513 // to the first pages, this will eventually clear out usage of this page unless we have a
514 // large need.
515 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
516 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
517 while (Plot* plot = plotIter.get()) {
518 // If this plot was used recently
519 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
520 // See if there's room in an earlier page and if so evict.
521 // We need to be somewhat harsh here so that a handful of plots that are
522 // consistently in use don't end up locking the page in memory.
523 if (availablePlots.count() > 0) {
524 this->processEvictionAndResetRects(plot);
525 this->processEvictionAndResetRects(availablePlots.back());
526 availablePlots.pop_back();
527 --usedPlots;
528 }
529 if (!usedPlots || !availablePlots.count()) {
530 break;
531 }
532 }
533 plotIter.next();
534 }
535 }
536
Jim Van Verth106b5c42017-09-26 12:45:29 -0400537 // If none of the plots in the last page have been used recently, delete it.
538 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400539#ifdef DUMP_ATLAS_DATA
540 if (gDumpAtlasData) {
541 SkDebugf("delete %d\n", fNumPages-1);
542 }
543#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500544 this->deactivateLastPage();
Jim Van Verth106b5c42017-09-26 12:45:29 -0400545 }
546 }
547
548 fPrevFlushToken = startTokenForNextFlush;
549}
550
Robert Phillips4bc70112018-03-01 10:24:02 -0500551bool GrDrawOpAtlas::createPages(GrProxyProvider* proxyProvider) {
552 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500553
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400554 GrSurfaceDesc desc;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400555 desc.fWidth = fTextureWidth;
556 desc.fHeight = fTextureHeight;
Greg Daniele877dce2019-07-11 10:52:43 -0400557 desc.fConfig = GrColorTypeToPixelConfig(fColorType);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400558
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400559 int numPlotsX = fTextureWidth/fPlotWidth;
560 int numPlotsY = fTextureHeight/fPlotHeight;
561
Robert Phillips4bc70112018-03-01 10:24:02 -0500562 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500563 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500564 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Greg Daniel47c20e82020-01-21 14:29:57 -0500565 fFormat, desc, swizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
566 GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400567 GrInternalSurfaceFlags::kNone, GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500568 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500569 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400570 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500571 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500572
573 // set up allocated plots
574 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
575
576 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
577 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
578 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
579 uint32_t plotIndex = r * numPlotsX + c;
580 currPlot->reset(new Plot(i, plotIndex, 1, x, y, fPlotWidth, fPlotHeight,
Robert Phillips42dda082019-05-14 13:29:45 -0400581 fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500582
583 // build LRU list
584 fPages[i].fPlotList.addToHead(currPlot->get());
585 ++currPlot;
586 }
587 }
588
589 }
590
591 return true;
592}
593
594
595bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500596 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500597
Greg Daniel9715b6c2019-12-10 15:03:10 -0500598 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500599 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400600 }
601
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400602#ifdef DUMP_ATLAS_DATA
603 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500604 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400605 }
606#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500607
608 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400609 return true;
610}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400611
Robert Phillips4bc70112018-03-01 10:24:02 -0500612
613inline void GrDrawOpAtlas::deactivateLastPage() {
614 SkASSERT(fNumActivePages);
615
616 uint32_t lastPageIndex = fNumActivePages - 1;
617
618 int numPlotsX = fTextureWidth/fPlotWidth;
619 int numPlotsY = fTextureHeight/fPlotHeight;
620
Jim Van Verth106b5c42017-09-26 12:45:29 -0400621 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500622 for (int r = 0; r < numPlotsY; ++r) {
623 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500624 uint32_t plotIndex = r * numPlotsX + c;
625
626 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
627 currPlot->resetRects();
628 currPlot->resetFlushesSinceLastUsed();
629
630 // rebuild the LRU list
631 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
632 SkDEBUGCODE(currPlot->fList = nullptr);
633 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
634 }
635 }
636
637 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500638 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500639 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400640}
Herb Derby15d9ef22018-10-18 13:41:32 -0400641
Jim Van Verthf6206f92018-12-14 08:22:24 -0500642GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
643 static const SkISize kARGBDimensions[] = {
644 {256, 256}, // maxBytes < 2^19
645 {512, 256}, // 2^19 <= maxBytes < 2^20
646 {512, 512}, // 2^20 <= maxBytes < 2^21
647 {1024, 512}, // 2^21 <= maxBytes < 2^22
648 {1024, 1024}, // 2^22 <= maxBytes < 2^23
649 {2048, 1024}, // 2^23 <= maxBytes
650 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400651
Jim Van Verthf6206f92018-12-14 08:22:24 -0500652 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
653 maxBytes >>= 18;
654 // Take the floor of the log to get the index
655 int index = maxBytes > 0
656 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
657 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400658
Jim Van Verthf6206f92018-12-14 08:22:24 -0500659 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
660 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
661 fARGBDimensions.set(SkTMin<int>(kARGBDimensions[index].width(), maxTextureSize),
662 SkTMin<int>(kARGBDimensions[index].height(), maxTextureSize));
663 fMaxTextureSize = SkTMin<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400664}
665
666SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500667 if (kA8_GrMaskFormat == type) {
668 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
669 return { SkTMin<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
670 SkTMin<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
671 } else {
672 return fARGBDimensions;
673 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400674}
675
Jim Van Verthf6206f92018-12-14 08:22:24 -0500676SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
677 if (kA8_GrMaskFormat == type) {
678 SkISize atlasDimensions = this->atlasDimensions(type);
679 // For A8 we want to grow the plots at larger texture sizes to accept more of the
680 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
681 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400682
Jim Van Verth578b0892018-12-20 20:48:55 +0000683 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
684 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500685 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000686 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400687
Jim Van Verthf6206f92018-12-14 08:22:24 -0500688 return { plotWidth, plotHeight };
689 } else {
690 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
691 return { 256, 256 };
692 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400693}
694
Jim Van Verthf6206f92018-12-14 08:22:24 -0500695constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;