blob: b41d34b14152f99b81671f4f2f08c4fd2ef2c643 [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"
11#include "include/gpu/GrTexture.h"
12#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"
17#include "src/gpu/GrRectanizer.h"
18#include "src/gpu/GrResourceProvider.h"
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040019#include "src/gpu/GrResourceProviderPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrSurfaceProxyPriv.h"
21#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070022
Robert Phillipscd5099c2018-02-09 09:56:56 -050023// When proxy allocation is deferred until flush time the proxies acting as atlases require
24// special handling. This is because the usage that can be determined from the ops themselves
25// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
26// atlases. Extending the usage interval of any op that uses an atlas to the start of the
27// flush (as is done for proxies that are used for sw-generated masks) also won't work because
28// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
29// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
30// (which calls this method).
31void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050032 for (uint32_t i = 0; i < fNumActivePages; ++i) {
33 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Brian Salomonfd98c2c2018-07-31 17:25:29 -040034 SkASSERT(fProxies[i] && fProxies[i]->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050035 }
36}
37
Robert Phillips4bc70112018-03-01 10:24:02 -050038std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050039 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040040 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050041 int height, int plotWidth, int plotHeight,
Brian Salomon9f545bc2017-11-06 10:36:57 -050042 AllowMultitexturing allowMultitexturing,
43 GrDrawOpAtlas::EvictionFunc func, void* data) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040044 if (!format.isValid()) {
45 return nullptr;
46 }
47
Robert Phillips42dda082019-05-14 13:29:45 -040048 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType, width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050049 height, plotWidth, plotHeight,
Robert Phillips4bc70112018-03-01 10:24:02 -050050 allowMultitexturing));
Jim Van Vertha950b632017-09-12 11:54:11 -040051 if (!atlas->getProxies()[0]) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040052 return nullptr;
53 }
54
Robert Phillips256c37b2017-03-01 14:32:46 -050055 atlas->registerEvictionCallback(func, data);
56 return atlas;
57}
58
Jim Van Verthc3269ae2017-09-28 15:04:00 -040059#ifdef DUMP_ATLAS_DATA
60static bool gDumpAtlasData = false;
61#endif
Robert Phillips256c37b2017-03-01 14:32:46 -050062
joshualitt5df175e2015-11-18 13:37:54 -080063////////////////////////////////////////////////////////////////////////////////
Jim Van Vertha950b632017-09-12 11:54:11 -040064GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY,
Robert Phillips42dda082019-05-14 13:29:45 -040065 int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040066 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
67 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040068 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040069 , fPageIndex(pageIndex)
70 , fPlotIndex(plotIndex)
Brian Salomon2ee084e2016-12-16 18:59:19 -050071 , fGenID(genID)
Jim Van Vertha950b632017-09-12 11:54:11 -040072 , fID(CreateId(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -050073 , fData(nullptr)
74 , fWidth(width)
75 , fHeight(height)
76 , fX(offX)
77 , fY(offY)
78 , fRects(nullptr)
79 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -040080 , fColorType(colorType)
81 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -080082#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050083 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080084#endif
85{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -050086 // We expect the allocated dimensions to be a multiple of 4 bytes
87 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
88 // The padding for faster uploads only works for 1, 2 and 4 byte texels
89 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -080090 fDirtyRect.setEmpty();
91}
joshualitt5bf99f12015-03-13 11:47:42 -070092
Brian Salomon2ee084e2016-12-16 18:59:19 -050093GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070094 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080095 delete fRects;
96}
joshualitt5bf99f12015-03-13 11:47:42 -070097
Brian Salomon2ee084e2016-12-16 18:59:19 -050098bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -080099 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700100
joshualitt5df175e2015-11-18 13:37:54 -0800101 if (!fRects) {
102 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700103 }
104
joshualitt5df175e2015-11-18 13:37:54 -0800105 if (!fRects->addRect(width, height, loc)) {
106 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700107 }
joshualitt5bf99f12015-03-13 11:47:42 -0700108
jvanverthc3d706f2016-04-20 10:33:27 -0700109 if (!fData) {
110 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
111 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800112 }
113 size_t rowBytes = width * fBytesPerPixel;
114 const unsigned char* imagePtr = (const unsigned char*)image;
115 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700116 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800117 dataPtr += fBytesPerPixel * fWidth * loc->fY;
118 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400119 // copy into the data buffer, swizzling as we go if this is ARGB data
120 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
121 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400122 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400123 dataPtr += fBytesPerPixel * fWidth;
124 imagePtr += rowBytes;
125 }
126 } else {
127 for (int i = 0; i < height; ++i) {
128 memcpy(dataPtr, imagePtr, rowBytes);
129 dataPtr += fBytesPerPixel * fWidth;
130 imagePtr += rowBytes;
131 }
joshualitt5bf99f12015-03-13 11:47:42 -0700132 }
133
Mike Reed92b33352019-08-24 19:39:13 -0400134 fDirtyRect.join({loc->fX, loc->fY, loc->fX + width, loc->fY + height});
robertphillips2b0536f2015-11-06 14:10:42 -0800135
joshualitt5df175e2015-11-18 13:37:54 -0800136 loc->fX += fOffset.fX;
137 loc->fY += fOffset.fY;
138 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700139
joshualitt5df175e2015-11-18 13:37:54 -0800140 return true;
141}
joshualitt5bf99f12015-03-13 11:47:42 -0700142
Brian Salomon943ed792017-10-30 09:37:55 -0400143void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400144 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800145 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400146 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400147 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800148 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700149 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500150 // Clamp to 4-byte aligned boundaries
151 unsigned int clearBits = 0x3 / fBytesPerPixel;
152 fDirtyRect.fLeft &= ~clearBits;
153 fDirtyRect.fRight += clearBits;
154 fDirtyRect.fRight &= ~clearBits;
155 SkASSERT(fDirtyRect.fRight <= fWidth);
156 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700157 dataPtr += rowBytes * fDirtyRect.fTop;
158 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400159
Robert Phillipsacaa6072017-07-28 10:54:53 -0400160 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400161 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800162 fDirtyRect.setEmpty();
163 SkDEBUGCODE(fDirty = false;)
164}
165
Brian Salomon2ee084e2016-12-16 18:59:19 -0500166void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800167 if (fRects) {
168 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700169 }
170
joshualitt5df175e2015-11-18 13:37:54 -0800171 fGenID++;
Jim Van Vertha950b632017-09-12 11:54:11 -0400172 fID = CreateId(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400173 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
174 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800175
176 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700177 if (fData) {
178 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700179 }
180
joshualitt5df175e2015-11-18 13:37:54 -0800181 fDirtyRect.setEmpty();
182 SkDEBUGCODE(fDirty = false;)
183}
joshualitt5bf99f12015-03-13 11:47:42 -0700184
joshualitt5bf99f12015-03-13 11:47:42 -0700185///////////////////////////////////////////////////////////////////////////////
186
Greg Daniel4065d452018-11-16 15:43:41 -0500187GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -0400188 GrColorType colorType, int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500189 int plotWidth, int plotHeight, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500190 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400191 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400192 , fTextureWidth(width)
193 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500194 , fPlotWidth(plotWidth)
195 , fPlotHeight(plotHeight)
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400196 , fAtlasGeneration(kInvalidAtlasGeneration + 1)
Brian Salomon943ed792017-10-30 09:37:55 -0400197 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500198 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500199 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500200 int numPlotsX = width/plotWidth;
201 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400202 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400203 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
204 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800205
Jim Van Verth06f593c2018-02-20 11:30:10 -0500206 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700207
Robert Phillips4bc70112018-03-01 10:24:02 -0500208 this->createPages(proxyProvider);
joshualitt5bf99f12015-03-13 11:47:42 -0700209}
210
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400211inline void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700212 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
213 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
214 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400215 ++fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700216}
217
Brian Salomon29b60c92017-10-31 14:42:10 -0400218inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target, AtlasID* id, Plot* plot) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400219 int pageIdx = GetPageIndexFromID(plot->id());
220 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700221
222 // If our most recent upload has already occurred then we have to insert a new
223 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
224 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500225 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700226 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500227 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500228
Jim Van Vertha950b632017-09-12 11:54:11 -0400229 GrTextureProxy* proxy = fProxies[pageIdx].get();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400230 SkASSERT(proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500231
Brian Salomon29b60c92017-10-31 14:42:10 -0400232 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400233 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
234 plotsp->uploadToTexture(writePixels, proxy);
235 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500236 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700237 }
238 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500239 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700240}
241
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400242bool GrDrawOpAtlas::uploadToPage(const GrCaps& caps, unsigned int pageIdx, AtlasID* id,
243 GrDeferredUploadTarget* target, int width, int height,
244 const void* image, SkIPoint16* loc) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400245 SkASSERT(fProxies[pageIdx] && fProxies[pageIdx]->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500246
247 // look through all allocated plots for one we can share, in Most Recently Refed order
248 PlotList::Iter plotIter;
249 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
250
251 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400252 SkASSERT(caps.bytesPerPixel(fProxies[pageIdx]->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500253
254 if (plot->addSubImage(width, height, image, loc)) {
255 return this->updatePlot(target, id, plot);
256 }
257 }
258
259 return false;
260}
261
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400262// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
263//
264// This value is somewhat arbitrary -- the idea is to keep it low enough that
265// a page with unused plots will get removed reasonably quickly, but allow it
266// to hang around for a bit in case it's needed. The assumption is that flushes
267// are rare; i.e., we are not continually refreshing the frame.
Derek Sollenberger90196cc2017-10-09 15:00:33 -0400268static constexpr auto kRecentlyUsedCount = 256;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400269
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500270GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
271 AtlasID* id, GrDeferredUploadTarget* target,
272 int width, int height,
273 const void* image, SkIPoint16* loc) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700274 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500275 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700276 }
joshualitt5bf99f12015-03-13 11:47:42 -0700277
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400278 const GrCaps& caps = *resourceProvider->caps();
279
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400280 // Look through each page to see if we can upload without having to flush
281 // We prioritize this upload to the first pages, not the most recently used, to make it easier
282 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500283 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400284 if (this->uploadToPage(caps, pageIdx, id, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500285 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400286 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400287 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400288
Jim Van Verth712fe732017-09-25 16:53:49 -0400289 // 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 -0400290 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
291 // We wait until we've grown to the full number of pages to begin evicting already flushed
292 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400293 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500294 if (fNumActivePages == this->maxPages()) {
295 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
296 Plot* plot = fPages[pageIdx].fPlotList.tail();
297 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500298 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500299 this->processEvictionAndResetRects(plot);
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400300 SkASSERT(caps.bytesPerPixel(fProxies[pageIdx]->backendFormat()) == plot->bpp());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500301 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
302 SkASSERT(verify);
303 if (!this->updatePlot(target, id, plot)) {
304 return ErrorCode::kError;
305 }
306 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400307 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500308 }
309 } else {
310 // If we haven't activated all the available pages, try to create a new one and add to it
311 if (!this->activateNewPage(resourceProvider)) {
312 return ErrorCode::kError;
313 }
314
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400315 if (this->uploadToPage(caps, fNumActivePages-1, id, target, width, height, image, loc)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500316 return ErrorCode::kSucceeded;
317 } else {
318 // If we fail to upload to a newly activated page then something has gone terribly
319 // wrong - return an error
320 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400321 }
322 }
323
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500324 if (!fNumActivePages) {
325 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700326 }
327
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400328 // Try to find a plot that we can perform an inline upload to.
329 // We prioritize this upload in reverse order of pages to counterbalance the order above.
330 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500331 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400332 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500333 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400334 plot = currentPlot;
335 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500336 }
joshualitt5bf99f12015-03-13 11:47:42 -0700337 }
338
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400339 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
340 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
341 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
342 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500343 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400344 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500345 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700346 }
347
joshualitt5bf99f12015-03-13 11:47:42 -0700348 this->processEviction(plot->id());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400349 int pageIdx = GetPageIndexFromID(plot->id());
Jim Van Vertha950b632017-09-12 11:54:11 -0400350 fPages[pageIdx].fPlotList.remove(plot);
351 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800352 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700353
Jim Van Vertha950b632017-09-12 11:54:11 -0400354 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400355 SkASSERT(caps.bytesPerPixel(fProxies[pageIdx]->backendFormat()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800356 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700357 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800358
robertphillips1f0e3502015-11-10 10:19:50 -0800359 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400360 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500361 // With c+14 we could move sk_sp into lambda to only ref once.
362 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500363
Jim Van Vertha950b632017-09-12 11:54:11 -0400364 GrTextureProxy* proxy = fProxies[pageIdx].get();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400365 SkASSERT(proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700366
Brian Salomon943ed792017-10-30 09:37:55 -0400367 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
368 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
369 plotsp->uploadToTexture(writePixels, proxy);
370 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500371 newPlot->setLastUploadToken(lastUploadToken);
372
joshualitt5bf99f12015-03-13 11:47:42 -0700373 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800374
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500375 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700376}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400377
Brian Salomon943ed792017-10-30 09:37:55 -0400378void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500379 if (fNumActivePages <= 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400380 fPrevFlushToken = startTokenForNextFlush;
381 return;
382 }
383
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400384 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400385 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400386 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500387 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400388 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
389 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400390 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400391 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
392 plot->resetFlushesSinceLastUsed();
393 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400394 }
395
396 plotIter.next();
397 }
398 }
399
400 // We only try to compact if the atlas was used in the recently completed flush.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400401 // This is to handle the case where a lot of text or path rendering has occurred but then just
402 // a blinking cursor is drawn.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400403 // TODO: consider if we should also do this if it's been a long time since the last atlas use
404 if (atlasUsedThisFlush) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500405 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500406 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400407
408 // For all plots but the last one, update number of flushes since used, and check to see
409 // if there are any in the first pages that the last page can safely upload to.
410 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400411#ifdef DUMP_ATLAS_DATA
412 if (gDumpAtlasData) {
413 SkDebugf("page %d: ", pageIndex);
414 }
415#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400416 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
417 while (Plot* plot = plotIter.get()) {
418 // Update number of flushes since plot was last used
419 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
420 // to avoid deleting everything when we return to text drawing in the blinking
421 // cursor case
422 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
423 plot->incFlushesSinceLastUsed();
424 }
425
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400426#ifdef DUMP_ATLAS_DATA
427 if (gDumpAtlasData) {
428 SkDebugf("%d ", plot->flushesSinceLastUsed());
429 }
430#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400431 // Count plots we can potentially upload to in all pages except the last one
432 // (the potential compactee).
433 if (plot->flushesSinceLastUsed() > kRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500434 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400435 }
436
437 plotIter.next();
438 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400439#ifdef DUMP_ATLAS_DATA
440 if (gDumpAtlasData) {
441 SkDebugf("\n");
442 }
443#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400444 }
445
Jim Van Verth06f593c2018-02-20 11:30:10 -0500446 // Count recently used plots in the last page and evict any that are no longer in use.
447 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400448 // clear out usage of this page unless we have a large need.
449 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500450 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400451#ifdef DUMP_ATLAS_DATA
452 if (gDumpAtlasData) {
453 SkDebugf("page %d: ", lastPageIndex);
454 }
455#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400456 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400457 // Update number of flushes since plot was last used
458 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
459 plot->incFlushesSinceLastUsed();
460 }
461
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400462#ifdef DUMP_ATLAS_DATA
463 if (gDumpAtlasData) {
464 SkDebugf("%d ", plot->flushesSinceLastUsed());
465 }
466#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400467 // If this plot was used recently
468 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
469 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400470 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400471 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400472 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400473 }
474 plotIter.next();
475 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400476#ifdef DUMP_ATLAS_DATA
477 if (gDumpAtlasData) {
478 SkDebugf("\n");
479 }
480#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500481
482 // If recently used plots in the last page are using less than a quarter of the page, try
483 // to evict them if there's available space in earlier pages. Since we prioritize uploading
484 // to the first pages, this will eventually clear out usage of this page unless we have a
485 // large need.
486 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
487 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
488 while (Plot* plot = plotIter.get()) {
489 // If this plot was used recently
490 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
491 // See if there's room in an earlier page and if so evict.
492 // We need to be somewhat harsh here so that a handful of plots that are
493 // consistently in use don't end up locking the page in memory.
494 if (availablePlots.count() > 0) {
495 this->processEvictionAndResetRects(plot);
496 this->processEvictionAndResetRects(availablePlots.back());
497 availablePlots.pop_back();
498 --usedPlots;
499 }
500 if (!usedPlots || !availablePlots.count()) {
501 break;
502 }
503 }
504 plotIter.next();
505 }
506 }
507
Jim Van Verth106b5c42017-09-26 12:45:29 -0400508 // If none of the plots in the last page have been used recently, delete it.
509 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400510#ifdef DUMP_ATLAS_DATA
511 if (gDumpAtlasData) {
512 SkDebugf("delete %d\n", fNumPages-1);
513 }
514#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500515 this->deactivateLastPage();
Jim Van Verth106b5c42017-09-26 12:45:29 -0400516 }
517 }
518
519 fPrevFlushToken = startTokenForNextFlush;
520}
521
Robert Phillips4bc70112018-03-01 10:24:02 -0500522bool GrDrawOpAtlas::createPages(GrProxyProvider* proxyProvider) {
523 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500524
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400525 GrSurfaceDesc desc;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400526 desc.fWidth = fTextureWidth;
527 desc.fHeight = fTextureHeight;
Greg Daniele877dce2019-07-11 10:52:43 -0400528 desc.fConfig = GrColorTypeToPixelConfig(fColorType);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400529
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400530 int numPlotsX = fTextureWidth/fPlotWidth;
531 int numPlotsY = fTextureHeight/fPlotHeight;
532
Robert Phillips4bc70112018-03-01 10:24:02 -0500533 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400534 fProxies[i] = proxyProvider->createProxy(
535 fFormat, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
536 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo,
537 GrInternalSurfaceFlags::kNone, GrSurfaceProxy::UseAllocator::kNo);
Robert Phillips4bc70112018-03-01 10:24:02 -0500538 if (!fProxies[i]) {
539 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400540 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500541
542 // set up allocated plots
543 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
544
545 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
546 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
547 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
548 uint32_t plotIndex = r * numPlotsX + c;
549 currPlot->reset(new Plot(i, plotIndex, 1, x, y, fPlotWidth, fPlotHeight,
Robert Phillips42dda082019-05-14 13:29:45 -0400550 fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500551
552 // build LRU list
553 fPages[i].fPlotList.addToHead(currPlot->get());
554 ++currPlot;
555 }
556 }
557
558 }
559
560 return true;
561}
562
563
564bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500565 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500566
567 if (!fProxies[fNumActivePages]->instantiate(resourceProvider)) {
568 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400569 }
570
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400571#ifdef DUMP_ATLAS_DATA
572 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500573 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400574 }
575#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500576
577 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400578 return true;
579}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400580
Robert Phillips4bc70112018-03-01 10:24:02 -0500581
582inline void GrDrawOpAtlas::deactivateLastPage() {
583 SkASSERT(fNumActivePages);
584
585 uint32_t lastPageIndex = fNumActivePages - 1;
586
587 int numPlotsX = fTextureWidth/fPlotWidth;
588 int numPlotsY = fTextureHeight/fPlotHeight;
589
Jim Van Verth106b5c42017-09-26 12:45:29 -0400590 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500591 for (int r = 0; r < numPlotsY; ++r) {
592 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500593 uint32_t plotIndex = r * numPlotsX + c;
594
595 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
596 currPlot->resetRects();
597 currPlot->resetFlushesSinceLastUsed();
598
599 // rebuild the LRU list
600 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
601 SkDEBUGCODE(currPlot->fList = nullptr);
602 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
603 }
604 }
605
606 // remove ref to the backing texture
Brian Salomon967df202018-12-07 11:15:53 -0500607 fProxies[lastPageIndex]->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500608 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400609}
Herb Derby15d9ef22018-10-18 13:41:32 -0400610
Jim Van Verthf6206f92018-12-14 08:22:24 -0500611GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
612 static const SkISize kARGBDimensions[] = {
613 {256, 256}, // maxBytes < 2^19
614 {512, 256}, // 2^19 <= maxBytes < 2^20
615 {512, 512}, // 2^20 <= maxBytes < 2^21
616 {1024, 512}, // 2^21 <= maxBytes < 2^22
617 {1024, 1024}, // 2^22 <= maxBytes < 2^23
618 {2048, 1024}, // 2^23 <= maxBytes
619 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400620
Jim Van Verthf6206f92018-12-14 08:22:24 -0500621 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
622 maxBytes >>= 18;
623 // Take the floor of the log to get the index
624 int index = maxBytes > 0
625 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
626 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400627
Jim Van Verthf6206f92018-12-14 08:22:24 -0500628 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
629 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
630 fARGBDimensions.set(SkTMin<int>(kARGBDimensions[index].width(), maxTextureSize),
631 SkTMin<int>(kARGBDimensions[index].height(), maxTextureSize));
632 fMaxTextureSize = SkTMin<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400633}
634
635SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500636 if (kA8_GrMaskFormat == type) {
637 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
638 return { SkTMin<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
639 SkTMin<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
640 } else {
641 return fARGBDimensions;
642 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400643}
644
Jim Van Verthf6206f92018-12-14 08:22:24 -0500645SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
646 if (kA8_GrMaskFormat == type) {
647 SkISize atlasDimensions = this->atlasDimensions(type);
648 // For A8 we want to grow the plots at larger texture sizes to accept more of the
649 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
650 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400651
Jim Van Verth578b0892018-12-20 20:48:55 +0000652 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
653 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500654 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000655 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400656
Jim Van Verthf6206f92018-12-14 08:22:24 -0500657 return { plotWidth, plotHeight };
658 } else {
659 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
660 return { 256, 256 };
661 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400662}
663
Jim Van Verthf6206f92018-12-14 08:22:24 -0500664constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;