blob: cb31a4a01e44ed89ae5ccf1a7326d6e7c2d5f66f [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 Phillips6d3bc292020-04-06 10:29:28 -040027std::array<uint16_t, 4> GrDrawOpAtlas::AtlasLocator::getUVs(int padding) const {
28
29 uint16_t left = fRect.fLeft + padding;
30 uint16_t top = fRect.fTop + padding;
31 uint16_t right = fRect.fRight - padding;
32 uint16_t bottom = fRect.fBottom - padding;
33
34 // We pack the 2bit page index in the low bit of the u and v texture coords
35 uint32_t pageIndex = this->pageIndex();
36 std::tie(left, bottom) = GrDrawOpAtlas::PackIndexInTexCoords(left, bottom, pageIndex);
37 std::tie(right, top) = GrDrawOpAtlas::PackIndexInTexCoords(right, top, pageIndex);
38 return { left, top, right, bottom };
39}
40
Robert Phillipscd5099c2018-02-09 09:56:56 -050041// When proxy allocation is deferred until flush time the proxies acting as atlases require
42// special handling. This is because the usage that can be determined from the ops themselves
43// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
44// atlases. Extending the usage interval of any op that uses an atlas to the start of the
45// flush (as is done for proxies that are used for sw-generated masks) also won't work because
46// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
47// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
48// (which calls this method).
49void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050050 for (uint32_t i = 0; i < fNumActivePages; ++i) {
51 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050052 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050053 }
54}
55
Robert Phillips4bc70112018-03-01 10:24:02 -050056std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050057 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040058 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050059 int height, int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -050060 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -050061 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050062 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040063 if (!format.isValid()) {
64 return nullptr;
65 }
66
Herb Derby0ef780b2020-01-24 15:57:11 -050067 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType,
68 width, height, plotWidth, plotHeight,
69 generationCounter,
Robert Phillips4bc70112018-03-01 10:24:02 -050070 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050071 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040072 return nullptr;
73 }
74
Herb Derbya90ed952020-01-28 15:55:58 -050075 if (evictor != nullptr) {
76 atlas->fEvictionCallbacks.emplace_back(evictor);
77 }
Robert Phillips256c37b2017-03-01 14:32:46 -050078 return atlas;
79}
80
Jim Van Verthfb395102020-02-03 10:11:19 -050081// The two bits that make up the texture index are packed into the lower bits of the u and v
82// coordinate respectively.
83std::pair<uint16_t, uint16_t> GrDrawOpAtlas::PackIndexInTexCoords(uint16_t u, uint16_t v,
84 int pageIndex) {
85 SkASSERT(pageIndex >= 0 && pageIndex < 4);
86 uint16_t uBit = (pageIndex >> 1u) & 0x1u;
87 uint16_t vBit = pageIndex & 0x1u;
88 u <<= 1u;
89 u |= uBit;
90 v <<= 1u;
91 v |= vBit;
92 return std::make_pair(u, v);
93}
94
95std::tuple<uint16_t, uint16_t, int> GrDrawOpAtlas::UnpackIndexFromTexCoords(uint16_t u,
96 uint16_t v) {
97 int pageIndex = 0;
98 if (u & 0x1) {
99 pageIndex |= 0x2;
100 }
101 if (v & 0x1) {
102 pageIndex |= 0x1;
103 }
104 return std::make_tuple(u >> 1, v >> 1, pageIndex);
105}
Robert Phillips256c37b2017-03-01 14:32:46 -0500106
joshualitt5df175e2015-11-18 13:37:54 -0800107////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -0500108GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
109 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -0400110 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
111 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -0400112 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -0400113 , fPageIndex(pageIndex)
114 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -0500115 , fGenerationCounter(generationCounter)
116 , fGenID(fGenerationCounter->next())
Herb Derby4d721712020-01-24 14:31:16 -0500117 , fPlotLocator(CreatePlotLocator(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -0500118 , fData(nullptr)
119 , fWidth(width)
120 , fHeight(height)
121 , fX(offX)
122 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -0500123 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500124 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400125 , fColorType(colorType)
126 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800127#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500128 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800129#endif
130{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500131 // We expect the allocated dimensions to be a multiple of 4 bytes
132 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
133 // The padding for faster uploads only works for 1, 2 and 4 byte texels
134 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800135 fDirtyRect.setEmpty();
136}
joshualitt5bf99f12015-03-13 11:47:42 -0700137
Brian Salomon2ee084e2016-12-16 18:59:19 -0500138GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700139 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800140}
joshualitt5bf99f12015-03-13 11:47:42 -0700141
Robert Phillips6d3bc292020-04-06 10:29:28 -0400142bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, GrIRect16* rect) {
joshualitt5df175e2015-11-18 13:37:54 -0800143 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700144
Robert Phillips6d3bc292020-04-06 10:29:28 -0400145 SkIPoint16 loc;
146 if (!fRectanizer.addRect(width, height, &loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800147 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700148 }
joshualitt5bf99f12015-03-13 11:47:42 -0700149
Robert Phillips6d3bc292020-04-06 10:29:28 -0400150 *rect = GrIRect16::MakeXYWH(loc.fX, loc.fY, width, height);
151
jvanverthc3d706f2016-04-20 10:33:27 -0700152 if (!fData) {
153 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
154 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800155 }
156 size_t rowBytes = width * fBytesPerPixel;
157 const unsigned char* imagePtr = (const unsigned char*)image;
158 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700159 unsigned char* dataPtr = fData;
Robert Phillips6d3bc292020-04-06 10:29:28 -0400160 dataPtr += fBytesPerPixel * fWidth * rect->fTop;
161 dataPtr += fBytesPerPixel * rect->fLeft;
Brian Osmancce3e582016-10-14 11:42:20 -0400162 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500163 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400164 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400165 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400166 dataPtr += fBytesPerPixel * fWidth;
167 imagePtr += rowBytes;
168 }
169 } else {
170 for (int i = 0; i < height; ++i) {
171 memcpy(dataPtr, imagePtr, rowBytes);
172 dataPtr += fBytesPerPixel * fWidth;
173 imagePtr += rowBytes;
174 }
joshualitt5bf99f12015-03-13 11:47:42 -0700175 }
176
Robert Phillips6d3bc292020-04-06 10:29:28 -0400177 fDirtyRect.join({rect->fLeft, rect->fTop, rect->fRight, rect->fBottom});
robertphillips2b0536f2015-11-06 14:10:42 -0800178
Robert Phillips6d3bc292020-04-06 10:29:28 -0400179 rect->offset(fOffset.fX, fOffset.fY);
joshualitt5df175e2015-11-18 13:37:54 -0800180 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700181
joshualitt5df175e2015-11-18 13:37:54 -0800182 return true;
183}
joshualitt5bf99f12015-03-13 11:47:42 -0700184
Brian Salomon943ed792017-10-30 09:37:55 -0400185void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400186 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800187 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400188 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400189 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800190 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700191 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500192 // Clamp to 4-byte aligned boundaries
193 unsigned int clearBits = 0x3 / fBytesPerPixel;
194 fDirtyRect.fLeft &= ~clearBits;
195 fDirtyRect.fRight += clearBits;
196 fDirtyRect.fRight &= ~clearBits;
197 SkASSERT(fDirtyRect.fRight <= fWidth);
198 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700199 dataPtr += rowBytes * fDirtyRect.fTop;
200 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400201
Robert Phillipsacaa6072017-07-28 10:54:53 -0400202 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400203 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800204 fDirtyRect.setEmpty();
205 SkDEBUGCODE(fDirty = false;)
206}
207
Brian Salomon2ee084e2016-12-16 18:59:19 -0500208void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500209 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700210
Herb Derby0ef780b2020-01-24 15:57:11 -0500211 fGenID = fGenerationCounter->next();
Herb Derby4d721712020-01-24 14:31:16 -0500212 fPlotLocator = CreatePlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400213 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
214 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800215
216 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700217 if (fData) {
218 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700219 }
220
joshualitt5df175e2015-11-18 13:37:54 -0800221 fDirtyRect.setEmpty();
222 SkDEBUGCODE(fDirty = false;)
223}
joshualitt5bf99f12015-03-13 11:47:42 -0700224
joshualitt5bf99f12015-03-13 11:47:42 -0700225///////////////////////////////////////////////////////////////////////////////
226
Herb Derby0ef780b2020-01-24 15:57:11 -0500227GrDrawOpAtlas::GrDrawOpAtlas(
228 GrProxyProvider* proxyProvider, const GrBackendFormat& format,
229 GrColorType colorType, int width, int height, int plotWidth, int plotHeight,
230 GenerationCounter* generationCounter, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500231 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400232 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400233 , fTextureWidth(width)
234 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500235 , fPlotWidth(plotWidth)
236 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500237 , fGenerationCounter(generationCounter)
238 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400239 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400240 , fFlushesSinceLastUse(0)
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500241 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500242 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500243 int numPlotsX = width/plotWidth;
244 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400245 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400246 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
247 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800248
Jim Van Verth06f593c2018-02-20 11:30:10 -0500249 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700250
Herb Derby0ef780b2020-01-24 15:57:11 -0500251 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700252}
253
Herb Derby4d721712020-01-24 14:31:16 -0500254inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
Herb Derby1a496c52020-01-22 17:26:56 -0500255 for (auto evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500256 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700257 }
Herb Derby1a496c52020-01-22 17:26:56 -0500258
Herb Derby0ef780b2020-01-24 15:57:11 -0500259 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700260}
261
Herb Derby4d721712020-01-24 14:31:16 -0500262inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400263 AtlasLocator* atlasLocator, Plot* plot) {
264 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400265 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700266
267 // If our most recent upload has already occurred then we have to insert a new
268 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
269 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500270 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700271 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500272 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500273
Greg Daniel9715b6c2019-12-10 15:03:10 -0500274 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
275 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500276
Brian Salomon29b60c92017-10-31 14:42:10 -0400277 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400278 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
279 plotsp->uploadToTexture(writePixels, proxy);
280 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500281 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700282 }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400283 atlasLocator->fPlotLocator = plot->plotLocator();
Robert Phillips256c37b2017-03-01 14:32:46 -0500284 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700285}
286
Robert Phillips6d3bc292020-04-06 10:29:28 -0400287bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400288 GrDeferredUploadTarget* target, int width, int height,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400289 const void* image, AtlasLocator* atlasLocator) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500290 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500291
292 // look through all allocated plots for one we can share, in Most Recently Refed order
293 PlotList::Iter plotIter;
294 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
295
296 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500297 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500298
Robert Phillips6d3bc292020-04-06 10:29:28 -0400299 if (plot->addSubImage(width, height, image, &atlasLocator->fRect)) {
300 return this->updatePlot(target, atlasLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500301 }
302 }
303
304 return false;
305}
306
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400307// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
308//
309// This value is somewhat arbitrary -- the idea is to keep it low enough that
310// a page with unused plots will get removed reasonably quickly, but allow it
311// to hang around for a bit in case it's needed. The assumption is that flushes
312// are rare; i.e., we are not continually refreshing the frame.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400313static constexpr auto kPlotRecentlyUsedCount = 256;
314static constexpr auto kAtlasRecentlyUsedCount = 1024;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400315
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500316GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500317 GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400318 int width, int height, const void* image,
319 AtlasLocator* atlasLocator) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700320 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500321 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700322 }
joshualitt5bf99f12015-03-13 11:47:42 -0700323
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400324 const GrCaps& caps = *resourceProvider->caps();
325
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400326 // Look through each page to see if we can upload without having to flush
327 // We prioritize this upload to the first pages, not the most recently used, to make it easier
328 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500329 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400330 if (this->uploadToPage(caps, pageIdx, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500331 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400332 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400333 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400334
Jim Van Verth712fe732017-09-25 16:53:49 -0400335 // 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 -0400336 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
337 // We wait until we've grown to the full number of pages to begin evicting already flushed
338 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400339 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500340 if (fNumActivePages == this->maxPages()) {
341 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
342 Plot* plot = fPages[pageIdx].fPlotList.tail();
343 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500344 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500345 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500346 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
347 plot->bpp());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400348 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image,
349 &atlasLocator->fRect);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500350 SkASSERT(verify);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400351 if (!this->updatePlot(target, atlasLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500352 return ErrorCode::kError;
353 }
354 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400355 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500356 }
357 } else {
358 // If we haven't activated all the available pages, try to create a new one and add to it
359 if (!this->activateNewPage(resourceProvider)) {
360 return ErrorCode::kError;
361 }
362
Robert Phillips6d3bc292020-04-06 10:29:28 -0400363 if (this->uploadToPage(caps, fNumActivePages-1, target, width, height, image,
364 atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500365 return ErrorCode::kSucceeded;
366 } else {
367 // If we fail to upload to a newly activated page then something has gone terribly
368 // wrong - return an error
369 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400370 }
371 }
372
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500373 if (!fNumActivePages) {
374 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700375 }
376
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400377 // Try to find a plot that we can perform an inline upload to.
378 // We prioritize this upload in reverse order of pages to counterbalance the order above.
379 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500380 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400381 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500382 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400383 plot = currentPlot;
384 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500385 }
joshualitt5bf99f12015-03-13 11:47:42 -0700386 }
387
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400388 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
389 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
390 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
391 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500392 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400393 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500394 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700395 }
396
Herb Derby4d721712020-01-24 14:31:16 -0500397 this->processEviction(plot->plotLocator());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400398 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400399 fPages[pageIdx].fPlotList.remove(plot);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400400 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
robertphillips2b0536f2015-11-06 14:10:42 -0800401 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700402
Jim Van Vertha950b632017-09-12 11:54:11 -0400403 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500404 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400405 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, &atlasLocator->fRect);
joshualitt5bf99f12015-03-13 11:47:42 -0700406 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800407
robertphillips1f0e3502015-11-10 10:19:50 -0800408 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400409 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500410 // With c+14 we could move sk_sp into lambda to only ref once.
411 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500412
Greg Daniel9715b6c2019-12-10 15:03:10 -0500413 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
414 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700415
Brian Salomon943ed792017-10-30 09:37:55 -0400416 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
417 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
418 plotsp->uploadToTexture(writePixels, proxy);
419 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500420 newPlot->setLastUploadToken(lastUploadToken);
421
Robert Phillips6d3bc292020-04-06 10:29:28 -0400422 atlasLocator->fPlotLocator = newPlot->plotLocator();
robertphillips2b0536f2015-11-06 14:10:42 -0800423
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500424 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700425}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400426
Brian Salomon943ed792017-10-30 09:37:55 -0400427void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400428 if (fNumActivePages < 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400429 fPrevFlushToken = startTokenForNextFlush;
430 return;
431 }
432
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400433 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400434 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400435 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500436 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400437 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
438 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400439 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400440 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
441 plot->resetFlushesSinceLastUsed();
442 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400443 }
444
445 plotIter.next();
446 }
447 }
448
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400449 if (atlasUsedThisFlush) {
450 fFlushesSinceLastUse = 0;
451 } else {
452 ++fFlushesSinceLastUse;
453 }
454
455 // We only try to compact if the atlas was used in the recently completed flush or
456 // hasn't been used in a long time.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400457 // This is to handle the case where a lot of text or path rendering has occurred but then just
458 // a blinking cursor is drawn.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400459 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500460 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500461 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400462
463 // For all plots but the last one, update number of flushes since used, and check to see
464 // if there are any in the first pages that the last page can safely upload to.
465 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400466#ifdef DUMP_ATLAS_DATA
467 if (gDumpAtlasData) {
468 SkDebugf("page %d: ", pageIndex);
469 }
470#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400471 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
472 while (Plot* plot = plotIter.get()) {
473 // Update number of flushes since plot was last used
474 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
475 // to avoid deleting everything when we return to text drawing in the blinking
476 // cursor case
477 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
478 plot->incFlushesSinceLastUsed();
479 }
480
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400481#ifdef DUMP_ATLAS_DATA
482 if (gDumpAtlasData) {
483 SkDebugf("%d ", plot->flushesSinceLastUsed());
484 }
485#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400486 // Count plots we can potentially upload to in all pages except the last one
487 // (the potential compactee).
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400488 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500489 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400490 }
491
492 plotIter.next();
493 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400494#ifdef DUMP_ATLAS_DATA
495 if (gDumpAtlasData) {
496 SkDebugf("\n");
497 }
498#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400499 }
500
Jim Van Verth06f593c2018-02-20 11:30:10 -0500501 // Count recently used plots in the last page and evict any that are no longer in use.
502 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400503 // clear out usage of this page unless we have a large need.
504 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500505 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400506#ifdef DUMP_ATLAS_DATA
507 if (gDumpAtlasData) {
508 SkDebugf("page %d: ", lastPageIndex);
509 }
510#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400511 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400512 // Update number of flushes since plot was last used
513 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
514 plot->incFlushesSinceLastUsed();
515 }
516
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400517#ifdef DUMP_ATLAS_DATA
518 if (gDumpAtlasData) {
519 SkDebugf("%d ", plot->flushesSinceLastUsed());
520 }
521#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400522 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400523 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400524 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400525 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400526 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400527 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400528 }
529 plotIter.next();
530 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400531#ifdef DUMP_ATLAS_DATA
532 if (gDumpAtlasData) {
533 SkDebugf("\n");
534 }
535#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500536
537 // If recently used plots in the last page are using less than a quarter of the page, try
538 // to evict them if there's available space in earlier pages. Since we prioritize uploading
539 // to the first pages, this will eventually clear out usage of this page unless we have a
540 // large need.
541 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
542 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
543 while (Plot* plot = plotIter.get()) {
544 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400545 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth06f593c2018-02-20 11:30:10 -0500546 // See if there's room in an earlier page and if so evict.
547 // We need to be somewhat harsh here so that a handful of plots that are
548 // consistently in use don't end up locking the page in memory.
549 if (availablePlots.count() > 0) {
550 this->processEvictionAndResetRects(plot);
551 this->processEvictionAndResetRects(availablePlots.back());
552 availablePlots.pop_back();
553 --usedPlots;
554 }
555 if (!usedPlots || !availablePlots.count()) {
556 break;
557 }
558 }
559 plotIter.next();
560 }
561 }
562
Jim Van Verth106b5c42017-09-26 12:45:29 -0400563 // If none of the plots in the last page have been used recently, delete it.
Jim Van Verth26651882020-03-18 15:30:07 +0000564 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400565#ifdef DUMP_ATLAS_DATA
566 if (gDumpAtlasData) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400567 SkDebugf("delete %d\n", fNumActivePages-1);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400568 }
569#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500570 this->deactivateLastPage();
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400571 fFlushesSinceLastUse = 0;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400572 }
573 }
574
575 fPrevFlushToken = startTokenForNextFlush;
576}
577
Herb Derby0ef780b2020-01-24 15:57:11 -0500578bool GrDrawOpAtlas::createPages(
579 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500580 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500581
Brian Salomona56a7462020-02-07 14:17:25 -0500582 SkISize dims = {fTextureWidth, fTextureHeight};
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400583
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400584 int numPlotsX = fTextureWidth/fPlotWidth;
585 int numPlotsY = fTextureHeight/fPlotHeight;
586
Robert Phillips4bc70112018-03-01 10:24:02 -0500587 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500588 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500589 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400590 fFormat, dims, GrRenderable::kNo, 1, GrMipMapped::kNo, SkBackingFit::kExact,
591 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
592 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500593 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500594 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400595 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500596 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500597
598 // set up allocated plots
599 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
600
601 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
602 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
603 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
604 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500605 currPlot->reset(new Plot(
606 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500607
608 // build LRU list
609 fPages[i].fPlotList.addToHead(currPlot->get());
610 ++currPlot;
611 }
612 }
613
614 }
615
616 return true;
617}
618
619
620bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500621 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500622
Greg Daniel9715b6c2019-12-10 15:03:10 -0500623 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500624 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400625 }
626
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400627#ifdef DUMP_ATLAS_DATA
628 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500629 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400630 }
631#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500632
633 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400634 return true;
635}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400636
Robert Phillips4bc70112018-03-01 10:24:02 -0500637
638inline void GrDrawOpAtlas::deactivateLastPage() {
639 SkASSERT(fNumActivePages);
640
641 uint32_t lastPageIndex = fNumActivePages - 1;
642
643 int numPlotsX = fTextureWidth/fPlotWidth;
644 int numPlotsY = fTextureHeight/fPlotHeight;
645
Jim Van Verth106b5c42017-09-26 12:45:29 -0400646 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500647 for (int r = 0; r < numPlotsY; ++r) {
648 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500649 uint32_t plotIndex = r * numPlotsX + c;
650
651 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
652 currPlot->resetRects();
653 currPlot->resetFlushesSinceLastUsed();
654
655 // rebuild the LRU list
656 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
657 SkDEBUGCODE(currPlot->fList = nullptr);
658 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
659 }
660 }
661
662 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500663 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500664 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400665}
Herb Derby15d9ef22018-10-18 13:41:32 -0400666
Jim Van Verthf6206f92018-12-14 08:22:24 -0500667GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
668 static const SkISize kARGBDimensions[] = {
669 {256, 256}, // maxBytes < 2^19
670 {512, 256}, // 2^19 <= maxBytes < 2^20
671 {512, 512}, // 2^20 <= maxBytes < 2^21
672 {1024, 512}, // 2^21 <= maxBytes < 2^22
673 {1024, 1024}, // 2^22 <= maxBytes < 2^23
674 {2048, 1024}, // 2^23 <= maxBytes
675 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400676
Jim Van Verthf6206f92018-12-14 08:22:24 -0500677 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
678 maxBytes >>= 18;
679 // Take the floor of the log to get the index
680 int index = maxBytes > 0
681 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
682 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400683
Jim Van Verthf6206f92018-12-14 08:22:24 -0500684 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
685 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500686 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
687 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
688 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400689}
690
691SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500692 if (kA8_GrMaskFormat == type) {
693 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500694 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
695 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500696 } else {
697 return fARGBDimensions;
698 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400699}
700
Jim Van Verthf6206f92018-12-14 08:22:24 -0500701SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
702 if (kA8_GrMaskFormat == type) {
703 SkISize atlasDimensions = this->atlasDimensions(type);
704 // For A8 we want to grow the plots at larger texture sizes to accept more of the
705 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
706 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400707
Jim Van Verth578b0892018-12-20 20:48:55 +0000708 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
709 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500710 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000711 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400712
Jim Van Verthf6206f92018-12-14 08:22:24 -0500713 return { plotWidth, plotHeight };
714 } else {
715 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
716 return { 256, 256 };
717 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400718}
719
Jim Van Verthf6206f92018-12-14 08:22:24 -0500720constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;