blob: 9f3c4dd983ca8cbdc67460046f135fb47dece37b [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"
12
joshualitt5df175e2015-11-18 13:37:54 -080013////////////////////////////////////////////////////////////////////////////////
joshualitt5bf99f12015-03-13 11:47:42 -070014
joshualitt5df175e2015-11-18 13:37:54 -080015GrBatchAtlas::BatchPlot::BatchPlot(int index, uint64_t genID, int offX, int offY, int width,
16 int height, GrPixelConfig config)
17 : fLastUpload(0)
18 , fLastUse(0)
19 , fIndex(index)
20 , fGenID(genID)
21 , fID(CreateId(fIndex, fGenID))
22 , fData(nullptr)
23 , fWidth(width)
24 , fHeight(height)
25 , fX(offX)
26 , fY(offY)
27 , fRects(nullptr)
28 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
29 , fConfig(config)
30 , fBytesPerPixel(GrBytesPerPixel(config))
31#ifdef SK_DEBUG
32 , fDirty(false)
33#endif
34{
35 fDirtyRect.setEmpty();
36}
joshualitt5bf99f12015-03-13 11:47:42 -070037
joshualitt5df175e2015-11-18 13:37:54 -080038GrBatchAtlas::BatchPlot::~BatchPlot() {
39 sk_free(fData);
40 delete fRects;
41}
joshualitt5bf99f12015-03-13 11:47:42 -070042
joshualitt5df175e2015-11-18 13:37:54 -080043bool GrBatchAtlas::BatchPlot::addSubImage(int width, int height, const void* image,
44 SkIPoint16* loc) {
45 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070046
joshualitt5df175e2015-11-18 13:37:54 -080047 if (!fRects) {
48 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070049 }
50
joshualitt5df175e2015-11-18 13:37:54 -080051 if (!fRects->addRect(width, height, loc)) {
52 return false;
joshualittb4c507e2015-04-08 08:07:59 -070053 }
joshualitt5bf99f12015-03-13 11:47:42 -070054
joshualitt5df175e2015-11-18 13:37:54 -080055 if (!fData) {
56 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
57 fHeight));
58 }
59 size_t rowBytes = width * fBytesPerPixel;
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;
69 imagePtr += rowBytes;
joshualitt5bf99f12015-03-13 11:47:42 -070070 }
71
joshualitt5df175e2015-11-18 13:37:54 -080072 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -080073
joshualitt5df175e2015-11-18 13:37:54 -080074 loc->fX += fOffset.fX;
75 loc->fY += fOffset.fY;
76 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -070077
joshualitt5df175e2015-11-18 13:37:54 -080078 return true;
79}
joshualitt5bf99f12015-03-13 11:47:42 -070080
joshualitt5df175e2015-11-18 13:37:54 -080081void GrBatchAtlas::BatchPlot::uploadToTexture(GrBatchUploader::TextureUploader* uploader,
82 GrTexture* texture) {
83 // We should only be issuing uploads if we are in fact dirty
84 SkASSERT(fDirty && fData && texture);
85 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture");
86 size_t rowBytes = fBytesPerPixel * fWidth;
87 const unsigned char* dataPtr = fData;
88 dataPtr += rowBytes * fDirtyRect.fTop;
89 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
90 uploader->writeTexturePixels(texture,
91 fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
92 fDirtyRect.width(), fDirtyRect.height(),
93 fConfig, dataPtr, rowBytes);
94 fDirtyRect.setEmpty();
95 SkDEBUGCODE(fDirty = false;)
96}
97
98void GrBatchAtlas::BatchPlot::resetRects() {
99 if (fRects) {
100 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700101 }
102
joshualitt5df175e2015-11-18 13:37:54 -0800103 fGenID++;
104 fID = CreateId(fIndex, fGenID);
105
106 // zero out the plot
107 if (fData) {
108 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700109 }
110
joshualitt5df175e2015-11-18 13:37:54 -0800111 fDirtyRect.setEmpty();
112 SkDEBUGCODE(fDirty = false;)
113}
joshualitt5bf99f12015-03-13 11:47:42 -0700114
115////////////////////////////////////////////////////////////////////////////////
116
bsalomon75398562015-08-17 12:55:38 -0700117class GrPlotUploader : public GrBatchUploader {
joshualitt5bf99f12015-03-13 11:47:42 -0700118public:
joshualitt5df175e2015-11-18 13:37:54 -0800119 GrPlotUploader(GrBatchAtlas::BatchPlot* plot, GrTexture* texture)
joshualitt5bf99f12015-03-13 11:47:42 -0700120 : INHERITED(plot->lastUploadToken())
robertphillips2b0536f2015-11-06 14:10:42 -0800121 , fPlot(SkRef(plot))
122 , fTexture(texture) {
joshualitt5bf99f12015-03-13 11:47:42 -0700123 SkASSERT(plot);
124 }
125
bsalomon75398562015-08-17 12:55:38 -0700126 void upload(TextureUploader* uploader) override {
robertphillips2b0536f2015-11-06 14:10:42 -0800127 fPlot->uploadToTexture(uploader, fTexture);
joshualitt5bf99f12015-03-13 11:47:42 -0700128 }
129
130private:
joshualitt5df175e2015-11-18 13:37:54 -0800131 SkAutoTUnref<GrBatchAtlas::BatchPlot> fPlot;
132 GrTexture* fTexture;
joshualitt5bf99f12015-03-13 11:47:42 -0700133
bsalomon75398562015-08-17 12:55:38 -0700134 typedef GrBatchUploader INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700135};
136
137///////////////////////////////////////////////////////////////////////////////
138
139GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY)
140 : fTexture(texture)
joshualitt7c3a2f82015-03-31 13:32:05 -0700141 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
robertphillips2b0536f2015-11-06 14:10:42 -0800142
143 int plotWidth = texture->width() / numPlotsX;
144 int plotHeight = texture->height() / numPlotsY;
145 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
146 SkASSERT(plotWidth * numPlotsX == texture->width());
147 SkASSERT(plotHeight * numPlotsY == texture->height());
148
149 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700150
151 // We currently do not support compressed atlases...
152 SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig));
153
154 // set up allocated plots
robertphillips2b0536f2015-11-06 14:10:42 -0800155 fPlotArray = new SkAutoTUnref<BatchPlot>[numPlotsX * numPlotsY];
joshualitt5bf99f12015-03-13 11:47:42 -0700156
157 SkAutoTUnref<BatchPlot>* currPlot = fPlotArray;
robertphillips2b0536f2015-11-06 14:10:42 -0800158 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
159 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
160 uint32_t index = r * numPlotsX + c;
161 currPlot->reset(new BatchPlot(index, 1, x, y, plotWidth, plotHeight,
162 texture->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700163
164 // build LRU list
165 fPlotList.addToHead(currPlot->get());
166 ++currPlot;
167 }
168 }
169}
170
171GrBatchAtlas::~GrBatchAtlas() {
172 SkSafeUnref(fTexture);
halcanary385fe4d2015-08-26 13:07:48 -0700173 delete[] fPlotArray;
joshualitt5bf99f12015-03-13 11:47:42 -0700174}
175
176void GrBatchAtlas::processEviction(AtlasID id) {
177 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
178 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
179 }
180}
181
bsalomon75398562015-08-17 12:55:38 -0700182inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, BatchPlot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700183 this->makeMRU(plot);
184
185 // If our most recent upload has already occurred then we have to insert a new
186 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
187 // This new update will piggy back on that previously scheduled update.
bsalomon75398562015-08-17 12:55:38 -0700188 if (target->hasTokenBeenFlushed(plot->lastUploadToken())) {
189 plot->setLastUploadToken(target->asapToken());
robertphillips2b0536f2015-11-06 14:10:42 -0800190 SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(plot, fTexture));
bsalomon75398562015-08-17 12:55:38 -0700191 target->upload(uploader);
joshualitt5bf99f12015-03-13 11:47:42 -0700192 }
193 *id = plot->id();
194}
195
bsalomon75398562015-08-17 12:55:38 -0700196bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* batchTarget,
joshualitt5bf99f12015-03-13 11:47:42 -0700197 int width, int height, const void* image, SkIPoint16* loc) {
198 // We should already have a texture, TODO clean this up
robertphillips2b0536f2015-11-06 14:10:42 -0800199 SkASSERT(fTexture);
joshualitt5bf99f12015-03-13 11:47:42 -0700200
201 // now look through all allocated plots for one we can share, in Most Recently Refed order
202 GrBatchPlotList::Iter plotIter;
203 plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart);
204 BatchPlot* plot;
205 while ((plot = plotIter.get())) {
robertphillips2b0536f2015-11-06 14:10:42 -0800206 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
207 if (plot->addSubImage(width, height, image, loc)) {
joshualitt5bf99f12015-03-13 11:47:42 -0700208 this->updatePlot(batchTarget, id, plot);
209 return true;
210 }
211 plotIter.next();
212 }
213
214 // If the above fails, then see if the least recently refed plot has already been flushed to the
215 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800216 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700217 SkASSERT(plot);
bsalomon75398562015-08-17 12:55:38 -0700218 if (batchTarget->hasTokenBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700219 this->processEviction(plot->id());
220 plot->resetRects();
robertphillips2b0536f2015-11-06 14:10:42 -0800221 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
222 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700223 SkASSERT(verify);
224 this->updatePlot(batchTarget, id, plot);
joshualitt7c3a2f82015-03-31 13:32:05 -0700225 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700226 return true;
227 }
228
robertphillips2b0536f2015-11-06 14:10:42 -0800229 // The least recently used plot hasn't been flushed to the gpu yet, however, if we have flushed
230 // 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 -0700231 // than or equal to the current token. If its 'less than' the current token, than we can spin
robertphillips2b0536f2015-11-06 14:10:42 -0800232 // 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 -0700233 // array. If it is equal to the currentToken, then the caller has to flush draws to the batch
234 // target so we can spin off the plot
joshualittb4c507e2015-04-08 08:07:59 -0700235 if (plot->lastUseToken() == batchTarget->currentToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700236 return false;
237 }
238
robertphillips2b0536f2015-11-06 14:10:42 -0800239 SkASSERT(plot->lastUseToken() < batchTarget->currentToken());
240 SkASSERT(!batchTarget->hasTokenBeenFlushed(batchTarget->currentToken()));
241
242 SkASSERT(!plot->unique()); // The GrPlotUpdater should have a ref too
joshualitt5bf99f12015-03-13 11:47:42 -0700243
244 this->processEviction(plot->id());
245 fPlotList.remove(plot);
246 SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800247 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700248
249 fPlotList.addToHead(newPlot.get());
robertphillips2b0536f2015-11-06 14:10:42 -0800250 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp());
251 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700252 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800253
robertphillips1f0e3502015-11-10 10:19:50 -0800254 // Note that this plot will be uploaded inline with the draws whereas the
255 // one it displaced most likely was uploaded asap.
joshualitt5bf99f12015-03-13 11:47:42 -0700256 newPlot->setLastUploadToken(batchTarget->currentToken());
robertphillips2b0536f2015-11-06 14:10:42 -0800257 SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(newPlot, fTexture));
joshualitt5bf99f12015-03-13 11:47:42 -0700258 batchTarget->upload(uploader);
259 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800260
joshualitt7c3a2f82015-03-31 13:32:05 -0700261 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700262 return true;
263}