blob: 262c320e54fe4b1d392c379ed505acd8122b1840 [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"
Robert Phillips03e4c952019-11-26 16:20:22 -050011#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040013#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrOnFlushResourceProvider.h"
15#include "src/gpu/GrOpFlushState.h"
16#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrResourceProvider.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040018#include "src/gpu/GrResourceProviderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000020#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070022
Jim Van Verthfb395102020-02-03 10:11:19 -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,
Herb Derby0ef780b2020-01-24 15:57:11 -050046 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -050047 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050048 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040049 if (!format.isValid()) {
50 return nullptr;
51 }
52
Herb Derby0ef780b2020-01-24 15:57:11 -050053 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType,
54 width, height, plotWidth, plotHeight,
55 generationCounter,
Robert Phillips4bc70112018-03-01 10:24:02 -050056 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050057 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040058 return nullptr;
59 }
60
Herb Derbya90ed952020-01-28 15:55:58 -050061 if (evictor != nullptr) {
62 atlas->fEvictionCallbacks.emplace_back(evictor);
63 }
Robert Phillips256c37b2017-03-01 14:32:46 -050064 return atlas;
65}
66
Jim Van Verthfb395102020-02-03 10:11:19 -050067// The two bits that make up the texture index are packed into the lower bits of the u and v
68// coordinate respectively.
69std::pair<uint16_t, uint16_t> GrDrawOpAtlas::PackIndexInTexCoords(uint16_t u, uint16_t v,
70 int pageIndex) {
71 SkASSERT(pageIndex >= 0 && pageIndex < 4);
72 uint16_t uBit = (pageIndex >> 1u) & 0x1u;
73 uint16_t vBit = pageIndex & 0x1u;
74 u <<= 1u;
75 u |= uBit;
76 v <<= 1u;
77 v |= vBit;
78 return std::make_pair(u, v);
79}
80
81std::tuple<uint16_t, uint16_t, int> GrDrawOpAtlas::UnpackIndexFromTexCoords(uint16_t u,
82 uint16_t v) {
83 int pageIndex = 0;
84 if (u & 0x1) {
85 pageIndex |= 0x2;
86 }
87 if (v & 0x1) {
88 pageIndex |= 0x1;
89 }
90 return std::make_tuple(u >> 1, v >> 1, pageIndex);
91}
Robert Phillips256c37b2017-03-01 14:32:46 -050092
joshualitt5df175e2015-11-18 13:37:54 -080093////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -050094GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
95 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040096 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
97 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040098 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040099 , fPageIndex(pageIndex)
100 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -0500101 , fGenerationCounter(generationCounter)
102 , fGenID(fGenerationCounter->next())
Herb Derby4d721712020-01-24 14:31:16 -0500103 , fPlotLocator(CreatePlotLocator(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -0500104 , fData(nullptr)
105 , fWidth(width)
106 , fHeight(height)
107 , fX(offX)
108 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -0500109 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500110 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400111 , fColorType(colorType)
112 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800113#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500114 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800115#endif
116{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500117 // We expect the allocated dimensions to be a multiple of 4 bytes
118 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
119 // The padding for faster uploads only works for 1, 2 and 4 byte texels
120 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800121 fDirtyRect.setEmpty();
122}
joshualitt5bf99f12015-03-13 11:47:42 -0700123
Brian Salomon2ee084e2016-12-16 18:59:19 -0500124GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700125 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800126}
joshualitt5bf99f12015-03-13 11:47:42 -0700127
Brian Salomon2ee084e2016-12-16 18:59:19 -0500128bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -0800129 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700130
Herb Derby73c75872020-01-22 18:09:16 -0500131 if (!fRectanizer.addRect(width, height, loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800132 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700133 }
joshualitt5bf99f12015-03-13 11:47:42 -0700134
jvanverthc3d706f2016-04-20 10:33:27 -0700135 if (!fData) {
136 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
137 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800138 }
139 size_t rowBytes = width * fBytesPerPixel;
140 const unsigned char* imagePtr = (const unsigned char*)image;
141 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700142 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800143 dataPtr += fBytesPerPixel * fWidth * loc->fY;
144 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400145 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500146 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400147 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400148 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400149 dataPtr += fBytesPerPixel * fWidth;
150 imagePtr += rowBytes;
151 }
152 } else {
153 for (int i = 0; i < height; ++i) {
154 memcpy(dataPtr, imagePtr, rowBytes);
155 dataPtr += fBytesPerPixel * fWidth;
156 imagePtr += rowBytes;
157 }
joshualitt5bf99f12015-03-13 11:47:42 -0700158 }
159
Mike Reed92b33352019-08-24 19:39:13 -0400160 fDirtyRect.join({loc->fX, loc->fY, loc->fX + width, loc->fY + height});
robertphillips2b0536f2015-11-06 14:10:42 -0800161
joshualitt5df175e2015-11-18 13:37:54 -0800162 loc->fX += fOffset.fX;
163 loc->fY += fOffset.fY;
164 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700165
joshualitt5df175e2015-11-18 13:37:54 -0800166 return true;
167}
joshualitt5bf99f12015-03-13 11:47:42 -0700168
Brian Salomon943ed792017-10-30 09:37:55 -0400169void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400170 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800171 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400172 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400173 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800174 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700175 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500176 // Clamp to 4-byte aligned boundaries
177 unsigned int clearBits = 0x3 / fBytesPerPixel;
178 fDirtyRect.fLeft &= ~clearBits;
179 fDirtyRect.fRight += clearBits;
180 fDirtyRect.fRight &= ~clearBits;
181 SkASSERT(fDirtyRect.fRight <= fWidth);
182 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700183 dataPtr += rowBytes * fDirtyRect.fTop;
184 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400185
Robert Phillipsacaa6072017-07-28 10:54:53 -0400186 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400187 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800188 fDirtyRect.setEmpty();
189 SkDEBUGCODE(fDirty = false;)
190}
191
Brian Salomon2ee084e2016-12-16 18:59:19 -0500192void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500193 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700194
Herb Derby0ef780b2020-01-24 15:57:11 -0500195 fGenID = fGenerationCounter->next();
Herb Derby4d721712020-01-24 14:31:16 -0500196 fPlotLocator = CreatePlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400197 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
198 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800199
200 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700201 if (fData) {
202 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700203 }
204
joshualitt5df175e2015-11-18 13:37:54 -0800205 fDirtyRect.setEmpty();
206 SkDEBUGCODE(fDirty = false;)
207}
joshualitt5bf99f12015-03-13 11:47:42 -0700208
joshualitt5bf99f12015-03-13 11:47:42 -0700209///////////////////////////////////////////////////////////////////////////////
210
Herb Derby0ef780b2020-01-24 15:57:11 -0500211GrDrawOpAtlas::GrDrawOpAtlas(
212 GrProxyProvider* proxyProvider, const GrBackendFormat& format,
213 GrColorType colorType, int width, int height, int plotWidth, int plotHeight,
214 GenerationCounter* generationCounter, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500215 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400216 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400217 , fTextureWidth(width)
218 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500219 , fPlotWidth(plotWidth)
220 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500221 , fGenerationCounter(generationCounter)
222 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400223 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400224 , fFlushesSinceLastUse(0)
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500225 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500226 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500227 int numPlotsX = width/plotWidth;
228 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400229 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400230 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
231 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800232
Jim Van Verth06f593c2018-02-20 11:30:10 -0500233 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700234
Herb Derby0ef780b2020-01-24 15:57:11 -0500235 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700236}
237
Herb Derby4d721712020-01-24 14:31:16 -0500238inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
Herb Derby1a496c52020-01-22 17:26:56 -0500239 for (auto evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500240 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700241 }
Herb Derby1a496c52020-01-22 17:26:56 -0500242
Herb Derby0ef780b2020-01-24 15:57:11 -0500243 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700244}
245
Herb Derby4d721712020-01-24 14:31:16 -0500246inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
247 PlotLocator* plotLocator, Plot* plot) {
248 int pageIdx = GetPageIndexFromID(plot->plotLocator());
Jim Van Vertha950b632017-09-12 11:54:11 -0400249 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700250
251 // If our most recent upload has already occurred then we have to insert a new
252 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
253 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500254 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700255 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500256 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500257
Greg Daniel9715b6c2019-12-10 15:03:10 -0500258 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
259 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500260
Brian Salomon29b60c92017-10-31 14:42:10 -0400261 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400262 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
263 plotsp->uploadToTexture(writePixels, proxy);
264 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500265 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700266 }
Herb Derby4d721712020-01-24 14:31:16 -0500267 *plotLocator = plot->plotLocator();
Robert Phillips256c37b2017-03-01 14:32:46 -0500268 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700269}
270
Herb Derby4d721712020-01-24 14:31:16 -0500271bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx, PlotLocator* plotLocator,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400272 GrDeferredUploadTarget* target, int width, int height,
273 const void* image, SkIPoint16* loc) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500274 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500275
276 // look through all allocated plots for one we can share, in Most Recently Refed order
277 PlotList::Iter plotIter;
278 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
279
280 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500281 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500282
283 if (plot->addSubImage(width, height, image, loc)) {
Herb Derby4d721712020-01-24 14:31:16 -0500284 return this->updatePlot(target, plotLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500285 }
286 }
287
288 return false;
289}
290
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400291// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
292//
293// This value is somewhat arbitrary -- the idea is to keep it low enough that
294// a page with unused plots will get removed reasonably quickly, but allow it
295// to hang around for a bit in case it's needed. The assumption is that flushes
296// are rare; i.e., we are not continually refreshing the frame.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400297static constexpr auto kPlotRecentlyUsedCount = 256;
298static constexpr auto kAtlasRecentlyUsedCount = 1024;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400299
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500300GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500301 PlotLocator* plotLocator,
302 GrDeferredUploadTarget* target,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500303 int width, int height,
304 const void* image, SkIPoint16* loc) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700305 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500306 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700307 }
joshualitt5bf99f12015-03-13 11:47:42 -0700308
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400309 const GrCaps& caps = *resourceProvider->caps();
310
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400311 // Look through each page to see if we can upload without having to flush
312 // We prioritize this upload to the first pages, not the most recently used, to make it easier
313 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500314 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Herb Derby4d721712020-01-24 14:31:16 -0500315 if (this->uploadToPage(caps, pageIdx, plotLocator, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500316 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400317 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400318 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400319
Jim Van Verth712fe732017-09-25 16:53:49 -0400320 // 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 -0400321 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
322 // We wait until we've grown to the full number of pages to begin evicting already flushed
323 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400324 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500325 if (fNumActivePages == this->maxPages()) {
326 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
327 Plot* plot = fPages[pageIdx].fPlotList.tail();
328 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500329 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500330 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500331 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
332 plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500333 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
334 SkASSERT(verify);
Herb Derby4d721712020-01-24 14:31:16 -0500335 if (!this->updatePlot(target, plotLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500336 return ErrorCode::kError;
337 }
338 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400339 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500340 }
341 } else {
342 // If we haven't activated all the available pages, try to create a new one and add to it
343 if (!this->activateNewPage(resourceProvider)) {
344 return ErrorCode::kError;
345 }
346
Herb Derby4d721712020-01-24 14:31:16 -0500347 if (this->uploadToPage(
348 caps, fNumActivePages-1, plotLocator, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500349 return ErrorCode::kSucceeded;
350 } else {
351 // If we fail to upload to a newly activated page then something has gone terribly
352 // wrong - return an error
353 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400354 }
355 }
356
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500357 if (!fNumActivePages) {
358 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700359 }
360
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400361 // Try to find a plot that we can perform an inline upload to.
362 // We prioritize this upload in reverse order of pages to counterbalance the order above.
363 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500364 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400365 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500366 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400367 plot = currentPlot;
368 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500369 }
joshualitt5bf99f12015-03-13 11:47:42 -0700370 }
371
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400372 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
373 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
374 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
375 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500376 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400377 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500378 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700379 }
380
Herb Derby4d721712020-01-24 14:31:16 -0500381 this->processEviction(plot->plotLocator());
382 int pageIdx = GetPageIndexFromID(plot->plotLocator());
Jim Van Vertha950b632017-09-12 11:54:11 -0400383 fPages[pageIdx].fPlotList.remove(plot);
384 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800385 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700386
Jim Van Vertha950b632017-09-12 11:54:11 -0400387 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500388 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800389 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700390 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800391
robertphillips1f0e3502015-11-10 10:19:50 -0800392 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400393 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500394 // With c+14 we could move sk_sp into lambda to only ref once.
395 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500396
Greg Daniel9715b6c2019-12-10 15:03:10 -0500397 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
398 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700399
Brian Salomon943ed792017-10-30 09:37:55 -0400400 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
401 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
402 plotsp->uploadToTexture(writePixels, proxy);
403 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500404 newPlot->setLastUploadToken(lastUploadToken);
405
Herb Derby4d721712020-01-24 14:31:16 -0500406 *plotLocator = newPlot->plotLocator();
robertphillips2b0536f2015-11-06 14:10:42 -0800407
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500408 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700409}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400410
Brian Salomon943ed792017-10-30 09:37:55 -0400411void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400412 if (fNumActivePages < 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400413 fPrevFlushToken = startTokenForNextFlush;
414 return;
415 }
416
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400417 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400418 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400419 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500420 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400421 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
422 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400423 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400424 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
425 plot->resetFlushesSinceLastUsed();
426 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400427 }
428
429 plotIter.next();
430 }
431 }
432
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400433 if (atlasUsedThisFlush) {
434 fFlushesSinceLastUse = 0;
435 } else {
436 ++fFlushesSinceLastUse;
437 }
438
439 // We only try to compact if the atlas was used in the recently completed flush or
440 // hasn't been used in a long time.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400441 // This is to handle the case where a lot of text or path rendering has occurred but then just
442 // a blinking cursor is drawn.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400443 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500444 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500445 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400446
447 // For all plots but the last one, update number of flushes since used, and check to see
448 // if there are any in the first pages that the last page can safely upload to.
449 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400450#ifdef DUMP_ATLAS_DATA
451 if (gDumpAtlasData) {
452 SkDebugf("page %d: ", pageIndex);
453 }
454#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400455 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
456 while (Plot* plot = plotIter.get()) {
457 // Update number of flushes since plot was last used
458 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
459 // to avoid deleting everything when we return to text drawing in the blinking
460 // cursor case
461 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
462 plot->incFlushesSinceLastUsed();
463 }
464
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400465#ifdef DUMP_ATLAS_DATA
466 if (gDumpAtlasData) {
467 SkDebugf("%d ", plot->flushesSinceLastUsed());
468 }
469#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400470 // Count plots we can potentially upload to in all pages except the last one
471 // (the potential compactee).
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400472 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500473 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400474 }
475
476 plotIter.next();
477 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400478#ifdef DUMP_ATLAS_DATA
479 if (gDumpAtlasData) {
480 SkDebugf("\n");
481 }
482#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400483 }
484
Jim Van Verth06f593c2018-02-20 11:30:10 -0500485 // Count recently used plots in the last page and evict any that are no longer in use.
486 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400487 // clear out usage of this page unless we have a large need.
488 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500489 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400490#ifdef DUMP_ATLAS_DATA
491 if (gDumpAtlasData) {
492 SkDebugf("page %d: ", lastPageIndex);
493 }
494#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400495 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400496 // Update number of flushes since plot was last used
497 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
498 plot->incFlushesSinceLastUsed();
499 }
500
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400501#ifdef DUMP_ATLAS_DATA
502 if (gDumpAtlasData) {
503 SkDebugf("%d ", plot->flushesSinceLastUsed());
504 }
505#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400506 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400507 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400508 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400509 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400510 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400511 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400512 }
513 plotIter.next();
514 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400515#ifdef DUMP_ATLAS_DATA
516 if (gDumpAtlasData) {
517 SkDebugf("\n");
518 }
519#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500520
521 // If recently used plots in the last page are using less than a quarter of the page, try
522 // to evict them if there's available space in earlier pages. Since we prioritize uploading
523 // to the first pages, this will eventually clear out usage of this page unless we have a
524 // large need.
525 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
526 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
527 while (Plot* plot = plotIter.get()) {
528 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400529 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth06f593c2018-02-20 11:30:10 -0500530 // See if there's room in an earlier page and if so evict.
531 // We need to be somewhat harsh here so that a handful of plots that are
532 // consistently in use don't end up locking the page in memory.
533 if (availablePlots.count() > 0) {
534 this->processEvictionAndResetRects(plot);
535 this->processEvictionAndResetRects(availablePlots.back());
536 availablePlots.pop_back();
537 --usedPlots;
538 }
539 if (!usedPlots || !availablePlots.count()) {
540 break;
541 }
542 }
543 plotIter.next();
544 }
545 }
546
Jim Van Verth106b5c42017-09-26 12:45:29 -0400547 // If none of the plots in the last page have been used recently, delete it.
Jim Van Verth26651882020-03-18 15:30:07 +0000548 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400549#ifdef DUMP_ATLAS_DATA
550 if (gDumpAtlasData) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400551 SkDebugf("delete %d\n", fNumActivePages-1);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400552 }
553#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500554 this->deactivateLastPage();
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400555 fFlushesSinceLastUse = 0;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400556 }
557 }
558
559 fPrevFlushToken = startTokenForNextFlush;
560}
561
Herb Derby0ef780b2020-01-24 15:57:11 -0500562bool GrDrawOpAtlas::createPages(
563 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500564 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500565
Brian Salomona56a7462020-02-07 14:17:25 -0500566 SkISize dims = {fTextureWidth, fTextureHeight};
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400567
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400568 int numPlotsX = fTextureWidth/fPlotWidth;
569 int numPlotsY = fTextureHeight/fPlotHeight;
570
Robert Phillips4bc70112018-03-01 10:24:02 -0500571 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500572 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500573 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400574 fFormat, dims, GrRenderable::kNo, 1, GrMipMapped::kNo, SkBackingFit::kExact,
575 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
576 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500577 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500578 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400579 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500580 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500581
582 // set up allocated plots
583 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
584
585 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
586 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
587 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
588 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500589 currPlot->reset(new Plot(
590 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500591
592 // build LRU list
593 fPages[i].fPlotList.addToHead(currPlot->get());
594 ++currPlot;
595 }
596 }
597
598 }
599
600 return true;
601}
602
603
604bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500605 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500606
Greg Daniel9715b6c2019-12-10 15:03:10 -0500607 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500608 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400609 }
610
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400611#ifdef DUMP_ATLAS_DATA
612 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500613 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400614 }
615#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500616
617 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400618 return true;
619}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400620
Robert Phillips4bc70112018-03-01 10:24:02 -0500621
622inline void GrDrawOpAtlas::deactivateLastPage() {
623 SkASSERT(fNumActivePages);
624
625 uint32_t lastPageIndex = fNumActivePages - 1;
626
627 int numPlotsX = fTextureWidth/fPlotWidth;
628 int numPlotsY = fTextureHeight/fPlotHeight;
629
Jim Van Verth106b5c42017-09-26 12:45:29 -0400630 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500631 for (int r = 0; r < numPlotsY; ++r) {
632 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500633 uint32_t plotIndex = r * numPlotsX + c;
634
635 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
636 currPlot->resetRects();
637 currPlot->resetFlushesSinceLastUsed();
638
639 // rebuild the LRU list
640 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
641 SkDEBUGCODE(currPlot->fList = nullptr);
642 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
643 }
644 }
645
646 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500647 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500648 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400649}
Herb Derby15d9ef22018-10-18 13:41:32 -0400650
Jim Van Verthf6206f92018-12-14 08:22:24 -0500651GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
652 static const SkISize kARGBDimensions[] = {
653 {256, 256}, // maxBytes < 2^19
654 {512, 256}, // 2^19 <= maxBytes < 2^20
655 {512, 512}, // 2^20 <= maxBytes < 2^21
656 {1024, 512}, // 2^21 <= maxBytes < 2^22
657 {1024, 1024}, // 2^22 <= maxBytes < 2^23
658 {2048, 1024}, // 2^23 <= maxBytes
659 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400660
Jim Van Verthf6206f92018-12-14 08:22:24 -0500661 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
662 maxBytes >>= 18;
663 // Take the floor of the log to get the index
664 int index = maxBytes > 0
665 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
666 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400667
Jim Van Verthf6206f92018-12-14 08:22:24 -0500668 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
669 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500670 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
671 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
672 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400673}
674
675SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500676 if (kA8_GrMaskFormat == type) {
677 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500678 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
679 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500680 } else {
681 return fARGBDimensions;
682 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400683}
684
Jim Van Verthf6206f92018-12-14 08:22:24 -0500685SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
686 if (kA8_GrMaskFormat == type) {
687 SkISize atlasDimensions = this->atlasDimensions(type);
688 // For A8 we want to grow the plots at larger texture sizes to accept more of the
689 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
690 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400691
Jim Van Verth578b0892018-12-20 20:48:55 +0000692 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
693 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500694 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000695 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400696
Jim Van Verthf6206f92018-12-14 08:22:24 -0500697 return { plotWidth, plotHeight };
698 } else {
699 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
700 return { 256, 256 };
701 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400702}
703
Jim Van Verthf6206f92018-12-14 08:22:24 -0500704constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;