blob: 78f6ba4b078b08e7c5ac921d9e81f36fab36db9f [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
joshualitt5bf99f12015-03-13 11:47:42 -0700175 // set up allocated plots
Brian Salomon2ee084e2016-12-16 18:59:19 -0500176 fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
joshualitt5bf99f12015-03-13 11:47:42 -0700177
Brian Salomon2ee084e2016-12-16 18:59:19 -0500178 sk_sp<Plot>* currPlot = fPlotArray.get();
robertphillips2b0536f2015-11-06 14:10:42 -0800179 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
180 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
181 uint32_t index = r * numPlotsX + c;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500182 currPlot->reset(
Brian Salomon63e79732017-05-15 21:23:13 -0400183 new Plot(index, 1, x, y, fPlotWidth, fPlotHeight, fProxy->config()));
joshualitt5bf99f12015-03-13 11:47:42 -0700184
185 // build LRU list
186 fPlotList.addToHead(currPlot->get());
187 ++currPlot;
188 }
189 }
190}
191
Brian Salomon2ee084e2016-12-16 18:59:19 -0500192void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700193 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
194 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
195 }
196}
197
Robert Phillips256c37b2017-03-01 14:32:46 -0500198inline bool GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700199 this->makeMRU(plot);
200
201 // If our most recent upload has already occurred then we have to insert a new
202 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
203 // This new update will piggy back on that previously scheduled update.
bsalomon342bfc22016-04-01 06:06:20 -0700204 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
jvanverthc3d706f2016-04-20 10:33:27 -0700205 // With c+14 we could move sk_sp into lamba to only ref once.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500206 sk_sp<Plot> plotsp(SkRef(plot));
Robert Phillips256c37b2017-03-01 14:32:46 -0500207
Robert Phillips32f28182017-02-28 16:20:03 -0500208 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
209 // Once it is deferred more care must be taken upon instantiation failure.
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400210 if (!fProxy->instantiate(fContext->resourceProvider())) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500211 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500212 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400213 GrTexture* texture = fProxy->priv().peekTexture();
Robert Phillips256c37b2017-03-01 14:32:46 -0500214
215 GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
216 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
217 plotsp->uploadToTexture(writePixels, texture);
218 }
219 );
220 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700221 }
222 *id = plot->id();
Robert Phillips256c37b2017-03-01 14:32:46 -0500223 return true;
joshualitt5bf99f12015-03-13 11:47:42 -0700224}
225
Brian Salomon2ee084e2016-12-16 18:59:19 -0500226bool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height,
227 const void* image, SkIPoint16* loc) {
joshualitt5bf99f12015-03-13 11:47:42 -0700228 // We should already have a texture, TODO clean this up
Robert Phillips32f28182017-02-28 16:20:03 -0500229 SkASSERT(fProxy);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700230 if (width > fPlotWidth || height > fPlotHeight) {
231 return false;
232 }
joshualitt5bf99f12015-03-13 11:47:42 -0700233
234 // now look through all allocated plots for one we can share, in Most Recently Refed order
Brian Salomon2ee084e2016-12-16 18:59:19 -0500235 PlotList::Iter plotIter;
236 plotIter.init(fPlotList, PlotList::Iter::kHead_IterStart);
237 Plot* plot;
joshualitt5bf99f12015-03-13 11:47:42 -0700238 while ((plot = plotIter.get())) {
Brian Salomon63e79732017-05-15 21:23:13 -0400239 SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800240 if (plot->addSubImage(width, height, image, loc)) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500241 return this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700242 }
243 plotIter.next();
244 }
245
246 // If the above fails, then see if the least recently refed plot has already been flushed to the
247 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800248 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700249 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700250 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700251 this->processEviction(plot->id());
252 plot->resetRects();
Brian Salomon63e79732017-05-15 21:23:13 -0400253 SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800254 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700255 SkASSERT(verify);
Robert Phillips256c37b2017-03-01 14:32:46 -0500256 if (!this->updatePlot(target, id, plot)) {
257 return false;
258 }
259
joshualitt7c3a2f82015-03-31 13:32:05 -0700260 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700261 return true;
262 }
263
Brian Salomon2ee084e2016-12-16 18:59:19 -0500264 // If this plot has been used in a draw that is currently being prepared by an op, then we have
265 // to fail. This gives the op a chance to enqueue the draw, and call back into this function.
266 // When that draw is enqueued, the draw token advances, and the subsequent call will continue
267 // past this branch and prepare an inline upload that will occur after the enqueued draw which
268 // references the plot's pre-upload content.
bsalomon342bfc22016-04-01 06:06:20 -0700269 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700270 return false;
271 }
272
joshualitt5bf99f12015-03-13 11:47:42 -0700273 this->processEviction(plot->id());
274 fPlotList.remove(plot);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500275 sk_sp<Plot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800276 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700277
278 fPlotList.addToHead(newPlot.get());
Brian Salomon63e79732017-05-15 21:23:13 -0400279 SkASSERT(GrBytesPerPixel(fProxy->config()) == newPlot->bpp());
robertphillips2b0536f2015-11-06 14:10:42 -0800280 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700281 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800282
robertphillips1f0e3502015-11-10 10:19:50 -0800283 // Note that this plot will be uploaded inline with the draws whereas the
284 // one it displaced most likely was uploaded asap.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500285 // With c+14 we could move sk_sp into lambda to only ref once.
286 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Robert Phillips32f28182017-02-28 16:20:03 -0500287 // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
288 // Once it is deferred more care must be taken upon instantiation failure.
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400289 if (!fProxy->instantiate(fContext->resourceProvider())) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500290 return false;
Robert Phillips32f28182017-02-28 16:20:03 -0500291 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400292 GrTexture* texture = fProxy->priv().peekTexture();
bsalomon342bfc22016-04-01 06:06:20 -0700293
Robert Phillips256c37b2017-03-01 14:32:46 -0500294 GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
295 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
296 plotsp->uploadToTexture(writePixels, texture);
297 }
298 );
299 newPlot->setLastUploadToken(lastUploadToken);
300
joshualitt5bf99f12015-03-13 11:47:42 -0700301 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800302
joshualitt7c3a2f82015-03-31 13:32:05 -0700303 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700304 return true;
305}