blob: 6ea095c4d55bf1a7265b77ca37bfd2ae534a75b4 [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 Phillips6d3bc292020-04-06 10:29:28 -040026std::array<uint16_t, 4> GrDrawOpAtlas::AtlasLocator::getUVs(int padding) const {
27
28 uint16_t left = fRect.fLeft + padding;
29 uint16_t top = fRect.fTop + padding;
30 uint16_t right = fRect.fRight - padding;
31 uint16_t bottom = fRect.fBottom - padding;
32
33 // We pack the 2bit page index in the low bit of the u and v texture coords
34 uint32_t pageIndex = this->pageIndex();
35 std::tie(left, bottom) = GrDrawOpAtlas::PackIndexInTexCoords(left, bottom, pageIndex);
36 std::tie(right, top) = GrDrawOpAtlas::PackIndexInTexCoords(right, top, pageIndex);
37 return { left, top, right, bottom };
38}
39
Robert Phillipse87106d2020-04-09 14:21:33 -040040#ifdef SK_DEBUG
41void GrDrawOpAtlas::AtlasLocator::validate(const GrDrawOpAtlas* drawOpAtlas) const {
42 // Verify that the plotIndex stored in the PlotLocator is consistent with the glyph rectangle
43 int numPlotsX = drawOpAtlas->fTextureWidth / drawOpAtlas->fPlotWidth;
44 int numPlotsY = drawOpAtlas->fTextureHeight / drawOpAtlas->fPlotHeight;
45
46 int plotIndex = this->plotIndex();
47 int plotX = fRect.fLeft / drawOpAtlas->fPlotWidth;
48 int plotY = fRect.fTop / drawOpAtlas->fPlotHeight;
49 SkASSERT(plotIndex == (numPlotsY - plotY - 1) * numPlotsX + (numPlotsX - plotX - 1));
50}
51#endif
52
Robert Phillipscd5099c2018-02-09 09:56:56 -050053// When proxy allocation is deferred until flush time the proxies acting as atlases require
54// special handling. This is because the usage that can be determined from the ops themselves
55// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
56// atlases. Extending the usage interval of any op that uses an atlas to the start of the
57// flush (as is done for proxies that are used for sw-generated masks) also won't work because
58// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
59// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
60// (which calls this method).
61void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050062 for (uint32_t i = 0; i < fNumActivePages; ++i) {
63 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Greg Daniel9715b6c2019-12-10 15:03:10 -050064 SkASSERT(fViews[i].proxy() && fViews[i].proxy()->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050065 }
66}
67
Robert Phillips4bc70112018-03-01 10:24:02 -050068std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050069 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040070 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050071 int height, int plotWidth, int plotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -050072 GenerationCounter* generationCounter,
Brian Salomon9f545bc2017-11-06 10:36:57 -050073 AllowMultitexturing allowMultitexturing,
Herb Derby1a496c52020-01-22 17:26:56 -050074 EvictionCallback* evictor) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040075 if (!format.isValid()) {
76 return nullptr;
77 }
78
Herb Derby0ef780b2020-01-24 15:57:11 -050079 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType,
80 width, height, plotWidth, plotHeight,
81 generationCounter,
Robert Phillips4bc70112018-03-01 10:24:02 -050082 allowMultitexturing));
Greg Daniel9715b6c2019-12-10 15:03:10 -050083 if (!atlas->getViews()[0].proxy()) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040084 return nullptr;
85 }
86
Herb Derbya90ed952020-01-28 15:55:58 -050087 if (evictor != nullptr) {
88 atlas->fEvictionCallbacks.emplace_back(evictor);
89 }
Robert Phillips256c37b2017-03-01 14:32:46 -050090 return atlas;
91}
92
Jim Van Verthfb395102020-02-03 10:11:19 -050093// The two bits that make up the texture index are packed into the lower bits of the u and v
94// coordinate respectively.
95std::pair<uint16_t, uint16_t> GrDrawOpAtlas::PackIndexInTexCoords(uint16_t u, uint16_t v,
96 int pageIndex) {
97 SkASSERT(pageIndex >= 0 && pageIndex < 4);
98 uint16_t uBit = (pageIndex >> 1u) & 0x1u;
99 uint16_t vBit = pageIndex & 0x1u;
100 u <<= 1u;
101 u |= uBit;
102 v <<= 1u;
103 v |= vBit;
104 return std::make_pair(u, v);
105}
106
107std::tuple<uint16_t, uint16_t, int> GrDrawOpAtlas::UnpackIndexFromTexCoords(uint16_t u,
108 uint16_t v) {
109 int pageIndex = 0;
110 if (u & 0x1) {
111 pageIndex |= 0x2;
112 }
113 if (v & 0x1) {
114 pageIndex |= 0x1;
115 }
116 return std::make_tuple(u >> 1, v >> 1, pageIndex);
117}
Robert Phillips256c37b2017-03-01 14:32:46 -0500118
joshualitt5df175e2015-11-18 13:37:54 -0800119////////////////////////////////////////////////////////////////////////////////
Herb Derby0ef780b2020-01-24 15:57:11 -0500120GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, GenerationCounter* generationCounter,
121 int offX, int offY, int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -0400122 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
123 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -0400124 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -0400125 , fPageIndex(pageIndex)
126 , fPlotIndex(plotIndex)
Herb Derby0ef780b2020-01-24 15:57:11 -0500127 , fGenerationCounter(generationCounter)
128 , fGenID(fGenerationCounter->next())
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400129 , fPlotLocator(fPageIndex, fPlotIndex, fGenID)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500130 , fData(nullptr)
131 , fWidth(width)
132 , fHeight(height)
133 , fX(offX)
134 , fY(offY)
Herb Derby73c75872020-01-22 18:09:16 -0500135 , fRectanizer(width, height)
Brian Salomon2ee084e2016-12-16 18:59:19 -0500136 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -0400137 , fColorType(colorType)
138 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -0800139#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -0500140 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -0800141#endif
142{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500143 // We expect the allocated dimensions to be a multiple of 4 bytes
144 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
145 // The padding for faster uploads only works for 1, 2 and 4 byte texels
146 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -0800147 fDirtyRect.setEmpty();
148}
joshualitt5bf99f12015-03-13 11:47:42 -0700149
Brian Salomon2ee084e2016-12-16 18:59:19 -0500150GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -0700151 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -0800152}
joshualitt5bf99f12015-03-13 11:47:42 -0700153
Robert Phillips6d3bc292020-04-06 10:29:28 -0400154bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, GrIRect16* rect) {
joshualitt5df175e2015-11-18 13:37:54 -0800155 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700156
Robert Phillips6d3bc292020-04-06 10:29:28 -0400157 SkIPoint16 loc;
158 if (!fRectanizer.addRect(width, height, &loc)) {
joshualitt5df175e2015-11-18 13:37:54 -0800159 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700160 }
joshualitt5bf99f12015-03-13 11:47:42 -0700161
Robert Phillips6d3bc292020-04-06 10:29:28 -0400162 *rect = GrIRect16::MakeXYWH(loc.fX, loc.fY, width, height);
163
jvanverthc3d706f2016-04-20 10:33:27 -0700164 if (!fData) {
165 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
166 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800167 }
168 size_t rowBytes = width * fBytesPerPixel;
169 const unsigned char* imagePtr = (const unsigned char*)image;
170 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700171 unsigned char* dataPtr = fData;
Robert Phillips6d3bc292020-04-06 10:29:28 -0400172 dataPtr += fBytesPerPixel * fWidth * rect->fTop;
173 dataPtr += fBytesPerPixel * rect->fLeft;
Brian Osmancce3e582016-10-14 11:42:20 -0400174 // copy into the data buffer, swizzling as we go if this is ARGB data
Greg Danielb58a3c72020-01-23 10:05:14 -0500175 if (4 == fBytesPerPixel && kN32_SkColorType == kBGRA_8888_SkColorType) {
Brian Osmancce3e582016-10-14 11:42:20 -0400176 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400177 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400178 dataPtr += fBytesPerPixel * fWidth;
179 imagePtr += rowBytes;
180 }
181 } else {
182 for (int i = 0; i < height; ++i) {
183 memcpy(dataPtr, imagePtr, rowBytes);
184 dataPtr += fBytesPerPixel * fWidth;
185 imagePtr += rowBytes;
186 }
joshualitt5bf99f12015-03-13 11:47:42 -0700187 }
188
Robert Phillips6d3bc292020-04-06 10:29:28 -0400189 fDirtyRect.join({rect->fLeft, rect->fTop, rect->fRight, rect->fBottom});
robertphillips2b0536f2015-11-06 14:10:42 -0800190
Robert Phillips6d3bc292020-04-06 10:29:28 -0400191 rect->offset(fOffset.fX, fOffset.fY);
joshualitt5df175e2015-11-18 13:37:54 -0800192 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700193
joshualitt5df175e2015-11-18 13:37:54 -0800194 return true;
195}
joshualitt5bf99f12015-03-13 11:47:42 -0700196
Brian Salomon943ed792017-10-30 09:37:55 -0400197void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400198 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800199 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400200 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400201 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800202 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700203 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500204 // Clamp to 4-byte aligned boundaries
205 unsigned int clearBits = 0x3 / fBytesPerPixel;
206 fDirtyRect.fLeft &= ~clearBits;
207 fDirtyRect.fRight += clearBits;
208 fDirtyRect.fRight &= ~clearBits;
209 SkASSERT(fDirtyRect.fRight <= fWidth);
210 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700211 dataPtr += rowBytes * fDirtyRect.fTop;
212 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400213
Robert Phillipsacaa6072017-07-28 10:54:53 -0400214 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400215 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800216 fDirtyRect.setEmpty();
217 SkDEBUGCODE(fDirty = false;)
218}
219
Brian Salomon2ee084e2016-12-16 18:59:19 -0500220void GrDrawOpAtlas::Plot::resetRects() {
Herb Derby73c75872020-01-22 18:09:16 -0500221 fRectanizer.reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700222
Herb Derby0ef780b2020-01-24 15:57:11 -0500223 fGenID = fGenerationCounter->next();
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400224 fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400225 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
226 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800227
228 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700229 if (fData) {
230 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700231 }
232
joshualitt5df175e2015-11-18 13:37:54 -0800233 fDirtyRect.setEmpty();
234 SkDEBUGCODE(fDirty = false;)
235}
joshualitt5bf99f12015-03-13 11:47:42 -0700236
joshualitt5bf99f12015-03-13 11:47:42 -0700237///////////////////////////////////////////////////////////////////////////////
238
Herb Derby0ef780b2020-01-24 15:57:11 -0500239GrDrawOpAtlas::GrDrawOpAtlas(
240 GrProxyProvider* proxyProvider, const GrBackendFormat& format,
241 GrColorType colorType, int width, int height, int plotWidth, int plotHeight,
242 GenerationCounter* generationCounter, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500243 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400244 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400245 , fTextureWidth(width)
246 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500247 , fPlotWidth(plotWidth)
248 , fPlotHeight(plotHeight)
Herb Derby0ef780b2020-01-24 15:57:11 -0500249 , fGenerationCounter(generationCounter)
250 , fAtlasGeneration(fGenerationCounter->next())
Brian Salomon943ed792017-10-30 09:37:55 -0400251 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400252 , fFlushesSinceLastUse(0)
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500253 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500254 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500255 int numPlotsX = width/plotWidth;
256 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400257 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400258 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
259 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800260
Jim Van Verth06f593c2018-02-20 11:30:10 -0500261 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700262
Herb Derby0ef780b2020-01-24 15:57:11 -0500263 this->createPages(proxyProvider, generationCounter);
joshualitt5bf99f12015-03-13 11:47:42 -0700264}
265
Herb Derby4d721712020-01-24 14:31:16 -0500266inline void GrDrawOpAtlas::processEviction(PlotLocator plotLocator) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400267 for (EvictionCallback* evictor : fEvictionCallbacks) {
Herb Derby4d721712020-01-24 14:31:16 -0500268 evictor->evict(plotLocator);
joshualitt5bf99f12015-03-13 11:47:42 -0700269 }
Herb Derby1a496c52020-01-22 17:26:56 -0500270
Herb Derby0ef780b2020-01-24 15:57:11 -0500271 fAtlasGeneration = fGenerationCounter->next();
joshualitt5bf99f12015-03-13 11:47:42 -0700272}
273
Herb Derby4d721712020-01-24 14:31:16 -0500274inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400275 AtlasLocator* atlasLocator, Plot* plot) {
276 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400277 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700278
279 // If our most recent upload has already occurred then we have to insert a new
280 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
281 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500282 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700283 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500284 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500285
Greg Daniel9715b6c2019-12-10 15:03:10 -0500286 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
287 SkASSERT(proxy && proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500288
Brian Salomon29b60c92017-10-31 14:42:10 -0400289 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400290 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
291 plotsp->uploadToTexture(writePixels, proxy);
292 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500293 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700294 }
Robert Phillips6d3bc292020-04-06 10:29:28 -0400295 atlasLocator->fPlotLocator = plot->plotLocator();
Robert Phillipse87106d2020-04-09 14:21:33 -0400296 SkDEBUGCODE(atlasLocator->validate(this);)
Robert Phillips256c37b2017-03-01 14:32:46 -0500297 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700298}
299
Robert Phillips6d3bc292020-04-06 10:29:28 -0400300bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx,
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400301 GrDeferredUploadTarget* target, int width, int height,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400302 const void* image, AtlasLocator* atlasLocator) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500303 SkASSERT(fViews[pageIdx].proxy() && fViews[pageIdx].proxy()->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500304
305 // look through all allocated plots for one we can share, in Most Recently Refed order
306 PlotList::Iter plotIter;
307 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
308
309 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500310 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500311
Robert Phillips6d3bc292020-04-06 10:29:28 -0400312 if (plot->addSubImage(width, height, image, &atlasLocator->fRect)) {
313 return this->updatePlot(target, atlasLocator, plot);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500314 }
315 }
316
317 return false;
318}
319
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400320// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
321//
322// This value is somewhat arbitrary -- the idea is to keep it low enough that
323// a page with unused plots will get removed reasonably quickly, but allow it
324// to hang around for a bit in case it's needed. The assumption is that flushes
325// are rare; i.e., we are not continually refreshing the frame.
Jonathan Backer40c683a2020-05-04 15:00:02 -0400326static constexpr auto kPlotRecentlyUsedCount = 32;
327static constexpr auto kAtlasRecentlyUsedCount = 128;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400328
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500329GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
Herb Derby4d721712020-01-24 14:31:16 -0500330 GrDeferredUploadTarget* target,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400331 int width, int height, const void* image,
332 AtlasLocator* atlasLocator) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700333 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500334 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700335 }
joshualitt5bf99f12015-03-13 11:47:42 -0700336
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400337 const GrCaps& caps = *resourceProvider->caps();
338
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400339 // Look through each page to see if we can upload without having to flush
340 // We prioritize this upload to the first pages, not the most recently used, to make it easier
341 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500342 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Robert Phillips6d3bc292020-04-06 10:29:28 -0400343 if (this->uploadToPage(caps, pageIdx, target, width, height, image, atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500344 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400345 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400346 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400347
Jim Van Verth712fe732017-09-25 16:53:49 -0400348 // 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 -0400349 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
350 // We wait until we've grown to the full number of pages to begin evicting already flushed
351 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400352 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500353 if (fNumActivePages == this->maxPages()) {
354 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
355 Plot* plot = fPages[pageIdx].fPlotList.tail();
356 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500357 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500358 this->processEvictionAndResetRects(plot);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500359 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) ==
360 plot->bpp());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400361 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image,
362 &atlasLocator->fRect);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500363 SkASSERT(verify);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400364 if (!this->updatePlot(target, atlasLocator, plot)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500365 return ErrorCode::kError;
366 }
367 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400368 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500369 }
370 } else {
371 // If we haven't activated all the available pages, try to create a new one and add to it
372 if (!this->activateNewPage(resourceProvider)) {
373 return ErrorCode::kError;
374 }
375
Robert Phillips6d3bc292020-04-06 10:29:28 -0400376 if (this->uploadToPage(caps, fNumActivePages-1, target, width, height, image,
377 atlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500378 return ErrorCode::kSucceeded;
379 } else {
380 // If we fail to upload to a newly activated page then something has gone terribly
381 // wrong - return an error
382 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400383 }
384 }
385
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500386 if (!fNumActivePages) {
387 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700388 }
389
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400390 // Try to find a plot that we can perform an inline upload to.
391 // We prioritize this upload in reverse order of pages to counterbalance the order above.
392 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500393 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400394 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500395 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400396 plot = currentPlot;
397 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500398 }
joshualitt5bf99f12015-03-13 11:47:42 -0700399 }
400
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400401 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
402 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
403 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
404 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500405 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400406 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500407 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700408 }
409
Herb Derby4d721712020-01-24 14:31:16 -0500410 this->processEviction(plot->plotLocator());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400411 int pageIdx = plot->pageIndex();
Jim Van Vertha950b632017-09-12 11:54:11 -0400412 fPages[pageIdx].fPlotList.remove(plot);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400413 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->plotIndex()];
robertphillips2b0536f2015-11-06 14:10:42 -0800414 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700415
Jim Van Vertha950b632017-09-12 11:54:11 -0400416 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500417 SkASSERT(caps.bytesPerPixel(fViews[pageIdx].proxy()->backendFormat()) == newPlot->bpp());
Robert Phillips6d3bc292020-04-06 10:29:28 -0400418 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, &atlasLocator->fRect);
joshualitt5bf99f12015-03-13 11:47:42 -0700419 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800420
robertphillips1f0e3502015-11-10 10:19:50 -0800421 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400422 // one it displaced most likely was uploaded ASAP.
Robert Phillipse87106d2020-04-09 14:21:33 -0400423 // With c++14 we could move sk_sp into lambda to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500424 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500425
Greg Daniel9715b6c2019-12-10 15:03:10 -0500426 GrTextureProxy* proxy = fViews[pageIdx].asTextureProxy();
427 SkASSERT(proxy && proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700428
Brian Salomon943ed792017-10-30 09:37:55 -0400429 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
430 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
431 plotsp->uploadToTexture(writePixels, proxy);
432 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500433 newPlot->setLastUploadToken(lastUploadToken);
434
Robert Phillips6d3bc292020-04-06 10:29:28 -0400435 atlasLocator->fPlotLocator = newPlot->plotLocator();
Robert Phillipse87106d2020-04-09 14:21:33 -0400436 SkDEBUGCODE(atlasLocator->validate(this);)
robertphillips2b0536f2015-11-06 14:10:42 -0800437
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500438 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700439}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400440
Brian Salomon943ed792017-10-30 09:37:55 -0400441void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400442 if (fNumActivePages < 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400443 fPrevFlushToken = startTokenForNextFlush;
444 return;
445 }
446
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400447 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400448 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400449 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500450 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400451 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
452 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400453 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400454 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
455 plot->resetFlushesSinceLastUsed();
456 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400457 }
458
459 plotIter.next();
460 }
461 }
462
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400463 if (atlasUsedThisFlush) {
464 fFlushesSinceLastUse = 0;
465 } else {
466 ++fFlushesSinceLastUse;
467 }
468
469 // We only try to compact if the atlas was used in the recently completed flush or
470 // hasn't been used in a long time.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400471 // This is to handle the case where a lot of text or path rendering has occurred but then just
472 // a blinking cursor is drawn.
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400473 if (atlasUsedThisFlush || fFlushesSinceLastUse > kAtlasRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500474 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500475 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400476
477 // For all plots but the last one, update number of flushes since used, and check to see
478 // if there are any in the first pages that the last page can safely upload to.
479 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400480#ifdef DUMP_ATLAS_DATA
481 if (gDumpAtlasData) {
482 SkDebugf("page %d: ", pageIndex);
483 }
484#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400485 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
486 while (Plot* plot = plotIter.get()) {
487 // Update number of flushes since plot was last used
488 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
489 // to avoid deleting everything when we return to text drawing in the blinking
490 // cursor case
491 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
492 plot->incFlushesSinceLastUsed();
493 }
494
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400495#ifdef DUMP_ATLAS_DATA
496 if (gDumpAtlasData) {
497 SkDebugf("%d ", plot->flushesSinceLastUsed());
498 }
499#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400500 // Count plots we can potentially upload to in all pages except the last one
501 // (the potential compactee).
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400502 if (plot->flushesSinceLastUsed() > kPlotRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500503 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400504 }
505
506 plotIter.next();
507 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400508#ifdef DUMP_ATLAS_DATA
509 if (gDumpAtlasData) {
510 SkDebugf("\n");
511 }
512#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400513 }
514
Jim Van Verth06f593c2018-02-20 11:30:10 -0500515 // Count recently used plots in the last page and evict any that are no longer in use.
516 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400517 // clear out usage of this page unless we have a large need.
518 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500519 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400520#ifdef DUMP_ATLAS_DATA
521 if (gDumpAtlasData) {
522 SkDebugf("page %d: ", lastPageIndex);
523 }
524#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400525 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400526 // Update number of flushes since plot was last used
527 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
528 plot->incFlushesSinceLastUsed();
529 }
530
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400531#ifdef DUMP_ATLAS_DATA
532 if (gDumpAtlasData) {
533 SkDebugf("%d ", plot->flushesSinceLastUsed());
534 }
535#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400536 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400537 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400538 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400539 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400540 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400541 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400542 }
543 plotIter.next();
544 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400545#ifdef DUMP_ATLAS_DATA
546 if (gDumpAtlasData) {
547 SkDebugf("\n");
548 }
549#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500550
551 // If recently used plots in the last page are using less than a quarter of the page, try
552 // to evict them if there's available space in earlier pages. Since we prioritize uploading
553 // to the first pages, this will eventually clear out usage of this page unless we have a
554 // large need.
555 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
556 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
557 while (Plot* plot = plotIter.get()) {
558 // If this plot was used recently
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400559 if (plot->flushesSinceLastUsed() <= kPlotRecentlyUsedCount) {
Jim Van Verth06f593c2018-02-20 11:30:10 -0500560 // See if there's room in an earlier page and if so evict.
561 // We need to be somewhat harsh here so that a handful of plots that are
562 // consistently in use don't end up locking the page in memory.
563 if (availablePlots.count() > 0) {
564 this->processEvictionAndResetRects(plot);
565 this->processEvictionAndResetRects(availablePlots.back());
566 availablePlots.pop_back();
567 --usedPlots;
568 }
569 if (!usedPlots || !availablePlots.count()) {
570 break;
571 }
572 }
573 plotIter.next();
574 }
575 }
576
Jim Van Verth106b5c42017-09-26 12:45:29 -0400577 // If none of the plots in the last page have been used recently, delete it.
Jim Van Verth26651882020-03-18 15:30:07 +0000578 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400579#ifdef DUMP_ATLAS_DATA
580 if (gDumpAtlasData) {
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400581 SkDebugf("delete %d\n", fNumActivePages-1);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400582 }
583#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500584 this->deactivateLastPage();
Jim Van Verth77eb96d2020-03-18 12:32:34 -0400585 fFlushesSinceLastUse = 0;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400586 }
587 }
588
589 fPrevFlushToken = startTokenForNextFlush;
590}
591
Herb Derby0ef780b2020-01-24 15:57:11 -0500592bool GrDrawOpAtlas::createPages(
593 GrProxyProvider* proxyProvider, GenerationCounter* generationCounter) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500594 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500595
Brian Salomona56a7462020-02-07 14:17:25 -0500596 SkISize dims = {fTextureWidth, fTextureHeight};
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400597
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400598 int numPlotsX = fTextureWidth/fPlotWidth;
599 int numPlotsY = fTextureHeight/fPlotHeight;
600
Robert Phillips4bc70112018-03-01 10:24:02 -0500601 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel47c20e82020-01-21 14:29:57 -0500602 GrSwizzle swizzle = proxyProvider->caps()->getReadSwizzle(fFormat, fColorType);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500603 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400604 fFormat, dims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact,
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400605 SkBudgeted::kYes, GrProtected::kNo, GrInternalSurfaceFlags::kNone,
606 GrSurfaceProxy::UseAllocator::kNo);
Greg Daniel9715b6c2019-12-10 15:03:10 -0500607 if (!proxy) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500608 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400609 }
Greg Daniel9715b6c2019-12-10 15:03:10 -0500610 fViews[i] = GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Robert Phillips4bc70112018-03-01 10:24:02 -0500611
612 // set up allocated plots
John Stilesfbd050b2020-08-03 13:21:46 -0400613 fPages[i].fPlotArray = std::make_unique<sk_sp<Plot>[]>(numPlotsX * numPlotsY);
Robert Phillips4bc70112018-03-01 10:24:02 -0500614
615 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
616 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
617 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
618 uint32_t plotIndex = r * numPlotsX + c;
Herb Derby0ef780b2020-01-24 15:57:11 -0500619 currPlot->reset(new Plot(
620 i, plotIndex, generationCounter, x, y, fPlotWidth, fPlotHeight, fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500621
622 // build LRU list
623 fPages[i].fPlotList.addToHead(currPlot->get());
624 ++currPlot;
625 }
626 }
627
628 }
629
630 return true;
631}
632
633
634bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500635 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500636
Greg Daniel9715b6c2019-12-10 15:03:10 -0500637 if (!fViews[fNumActivePages].proxy()->instantiate(resourceProvider)) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500638 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400639 }
640
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400641#ifdef DUMP_ATLAS_DATA
642 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500643 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400644 }
645#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500646
647 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400648 return true;
649}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400650
Robert Phillips4bc70112018-03-01 10:24:02 -0500651
652inline void GrDrawOpAtlas::deactivateLastPage() {
653 SkASSERT(fNumActivePages);
654
655 uint32_t lastPageIndex = fNumActivePages - 1;
656
657 int numPlotsX = fTextureWidth/fPlotWidth;
658 int numPlotsY = fTextureHeight/fPlotHeight;
659
Jim Van Verth106b5c42017-09-26 12:45:29 -0400660 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500661 for (int r = 0; r < numPlotsY; ++r) {
662 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500663 uint32_t plotIndex = r * numPlotsX + c;
664
665 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
666 currPlot->resetRects();
667 currPlot->resetFlushesSinceLastUsed();
668
669 // rebuild the LRU list
670 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
671 SkDEBUGCODE(currPlot->fList = nullptr);
672 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
673 }
674 }
675
676 // remove ref to the backing texture
Greg Daniel9715b6c2019-12-10 15:03:10 -0500677 fViews[lastPageIndex].proxy()->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500678 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400679}
Herb Derby15d9ef22018-10-18 13:41:32 -0400680
Jim Van Verthf6206f92018-12-14 08:22:24 -0500681GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
682 static const SkISize kARGBDimensions[] = {
683 {256, 256}, // maxBytes < 2^19
684 {512, 256}, // 2^19 <= maxBytes < 2^20
685 {512, 512}, // 2^20 <= maxBytes < 2^21
686 {1024, 512}, // 2^21 <= maxBytes < 2^22
687 {1024, 1024}, // 2^22 <= maxBytes < 2^23
688 {2048, 1024}, // 2^23 <= maxBytes
689 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400690
Jim Van Verthf6206f92018-12-14 08:22:24 -0500691 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
692 maxBytes >>= 18;
693 // Take the floor of the log to get the index
694 int index = maxBytes > 0
695 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
696 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400697
Jim Van Verthf6206f92018-12-14 08:22:24 -0500698 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
699 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
Brian Osman788b9162020-02-07 10:36:46 -0500700 fARGBDimensions.set(std::min<int>(kARGBDimensions[index].width(), maxTextureSize),
701 std::min<int>(kARGBDimensions[index].height(), maxTextureSize));
702 fMaxTextureSize = std::min<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400703}
704
705SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500706 if (kA8_GrMaskFormat == type) {
707 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
Brian Osman788b9162020-02-07 10:36:46 -0500708 return { std::min<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
709 std::min<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
Jim Van Verthf6206f92018-12-14 08:22:24 -0500710 } else {
711 return fARGBDimensions;
712 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400713}
714
Jim Van Verthf6206f92018-12-14 08:22:24 -0500715SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
716 if (kA8_GrMaskFormat == type) {
717 SkISize atlasDimensions = this->atlasDimensions(type);
718 // For A8 we want to grow the plots at larger texture sizes to accept more of the
719 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
720 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400721
Jim Van Verth578b0892018-12-20 20:48:55 +0000722 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
723 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500724 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000725 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400726
Jim Van Verthf6206f92018-12-14 08:22:24 -0500727 return { plotWidth, plotHeight };
728 } else {
729 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
730 return { 256, 256 };
731 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400732}
733
Jim Van Verthf6206f92018-12-14 08:22:24 -0500734constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;