blob: 63f60ea742225f6cb0343216364f5416ac36e8e4 [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 Verthcb93a202020-01-24 12:12:41 -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 Verthcb93a202020-01-24 12:12:41 -050063// The two bits that make up the texture index are packed into the lower bits of the u and v
64// coordinate respectively.
65std::pair<uint16_t, uint16_t> GrDrawOpAtlas::PackIndexInTexCoords(uint16_t u, uint16_t v,
66 int pageIndex) {
67 SkASSERT(pageIndex >= 0 && pageIndex < 4);
68 uint16_t uBit = (pageIndex >> 1u) & 0x1u;
69 uint16_t vBit = pageIndex & 0x1u;
70 u <<= 1u;
71 u |= uBit;
72 v <<= 1u;
73 v |= vBit;
74 return std::make_pair(u, v);
75}
76
77std::tuple<uint16_t, uint16_t, int> GrDrawOpAtlas::UnpackIndexFromTexCoords(uint16_t u,
78 uint16_t v) {
79 int pageIndex = 0;
80 if (u & 0x1) {
81 pageIndex |= 0x2;
82 }
83 if (v & 0x1) {
84 pageIndex |= 0x1;
85 }
86 return std::make_tuple(u >> 1, v >> 1, pageIndex);
87}
Robert Phillips256c37b2017-03-01 14:32:46 -050088
joshualitt5df175e2015-11-18 13:37:54 -080089////////////////////////////////////////////////////////////////////////////////
Jim Van Vertha950b632017-09-12 11:54:11 -040090GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY,
Robert Phillips42dda082019-05-14 13:29:45 -040091 int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040092 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
93 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040094 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040095 , fPageIndex(pageIndex)
96 , fPlotIndex(plotIndex)
Brian Salomon2ee084e2016-12-16 18:59:19 -050097 , fGenID(genID)
Jim Van Vertha950b632017-09-12 11:54:11 -040098 , fID(CreateId(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -050099 , fData(nullptr)
100 , fWidth(width)
101 , fHeight(height)
102 , fX(offX)
103 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -0500104 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500105 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400106 , fColorType(colorType)
107 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800108#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500109 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800110#endif
111{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500112 // We expect the allocated dimensions to be a multiple of 4 bytes
113 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
114 // The padding for faster uploads only works for 1, 2 and 4 byte texels
115 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800116 fDirtyRect.setEmpty();
117}
joshualitt5bf99f12015-03-13 11:47:42 -0700118
Brian Salomon2ee084e2016-12-16 18:59:19 -0500119GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700120 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800121}
joshualitt5bf99f12015-03-13 11:47:42 -0700122
Brian Salomon2ee084e2016-12-16 18:59:19 -0500123bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -0800124 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700125
Herb Derby73c75872020-01-22 18:09:16 -0500126 if (!fRectanizer.addRect(width, height, loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800127 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700128 }
joshualitt5bf99f12015-03-13 11:47:42 -0700129
jvanverthc3d706f2016-04-20 10:33:27 -0700130 if (!fData) {
131 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
132 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800133 }
134 size_t rowBytes = width * fBytesPerPixel;
135 const unsigned char* imagePtr = (const unsigned char*)image;
136 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700137 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800138 dataPtr += fBytesPerPixel * fWidth * loc->fY;
139 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400140 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500141 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400142 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400143 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400144 dataPtr += fBytesPerPixel * fWidth;
145 imagePtr += rowBytes;
146 }
147 } else {
148 for (int i = 0; i < height; ++i) {
149 memcpy(dataPtr, imagePtr, rowBytes);
150 dataPtr += fBytesPerPixel * fWidth;
151 imagePtr += rowBytes;
152 }
joshualitt5bf99f12015-03-13 11:47:42 -0700153 }
154
Mike Reed92b33352019-08-24 19:39:13 -0400155 fDirtyRect.join({loc->fX, loc->fY, loc->fX + width, loc->fY + height});
robertphillips2b0536f2015-11-06 14:10:42 -0800156
joshualitt5df175e2015-11-18 13:37:54 -0800157 loc->fX += fOffset.fX;
158 loc->fY += fOffset.fY;
159 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700160
joshualitt5df175e2015-11-18 13:37:54 -0800161 return true;
162}
joshualitt5bf99f12015-03-13 11:47:42 -0700163
Brian Salomon943ed792017-10-30 09:37:55 -0400164void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400165 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800166 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400167 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400168 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800169 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700170 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500171 // Clamp to 4-byte aligned boundaries
172 unsigned int clearBits = 0x3 / fBytesPerPixel;
173 fDirtyRect.fLeft &= ~clearBits;
174 fDirtyRect.fRight += clearBits;
175 fDirtyRect.fRight &= ~clearBits;
176 SkASSERT(fDirtyRect.fRight <= fWidth);
177 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700178 dataPtr += rowBytes * fDirtyRect.fTop;
179 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400180
Robert Phillipsacaa6072017-07-28 10:54:53 -0400181 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400182 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800183 fDirtyRect.setEmpty();
184 SkDEBUGCODE(fDirty = false;)
185}
186
Brian Salomon2ee084e2016-12-16 18:59:19 -0500187void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500188 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700189
joshualitt5df175e2015-11-18 13:37:54 -0800190 fGenID++;
Jim Van Vertha950b632017-09-12 11:54:11 -0400191 fID = CreateId(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400192 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
193 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800194
195 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700196 if (fData) {
197 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700198 }
199
joshualitt5df175e2015-11-18 13:37:54 -0800200 fDirtyRect.setEmpty();
201 SkDEBUGCODE(fDirty = false;)
202}
joshualitt5bf99f12015-03-13 11:47:42 -0700203
joshualitt5bf99f12015-03-13 11:47:42 -0700204///////////////////////////////////////////////////////////////////////////////
205
Greg Daniel4065d452018-11-16 15:43:41 -0500206GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -0400207 GrColorType colorType, int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500208 int plotWidth, int plotHeight, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500209 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400210 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400211 , fTextureWidth(width)
212 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500213 , fPlotWidth(plotWidth)
214 , fPlotHeight(plotHeight)
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400215 , fAtlasGeneration(kInvalidAtlasGeneration + 1)
Brian Salomon943ed792017-10-30 09:37:55 -0400216 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500217 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500218 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500219 int numPlotsX = width/plotWidth;
220 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400221 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400222 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
223 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800224
Jim Van Verth06f593c2018-02-20 11:30:10 -0500225 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700226
Robert Phillips4bc70112018-03-01 10:24:02 -0500227 this->createPages(proxyProvider);
joshualitt5bf99f12015-03-13 11:47:42 -0700228}
229
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400230inline void GrDrawOpAtlas::processEviction(AtlasID id) {
Herb Derby1a496c52020-01-22 17:26:56 -0500231 for (auto evictor : fEvictionCallbacks) {
232 evictor->evict(id);
joshualitt5bf99f12015-03-13 11:47:42 -0700233 }
Herb Derby1a496c52020-01-22 17:26:56 -0500234
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400235 ++fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700236}
237
Brian Salomon29b60c92017-10-31 14:42:10 -0400238inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target, AtlasID* id, Plot* plot) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400239 int pageIdx = GetPageIndexFromID(plot->id());
240 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700241
242 // If our most recent upload has already occurred then we have to insert a new
243 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
244 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500245 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700246 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500247 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500248
Greg Daniel9715b6c2019-12-10 15:03:10 -0500249 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
250 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500251
Brian Salomon29b60c92017-10-31 14:42:10 -0400252 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400253 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
254 plotsp->uploadToTexture(writePixels, proxy);
255 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500256 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700257 }
258 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500259 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700260}
261
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400262bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx, AtlasID* id,
263 GrDeferredUploadTarget* target, int width, int height,
264 const void* image, SkIPoint16* loc) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500265 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500266
267 // look through all allocated plots for one we can share, in Most Recently Refed order
268 PlotList::Iter plotIter;
269 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
270
271 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500272 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500273
274 if (plot->addSubImage(width, height, image, loc)) {
275 return this->updatePlot(target, id, plot);
276 }
277 }
278
279 return false;
280}
281
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400282// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
283//
284// This value is somewhat arbitrary -- the idea is to keep it low enough that
285// a page with unused plots will get removed reasonably quickly, but allow it
286// to hang around for a bit in case it's needed. The assumption is that flushes
287// are rare; i.e., we are not continually refreshing the frame.
Derek Sollenberger90196cc2017-10-09 15:00:33 -0400288static constexpr auto kRecentlyUsedCount = 256;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400289
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500290GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
291 AtlasID* id, GrDeferredUploadTarget* target,
292 int width, int height,
293 const void* image, SkIPoint16* loc) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700294 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500295 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700296 }
joshualitt5bf99f12015-03-13 11:47:42 -0700297
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400298 const GrCaps& caps = *resourceProvider->caps();
299
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400300 // Look through each page to see if we can upload without having to flush
301 // We prioritize this upload to the first pages, not the most recently used, to make it easier
302 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500303 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400304 if (this->uploadToPage(caps, pageIdx, id, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500305 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400306 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400307 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400308
Jim Van Verth712fe732017-09-25 16:53:49 -0400309 // 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 -0400310 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
311 // We wait until we've grown to the full number of pages to begin evicting already flushed
312 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400313 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500314 if (fNumActivePages == this->maxPages()) {
315 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
316 Plot* plot = fPages[pageIdx].fPlotList.tail();
317 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500318 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500319 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500320 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
321 plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500322 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
323 SkASSERT(verify);
324 if (!this->updatePlot(target, id, plot)) {
325 return ErrorCode::kError;
326 }
327 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400328 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500329 }
330 } else {
331 // If we haven't activated all the available pages, try to create a new one and add to it
332 if (!this->activateNewPage(resourceProvider)) {
333 return ErrorCode::kError;
334 }
335
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400336 if (this->uploadToPage(caps, fNumActivePages-1, id, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500337 return ErrorCode::kSucceeded;
338 } else {
339 // If we fail to upload to a newly activated page then something has gone terribly
340 // wrong - return an error
341 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400342 }
343 }
344
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500345 if (!fNumActivePages) {
346 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700347 }
348
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400349 // Try to find a plot that we can perform an inline upload to.
350 // We prioritize this upload in reverse order of pages to counterbalance the order above.
351 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500352 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400353 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500354 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400355 plot = currentPlot;
356 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500357 }
joshualitt5bf99f12015-03-13 11:47:42 -0700358 }
359
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400360 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
361 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
362 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
363 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500364 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400365 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500366 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700367 }
368
joshualitt5bf99f12015-03-13 11:47:42 -0700369 this->processEviction(plot->id());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400370 int pageIdx = GetPageIndexFromID(plot->id());
Jim Van Vertha950b632017-09-12 11:54:11 -0400371 fPages[pageIdx].fPlotList.remove(plot);
372 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800373 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700374
Jim Van Vertha950b632017-09-12 11:54:11 -0400375 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500376 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800377 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700378 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800379
robertphillips1f0e3502015-11-10 10:19:50 -0800380 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400381 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500382 // With c+14 we could move sk_sp into lambda to only ref once.
383 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500384
Greg Daniel9715b6c2019-12-10 15:03:10 -0500385 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
386 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700387
Brian Salomon943ed792017-10-30 09:37:55 -0400388 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
389 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
390 plotsp->uploadToTexture(writePixels, proxy);
391 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500392 newPlot->setLastUploadToken(lastUploadToken);
393
joshualitt5bf99f12015-03-13 11:47:42 -0700394 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800395
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500396 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700397}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400398
Brian Salomon943ed792017-10-30 09:37:55 -0400399void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500400 if (fNumActivePages <= 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400401 fPrevFlushToken = startTokenForNextFlush;
402 return;
403 }
404
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400405 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400406 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400407 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500408 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400409 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
410 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400411 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400412 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
413 plot->resetFlushesSinceLastUsed();
414 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400415 }
416
417 plotIter.next();
418 }
419 }
420
421 // We only try to compact if the atlas was used in the recently completed flush.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400422 // This is to handle the case where a lot of text or path rendering has occurred but then just
423 // a blinking cursor is drawn.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400424 // TODO: consider if we should also do this if it's been a long time since the last atlas use
425 if (atlasUsedThisFlush) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500426 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500427 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400428
429 // For all plots but the last one, update number of flushes since used, and check to see
430 // if there are any in the first pages that the last page can safely upload to.
431 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400432#ifdef DUMP_ATLAS_DATA
433 if (gDumpAtlasData) {
434 SkDebugf("page %d: ", pageIndex);
435 }
436#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400437 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
438 while (Plot* plot = plotIter.get()) {
439 // Update number of flushes since plot was last used
440 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
441 // to avoid deleting everything when we return to text drawing in the blinking
442 // cursor case
443 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
444 plot->incFlushesSinceLastUsed();
445 }
446
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400447#ifdef DUMP_ATLAS_DATA
448 if (gDumpAtlasData) {
449 SkDebugf("%d ", plot->flushesSinceLastUsed());
450 }
451#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400452 // Count plots we can potentially upload to in all pages except the last one
453 // (the potential compactee).
454 if (plot->flushesSinceLastUsed() > kRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500455 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400456 }
457
458 plotIter.next();
459 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400460#ifdef DUMP_ATLAS_DATA
461 if (gDumpAtlasData) {
462 SkDebugf("\n");
463 }
464#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400465 }
466
Jim Van Verth06f593c2018-02-20 11:30:10 -0500467 // Count recently used plots in the last page and evict any that are no longer in use.
468 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400469 // clear out usage of this page unless we have a large need.
470 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500471 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400472#ifdef DUMP_ATLAS_DATA
473 if (gDumpAtlasData) {
474 SkDebugf("page %d: ", lastPageIndex);
475 }
476#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400477 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400478 // Update number of flushes since plot was last used
479 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
480 plot->incFlushesSinceLastUsed();
481 }
482
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400483#ifdef DUMP_ATLAS_DATA
484 if (gDumpAtlasData) {
485 SkDebugf("%d ", plot->flushesSinceLastUsed());
486 }
487#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400488 // If this plot was used recently
489 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
490 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400491 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400492 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400493 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400494 }
495 plotIter.next();
496 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400497#ifdef DUMP_ATLAS_DATA
498 if (gDumpAtlasData) {
499 SkDebugf("\n");
500 }
501#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500502
503 // If recently used plots in the last page are using less than a quarter of the page, try
504 // to evict them if there's available space in earlier pages. Since we prioritize uploading
505 // to the first pages, this will eventually clear out usage of this page unless we have a
506 // large need.
507 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
508 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
509 while (Plot* plot = plotIter.get()) {
510 // If this plot was used recently
511 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
512 // See if there's room in an earlier page and if so evict.
513 // We need to be somewhat harsh here so that a handful of plots that are
514 // consistently in use don't end up locking the page in memory.
515 if (availablePlots.count() > 0) {
516 this->processEvictionAndResetRects(plot);
517 this->processEvictionAndResetRects(availablePlots.back());
518 availablePlots.pop_back();
519 --usedPlots;
520 }
521 if (!usedPlots || !availablePlots.count()) {
522 break;
523 }
524 }
525 plotIter.next();
526 }
527 }
528
Jim Van Verth106b5c42017-09-26 12:45:29 -0400529 // If none of the plots in the last page have been used recently, delete it.
530 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400531#ifdef DUMP_ATLAS_DATA
532 if (gDumpAtlasData) {
533 SkDebugf("delete %d\n", fNumPages-1);
534 }
535#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500536 this->deactivateLastPage();
Jim Van Verth106b5c42017-09-26 12:45:29 -0400537 }
538 }
539
540 fPrevFlushToken = startTokenForNextFlush;
541}
542
Robert Phillips4bc70112018-03-01 10:24:02 -0500543bool GrDrawOpAtlas::createPages(GrProxyProvider* proxyProvider) {
544 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500545
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400546 GrSurfaceDesc desc;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400547 desc.fWidth = fTextureWidth;
548 desc.fHeight = fTextureHeight;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400549
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400550 int numPlotsX = fTextureWidth/fPlotWidth;
551 int numPlotsY = fTextureHeight/fPlotHeight;
552
Robert Phillips4bc70112018-03-01 10:24:02 -0500553 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500554 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500555 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Greg Daniel47c20e82020-01-21 14:29:57 -0500556 fFormat, desc, swizzle, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
557 GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400558 GrInternalSurfaceFlags::kNone, GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500559 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500560 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400561 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500562 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500563
564 // set up allocated plots
565 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
566
567 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
568 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
569 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
570 uint32_t plotIndex = r * numPlotsX + c;
571 currPlot->reset(new Plot(i, plotIndex, 1, x, y, fPlotWidth, fPlotHeight,
Robert Phillips42dda082019-05-14 13:29:45 -0400572 fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500573
574 // build LRU list
575 fPages[i].fPlotList.addToHead(currPlot->get());
576 ++currPlot;
577 }
578 }
579
580 }
581
582 return true;
583}
584
585
586bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500587 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500588
Greg Daniel9715b6c2019-12-10 15:03:10 -0500589 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500590 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400591 }
592
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400593#ifdef DUMP_ATLAS_DATA
594 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500595 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400596 }
597#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500598
599 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400600 return true;
601}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400602
Robert Phillips4bc70112018-03-01 10:24:02 -0500603
604inline void GrDrawOpAtlas::deactivateLastPage() {
605 SkASSERT(fNumActivePages);
606
607 uint32_t lastPageIndex = fNumActivePages - 1;
608
609 int numPlotsX = fTextureWidth/fPlotWidth;
610 int numPlotsY = fTextureHeight/fPlotHeight;
611
Jim Van Verth106b5c42017-09-26 12:45:29 -0400612 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500613 for (int r = 0; r < numPlotsY; ++r) {
614 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500615 uint32_t plotIndex = r * numPlotsX + c;
616
617 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
618 currPlot->resetRects();
619 currPlot->resetFlushesSinceLastUsed();
620
621 // rebuild the LRU list
622 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
623 SkDEBUGCODE(currPlot->fList = nullptr);
624 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
625 }
626 }
627
628 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500629 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500630 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400631}
Herb Derby15d9ef22018-10-18 13:41:32 -0400632
Jim Van Verthf6206f92018-12-14 08:22:24 -0500633GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
634 static const SkISize kARGBDimensions[] = {
635 {256, 256}, // maxBytes < 2^19
636 {512, 256}, // 2^19 <= maxBytes < 2^20
637 {512, 512}, // 2^20 <= maxBytes < 2^21
638 {1024, 512}, // 2^21 <= maxBytes < 2^22
639 {1024, 1024}, // 2^22 <= maxBytes < 2^23
640 {2048, 1024}, // 2^23 <= maxBytes
641 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400642
Jim Van Verthf6206f92018-12-14 08:22:24 -0500643 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
644 maxBytes >>= 18;
645 // Take the floor of the log to get the index
646 int index = maxBytes > 0
647 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
648 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400649
Jim Van Verthf6206f92018-12-14 08:22:24 -0500650 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
651 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
652 fARGBDimensions.set(SkTMin<int>(kARGBDimensions[index].width(), maxTextureSize),
653 SkTMin<int>(kARGBDimensions[index].height(), maxTextureSize));
654 fMaxTextureSize = SkTMin<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400655}
656
657SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500658 if (kA8_GrMaskFormat == type) {
659 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
660 return { SkTMin<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
661 SkTMin<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
662 } else {
663 return fARGBDimensions;
664 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400665}
666
Jim Van Verthf6206f92018-12-14 08:22:24 -0500667SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
668 if (kA8_GrMaskFormat == type) {
669 SkISize atlasDimensions = this->atlasDimensions(type);
670 // For A8 we want to grow the plots at larger texture sizes to accept more of the
671 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
672 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400673
Jim Van Verth578b0892018-12-20 20:48:55 +0000674 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
675 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500676 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000677 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400678
Jim Van Verthf6206f92018-12-14 08:22:24 -0500679 return { plotWidth, plotHeight };
680 } else {
681 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
682 return { 256, 256 };
683 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400684}
685
Jim Van Verthf6206f92018-12-14 08:22:24 -0500686constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;