blob: bdd07e7c829f32a166f35bd472d9c13686783b41 [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"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrOpFlushState.h"
joshualitt5bf99f12015-03-13 11:47:42 -070012#include "GrRectanizer.h"
Robert Phillips256c37b2017-03-01 14:32:46 -050013#include "GrResourceProvider.h"
Robert Phillips646e4292017-06-13 12:44:56 -040014#include "GrTexture.h"
joshualitt5bf99f12015-03-13 11:47:42 -070015#include "GrTracing.h"
16
Robert Phillips256c37b2017-03-01 14:32:46 -050017std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrContext* ctx, GrPixelConfig config,
18 int width, int height,
19 int numPlotsX, int numPlotsY,
20 GrDrawOpAtlas::EvictionFunc func,
21 void* data) {
Robert Phillips256c37b2017-03-01 14:32:46 -050022 std::unique_ptr<GrDrawOpAtlas> atlas(
Jim Van Verthd74f3f22017-08-31 16:44:08 -040023 new GrDrawOpAtlas(ctx, config, width, height, numPlotsX, numPlotsY));
Jim Van Vertha950b632017-09-12 11:54:11 -040024 if (!atlas->getProxies()[0]) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -040025 return nullptr;
26 }
27
Robert Phillips256c37b2017-03-01 14:32:46 -050028 atlas->registerEvictionCallback(func, data);
29 return atlas;
30}
31
32
joshualitt5df175e2015-11-18 13:37:54 -080033////////////////////////////////////////////////////////////////////////////////
joshualitt5bf99f12015-03-13 11:47:42 -070034
Jim Van Vertha950b632017-09-12 11:54:11 -040035GrDrawOpAtlas::Plot::Plot(int pageIndex, int plotIndex, uint64_t genID, int offX, int offY,
36 int width, int height, GrPixelConfig config)
Brian Salomon2ee084e2016-12-16 18:59:19 -050037 : fLastUpload(GrDrawOpUploadToken::AlreadyFlushedToken())
38 , fLastUse(GrDrawOpUploadToken::AlreadyFlushedToken())
Jim Van Vertha950b632017-09-12 11:54:11 -040039 , fPageIndex(pageIndex)
40 , fPlotIndex(plotIndex)
Brian Salomon2ee084e2016-12-16 18:59:19 -050041 , fGenID(genID)
Jim Van Vertha950b632017-09-12 11:54:11 -040042 , fID(CreateId(fPageIndex, fPlotIndex, fGenID))
Brian Salomon2ee084e2016-12-16 18:59:19 -050043 , fData(nullptr)
44 , fWidth(width)
45 , fHeight(height)
46 , fX(offX)
47 , fY(offY)
48 , fRects(nullptr)
49 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
50 , fConfig(config)
51 , fBytesPerPixel(GrBytesPerPixel(config))
joshualitt5df175e2015-11-18 13:37:54 -080052#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050053 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080054#endif
55{
56 fDirtyRect.setEmpty();
57}
joshualitt5bf99f12015-03-13 11:47:42 -070058
Brian Salomon2ee084e2016-12-16 18:59:19 -050059GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070060 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080061 delete fRects;
62}
joshualitt5bf99f12015-03-13 11:47:42 -070063
Brian Salomon2ee084e2016-12-16 18:59:19 -050064bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -080065 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070066
joshualitt5df175e2015-11-18 13:37:54 -080067 if (!fRects) {
68 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070069 }
70
joshualitt5df175e2015-11-18 13:37:54 -080071 if (!fRects->addRect(width, height, loc)) {
72 return false;
joshualittb4c507e2015-04-08 08:07:59 -070073 }
joshualitt5bf99f12015-03-13 11:47:42 -070074
jvanverthc3d706f2016-04-20 10:33:27 -070075 if (!fData) {
76 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
77 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -080078 }
79 size_t rowBytes = width * fBytesPerPixel;
80 const unsigned char* imagePtr = (const unsigned char*)image;
81 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -070082 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -080083 dataPtr += fBytesPerPixel * fWidth * loc->fY;
84 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -040085 // copy into the data buffer, swizzling as we go if this is ARGB data
86 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
87 for (int i = 0; i < height; ++i) {
88 SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width);
89 dataPtr += fBytesPerPixel * fWidth;
90 imagePtr += rowBytes;
91 }
92 } else {
93 for (int i = 0; i < height; ++i) {
94 memcpy(dataPtr, imagePtr, rowBytes);
95 dataPtr += fBytesPerPixel * fWidth;
96 imagePtr += rowBytes;
97 }
joshualitt5bf99f12015-03-13 11:47:42 -070098 }
99
joshualitt5df175e2015-11-18 13:37:54 -0800100 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -0800101
joshualitt5df175e2015-11-18 13:37:54 -0800102 loc->fX += fOffset.fX;
103 loc->fY += fOffset.fY;
104 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700105
joshualitt5df175e2015-11-18 13:37:54 -0800106 return true;
107}
joshualitt5bf99f12015-03-13 11:47:42 -0700108
Brian Salomon2ee084e2016-12-16 18:59:19 -0500109void GrDrawOpAtlas::Plot::uploadToTexture(GrDrawOp::WritePixelsFn& writePixels,
Robert Phillipsacaa6072017-07-28 10:54:53 -0400110 GrTextureProxy* proxy) {
joshualitt5df175e2015-11-18 13:37:54 -0800111 // We should only be issuing uploads if we are in fact dirty
Robert Phillipsacaa6072017-07-28 10:54:53 -0400112 SkASSERT(fDirty && fData && proxy && proxy->priv().peekTexture());
Brian Osman39c08ac2017-07-26 09:36:09 -0400113 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt5df175e2015-11-18 13:37:54 -0800114 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700115 const unsigned char* dataPtr = fData;
116 dataPtr += rowBytes * fDirtyRect.fTop;
117 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
Robert Phillipsacaa6072017-07-28 10:54:53 -0400118 writePixels(proxy, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
jvanverthc3d706f2016-04-20 10:33:27 -0700119 fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800120 fDirtyRect.setEmpty();
121 SkDEBUGCODE(fDirty = false;)
122}
123
Brian Salomon2ee084e2016-12-16 18:59:19 -0500124void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800125 if (fRects) {
126 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700127 }
128
joshualitt5df175e2015-11-18 13:37:54 -0800129 fGenID++;
Jim Van Vertha950b632017-09-12 11:54:11 -0400130 fID = CreateId(fPageIndex, fPlotIndex, fGenID);
joshualitt5df175e2015-11-18 13:37:54 -0800131
132 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700133 if (fData) {
134 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700135 }
136
joshualitt5df175e2015-11-18 13:37:54 -0800137 fDirtyRect.setEmpty();
138 SkDEBUGCODE(fDirty = false;)
139}
joshualitt5bf99f12015-03-13 11:47:42 -0700140
joshualitt5bf99f12015-03-13 11:47:42 -0700141///////////////////////////////////////////////////////////////////////////////
142
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400143GrDrawOpAtlas::GrDrawOpAtlas(GrContext* context, GrPixelConfig config, int width, int height,
Robert Phillips32f28182017-02-28 16:20:03 -0500144 int numPlotsX, int numPlotsY)
145 : fContext(context)
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400146 , fPixelConfig(config)
147 , fTextureWidth(width)
148 , fTextureHeight(height)
Robert Phillips32f28182017-02-28 16:20:03 -0500149 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400150
151 GrSurfaceDesc desc;
152 desc.fFlags = kNone_GrSurfaceFlags;
153 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
154 desc.fWidth = fTextureWidth;
155 desc.fHeight = fTextureHeight;
156 desc.fConfig = fPixelConfig;
157
158 // We don't want to flush the context so we claim we're in the middle of flushing so as to
159 // guarantee we do not recieve a texture with pending IO
160 // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156)
161 static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag;
162 sk_sp<GrTexture> texture(context->resourceProvider()->createApproxTexture(desc, kFlags));
163 if (texture) {
164 // MDB TODO: for now, wrap an instantiated texture. Having the deferred instantiation
165 // possess the correct properties (e.g., no pendingIO) should fall out of the system but
166 // should receive special attention.
167 // Note: When switching over to the deferred proxy, use the kExact flag to create
168 // the atlas and assert that the width & height are powers of 2.
Jim Van Vertha950b632017-09-12 11:54:11 -0400169 fProxies[0] = GrSurfaceProxy::MakeWrapped(std::move(texture),
170 kTopLeft_GrSurfaceOrigin);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400171 }
172
173 fPlotWidth = fTextureWidth / numPlotsX;
174 fPlotHeight = fTextureHeight / numPlotsY;
robertphillips2b0536f2015-11-06 14:10:42 -0800175 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
Jim Van Verthd74f3f22017-08-31 16:44:08 -0400176 SkASSERT(fPlotWidth * numPlotsX == fTextureWidth);
177 SkASSERT(fPlotHeight * numPlotsY == fTextureHeight);
robertphillips2b0536f2015-11-06 14:10:42 -0800178
179 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
Jim Van Vertha950b632017-09-12 11:54:11 -0400180 SkDEBUGCODE(fNumPages = 1;)
joshualitt5bf99f12015-03-13 11:47:42 -0700181
joshualitt5bf99f12015-03-13 11:47:42 -0700182 // set up allocated plots
Jim Van Vertha950b632017-09-12 11:54:11 -0400183 fPages[0].fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
joshualitt5bf99f12015-03-13 11:47:42 -0700184
Jim Van Vertha950b632017-09-12 11:54:11 -0400185 sk_sp<Plot>* currPlot = fPages[0].fPlotArray.get();
robertphillips2b0536f2015-11-06 14:10:42 -0800186 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
187 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
188 uint32_t index = r * numPlotsX + c;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500189 currPlot->reset(
Jim Van Vertha950b632017-09-12 11:54:11 -0400190 new Plot(0, index, 1, x, y, fPlotWidth, fPlotHeight, fPixelConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700191
192 // build LRU list
Jim Van Vertha950b632017-09-12 11:54:11 -0400193 fPages[0].fPlotList.addToHead(currPlot->get());
joshualitt5bf99f12015-03-13 11:47:42 -0700194 ++currPlot;
195 }
196 }
197}
198
Brian Salomon2ee084e2016-12-16 18:59:19 -0500199void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700200 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
201 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
202 }
203}
204
Robert Phillips256c37b2017-03-01 14:32:46 -0500205inline bool GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* plot) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400206 int pageIdx = GetPageIndexFromID(plot->id());
207 this->makeMRU(plot, pageIdx);
joshualitt5bf99f12015-03-13 11:47:42 -0700208
209 // If our most recent upload has already occurred then we have to insert a new
210 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
211 // This new update will piggy back on that previously scheduled update.
bsalomon342bfc22016-04-01 06:06:20 -0700212 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
jvanverthc3d706f2016-04-20 10:33:27 -0700213 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500214 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500215
Robert Phillips32f28182017-02-28 16:20:03 -0500216 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
217 // Once it is deferred more care must be taken upon instantiation failure.
Jim Van Vertha950b632017-09-12 11:54:11 -0400218 if (!fProxies[pageIdx]->instantiate(fContext->resourceProvider())) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500219 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500220 }
Robert Phillipsacaa6072017-07-28 10:54:53 -0400221
Jim Van Vertha950b632017-09-12 11:54:11 -0400222 GrTextureProxy* proxy = fProxies[pageIdx].get();
Robert Phillips256c37b2017-03-01 14:32:46 -0500223
224 GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
Robert Phillipsacaa6072017-07-28 10:54:53 -0400225 [plotsp, proxy] (GrDrawOp::WritePixelsFn& writePixels) {
226 plotsp->uploadToTexture(writePixels, proxy);
Robert Phillips256c37b2017-03-01 14:32:46 -0500227 }
228 );
229 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700230 }
231 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500232 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700233}
234
Brian Salomon2ee084e2016-12-16 18:59:19 -0500235bool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height,
236 const void* image, SkIPoint16* loc) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400237 // Eventually we will iterate through these, for now just use the one.
238 int pageIdx = 0;
239
joshualitt5bf99f12015-03-13 11:47:42 -0700240 // We should already have a texture, TODO clean this up
Jim Van Vertha950b632017-09-12 11:54:11 -0400241 SkASSERT(fProxies[pageIdx]);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700242 if (width > fPlotWidth || height > fPlotHeight) {
243 return false;
244 }
joshualitt5bf99f12015-03-13 11:47:42 -0700245
246 // now look through all allocated plots for one we can share, in Most Recently Refed order
Brian Salomon2ee084e2016-12-16 18:59:19 -0500247 PlotList::Iter plotIter;
Jim Van Vertha950b632017-09-12 11:54:11 -0400248 plotIter.init(fPages[pageIdx].fPlotList, PlotList::Iter::kHead_IterStart);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500249 Plot* plot;
joshualitt5bf99f12015-03-13 11:47:42 -0700250 while ((plot = plotIter.get())) {
Jim Van Vertha950b632017-09-12 11:54:11 -0400251 SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800252 if (plot->addSubImage(width, height, image, loc)) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500253 return this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700254 }
255 plotIter.next();
256 }
257
258 // If the above fails, then see if the least recently refed plot has already been flushed to the
259 // gpu
Jim Van Vertha950b632017-09-12 11:54:11 -0400260 plot = fPages[pageIdx].fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700261 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700262 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700263 this->processEviction(plot->id());
264 plot->resetRects();
Jim Van Vertha950b632017-09-12 11:54:11 -0400265 SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800266 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700267 SkASSERT(verify);
Robert Phillips256c37b2017-03-01 14:32:46 -0500268 if (!this->updatePlot(target, id, plot)) {
269 return false;
270 }
271
joshualitt7c3a2f82015-03-31 13:32:05 -0700272 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700273 return true;
274 }
275
Jim Van Vertha950b632017-09-12 11:54:11 -0400276 // TODO: at this point try to create a new page and add to it before evicting
277
Brian Salomon2ee084e2016-12-16 18:59:19 -0500278 // If this plot has been used in a draw that is currently being prepared by an op, then we have
279 // to fail. This gives the op a chance to enqueue the draw, and call back into this function.
280 // When that draw is enqueued, the draw token advances, and the subsequent call will continue
281 // past this branch and prepare an inline upload that will occur after the enqueued draw which
282 // references the plot's pre-upload content.
bsalomon342bfc22016-04-01 06:06:20 -0700283 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700284 return false;
285 }
286
joshualitt5bf99f12015-03-13 11:47:42 -0700287 this->processEviction(plot->id());
Jim Van Vertha950b632017-09-12 11:54:11 -0400288 fPages[pageIdx].fPlotList.remove(plot);
289 sk_sp<Plot>& newPlot = fPages[pageIdx].fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800290 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700291
Jim Van Vertha950b632017-09-12 11:54:11 -0400292 fPages[pageIdx].fPlotList.addToHead(newPlot.get());
293 SkASSERT(GrBytesPerPixel(fProxies[pageIdx]->config()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800294 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700295 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800296
robertphillips1f0e3502015-11-10 10:19:50 -0800297 // Note that this plot will be uploaded inline with the draws whereas the
298 // one it displaced most likely was uploaded asap.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500299 // With c+14 we could move sk_sp into lambda to only ref once.
300 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips32f28182017-02-28 16:20:03 -0500301 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
302 // Once it is deferred more care must be taken upon instantiation failure.
Jim Van Vertha950b632017-09-12 11:54:11 -0400303 if (!fProxies[pageIdx]->instantiate(fContext->resourceProvider())) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500304 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500305 }
Jim Van Vertha950b632017-09-12 11:54:11 -0400306 GrTextureProxy* proxy = fProxies[pageIdx].get();
bsalomon342bfc22016-04-01 06:06:20 -0700307
Robert Phillips256c37b2017-03-01 14:32:46 -0500308 GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
Robert Phillipsacaa6072017-07-28 10:54:53 -0400309 [plotsp, proxy] (GrDrawOp::WritePixelsFn& writePixels) {
310 plotsp->uploadToTexture(writePixels, proxy);
Robert Phillips256c37b2017-03-01 14:32:46 -0500311 }
312 );
313 newPlot->setLastUploadToken(lastUploadToken);
314
joshualitt5bf99f12015-03-13 11:47:42 -0700315 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800316
joshualitt7c3a2f82015-03-31 13:32:05 -0700317 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700318 return true;
319}