blob: 37bad65a1fb20a70cb675b628762a8e8215af1c5 [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 Phillips96be9df2017-07-21 14:17:45 +000042 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(texture));
Robert Phillips256c37b2017-03-01 14:32:46 -050043 if (!proxy) {
44 return nullptr;
45 }
46
47 std::unique_ptr<GrDrawOpAtlas> atlas(
48 new GrDrawOpAtlas(ctx, std::move(proxy), numPlotsX, numPlotsY));
49 atlas->registerEvictionCallback(func, data);
50 return atlas;
51}
52
53
joshualitt5df175e2015-11-18 13:37:54 -080054////////////////////////////////////////////////////////////////////////////////
joshualitt5bf99f12015-03-13 11:47:42 -070055
Brian Salomon2ee084e2016-12-16 18:59:19 -050056GrDrawOpAtlas::Plot::Plot(int index, uint64_t genID, int offX, int offY, int width, int height,
57 GrPixelConfig config)
58 : fLastUpload(GrDrawOpUploadToken::AlreadyFlushedToken())
59 , fLastUse(GrDrawOpUploadToken::AlreadyFlushedToken())
60 , fIndex(index)
61 , fGenID(genID)
62 , fID(CreateId(fIndex, fGenID))
63 , fData(nullptr)
64 , fWidth(width)
65 , fHeight(height)
66 , fX(offX)
67 , fY(offY)
68 , fRects(nullptr)
69 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
70 , fConfig(config)
71 , fBytesPerPixel(GrBytesPerPixel(config))
joshualitt5df175e2015-11-18 13:37:54 -080072#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050073 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080074#endif
75{
76 fDirtyRect.setEmpty();
77}
joshualitt5bf99f12015-03-13 11:47:42 -070078
Brian Salomon2ee084e2016-12-16 18:59:19 -050079GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070080 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080081 delete fRects;
82}
joshualitt5bf99f12015-03-13 11:47:42 -070083
Brian Salomon2ee084e2016-12-16 18:59:19 -050084bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -080085 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070086
joshualitt5df175e2015-11-18 13:37:54 -080087 if (!fRects) {
88 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070089 }
90
joshualitt5df175e2015-11-18 13:37:54 -080091 if (!fRects->addRect(width, height, loc)) {
92 return false;
joshualittb4c507e2015-04-08 08:07:59 -070093 }
joshualitt5bf99f12015-03-13 11:47:42 -070094
jvanverthc3d706f2016-04-20 10:33:27 -070095 if (!fData) {
96 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
97 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -080098 }
99 size_t rowBytes = width * fBytesPerPixel;
100 const unsigned char* imagePtr = (const unsigned char*)image;
101 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700102 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800103 dataPtr += fBytesPerPixel * fWidth * loc->fY;
104 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400105 // copy into the data buffer, swizzling as we go if this is ARGB data
106 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
107 for (int i = 0; i < height; ++i) {
108 SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width);
109 dataPtr += fBytesPerPixel * fWidth;
110 imagePtr += rowBytes;
111 }
112 } else {
113 for (int i = 0; i < height; ++i) {
114 memcpy(dataPtr, imagePtr, rowBytes);
115 dataPtr += fBytesPerPixel * fWidth;
116 imagePtr += rowBytes;
117 }
joshualitt5bf99f12015-03-13 11:47:42 -0700118 }
119
joshualitt5df175e2015-11-18 13:37:54 -0800120 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -0800121
joshualitt5df175e2015-11-18 13:37:54 -0800122 loc->fX += fOffset.fX;
123 loc->fY += fOffset.fY;
124 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700125
joshualitt5df175e2015-11-18 13:37:54 -0800126 return true;
127}
joshualitt5bf99f12015-03-13 11:47:42 -0700128
Brian Salomon2ee084e2016-12-16 18:59:19 -0500129void GrDrawOpAtlas::Plot::uploadToTexture(GrDrawOp::WritePixelsFn& writePixels,
130 GrTexture* texture) {
joshualitt5df175e2015-11-18 13:37:54 -0800131 // We should only be issuing uploads if we are in fact dirty
jvanverthc3d706f2016-04-20 10:33:27 -0700132 SkASSERT(fDirty && fData && texture);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500133 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrDrawOpAtlas::Plot::uploadToTexture");
joshualitt5df175e2015-11-18 13:37:54 -0800134 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700135 const unsigned char* dataPtr = fData;
136 dataPtr += rowBytes * fDirtyRect.fTop;
137 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
138 writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
139 fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800140 fDirtyRect.setEmpty();
141 SkDEBUGCODE(fDirty = false;)
142}
143
Brian Salomon2ee084e2016-12-16 18:59:19 -0500144void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800145 if (fRects) {
146 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700147 }
148
joshualitt5df175e2015-11-18 13:37:54 -0800149 fGenID++;
150 fID = CreateId(fIndex, fGenID);
151
152 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700153 if (fData) {
154 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700155 }
156
joshualitt5df175e2015-11-18 13:37:54 -0800157 fDirtyRect.setEmpty();
158 SkDEBUGCODE(fDirty = false;)
159}
joshualitt5bf99f12015-03-13 11:47:42 -0700160
joshualitt5bf99f12015-03-13 11:47:42 -0700161///////////////////////////////////////////////////////////////////////////////
162
Robert Phillips32f28182017-02-28 16:20:03 -0500163GrDrawOpAtlas::GrDrawOpAtlas(GrContext* context, sk_sp<GrTextureProxy> proxy,
164 int numPlotsX, int numPlotsY)
165 : fContext(context)
166 , fProxy(std::move(proxy))
167 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
168 fPlotWidth = fProxy->width() / numPlotsX;
169 fPlotHeight = fProxy->height() / numPlotsY;
robertphillips2b0536f2015-11-06 14:10:42 -0800170 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
Robert Phillips32f28182017-02-28 16:20:03 -0500171 SkASSERT(fPlotWidth * numPlotsX == fProxy->width());
172 SkASSERT(fPlotHeight * numPlotsY == fProxy->height());
robertphillips2b0536f2015-11-06 14:10:42 -0800173
174 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700175
joshualitt5bf99f12015-03-13 11:47:42 -0700176 // set up allocated plots
Brian Salomon2ee084e2016-12-16 18:59:19 -0500177 fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
joshualitt5bf99f12015-03-13 11:47:42 -0700178
Brian Salomon2ee084e2016-12-16 18:59:19 -0500179 sk_sp<Plot>* currPlot = fPlotArray.get();
robertphillips2b0536f2015-11-06 14:10:42 -0800180 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
181 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
182 uint32_t index = r * numPlotsX + c;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500183 currPlot->reset(
Brian Salomon63e79732017-05-15 21:23:13 -0400184 new Plot(index, 1, x, y, fPlotWidth, fPlotHeight, fProxy->config()));
joshualitt5bf99f12015-03-13 11:47:42 -0700185
186 // build LRU list
187 fPlotList.addToHead(currPlot->get());
188 ++currPlot;
189 }
190 }
191}
192
Brian Salomon2ee084e2016-12-16 18:59:19 -0500193void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700194 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
195 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
196 }
197}
198
Robert Phillips256c37b2017-03-01 14:32:46 -0500199inline bool GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700200 this->makeMRU(plot);
201
202 // If our most recent upload has already occurred then we have to insert a new
203 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
204 // This new update will piggy back on that previously scheduled update.
bsalomon342bfc22016-04-01 06:06:20 -0700205 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
jvanverthc3d706f2016-04-20 10:33:27 -0700206 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500207 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500208
Robert Phillips32f28182017-02-28 16:20:03 -0500209 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
210 // Once it is deferred more care must be taken upon instantiation failure.
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400211 if (!fProxy->instantiate(fContext->resourceProvider())) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500212 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500213 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400214 GrTexture* texture = fProxy->priv().peekTexture();
Robert Phillips256c37b2017-03-01 14:32:46 -0500215
216 GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
217 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
218 plotsp->uploadToTexture(writePixels, texture);
219 }
220 );
221 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700222 }
223 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500224 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700225}
226
Brian Salomon2ee084e2016-12-16 18:59:19 -0500227bool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height,
228 const void* image, SkIPoint16* loc) {
joshualitt5bf99f12015-03-13 11:47:42 -0700229 // We should already have a texture, TODO clean this up
Robert Phillips32f28182017-02-28 16:20:03 -0500230 SkASSERT(fProxy);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700231 if (width > fPlotWidth || height > fPlotHeight) {
232 return false;
233 }
joshualitt5bf99f12015-03-13 11:47:42 -0700234
235 // now look through all allocated plots for one we can share, in Most Recently Refed order
Brian Salomon2ee084e2016-12-16 18:59:19 -0500236 PlotList::Iter plotIter;
237 plotIter.init(fPlotList, PlotList::Iter::kHead_IterStart);
238 Plot* plot;
joshualitt5bf99f12015-03-13 11:47:42 -0700239 while ((plot = plotIter.get())) {
Brian Salomon63e79732017-05-15 21:23:13 -0400240 SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800241 if (plot->addSubImage(width, height, image, loc)) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500242 return this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700243 }
244 plotIter.next();
245 }
246
247 // If the above fails, then see if the least recently refed plot has already been flushed to the
248 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800249 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700250 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700251 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700252 this->processEviction(plot->id());
253 plot->resetRects();
Brian Salomon63e79732017-05-15 21:23:13 -0400254 SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800255 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700256 SkASSERT(verify);
Robert Phillips256c37b2017-03-01 14:32:46 -0500257 if (!this->updatePlot(target, id, plot)) {
258 return false;
259 }
260
joshualitt7c3a2f82015-03-31 13:32:05 -0700261 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700262 return true;
263 }
264
Brian Salomon2ee084e2016-12-16 18:59:19 -0500265 // If this plot has been used in a draw that is currently being prepared by an op, then we have
266 // to fail. This gives the op a chance to enqueue the draw, and call back into this function.
267 // When that draw is enqueued, the draw token advances, and the subsequent call will continue
268 // past this branch and prepare an inline upload that will occur after the enqueued draw which
269 // references the plot's pre-upload content.
bsalomon342bfc22016-04-01 06:06:20 -0700270 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700271 return false;
272 }
273
joshualitt5bf99f12015-03-13 11:47:42 -0700274 this->processEviction(plot->id());
275 fPlotList.remove(plot);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500276 sk_sp<Plot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800277 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700278
279 fPlotList.addToHead(newPlot.get());
Brian Salomon63e79732017-05-15 21:23:13 -0400280 SkASSERT(GrBytesPerPixel(fProxy->config()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800281 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700282 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800283
robertphillips1f0e3502015-11-10 10:19:50 -0800284 // Note that this plot will be uploaded inline with the draws whereas the
285 // one it displaced most likely was uploaded asap.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500286 // With c+14 we could move sk_sp into lambda to only ref once.
287 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips32f28182017-02-28 16:20:03 -0500288 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
289 // Once it is deferred more care must be taken upon instantiation failure.
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400290 if (!fProxy->instantiate(fContext->resourceProvider())) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500291 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500292 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400293 GrTexture* texture = fProxy->priv().peekTexture();
bsalomon342bfc22016-04-01 06:06:20 -0700294
Robert Phillips256c37b2017-03-01 14:32:46 -0500295 GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
296 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
297 plotsp->uploadToTexture(writePixels, texture);
298 }
299 );
300 newPlot->setLastUploadToken(lastUploadToken);
301
joshualitt5bf99f12015-03-13 11:47:42 -0700302 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800303
joshualitt7c3a2f82015-03-31 13:32:05 -0700304 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700305 return true;
306}