2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrBatchAtlas.h"
9 #include "GrBatchTarget.h"
11 #include "GrRectanizer.h"
12 #include "GrTracing.h"
17 static int g_UploadCount = 0;
20 static inline void adjust_for_offset(SkIPoint16* loc, const SkIPoint16& offset) {
25 static GrBatchAtlas::AtlasID create_id(int index, int generation) {
26 // Generation ID can roll over because we only check for equality
27 SkASSERT(index < (1 << 16));
28 return generation << 16 | index;
31 // The backing GrTexture for a GrBatchAtlas is broken into a spatial grid of GrBatchPlots.
32 // The GrBatchPlots keep track of subimage placement via their GrRectanizer. In turn, a GrBatchPlot
33 // manages the lifetime of its data using two tokens, a last ref toke and a last upload token.
34 // Once a GrBatchPlot is "full" (i.e. there is no room for the new subimage according to the
35 // GrRectanizer), it can no longer be used unless the last ref on the GrPlot has already been
36 // flushed through to the gpu.
38 class BatchPlot : public SkRefCnt {
40 typedef GrBatchAtlas::BatchToken BatchToken;
41 SK_DECLARE_INST_COUNT(BatchPlot);
42 SK_DECLARE_INTERNAL_LLIST_INTERFACE(BatchPlot);
44 // index() refers to the index of the plot in the owning GrAtlas's plot array. genID() is a
45 // monotonically incrementing number which is bumped every time the cpu backing store is
46 // wiped, or when the plot itself is evicted from the atlas(ie, there is continuity in genID()
47 // across atlas spills)
48 int index() const { return fIndex; }
49 int genID() const { return fGenID; }
50 GrBatchAtlas::AtlasID id() { return fID; }
52 GrTexture* texture() const { return fTexture; }
54 bool addSubImage(int width, int height, const void* image, SkIPoint16* loc, size_t rowBytes) {
55 if (!fRects->addRect(width, height, loc)) {
60 const unsigned char* imagePtr = (const unsigned char*)image;
61 // point ourselves at the right starting spot
62 unsigned char* dataPtr = fData;
63 dataPtr += fBytesPerPixel * fWidth * loc->fY;
64 dataPtr += fBytesPerPixel * loc->fX;
65 // copy into the data buffer
66 for (int i = 0; i < height; ++i) {
67 memcpy(dataPtr, imagePtr, rowBytes);
68 dataPtr += fBytesPerPixel * fWidth;
72 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
73 adjust_for_offset(loc, fOffset);
74 SkDEBUGCODE(fDirty = true;)
83 // to manage the lifetime of a plot, we use two tokens. We use last upload token to know when
84 // we can 'piggy back' uploads, ie if the last upload hasn't been flushed to gpu, we don't need
85 // to issue a new upload even if we update the cpu backing store. We use lastref to determine
86 // when we can evict a plot from the cache, ie if the last ref has already flushed through
87 // the gpu then we can reuse the plot
88 BatchToken lastUploadToken() const { return fLastUpload; }
89 BatchToken lastRefToken() const { return fLastRef; }
90 void setLastUploadToken(BatchToken batchToken) { fLastUpload = batchToken; }
91 void setLastRefToken(BatchToken batchToken) { fLastRef = batchToken; }
93 void uploadToTexture(GrBatchTarget::TextureUploader uploader) {
94 // We should only be issuing uploads if we are in fact dirty
96 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture");
98 size_t rowBytes = fBytesPerPixel * fRects->width();
99 const unsigned char* dataPtr = fData;
100 dataPtr += rowBytes * fDirtyRect.fTop;
101 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
102 uploader.writeTexturePixels(fTexture,
103 fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
104 fDirtyRect.width(), fDirtyRect.height(),
105 fTexture->config(), dataPtr, rowBytes);
106 fDirtyRect.setEmpty();
107 SkDEBUGCODE(fDirty = false;)
114 fID = create_id(fIndex, fGenID);
118 memset(fData, 0, fBytesPerPixel * fWidth * fHeight);
120 fDirtyRect.setEmpty();
121 SkDEBUGCODE(fDirty = false;)
124 int x() const { return fX; }
125 int y() const { return fY; }
151 SkDELETE_ARRAY(fData);
156 void init(GrBatchAtlas* atlas, GrTexture* texture, int index, uint32_t generation,
157 int offX, int offY, int width, int height, size_t bpp) {
160 fID = create_id(index, generation);
165 fRects = GrRectanizer::Factory(width, height);
167 fOffset.set(offX * width, offY * height);
168 fBytesPerPixel = bpp;
170 fDirtyRect.setEmpty();
171 SkDEBUGCODE(fDirty = false;)
174 // allocate backing store
175 fData = SkNEW_ARRAY(unsigned char, fBytesPerPixel * width * height);
176 memset(fData, 0, fBytesPerPixel * width * height);
179 BatchToken fLastUpload;
184 GrBatchAtlas::AtlasID fID;
185 unsigned char* fData;
191 GrRectanizer* fRects;
192 GrBatchAtlas* fAtlas;
193 SkIPoint16 fOffset; // the offset of the plot in the backing texture
194 size_t fBytesPerPixel;
196 SkDEBUGCODE(bool fDirty;)
198 friend class GrBatchAtlas;
200 typedef SkRefCnt INHERITED;
203 ////////////////////////////////////////////////////////////////////////////////
205 class GrPlotUploader : public GrBatchTarget::Uploader {
207 GrPlotUploader(BatchPlot* plot)
208 : INHERITED(plot->lastUploadToken())
209 , fPlot(SkRef(plot)) {
213 void upload(GrBatchTarget::TextureUploader uploader) override {
214 fPlot->uploadToTexture(uploader);
218 SkAutoTUnref<BatchPlot> fPlot;
220 typedef GrBatchTarget::Uploader INHERITED;
223 ///////////////////////////////////////////////////////////////////////////////
225 GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY)
227 , fNumPlotsX(numPlotsX)
228 , fNumPlotsY(numPlotsY)
229 , fPlotWidth(texture->width() / numPlotsX)
230 , fPlotHeight(texture->height() / numPlotsY) {
231 SkASSERT(fPlotWidth * fNumPlotsX == texture->width());
232 SkASSERT(fPlotHeight * fNumPlotsY == texture->height());
234 // We currently do not support compressed atlases...
235 SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig));
237 // set up allocated plots
238 fBPP = GrBytesPerPixel(texture->desc().fConfig);
239 fPlotArray = SkNEW_ARRAY(SkAutoTUnref<BatchPlot>, (fNumPlotsX * fNumPlotsY));
241 SkAutoTUnref<BatchPlot>* currPlot = fPlotArray;
242 for (int y = fNumPlotsY - 1, r = 0; y >= 0; --y, ++r) {
243 for (int x = fNumPlotsX - 1, c = 0; x >= 0; --x, ++c) {
244 int id = r * fNumPlotsX + c;
245 currPlot->reset(SkNEW(BatchPlot));
246 (*currPlot)->init(this, texture, id, 0, x, y, fPlotWidth, fPlotHeight, fBPP);
249 fPlotList.addToHead(currPlot->get());
255 GrBatchAtlas::~GrBatchAtlas() {
256 SkSafeUnref(fTexture);
257 SkDELETE_ARRAY(fPlotArray);
260 SkDebugf("Num uploads: %d\n", g_UploadCount);
264 void GrBatchAtlas::processEviction(AtlasID id) {
265 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
266 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
270 void GrBatchAtlas::makeMRU(BatchPlot* plot) {
271 if (fPlotList.head() == plot) {
275 fPlotList.remove(plot);
276 fPlotList.addToHead(plot);
279 inline void GrBatchAtlas::updatePlot(GrBatchTarget* batchTarget, AtlasID* id, BatchPlot* plot) {
282 // If our most recent upload has already occurred then we have to insert a new
283 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
284 // This new update will piggy back on that previously scheduled update.
285 if (batchTarget->isIssued(plot->lastUploadToken())) {
286 plot->setLastUploadToken(batchTarget->asapToken());
287 SkAutoTUnref<GrPlotUploader> uploader(SkNEW_ARGS(GrPlotUploader, (plot)));
288 batchTarget->upload(uploader);
293 bool GrBatchAtlas::addToAtlas(AtlasID* id, GrBatchTarget* batchTarget,
294 int width, int height, const void* image, SkIPoint16* loc) {
295 // We should already have a texture, TODO clean this up
296 SkASSERT(fTexture && width < fPlotWidth && height < fPlotHeight);
298 // now look through all allocated plots for one we can share, in Most Recently Refed order
299 GrBatchPlotList::Iter plotIter;
300 plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart);
302 while ((plot = plotIter.get())) {
303 if (plot->addSubImage(width, height, image, loc, fBPP * width)) {
304 this->updatePlot(batchTarget, id, plot);
310 // If the above fails, then see if the least recently refed plot has already been flushed to the
312 plotIter.init(fPlotList, GrBatchPlotList::Iter::kTail_IterStart);
313 plot = plotIter.get();
315 if (batchTarget->isIssued(plot->lastRefToken())) {
316 this->processEviction(plot->id());
318 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc, fBPP * width);
320 this->updatePlot(batchTarget, id, plot);
324 // The least recently refed plot hasn't been flushed to the gpu yet, however, if we have flushed
325 // it to the batch target than we can reuse it. Our last ref token is guaranteed to be less
326 // than or equal to the current token. If its 'less than' the current token, than we can spin
327 // off the plot(ie let the batch target manage it) and create a new plot in its place in our
328 // array. If it is equal to the currentToken, then the caller has to flush draws to the batch
329 // target so we can spin off the plot
330 if (plot->lastRefToken() == batchTarget->currentToken()) {
334 // We take an extra ref here so our plot isn't deleted when we reset its index in the array.
336 int index = plot->index();
339 int generation = plot->genID();
341 this->processEviction(plot->id());
342 fPlotList.remove(plot);
343 SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()];
344 newPlot.reset(SkNEW(BatchPlot));
345 newPlot->init(this, fTexture, index, ++generation, x, y, fPlotWidth, fPlotHeight, fBPP);
347 fPlotList.addToHead(newPlot.get());
348 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc, fBPP * width);
350 newPlot->setLastUploadToken(batchTarget->currentToken());
351 SkAutoTUnref<GrPlotUploader> uploader(SkNEW_ARGS(GrPlotUploader, (newPlot)));
352 batchTarget->upload(uploader);
358 bool GrBatchAtlas::hasID(AtlasID id) {
359 int index = this->getIndexFromID(id);
360 SkASSERT(index < fNumPlotsX * fNumPlotsY);
361 return fPlotArray[index]->genID() == this->getGenerationFromID(id);
364 void GrBatchAtlas::setLastRefToken(AtlasID id, BatchToken batchToken) {
365 SkASSERT(this->hasID(id));
366 int index = this->getIndexFromID(id);
367 this->makeMRU(fPlotArray[index]);
368 fPlotArray[index]->setLastRefToken(batchToken);