blob: f4f0883ccf9a8b810b43da7122c2655c49c178f2 [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) {
22 GrSurfaceDesc desc;
23 desc.fFlags = kNone_GrSurfaceFlags;
24 desc.fWidth = width;
25 desc.fHeight = height;
26 desc.fConfig = config;
27
28 // We don't want to flush the context so we claim we're in the middle of flushing so as to
29 // guarantee we do not recieve a texture with pending IO
30 // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156)
31 static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag;
Brian Osman32342f02017-03-04 08:12:46 -050032 sk_sp<GrTexture> texture(ctx->resourceProvider()->createApproxTexture(desc, kFlags));
Robert Phillips256c37b2017-03-01 14:32:46 -050033 if (!texture) {
34 return nullptr;
35 }
36
37 // MDB TODO: for now, wrap an instantiated texture. Having the deferred instantiation
38 // possess the correct properties (e.g., no pendingIO) should fall out of the system but
39 // should receive special attention.
40 // Note: When switching over to the deferred proxy, use the kExact flag to create
41 // the atlas and assert that the width & height are powers of 2.
Robert Phillips467022b2017-07-21 08:44:46 -040042 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(texture),
43 kTopLeft_GrSurfaceOrigin);
Robert Phillips256c37b2017-03-01 14:32:46 -050044 if (!proxy) {
45 return nullptr;
46 }
47
48 std::unique_ptr<GrDrawOpAtlas> atlas(
49 new GrDrawOpAtlas(ctx, std::move(proxy), numPlotsX, numPlotsY));
50 atlas->registerEvictionCallback(func, data);
51 return atlas;
52}
53
54
joshualitt5df175e2015-11-18 13:37:54 -080055////////////////////////////////////////////////////////////////////////////////
joshualitt5bf99f12015-03-13 11:47:42 -070056
Brian Salomon2ee084e2016-12-16 18:59:19 -050057GrDrawOpAtlas::Plot::Plot(int index, uint64_t genID, int offX, int offY, int width, int height,
58 GrPixelConfig config)
59 : fLastUpload(GrDrawOpUploadToken::AlreadyFlushedToken())
60 , fLastUse(GrDrawOpUploadToken::AlreadyFlushedToken())
61 , fIndex(index)
62 , fGenID(genID)
63 , fID(CreateId(fIndex, fGenID))
64 , fData(nullptr)
65 , fWidth(width)
66 , fHeight(height)
67 , fX(offX)
68 , fY(offY)
69 , fRects(nullptr)
70 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
71 , fConfig(config)
72 , fBytesPerPixel(GrBytesPerPixel(config))
joshualitt5df175e2015-11-18 13:37:54 -080073#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050074 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080075#endif
76{
77 fDirtyRect.setEmpty();
78}
joshualitt5bf99f12015-03-13 11:47:42 -070079
Brian Salomon2ee084e2016-12-16 18:59:19 -050080GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070081 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080082 delete fRects;
83}
joshualitt5bf99f12015-03-13 11:47:42 -070084
Brian Salomon2ee084e2016-12-16 18:59:19 -050085bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -080086 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070087
joshualitt5df175e2015-11-18 13:37:54 -080088 if (!fRects) {
89 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070090 }
91
joshualitt5df175e2015-11-18 13:37:54 -080092 if (!fRects->addRect(width, height, loc)) {
93 return false;
joshualittb4c507e2015-04-08 08:07:59 -070094 }
joshualitt5bf99f12015-03-13 11:47:42 -070095
jvanverthc3d706f2016-04-20 10:33:27 -070096 if (!fData) {
97 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
98 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -080099 }
100 size_t rowBytes = width * fBytesPerPixel;
101 const unsigned char* imagePtr = (const unsigned char*)image;
102 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700103 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800104 dataPtr += fBytesPerPixel * fWidth * loc->fY;
105 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400106 // copy into the data buffer, swizzling as we go if this is ARGB data
107 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
108 for (int i = 0; i < height; ++i) {
109 SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width);
110 dataPtr += fBytesPerPixel * fWidth;
111 imagePtr += rowBytes;
112 }
113 } else {
114 for (int i = 0; i < height; ++i) {
115 memcpy(dataPtr, imagePtr, rowBytes);
116 dataPtr += fBytesPerPixel * fWidth;
117 imagePtr += rowBytes;
118 }
joshualitt5bf99f12015-03-13 11:47:42 -0700119 }
120
joshualitt5df175e2015-11-18 13:37:54 -0800121 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -0800122
joshualitt5df175e2015-11-18 13:37:54 -0800123 loc->fX += fOffset.fX;
124 loc->fY += fOffset.fY;
125 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700126
joshualitt5df175e2015-11-18 13:37:54 -0800127 return true;
128}
joshualitt5bf99f12015-03-13 11:47:42 -0700129
Brian Salomon2ee084e2016-12-16 18:59:19 -0500130void GrDrawOpAtlas::Plot::uploadToTexture(GrDrawOp::WritePixelsFn& writePixels,
131 GrTexture* texture) {
joshualitt5df175e2015-11-18 13:37:54 -0800132 // We should only be issuing uploads if we are in fact dirty
jvanverthc3d706f2016-04-20 10:33:27 -0700133 SkASSERT(fDirty && fData && texture);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500134 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrDrawOpAtlas::Plot::uploadToTexture");
joshualitt5df175e2015-11-18 13:37:54 -0800135 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700136 const unsigned char* dataPtr = fData;
137 dataPtr += rowBytes * fDirtyRect.fTop;
138 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
139 writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
140 fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800141 fDirtyRect.setEmpty();
142 SkDEBUGCODE(fDirty = false;)
143}
144
Brian Salomon2ee084e2016-12-16 18:59:19 -0500145void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800146 if (fRects) {
147 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700148 }
149
joshualitt5df175e2015-11-18 13:37:54 -0800150 fGenID++;
151 fID = CreateId(fIndex, fGenID);
152
153 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700154 if (fData) {
155 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700156 }
157
joshualitt5df175e2015-11-18 13:37:54 -0800158 fDirtyRect.setEmpty();
159 SkDEBUGCODE(fDirty = false;)
160}
joshualitt5bf99f12015-03-13 11:47:42 -0700161
joshualitt5bf99f12015-03-13 11:47:42 -0700162///////////////////////////////////////////////////////////////////////////////
163
Robert Phillips32f28182017-02-28 16:20:03 -0500164GrDrawOpAtlas::GrDrawOpAtlas(GrContext* context, sk_sp<GrTextureProxy> proxy,
165 int numPlotsX, int numPlotsY)
166 : fContext(context)
167 , fProxy(std::move(proxy))
168 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
169 fPlotWidth = fProxy->width() / numPlotsX;
170 fPlotHeight = fProxy->height() / numPlotsY;
robertphillips2b0536f2015-11-06 14:10:42 -0800171 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
Robert Phillips32f28182017-02-28 16:20:03 -0500172 SkASSERT(fPlotWidth * numPlotsX == fProxy->width());
173 SkASSERT(fPlotHeight * numPlotsY == fProxy->height());
robertphillips2b0536f2015-11-06 14:10:42 -0800174
175 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700176
joshualitt5bf99f12015-03-13 11:47:42 -0700177 // set up allocated plots
Brian Salomon2ee084e2016-12-16 18:59:19 -0500178 fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
joshualitt5bf99f12015-03-13 11:47:42 -0700179
Brian Salomon2ee084e2016-12-16 18:59:19 -0500180 sk_sp<Plot>* currPlot = fPlotArray.get();
robertphillips2b0536f2015-11-06 14:10:42 -0800181 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
182 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
183 uint32_t index = r * numPlotsX + c;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500184 currPlot->reset(
Brian Salomon63e79732017-05-15 21:23:13 -0400185 new Plot(index, 1, x, y, fPlotWidth, fPlotHeight, fProxy->config()));
joshualitt5bf99f12015-03-13 11:47:42 -0700186
187 // build LRU list
188 fPlotList.addToHead(currPlot->get());
189 ++currPlot;
190 }
191 }
192}
193
Brian Salomon2ee084e2016-12-16 18:59:19 -0500194void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700195 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
196 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
197 }
198}
199
Robert Phillips256c37b2017-03-01 14:32:46 -0500200inline bool GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700201 this->makeMRU(plot);
202
203 // If our most recent upload has already occurred then we have to insert a new
204 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
205 // This new update will piggy back on that previously scheduled update.
bsalomon342bfc22016-04-01 06:06:20 -0700206 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
jvanverthc3d706f2016-04-20 10:33:27 -0700207 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500208 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500209
Robert Phillips32f28182017-02-28 16:20:03 -0500210 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
211 // Once it is deferred more care must be taken upon instantiation failure.
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400212 if (!fProxy->instantiate(fContext->resourceProvider())) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500213 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500214 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400215 GrTexture* texture = fProxy->priv().peekTexture();
Robert Phillips256c37b2017-03-01 14:32:46 -0500216
217 GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
218 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
219 plotsp->uploadToTexture(writePixels, texture);
220 }
221 );
222 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700223 }
224 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500225 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700226}
227
Brian Salomon2ee084e2016-12-16 18:59:19 -0500228bool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height,
229 const void* image, SkIPoint16* loc) {
joshualitt5bf99f12015-03-13 11:47:42 -0700230 // We should already have a texture, TODO clean this up
Robert Phillips32f28182017-02-28 16:20:03 -0500231 SkASSERT(fProxy);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700232 if (width > fPlotWidth || height > fPlotHeight) {
233 return false;
234 }
joshualitt5bf99f12015-03-13 11:47:42 -0700235
236 // now look through all allocated plots for one we can share, in Most Recently Refed order
Brian Salomon2ee084e2016-12-16 18:59:19 -0500237 PlotList::Iter plotIter;
238 plotIter.init(fPlotList, PlotList::Iter::kHead_IterStart);
239 Plot* plot;
joshualitt5bf99f12015-03-13 11:47:42 -0700240 while ((plot = plotIter.get())) {
Brian Salomon63e79732017-05-15 21:23:13 -0400241 SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800242 if (plot->addSubImage(width, height, image, loc)) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500243 return this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700244 }
245 plotIter.next();
246 }
247
248 // If the above fails, then see if the least recently refed plot has already been flushed to the
249 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800250 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700251 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700252 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700253 this->processEviction(plot->id());
254 plot->resetRects();
Brian Salomon63e79732017-05-15 21:23:13 -0400255 SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800256 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700257 SkASSERT(verify);
Robert Phillips256c37b2017-03-01 14:32:46 -0500258 if (!this->updatePlot(target, id, plot)) {
259 return false;
260 }
261
joshualitt7c3a2f82015-03-31 13:32:05 -0700262 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700263 return true;
264 }
265
Brian Salomon2ee084e2016-12-16 18:59:19 -0500266 // If this plot has been used in a draw that is currently being prepared by an op, then we have
267 // to fail. This gives the op a chance to enqueue the draw, and call back into this function.
268 // When that draw is enqueued, the draw token advances, and the subsequent call will continue
269 // past this branch and prepare an inline upload that will occur after the enqueued draw which
270 // references the plot's pre-upload content.
bsalomon342bfc22016-04-01 06:06:20 -0700271 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700272 return false;
273 }
274
joshualitt5bf99f12015-03-13 11:47:42 -0700275 this->processEviction(plot->id());
276 fPlotList.remove(plot);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500277 sk_sp<Plot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800278 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700279
280 fPlotList.addToHead(newPlot.get());
Brian Salomon63e79732017-05-15 21:23:13 -0400281 SkASSERT(GrBytesPerPixel(fProxy->config()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800282 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700283 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800284
robertphillips1f0e3502015-11-10 10:19:50 -0800285 // Note that this plot will be uploaded inline with the draws whereas the
286 // one it displaced most likely was uploaded asap.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500287 // With c+14 we could move sk_sp into lambda to only ref once.
288 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips32f28182017-02-28 16:20:03 -0500289 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
290 // Once it is deferred more care must be taken upon instantiation failure.
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400291 if (!fProxy->instantiate(fContext->resourceProvider())) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500292 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500293 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400294 GrTexture* texture = fProxy->priv().peekTexture();
bsalomon342bfc22016-04-01 06:06:20 -0700295
Robert Phillips256c37b2017-03-01 14:32:46 -0500296 GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
297 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
298 plotsp->uploadToTexture(writePixels, texture);
299 }
300 );
301 newPlot->setLastUploadToken(lastUploadToken);
302
joshualitt5bf99f12015-03-13 11:47:42 -0700303 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800304
joshualitt7c3a2f82015-03-31 13:32:05 -0700305 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700306 return true;
307}