blob: 1f4bb388cc6d4d803c0239b54a44bc6416087a9c [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"
13#include "src/gpu/GrOnFlushResourceProvider.h"
14#include "src/gpu/GrOpFlushState.h"
15#include "src/gpu/GrProxyProvider.h"
16#include "src/gpu/GrRectanizer.h"
17#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrSurfaceProxyPriv.h"
19#include "src/gpu/GrTracing.h"
joshualitt5bf99f12015-03-13 11:47:42 -070020
Robert Phillipscd5099c2018-02-09 09:56:56 -050021// When proxy allocation is deferred until flush time the proxies acting as atlases require
22// special handling. This is because the usage that can be determined from the ops themselves
23// isn't sufficient. Independent of the ops there will be ASAP and inline uploads to the
24// atlases. Extending the usage interval of any op that uses an atlas to the start of the
25// flush (as is done for proxies that are used for sw-generated masks) also won't work because
26// the atlas persists even beyond the last use in an op - for a given flush. Given this, atlases
27// must explicitly manage the lifetime of their backing proxies via the onFlushCallback system
28// (which calls this method).
29void GrDrawOpAtlas::instantiate(GrOnFlushResourceProvider* onFlushResourceProvider) {
Robert Phillips4bc70112018-03-01 10:24:02 -050030 for (uint32_t i = 0; i < fNumActivePages; ++i) {
31 // All the atlas pages are now instantiated at flush time in the activeNewPage method.
Brian Salomonfd98c2c2018-07-31 17:25:29 -040032 SkASSERT(fProxies[i] && fProxies[i]->isInstantiated());
Robert Phillipscd5099c2018-02-09 09:56:56 -050033 }
34}
35
Robert Phillips4bc70112018-03-01 10:24:02 -050036std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrProxyProvider* proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -050037 const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -040038 GrColorType colorType, int width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050039 int height, int plotWidth, int plotHeight,
Brian Salomon9f545bc2017-11-06 10:36:57 -050040 AllowMultitexturing allowMultitexturing,
41 GrDrawOpAtlas::EvictionFunc func, void* data) {
Robert Phillips0a15cc62019-07-30 12:49:10 -040042 if (!format.isValid()) {
43 return nullptr;
44 }
45
Robert Phillips42dda082019-05-14 13:29:45 -040046 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, colorType, width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050047 height, plotWidth, plotHeight,
Robert Phillips4bc70112018-03-01 10:24:02 -050048 allowMultitexturing));
Jim Van Vertha950b632017-09-12 11:54:11 -040049 if (!atlas->getProxies()[0]) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040050 return nullptr;
51 }
52
Robert Phillips256c37b2017-03-01 14:32:46 -050053 atlas->registerEvictionCallback(func, data);
54 return atlas;
55}
56
Jim Van Verthc3269ae2017-09-28 15:04:00 -040057#ifdef DUMP_ATLAS_DATA
58static bool gDumpAtlasData = false;
59#endif
Robert Phillips256c37b2017-03-01 14:32:46 -050060
joshualitt5df175e2015-11-18 13:37:54 -080061////////////////////////////////////////////////////////////////////////////////
Jim Van Vertha950b632017-09-12 11:54:11 -040062GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY,
Robert Phillips42dda082019-05-14 13:29:45 -040063 int width, int height, GrColorType colorType)
Brian Salomon943ed792017-10-30 09:37:55 -040064 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
65 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040066 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040067 , fPageIndex(pageIndex)
68 , fPlotIndex(plotIndex)
Brian Salomon2ee084e2016-12-16 18:59:19 -050069 , fGenID(genID)
Jim Van Vertha950b632017-09-12 11:54:11 -040070 , fID(CreateId(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -050071 , fData(nullptr)
72 , fWidth(width)
73 , fHeight(height)
74 , fX(offX)
75 , fY(offY)
76 , fRects(nullptr)
77 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
Robert Phillips42dda082019-05-14 13:29:45 -040078 , fColorType(colorType)
79 , fBytesPerPixel(GrColorTypeBytesPerPixel(colorType))
joshualitt5df175e2015-11-18 13:37:54 -080080#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050081 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080082#endif
83{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -050084 // We expect the allocated dimensions to be a multiple of 4 bytes
85 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
86 // The padding for faster uploads only works for 1, 2 and 4 byte texels
87 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -080088 fDirtyRect.setEmpty();
89}
joshualitt5bf99f12015-03-13 11:47:42 -070090
Brian Salomon2ee084e2016-12-16 18:59:19 -050091GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070092 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080093 delete fRects;
94}
joshualitt5bf99f12015-03-13 11:47:42 -070095
Brian Salomon2ee084e2016-12-16 18:59:19 -050096bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -080097 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070098
joshualitt5df175e2015-11-18 13:37:54 -080099 if (!fRects) {
100 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700101 }
102
joshualitt5df175e2015-11-18 13:37:54 -0800103 if (!fRects->addRect(width, height, loc)) {
104 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700105 }
joshualitt5bf99f12015-03-13 11:47:42 -0700106
jvanverthc3d706f2016-04-20 10:33:27 -0700107 if (!fData) {
108 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
109 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800110 }
111 size_t rowBytes = width * fBytesPerPixel;
112 const unsigned char* imagePtr = (const unsigned char*)image;
113 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700114 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800115 dataPtr += fBytesPerPixel * fWidth * loc->fY;
116 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400117 // copy into the data buffer, swizzling as we go if this is ARGB data
118 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
119 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400120 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400121 dataPtr += fBytesPerPixel * fWidth;
122 imagePtr += rowBytes;
123 }
124 } else {
125 for (int i = 0; i < height; ++i) {
126 memcpy(dataPtr, imagePtr, rowBytes);
127 dataPtr += fBytesPerPixel * fWidth;
128 imagePtr += rowBytes;
129 }
joshualitt5bf99f12015-03-13 11:47:42 -0700130 }
131
Mike Reed92b33352019-08-24 19:39:13 -0400132 fDirtyRect.join({loc->fX, loc->fY, loc->fX + width, loc->fY + height});
robertphillips2b0536f2015-11-06 14:10:42 -0800133
joshualitt5df175e2015-11-18 13:37:54 -0800134 loc->fX += fOffset.fX;
135 loc->fY += fOffset.fY;
136 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700137
joshualitt5df175e2015-11-18 13:37:54 -0800138 return true;
139}
joshualitt5bf99f12015-03-13 11:47:42 -0700140
Brian Salomon943ed792017-10-30 09:37:55 -0400141void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400142 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800143 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400144 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400145 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800146 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700147 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500148 // Clamp to 4-byte aligned boundaries
149 unsigned int clearBits = 0x3 / fBytesPerPixel;
150 fDirtyRect.fLeft &= ~clearBits;
151 fDirtyRect.fRight += clearBits;
152 fDirtyRect.fRight &= ~clearBits;
153 SkASSERT(fDirtyRect.fRight <= fWidth);
154 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700155 dataPtr += rowBytes * fDirtyRect.fTop;
156 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillips42dda082019-05-14 13:29:45 -0400157
Robert Phillipsacaa6072017-07-28 10:54:53 -0400158 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Robert Phillips42dda082019-05-14 13:29:45 -0400159 fDirtyRect.width(), fDirtyRect.height(), fColorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800160 fDirtyRect.setEmpty();
161 SkDEBUGCODE(fDirty = false;)
162}
163
Brian Salomon2ee084e2016-12-16 18:59:19 -0500164void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800165 if (fRects) {
166 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700167 }
168
joshualitt5df175e2015-11-18 13:37:54 -0800169 fGenID++;
Jim Van Vertha950b632017-09-12 11:54:11 -0400170 fID = CreateId(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400171 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
172 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800173
174 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700175 if (fData) {
176 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700177 }
178
joshualitt5df175e2015-11-18 13:37:54 -0800179 fDirtyRect.setEmpty();
180 SkDEBUGCODE(fDirty = false;)
181}
joshualitt5bf99f12015-03-13 11:47:42 -0700182
joshualitt5bf99f12015-03-13 11:47:42 -0700183///////////////////////////////////////////////////////////////////////////////
184
Greg Daniel4065d452018-11-16 15:43:41 -0500185GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
Robert Phillips42dda082019-05-14 13:29:45 -0400186 GrColorType colorType, int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500187 int plotWidth, int plotHeight, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500188 : fFormat(format)
Robert Phillips42dda082019-05-14 13:29:45 -0400189 , fColorType(colorType)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400190 , fTextureWidth(width)
191 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500192 , fPlotWidth(plotWidth)
193 , fPlotHeight(plotHeight)
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400194 , fAtlasGeneration(kInvalidAtlasGeneration + 1)
Brian Salomon943ed792017-10-30 09:37:55 -0400195 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500196 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500197 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500198 int numPlotsX = width/plotWidth;
199 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400200 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400201 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
202 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800203
Jim Van Verth06f593c2018-02-20 11:30:10 -0500204 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700205
Robert Phillips4bc70112018-03-01 10:24:02 -0500206 this->createPages(proxyProvider);
joshualitt5bf99f12015-03-13 11:47:42 -0700207}
208
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400209inline void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700210 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
211 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
212 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400213 ++fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700214}
215
Brian Salomon29b60c92017-10-31 14:42:10 -0400216inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target, AtlasID* id, Plot* plot) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400217 int pageIdx = GetPageIndexFromID(plot->id());
218 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700219
220 // If our most recent upload has already occurred then we have to insert a new
221 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
222 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500223 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700224 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500225 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500226
Jim Van Vertha950b632017-09-12 11:54:11 -0400227 GrTextureProxy* proxy = fProxies[pageIdx].get();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400228 SkASSERT(proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500229
Brian Salomon29b60c92017-10-31 14:42:10 -0400230 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400231 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
232 plotsp->uploadToTexture(writePixels, proxy);
233 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500234 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700235 }
236 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500237 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700238}
239
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500240bool GrDrawOpAtlas::uploadToPage(unsigned int pageIdx, AtlasID* id, GrDeferredUploadTarget* target,
241 int width, int height, const void* image, SkIPoint16* loc) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400242 SkASSERT(fProxies[pageIdx] && fProxies[pageIdx]->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500243
244 // look through all allocated plots for one we can share, in Most Recently Refed order
245 PlotList::Iter plotIter;
246 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
247
248 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
249 SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == plot->bpp());
250
251 if (plot->addSubImage(width, height, image, loc)) {
252 return this->updatePlot(target, id, plot);
253 }
254 }
255
256 return false;
257}
258
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400259// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
260//
261// This value is somewhat arbitrary -- the idea is to keep it low enough that
262// a page with unused plots will get removed reasonably quickly, but allow it
263// to hang around for a bit in case it's needed. The assumption is that flushes
264// are rare; i.e., we are not continually refreshing the frame.
Derek Sollenberger90196cc2017-10-09 15:00:33 -0400265static constexpr auto kRecentlyUsedCount = 256;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400266
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500267GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
268 AtlasID* id, GrDeferredUploadTarget* target,
269 int width, int height,
270 const void* image, SkIPoint16* loc) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700271 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500272 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700273 }
joshualitt5bf99f12015-03-13 11:47:42 -0700274
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400275 // Look through each page to see if we can upload without having to flush
276 // We prioritize this upload to the first pages, not the most recently used, to make it easier
277 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500278 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500279 if (this->uploadToPage(pageIdx, id, target, width, height, image, loc)) {
280 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400281 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400282 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400283
Jim Van Verth712fe732017-09-25 16:53:49 -0400284 // 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 -0400285 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
286 // We wait until we've grown to the full number of pages to begin evicting already flushed
287 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400288 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500289 if (fNumActivePages == this->maxPages()) {
290 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
291 Plot* plot = fPages[pageIdx].fPlotList.tail();
292 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500293 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500294 this->processEvictionAndResetRects(plot);
295 SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == plot->bpp());
296 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
297 SkASSERT(verify);
298 if (!this->updatePlot(target, id, plot)) {
299 return ErrorCode::kError;
300 }
301 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400302 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500303 }
304 } else {
305 // If we haven't activated all the available pages, try to create a new one and add to it
306 if (!this->activateNewPage(resourceProvider)) {
307 return ErrorCode::kError;
308 }
309
310 if (this->uploadToPage(fNumActivePages-1, id, target, width, height, image, loc)) {
311 return ErrorCode::kSucceeded;
312 } else {
313 // If we fail to upload to a newly activated page then something has gone terribly
314 // wrong - return an error
315 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400316 }
317 }
318
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500319 if (!fNumActivePages) {
320 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700321 }
322
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400323 // Try to find a plot that we can perform an inline upload to.
324 // We prioritize this upload in reverse order of pages to counterbalance the order above.
325 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500326 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400327 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500328 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400329 plot = currentPlot;
330 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500331 }
joshualitt5bf99f12015-03-13 11:47:42 -0700332 }
333
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400334 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
335 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
336 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
337 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500338 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400339 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500340 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700341 }
342
joshualitt5bf99f12015-03-13 11:47:42 -0700343 this->processEviction(plot->id());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400344 int pageIdx = GetPageIndexFromID(plot->id());
Jim Van Vertha950b632017-09-12 11:54:11 -0400345 fPages[pageIdx].fPlotList.remove(plot);
346 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800347 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700348
Jim Van Vertha950b632017-09-12 11:54:11 -0400349 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
350 SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800351 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700352 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800353
robertphillips1f0e3502015-11-10 10:19:50 -0800354 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400355 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500356 // With c+14 we could move sk_sp into lambda to only ref once.
357 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500358
Jim Van Vertha950b632017-09-12 11:54:11 -0400359 GrTextureProxy* proxy = fProxies[pageIdx].get();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400360 SkASSERT(proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700361
Brian Salomon943ed792017-10-30 09:37:55 -0400362 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
363 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
364 plotsp->uploadToTexture(writePixels, proxy);
365 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500366 newPlot->setLastUploadToken(lastUploadToken);
367
joshualitt5bf99f12015-03-13 11:47:42 -0700368 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800369
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500370 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700371}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400372
Brian Salomon943ed792017-10-30 09:37:55 -0400373void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500374 if (fNumActivePages <= 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400375 fPrevFlushToken = startTokenForNextFlush;
376 return;
377 }
378
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400379 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400380 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400381 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500382 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400383 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
384 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400385 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400386 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
387 plot->resetFlushesSinceLastUsed();
388 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400389 }
390
391 plotIter.next();
392 }
393 }
394
395 // We only try to compact if the atlas was used in the recently completed flush.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400396 // This is to handle the case where a lot of text or path rendering has occurred but then just
397 // a blinking cursor is drawn.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400398 // TODO: consider if we should also do this if it's been a long time since the last atlas use
399 if (atlasUsedThisFlush) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500400 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500401 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400402
403 // For all plots but the last one, update number of flushes since used, and check to see
404 // if there are any in the first pages that the last page can safely upload to.
405 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400406#ifdef DUMP_ATLAS_DATA
407 if (gDumpAtlasData) {
408 SkDebugf("page %d: ", pageIndex);
409 }
410#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400411 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
412 while (Plot* plot = plotIter.get()) {
413 // Update number of flushes since plot was last used
414 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
415 // to avoid deleting everything when we return to text drawing in the blinking
416 // cursor case
417 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
418 plot->incFlushesSinceLastUsed();
419 }
420
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400421#ifdef DUMP_ATLAS_DATA
422 if (gDumpAtlasData) {
423 SkDebugf("%d ", plot->flushesSinceLastUsed());
424 }
425#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400426 // Count plots we can potentially upload to in all pages except the last one
427 // (the potential compactee).
428 if (plot->flushesSinceLastUsed() > kRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500429 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400430 }
431
432 plotIter.next();
433 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400434#ifdef DUMP_ATLAS_DATA
435 if (gDumpAtlasData) {
436 SkDebugf("\n");
437 }
438#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400439 }
440
Jim Van Verth06f593c2018-02-20 11:30:10 -0500441 // Count recently used plots in the last page and evict any that are no longer in use.
442 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400443 // clear out usage of this page unless we have a large need.
444 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500445 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400446#ifdef DUMP_ATLAS_DATA
447 if (gDumpAtlasData) {
448 SkDebugf("page %d: ", lastPageIndex);
449 }
450#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400451 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400452 // Update number of flushes since plot was last used
453 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
454 plot->incFlushesSinceLastUsed();
455 }
456
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400457#ifdef DUMP_ATLAS_DATA
458 if (gDumpAtlasData) {
459 SkDebugf("%d ", plot->flushesSinceLastUsed());
460 }
461#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400462 // If this plot was used recently
463 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
464 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400465 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400466 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400467 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400468 }
469 plotIter.next();
470 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400471#ifdef DUMP_ATLAS_DATA
472 if (gDumpAtlasData) {
473 SkDebugf("\n");
474 }
475#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500476
477 // If recently used plots in the last page are using less than a quarter of the page, try
478 // to evict them if there's available space in earlier pages. Since we prioritize uploading
479 // to the first pages, this will eventually clear out usage of this page unless we have a
480 // large need.
481 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
482 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
483 while (Plot* plot = plotIter.get()) {
484 // If this plot was used recently
485 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
486 // See if there's room in an earlier page and if so evict.
487 // We need to be somewhat harsh here so that a handful of plots that are
488 // consistently in use don't end up locking the page in memory.
489 if (availablePlots.count() > 0) {
490 this->processEvictionAndResetRects(plot);
491 this->processEvictionAndResetRects(availablePlots.back());
492 availablePlots.pop_back();
493 --usedPlots;
494 }
495 if (!usedPlots || !availablePlots.count()) {
496 break;
497 }
498 }
499 plotIter.next();
500 }
501 }
502
Jim Van Verth106b5c42017-09-26 12:45:29 -0400503 // If none of the plots in the last page have been used recently, delete it.
504 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400505#ifdef DUMP_ATLAS_DATA
506 if (gDumpAtlasData) {
507 SkDebugf("delete %d\n", fNumPages-1);
508 }
509#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500510 this->deactivateLastPage();
Jim Van Verth106b5c42017-09-26 12:45:29 -0400511 }
512 }
513
514 fPrevFlushToken = startTokenForNextFlush;
515}
516
Robert Phillips4bc70112018-03-01 10:24:02 -0500517bool GrDrawOpAtlas::createPages(GrProxyProvider* proxyProvider) {
518 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500519
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400520 GrSurfaceDesc desc;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400521 desc.fWidth = fTextureWidth;
522 desc.fHeight = fTextureHeight;
Greg Daniele877dce2019-07-11 10:52:43 -0400523 desc.fConfig = GrColorTypeToPixelConfig(fColorType);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400524
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400525 int numPlotsX = fTextureWidth/fPlotWidth;
526 int numPlotsY = fTextureHeight/fPlotHeight;
527
Robert Phillips4bc70112018-03-01 10:24:02 -0500528 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400529 fProxies[i] = proxyProvider->createProxy(
530 fFormat, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
531 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo,
532 GrInternalSurfaceFlags::kNone, GrSurfaceProxy::UseAllocator::kNo);
Robert Phillips4bc70112018-03-01 10:24:02 -0500533 if (!fProxies[i]) {
534 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400535 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500536
537 // set up allocated plots
538 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
539
540 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
541 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
542 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
543 uint32_t plotIndex = r * numPlotsX + c;
544 currPlot->reset(new Plot(i, plotIndex, 1, x, y, fPlotWidth, fPlotHeight,
Robert Phillips42dda082019-05-14 13:29:45 -0400545 fColorType));
Robert Phillips4bc70112018-03-01 10:24:02 -0500546
547 // build LRU list
548 fPages[i].fPlotList.addToHead(currPlot->get());
549 ++currPlot;
550 }
551 }
552
553 }
554
555 return true;
556}
557
558
559bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500560 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500561
562 if (!fProxies[fNumActivePages]->instantiate(resourceProvider)) {
563 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400564 }
565
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400566#ifdef DUMP_ATLAS_DATA
567 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500568 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400569 }
570#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500571
572 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400573 return true;
574}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400575
Robert Phillips4bc70112018-03-01 10:24:02 -0500576
577inline void GrDrawOpAtlas::deactivateLastPage() {
578 SkASSERT(fNumActivePages);
579
580 uint32_t lastPageIndex = fNumActivePages - 1;
581
582 int numPlotsX = fTextureWidth/fPlotWidth;
583 int numPlotsY = fTextureHeight/fPlotHeight;
584
Jim Van Verth106b5c42017-09-26 12:45:29 -0400585 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500586 for (int r = 0; r < numPlotsY; ++r) {
587 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500588 uint32_t plotIndex = r * numPlotsX + c;
589
590 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
591 currPlot->resetRects();
592 currPlot->resetFlushesSinceLastUsed();
593
594 // rebuild the LRU list
595 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
596 SkDEBUGCODE(currPlot->fList = nullptr);
597 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
598 }
599 }
600
601 // remove ref to the backing texture
Brian Salomon967df202018-12-07 11:15:53 -0500602 fProxies[lastPageIndex]->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500603 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400604}
Herb Derby15d9ef22018-10-18 13:41:32 -0400605
Jim Van Verthf6206f92018-12-14 08:22:24 -0500606GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
607 static const SkISize kARGBDimensions[] = {
608 {256, 256}, // maxBytes < 2^19
609 {512, 256}, // 2^19 <= maxBytes < 2^20
610 {512, 512}, // 2^20 <= maxBytes < 2^21
611 {1024, 512}, // 2^21 <= maxBytes < 2^22
612 {1024, 1024}, // 2^22 <= maxBytes < 2^23
613 {2048, 1024}, // 2^23 <= maxBytes
614 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400615
Jim Van Verthf6206f92018-12-14 08:22:24 -0500616 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
617 maxBytes >>= 18;
618 // Take the floor of the log to get the index
619 int index = maxBytes > 0
620 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
621 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400622
Jim Van Verthf6206f92018-12-14 08:22:24 -0500623 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
624 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
625 fARGBDimensions.set(SkTMin<int>(kARGBDimensions[index].width(), maxTextureSize),
626 SkTMin<int>(kARGBDimensions[index].height(), maxTextureSize));
627 fMaxTextureSize = SkTMin<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400628}
629
630SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500631 if (kA8_GrMaskFormat == type) {
632 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
633 return { SkTMin<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
634 SkTMin<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
635 } else {
636 return fARGBDimensions;
637 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400638}
639
Jim Van Verthf6206f92018-12-14 08:22:24 -0500640SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
641 if (kA8_GrMaskFormat == type) {
642 SkISize atlasDimensions = this->atlasDimensions(type);
643 // For A8 we want to grow the plots at larger texture sizes to accept more of the
644 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
645 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400646
Jim Van Verth578b0892018-12-20 20:48:55 +0000647 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
648 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500649 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000650 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400651
Jim Van Verthf6206f92018-12-14 08:22:24 -0500652 return { plotWidth, plotHeight };
653 } else {
654 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
655 return { 256, 256 };
656 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400657}
658
Jim Van Verthf6206f92018-12-14 08:22:24 -0500659constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;