blob: 143bdd81fb904aeba7878acb62547001fcffe329 [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
27 // We don't want to flush the context so we claim we're in the middle of flushing so as to
28 // guarantee we do not recieve a texture with pending IO
29 // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156)
30 static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag;
Brian Osman32342f02017-03-04 08:12:46 -050031 sk_sp<GrTexture> texture(ctx->resourceProvider()->createApproxTexture(desc, kFlags));
Robert Phillips256c37b2017-03-01 14:32:46 -050032 if (!texture) {
33 return nullptr;
34 }
35
36 // MDB TODO: for now, wrap an instantiated texture. Having the deferred instantiation
37 // possess the correct properties (e.g., no pendingIO) should fall out of the system but
38 // should receive special attention.
39 // Note: When switching over to the deferred proxy, use the kExact flag to create
40 // the atlas and assert that the width & height are powers of 2.
41 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(texture));
42 if (!proxy) {
43 return nullptr;
44 }
45
46 std::unique_ptr<GrDrawOpAtlas> atlas(
47 new GrDrawOpAtlas(ctx, std::move(proxy), numPlotsX, numPlotsY));
48 atlas->registerEvictionCallback(func, data);
49 return atlas;
50}
51
52
joshualitt5df175e2015-11-18 13:37:54 -080053////////////////////////////////////////////////////////////////////////////////
joshualitt5bf99f12015-03-13 11:47:42 -070054
Brian Salomon2ee084e2016-12-16 18:59:19 -050055GrDrawOpAtlas::Plot::Plot(int index, uint64_t genID, int offX, int offY, int width, int height,
56 GrPixelConfig config)
57 : fLastUpload(GrDrawOpUploadToken::AlreadyFlushedToken())
58 , fLastUse(GrDrawOpUploadToken::AlreadyFlushedToken())
59 , fIndex(index)
60 , fGenID(genID)
61 , fID(CreateId(fIndex, fGenID))
62 , fData(nullptr)
63 , fWidth(width)
64 , fHeight(height)
65 , fX(offX)
66 , fY(offY)
67 , fRects(nullptr)
68 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
69 , fConfig(config)
70 , fBytesPerPixel(GrBytesPerPixel(config))
joshualitt5df175e2015-11-18 13:37:54 -080071#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050072 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080073#endif
74{
75 fDirtyRect.setEmpty();
76}
joshualitt5bf99f12015-03-13 11:47:42 -070077
Brian Salomon2ee084e2016-12-16 18:59:19 -050078GrDrawOpAtlas::Plot::~Plot() {
jvanverthc3d706f2016-04-20 10:33:27 -070079 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080080 delete fRects;
81}
joshualitt5bf99f12015-03-13 11:47:42 -070082
Brian Salomon2ee084e2016-12-16 18:59:19 -050083bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -080084 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070085
joshualitt5df175e2015-11-18 13:37:54 -080086 if (!fRects) {
87 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070088 }
89
joshualitt5df175e2015-11-18 13:37:54 -080090 if (!fRects->addRect(width, height, loc)) {
91 return false;
joshualittb4c507e2015-04-08 08:07:59 -070092 }
joshualitt5bf99f12015-03-13 11:47:42 -070093
jvanverthc3d706f2016-04-20 10:33:27 -070094 if (!fData) {
95 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
96 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -080097 }
98 size_t rowBytes = width * fBytesPerPixel;
99 const unsigned char* imagePtr = (const unsigned char*)image;
100 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -0700101 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -0800102 dataPtr += fBytesPerPixel * fWidth * loc->fY;
103 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -0400104 // copy into the data buffer, swizzling as we go if this is ARGB data
105 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
106 for (int i = 0; i < height; ++i) {
107 SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width);
108 dataPtr += fBytesPerPixel * fWidth;
109 imagePtr += rowBytes;
110 }
111 } else {
112 for (int i = 0; i < height; ++i) {
113 memcpy(dataPtr, imagePtr, rowBytes);
114 dataPtr += fBytesPerPixel * fWidth;
115 imagePtr += rowBytes;
116 }
joshualitt5bf99f12015-03-13 11:47:42 -0700117 }
118
joshualitt5df175e2015-11-18 13:37:54 -0800119 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -0800120
joshualitt5df175e2015-11-18 13:37:54 -0800121 loc->fX += fOffset.fX;
122 loc->fY += fOffset.fY;
123 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -0700124
joshualitt5df175e2015-11-18 13:37:54 -0800125 return true;
126}
joshualitt5bf99f12015-03-13 11:47:42 -0700127
Brian Salomon2ee084e2016-12-16 18:59:19 -0500128void GrDrawOpAtlas::Plot::uploadToTexture(GrDrawOp::WritePixelsFn& writePixels,
129 GrTexture* texture) {
joshualitt5df175e2015-11-18 13:37:54 -0800130 // We should only be issuing uploads if we are in fact dirty
jvanverthc3d706f2016-04-20 10:33:27 -0700131 SkASSERT(fDirty && fData && texture);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500132 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrDrawOpAtlas::Plot::uploadToTexture");
joshualitt5df175e2015-11-18 13:37:54 -0800133 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -0700134 const unsigned char* dataPtr = fData;
135 dataPtr += rowBytes * fDirtyRect.fTop;
136 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
137 writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
138 fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800139 fDirtyRect.setEmpty();
140 SkDEBUGCODE(fDirty = false;)
141}
142
Brian Salomon2ee084e2016-12-16 18:59:19 -0500143void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800144 if (fRects) {
145 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700146 }
147
joshualitt5df175e2015-11-18 13:37:54 -0800148 fGenID++;
149 fID = CreateId(fIndex, fGenID);
150
151 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700152 if (fData) {
153 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700154 }
155
joshualitt5df175e2015-11-18 13:37:54 -0800156 fDirtyRect.setEmpty();
157 SkDEBUGCODE(fDirty = false;)
158}
joshualitt5bf99f12015-03-13 11:47:42 -0700159
joshualitt5bf99f12015-03-13 11:47:42 -0700160///////////////////////////////////////////////////////////////////////////////
161
Robert Phillips32f28182017-02-28 16:20:03 -0500162GrDrawOpAtlas::GrDrawOpAtlas(GrContext* context, sk_sp<GrTextureProxy> proxy,
163 int numPlotsX, int numPlotsY)
164 : fContext(context)
165 , fProxy(std::move(proxy))
166 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
167 fPlotWidth = fProxy->width() / numPlotsX;
168 fPlotHeight = fProxy->height() / numPlotsY;
robertphillips2b0536f2015-11-06 14:10:42 -0800169 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
Robert Phillips32f28182017-02-28 16:20:03 -0500170 SkASSERT(fPlotWidth * numPlotsX == fProxy->width());
171 SkASSERT(fPlotHeight * numPlotsY == fProxy->height());
robertphillips2b0536f2015-11-06 14:10:42 -0800172
173 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700174
175 // We currently do not support compressed atlases...
Robert Phillips32f28182017-02-28 16:20:03 -0500176 SkASSERT(!GrPixelConfigIsCompressed(fProxy->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700177
178 // set up allocated plots
Brian Salomon2ee084e2016-12-16 18:59:19 -0500179 fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
joshualitt5bf99f12015-03-13 11:47:42 -0700180
Brian Salomon2ee084e2016-12-16 18:59:19 -0500181 sk_sp<Plot>* currPlot = fPlotArray.get();
robertphillips2b0536f2015-11-06 14:10:42 -0800182 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
183 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
184 uint32_t index = r * numPlotsX + c;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500185 currPlot->reset(
Robert Phillips32f28182017-02-28 16:20:03 -0500186 new Plot(index, 1, x, y, fPlotWidth, fPlotHeight, fProxy->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700187
188 // build LRU list
189 fPlotList.addToHead(currPlot->get());
190 ++currPlot;
191 }
192 }
193}
194
Brian Salomon2ee084e2016-12-16 18:59:19 -0500195void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700196 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
197 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
198 }
199}
200
Robert Phillips256c37b2017-03-01 14:32:46 -0500201inline bool GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700202 this->makeMRU(plot);
203
204 // If our most recent upload has already occurred then we have to insert a new
205 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
206 // This new update will piggy back on that previously scheduled update.
bsalomon342bfc22016-04-01 06:06:20 -0700207 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
jvanverthc3d706f2016-04-20 10:33:27 -0700208 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500209 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500210
Robert Phillips32f28182017-02-28 16:20:03 -0500211 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
212 // Once it is deferred more care must be taken upon instantiation failure.
Brian Osman32342f02017-03-04 08:12:46 -0500213 GrTexture* texture = fProxy->instantiate(fContext->resourceProvider());
Robert Phillips256c37b2017-03-01 14:32:46 -0500214 if (!texture) {
215 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500216 }
Robert Phillips256c37b2017-03-01 14:32:46 -0500217
218 GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
219 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
220 plotsp->uploadToTexture(writePixels, texture);
221 }
222 );
223 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700224 }
225 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500226 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700227}
228
Brian Salomon2ee084e2016-12-16 18:59:19 -0500229bool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height,
230 const void* image, SkIPoint16* loc) {
joshualitt5bf99f12015-03-13 11:47:42 -0700231 // We should already have a texture, TODO clean this up
Robert Phillips32f28182017-02-28 16:20:03 -0500232 SkASSERT(fProxy);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700233 if (width > fPlotWidth || height > fPlotHeight) {
234 return false;
235 }
joshualitt5bf99f12015-03-13 11:47:42 -0700236
237 // now look through all allocated plots for one we can share, in Most Recently Refed order
Brian Salomon2ee084e2016-12-16 18:59:19 -0500238 PlotList::Iter plotIter;
239 plotIter.init(fPlotList, PlotList::Iter::kHead_IterStart);
240 Plot* plot;
joshualitt5bf99f12015-03-13 11:47:42 -0700241 while ((plot = plotIter.get())) {
Robert Phillips32f28182017-02-28 16:20:03 -0500242 SkASSERT(GrBytesPerPixel(fProxy->desc().fConfig) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800243 if (plot->addSubImage(width, height, image, loc)) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500244 return this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700245 }
246 plotIter.next();
247 }
248
249 // If the above fails, then see if the least recently refed plot has already been flushed to the
250 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800251 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700252 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700253 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700254 this->processEviction(plot->id());
255 plot->resetRects();
Robert Phillips32f28182017-02-28 16:20:03 -0500256 SkASSERT(GrBytesPerPixel(fProxy->desc().fConfig) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800257 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700258 SkASSERT(verify);
Robert Phillips256c37b2017-03-01 14:32:46 -0500259 if (!this->updatePlot(target, id, plot)) {
260 return false;
261 }
262
joshualitt7c3a2f82015-03-31 13:32:05 -0700263 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700264 return true;
265 }
266
Brian Salomon2ee084e2016-12-16 18:59:19 -0500267 // If this plot has been used in a draw that is currently being prepared by an op, then we have
268 // to fail. This gives the op a chance to enqueue the draw, and call back into this function.
269 // When that draw is enqueued, the draw token advances, and the subsequent call will continue
270 // past this branch and prepare an inline upload that will occur after the enqueued draw which
271 // references the plot's pre-upload content.
bsalomon342bfc22016-04-01 06:06:20 -0700272 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700273 return false;
274 }
275
joshualitt5bf99f12015-03-13 11:47:42 -0700276 this->processEviction(plot->id());
277 fPlotList.remove(plot);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500278 sk_sp<Plot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800279 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700280
281 fPlotList.addToHead(newPlot.get());
Robert Phillips32f28182017-02-28 16:20:03 -0500282 SkASSERT(GrBytesPerPixel(fProxy->desc().fConfig) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800283 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700284 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800285
robertphillips1f0e3502015-11-10 10:19:50 -0800286 // Note that this plot will be uploaded inline with the draws whereas the
287 // one it displaced most likely was uploaded asap.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500288 // With c+14 we could move sk_sp into lambda to only ref once.
289 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips32f28182017-02-28 16:20:03 -0500290 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
291 // Once it is deferred more care must be taken upon instantiation failure.
Brian Osman32342f02017-03-04 08:12:46 -0500292 GrTexture* texture = fProxy->instantiate(fContext->resourceProvider());
Robert Phillips256c37b2017-03-01 14:32:46 -0500293 if (!texture) {
294 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500295 }
bsalomon342bfc22016-04-01 06:06:20 -0700296
Robert Phillips256c37b2017-03-01 14:32:46 -0500297 GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
298 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
299 plotsp->uploadToTexture(writePixels, texture);
300 }
301 );
302 newPlot->setLastUploadToken(lastUploadToken);
303
joshualitt5bf99f12015-03-13 11:47:42 -0700304 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800305
joshualitt7c3a2f82015-03-31 13:32:05 -0700306 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700307 return true;
308}