blob: 40ab0e6c0e09d562ecc483d6130a2a65db509a41 [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)
bsalomon342bfc22016-04-01 06:06:20 -070017 : fLastUpload(GrBatchDrawToken::AlreadyFlushedToken())
18 , fLastUse(GrBatchDrawToken::AlreadyFlushedToken())
joshualitt5df175e2015-11-18 13:37:54 -080019 , 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
bsalomon342bfc22016-04-01 06:06:20 -070081void GrBatchAtlas::BatchPlot::uploadToTexture(GrDrawBatch::WritePixelsFn& writePixels,
joshualitt5df175e2015-11-18 13:37:54 -080082 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;
bsalomon342bfc22016-04-01 06:06:20 -070090 writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
91 fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -080092 fDirtyRect.setEmpty();
93 SkDEBUGCODE(fDirty = false;)
94}
95
96void GrBatchAtlas::BatchPlot::resetRects() {
97 if (fRects) {
98 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -070099 }
100
joshualitt5df175e2015-11-18 13:37:54 -0800101 fGenID++;
102 fID = CreateId(fIndex, fGenID);
103
104 // zero out the plot
105 if (fData) {
106 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700107 }
108
joshualitt5df175e2015-11-18 13:37:54 -0800109 fDirtyRect.setEmpty();
110 SkDEBUGCODE(fDirty = false;)
111}
joshualitt5bf99f12015-03-13 11:47:42 -0700112
joshualitt5bf99f12015-03-13 11:47:42 -0700113///////////////////////////////////////////////////////////////////////////////
114
115GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY)
116 : fTexture(texture)
joshualitt7c3a2f82015-03-31 13:32:05 -0700117 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
robertphillips2b0536f2015-11-06 14:10:42 -0800118
119 int plotWidth = texture->width() / numPlotsX;
120 int plotHeight = texture->height() / numPlotsY;
121 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
122 SkASSERT(plotWidth * numPlotsX == texture->width());
123 SkASSERT(plotHeight * numPlotsY == texture->height());
124
125 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700126
127 // We currently do not support compressed atlases...
128 SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig));
129
130 // set up allocated plots
robertphillips2b0536f2015-11-06 14:10:42 -0800131 fPlotArray = new SkAutoTUnref<BatchPlot>[numPlotsX * numPlotsY];
joshualitt5bf99f12015-03-13 11:47:42 -0700132
133 SkAutoTUnref<BatchPlot>* currPlot = fPlotArray;
robertphillips2b0536f2015-11-06 14:10:42 -0800134 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
135 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
136 uint32_t index = r * numPlotsX + c;
137 currPlot->reset(new BatchPlot(index, 1, x, y, plotWidth, plotHeight,
138 texture->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700139
140 // build LRU list
141 fPlotList.addToHead(currPlot->get());
142 ++currPlot;
143 }
144 }
145}
146
147GrBatchAtlas::~GrBatchAtlas() {
148 SkSafeUnref(fTexture);
halcanary385fe4d2015-08-26 13:07:48 -0700149 delete[] fPlotArray;
joshualitt5bf99f12015-03-13 11:47:42 -0700150}
151
152void GrBatchAtlas::processEviction(AtlasID id) {
153 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
154 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
155 }
156}
157
bsalomon75398562015-08-17 12:55:38 -0700158inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, BatchPlot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700159 this->makeMRU(plot);
160
161 // If our most recent upload has already occurred then we have to insert a new
162 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
163 // This new update will piggy back on that previously scheduled update.
bsalomon342bfc22016-04-01 06:06:20 -0700164 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
165 // With c+14 we could move sk_sp into lamba to only ref once.
166 sk_sp<BatchPlot> plotsp(SkRef(plot));
167 GrTexture* texture = fTexture;
168 GrBatchDrawToken lastUploadToken = target->addAsapUpload(
169 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) {
170 plotsp->uploadToTexture(writePixels, texture);
171 }
172 );
173 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700174 }
175 *id = plot->id();
176}
177
bsalomon342bfc22016-04-01 06:06:20 -0700178bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* target,
joshualitt5bf99f12015-03-13 11:47:42 -0700179 int width, int height, const void* image, SkIPoint16* loc) {
180 // We should already have a texture, TODO clean this up
robertphillips2b0536f2015-11-06 14:10:42 -0800181 SkASSERT(fTexture);
joshualitt5bf99f12015-03-13 11:47:42 -0700182
183 // now look through all allocated plots for one we can share, in Most Recently Refed order
184 GrBatchPlotList::Iter plotIter;
185 plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart);
186 BatchPlot* plot;
187 while ((plot = plotIter.get())) {
robertphillips2b0536f2015-11-06 14:10:42 -0800188 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
189 if (plot->addSubImage(width, height, image, loc)) {
bsalomon342bfc22016-04-01 06:06:20 -0700190 this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700191 return true;
192 }
193 plotIter.next();
194 }
195
196 // If the above fails, then see if the least recently refed plot has already been flushed to the
197 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800198 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700199 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700200 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700201 this->processEviction(plot->id());
202 plot->resetRects();
robertphillips2b0536f2015-11-06 14:10:42 -0800203 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
204 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700205 SkASSERT(verify);
bsalomon342bfc22016-04-01 06:06:20 -0700206 this->updatePlot(target, id, plot);
joshualitt7c3a2f82015-03-31 13:32:05 -0700207 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700208 return true;
209 }
210
bsalomon342bfc22016-04-01 06:06:20 -0700211 // If this plot has been used in a draw that is currently being prepared by a batch, then we
212 // have to fail. This gives the batch a chance to enqueue the draw, and call back into this
213 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
214 // continue past this branch and prepare an inline upload that will occur after the enqueued
215 // draw which references the plot's pre-upload content.
216 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700217 return false;
218 }
219
robertphillips2b0536f2015-11-06 14:10:42 -0800220 SkASSERT(!plot->unique()); // The GrPlotUpdater should have a ref too
joshualitt5bf99f12015-03-13 11:47:42 -0700221
222 this->processEviction(plot->id());
223 fPlotList.remove(plot);
224 SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800225 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700226
227 fPlotList.addToHead(newPlot.get());
robertphillips2b0536f2015-11-06 14:10:42 -0800228 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp());
229 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700230 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800231
robertphillips1f0e3502015-11-10 10:19:50 -0800232 // Note that this plot will be uploaded inline with the draws whereas the
233 // one it displaced most likely was uploaded asap.
bsalomon342bfc22016-04-01 06:06:20 -0700234 // With c+14 we could move sk_sp into lamba to only ref once.
235 sk_sp<BatchPlot> plotsp(SkRef(newPlot.get()));
236 GrTexture* texture = fTexture;
237 GrBatchDrawToken lastUploadToken = target->addInlineUpload(
238 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) {
239 plotsp->uploadToTexture(writePixels, texture);
240 }
241 );
242 newPlot->setLastUploadToken(lastUploadToken);
243
joshualitt5bf99f12015-03-13 11:47:42 -0700244 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800245
joshualitt7c3a2f82015-03-31 13:32:05 -0700246 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700247 return true;
248}