blob: 94afadc5e83541e2a48514fad387fe3dee10134f [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
Brian Salomon903da792016-12-16 14:24:46 -05008#include "GrDrawOpAtlas.h"
Robert Phillips32f28182017-02-28 16:20:03 -05009
10#include "GrContext.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050011#include "GrContextPriv.h"
Robert Phillipscd5099c2018-02-09 09:56:56 -050012#include "GrOnFlushResourceProvider.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050013#include "GrOpFlushState.h"
joshualitt5bf99f12015-03-13 11:47:42 -070014#include "GrRectanizer.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050015#include "GrProxyProvider.h"
Robert Phillips256c37b2017-03-01 14:32:46 -050016#include "GrResourceProvider.h"
Robert Phillipsc4039ea2018-03-01 11:36:45 -050017#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040018#include "GrTexture.h"
joshualitt5bf99f12015-03-13 11:47:42 -070019#include "GrTracing.h"
20
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 Phillips4bc70112018-03-01 10:24:02 -050038 GrPixelConfig config, 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) {
Greg Daniel4065d452018-11-16 15:43:41 -050042 std::unique_ptr<GrDrawOpAtlas> atlas(new GrDrawOpAtlas(proxyProvider, format, config, width,
Jim Van Verthf6206f92018-12-14 08:22:24 -050043 height, plotWidth, plotHeight,
Robert Phillips4bc70112018-03-01 10:24:02 -050044 allowMultitexturing));
Jim Van Vertha950b632017-09-12 11:54:11 -040045 if (!atlas->getProxies()[0]) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040046 return nullptr;
47 }
48
Robert Phillips256c37b2017-03-01 14:32:46 -050049 atlas->registerEvictionCallback(func, data);
50 return atlas;
51}
52
Jim Van Verthc3269ae2017-09-28 15:04:00 -040053#ifdef DUMP_ATLAS_DATA
54static bool gDumpAtlasData = false;
55#endif
Robert Phillips256c37b2017-03-01 14:32:46 -050056
joshualitt5df175e2015-11-18 13:37:54 -080057////////////////////////////////////////////////////////////////////////////////
Jim Van Vertha950b632017-09-12 11:54:11 -040058GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY,
59 int width, int height, GrPixelConfig config)
Brian Salomon943ed792017-10-30 09:37:55 -040060 : fLastUpload(GrDeferredUploadToken::AlreadyFlushedToken())
61 , fLastUse(GrDeferredUploadToken::AlreadyFlushedToken())
Jim Van Verth106b5c42017-09-26 12:45:29 -040062 , fFlushesSinceLastUse(0)
Jim Van Vertha950b632017-09-12 11:54:11 -040063 , fPageIndex(pageIndex)
64 , fPlotIndex(plotIndex)
Brian Salomon2ee084e2016-12-16 18:59:19 -050065 , fGenID(genID)
Jim Van Vertha950b632017-09-12 11:54:11 -040066 , fID(CreateId(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -050067 , fData(nullptr)
68 , fWidth(width)
69 , fHeight(height)
70 , fX(offX)
71 , fY(offY)
72 , fRects(nullptr)
73 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
74 , fConfig(config)
75 , fBytesPerPixel(GrBytesPerPixel(config))
joshualitt5df175e2015-11-18 13:37:54 -080076#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050077 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080078#endif
79{
Jim Van Vertha8c55fa2018-02-20 15:38:08 -050080 // We expect the allocated dimensions to be a multiple of 4 bytes
81 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
82 // The padding for faster uploads only works for 1, 2 and 4 byte texels
83 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
joshualitt5df175e2015-11-18 13:37:54 -080084 fDirtyRect.setEmpty();
85}
joshualitt5bf99f12015-03-13 11:47:42 -070086
Brian Salomon2ee084e2016-12-16 18:59:19 -050087GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070088 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080089 delete fRects;
90}
joshualitt5bf99f12015-03-13 11:47:42 -070091
Brian Salomon2ee084e2016-12-16 18:59:19 -050092bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -080093 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070094
joshualitt5df175e2015-11-18 13:37:54 -080095 if (!fRects) {
96 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070097 }
98
joshualitt5df175e2015-11-18 13:37:54 -080099 if (!fRects->addRect(width, height, loc)) {
100 return false;
joshualittb4c507e2015-04-08 08:07:59 -0700101 }
joshualitt5bf99f12015-03-13 11:47:42 -0700102
jvanverthc3d706f2016-04-20 10:33:27 -0700103 if (!fData) {
104 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
105 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800106 }
107 size_t rowBytes = width * fBytesPerPixel;
108 const unsigned char* imagePtr = (const unsigned char*)image;
109 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700110 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800111 dataPtr += fBytesPerPixel * fWidth * loc->fY;
112 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400113 // copy into the data buffer, swizzling as we go if this is ARGB data
114 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
115 for (int i = 0; i < height; ++i) {
Mike Klein6e78ae52018-09-19 13:37:16 -0400116 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
Brian Osmancce3e582016-10-14 11:42:20 -0400117 dataPtr += fBytesPerPixel * fWidth;
118 imagePtr += rowBytes;
119 }
120 } else {
121 for (int i = 0; i < height; ++i) {
122 memcpy(dataPtr, imagePtr, rowBytes);
123 dataPtr += fBytesPerPixel * fWidth;
124 imagePtr += rowBytes;
125 }
joshualitt5bf99f12015-03-13 11:47:42 -0700126 }
127
joshualitt5df175e2015-11-18 13:37:54 -0800128 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -0800129
joshualitt5df175e2015-11-18 13:37:54 -0800130 loc->fX += fOffset.fX;
131 loc->fY += fOffset.fY;
132 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700133
joshualitt5df175e2015-11-18 13:37:54 -0800134 return true;
135}
joshualitt5bf99f12015-03-13 11:47:42 -0700136
Brian Salomon943ed792017-10-30 09:37:55 -0400137void GrDrawOpAtlas::Plot::uploadToTexture(GrDeferredTextureUploadWritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400138 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800139 // We should only be issuing uploads if we are in fact dirty
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400140 SkASSERT(fDirty && fData && proxy && proxy->peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400141 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800142 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700143 const unsigned char* dataPtr = fData;
Jim Van Vertha8c55fa2018-02-20 15:38:08 -0500144 // Clamp to 4-byte aligned boundaries
145 unsigned int clearBits = 0x3 / fBytesPerPixel;
146 fDirtyRect.fLeft &= ~clearBits;
147 fDirtyRect.fRight += clearBits;
148 fDirtyRect.fRight &= ~clearBits;
149 SkASSERT(fDirtyRect.fRight <= fWidth);
150 // Set up dataPtr
jvanverthc3d706f2016-04-20 10:33:27 -0700151 dataPtr += rowBytes * fDirtyRect.fTop;
152 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Brian Salomonc320b152018-02-20 14:05:36 -0500153 // TODO: Make GrDrawOpAtlas store a GrColorType rather than GrPixelConfig.
154 auto colorType = GrPixelConfigToColorType(fConfig);
Robert Phillipsacaa6072017-07-28 10:54:53 -0400155 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
Brian Salomonc320b152018-02-20 14:05:36 -0500156 fDirtyRect.width(), fDirtyRect.height(), colorType, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800157 fDirtyRect.setEmpty();
158 SkDEBUGCODE(fDirty = false;)
159}
160
Brian Salomon2ee084e2016-12-16 18:59:19 -0500161void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800162 if (fRects) {
163 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700164 }
165
joshualitt5df175e2015-11-18 13:37:54 -0800166 fGenID++;
Jim Van Vertha950b632017-09-12 11:54:11 -0400167 fID = CreateId(fPageIndex, fPlotIndex, fGenID);
Brian Salomon943ed792017-10-30 09:37:55 -0400168 fLastUpload = GrDeferredUploadToken::AlreadyFlushedToken();
169 fLastUse = GrDeferredUploadToken::AlreadyFlushedToken();
joshualitt5df175e2015-11-18 13:37:54 -0800170
171 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700172 if (fData) {
173 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700174 }
175
joshualitt5df175e2015-11-18 13:37:54 -0800176 fDirtyRect.setEmpty();
177 SkDEBUGCODE(fDirty = false;)
178}
joshualitt5bf99f12015-03-13 11:47:42 -0700179
joshualitt5bf99f12015-03-13 11:47:42 -0700180///////////////////////////////////////////////////////////////////////////////
181
Greg Daniel4065d452018-11-16 15:43:41 -0500182GrDrawOpAtlas::GrDrawOpAtlas(GrProxyProvider* proxyProvider, const GrBackendFormat& format,
Robert Phillips4bc70112018-03-01 10:24:02 -0500183 GrPixelConfig config, int width, int height,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500184 int plotWidth, int plotHeight, AllowMultitexturing allowMultitexturing)
Greg Daniel4065d452018-11-16 15:43:41 -0500185 : fFormat(format)
186 , fPixelConfig(config)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400187 , fTextureWidth(width)
188 , fTextureHeight(height)
Jim Van Verthf6206f92018-12-14 08:22:24 -0500189 , fPlotWidth(plotWidth)
190 , fPlotHeight(plotHeight)
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400191 , fAtlasGeneration(kInvalidAtlasGeneration + 1)
Brian Salomon943ed792017-10-30 09:37:55 -0400192 , fPrevFlushToken(GrDeferredUploadToken::AlreadyFlushedToken())
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500193 , fMaxPages(AllowMultitexturing::kYes == allowMultitexturing ? kMaxMultitexturePages : 1)
Robert Phillips4bc70112018-03-01 10:24:02 -0500194 , fNumActivePages(0) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500195 int numPlotsX = width/plotWidth;
196 int numPlotsY = height/plotHeight;
Herb Derbybbf5fb52018-10-15 16:39:39 -0400197 SkASSERT(numPlotsX * numPlotsY <= GrDrawOpAtlas::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400198 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
199 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800200
Jim Van Verth06f593c2018-02-20 11:30:10 -0500201 fNumPlots = numPlotsX * numPlotsY;
joshualitt5bf99f12015-03-13 11:47:42 -0700202
Robert Phillips4bc70112018-03-01 10:24:02 -0500203 this->createPages(proxyProvider);
joshualitt5bf99f12015-03-13 11:47:42 -0700204}
205
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400206inline void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700207 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
208 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
209 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400210 ++fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -0700211}
212
Brian Salomon29b60c92017-10-31 14:42:10 -0400213inline bool GrDrawOpAtlas::updatePlot(GrDeferredUploadTarget* target, AtlasID* id, Plot* plot) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400214 int pageIdx = GetPageIndexFromID(plot->id());
215 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700216
217 // If our most recent upload has already occurred then we have to insert a new
218 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
219 // This new update will piggy back on that previously scheduled update.
Robert Phillips40a29d72018-01-18 12:59:22 -0500220 if (plot->lastUploadToken() < target->tokenTracker()->nextTokenToFlush()) {
jvanverthc3d706f2016-04-20 10:33:27 -0700221 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500222 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500223
Jim Van Vertha950b632017-09-12 11:54:11 -0400224 GrTextureProxy* proxy = fProxies[pageIdx].get();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400225 SkASSERT(proxy->isInstantiated()); // This is occurring at flush time
Robert Phillips256c37b2017-03-01 14:32:46 -0500226
Brian Salomon29b60c92017-10-31 14:42:10 -0400227 GrDeferredUploadToken lastUploadToken = target->addASAPUpload(
Brian Salomon943ed792017-10-30 09:37:55 -0400228 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
229 plotsp->uploadToTexture(writePixels, proxy);
230 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500231 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700232 }
233 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500234 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700235}
236
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500237bool GrDrawOpAtlas::uploadToPage(unsigned int pageIdx, AtlasID* id, GrDeferredUploadTarget* target,
238 int width, int height, const void* image, SkIPoint16* loc) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400239 SkASSERT(fProxies[pageIdx] && fProxies[pageIdx]->isInstantiated());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500240
241 // look through all allocated plots for one we can share, in Most Recently Refed order
242 PlotList::Iter plotIter;
243 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
244
245 for (Plot* plot = plotIter.get(); plot; plot = plotIter.next()) {
246 SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == plot->bpp());
247
248 if (plot->addSubImage(width, height, image, loc)) {
249 return this->updatePlot(target, id, plot);
250 }
251 }
252
253 return false;
254}
255
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400256// Number of atlas-related flushes beyond which we consider a plot to no longer be in use.
257//
258// This value is somewhat arbitrary -- the idea is to keep it low enough that
259// a page with unused plots will get removed reasonably quickly, but allow it
260// to hang around for a bit in case it's needed. The assumption is that flushes
261// are rare; i.e., we are not continually refreshing the frame.
Derek Sollenberger90196cc2017-10-09 15:00:33 -0400262static constexpr auto kRecentlyUsedCount = 256;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400263
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500264GrDrawOpAtlas::ErrorCode GrDrawOpAtlas::addToAtlas(GrResourceProvider* resourceProvider,
265 AtlasID* id, GrDeferredUploadTarget* target,
266 int width, int height,
267 const void* image, SkIPoint16* loc) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700268 if (width > fPlotWidth || height > fPlotHeight) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500269 return ErrorCode::kError;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700270 }
joshualitt5bf99f12015-03-13 11:47:42 -0700271
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400272 // Look through each page to see if we can upload without having to flush
273 // We prioritize this upload to the first pages, not the most recently used, to make it easier
274 // to remove unused pages in reverse page order.
Robert Phillips4bc70112018-03-01 10:24:02 -0500275 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500276 if (this->uploadToPage(pageIdx, id, target, width, height, image, loc)) {
277 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400278 }
Jim Van Verth712fe732017-09-25 16:53:49 -0400279 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400280
Jim Van Verth712fe732017-09-25 16:53:49 -0400281 // 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 -0400282 // flushed to the gpu if we're at max page allocation, or if the plot has aged out otherwise.
283 // We wait until we've grown to the full number of pages to begin evicting already flushed
284 // plots so that we can maximize the opportunity for reuse.
Jim Van Verth712fe732017-09-25 16:53:49 -0400285 // As before we prioritize this upload to the first pages, not the most recently used.
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500286 if (fNumActivePages == this->maxPages()) {
287 for (unsigned int pageIdx = 0; pageIdx < fNumActivePages; ++pageIdx) {
288 Plot* plot = fPages[pageIdx].fPlotList.tail();
289 SkASSERT(plot);
Jim Van Verthba98b7d2018-12-05 12:33:43 -0500290 if (plot->lastUseToken() < target->tokenTracker()->nextTokenToFlush()) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500291 this->processEvictionAndResetRects(plot);
292 SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == plot->bpp());
293 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
294 SkASSERT(verify);
295 if (!this->updatePlot(target, id, plot)) {
296 return ErrorCode::kError;
297 }
298 return ErrorCode::kSucceeded;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400299 }
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500300 }
301 } else {
302 // If we haven't activated all the available pages, try to create a new one and add to it
303 if (!this->activateNewPage(resourceProvider)) {
304 return ErrorCode::kError;
305 }
306
307 if (this->uploadToPage(fNumActivePages-1, id, target, width, height, image, loc)) {
308 return ErrorCode::kSucceeded;
309 } else {
310 // If we fail to upload to a newly activated page then something has gone terribly
311 // wrong - return an error
312 return ErrorCode::kError;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400313 }
314 }
315
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500316 if (!fNumActivePages) {
317 return ErrorCode::kError;
joshualitt5bf99f12015-03-13 11:47:42 -0700318 }
319
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400320 // Try to find a plot that we can perform an inline upload to.
321 // We prioritize this upload in reverse order of pages to counterbalance the order above.
322 Plot* plot = nullptr;
Robert Phillips6250f292018-03-01 10:53:45 -0500323 for (int pageIdx = ((int)fNumActivePages)-1; pageIdx >= 0; --pageIdx) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400324 Plot* currentPlot = fPages[pageIdx].fPlotList.tail();
Robert Phillips40a29d72018-01-18 12:59:22 -0500325 if (currentPlot->lastUseToken() != target->tokenTracker()->nextDrawToken()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400326 plot = currentPlot;
327 break;
Robert Phillips256c37b2017-03-01 14:32:46 -0500328 }
joshualitt5bf99f12015-03-13 11:47:42 -0700329 }
330
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400331 // If we can't find a plot that is not used in a draw currently being prepared by an op, then
332 // we have to fail. This gives the op a chance to enqueue the draw, and call back into this
333 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
334 // continue past this branch and prepare an inline upload that will occur after the enqueued
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500335 // draw which references the plot's pre-upload content.
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400336 if (!plot) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500337 return ErrorCode::kTryAgain;
joshualitt5bf99f12015-03-13 11:47:42 -0700338 }
339
joshualitt5bf99f12015-03-13 11:47:42 -0700340 this->processEviction(plot->id());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400341 int pageIdx = GetPageIndexFromID(plot->id());
Jim Van Vertha950b632017-09-12 11:54:11 -0400342 fPages[pageIdx].fPlotList.remove(plot);
343 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800344 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700345
Jim Van Vertha950b632017-09-12 11:54:11 -0400346 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
347 SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800348 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700349 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800350
robertphillips1f0e3502015-11-10 10:19:50 -0800351 // Note that this plot will be uploaded inline with the draws whereas the
Brian Salomon29b60c92017-10-31 14:42:10 -0400352 // one it displaced most likely was uploaded ASAP.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500353 // With c+14 we could move sk_sp into lambda to only ref once.
354 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips4bc70112018-03-01 10:24:02 -0500355
Jim Van Vertha950b632017-09-12 11:54:11 -0400356 GrTextureProxy* proxy = fProxies[pageIdx].get();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400357 SkASSERT(proxy->isInstantiated());
bsalomon342bfc22016-04-01 06:06:20 -0700358
Brian Salomon943ed792017-10-30 09:37:55 -0400359 GrDeferredUploadToken lastUploadToken = target->addInlineUpload(
360 [plotsp, proxy](GrDeferredTextureUploadWritePixelsFn& writePixels) {
361 plotsp->uploadToTexture(writePixels, proxy);
362 });
Robert Phillips256c37b2017-03-01 14:32:46 -0500363 newPlot->setLastUploadToken(lastUploadToken);
364
joshualitt5bf99f12015-03-13 11:47:42 -0700365 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800366
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500367 return ErrorCode::kSucceeded;
joshualitt5bf99f12015-03-13 11:47:42 -0700368}
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400369
Brian Salomon943ed792017-10-30 09:37:55 -0400370void GrDrawOpAtlas::compact(GrDeferredUploadToken startTokenForNextFlush) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500371 if (fNumActivePages <= 1) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400372 fPrevFlushToken = startTokenForNextFlush;
373 return;
374 }
375
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400376 // For all plots, reset number of flushes since used if used this frame.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400377 PlotList::Iter plotIter;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400378 bool atlasUsedThisFlush = false;
Robert Phillips4bc70112018-03-01 10:24:02 -0500379 for (uint32_t pageIndex = 0; pageIndex < fNumActivePages; ++pageIndex) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400380 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
381 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400382 // Reset number of flushes since used
Jim Van Verth106b5c42017-09-26 12:45:29 -0400383 if (plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
384 plot->resetFlushesSinceLastUsed();
385 atlasUsedThisFlush = true;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400386 }
387
388 plotIter.next();
389 }
390 }
391
392 // We only try to compact if the atlas was used in the recently completed flush.
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400393 // This is to handle the case where a lot of text or path rendering has occurred but then just
394 // a blinking cursor is drawn.
Jim Van Verth106b5c42017-09-26 12:45:29 -0400395 // TODO: consider if we should also do this if it's been a long time since the last atlas use
396 if (atlasUsedThisFlush) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500397 SkTArray<Plot*> availablePlots;
Robert Phillips4bc70112018-03-01 10:24:02 -0500398 uint32_t lastPageIndex = fNumActivePages - 1;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400399
400 // For all plots but the last one, update number of flushes since used, and check to see
401 // if there are any in the first pages that the last page can safely upload to.
402 for (uint32_t pageIndex = 0; pageIndex < lastPageIndex; ++pageIndex) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400403#ifdef DUMP_ATLAS_DATA
404 if (gDumpAtlasData) {
405 SkDebugf("page %d: ", pageIndex);
406 }
407#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400408 plotIter.init(fPages[pageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
409 while (Plot* plot = plotIter.get()) {
410 // Update number of flushes since plot was last used
411 // We only increment the 'sinceLastUsed' count for flushes where the atlas was used
412 // to avoid deleting everything when we return to text drawing in the blinking
413 // cursor case
414 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
415 plot->incFlushesSinceLastUsed();
416 }
417
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400418#ifdef DUMP_ATLAS_DATA
419 if (gDumpAtlasData) {
420 SkDebugf("%d ", plot->flushesSinceLastUsed());
421 }
422#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400423 // Count plots we can potentially upload to in all pages except the last one
424 // (the potential compactee).
425 if (plot->flushesSinceLastUsed() > kRecentlyUsedCount) {
Jim Van Verthcad0acf2018-02-16 18:41:41 -0500426 availablePlots.push_back() = plot;
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400427 }
428
429 plotIter.next();
430 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400431#ifdef DUMP_ATLAS_DATA
432 if (gDumpAtlasData) {
433 SkDebugf("\n");
434 }
435#endif
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400436 }
437
Jim Van Verth06f593c2018-02-20 11:30:10 -0500438 // Count recently used plots in the last page and evict any that are no longer in use.
439 // Since we prioritize uploading to the first pages, this will eventually
Jim Van Verth106b5c42017-09-26 12:45:29 -0400440 // clear out usage of this page unless we have a large need.
441 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
Jim Van Verth06f593c2018-02-20 11:30:10 -0500442 unsigned int usedPlots = 0;
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400443#ifdef DUMP_ATLAS_DATA
444 if (gDumpAtlasData) {
445 SkDebugf("page %d: ", lastPageIndex);
446 }
447#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400448 while (Plot* plot = plotIter.get()) {
Jim Van Verth62ea0cd2017-09-27 12:59:45 -0400449 // Update number of flushes since plot was last used
450 if (!plot->lastUseToken().inInterval(fPrevFlushToken, startTokenForNextFlush)) {
451 plot->incFlushesSinceLastUsed();
452 }
453
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400454#ifdef DUMP_ATLAS_DATA
455 if (gDumpAtlasData) {
456 SkDebugf("%d ", plot->flushesSinceLastUsed());
457 }
458#endif
Jim Van Verth106b5c42017-09-26 12:45:29 -0400459 // If this plot was used recently
460 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
461 usedPlots++;
Brian Salomon943ed792017-10-30 09:37:55 -0400462 } else if (plot->lastUseToken() != GrDeferredUploadToken::AlreadyFlushedToken()) {
Jim Van Verth106b5c42017-09-26 12:45:29 -0400463 // otherwise if aged out just evict it.
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400464 this->processEvictionAndResetRects(plot);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400465 }
466 plotIter.next();
467 }
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400468#ifdef DUMP_ATLAS_DATA
469 if (gDumpAtlasData) {
470 SkDebugf("\n");
471 }
472#endif
Jim Van Verth06f593c2018-02-20 11:30:10 -0500473
474 // If recently used plots in the last page are using less than a quarter of the page, try
475 // to evict them if there's available space in earlier pages. Since we prioritize uploading
476 // to the first pages, this will eventually clear out usage of this page unless we have a
477 // large need.
478 if (availablePlots.count() && usedPlots && usedPlots <= fNumPlots / 4) {
479 plotIter.init(fPages[lastPageIndex].fPlotList, PlotList::Iter::kHead_IterStart);
480 while (Plot* plot = plotIter.get()) {
481 // If this plot was used recently
482 if (plot->flushesSinceLastUsed() <= kRecentlyUsedCount) {
483 // See if there's room in an earlier page and if so evict.
484 // We need to be somewhat harsh here so that a handful of plots that are
485 // consistently in use don't end up locking the page in memory.
486 if (availablePlots.count() > 0) {
487 this->processEvictionAndResetRects(plot);
488 this->processEvictionAndResetRects(availablePlots.back());
489 availablePlots.pop_back();
490 --usedPlots;
491 }
492 if (!usedPlots || !availablePlots.count()) {
493 break;
494 }
495 }
496 plotIter.next();
497 }
498 }
499
Jim Van Verth106b5c42017-09-26 12:45:29 -0400500 // If none of the plots in the last page have been used recently, delete it.
501 if (!usedPlots) {
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400502#ifdef DUMP_ATLAS_DATA
503 if (gDumpAtlasData) {
504 SkDebugf("delete %d\n", fNumPages-1);
505 }
506#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500507 this->deactivateLastPage();
Jim Van Verth106b5c42017-09-26 12:45:29 -0400508 }
509 }
510
511 fPrevFlushToken = startTokenForNextFlush;
512}
513
Robert Phillips4bc70112018-03-01 10:24:02 -0500514bool GrDrawOpAtlas::createPages(GrProxyProvider* proxyProvider) {
515 SkASSERT(SkIsPow2(fTextureWidth) && SkIsPow2(fTextureHeight));
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500516
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400517 GrSurfaceDesc desc;
518 desc.fFlags = kNone_GrSurfaceFlags;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400519 desc.fWidth = fTextureWidth;
520 desc.fHeight = fTextureHeight;
521 desc.fConfig = fPixelConfig;
522
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400523 int numPlotsX = fTextureWidth/fPlotWidth;
524 int numPlotsY = fTextureHeight/fPlotHeight;
525
Robert Phillips4bc70112018-03-01 10:24:02 -0500526 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel4065d452018-11-16 15:43:41 -0500527 fProxies[i] = proxyProvider->createProxy(fFormat, desc, kTopLeft_GrSurfaceOrigin,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400528 SkBackingFit::kExact, SkBudgeted::kYes, GrInternalSurfaceFlags::kNoPendingIO);
Robert Phillips4bc70112018-03-01 10:24:02 -0500529 if (!fProxies[i]) {
530 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400531 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500532
533 // set up allocated plots
534 fPages[i].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
535
536 sk_sp<Plot>* currPlot = fPages[i].fPlotArray.get();
537 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
538 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
539 uint32_t plotIndex = r * numPlotsX + c;
540 currPlot->reset(new Plot(i, plotIndex, 1, x, y, fPlotWidth, fPlotHeight,
541 fPixelConfig));
542
543 // build LRU list
544 fPages[i].fPlotList.addToHead(currPlot->get());
545 ++currPlot;
546 }
547 }
548
549 }
550
551 return true;
552}
553
554
555bool GrDrawOpAtlas::activateNewPage(GrResourceProvider* resourceProvider) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500556 SkASSERT(fNumActivePages < this->maxPages());
Robert Phillips4bc70112018-03-01 10:24:02 -0500557
558 if (!fProxies[fNumActivePages]->instantiate(resourceProvider)) {
559 return false;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400560 }
561
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400562#ifdef DUMP_ATLAS_DATA
563 if (gDumpAtlasData) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500564 SkDebugf("activated page#: %d\n", fNumActivePages);
Jim Van Verthc3269ae2017-09-28 15:04:00 -0400565 }
566#endif
Robert Phillips4bc70112018-03-01 10:24:02 -0500567
568 ++fNumActivePages;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400569 return true;
570}
Jim Van Verth106b5c42017-09-26 12:45:29 -0400571
Robert Phillips4bc70112018-03-01 10:24:02 -0500572
573inline void GrDrawOpAtlas::deactivateLastPage() {
574 SkASSERT(fNumActivePages);
575
576 uint32_t lastPageIndex = fNumActivePages - 1;
577
578 int numPlotsX = fTextureWidth/fPlotWidth;
579 int numPlotsY = fTextureHeight/fPlotHeight;
580
Jim Van Verth106b5c42017-09-26 12:45:29 -0400581 fPages[lastPageIndex].fPlotList.reset();
Robert Phillips6250f292018-03-01 10:53:45 -0500582 for (int r = 0; r < numPlotsY; ++r) {
583 for (int c = 0; c < numPlotsX; ++c) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500584 uint32_t plotIndex = r * numPlotsX + c;
585
586 Plot* currPlot = fPages[lastPageIndex].fPlotArray[plotIndex].get();
587 currPlot->resetRects();
588 currPlot->resetFlushesSinceLastUsed();
589
590 // rebuild the LRU list
591 SkDEBUGCODE(currPlot->fPrev = currPlot->fNext = nullptr);
592 SkDEBUGCODE(currPlot->fList = nullptr);
593 fPages[lastPageIndex].fPlotList.addToHead(currPlot);
594 }
595 }
596
597 // remove ref to the backing texture
Brian Salomon967df202018-12-07 11:15:53 -0500598 fProxies[lastPageIndex]->deinstantiate();
Robert Phillips4bc70112018-03-01 10:24:02 -0500599 --fNumActivePages;
Jim Van Verth106b5c42017-09-26 12:45:29 -0400600}
Herb Derby15d9ef22018-10-18 13:41:32 -0400601
Jim Van Verthf6206f92018-12-14 08:22:24 -0500602GrDrawOpAtlasConfig::GrDrawOpAtlasConfig(int maxTextureSize, size_t maxBytes) {
603 static const SkISize kARGBDimensions[] = {
604 {256, 256}, // maxBytes < 2^19
605 {512, 256}, // 2^19 <= maxBytes < 2^20
606 {512, 512}, // 2^20 <= maxBytes < 2^21
607 {1024, 512}, // 2^21 <= maxBytes < 2^22
608 {1024, 1024}, // 2^22 <= maxBytes < 2^23
609 {2048, 1024}, // 2^23 <= maxBytes
610 };
Herb Derby15d9ef22018-10-18 13:41:32 -0400611
Jim Van Verthf6206f92018-12-14 08:22:24 -0500612 // Index 0 corresponds to maxBytes of 2^18, so start by dividing it by that
613 maxBytes >>= 18;
614 // Take the floor of the log to get the index
615 int index = maxBytes > 0
616 ? SkTPin<int>(SkPrevLog2(maxBytes), 0, SK_ARRAY_COUNT(kARGBDimensions) - 1)
617 : 0;
Herb Derby15d9ef22018-10-18 13:41:32 -0400618
Jim Van Verthf6206f92018-12-14 08:22:24 -0500619 SkASSERT(kARGBDimensions[index].width() <= kMaxAtlasDim);
620 SkASSERT(kARGBDimensions[index].height() <= kMaxAtlasDim);
621 fARGBDimensions.set(SkTMin<int>(kARGBDimensions[index].width(), maxTextureSize),
622 SkTMin<int>(kARGBDimensions[index].height(), maxTextureSize));
623 fMaxTextureSize = SkTMin<int>(maxTextureSize, kMaxAtlasDim);
Herb Derby15d9ef22018-10-18 13:41:32 -0400624}
625
626SkISize GrDrawOpAtlasConfig::atlasDimensions(GrMaskFormat type) const {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500627 if (kA8_GrMaskFormat == type) {
628 // A8 is always 2x the ARGB dimensions, clamped to the max allowed texture size
629 return { SkTMin<int>(2 * fARGBDimensions.width(), fMaxTextureSize),
630 SkTMin<int>(2 * fARGBDimensions.height(), fMaxTextureSize) };
631 } else {
632 return fARGBDimensions;
633 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400634}
635
Jim Van Verthf6206f92018-12-14 08:22:24 -0500636SkISize GrDrawOpAtlasConfig::plotDimensions(GrMaskFormat type) const {
637 if (kA8_GrMaskFormat == type) {
638 SkISize atlasDimensions = this->atlasDimensions(type);
639 // For A8 we want to grow the plots at larger texture sizes to accept more of the
640 // larger SDF glyphs. Since the largest SDF glyph can be 170x170 with padding, this
641 // allows us to pack 3 in a 512x256 plot, or 9 in a 512x512 plot.
Herb Derby15d9ef22018-10-18 13:41:32 -0400642
Jim Van Verth578b0892018-12-20 20:48:55 +0000643 // This will give us 512x256 plots for 2048x1024, 512x512 plots for 2048x2048,
644 // and 256x256 plots otherwise.
Jim Van Verthf6206f92018-12-14 08:22:24 -0500645 int plotWidth = atlasDimensions.width() >= 2048 ? 512 : 256;
Jim Van Verth578b0892018-12-20 20:48:55 +0000646 int plotHeight = atlasDimensions.height() >= 2048 ? 512 : 256;
Herb Derby15d9ef22018-10-18 13:41:32 -0400647
Jim Van Verthf6206f92018-12-14 08:22:24 -0500648 return { plotWidth, plotHeight };
649 } else {
650 // ARGB and LCD always use 256x256 plots -- this has been shown to be faster
651 return { 256, 256 };
652 }
Herb Derby15d9ef22018-10-18 13:41:32 -0400653}
654
Jim Van Verthf6206f92018-12-14 08:22:24 -0500655constexpr int GrDrawOpAtlasConfig::kMaxAtlasDim;