blob: 2d9020644e2e30ca5b6b838a9dbc43bbbcd3a7ae [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
8#include "GrBatchAtlas.h"
bsalomon75398562015-08-17 12:55:38 -07009#include "GrBatchFlushState.h"
joshualitt5bf99f12015-03-13 11:47:42 -070010#include "GrRectanizer.h"
11#include "GrTracing.h"
bsalomon72e3ae42015-04-28 08:08:46 -070012#include "GrVertexBuffer.h"
joshualitt5bf99f12015-03-13 11:47:42 -070013
robertphillips2b0536f2015-11-06 14:10:42 -080014// The backing GrTexture for a GrBatchAtlas is broken into a spatial grid of BatchPlots.
15// The BatchPlots keep track of subimage placement via their GrRectanizer. A BatchPlot
16// manages the lifetime of its data using two tokens, a last use token and a last upload token.
17// Once a BatchPlot is "full" (i.e. there is no room for the new subimage according to the
18// GrRectanizer), it can no longer be used unless the last use of the GrPlot has already been
joshualitt5bf99f12015-03-13 11:47:42 -070019// flushed through to the gpu.
20
21class BatchPlot : public SkRefCnt {
joshualitt5bf99f12015-03-13 11:47:42 -070022 SK_DECLARE_INTERNAL_LLIST_INTERFACE(BatchPlot);
23
robertphillips2b0536f2015-11-06 14:10:42 -080024public:
25 // index() is a unique id for the plot relative to the owning GrAtlas. genID() is a
26 // monotonically incremented number which is bumped every time this plot is
27 // evicted from the cache (i.e., there is continuity in genID() across atlas spills).
joshualitt8db6fdc2015-07-31 08:25:07 -070028 uint32_t index() const { return fIndex; }
29 uint64_t genID() const { return fGenID; }
robertphillips2b0536f2015-11-06 14:10:42 -080030 GrBatchAtlas::AtlasID id() const {
joshualitt65e96b42015-07-31 11:45:22 -070031 SkASSERT(GrBatchAtlas::kInvalidAtlasID != fID);
32 return fID;
33 }
robertphillips2b0536f2015-11-06 14:10:42 -080034 SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel; })
joshualitt5bf99f12015-03-13 11:47:42 -070035
robertphillips2b0536f2015-11-06 14:10:42 -080036 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
37 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070038
robertphillips2b0536f2015-11-06 14:10:42 -080039 if (!fRects) {
40 fRects = GrRectanizer::Factory(fWidth, fHeight);
41 }
42
joshualitt5bf99f12015-03-13 11:47:42 -070043 if (!fRects->addRect(width, height, loc)) {
44 return false;
45 }
46
joshualitte062db92015-04-24 08:33:21 -070047 if (!fData) {
48 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
49 fHeight));
50 }
robertphillips2b0536f2015-11-06 14:10:42 -080051 size_t rowBytes = width * fBytesPerPixel;
joshualitt5bf99f12015-03-13 11:47:42 -070052 const unsigned char* imagePtr = (const unsigned char*)image;
53 // point ourselves at the right starting spot
54 unsigned char* dataPtr = fData;
55 dataPtr += fBytesPerPixel * fWidth * loc->fY;
56 dataPtr += fBytesPerPixel * loc->fX;
57 // copy into the data buffer
58 for (int i = 0; i < height; ++i) {
59 memcpy(dataPtr, imagePtr, rowBytes);
60 dataPtr += fBytesPerPixel * fWidth;
61 imagePtr += rowBytes;
62 }
63
64 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -080065
66 loc->fX += fOffset.fX;
67 loc->fY += fOffset.fY;
joshualitt5bf99f12015-03-13 11:47:42 -070068 SkDEBUGCODE(fDirty = true;)
69
joshualitt5bf99f12015-03-13 11:47:42 -070070 return true;
71 }
72
robertphillips2b0536f2015-11-06 14:10:42 -080073 // To manage the lifetime of a plot, we use two tokens. We use the last upload token to know
74 // when we can 'piggy back' uploads, ie if the last upload hasn't been flushed to gpu, we don't
75 // need to issue a new upload even if we update the cpu backing store. We use lastUse to
76 // determine when we can evict a plot from the cache, ie if the last use has already flushed
77 // through the gpu then we can reuse the plot.
bsalomon75398562015-08-17 12:55:38 -070078 GrBatchToken lastUploadToken() const { return fLastUpload; }
79 GrBatchToken lastUseToken() const { return fLastUse; }
80 void setLastUploadToken(GrBatchToken batchToken) {
joshualittb4c507e2015-04-08 08:07:59 -070081 SkASSERT(batchToken >= fLastUpload);
82 fLastUpload = batchToken;
83 }
bsalomon75398562015-08-17 12:55:38 -070084 void setLastUseToken(GrBatchToken batchToken) {
joshualittb4c507e2015-04-08 08:07:59 -070085 SkASSERT(batchToken >= fLastUse);
86 fLastUse = batchToken;
87 }
joshualitt5bf99f12015-03-13 11:47:42 -070088
robertphillips2b0536f2015-11-06 14:10:42 -080089 void uploadToTexture(GrBatchUploader::TextureUploader* uploader, GrTexture* texture) {
joshualitt5bf99f12015-03-13 11:47:42 -070090 // We should only be issuing uploads if we are in fact dirty
robertphillips2b0536f2015-11-06 14:10:42 -080091 SkASSERT(fDirty && fData && texture);
joshualitt5bf99f12015-03-13 11:47:42 -070092 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture");
robertphillips2b0536f2015-11-06 14:10:42 -080093 size_t rowBytes = fBytesPerPixel * fWidth;
joshualitt5bf99f12015-03-13 11:47:42 -070094 const unsigned char* dataPtr = fData;
95 dataPtr += rowBytes * fDirtyRect.fTop;
96 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
robertphillips2b0536f2015-11-06 14:10:42 -080097 uploader->writeTexturePixels(texture,
bsalomon75398562015-08-17 12:55:38 -070098 fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
99 fDirtyRect.width(), fDirtyRect.height(),
robertphillips2b0536f2015-11-06 14:10:42 -0800100 fConfig, dataPtr, rowBytes);
joshualitt5bf99f12015-03-13 11:47:42 -0700101 fDirtyRect.setEmpty();
102 SkDEBUGCODE(fDirty = false;)
103 }
104
105 void resetRects() {
robertphillips2b0536f2015-11-06 14:10:42 -0800106 if (fRects) {
107 fRects->reset();
108 }
109
joshualitt5bf99f12015-03-13 11:47:42 -0700110 fGenID++;
robertphillips2b0536f2015-11-06 14:10:42 -0800111 fID = CreateId(fIndex, fGenID);
joshualitt5bf99f12015-03-13 11:47:42 -0700112
113 // zero out the plot
joshualitte062db92015-04-24 08:33:21 -0700114 if (fData) {
115 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
116 }
joshualitt5bf99f12015-03-13 11:47:42 -0700117
118 fDirtyRect.setEmpty();
119 SkDEBUGCODE(fDirty = false;)
120 }
121
joshualitt5bf99f12015-03-13 11:47:42 -0700122private:
robertphillips2b0536f2015-11-06 14:10:42 -0800123 BatchPlot(int index, uint64_t genID, int offX, int offY, int width, int height,
124 GrPixelConfig config)
joshualitt5bf99f12015-03-13 11:47:42 -0700125 : fLastUpload(0)
joshualittb4c507e2015-04-08 08:07:59 -0700126 , fLastUse(0)
robertphillips2b0536f2015-11-06 14:10:42 -0800127 , fIndex(index)
128 , fGenID(genID)
129 , fID(CreateId(fIndex, fGenID))
halcanary96fcdcc2015-08-27 07:41:13 -0700130 , fData(nullptr)
robertphillips2b0536f2015-11-06 14:10:42 -0800131 , fWidth(width)
132 , fHeight(height)
133 , fX(offX)
134 , fY(offY)
halcanary96fcdcc2015-08-27 07:41:13 -0700135 , fRects(nullptr)
robertphillips2b0536f2015-11-06 14:10:42 -0800136 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
137 , fConfig(config)
138 , fBytesPerPixel(GrBytesPerPixel(config))
joshualitt5bf99f12015-03-13 11:47:42 -0700139 #ifdef SK_DEBUG
140 , fDirty(false)
141 #endif
142 {
robertphillips2b0536f2015-11-06 14:10:42 -0800143 fDirtyRect.setEmpty();
joshualitt5bf99f12015-03-13 11:47:42 -0700144 }
145
robertphillips2b0536f2015-11-06 14:10:42 -0800146 ~BatchPlot() override {
joshualitt8e71d382015-04-24 18:47:11 -0700147 sk_free(fData);
joshualitt5bf99f12015-03-13 11:47:42 -0700148 delete fRects;
149 }
150
robertphillips2b0536f2015-11-06 14:10:42 -0800151 // Create a clone of this plot. The cloned plot will take the place of the
152 // current plot in the atlas.
153 BatchPlot* clone() const {
154 return new BatchPlot(fIndex, fGenID+1, fX, fY, fWidth, fHeight, fConfig);
joshualitt5bf99f12015-03-13 11:47:42 -0700155 }
156
robertphillips2b0536f2015-11-06 14:10:42 -0800157 static GrBatchAtlas::AtlasID CreateId(uint32_t index, uint64_t generation) {
158 SkASSERT(index < (1 << 16));
159 SkASSERT(generation < ((uint64_t)1 << 48));
160 return generation << 16 | index;
161 }
joshualitt5bf99f12015-03-13 11:47:42 -0700162
robertphillips2b0536f2015-11-06 14:10:42 -0800163 GrBatchToken fLastUpload;
164 GrBatchToken fLastUse;
165
166 const uint32_t fIndex;
167 uint64_t fGenID;
joshualitt5bf99f12015-03-13 11:47:42 -0700168 GrBatchAtlas::AtlasID fID;
robertphillips2b0536f2015-11-06 14:10:42 -0800169 unsigned char* fData;
170 const int fWidth;
171 const int fHeight;
172 const int fX;
173 const int fY;
174 GrRectanizer* fRects;
175 const SkIPoint16 fOffset; // the offset of the plot in the backing texture
176 const GrPixelConfig fConfig;
177 const size_t fBytesPerPixel;
178 SkIRect fDirtyRect;
179 SkDEBUGCODE(bool fDirty;)
joshualitt5bf99f12015-03-13 11:47:42 -0700180
181 friend class GrBatchAtlas;
182
183 typedef SkRefCnt INHERITED;
184};
185
186////////////////////////////////////////////////////////////////////////////////
187
bsalomon75398562015-08-17 12:55:38 -0700188class GrPlotUploader : public GrBatchUploader {
joshualitt5bf99f12015-03-13 11:47:42 -0700189public:
robertphillips2b0536f2015-11-06 14:10:42 -0800190 GrPlotUploader(BatchPlot* plot, GrTexture* texture)
joshualitt5bf99f12015-03-13 11:47:42 -0700191 : INHERITED(plot->lastUploadToken())
robertphillips2b0536f2015-11-06 14:10:42 -0800192 , fPlot(SkRef(plot))
193 , fTexture(texture) {
joshualitt5bf99f12015-03-13 11:47:42 -0700194 SkASSERT(plot);
195 }
196
bsalomon75398562015-08-17 12:55:38 -0700197 void upload(TextureUploader* uploader) override {
robertphillips2b0536f2015-11-06 14:10:42 -0800198 fPlot->uploadToTexture(uploader, fTexture);
joshualitt5bf99f12015-03-13 11:47:42 -0700199 }
200
201private:
202 SkAutoTUnref<BatchPlot> fPlot;
robertphillips2b0536f2015-11-06 14:10:42 -0800203 GrTexture* fTexture;
joshualitt5bf99f12015-03-13 11:47:42 -0700204
bsalomon75398562015-08-17 12:55:38 -0700205 typedef GrBatchUploader INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700206};
207
208///////////////////////////////////////////////////////////////////////////////
209
210GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY)
211 : fTexture(texture)
joshualitt7c3a2f82015-03-31 13:32:05 -0700212 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
robertphillips2b0536f2015-11-06 14:10:42 -0800213
214 int plotWidth = texture->width() / numPlotsX;
215 int plotHeight = texture->height() / numPlotsY;
216 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
217 SkASSERT(plotWidth * numPlotsX == texture->width());
218 SkASSERT(plotHeight * numPlotsY == texture->height());
219
220 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700221
222 // We currently do not support compressed atlases...
223 SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig));
224
225 // set up allocated plots
robertphillips2b0536f2015-11-06 14:10:42 -0800226 fPlotArray = new SkAutoTUnref<BatchPlot>[numPlotsX * numPlotsY];
joshualitt5bf99f12015-03-13 11:47:42 -0700227
228 SkAutoTUnref<BatchPlot>* currPlot = fPlotArray;
robertphillips2b0536f2015-11-06 14:10:42 -0800229 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
230 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
231 uint32_t index = r * numPlotsX + c;
232 currPlot->reset(new BatchPlot(index, 1, x, y, plotWidth, plotHeight,
233 texture->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700234
235 // build LRU list
236 fPlotList.addToHead(currPlot->get());
237 ++currPlot;
238 }
239 }
240}
241
242GrBatchAtlas::~GrBatchAtlas() {
243 SkSafeUnref(fTexture);
halcanary385fe4d2015-08-26 13:07:48 -0700244 delete[] fPlotArray;
joshualitt5bf99f12015-03-13 11:47:42 -0700245}
246
247void GrBatchAtlas::processEviction(AtlasID id) {
248 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
249 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
250 }
251}
252
253void GrBatchAtlas::makeMRU(BatchPlot* plot) {
254 if (fPlotList.head() == plot) {
255 return;
256 }
257
258 fPlotList.remove(plot);
259 fPlotList.addToHead(plot);
260}
261
bsalomon75398562015-08-17 12:55:38 -0700262inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, BatchPlot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700263 this->makeMRU(plot);
264
265 // If our most recent upload has already occurred then we have to insert a new
266 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
267 // This new update will piggy back on that previously scheduled update.
bsalomon75398562015-08-17 12:55:38 -0700268 if (target->hasTokenBeenFlushed(plot->lastUploadToken())) {
269 plot->setLastUploadToken(target->asapToken());
robertphillips2b0536f2015-11-06 14:10:42 -0800270 SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(plot, fTexture));
bsalomon75398562015-08-17 12:55:38 -0700271 target->upload(uploader);
joshualitt5bf99f12015-03-13 11:47:42 -0700272 }
273 *id = plot->id();
274}
275
bsalomon75398562015-08-17 12:55:38 -0700276bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* batchTarget,
joshualitt5bf99f12015-03-13 11:47:42 -0700277 int width, int height, const void* image, SkIPoint16* loc) {
278 // We should already have a texture, TODO clean this up
robertphillips2b0536f2015-11-06 14:10:42 -0800279 SkASSERT(fTexture);
joshualitt5bf99f12015-03-13 11:47:42 -0700280
281 // now look through all allocated plots for one we can share, in Most Recently Refed order
282 GrBatchPlotList::Iter plotIter;
283 plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart);
284 BatchPlot* plot;
285 while ((plot = plotIter.get())) {
robertphillips2b0536f2015-11-06 14:10:42 -0800286 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
287 if (plot->addSubImage(width, height, image, loc)) {
joshualitt5bf99f12015-03-13 11:47:42 -0700288 this->updatePlot(batchTarget, id, plot);
289 return true;
290 }
291 plotIter.next();
292 }
293
294 // If the above fails, then see if the least recently refed plot has already been flushed to the
295 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800296 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700297 SkASSERT(plot);
bsalomon75398562015-08-17 12:55:38 -0700298 if (batchTarget->hasTokenBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700299 this->processEviction(plot->id());
300 plot->resetRects();
robertphillips2b0536f2015-11-06 14:10:42 -0800301 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
302 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700303 SkASSERT(verify);
304 this->updatePlot(batchTarget, id, plot);
joshualitt7c3a2f82015-03-31 13:32:05 -0700305 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700306 return true;
307 }
308
robertphillips2b0536f2015-11-06 14:10:42 -0800309 // The least recently used plot hasn't been flushed to the gpu yet, however, if we have flushed
310 // it to the batch target than we can reuse it. Our last use token is guaranteed to be less
joshualitt5bf99f12015-03-13 11:47:42 -0700311 // than or equal to the current token. If its 'less than' the current token, than we can spin
robertphillips2b0536f2015-11-06 14:10:42 -0800312 // off the plot (ie let the batch target manage it) and create a new plot in its place in our
joshualitt5bf99f12015-03-13 11:47:42 -0700313 // array. If it is equal to the currentToken, then the caller has to flush draws to the batch
314 // target so we can spin off the plot
joshualittb4c507e2015-04-08 08:07:59 -0700315 if (plot->lastUseToken() == batchTarget->currentToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700316 return false;
317 }
318
robertphillips2b0536f2015-11-06 14:10:42 -0800319 SkASSERT(plot->lastUseToken() < batchTarget->currentToken());
320 SkASSERT(!batchTarget->hasTokenBeenFlushed(batchTarget->currentToken()));
321
322 SkASSERT(!plot->unique()); // The GrPlotUpdater should have a ref too
joshualitt5bf99f12015-03-13 11:47:42 -0700323
324 this->processEviction(plot->id());
325 fPlotList.remove(plot);
326 SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800327 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700328
329 fPlotList.addToHead(newPlot.get());
robertphillips2b0536f2015-11-06 14:10:42 -0800330 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp());
331 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700332 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800333
robertphillips1f0e3502015-11-10 10:19:50 -0800334 // Note that this plot will be uploaded inline with the draws whereas the
335 // one it displaced most likely was uploaded asap.
joshualitt5bf99f12015-03-13 11:47:42 -0700336 newPlot->setLastUploadToken(batchTarget->currentToken());
robertphillips2b0536f2015-11-06 14:10:42 -0800337 SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(newPlot, fTexture));
joshualitt5bf99f12015-03-13 11:47:42 -0700338 batchTarget->upload(uploader);
339 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800340
joshualitt7c3a2f82015-03-31 13:32:05 -0700341 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700342 return true;
343}
344
345bool GrBatchAtlas::hasID(AtlasID id) {
joshualitt8db6fdc2015-07-31 08:25:07 -0700346 uint32_t index = GetIndexFromID(id);
robertphillips2b0536f2015-11-06 14:10:42 -0800347 SkASSERT(index < fNumPlots);
joshualittb4c507e2015-04-08 08:07:59 -0700348 return fPlotArray[index]->genID() == GetGenerationFromID(id);
joshualitt5bf99f12015-03-13 11:47:42 -0700349}
350
bsalomon75398562015-08-17 12:55:38 -0700351void GrBatchAtlas::setLastUseToken(AtlasID id, GrBatchToken batchToken) {
joshualitt5bf99f12015-03-13 11:47:42 -0700352 SkASSERT(this->hasID(id));
joshualitt8db6fdc2015-07-31 08:25:07 -0700353 uint32_t index = GetIndexFromID(id);
robertphillips2b0536f2015-11-06 14:10:42 -0800354 SkASSERT(index < fNumPlots);
joshualitt5bf99f12015-03-13 11:47:42 -0700355 this->makeMRU(fPlotArray[index]);
joshualittb4c507e2015-04-08 08:07:59 -0700356 fPlotArray[index]->setLastUseToken(batchToken);
357}
358
bsalomon75398562015-08-17 12:55:38 -0700359void GrBatchAtlas::setLastUseTokenBulk(const BulkUseTokenUpdater& updater,
360 GrBatchToken batchToken) {
joshualitt4314e082015-04-23 08:03:35 -0700361 int count = updater.fPlotsToUpdate.count();
362 for (int i = 0; i < count; i++) {
joshualittb4c507e2015-04-08 08:07:59 -0700363 BatchPlot* plot = fPlotArray[updater.fPlotsToUpdate[i]];
364 this->makeMRU(plot);
365 plot->setLastUseToken(batchToken);
366 }
joshualitt5bf99f12015-03-13 11:47:42 -0700367}