blob: 235bc87a00e4157482ddb85ed3c902efbbf51cb8 [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"
joshualitt5bf99f12015-03-13 11:47:42 -070014#include "GrTracing.h"
15
Robert Phillips256c37b2017-03-01 14:32:46 -050016std::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrContext* ctx, GrPixelConfig config,
17 int width, int height,
18 int numPlotsX, int numPlotsY,
19 GrDrawOpAtlas::EvictionFunc func,
20 void* data) {
21 GrSurfaceDesc desc;
22 desc.fFlags = kNone_GrSurfaceFlags;
23 desc.fWidth = width;
24 desc.fHeight = height;
25 desc.fConfig = config;
26
Brian Salomon8785df12017-05-24 10:07:30 -040027 if (ctx->caps()->mustClearAtlases()) {
28 desc.fFlags |= kPerformInitialClear_GrSurfaceFlag;
29 }
30
Robert Phillips256c37b2017-03-01 14:32:46 -050031 // We don't want to flush the context so we claim we're in the middle of flushing so as to
32 // guarantee we do not recieve a texture with pending IO
33 // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156)
34 static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag;
Brian Osman32342f02017-03-04 08:12:46 -050035 sk_sp<GrTexture> texture(ctx->resourceProvider()->createApproxTexture(desc, kFlags));
Robert Phillips256c37b2017-03-01 14:32:46 -050036 if (!texture) {
37 return nullptr;
38 }
39
40 // MDB TODO: for now, wrap an instantiated texture. Having the deferred instantiation
41 // possess the correct properties (e.g., no pendingIO) should fall out of the system but
42 // should receive special attention.
43 // Note: When switching over to the deferred proxy, use the kExact flag to create
44 // the atlas and assert that the width & height are powers of 2.
45 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(texture));
46 if (!proxy) {
47 return nullptr;
48 }
49
50 std::unique_ptr<GrDrawOpAtlas> atlas(
51 new GrDrawOpAtlas(ctx, std::move(proxy), numPlotsX, numPlotsY));
52 atlas->registerEvictionCallback(func, data);
53 return atlas;
54}
55
56
joshualitt5df175e2015-11-18 13:37:54 -080057////////////////////////////////////////////////////////////////////////////////
joshualitt5bf99f12015-03-13 11:47:42 -070058
Brian Salomon2ee084e2016-12-16 18:59:19 -050059GrDrawOpAtlas::Plot::Plot(int index, uint64_t genID, int offX, int offY, int width, int height,
60 GrPixelConfig config)
61 : fLastUpload(GrDrawOpUploadToken::AlreadyFlushedToken())
62 , fLastUse(GrDrawOpUploadToken::AlreadyFlushedToken())
63 , fIndex(index)
64 , fGenID(genID)
65 , fID(CreateId(fIndex, fGenID))
66 , fData(nullptr)
67 , fWidth(width)
68 , fHeight(height)
69 , fX(offX)
70 , fY(offY)
71 , fRects(nullptr)
72 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
73 , fConfig(config)
74 , fBytesPerPixel(GrBytesPerPixel(config))
joshualitt5df175e2015-11-18 13:37:54 -080075#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050076 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080077#endif
78{
79 fDirtyRect.setEmpty();
80}
joshualitt5bf99f12015-03-13 11:47:42 -070081
Brian Salomon2ee084e2016-12-16 18:59:19 -050082GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070083 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080084 delete fRects;
85}
joshualitt5bf99f12015-03-13 11:47:42 -070086
Brian Salomon2ee084e2016-12-16 18:59:19 -050087bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -080088 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070089
joshualitt5df175e2015-11-18 13:37:54 -080090 if (!fRects) {
91 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070092 }
93
joshualitt5df175e2015-11-18 13:37:54 -080094 if (!fRects->addRect(width, height, loc)) {
95 return false;
joshualittb4c507e2015-04-08 08:07:59 -070096 }
joshualitt5bf99f12015-03-13 11:47:42 -070097
jvanverthc3d706f2016-04-20 10:33:27 -070098 if (!fData) {
99 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
100 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -0800101 }
102 size_t rowBytes = width * fBytesPerPixel;
103 const unsigned char* imagePtr = (const unsigned char*)image;
104 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700105 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800106 dataPtr += fBytesPerPixel * fWidth * loc->fY;
107 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400108 // copy into the data buffer, swizzling as we go if this is ARGB data
109 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
110 for (int i = 0; i < height; ++i) {
111 SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width);
112 dataPtr += fBytesPerPixel * fWidth;
113 imagePtr += rowBytes;
114 }
115 } else {
116 for (int i = 0; i < height; ++i) {
117 memcpy(dataPtr, imagePtr, rowBytes);
118 dataPtr += fBytesPerPixel * fWidth;
119 imagePtr += rowBytes;
120 }
joshualitt5bf99f12015-03-13 11:47:42 -0700121 }
122
joshualitt5df175e2015-11-18 13:37:54 -0800123 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -0800124
joshualitt5df175e2015-11-18 13:37:54 -0800125 loc->fX += fOffset.fX;
126 loc->fY += fOffset.fY;
127 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700128
joshualitt5df175e2015-11-18 13:37:54 -0800129 return true;
130}
joshualitt5bf99f12015-03-13 11:47:42 -0700131
Brian Salomon2ee084e2016-12-16 18:59:19 -0500132void GrDrawOpAtlas::Plot::uploadToTexture(GrDrawOp::WritePixelsFn& writePixels,
133 GrTexture* texture) {
joshualitt5df175e2015-11-18 13:37:54 -0800134 // We should only be issuing uploads if we are in fact dirty
jvanverthc3d706f2016-04-20 10:33:27 -0700135 SkASSERT(fDirty && fData && texture);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500136 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrDrawOpAtlas::Plot::uploadToTexture");
joshualitt5df175e2015-11-18 13:37:54 -0800137 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700138 const unsigned char* dataPtr = fData;
139 dataPtr += rowBytes * fDirtyRect.fTop;
140 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
141 writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
142 fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800143 fDirtyRect.setEmpty();
144 SkDEBUGCODE(fDirty = false;)
145}
146
Brian Salomon2ee084e2016-12-16 18:59:19 -0500147void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800148 if (fRects) {
149 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700150 }
151
joshualitt5df175e2015-11-18 13:37:54 -0800152 fGenID++;
153 fID = CreateId(fIndex, fGenID);
154
155 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700156 if (fData) {
157 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700158 }
159
joshualitt5df175e2015-11-18 13:37:54 -0800160 fDirtyRect.setEmpty();
161 SkDEBUGCODE(fDirty = false;)
162}
joshualitt5bf99f12015-03-13 11:47:42 -0700163
joshualitt5bf99f12015-03-13 11:47:42 -0700164///////////////////////////////////////////////////////////////////////////////
165
Robert Phillips32f28182017-02-28 16:20:03 -0500166GrDrawOpAtlas::GrDrawOpAtlas(GrContext* context, sk_sp<GrTextureProxy> proxy,
167 int numPlotsX, int numPlotsY)
168 : fContext(context)
169 , fProxy(std::move(proxy))
170 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
171 fPlotWidth = fProxy->width() / numPlotsX;
172 fPlotHeight = fProxy->height() / numPlotsY;
robertphillips2b0536f2015-11-06 14:10:42 -0800173 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
Robert Phillips32f28182017-02-28 16:20:03 -0500174 SkASSERT(fPlotWidth * numPlotsX == fProxy->width());
175 SkASSERT(fPlotHeight * numPlotsY == fProxy->height());
robertphillips2b0536f2015-11-06 14:10:42 -0800176
177 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700178
joshualitt5bf99f12015-03-13 11:47:42 -0700179 // set up allocated plots
Brian Salomon2ee084e2016-12-16 18:59:19 -0500180 fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
joshualitt5bf99f12015-03-13 11:47:42 -0700181
Brian Salomon2ee084e2016-12-16 18:59:19 -0500182 sk_sp<Plot>* currPlot = fPlotArray.get();
robertphillips2b0536f2015-11-06 14:10:42 -0800183 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
184 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
185 uint32_t index = r * numPlotsX + c;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500186 currPlot->reset(
Brian Salomon63e79732017-05-15 21:23:13 -0400187 new Plot(index, 1, x, y, fPlotWidth, fPlotHeight, fProxy->config()));
joshualitt5bf99f12015-03-13 11:47:42 -0700188
189 // build LRU list
190 fPlotList.addToHead(currPlot->get());
191 ++currPlot;
192 }
193 }
194}
195
Brian Salomon2ee084e2016-12-16 18:59:19 -0500196void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700197 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
198 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
199 }
200}
201
Robert Phillips256c37b2017-03-01 14:32:46 -0500202inline bool GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700203 this->makeMRU(plot);
204
205 // If our most recent upload has already occurred then we have to insert a new
206 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
207 // This new update will piggy back on that previously scheduled update.
bsalomon342bfc22016-04-01 06:06:20 -0700208 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
jvanverthc3d706f2016-04-20 10:33:27 -0700209 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500210 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500211
Robert Phillips32f28182017-02-28 16:20:03 -0500212 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
213 // Once it is deferred more care must be taken upon instantiation failure.
Brian Salomonbb5711a2017-05-17 13:49:59 -0400214 GrTexture* texture = fProxy->instantiateTexture(fContext->resourceProvider());
Robert Phillips256c37b2017-03-01 14:32:46 -0500215 if (!texture) {
216 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500217 }
Robert Phillips256c37b2017-03-01 14:32:46 -0500218
219 GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
220 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
221 plotsp->uploadToTexture(writePixels, texture);
222 }
223 );
224 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700225 }
226 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500227 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700228}
229
Brian Salomon2ee084e2016-12-16 18:59:19 -0500230bool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height,
231 const void* image, SkIPoint16* loc) {
joshualitt5bf99f12015-03-13 11:47:42 -0700232 // We should already have a texture, TODO clean this up
Robert Phillips32f28182017-02-28 16:20:03 -0500233 SkASSERT(fProxy);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700234 if (width > fPlotWidth || height > fPlotHeight) {
235 return false;
236 }
joshualitt5bf99f12015-03-13 11:47:42 -0700237
238 // now look through all allocated plots for one we can share, in Most Recently Refed order
Brian Salomon2ee084e2016-12-16 18:59:19 -0500239 PlotList::Iter plotIter;
240 plotIter.init(fPlotList, PlotList::Iter::kHead_IterStart);
241 Plot* plot;
joshualitt5bf99f12015-03-13 11:47:42 -0700242 while ((plot = plotIter.get())) {
Brian Salomon63e79732017-05-15 21:23:13 -0400243 SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800244 if (plot->addSubImage(width, height, image, loc)) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500245 return this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700246 }
247 plotIter.next();
248 }
249
250 // If the above fails, then see if the least recently refed plot has already been flushed to the
251 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800252 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700253 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700254 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700255 this->processEviction(plot->id());
256 plot->resetRects();
Brian Salomon63e79732017-05-15 21:23:13 -0400257 SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800258 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700259 SkASSERT(verify);
Robert Phillips256c37b2017-03-01 14:32:46 -0500260 if (!this->updatePlot(target, id, plot)) {
261 return false;
262 }
263
joshualitt7c3a2f82015-03-31 13:32:05 -0700264 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700265 return true;
266 }
267
Brian Salomon2ee084e2016-12-16 18:59:19 -0500268 // If this plot has been used in a draw that is currently being prepared by an op, then we have
269 // to fail. This gives the op a chance to enqueue the draw, and call back into this function.
270 // When that draw is enqueued, the draw token advances, and the subsequent call will continue
271 // past this branch and prepare an inline upload that will occur after the enqueued draw which
272 // references the plot's pre-upload content.
bsalomon342bfc22016-04-01 06:06:20 -0700273 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700274 return false;
275 }
276
joshualitt5bf99f12015-03-13 11:47:42 -0700277 this->processEviction(plot->id());
278 fPlotList.remove(plot);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500279 sk_sp<Plot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800280 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700281
282 fPlotList.addToHead(newPlot.get());
Brian Salomon63e79732017-05-15 21:23:13 -0400283 SkASSERT(GrBytesPerPixel(fProxy->config()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800284 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700285 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800286
robertphillips1f0e3502015-11-10 10:19:50 -0800287 // Note that this plot will be uploaded inline with the draws whereas the
288 // one it displaced most likely was uploaded asap.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500289 // With c+14 we could move sk_sp into lambda to only ref once.
290 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips32f28182017-02-28 16:20:03 -0500291 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
292 // Once it is deferred more care must be taken upon instantiation failure.
Brian Salomonbb5711a2017-05-17 13:49:59 -0400293 GrTexture* texture = fProxy->instantiateTexture(fContext->resourceProvider());
Robert Phillips256c37b2017-03-01 14:32:46 -0500294 if (!texture) {
295 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500296 }
bsalomon342bfc22016-04-01 06:06:20 -0700297
Robert Phillips256c37b2017-03-01 14:32:46 -0500298 GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
299 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
300 plotsp->uploadToTexture(writePixels, texture);
301 }
302 );
303 newPlot->setLastUploadToken(lastUploadToken);
304
joshualitt5bf99f12015-03-13 11:47:42 -0700305 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800306
joshualitt7c3a2f82015-03-31 13:32:05 -0700307 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700308 return true;
309}