blob: ebd0907841ce3bb1f17694b5315f30aace5ad420 [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
joshualitt5bf99f12015-03-13 11:47:42 -070014static inline void adjust_for_offset(SkIPoint16* loc, const SkIPoint16& offset) {
15 loc->fX += offset.fX;
16 loc->fY += offset.fY;
17}
18
joshualitt8db6fdc2015-07-31 08:25:07 -070019static GrBatchAtlas::AtlasID create_id(uint32_t index, uint64_t generation) {
joshualitt5bf99f12015-03-13 11:47:42 -070020 SkASSERT(index < (1 << 16));
joshualitt8db6fdc2015-07-31 08:25:07 -070021 SkASSERT(generation < ((uint64_t)1 << 48));
joshualitt5bf99f12015-03-13 11:47:42 -070022 return generation << 16 | index;
23}
24
25// The backing GrTexture for a GrBatchAtlas is broken into a spatial grid of GrBatchPlots.
26// The GrBatchPlots keep track of subimage placement via their GrRectanizer. In turn, a GrBatchPlot
27// manages the lifetime of its data using two tokens, a last ref toke and a last upload token.
28// Once a GrBatchPlot is "full" (i.e. there is no room for the new subimage according to the
29// GrRectanizer), it can no longer be used unless the last ref on the GrPlot has already been
30// flushed through to the gpu.
31
32class BatchPlot : public SkRefCnt {
33public:
joshualitt5bf99f12015-03-13 11:47:42 -070034 SK_DECLARE_INTERNAL_LLIST_INTERFACE(BatchPlot);
35
36 // index() refers to the index of the plot in the owning GrAtlas's plot array. genID() is a
37 // monotonically incrementing number which is bumped every time the cpu backing store is
38 // wiped, or when the plot itself is evicted from the atlas(ie, there is continuity in genID()
39 // across atlas spills)
joshualitt8db6fdc2015-07-31 08:25:07 -070040 uint32_t index() const { return fIndex; }
41 uint64_t genID() const { return fGenID; }
joshualitt65e96b42015-07-31 11:45:22 -070042 GrBatchAtlas::AtlasID id() {
43 SkASSERT(GrBatchAtlas::kInvalidAtlasID != fID);
44 return fID;
45 }
joshualitt5bf99f12015-03-13 11:47:42 -070046
47 GrTexture* texture() const { return fTexture; }
48
49 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc, size_t rowBytes) {
50 if (!fRects->addRect(width, height, loc)) {
51 return false;
52 }
53
joshualitte062db92015-04-24 08:33:21 -070054 if (!fData) {
55 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
56 fHeight));
57 }
joshualitt5bf99f12015-03-13 11:47:42 -070058 const unsigned char* imagePtr = (const unsigned char*)image;
59 // point ourselves at the right starting spot
60 unsigned char* dataPtr = fData;
61 dataPtr += fBytesPerPixel * fWidth * loc->fY;
62 dataPtr += fBytesPerPixel * loc->fX;
63 // copy into the data buffer
64 for (int i = 0; i < height; ++i) {
65 memcpy(dataPtr, imagePtr, rowBytes);
66 dataPtr += fBytesPerPixel * fWidth;
67 imagePtr += rowBytes;
68 }
69
70 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
71 adjust_for_offset(loc, fOffset);
72 SkDEBUGCODE(fDirty = true;)
73
joshualitt5bf99f12015-03-13 11:47:42 -070074 return true;
75 }
76
77 // to manage the lifetime of a plot, we use two tokens. We use last upload token to know when
78 // we can 'piggy back' uploads, ie if the last upload hasn't been flushed to gpu, we don't need
79 // to issue a new upload even if we update the cpu backing store. We use lastref to determine
80 // when we can evict a plot from the cache, ie if the last ref has already flushed through
81 // the gpu then we can reuse the plot
bsalomon75398562015-08-17 12:55:38 -070082 GrBatchToken lastUploadToken() const { return fLastUpload; }
83 GrBatchToken lastUseToken() const { return fLastUse; }
84 void setLastUploadToken(GrBatchToken batchToken) {
joshualittb4c507e2015-04-08 08:07:59 -070085 SkASSERT(batchToken >= fLastUpload);
86 fLastUpload = batchToken;
87 }
bsalomon75398562015-08-17 12:55:38 -070088 void setLastUseToken(GrBatchToken batchToken) {
joshualittb4c507e2015-04-08 08:07:59 -070089 SkASSERT(batchToken >= fLastUse);
90 fLastUse = batchToken;
91 }
joshualitt5bf99f12015-03-13 11:47:42 -070092
bsalomon75398562015-08-17 12:55:38 -070093 void uploadToTexture(GrBatchUploader::TextureUploader* uploader) {
joshualitt5bf99f12015-03-13 11:47:42 -070094 // We should only be issuing uploads if we are in fact dirty
joshualitte062db92015-04-24 08:33:21 -070095 SkASSERT(fDirty && fData && fTexture);
joshualitt5bf99f12015-03-13 11:47:42 -070096 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture");
joshualitt5bf99f12015-03-13 11:47:42 -070097 size_t rowBytes = fBytesPerPixel * fRects->width();
98 const unsigned char* dataPtr = fData;
99 dataPtr += rowBytes * fDirtyRect.fTop;
100 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
bsalomon75398562015-08-17 12:55:38 -0700101 uploader->writeTexturePixels(fTexture,
102 fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
103 fDirtyRect.width(), fDirtyRect.height(),
104 fTexture->config(), dataPtr, rowBytes);
joshualitt5bf99f12015-03-13 11:47:42 -0700105 fDirtyRect.setEmpty();
106 SkDEBUGCODE(fDirty = false;)
107 }
108
109 void resetRects() {
110 SkASSERT(fRects);
111 fRects->reset();
112 fGenID++;
113 fID = create_id(fIndex, fGenID);
114
115 // zero out the plot
joshualitte062db92015-04-24 08:33:21 -0700116 if (fData) {
117 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
118 }
joshualitt5bf99f12015-03-13 11:47:42 -0700119
120 fDirtyRect.setEmpty();
121 SkDEBUGCODE(fDirty = false;)
122 }
123
joshualitt8db6fdc2015-07-31 08:25:07 -0700124 uint32_t x() const { return fX; }
125 uint32_t y() const { return fY; }
joshualitt5bf99f12015-03-13 11:47:42 -0700126
127private:
128 BatchPlot()
129 : fLastUpload(0)
joshualittb4c507e2015-04-08 08:07:59 -0700130 , fLastUse(0)
joshualitt5bf99f12015-03-13 11:47:42 -0700131 , fIndex(-1)
132 , fGenID(-1)
133 , fID(0)
halcanary96fcdcc2015-08-27 07:41:13 -0700134 , fData(nullptr)
joshualitt5bf99f12015-03-13 11:47:42 -0700135 , fWidth(0)
136 , fHeight(0)
137 , fX(0)
138 , fY(0)
halcanary96fcdcc2015-08-27 07:41:13 -0700139 , fTexture(nullptr)
140 , fRects(nullptr)
141 , fAtlas(nullptr)
joshualitt5bf99f12015-03-13 11:47:42 -0700142 , fBytesPerPixel(1)
143 #ifdef SK_DEBUG
144 , fDirty(false)
145 #endif
146 {
147 fOffset.set(0, 0);
148 }
149
150 ~BatchPlot() {
joshualitt8e71d382015-04-24 18:47:11 -0700151 sk_free(fData);
halcanary96fcdcc2015-08-27 07:41:13 -0700152 fData = nullptr;
joshualitt5bf99f12015-03-13 11:47:42 -0700153 delete fRects;
154 }
155
joshualitt8db6fdc2015-07-31 08:25:07 -0700156 void init(GrBatchAtlas* atlas, GrTexture* texture, int index, uint64_t generation,
joshualitt5bf99f12015-03-13 11:47:42 -0700157 int offX, int offY, int width, int height, size_t bpp) {
158 fIndex = index;
159 fGenID = generation;
160 fID = create_id(index, generation);
161 fWidth = width;
162 fHeight = height;
163 fX = offX;
164 fY = offY;
165 fRects = GrRectanizer::Factory(width, height);
166 fAtlas = atlas;
167 fOffset.set(offX * width, offY * height);
168 fBytesPerPixel = bpp;
halcanary96fcdcc2015-08-27 07:41:13 -0700169 fData = nullptr;
joshualitt5bf99f12015-03-13 11:47:42 -0700170 fDirtyRect.setEmpty();
171 SkDEBUGCODE(fDirty = false;)
172 fTexture = texture;
joshualitt5bf99f12015-03-13 11:47:42 -0700173 }
174
bsalomon75398562015-08-17 12:55:38 -0700175 GrBatchToken fLastUpload;
176 GrBatchToken fLastUse;
joshualitt5bf99f12015-03-13 11:47:42 -0700177
178 uint32_t fIndex;
joshualitt8db6fdc2015-07-31 08:25:07 -0700179 uint64_t fGenID;
joshualitt5bf99f12015-03-13 11:47:42 -0700180 GrBatchAtlas::AtlasID fID;
181 unsigned char* fData;
joshualitt8db6fdc2015-07-31 08:25:07 -0700182 uint32_t fWidth;
183 uint32_t fHeight;
184 uint32_t fX;
185 uint32_t fY;
joshualitt5bf99f12015-03-13 11:47:42 -0700186 GrTexture* fTexture;
187 GrRectanizer* fRects;
188 GrBatchAtlas* fAtlas;
189 SkIPoint16 fOffset; // the offset of the plot in the backing texture
190 size_t fBytesPerPixel;
191 SkIRect fDirtyRect;
192 SkDEBUGCODE(bool fDirty;)
193
194 friend class GrBatchAtlas;
195
196 typedef SkRefCnt INHERITED;
197};
198
199////////////////////////////////////////////////////////////////////////////////
200
bsalomon75398562015-08-17 12:55:38 -0700201class GrPlotUploader : public GrBatchUploader {
joshualitt5bf99f12015-03-13 11:47:42 -0700202public:
203 GrPlotUploader(BatchPlot* plot)
204 : INHERITED(plot->lastUploadToken())
205 , fPlot(SkRef(plot)) {
206 SkASSERT(plot);
207 }
208
bsalomon75398562015-08-17 12:55:38 -0700209 void upload(TextureUploader* uploader) override {
joshualitt5bf99f12015-03-13 11:47:42 -0700210 fPlot->uploadToTexture(uploader);
211 }
212
213private:
214 SkAutoTUnref<BatchPlot> fPlot;
215
bsalomon75398562015-08-17 12:55:38 -0700216 typedef GrBatchUploader INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700217};
218
219///////////////////////////////////////////////////////////////////////////////
220
221GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY)
222 : fTexture(texture)
223 , fNumPlotsX(numPlotsX)
224 , fNumPlotsY(numPlotsY)
225 , fPlotWidth(texture->width() / numPlotsX)
joshualitt7c3a2f82015-03-31 13:32:05 -0700226 , fPlotHeight(texture->height() / numPlotsY)
227 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
joshualittb4c507e2015-04-08 08:07:59 -0700228 SkASSERT(fNumPlotsX * fNumPlotsY <= BulkUseTokenUpdater::kMaxPlots);
joshualitt8db6fdc2015-07-31 08:25:07 -0700229 SkASSERT(fPlotWidth * fNumPlotsX == static_cast<uint32_t>(texture->width()));
230 SkASSERT(fPlotHeight * fNumPlotsY == static_cast<uint32_t>(texture->height()));
joshualitt5bf99f12015-03-13 11:47:42 -0700231
232 // We currently do not support compressed atlases...
233 SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig));
234
235 // set up allocated plots
236 fBPP = GrBytesPerPixel(texture->desc().fConfig);
halcanary385fe4d2015-08-26 13:07:48 -0700237 fPlotArray = new SkAutoTUnref<BatchPlot>[(fNumPlotsX * fNumPlotsY)];
joshualitt5bf99f12015-03-13 11:47:42 -0700238
239 SkAutoTUnref<BatchPlot>* currPlot = fPlotArray;
240 for (int y = fNumPlotsY - 1, r = 0; y >= 0; --y, ++r) {
241 for (int x = fNumPlotsX - 1, c = 0; x >= 0; --x, ++c) {
joshualitt8db6fdc2015-07-31 08:25:07 -0700242 uint32_t id = r * fNumPlotsX + c;
halcanary385fe4d2015-08-26 13:07:48 -0700243 currPlot->reset(new BatchPlot);
joshualitt7c3a2f82015-03-31 13:32:05 -0700244 (*currPlot)->init(this, texture, id, 1, x, y, fPlotWidth, fPlotHeight, fBPP);
joshualitt5bf99f12015-03-13 11:47:42 -0700245
246 // build LRU list
247 fPlotList.addToHead(currPlot->get());
248 ++currPlot;
249 }
250 }
251}
252
253GrBatchAtlas::~GrBatchAtlas() {
254 SkSafeUnref(fTexture);
halcanary385fe4d2015-08-26 13:07:48 -0700255 delete[] fPlotArray;
joshualitt5bf99f12015-03-13 11:47:42 -0700256}
257
258void GrBatchAtlas::processEviction(AtlasID id) {
259 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
260 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
261 }
262}
263
264void GrBatchAtlas::makeMRU(BatchPlot* plot) {
265 if (fPlotList.head() == plot) {
266 return;
267 }
268
269 fPlotList.remove(plot);
270 fPlotList.addToHead(plot);
271}
272
bsalomon75398562015-08-17 12:55:38 -0700273inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, BatchPlot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700274 this->makeMRU(plot);
275
276 // If our most recent upload has already occurred then we have to insert a new
277 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
278 // This new update will piggy back on that previously scheduled update.
bsalomon75398562015-08-17 12:55:38 -0700279 if (target->hasTokenBeenFlushed(plot->lastUploadToken())) {
280 plot->setLastUploadToken(target->asapToken());
halcanary385fe4d2015-08-26 13:07:48 -0700281 SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(plot));
bsalomon75398562015-08-17 12:55:38 -0700282 target->upload(uploader);
joshualitt5bf99f12015-03-13 11:47:42 -0700283 }
284 *id = plot->id();
285}
286
bsalomon75398562015-08-17 12:55:38 -0700287bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* batchTarget,
joshualitt5bf99f12015-03-13 11:47:42 -0700288 int width, int height, const void* image, SkIPoint16* loc) {
289 // We should already have a texture, TODO clean this up
joshualitt8db6fdc2015-07-31 08:25:07 -0700290 SkASSERT(fTexture &&
291 static_cast<uint32_t>(width) <= fPlotWidth &&
292 static_cast<uint32_t>(height) <= fPlotHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700293
294 // now look through all allocated plots for one we can share, in Most Recently Refed order
295 GrBatchPlotList::Iter plotIter;
296 plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart);
297 BatchPlot* plot;
298 while ((plot = plotIter.get())) {
299 if (plot->addSubImage(width, height, image, loc, fBPP * width)) {
300 this->updatePlot(batchTarget, id, plot);
301 return true;
302 }
303 plotIter.next();
304 }
305
306 // If the above fails, then see if the least recently refed plot has already been flushed to the
307 // gpu
308 plotIter.init(fPlotList, GrBatchPlotList::Iter::kTail_IterStart);
309 plot = plotIter.get();
310 SkASSERT(plot);
bsalomon75398562015-08-17 12:55:38 -0700311 if (batchTarget->hasTokenBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700312 this->processEviction(plot->id());
313 plot->resetRects();
314 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc, fBPP * width);
315 SkASSERT(verify);
316 this->updatePlot(batchTarget, id, plot);
joshualitt7c3a2f82015-03-31 13:32:05 -0700317 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700318 return true;
319 }
320
321 // The least recently refed plot hasn't been flushed to the gpu yet, however, if we have flushed
322 // it to the batch target than we can reuse it. Our last ref token is guaranteed to be less
323 // than or equal to the current token. If its 'less than' the current token, than we can spin
324 // off the plot(ie let the batch target manage it) and create a new plot in its place in our
325 // array. If it is equal to the currentToken, then the caller has to flush draws to the batch
326 // target so we can spin off the plot
joshualittb4c507e2015-04-08 08:07:59 -0700327 if (plot->lastUseToken() == batchTarget->currentToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700328 return false;
329 }
330
331 // We take an extra ref here so our plot isn't deleted when we reset its index in the array.
332 plot->ref();
333 int index = plot->index();
334 int x = plot->x();
335 int y = plot->y();
joshualitt8db6fdc2015-07-31 08:25:07 -0700336 uint64_t generation = plot->genID();
joshualitt5bf99f12015-03-13 11:47:42 -0700337
338 this->processEviction(plot->id());
339 fPlotList.remove(plot);
340 SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()];
halcanary385fe4d2015-08-26 13:07:48 -0700341 newPlot.reset(new BatchPlot);
joshualitt5bf99f12015-03-13 11:47:42 -0700342 newPlot->init(this, fTexture, index, ++generation, x, y, fPlotWidth, fPlotHeight, fBPP);
343
344 fPlotList.addToHead(newPlot.get());
345 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc, fBPP * width);
346 SkASSERT(verify);
347 newPlot->setLastUploadToken(batchTarget->currentToken());
halcanary385fe4d2015-08-26 13:07:48 -0700348 SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(newPlot));
joshualitt5bf99f12015-03-13 11:47:42 -0700349 batchTarget->upload(uploader);
350 *id = newPlot->id();
351 plot->unref();
joshualitt7c3a2f82015-03-31 13:32:05 -0700352 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700353 return true;
354}
355
356bool GrBatchAtlas::hasID(AtlasID id) {
joshualitt8db6fdc2015-07-31 08:25:07 -0700357 uint32_t index = GetIndexFromID(id);
joshualitt5bf99f12015-03-13 11:47:42 -0700358 SkASSERT(index < fNumPlotsX * fNumPlotsY);
joshualittb4c507e2015-04-08 08:07:59 -0700359 return fPlotArray[index]->genID() == GetGenerationFromID(id);
joshualitt5bf99f12015-03-13 11:47:42 -0700360}
361
bsalomon75398562015-08-17 12:55:38 -0700362void GrBatchAtlas::setLastUseToken(AtlasID id, GrBatchToken batchToken) {
joshualitt5bf99f12015-03-13 11:47:42 -0700363 SkASSERT(this->hasID(id));
joshualitt8db6fdc2015-07-31 08:25:07 -0700364 uint32_t index = GetIndexFromID(id);
joshualittb4c507e2015-04-08 08:07:59 -0700365 SkASSERT(index < fNumPlotsX * fNumPlotsY);
joshualitt5bf99f12015-03-13 11:47:42 -0700366 this->makeMRU(fPlotArray[index]);
joshualittb4c507e2015-04-08 08:07:59 -0700367 fPlotArray[index]->setLastUseToken(batchToken);
368}
369
bsalomon75398562015-08-17 12:55:38 -0700370void GrBatchAtlas::setLastUseTokenBulk(const BulkUseTokenUpdater& updater,
371 GrBatchToken batchToken) {
joshualitt4314e082015-04-23 08:03:35 -0700372 int count = updater.fPlotsToUpdate.count();
373 for (int i = 0; i < count; i++) {
joshualittb4c507e2015-04-08 08:07:59 -0700374 BatchPlot* plot = fPlotArray[updater.fPlotsToUpdate[i]];
375 this->makeMRU(plot);
376 plot->setLastUseToken(batchToken);
377 }
joshualitt5bf99f12015-03-13 11:47:42 -0700378}