blob: db1b13471f4cf329938dff0a69c0329ec5a7852b [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
joshualitt5df175e2015-11-18 13:37:54 -080014////////////////////////////////////////////////////////////////////////////////
joshualitt5bf99f12015-03-13 11:47:42 -070015
joshualitt5df175e2015-11-18 13:37:54 -080016GrBatchAtlas::BatchPlot::BatchPlot(int index, uint64_t genID, int offX, int offY, int width,
17 int height, GrPixelConfig config)
18 : fLastUpload(0)
19 , fLastUse(0)
20 , fIndex(index)
21 , fGenID(genID)
22 , fID(CreateId(fIndex, fGenID))
23 , fData(nullptr)
24 , fWidth(width)
25 , fHeight(height)
26 , fX(offX)
27 , fY(offY)
28 , fRects(nullptr)
29 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
30 , fConfig(config)
31 , fBytesPerPixel(GrBytesPerPixel(config))
32#ifdef SK_DEBUG
33 , fDirty(false)
34#endif
35{
36 fDirtyRect.setEmpty();
37}
joshualitt5bf99f12015-03-13 11:47:42 -070038
joshualitt5df175e2015-11-18 13:37:54 -080039GrBatchAtlas::BatchPlot::~BatchPlot() {
40 sk_free(fData);
41 delete fRects;
42}
joshualitt5bf99f12015-03-13 11:47:42 -070043
joshualitt5df175e2015-11-18 13:37:54 -080044bool GrBatchAtlas::BatchPlot::addSubImage(int width, int height, const void* image,
45 SkIPoint16* loc) {
46 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070047
joshualitt5df175e2015-11-18 13:37:54 -080048 if (!fRects) {
49 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070050 }
51
joshualitt5df175e2015-11-18 13:37:54 -080052 if (!fRects->addRect(width, height, loc)) {
53 return false;
joshualittb4c507e2015-04-08 08:07:59 -070054 }
joshualitt5bf99f12015-03-13 11:47:42 -070055
joshualitt5df175e2015-11-18 13:37:54 -080056 if (!fData) {
57 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
58 fHeight));
59 }
60 size_t rowBytes = width * fBytesPerPixel;
61 const unsigned char* imagePtr = (const unsigned char*)image;
62 // point ourselves at the right starting spot
63 unsigned char* dataPtr = fData;
64 dataPtr += fBytesPerPixel * fWidth * loc->fY;
65 dataPtr += fBytesPerPixel * loc->fX;
66 // copy into the data buffer
67 for (int i = 0; i < height; ++i) {
68 memcpy(dataPtr, imagePtr, rowBytes);
69 dataPtr += fBytesPerPixel * fWidth;
70 imagePtr += rowBytes;
joshualitt5bf99f12015-03-13 11:47:42 -070071 }
72
joshualitt5df175e2015-11-18 13:37:54 -080073 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -080074
joshualitt5df175e2015-11-18 13:37:54 -080075 loc->fX += fOffset.fX;
76 loc->fY += fOffset.fY;
77 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -070078
joshualitt5df175e2015-11-18 13:37:54 -080079 return true;
80}
joshualitt5bf99f12015-03-13 11:47:42 -070081
joshualitt5df175e2015-11-18 13:37:54 -080082void GrBatchAtlas::BatchPlot::uploadToTexture(GrBatchUploader::TextureUploader* uploader,
83 GrTexture* texture) {
84 // We should only be issuing uploads if we are in fact dirty
85 SkASSERT(fDirty && fData && texture);
86 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture");
87 size_t rowBytes = fBytesPerPixel * fWidth;
88 const unsigned char* dataPtr = fData;
89 dataPtr += rowBytes * fDirtyRect.fTop;
90 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
91 uploader->writeTexturePixels(texture,
92 fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
93 fDirtyRect.width(), fDirtyRect.height(),
94 fConfig, dataPtr, rowBytes);
95 fDirtyRect.setEmpty();
96 SkDEBUGCODE(fDirty = false;)
97}
98
99void GrBatchAtlas::BatchPlot::resetRects() {
100 if (fRects) {
101 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700102 }
103
joshualitt5df175e2015-11-18 13:37:54 -0800104 fGenID++;
105 fID = CreateId(fIndex, fGenID);
106
107 // zero out the plot
108 if (fData) {
109 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700110 }
111
joshualitt5df175e2015-11-18 13:37:54 -0800112 fDirtyRect.setEmpty();
113 SkDEBUGCODE(fDirty = false;)
114}
joshualitt5bf99f12015-03-13 11:47:42 -0700115
116////////////////////////////////////////////////////////////////////////////////
117
bsalomon75398562015-08-17 12:55:38 -0700118class GrPlotUploader : public GrBatchUploader {
joshualitt5bf99f12015-03-13 11:47:42 -0700119public:
joshualitt5df175e2015-11-18 13:37:54 -0800120 GrPlotUploader(GrBatchAtlas::BatchPlot* plot, GrTexture* texture)
joshualitt5bf99f12015-03-13 11:47:42 -0700121 : INHERITED(plot->lastUploadToken())
robertphillips2b0536f2015-11-06 14:10:42 -0800122 , fPlot(SkRef(plot))
123 , fTexture(texture) {
joshualitt5bf99f12015-03-13 11:47:42 -0700124 SkASSERT(plot);
125 }
126
bsalomon75398562015-08-17 12:55:38 -0700127 void upload(TextureUploader* uploader) override {
robertphillips2b0536f2015-11-06 14:10:42 -0800128 fPlot->uploadToTexture(uploader, fTexture);
joshualitt5bf99f12015-03-13 11:47:42 -0700129 }
130
131private:
joshualitt5df175e2015-11-18 13:37:54 -0800132 SkAutoTUnref<GrBatchAtlas::BatchPlot> fPlot;
133 GrTexture* fTexture;
joshualitt5bf99f12015-03-13 11:47:42 -0700134
bsalomon75398562015-08-17 12:55:38 -0700135 typedef GrBatchUploader INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700136};
137
138///////////////////////////////////////////////////////////////////////////////
139
140GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY)
141 : fTexture(texture)
joshualitt7c3a2f82015-03-31 13:32:05 -0700142 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
robertphillips2b0536f2015-11-06 14:10:42 -0800143
144 int plotWidth = texture->width() / numPlotsX;
145 int plotHeight = texture->height() / numPlotsY;
146 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
147 SkASSERT(plotWidth * numPlotsX == texture->width());
148 SkASSERT(plotHeight * numPlotsY == texture->height());
149
150 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700151
152 // We currently do not support compressed atlases...
153 SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig));
154
155 // set up allocated plots
robertphillips2b0536f2015-11-06 14:10:42 -0800156 fPlotArray = new SkAutoTUnref<BatchPlot>[numPlotsX * numPlotsY];
joshualitt5bf99f12015-03-13 11:47:42 -0700157
158 SkAutoTUnref<BatchPlot>* currPlot = fPlotArray;
robertphillips2b0536f2015-11-06 14:10:42 -0800159 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
160 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
161 uint32_t index = r * numPlotsX + c;
162 currPlot->reset(new BatchPlot(index, 1, x, y, plotWidth, plotHeight,
163 texture->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700164
165 // build LRU list
166 fPlotList.addToHead(currPlot->get());
167 ++currPlot;
168 }
169 }
170}
171
172GrBatchAtlas::~GrBatchAtlas() {
173 SkSafeUnref(fTexture);
halcanary385fe4d2015-08-26 13:07:48 -0700174 delete[] fPlotArray;
joshualitt5bf99f12015-03-13 11:47:42 -0700175}
176
177void GrBatchAtlas::processEviction(AtlasID id) {
178 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
179 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
180 }
181}
182
bsalomon75398562015-08-17 12:55:38 -0700183inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, BatchPlot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700184 this->makeMRU(plot);
185
186 // If our most recent upload has already occurred then we have to insert a new
187 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
188 // This new update will piggy back on that previously scheduled update.
bsalomon75398562015-08-17 12:55:38 -0700189 if (target->hasTokenBeenFlushed(plot->lastUploadToken())) {
190 plot->setLastUploadToken(target->asapToken());
robertphillips2b0536f2015-11-06 14:10:42 -0800191 SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(plot, fTexture));
bsalomon75398562015-08-17 12:55:38 -0700192 target->upload(uploader);
joshualitt5bf99f12015-03-13 11:47:42 -0700193 }
194 *id = plot->id();
195}
196
bsalomon75398562015-08-17 12:55:38 -0700197bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* batchTarget,
joshualitt5bf99f12015-03-13 11:47:42 -0700198 int width, int height, const void* image, SkIPoint16* loc) {
199 // We should already have a texture, TODO clean this up
robertphillips2b0536f2015-11-06 14:10:42 -0800200 SkASSERT(fTexture);
joshualitt5bf99f12015-03-13 11:47:42 -0700201
202 // now look through all allocated plots for one we can share, in Most Recently Refed order
203 GrBatchPlotList::Iter plotIter;
204 plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart);
205 BatchPlot* plot;
206 while ((plot = plotIter.get())) {
robertphillips2b0536f2015-11-06 14:10:42 -0800207 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
208 if (plot->addSubImage(width, height, image, loc)) {
joshualitt5bf99f12015-03-13 11:47:42 -0700209 this->updatePlot(batchTarget, id, plot);
210 return true;
211 }
212 plotIter.next();
213 }
214
215 // If the above fails, then see if the least recently refed plot has already been flushed to the
216 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800217 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700218 SkASSERT(plot);
bsalomon75398562015-08-17 12:55:38 -0700219 if (batchTarget->hasTokenBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700220 this->processEviction(plot->id());
221 plot->resetRects();
robertphillips2b0536f2015-11-06 14:10:42 -0800222 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
223 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700224 SkASSERT(verify);
225 this->updatePlot(batchTarget, id, plot);
joshualitt7c3a2f82015-03-31 13:32:05 -0700226 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700227 return true;
228 }
229
robertphillips2b0536f2015-11-06 14:10:42 -0800230 // The least recently used plot hasn't been flushed to the gpu yet, however, if we have flushed
231 // 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 -0700232 // than or equal to the current token. If its 'less than' the current token, than we can spin
robertphillips2b0536f2015-11-06 14:10:42 -0800233 // 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 -0700234 // array. If it is equal to the currentToken, then the caller has to flush draws to the batch
235 // target so we can spin off the plot
joshualittb4c507e2015-04-08 08:07:59 -0700236 if (plot->lastUseToken() == batchTarget->currentToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700237 return false;
238 }
239
robertphillips2b0536f2015-11-06 14:10:42 -0800240 SkASSERT(plot->lastUseToken() < batchTarget->currentToken());
241 SkASSERT(!batchTarget->hasTokenBeenFlushed(batchTarget->currentToken()));
242
243 SkASSERT(!plot->unique()); // The GrPlotUpdater should have a ref too
joshualitt5bf99f12015-03-13 11:47:42 -0700244
245 this->processEviction(plot->id());
246 fPlotList.remove(plot);
247 SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800248 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700249
250 fPlotList.addToHead(newPlot.get());
robertphillips2b0536f2015-11-06 14:10:42 -0800251 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp());
252 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700253 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800254
robertphillips1f0e3502015-11-10 10:19:50 -0800255 // Note that this plot will be uploaded inline with the draws whereas the
256 // one it displaced most likely was uploaded asap.
joshualitt5bf99f12015-03-13 11:47:42 -0700257 newPlot->setLastUploadToken(batchTarget->currentToken());
robertphillips2b0536f2015-11-06 14:10:42 -0800258 SkAutoTUnref<GrPlotUploader> uploader(new GrPlotUploader(newPlot, fTexture));
joshualitt5bf99f12015-03-13 11:47:42 -0700259 batchTarget->upload(uploader);
260 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800261
joshualitt7c3a2f82015-03-31 13:32:05 -0700262 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700263 return true;
264}