blob: 8197040f10fa1cb5445e2c54e98ca1b780b3328e [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
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Robert Phillips03e4c952019-11-26 16:20:22 -050012#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrOnFlushResourceProvider.h"
14#include "src/gpu/GrOpFlushState.h"
15#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrResourceProvider.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040017#include "src/gpu/GrResourceProviderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000019#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070021
Jim Van Verthfb395102020-02-03 10:11:19 -050022#ifdef DUMP_ATLAS_DATA
23static bool gDumpAtlasData = false;
24#endif
25
Robert Phillipse87106d2020-04-09 14:21:33 -040026#ifdef SK_DEBUG
27void GrDrawOpAtlas::AtlasLocator::validate(const GrDrawOpAtlas* drawOpAtlas) const {
28 // Verify that the plotIndex stored in the PlotLocator is consistent with the glyph rectangle
29 int numPlotsX = drawOpAtlas->fTextureWidth / drawOpAtlas->fPlotWidth;
30 int numPlotsY = drawOpAtlas->fTextureHeight / drawOpAtlas->fPlotHeight;
31
32 int plotIndex = this->plotIndex();
33 int plotX = fRect.fLeft / drawOpAtlas->fPlotWidth;
34 int plotY = fRect.fTop / drawOpAtlas->fPlotHeight;
35 SkASSERT(plotIndex == (numPlotsY - plotY - 1) * numPlotsX + (numPlotsX - plotX - 1));
36}
37#endif
38
Robert Phillipscd5099c2018-02-09 09:56:56 -050039// When proxy allocation is deferred until flush time the proxies acting as atlases require
40// special handling. This is because the usage that can be determined from the ops themselves
41// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
42// atlases. Extending the usage interval of any op that uses an atlas to the start of the
43// flush (as is done for proxies that are used for sw-generated masks) also won't work because
44// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
45// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
46// (which calls this method).
47void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050048 for (uint32_t i = 0; i < fNumActivePages; ++i) {
49 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050050 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050051 }
52}
53
Robert Phillips4bc70112018-03-01 10:24:02 -050054std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050055 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040056 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050057 int height, int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -050058 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -050059 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050060 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040061 if (!format.isValid()) {
62 return nullptr;
63 }
64
Herb Derby0ef780b2020-01-24 15:57:11 -050065 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType,
66 width, height, plotWidth, plotHeight,
67 generationCounter,
Robert Phillips4bc70112018-03-01 10:24:02 -050068 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050069 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040070 return nullptr;
71 }
72
Herb Derbya90ed952020-01-28 15:55:58 -050073 if (evictor != nullptr) {
74 atlas->fEvictionCallbacks.emplace_back(evictor);
75 }
Robert Phillips256c37b2017-03-01 14:32:46 -050076 return atlas;
77}
78
Jim Van Verthfb395102020-02-03 10:11:19 -050079std::tuple<uint16_t, uint16_t, int> GrDrawOpAtlas::UnpackIndexFromTexCoords(uint16_t u,
80 uint16_t v) {
81 int pageIndex = 0;
82 if (u & 0x1) {
83 pageIndex |= 0x2;
84 }
85 if (v & 0x1) {
86 pageIndex |= 0x1;
87 }
88 return std::make_tuple(u >> 1, v >> 1, pageIndex);
89}
Robert Phillips256c37b2017-03-01 14:32:46 -050090
joshualitt5df175e2015-11-18 13:37:54 -080091////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -050092GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
93 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040094 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
95 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040096 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040097 , fPageIndex(pageIndex)
98 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -050099 , fGenerationCounter(generationCounter)
100 , fGenID(fGenerationCounter->next())
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400101 , fPlotLocator(fPageIndex, fPlotIndex, fGenID)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500102 , fData(nullptr)
103 , fWidth(width)
104 , fHeight(height)
105 , fX(offX)
106 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -0500107 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500108 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400109 , fColorType(colorType)
110 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800111#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500112 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800113#endif
114{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500115 // We expect the allocated dimensions to be a multiple of 4 bytes
116 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
117 // The padding for faster uploads only works for 1, 2 and 4 byte texels
118 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800119 fDirtyRect.setEmpty();
120}
joshualitt5bf99f12015-03-13 11:47:42 -0700121
Brian Salomon2ee084e2016-12-16 18:59:19 -0500122GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700123 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800124}
joshualitt5bf99f12015-03-13 11:47:42 -0700125
Robert Phillips6d3bc292020-04-06 10:29:28 -0400126bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, GrIRect16* rect) {
joshualitt5df175e2015-11-18 13:37:54 -0800127 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700128
Robert Phillips6d3bc292020-04-06 10:29:28 -0400129 SkIPoint16 loc;
130 if (!fRectanizer.addRect(width, height, &loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800131 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700132 }
joshualitt5bf99f12015-03-13 11:47:42 -0700133
Robert Phillips6d3bc292020-04-06 10:29:28 -0400134 *rect = GrIRect16::MakeXYWH(loc.fX, loc.fY, width, height);
135
jvanverthc3d706f2016-04-20 10:33:27 -0700136 if (!fData) {
137 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
138 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800139 }
140 size_t rowBytes = width * fBytesPerPixel;
141 const unsigned char* imagePtr = (const unsigned char*)image;
142 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700143 unsigned char* dataPtr = fData;
Robert Phillips6d3bc292020-04-06 10:29:28 -0400144 dataPtr += fBytesPerPixel * fWidth * rect->fTop;
145 dataPtr += fBytesPerPixel * rect->fLeft;
Brian Osmancce3e582016-10-14 11:42:20 -0400146 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500147 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400148 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400149 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400150 dataPtr += fBytesPerPixel * fWidth;
151 imagePtr += rowBytes;
152 }
153 } else {
154 for (int i = 0; i < height; ++i) {
155 memcpy(dataPtr, imagePtr, rowBytes);
156 dataPtr += fBytesPerPixel * fWidth;
157 imagePtr += rowBytes;
158 }
joshualitt5bf99f12015-03-13 11:47:42 -0700159 }
160
Robert Phillips6d3bc292020-04-06 10:29:28 -0400161 fDirtyRect.join({rect->fLeft, rect->fTop, rect->fRight, rect->fBottom});
robertphillips2b0536f2015-11-06 14:10:42 -0800162
Robert Phillips6d3bc292020-04-06 10:29:28 -0400163 rect->offset(fOffset.fX, fOffset.fY);
joshualitt5df175e2015-11-18 13:37:54 -0800164 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();
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400196 fPlotLocator = PlotLocator(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
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400211GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
212 GrColorType colorType, int width, int height,
213 int plotWidth, int plotHeight, GenerationCounter* generationCounter,
214 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) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400239 for (EvictionCallback* 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,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400247 AtlasLocator* atlasLocator, Plot* plot) {
248 int pageIdx = plot->pageIndex();
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 }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400267 atlasLocator->fPlotLocator = plot->plotLocator();
Robert Phillipse87106d2020-04-09 14:21:33 -0400268 SkDEBUGCODE(atlasLocator->validate(this);)
Robert Phillips256c37b2017-03-01 14:32:46 -0500269 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700270}
271
Robert Phillips6d3bc292020-04-06 10:29:28 -0400272bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400273 GrDeferredUploadTarget* target, int width, int height,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400274 const void* image, AtlasLocator* atlasLocator) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500275 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500276
277 // look through all allocated plots for one we can share, in Most Recently Refed order
278 PlotList::Iter plotIter;
279 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
280
281 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500282 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500283
Robert Phillips6d3bc292020-04-06 10:29:28 -0400284 if (plot->addSubImage(width, height, image, &atlasLocator->fRect)) {
285 return this->updatePlot(target, atlasLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500286 }
287 }
288
289 return false;
290}
291
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400292// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
293//
294// This value is somewhat arbitrary -- the idea is to keep it low enough that
295// a page with unused plots will get removed reasonably quickly, but allow it
296// to hang around for a bit in case it's needed. The assumption is that flushes
297// are rare; i.e., we are not continually refreshing the frame.
Jonathan Backer40c683a2020-05-04 15:00:02 -0400298static constexpr auto kPlotRecentlyUsedCount = 32;
299static constexpr auto kAtlasRecentlyUsedCount = 128;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400300
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500301GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500302 GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400303 int width, int height, const void* image,
304 AtlasLocator* atlasLocator) {
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) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400315 if (this->uploadToPage(caps, pageIdx, target, width, height, image, atlasLocator)) {
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 Phillips6d3bc292020-04-06 10:29:28 -0400333 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image,
334 &atlasLocator->fRect);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500335 SkASSERT(verify);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400336 if (!this->updatePlot(target, atlasLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500337 return ErrorCode::kError;
338 }
339 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400340 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500341 }
342 } else {
343 // If we haven't activated all the available pages, try to create a new one and add to it
344 if (!this->activateNewPage(resourceProvider)) {
345 return ErrorCode::kError;
346 }
347
Robert Phillips6d3bc292020-04-06 10:29:28 -0400348 if (this->uploadToPage(caps, fNumActivePages-1, target, width, height, image,
349 atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500350 return ErrorCode::kSucceeded;
351 } else {
352 // If we fail to upload to a newly activated page then something has gone terribly
353 // wrong - return an error
354 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400355 }
356 }
357
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500358 if (!fNumActivePages) {
359 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700360 }
361
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400362 // Try to find a plot that we can perform an inline upload to.
363 // We prioritize this upload in reverse order of pages to counterbalance the order above.
364 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500365 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400366 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500367 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400368 plot = currentPlot;
369 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500370 }
joshualitt5bf99f12015-03-13 11:47:42 -0700371 }
372
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400373 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
374 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
375 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
376 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500377 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400378 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500379 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700380 }
381
Herb Derby4d721712020-01-24 14:31:16 -0500382 this->processEviction(plot->plotLocator());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400383 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400384 fPages[pageIdx].fPlotList.remove(plot);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400385 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
robertphillips2b0536f2015-11-06 14:10:42 -0800386 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700387
Jim Van Vertha950b632017-09-12 11:54:11 -0400388 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500389 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400390 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, &atlasLocator->fRect);
joshualitt5bf99f12015-03-13 11:47:42 -0700391 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800392
robertphillips1f0e3502015-11-10 10:19:50 -0800393 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400394 // one it displaced most likely was uploaded ASAP.
Robert Phillipse87106d2020-04-09 14:21:33 -0400395 // With c++14 we could move sk_sp into lambda to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500396 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500397
Greg Daniel9715b6c2019-12-10 15:03:10 -0500398 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
399 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700400
Brian Salomon943ed792017-10-30 09:37:55 -0400401 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
402 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
403 plotsp->uploadToTexture(writePixels, proxy);
404 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500405 newPlot->setLastUploadToken(lastUploadToken);
406
Robert Phillips6d3bc292020-04-06 10:29:28 -0400407 atlasLocator->fPlotLocator = newPlot->plotLocator();
Robert Phillipse87106d2020-04-09 14:21:33 -0400408 SkDEBUGCODE(atlasLocator->validate(this);)
robertphillips2b0536f2015-11-06 14:10:42 -0800409
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500410 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700411}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400412
Brian Salomon943ed792017-10-30 09:37:55 -0400413void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400414 if (fNumActivePages < 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400415 fPrevFlushToken = startTokenForNextFlush;
416 return;
417 }
418
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400419 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400420 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400421 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500422 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400423 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
424 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400425 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400426 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
427 plot->resetFlushesSinceLastUsed();
428 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400429 }
430
431 plotIter.next();
432 }
433 }
434
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400435 if (atlasUsedThisFlush) {
436 fFlushesSinceLastUse = 0;
437 } else {
438 ++fFlushesSinceLastUse;
439 }
440
441 // We only try to compact if the atlas was used in the recently completed flush or
442 // hasn't been used in a long time.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400443 // This is to handle the case where a lot of text or path rendering has occurred but then just
444 // a blinking cursor is drawn.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400445 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500446 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500447 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400448
449 // For all plots but the last one, update number of flushes since used, and check to see
450 // if there are any in the first pages that the last page can safely upload to.
451 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400452#ifdef DUMP_ATLAS_DATA
453 if (gDumpAtlasData) {
454 SkDebugf("page %d: ", pageIndex);
455 }
456#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400457 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
458 while (Plot* plot = plotIter.get()) {
459 // Update number of flushes since plot was last used
460 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
461 // to avoid deleting everything when we return to text drawing in the blinking
462 // cursor case
463 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
464 plot->incFlushesSinceLastUsed();
465 }
466
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400467#ifdef DUMP_ATLAS_DATA
468 if (gDumpAtlasData) {
469 SkDebugf("%d ", plot->flushesSinceLastUsed());
470 }
471#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400472 // Count plots we can potentially upload to in all pages except the last one
473 // (the potential compactee).
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400474 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500475 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400476 }
477
478 plotIter.next();
479 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400480#ifdef DUMP_ATLAS_DATA
481 if (gDumpAtlasData) {
482 SkDebugf("\n");
483 }
484#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400485 }
486
Jim Van Verth06f593c2018-02-20 11:30:10 -0500487 // Count recently used plots in the last page and evict any that are no longer in use.
488 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400489 // clear out usage of this page unless we have a large need.
490 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500491 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400492#ifdef DUMP_ATLAS_DATA
493 if (gDumpAtlasData) {
494 SkDebugf("page %d: ", lastPageIndex);
495 }
496#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400497 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400498 // Update number of flushes since plot was last used
499 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
500 plot->incFlushesSinceLastUsed();
501 }
502
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400503#ifdef DUMP_ATLAS_DATA
504 if (gDumpAtlasData) {
505 SkDebugf("%d ", plot->flushesSinceLastUsed());
506 }
507#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400508 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400509 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400510 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400511 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400512 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400513 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400514 }
515 plotIter.next();
516 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400517#ifdef DUMP_ATLAS_DATA
518 if (gDumpAtlasData) {
519 SkDebugf("\n");
520 }
521#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500522
523 // If recently used plots in the last page are using less than a quarter of the page, try
524 // to evict them if there's available space in earlier pages. Since we prioritize uploading
525 // to the first pages, this will eventually clear out usage of this page unless we have a
526 // large need.
527 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
528 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
529 while (Plot* plot = plotIter.get()) {
530 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400531 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth06f593c2018-02-20 11:30:10 -0500532 // See if there's room in an earlier page and if so evict.
533 // We need to be somewhat harsh here so that a handful of plots that are
534 // consistently in use don't end up locking the page in memory.
535 if (availablePlots.count() > 0) {
536 this->processEvictionAndResetRects(plot);
537 this->processEvictionAndResetRects(availablePlots.back());
538 availablePlots.pop_back();
539 --usedPlots;
540 }
541 if (!usedPlots || !availablePlots.count()) {
542 break;
543 }
544 }
545 plotIter.next();
546 }
547 }
548
Jim Van Verth106b5c42017-09-26 12:45:29 -0400549 // If none of the plots in the last page have been used recently, delete it.
Jim Van Verth26651882020-03-18 15:30:07 +0000550 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400551#ifdef DUMP_ATLAS_DATA
552 if (gDumpAtlasData) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400553 SkDebugf("delete %d\n", fNumActivePages-1);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400554 }
555#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500556 this->deactivateLastPage();
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400557 fFlushesSinceLastUse = 0;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400558 }
559 }
560
561 fPrevFlushToken = startTokenForNextFlush;
562}
563
Herb Derby0ef780b2020-01-24 15:57:11 -0500564bool GrDrawOpAtlas::createPages(
565 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500566 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500567
Brian Salomona56a7462020-02-07 14:17:25 -0500568 SkISize dims = {fTextureWidth, fTextureHeight};
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400569
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400570 int numPlotsX = fTextureWidth/fPlotWidth;
571 int numPlotsY = fTextureHeight/fPlotHeight;
572
Robert Phillips4bc70112018-03-01 10:24:02 -0500573 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500574 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500575 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400576 fFormat, dims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400577 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
578 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500579 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500580 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400581 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500582 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500583
584 // set up allocated plots
John Stilesfbd050b2020-08-03 13:21:46 -0400585 fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY);
Robert Phillips4bc70112018-03-01 10:24:02 -0500586
587 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
588 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
589 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
590 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500591 currPlot->reset(new Plot(
592 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500593
594 // build LRU list
595 fPages[i].fPlotList.addToHead(currPlot->get());
596 ++currPlot;
597 }
598 }
599
600 }
601
602 return true;
603}
604
605
606bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500607 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500608
Greg Daniel9715b6c2019-12-10 15:03:10 -0500609 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500610 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400611 }
612
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400613#ifdef DUMP_ATLAS_DATA
614 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500615 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400616 }
617#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500618
619 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400620 return true;
621}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400622
Robert Phillips4bc70112018-03-01 10:24:02 -0500623
624inline void GrDrawOpAtlas::deactivateLastPage() {
625 SkASSERT(fNumActivePages);
626
627 uint32_t lastPageIndex = fNumActivePages - 1;
628
629 int numPlotsX = fTextureWidth/fPlotWidth;
630 int numPlotsY = fTextureHeight/fPlotHeight;
631
Jim Van Verth106b5c42017-09-26 12:45:29 -0400632 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500633 for (int r = 0; r < numPlotsY; ++r) {
634 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500635 uint32_t plotIndex = r * numPlotsX + c;
636
637 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
638 currPlot->resetRects();
639 currPlot->resetFlushesSinceLastUsed();
640
641 // rebuild the LRU list
642 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
643 SkDEBUGCODE(currPlot->fList = nullptr);
644 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
645 }
646 }
647
648 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500649 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500650 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400651}
Herb Derby15d9ef22018-10-18 13:41:32 -0400652
Jim Van Verthf6206f92018-12-14 08:22:24 -0500653GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
654 static const SkISize kARGBDimensions[] = {
655 {256, 256}, // maxBytes < 2^19
656 {512, 256}, // 2^19 <= maxBytes < 2^20
657 {512, 512}, // 2^20 <= maxBytes < 2^21
658 {1024, 512}, // 2^21 <= maxBytes < 2^22
659 {1024, 1024}, // 2^22 <= maxBytes < 2^23
660 {2048, 1024}, // 2^23 <= maxBytes
661 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400662
Jim Van Verthf6206f92018-12-14 08:22:24 -0500663 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
664 maxBytes >>= 18;
665 // Take the floor of the log to get the index
666 int index = maxBytes > 0
667 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
668 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400669
Jim Van Verthf6206f92018-12-14 08:22:24 -0500670 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
671 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500672 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
673 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
674 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400675}
676
677SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500678 if (kA8_GrMaskFormat == type) {
679 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500680 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
681 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500682 } else {
683 return fARGBDimensions;
684 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400685}
686
Jim Van Verthf6206f92018-12-14 08:22:24 -0500687SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
688 if (kA8_GrMaskFormat == type) {
689 SkISize atlasDimensions = this->atlasDimensions(type);
690 // For A8 we want to grow the plots at larger texture sizes to accept more of the
691 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
692 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400693
Jim Van Verth578b0892018-12-20 20:48:55 +0000694 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
695 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500696 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000697 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400698
Jim Van Verthf6206f92018-12-14 08:22:24 -0500699 return { plotWidth, plotHeight };
700 } else {
701 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
702 return { 256, 256 };
703 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400704}
705
Jim Van Verthf6206f92018-12-14 08:22:24 -0500706constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;