blob: ffea3e10b341815e3aab59b80e744ad677741c75 [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
Robert Phillips03e4c952019-11-26 16:20:22 -050010#include "src/core/SkOpts.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrOnFlushResourceProvider.h"
12#include "src/gpu/GrOpFlushState.h"
13#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrResourceProvider.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040015#include "src/gpu/GrResourceProviderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000017#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070019
Jim Van Verthfb395102020-02-03 10:11:19 -050020#ifdef DUMP_ATLAS_DATA
21static bool gDumpAtlasData = false;
22#endif
23
Robert Phillips6d3bc292020-04-06 10:29:28 -040024std::array<uint16_t, 4> GrDrawOpAtlas::AtlasLocator::getUVs(int padding) const {
25
26 uint16_t left = fRect.fLeft + padding;
27 uint16_t top = fRect.fTop + padding;
28 uint16_t right = fRect.fRight - padding;
29 uint16_t bottom = fRect.fBottom - padding;
30
31 // We pack the 2bit page index in the low bit of the u and v texture coords
32 uint32_t pageIndex = this->pageIndex();
33 std::tie(left, bottom) = GrDrawOpAtlas::PackIndexInTexCoords(left, bottom, pageIndex);
34 std::tie(right, top) = GrDrawOpAtlas::PackIndexInTexCoords(right, top, pageIndex);
35 return { left, top, right, bottom };
36}
37
Robert Phillipse87106d2020-04-09 14:21:33 -040038#ifdef SK_DEBUG
39void GrDrawOpAtlas::AtlasLocator::validate(const GrDrawOpAtlas* drawOpAtlas) const {
40 // Verify that the plotIndex stored in the PlotLocator is consistent with the glyph rectangle
41 int numPlotsX = drawOpAtlas->fTextureWidth / drawOpAtlas->fPlotWidth;
42 int numPlotsY = drawOpAtlas->fTextureHeight / drawOpAtlas->fPlotHeight;
43
44 int plotIndex = this->plotIndex();
45 int plotX = fRect.fLeft / drawOpAtlas->fPlotWidth;
46 int plotY = fRect.fTop / drawOpAtlas->fPlotHeight;
47 SkASSERT(plotIndex == (numPlotsY - plotY - 1) * numPlotsX + (numPlotsX - plotX - 1));
48}
49#endif
50
Robert Phillipscd5099c2018-02-09 09:56:56 -050051// When proxy allocation is deferred until flush time the proxies acting as atlases require
52// special handling. This is because the usage that can be determined from the ops themselves
53// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
54// atlases. Extending the usage interval of any op that uses an atlas to the start of the
55// flush (as is done for proxies that are used for sw-generated masks) also won't work because
56// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
57// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
58// (which calls this method).
59void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050060 for (uint32_t i = 0; i < fNumActivePages; ++i) {
61 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050062 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050063 }
64}
65
Robert Phillips4bc70112018-03-01 10:24:02 -050066std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050067 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040068 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050069 int height, int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -050070 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -050071 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050072 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040073 if (!format.isValid()) {
74 return nullptr;
75 }
76
Herb Derby0ef780b2020-01-24 15:57:11 -050077 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType,
78 width, height, plotWidth, plotHeight,
79 generationCounter,
Robert Phillips4bc70112018-03-01 10:24:02 -050080 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050081 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040082 return nullptr;
83 }
84
Herb Derbya90ed952020-01-28 15:55:58 -050085 if (evictor != nullptr) {
86 atlas->fEvictionCallbacks.emplace_back(evictor);
87 }
Robert Phillips256c37b2017-03-01 14:32:46 -050088 return atlas;
89}
90
Jim Van Verthfb395102020-02-03 10:11:19 -050091// The two bits that make up the texture index are packed into the lower bits of the u and v
92// coordinate respectively.
93std::pair<uint16_t, uint16_t> GrDrawOpAtlas::PackIndexInTexCoords(uint16_t u, uint16_t v,
94 int pageIndex) {
95 SkASSERT(pageIndex >= 0 && pageIndex < 4);
96 uint16_t uBit = (pageIndex >> 1u) & 0x1u;
97 uint16_t vBit = pageIndex & 0x1u;
98 u <<= 1u;
99 u |= uBit;
100 v <<= 1u;
101 v |= vBit;
102 return std::make_pair(u, v);
103}
104
105std::tuple<uint16_t, uint16_t, int> GrDrawOpAtlas::UnpackIndexFromTexCoords(uint16_t u,
106 uint16_t v) {
107 int pageIndex = 0;
108 if (u & 0x1) {
109 pageIndex |= 0x2;
110 }
111 if (v & 0x1) {
112 pageIndex |= 0x1;
113 }
114 return std::make_tuple(u >> 1, v >> 1, pageIndex);
115}
Robert Phillips256c37b2017-03-01 14:32:46 -0500116
joshualitt5df175e2015-11-18 13:37:54 -0800117////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -0500118GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
119 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -0400120 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
121 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -0400122 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -0400123 , fPageIndex(pageIndex)
124 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -0500125 , fGenerationCounter(generationCounter)
126 , fGenID(fGenerationCounter->next())
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400127 , fPlotLocator(fPageIndex, fPlotIndex, fGenID)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500128 , fData(nullptr)
129 , fWidth(width)
130 , fHeight(height)
131 , fX(offX)
132 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -0500133 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500134 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400135 , fColorType(colorType)
136 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800137#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500138 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800139#endif
140{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500141 // We expect the allocated dimensions to be a multiple of 4 bytes
142 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
143 // The padding for faster uploads only works for 1, 2 and 4 byte texels
144 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800145 fDirtyRect.setEmpty();
146}
joshualitt5bf99f12015-03-13 11:47:42 -0700147
Brian Salomon2ee084e2016-12-16 18:59:19 -0500148GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700149 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800150}
joshualitt5bf99f12015-03-13 11:47:42 -0700151
Robert Phillips6d3bc292020-04-06 10:29:28 -0400152bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, GrIRect16* rect) {
joshualitt5df175e2015-11-18 13:37:54 -0800153 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700154
Robert Phillips6d3bc292020-04-06 10:29:28 -0400155 SkIPoint16 loc;
156 if (!fRectanizer.addRect(width, height, &loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800157 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700158 }
joshualitt5bf99f12015-03-13 11:47:42 -0700159
Robert Phillips6d3bc292020-04-06 10:29:28 -0400160 *rect = GrIRect16::MakeXYWH(loc.fX, loc.fY, width, height);
161
jvanverthc3d706f2016-04-20 10:33:27 -0700162 if (!fData) {
163 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
164 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800165 }
166 size_t rowBytes = width * fBytesPerPixel;
167 const unsigned char* imagePtr = (const unsigned char*)image;
168 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700169 unsigned char* dataPtr = fData;
Robert Phillips6d3bc292020-04-06 10:29:28 -0400170 dataPtr += fBytesPerPixel * fWidth * rect->fTop;
171 dataPtr += fBytesPerPixel * rect->fLeft;
Brian Osmancce3e582016-10-14 11:42:20 -0400172 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500173 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400174 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400175 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400176 dataPtr += fBytesPerPixel * fWidth;
177 imagePtr += rowBytes;
178 }
179 } else {
180 for (int i = 0; i < height; ++i) {
181 memcpy(dataPtr, imagePtr, rowBytes);
182 dataPtr += fBytesPerPixel * fWidth;
183 imagePtr += rowBytes;
184 }
joshualitt5bf99f12015-03-13 11:47:42 -0700185 }
186
Robert Phillips6d3bc292020-04-06 10:29:28 -0400187 fDirtyRect.join({rect->fLeft, rect->fTop, rect->fRight, rect->fBottom});
robertphillips2b0536f2015-11-06 14:10:42 -0800188
Robert Phillips6d3bc292020-04-06 10:29:28 -0400189 rect->offset(fOffset.fX, fOffset.fY);
joshualitt5df175e2015-11-18 13:37:54 -0800190 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700191
joshualitt5df175e2015-11-18 13:37:54 -0800192 return true;
193}
joshualitt5bf99f12015-03-13 11:47:42 -0700194
Brian Salomon943ed792017-10-30 09:37:55 -0400195void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400196 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800197 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400198 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400199 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800200 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700201 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500202 // Clamp to 4-byte aligned boundaries
203 unsigned int clearBits = 0x3 / fBytesPerPixel;
204 fDirtyRect.fLeft &= ~clearBits;
205 fDirtyRect.fRight += clearBits;
206 fDirtyRect.fRight &= ~clearBits;
207 SkASSERT(fDirtyRect.fRight <= fWidth);
208 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700209 dataPtr += rowBytes * fDirtyRect.fTop;
210 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400211
Robert Phillipsacaa6072017-07-28 10:54:53 -0400212 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400213 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800214 fDirtyRect.setEmpty();
215 SkDEBUGCODE(fDirty = false;)
216}
217
Brian Salomon2ee084e2016-12-16 18:59:19 -0500218void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500219 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700220
Herb Derby0ef780b2020-01-24 15:57:11 -0500221 fGenID = fGenerationCounter->next();
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400222 fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400223 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
224 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800225
226 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700227 if (fData) {
228 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700229 }
230
joshualitt5df175e2015-11-18 13:37:54 -0800231 fDirtyRect.setEmpty();
232 SkDEBUGCODE(fDirty = false;)
233}
joshualitt5bf99f12015-03-13 11:47:42 -0700234
joshualitt5bf99f12015-03-13 11:47:42 -0700235///////////////////////////////////////////////////////////////////////////////
236
Herb Derby0ef780b2020-01-24 15:57:11 -0500237GrDrawOpAtlas::GrDrawOpAtlas(
238 GrProxyProvider* proxyProvider, const GrBackendFormat& format,
239 GrColorType colorType, int width, int height, int plotWidth, int plotHeight,
240 GenerationCounter* generationCounter, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500241 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400242 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400243 , fTextureWidth(width)
244 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500245 , fPlotWidth(plotWidth)
246 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500247 , fGenerationCounter(generationCounter)
248 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400249 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400250 , fFlushesSinceLastUse(0)
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500251 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500252 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500253 int numPlotsX = width/plotWidth;
254 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400255 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400256 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
257 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800258
Jim Van Verth06f593c2018-02-20 11:30:10 -0500259 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700260
Herb Derby0ef780b2020-01-24 15:57:11 -0500261 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700262}
263
Herb Derby4d721712020-01-24 14:31:16 -0500264inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
Herb Derby1a496c52020-01-22 17:26:56 -0500265 for (auto evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500266 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700267 }
Herb Derby1a496c52020-01-22 17:26:56 -0500268
Herb Derby0ef780b2020-01-24 15:57:11 -0500269 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700270}
271
Herb Derby4d721712020-01-24 14:31:16 -0500272inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400273 AtlasLocator* atlasLocator, Plot* plot) {
274 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400275 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700276
277 // If our most recent upload has already occurred then we have to insert a new
278 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
279 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500280 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700281 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500282 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500283
Greg Daniel9715b6c2019-12-10 15:03:10 -0500284 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
285 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500286
Brian Salomon29b60c92017-10-31 14:42:10 -0400287 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400288 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
289 plotsp->uploadToTexture(writePixels, proxy);
290 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500291 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700292 }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400293 atlasLocator->fPlotLocator = plot->plotLocator();
Robert Phillipse87106d2020-04-09 14:21:33 -0400294 SkDEBUGCODE(atlasLocator->validate(this);)
Robert Phillips256c37b2017-03-01 14:32:46 -0500295 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700296}
297
Robert Phillips6d3bc292020-04-06 10:29:28 -0400298bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400299 GrDeferredUploadTarget* target, int width, int height,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400300 const void* image, AtlasLocator* atlasLocator) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500301 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500302
303 // look through all allocated plots for one we can share, in Most Recently Refed order
304 PlotList::Iter plotIter;
305 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
306
307 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500308 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500309
Robert Phillips6d3bc292020-04-06 10:29:28 -0400310 if (plot->addSubImage(width, height, image, &atlasLocator->fRect)) {
311 return this->updatePlot(target, atlasLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500312 }
313 }
314
315 return false;
316}
317
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400318// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
319//
320// This value is somewhat arbitrary -- the idea is to keep it low enough that
321// a page with unused plots will get removed reasonably quickly, but allow it
322// to hang around for a bit in case it's needed. The assumption is that flushes
323// are rare; i.e., we are not continually refreshing the frame.
Jonathan Backer40c683a2020-05-04 15:00:02 -0400324static constexpr auto kPlotRecentlyUsedCount = 32;
325static constexpr auto kAtlasRecentlyUsedCount = 128;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400326
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500327GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500328 GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400329 int width, int height, const void* image,
330 AtlasLocator* atlasLocator) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700331 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500332 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700333 }
joshualitt5bf99f12015-03-13 11:47:42 -0700334
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400335 const GrCaps& caps = *resourceProvider->caps();
336
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400337 // Look through each page to see if we can upload without having to flush
338 // We prioritize this upload to the first pages, not the most recently used, to make it easier
339 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500340 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400341 if (this->uploadToPage(caps, pageIdx, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500342 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400343 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400344 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400345
Jim Van Verth712fe732017-09-25 16:53:49 -0400346 // 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 -0400347 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
348 // We wait until we've grown to the full number of pages to begin evicting already flushed
349 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400350 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500351 if (fNumActivePages == this->maxPages()) {
352 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
353 Plot* plot = fPages[pageIdx].fPlotList.tail();
354 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500355 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500356 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500357 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
358 plot->bpp());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400359 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image,
360 &atlasLocator->fRect);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500361 SkASSERT(verify);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400362 if (!this->updatePlot(target, atlasLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500363 return ErrorCode::kError;
364 }
365 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400366 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500367 }
368 } else {
369 // If we haven't activated all the available pages, try to create a new one and add to it
370 if (!this->activateNewPage(resourceProvider)) {
371 return ErrorCode::kError;
372 }
373
Robert Phillips6d3bc292020-04-06 10:29:28 -0400374 if (this->uploadToPage(caps, fNumActivePages-1, target, width, height, image,
375 atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500376 return ErrorCode::kSucceeded;
377 } else {
378 // If we fail to upload to a newly activated page then something has gone terribly
379 // wrong - return an error
380 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400381 }
382 }
383
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500384 if (!fNumActivePages) {
385 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700386 }
387
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400388 // Try to find a plot that we can perform an inline upload to.
389 // We prioritize this upload in reverse order of pages to counterbalance the order above.
390 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500391 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400392 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500393 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400394 plot = currentPlot;
395 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500396 }
joshualitt5bf99f12015-03-13 11:47:42 -0700397 }
398
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400399 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
400 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
401 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
402 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500403 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400404 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500405 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700406 }
407
Herb Derby4d721712020-01-24 14:31:16 -0500408 this->processEviction(plot->plotLocator());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400409 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400410 fPages[pageIdx].fPlotList.remove(plot);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400411 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
robertphillips2b0536f2015-11-06 14:10:42 -0800412 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700413
Jim Van Vertha950b632017-09-12 11:54:11 -0400414 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500415 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400416 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, &atlasLocator->fRect);
joshualitt5bf99f12015-03-13 11:47:42 -0700417 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800418
robertphillips1f0e3502015-11-10 10:19:50 -0800419 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400420 // one it displaced most likely was uploaded ASAP.
Robert Phillipse87106d2020-04-09 14:21:33 -0400421 // With c++14 we could move sk_sp into lambda to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500422 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500423
Greg Daniel9715b6c2019-12-10 15:03:10 -0500424 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
425 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700426
Brian Salomon943ed792017-10-30 09:37:55 -0400427 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
428 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
429 plotsp->uploadToTexture(writePixels, proxy);
430 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500431 newPlot->setLastUploadToken(lastUploadToken);
432
Robert Phillips6d3bc292020-04-06 10:29:28 -0400433 atlasLocator->fPlotLocator = newPlot->plotLocator();
Robert Phillipse87106d2020-04-09 14:21:33 -0400434 SkDEBUGCODE(atlasLocator->validate(this);)
robertphillips2b0536f2015-11-06 14:10:42 -0800435
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500436 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700437}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400438
Brian Salomon943ed792017-10-30 09:37:55 -0400439void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400440 if (fNumActivePages < 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400441 fPrevFlushToken = startTokenForNextFlush;
442 return;
443 }
444
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400445 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400446 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400447 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500448 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400449 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
450 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400451 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400452 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
453 plot->resetFlushesSinceLastUsed();
454 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400455 }
456
457 plotIter.next();
458 }
459 }
460
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400461 if (atlasUsedThisFlush) {
462 fFlushesSinceLastUse = 0;
463 } else {
464 ++fFlushesSinceLastUse;
465 }
466
467 // We only try to compact if the atlas was used in the recently completed flush or
468 // hasn't been used in a long time.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400469 // This is to handle the case where a lot of text or path rendering has occurred but then just
470 // a blinking cursor is drawn.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400471 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500472 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500473 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400474
475 // For all plots but the last one, update number of flushes since used, and check to see
476 // if there are any in the first pages that the last page can safely upload to.
477 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400478#ifdef DUMP_ATLAS_DATA
479 if (gDumpAtlasData) {
480 SkDebugf("page %d: ", pageIndex);
481 }
482#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400483 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
484 while (Plot* plot = plotIter.get()) {
485 // Update number of flushes since plot was last used
486 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
487 // to avoid deleting everything when we return to text drawing in the blinking
488 // cursor case
489 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
490 plot->incFlushesSinceLastUsed();
491 }
492
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400493#ifdef DUMP_ATLAS_DATA
494 if (gDumpAtlasData) {
495 SkDebugf("%d ", plot->flushesSinceLastUsed());
496 }
497#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400498 // Count plots we can potentially upload to in all pages except the last one
499 // (the potential compactee).
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400500 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500501 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400502 }
503
504 plotIter.next();
505 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400506#ifdef DUMP_ATLAS_DATA
507 if (gDumpAtlasData) {
508 SkDebugf("\n");
509 }
510#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400511 }
512
Jim Van Verth06f593c2018-02-20 11:30:10 -0500513 // Count recently used plots in the last page and evict any that are no longer in use.
514 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400515 // clear out usage of this page unless we have a large need.
516 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500517 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400518#ifdef DUMP_ATLAS_DATA
519 if (gDumpAtlasData) {
520 SkDebugf("page %d: ", lastPageIndex);
521 }
522#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400523 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400524 // Update number of flushes since plot was last used
525 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
526 plot->incFlushesSinceLastUsed();
527 }
528
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400529#ifdef DUMP_ATLAS_DATA
530 if (gDumpAtlasData) {
531 SkDebugf("%d ", plot->flushesSinceLastUsed());
532 }
533#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400534 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400535 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400536 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400537 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400538 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400539 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400540 }
541 plotIter.next();
542 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400543#ifdef DUMP_ATLAS_DATA
544 if (gDumpAtlasData) {
545 SkDebugf("\n");
546 }
547#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500548
549 // If recently used plots in the last page are using less than a quarter of the page, try
550 // to evict them if there's available space in earlier pages. Since we prioritize uploading
551 // to the first pages, this will eventually clear out usage of this page unless we have a
552 // large need.
553 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
554 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
555 while (Plot* plot = plotIter.get()) {
556 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400557 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth06f593c2018-02-20 11:30:10 -0500558 // See if there's room in an earlier page and if so evict.
559 // We need to be somewhat harsh here so that a handful of plots that are
560 // consistently in use don't end up locking the page in memory.
561 if (availablePlots.count() > 0) {
562 this->processEvictionAndResetRects(plot);
563 this->processEvictionAndResetRects(availablePlots.back());
564 availablePlots.pop_back();
565 --usedPlots;
566 }
567 if (!usedPlots || !availablePlots.count()) {
568 break;
569 }
570 }
571 plotIter.next();
572 }
573 }
574
Jim Van Verth106b5c42017-09-26 12:45:29 -0400575 // If none of the plots in the last page have been used recently, delete it.
Jim Van Verth26651882020-03-18 15:30:07 +0000576 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400577#ifdef DUMP_ATLAS_DATA
578 if (gDumpAtlasData) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400579 SkDebugf("delete %d\n", fNumActivePages-1);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400580 }
581#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500582 this->deactivateLastPage();
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400583 fFlushesSinceLastUse = 0;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400584 }
585 }
586
587 fPrevFlushToken = startTokenForNextFlush;
588}
589
Herb Derby0ef780b2020-01-24 15:57:11 -0500590bool GrDrawOpAtlas::createPages(
591 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500592 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500593
Brian Salomona56a7462020-02-07 14:17:25 -0500594 SkISize dims = {fTextureWidth, fTextureHeight};
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400595
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400596 int numPlotsX = fTextureWidth/fPlotWidth;
597 int numPlotsY = fTextureHeight/fPlotHeight;
598
Robert Phillips4bc70112018-03-01 10:24:02 -0500599 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500600 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500601 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400602 fFormat, dims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400603 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
604 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500605 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500606 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400607 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500608 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500609
610 // set up allocated plots
611 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
612
613 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
614 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
615 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
616 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500617 currPlot->reset(new Plot(
618 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500619
620 // build LRU list
621 fPages[i].fPlotList.addToHead(currPlot->get());
622 ++currPlot;
623 }
624 }
625
626 }
627
628 return true;
629}
630
631
632bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500633 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500634
Greg Daniel9715b6c2019-12-10 15:03:10 -0500635 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500636 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400637 }
638
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400639#ifdef DUMP_ATLAS_DATA
640 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500641 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400642 }
643#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500644
645 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400646 return true;
647}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400648
Robert Phillips4bc70112018-03-01 10:24:02 -0500649
650inline void GrDrawOpAtlas::deactivateLastPage() {
651 SkASSERT(fNumActivePages);
652
653 uint32_t lastPageIndex = fNumActivePages - 1;
654
655 int numPlotsX = fTextureWidth/fPlotWidth;
656 int numPlotsY = fTextureHeight/fPlotHeight;
657
Jim Van Verth106b5c42017-09-26 12:45:29 -0400658 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500659 for (int r = 0; r < numPlotsY; ++r) {
660 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500661 uint32_t plotIndex = r * numPlotsX + c;
662
663 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
664 currPlot->resetRects();
665 currPlot->resetFlushesSinceLastUsed();
666
667 // rebuild the LRU list
668 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
669 SkDEBUGCODE(currPlot->fList = nullptr);
670 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
671 }
672 }
673
674 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500675 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500676 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400677}
Herb Derby15d9ef22018-10-18 13:41:32 -0400678
Jim Van Verthf6206f92018-12-14 08:22:24 -0500679GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
680 static const SkISize kARGBDimensions[] = {
681 {256, 256}, // maxBytes < 2^19
682 {512, 256}, // 2^19 <= maxBytes < 2^20
683 {512, 512}, // 2^20 <= maxBytes < 2^21
684 {1024, 512}, // 2^21 <= maxBytes < 2^22
685 {1024, 1024}, // 2^22 <= maxBytes < 2^23
686 {2048, 1024}, // 2^23 <= maxBytes
687 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400688
Jim Van Verthf6206f92018-12-14 08:22:24 -0500689 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
690 maxBytes >>= 18;
691 // Take the floor of the log to get the index
692 int index = maxBytes > 0
693 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
694 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400695
Jim Van Verthf6206f92018-12-14 08:22:24 -0500696 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
697 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500698 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
699 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
700 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400701}
702
703SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500704 if (kA8_GrMaskFormat == type) {
705 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500706 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
707 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500708 } else {
709 return fARGBDimensions;
710 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400711}
712
Jim Van Verthf6206f92018-12-14 08:22:24 -0500713SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
714 if (kA8_GrMaskFormat == type) {
715 SkISize atlasDimensions = this->atlasDimensions(type);
716 // For A8 we want to grow the plots at larger texture sizes to accept more of the
717 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
718 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400719
Jim Van Verth578b0892018-12-20 20:48:55 +0000720 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
721 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500722 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000723 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400724
Jim Van Verthf6206f92018-12-14 08:22:24 -0500725 return { plotWidth, plotHeight };
726 } else {
727 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
728 return { 256, 256 };
729 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400730}
731
Jim Van Verthf6206f92018-12-14 08:22:24 -0500732constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;