blob: e478ca1c7d44ba21314d264482a939d6c8b2395f [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,
jvanverthc3d706f2016-04-20 10:33:27 -070016 int height, GrPixelConfig config)
17 : fLastUpload(GrBatchDrawToken::AlreadyFlushedToken())
bsalomon342bfc22016-04-01 06:06:20 -070018 , fLastUse(GrBatchDrawToken::AlreadyFlushedToken())
joshualitt5df175e2015-11-18 13:37:54 -080019 , fIndex(index)
20 , fGenID(genID)
21 , fID(CreateId(fIndex, fGenID))
jvanverthc3d706f2016-04-20 10:33:27 -070022 , fData(nullptr)
joshualitt5df175e2015-11-18 13:37:54 -080023 , 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() {
jvanverthc3d706f2016-04-20 10:33:27 -070039 sk_free(fData);
joshualitt5df175e2015-11-18 13:37:54 -080040 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
jvanverthc3d706f2016-04-20 10:33:27 -070055 if (!fData) {
56 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
57 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -080058 }
59 size_t rowBytes = width * fBytesPerPixel;
60 const unsigned char* imagePtr = (const unsigned char*)image;
61 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -070062 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -080063 dataPtr += fBytesPerPixel * fWidth * loc->fY;
64 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -040065 // copy into the data buffer, swizzling as we go if this is ARGB data
66 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
67 for (int i = 0; i < height; ++i) {
68 SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width);
69 dataPtr += fBytesPerPixel * fWidth;
70 imagePtr += rowBytes;
71 }
72 } else {
73 for (int i = 0; i < height; ++i) {
74 memcpy(dataPtr, imagePtr, rowBytes);
75 dataPtr += fBytesPerPixel * fWidth;
76 imagePtr += rowBytes;
77 }
joshualitt5bf99f12015-03-13 11:47:42 -070078 }
79
joshualitt5df175e2015-11-18 13:37:54 -080080 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -080081
joshualitt5df175e2015-11-18 13:37:54 -080082 loc->fX += fOffset.fX;
83 loc->fY += fOffset.fY;
84 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -070085
joshualitt5df175e2015-11-18 13:37:54 -080086 return true;
87}
joshualitt5bf99f12015-03-13 11:47:42 -070088
bsalomon342bfc22016-04-01 06:06:20 -070089void GrBatchAtlas::BatchPlot::uploadToTexture(GrDrawBatch::WritePixelsFn& writePixels,
joshualitt5df175e2015-11-18 13:37:54 -080090 GrTexture* texture) {
91 // We should only be issuing uploads if we are in fact dirty
jvanverthc3d706f2016-04-20 10:33:27 -070092 SkASSERT(fDirty && fData && texture);
joshualitt5df175e2015-11-18 13:37:54 -080093 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture");
94 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -070095 const unsigned char* dataPtr = fData;
96 dataPtr += rowBytes * fDirtyRect.fTop;
97 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
98 writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
99 fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -0800100 fDirtyRect.setEmpty();
101 SkDEBUGCODE(fDirty = false;)
102}
103
104void GrBatchAtlas::BatchPlot::resetRects() {
105 if (fRects) {
106 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700107 }
108
joshualitt5df175e2015-11-18 13:37:54 -0800109 fGenID++;
110 fID = CreateId(fIndex, fGenID);
111
112 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700113 if (fData) {
114 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700115 }
116
joshualitt5df175e2015-11-18 13:37:54 -0800117 fDirtyRect.setEmpty();
118 SkDEBUGCODE(fDirty = false;)
119}
joshualitt5bf99f12015-03-13 11:47:42 -0700120
joshualitt5bf99f12015-03-13 11:47:42 -0700121///////////////////////////////////////////////////////////////////////////////
122
jvanverthc3d706f2016-04-20 10:33:27 -0700123GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY)
joshualitt5bf99f12015-03-13 11:47:42 -0700124 : fTexture(texture)
joshualitt7c3a2f82015-03-31 13:32:05 -0700125 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
robertphillips2b0536f2015-11-06 14:10:42 -0800126
bsalomon6d6b6ad2016-07-13 14:45:28 -0700127 fPlotWidth = texture->width() / numPlotsX;
128 fPlotHeight = texture->height() / numPlotsY;
robertphillips2b0536f2015-11-06 14:10:42 -0800129 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700130 SkASSERT(fPlotWidth * numPlotsX == texture->width());
131 SkASSERT(fPlotHeight * numPlotsY == texture->height());
robertphillips2b0536f2015-11-06 14:10:42 -0800132
133 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700134
135 // We currently do not support compressed atlases...
136 SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig));
137
138 // set up allocated plots
robertphillips2b0536f2015-11-06 14:10:42 -0800139 fPlotArray = new SkAutoTUnref<BatchPlot>[numPlotsX * numPlotsY];
joshualitt5bf99f12015-03-13 11:47:42 -0700140
141 SkAutoTUnref<BatchPlot>* currPlot = fPlotArray;
robertphillips2b0536f2015-11-06 14:10:42 -0800142 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
143 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
144 uint32_t index = r * numPlotsX + c;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700145 currPlot->reset(new BatchPlot(index, 1, x, y, fPlotWidth, fPlotHeight,
jvanverthc3d706f2016-04-20 10:33:27 -0700146 texture->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700147
148 // build LRU list
149 fPlotList.addToHead(currPlot->get());
150 ++currPlot;
151 }
152 }
153}
154
155GrBatchAtlas::~GrBatchAtlas() {
156 SkSafeUnref(fTexture);
halcanary385fe4d2015-08-26 13:07:48 -0700157 delete[] fPlotArray;
joshualitt5bf99f12015-03-13 11:47:42 -0700158}
159
160void GrBatchAtlas::processEviction(AtlasID id) {
161 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
162 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
163 }
164}
165
bsalomon75398562015-08-17 12:55:38 -0700166inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, BatchPlot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700167 this->makeMRU(plot);
168
169 // If our most recent upload has already occurred then we have to insert a new
170 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
171 // This new update will piggy back on that previously scheduled update.
bsalomon342bfc22016-04-01 06:06:20 -0700172 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
jvanverthc3d706f2016-04-20 10:33:27 -0700173 // With c+14 we could move sk_sp into lamba to only ref once.
bsalomon342bfc22016-04-01 06:06:20 -0700174 sk_sp<BatchPlot> plotsp(SkRef(plot));
175 GrTexture* texture = fTexture;
176 GrBatchDrawToken lastUploadToken = target->addAsapUpload(
jvanverthc3d706f2016-04-20 10:33:27 -0700177 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) {
178 plotsp->uploadToTexture(writePixels, texture);
bsalomon342bfc22016-04-01 06:06:20 -0700179 }
180 );
181 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700182 }
183 *id = plot->id();
184}
185
bsalomon342bfc22016-04-01 06:06:20 -0700186bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* target,
joshualitt5bf99f12015-03-13 11:47:42 -0700187 int width, int height, const void* image, SkIPoint16* loc) {
188 // We should already have a texture, TODO clean this up
robertphillips2b0536f2015-11-06 14:10:42 -0800189 SkASSERT(fTexture);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700190 if (width > fPlotWidth || height > fPlotHeight) {
191 return false;
192 }
joshualitt5bf99f12015-03-13 11:47:42 -0700193
194 // now look through all allocated plots for one we can share, in Most Recently Refed order
195 GrBatchPlotList::Iter plotIter;
196 plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart);
197 BatchPlot* plot;
198 while ((plot = plotIter.get())) {
robertphillips2b0536f2015-11-06 14:10:42 -0800199 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
200 if (plot->addSubImage(width, height, image, loc)) {
bsalomon342bfc22016-04-01 06:06:20 -0700201 this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700202 return true;
203 }
204 plotIter.next();
205 }
206
207 // If the above fails, then see if the least recently refed plot has already been flushed to the
208 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800209 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700210 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700211 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700212 this->processEviction(plot->id());
213 plot->resetRects();
robertphillips2b0536f2015-11-06 14:10:42 -0800214 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
215 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700216 SkASSERT(verify);
bsalomon342bfc22016-04-01 06:06:20 -0700217 this->updatePlot(target, id, plot);
joshualitt7c3a2f82015-03-31 13:32:05 -0700218 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700219 return true;
220 }
221
bsalomon342bfc22016-04-01 06:06:20 -0700222 // If this plot has been used in a draw that is currently being prepared by a batch, then we
223 // have to fail. This gives the batch a chance to enqueue the draw, and call back into this
224 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
225 // continue past this branch and prepare an inline upload that will occur after the enqueued
226 // draw which references the plot's pre-upload content.
227 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700228 return false;
229 }
230
joshualitt5bf99f12015-03-13 11:47:42 -0700231 this->processEviction(plot->id());
232 fPlotList.remove(plot);
233 SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800234 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700235
236 fPlotList.addToHead(newPlot.get());
robertphillips2b0536f2015-11-06 14:10:42 -0800237 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp());
238 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700239 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800240
robertphillips1f0e3502015-11-10 10:19:50 -0800241 // Note that this plot will be uploaded inline with the draws whereas the
242 // one it displaced most likely was uploaded asap.
bsalomon342bfc22016-04-01 06:06:20 -0700243 // With c+14 we could move sk_sp into lamba to only ref once.
244 sk_sp<BatchPlot> plotsp(SkRef(newPlot.get()));
245 GrTexture* texture = fTexture;
246 GrBatchDrawToken lastUploadToken = target->addInlineUpload(
jvanverthc3d706f2016-04-20 10:33:27 -0700247 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) {
248 plotsp->uploadToTexture(writePixels, texture);
bsalomon342bfc22016-04-01 06:06:20 -0700249 }
250 );
251 newPlot->setLastUploadToken(lastUploadToken);
252
joshualitt5bf99f12015-03-13 11:47:42 -0700253 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800254
joshualitt7c3a2f82015-03-31 13:32:05 -0700255 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700256 return true;
257}