blob: e2a11db1a9978839312af0833f519a578500ad0a [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
Jim Van Verth3b9c5442020-01-16 14:52:16 -050023#ifdef DUMP_ATLAS_DATA
24static bool gDumpAtlasData = false;
25#endif
26
Robert Phillipscd5099c2018-02-09 09:56:56 -050027// When proxy allocation is deferred until flush time the proxies acting as atlases require
28// special handling. This is because the usage that can be determined from the ops themselves
29// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
30// atlases. Extending the usage interval of any op that uses an atlas to the start of the
31// flush (as is done for proxies that are used for sw-generated masks) also won't work because
32// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
33// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
34// (which calls this method).
35void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050036 for (uint32_t i = 0; i < fNumActivePages; ++i) {
37 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050038 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050039 }
40}
41
Robert Phillips4bc70112018-03-01 10:24:02 -050042std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050043 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040044 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050045 int height, int plotWidth, int plotHeight,
Brian Salomon9f545bc2017-11-06 10:36:57 -050046 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050047 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040048 if (!format.isValid()) {
49 return nullptr;
50 }
51
Robert Phillips42dda082019-05-14 13:29:45 -040052 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType, width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050053 height, plotWidth, plotHeight,
Robert Phillips4bc70112018-03-01 10:24:02 -050054 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050055 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040056 return nullptr;
57 }
58
Herb Derby1a496c52020-01-22 17:26:56 -050059 atlas->fEvictionCallbacks.emplace_back(evictor);
Robert Phillips256c37b2017-03-01 14:32:46 -050060 return atlas;
61}
62
Jim Van Verth3b9c5442020-01-16 14:52:16 -050063// The two bits that make up the texture index are packed into the u and v coordinate
64// respectively. To represent a '1', we negate the coordinate and subtract 1 (to handle 0).
65std::pair<int16_t, int16_t> GrDrawOpAtlas::PackIndexInTexCoords(int16_t u, int16_t v,
66 int texIndex) {
67 SkASSERT(texIndex >= 0 && texIndex < 4);
68 if (texIndex & 0x2) {
69 u = -u-1;
70 }
71 if (texIndex & 0x1) {
72 v = -v-1;
73 }
74 return std::make_pair(u, v);
75}
76
77std::tuple<int16_t, int16_t, int> GrDrawOpAtlas::UnpackIndexFromTexCoords(int16_t u, int16_t v) {
78 int texIndex = 0;
79 if (u < 0) {
80 u = -u-1;
81 texIndex |= 0x2;
82 }
83 if (v < 0) {
84 v = -v-1;
85 texIndex |= 0x1;
86 }
87 return std::make_tuple(u, v, texIndex);
88}
Robert Phillips256c37b2017-03-01 14:32:46 -050089
joshualitt5df175e2015-11-18 13:37:54 -080090////////////////////////////////////////////////////////////////////////////////
Jim Van Vertha950b632017-09-12 11:54:11 -040091GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY,
Robert Phillips42dda082019-05-14 13:29:45 -040092 int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040093 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
94 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040095 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040096 , fPageIndex(pageIndex)
97 , fPlotIndex(plotIndex)
Brian Salomon2ee084e2016-12-16 18:59:19 -050098 , fGenID(genID)
Jim Van Vertha950b632017-09-12 11:54:11 -040099 , fID(CreateId(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -0500100 , fData(nullptr)
101 , fWidth(width)
102 , fHeight(height)
103 , fX(offX)
104 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -0500105 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500106 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400107 , fColorType(colorType)
108 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800109#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500110 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800111#endif
112{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500113 // We expect the allocated dimensions to be a multiple of 4 bytes
114 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
115 // The padding for faster uploads only works for 1, 2 and 4 byte texels
116 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800117 fDirtyRect.setEmpty();
118}
joshualitt5bf99f12015-03-13 11:47:42 -0700119
Brian Salomon2ee084e2016-12-16 18:59:19 -0500120GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700121 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800122}
joshualitt5bf99f12015-03-13 11:47:42 -0700123
Brian Salomon2ee084e2016-12-16 18:59:19 -0500124bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -0800125 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700126
Herb Derby73c75872020-01-22 18:09:16 -0500127 if (!fRectanizer.addRect(width, height, loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800128 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700129 }
joshualitt5bf99f12015-03-13 11:47:42 -0700130
jvanverthc3d706f2016-04-20 10:33:27 -0700131 if (!fData) {
132 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
133 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800134 }
135 size_t rowBytes = width * fBytesPerPixel;
136 const unsigned char* imagePtr = (const unsigned char*)image;
137 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700138 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800139 dataPtr += fBytesPerPixel * fWidth * loc->fY;
140 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400141 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500142 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400143 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400144 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400145 dataPtr += fBytesPerPixel * fWidth;
146 imagePtr += rowBytes;
147 }
148 } else {
149 for (int i = 0; i < height; ++i) {
150 memcpy(dataPtr, imagePtr, rowBytes);
151 dataPtr += fBytesPerPixel * fWidth;
152 imagePtr += rowBytes;
153 }
joshualitt5bf99f12015-03-13 11:47:42 -0700154 }
155
Mike Reed92b33352019-08-24 19:39:13 -0400156 fDirtyRect.join({loc->fX, loc->fY, loc->fX + width, loc->fY + height});
robertphillips2b0536f2015-11-06 14:10:42 -0800157
joshualitt5df175e2015-11-18 13:37:54 -0800158 loc->fX += fOffset.fX;
159 loc->fY += fOffset.fY;
160 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700161
joshualitt5df175e2015-11-18 13:37:54 -0800162 return true;
163}
joshualitt5bf99f12015-03-13 11:47:42 -0700164
Brian Salomon943ed792017-10-30 09:37:55 -0400165void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400166 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800167 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400168 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400169 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800170 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700171 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500172 // Clamp to 4-byte aligned boundaries
173 unsigned int clearBits = 0x3 / fBytesPerPixel;
174 fDirtyRect.fLeft &= ~clearBits;
175 fDirtyRect.fRight += clearBits;
176 fDirtyRect.fRight &= ~clearBits;
177 SkASSERT(fDirtyRect.fRight <= fWidth);
178 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700179 dataPtr += rowBytes * fDirtyRect.fTop;
180 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400181
Robert Phillipsacaa6072017-07-28 10:54:53 -0400182 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400183 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800184 fDirtyRect.setEmpty();
185 SkDEBUGCODE(fDirty = false;)
186}
187
Brian Salomon2ee084e2016-12-16 18:59:19 -0500188void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500189 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700190
joshualitt5df175e2015-11-18 13:37:54 -0800191 fGenID++;
Jim Van Vertha950b632017-09-12 11:54:11 -0400192 fID = CreateId(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400193 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
194 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800195
196 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700197 if (fData) {
198 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700199 }
200
joshualitt5df175e2015-11-18 13:37:54 -0800201 fDirtyRect.setEmpty();
202 SkDEBUGCODE(fDirty = false;)
203}
joshualitt5bf99f12015-03-13 11:47:42 -0700204
joshualitt5bf99f12015-03-13 11:47:42 -0700205///////////////////////////////////////////////////////////////////////////////
206
Greg Daniel4065d452018-11-16 15:43:41 -0500207GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -0400208 GrColorType colorType, int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500209 int plotWidth, int plotHeight, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500210 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400211 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400212 , fTextureWidth(width)
213 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500214 , fPlotWidth(plotWidth)
215 , fPlotHeight(plotHeight)
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400216 , fAtlasGeneration(kInvalidAtlasGeneration + 1)
Brian Salomon943ed792017-10-30 09:37:55 -0400217 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500218 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500219 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500220 int numPlotsX = width/plotWidth;
221 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400222 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400223 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
224 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800225
Jim Van Verth06f593c2018-02-20 11:30:10 -0500226 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700227
Robert Phillips4bc70112018-03-01 10:24:02 -0500228 this->createPages(proxyProvider);
joshualitt5bf99f12015-03-13 11:47:42 -0700229}
230
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400231inline void GrDrawOpAtlas::processEviction(AtlasID id) {
Herb Derby1a496c52020-01-22 17:26:56 -0500232 for (auto evictor : fEvictionCallbacks) {
233 evictor->evict(id);
joshualitt5bf99f12015-03-13 11:47:42 -0700234 }
Herb Derby1a496c52020-01-22 17:26:56 -0500235
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400236 ++fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700237}
238
Brian Salomon29b60c92017-10-31 14:42:10 -0400239inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target, AtlasID* id, Plot* plot) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400240 int pageIdx = GetPageIndexFromID(plot->id());
241 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700242
243 // If our most recent upload has already occurred then we have to insert a new
244 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
245 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500246 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700247 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500248 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500249
Greg Daniel9715b6c2019-12-10 15:03:10 -0500250 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
251 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500252
Brian Salomon29b60c92017-10-31 14:42:10 -0400253 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400254 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
255 plotsp->uploadToTexture(writePixels, proxy);
256 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500257 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700258 }
259 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500260 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700261}
262
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400263bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx, AtlasID* id,
264 GrDeferredUploadTarget* target, int width, int height,
265 const void* image, SkIPoint16* loc) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500266 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500267
268 // look through all allocated plots for one we can share, in Most Recently Refed order
269 PlotList::Iter plotIter;
270 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
271
272 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500273 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500274
275 if (plot->addSubImage(width, height, image, loc)) {
276 return this->updatePlot(target, id, plot);
277 }
278 }
279
280 return false;
281}
282
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400283// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
284//
285// This value is somewhat arbitrary -- the idea is to keep it low enough that
286// a page with unused plots will get removed reasonably quickly, but allow it
287// to hang around for a bit in case it's needed. The assumption is that flushes
288// are rare; i.e., we are not continually refreshing the frame.
Derek Sollenberger90196cc2017-10-09 15:00:33 -0400289static constexpr auto kRecentlyUsedCount = 256;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400290
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500291GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
292 AtlasID* id, GrDeferredUploadTarget* target,
293 int width, int height,
294 const void* image, SkIPoint16* loc) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700295 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500296 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700297 }
joshualitt5bf99f12015-03-13 11:47:42 -0700298
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400299 const GrCaps& caps = *resourceProvider->caps();
300
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400301 // Look through each page to see if we can upload without having to flush
302 // We prioritize this upload to the first pages, not the most recently used, to make it easier
303 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500304 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400305 if (this->uploadToPage(caps, pageIdx, id, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500306 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400307 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400308 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400309
Jim Van Verth712fe732017-09-25 16:53:49 -0400310 // 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 -0400311 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
312 // We wait until we've grown to the full number of pages to begin evicting already flushed
313 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400314 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500315 if (fNumActivePages == this->maxPages()) {
316 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
317 Plot* plot = fPages[pageIdx].fPlotList.tail();
318 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500319 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500320 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500321 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
322 plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500323 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
324 SkASSERT(verify);
325 if (!this->updatePlot(target, id, plot)) {
326 return ErrorCode::kError;
327 }
328 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400329 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500330 }
331 } else {
332 // If we haven't activated all the available pages, try to create a new one and add to it
333 if (!this->activateNewPage(resourceProvider)) {
334 return ErrorCode::kError;
335 }
336
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400337 if (this->uploadToPage(caps, fNumActivePages-1, id, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500338 return ErrorCode::kSucceeded;
339 } else {
340 // If we fail to upload to a newly activated page then something has gone terribly
341 // wrong - return an error
342 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400343 }
344 }
345
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500346 if (!fNumActivePages) {
347 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700348 }
349
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400350 // Try to find a plot that we can perform an inline upload to.
351 // We prioritize this upload in reverse order of pages to counterbalance the order above.
352 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500353 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400354 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500355 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400356 plot = currentPlot;
357 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500358 }
joshualitt5bf99f12015-03-13 11:47:42 -0700359 }
360
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400361 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
362 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
363 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
364 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500365 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400366 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500367 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700368 }
369
joshualitt5bf99f12015-03-13 11:47:42 -0700370 this->processEviction(plot->id());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400371 int pageIdx = GetPageIndexFromID(plot->id());
Jim Van Vertha950b632017-09-12 11:54:11 -0400372 fPages[pageIdx].fPlotList.remove(plot);
373 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800374 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700375
Jim Van Vertha950b632017-09-12 11:54:11 -0400376 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500377 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800378 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700379 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800380
robertphillips1f0e3502015-11-10 10:19:50 -0800381 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400382 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500383 // With c+14 we could move sk_sp into lambda to only ref once.
384 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500385
Greg Daniel9715b6c2019-12-10 15:03:10 -0500386 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
387 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700388
Brian Salomon943ed792017-10-30 09:37:55 -0400389 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
390 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
391 plotsp->uploadToTexture(writePixels, proxy);
392 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500393 newPlot->setLastUploadToken(lastUploadToken);
394
joshualitt5bf99f12015-03-13 11:47:42 -0700395 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800396
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500397 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700398}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400399
Brian Salomon943ed792017-10-30 09:37:55 -0400400void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500401 if (fNumActivePages <= 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400402 fPrevFlushToken = startTokenForNextFlush;
403 return;
404 }
405
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400406 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400407 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400408 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500409 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400410 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
411 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400412 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400413 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
414 plot->resetFlushesSinceLastUsed();
415 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400416 }
417
418 plotIter.next();
419 }
420 }
421
422 // We only try to compact if the atlas was used in the recently completed flush.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400423 // This is to handle the case where a lot of text or path rendering has occurred but then just
424 // a blinking cursor is drawn.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400425 // TODO: consider if we should also do this if it's been a long time since the last atlas use
426 if (atlasUsedThisFlush) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500427 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500428 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400429
430 // For all plots but the last one, update number of flushes since used, and check to see
431 // if there are any in the first pages that the last page can safely upload to.
432 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400433#ifdef DUMP_ATLAS_DATA
434 if (gDumpAtlasData) {
435 SkDebugf("page %d: ", pageIndex);
436 }
437#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400438 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
439 while (Plot* plot = plotIter.get()) {
440 // Update number of flushes since plot was last used
441 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
442 // to avoid deleting everything when we return to text drawing in the blinking
443 // cursor case
444 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
445 plot->incFlushesSinceLastUsed();
446 }
447
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400448#ifdef DUMP_ATLAS_DATA
449 if (gDumpAtlasData) {
450 SkDebugf("%d ", plot->flushesSinceLastUsed());
451 }
452#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400453 // Count plots we can potentially upload to in all pages except the last one
454 // (the potential compactee).
455 if (plot->flushesSinceLastUsed() > kRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500456 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400457 }
458
459 plotIter.next();
460 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400461#ifdef DUMP_ATLAS_DATA
462 if (gDumpAtlasData) {
463 SkDebugf("\n");
464 }
465#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400466 }
467
Jim Van Verth06f593c2018-02-20 11:30:10 -0500468 // Count recently used plots in the last page and evict any that are no longer in use.
469 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400470 // clear out usage of this page unless we have a large need.
471 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500472 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400473#ifdef DUMP_ATLAS_DATA
474 if (gDumpAtlasData) {
475 SkDebugf("page %d: ", lastPageIndex);
476 }
477#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400478 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400479 // Update number of flushes since plot was last used
480 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
481 plot->incFlushesSinceLastUsed();
482 }
483
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400484#ifdef DUMP_ATLAS_DATA
485 if (gDumpAtlasData) {
486 SkDebugf("%d ", plot->flushesSinceLastUsed());
487 }
488#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400489 // If this plot was used recently
490 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
491 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400492 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400493 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400494 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400495 }
496 plotIter.next();
497 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400498#ifdef DUMP_ATLAS_DATA
499 if (gDumpAtlasData) {
500 SkDebugf("\n");
501 }
502#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500503
504 // If recently used plots in the last page are using less than a quarter of the page, try
505 // to evict them if there's available space in earlier pages. Since we prioritize uploading
506 // to the first pages, this will eventually clear out usage of this page unless we have a
507 // large need.
508 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
509 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
510 while (Plot* plot = plotIter.get()) {
511 // If this plot was used recently
512 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
513 // See if there's room in an earlier page and if so evict.
514 // We need to be somewhat harsh here so that a handful of plots that are
515 // consistently in use don't end up locking the page in memory.
516 if (availablePlots.count() > 0) {
517 this->processEvictionAndResetRects(plot);
518 this->processEvictionAndResetRects(availablePlots.back());
519 availablePlots.pop_back();
520 --usedPlots;
521 }
522 if (!usedPlots || !availablePlots.count()) {
523 break;
524 }
525 }
526 plotIter.next();
527 }
528 }
529
Jim Van Verth106b5c42017-09-26 12:45:29 -0400530 // If none of the plots in the last page have been used recently, delete it.
531 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400532#ifdef DUMP_ATLAS_DATA
533 if (gDumpAtlasData) {
534 SkDebugf("delete %d\n", fNumPages-1);
535 }
536#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500537 this->deactivateLastPage();
Jim Van Verth106b5c42017-09-26 12:45:29 -0400538 }
539 }
540
541 fPrevFlushToken = startTokenForNextFlush;
542}
543
Robert Phillips4bc70112018-03-01 10:24:02 -0500544bool GrDrawOpAtlas::createPages(GrProxyProvider* proxyProvider) {
545 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500546
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400547 GrSurfaceDesc desc;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400548 desc.fWidth = fTextureWidth;
549 desc.fHeight = fTextureHeight;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400550
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400551 int numPlotsX = fTextureWidth/fPlotWidth;
552 int numPlotsY = fTextureHeight/fPlotHeight;
553
Robert Phillips4bc70112018-03-01 10:24:02 -0500554 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500555 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500556 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Greg Daniel47c20e82020-01-21 14:29:57 -0500557 fFormat, desc, swizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
558 GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400559 GrInternalSurfaceFlags::kNone, GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500560 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500561 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400562 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500563 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500564
565 // set up allocated plots
566 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
567
568 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
569 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
570 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
571 uint32_t plotIndex = r * numPlotsX + c;
572 currPlot->reset(new Plot(i, plotIndex, 1, x, y, fPlotWidth, fPlotHeight,
Robert Phillips42dda082019-05-14 13:29:45 -0400573 fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500574
575 // build LRU list
576 fPages[i].fPlotList.addToHead(currPlot->get());
577 ++currPlot;
578 }
579 }
580
581 }
582
583 return true;
584}
585
586
587bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500588 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500589
Greg Daniel9715b6c2019-12-10 15:03:10 -0500590 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500591 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400592 }
593
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400594#ifdef DUMP_ATLAS_DATA
595 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500596 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400597 }
598#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500599
600 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400601 return true;
602}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400603
Robert Phillips4bc70112018-03-01 10:24:02 -0500604
605inline void GrDrawOpAtlas::deactivateLastPage() {
606 SkASSERT(fNumActivePages);
607
608 uint32_t lastPageIndex = fNumActivePages - 1;
609
610 int numPlotsX = fTextureWidth/fPlotWidth;
611 int numPlotsY = fTextureHeight/fPlotHeight;
612
Jim Van Verth106b5c42017-09-26 12:45:29 -0400613 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500614 for (int r = 0; r < numPlotsY; ++r) {
615 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500616 uint32_t plotIndex = r * numPlotsX + c;
617
618 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
619 currPlot->resetRects();
620 currPlot->resetFlushesSinceLastUsed();
621
622 // rebuild the LRU list
623 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
624 SkDEBUGCODE(currPlot->fList = nullptr);
625 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
626 }
627 }
628
629 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500630 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500631 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400632}
Herb Derby15d9ef22018-10-18 13:41:32 -0400633
Jim Van Verthf6206f92018-12-14 08:22:24 -0500634GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
635 static const SkISize kARGBDimensions[] = {
636 {256, 256}, // maxBytes < 2^19
637 {512, 256}, // 2^19 <= maxBytes < 2^20
638 {512, 512}, // 2^20 <= maxBytes < 2^21
639 {1024, 512}, // 2^21 <= maxBytes < 2^22
640 {1024, 1024}, // 2^22 <= maxBytes < 2^23
641 {2048, 1024}, // 2^23 <= maxBytes
642 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400643
Jim Van Verthf6206f92018-12-14 08:22:24 -0500644 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
645 maxBytes >>= 18;
646 // Take the floor of the log to get the index
647 int index = maxBytes > 0
648 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
649 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400650
Jim Van Verthf6206f92018-12-14 08:22:24 -0500651 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
652 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
653 fARGBDimensions.set(SkTMin<int>(kARGBDimensions[index].width(), maxTextureSize),
654 SkTMin<int>(kARGBDimensions[index].height(), maxTextureSize));
655 fMaxTextureSize = SkTMin<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400656}
657
658SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500659 if (kA8_GrMaskFormat == type) {
660 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
661 return { SkTMin<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
662 SkTMin<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
663 } else {
664 return fARGBDimensions;
665 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400666}
667
Jim Van Verthf6206f92018-12-14 08:22:24 -0500668SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
669 if (kA8_GrMaskFormat == type) {
670 SkISize atlasDimensions = this->atlasDimensions(type);
671 // For A8 we want to grow the plots at larger texture sizes to accept more of the
672 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
673 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400674
Jim Van Verth578b0892018-12-20 20:48:55 +0000675 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
676 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500677 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000678 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400679
Jim Van Verthf6206f92018-12-14 08:22:24 -0500680 return { plotWidth, plotHeight };
681 } else {
682 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
683 return { 256, 256 };
684 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400685}
686
Jim Van Verthf6206f92018-12-14 08:22:24 -0500687constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;