blob: 8cc6f6181149f3223c78e06f79255c7289fccd57 [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;
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
jvanverthc3d706f2016-04-20 10:33:27 -070084 SkASSERT(fDirty && fData && texture);
joshualitt5df175e2015-11-18 13:37:54 -080085 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTexture");
86 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -070087 const unsigned char* dataPtr = fData;
88 dataPtr += rowBytes * fDirtyRect.fTop;
89 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
90 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
jvanverthc3d706f2016-04-20 10:33:27 -0700105 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
jvanverthc3d706f2016-04-20 10:33:27 -0700115GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY)
joshualitt5bf99f12015-03-13 11:47:42 -0700116 : fTexture(texture)
joshualitt7c3a2f82015-03-31 13:32:05 -0700117 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
robertphillips2b0536f2015-11-06 14:10:42 -0800118
bsalomon6d6b6ad2016-07-13 14:45:28 -0700119 fPlotWidth = texture->width() / numPlotsX;
120 fPlotHeight = texture->height() / numPlotsY;
robertphillips2b0536f2015-11-06 14:10:42 -0800121 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700122 SkASSERT(fPlotWidth * numPlotsX == texture->width());
123 SkASSERT(fPlotHeight * numPlotsY == texture->height());
robertphillips2b0536f2015-11-06 14:10:42 -0800124
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;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700137 currPlot->reset(new BatchPlot(index, 1, x, y, fPlotWidth, fPlotHeight,
jvanverthc3d706f2016-04-20 10:33:27 -0700138 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())) {
jvanverthc3d706f2016-04-20 10:33:27 -0700165 // With c+14 we could move sk_sp into lamba to only ref once.
bsalomon342bfc22016-04-01 06:06:20 -0700166 sk_sp<BatchPlot> plotsp(SkRef(plot));
167 GrTexture* texture = fTexture;
168 GrBatchDrawToken lastUploadToken = target->addAsapUpload(
jvanverthc3d706f2016-04-20 10:33:27 -0700169 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) {
170 plotsp->uploadToTexture(writePixels, texture);
bsalomon342bfc22016-04-01 06:06:20 -0700171 }
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);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700182 if (width > fPlotWidth || height > fPlotHeight) {
183 return false;
184 }
joshualitt5bf99f12015-03-13 11:47:42 -0700185
186 // now look through all allocated plots for one we can share, in Most Recently Refed order
187 GrBatchPlotList::Iter plotIter;
188 plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart);
189 BatchPlot* plot;
190 while ((plot = plotIter.get())) {
robertphillips2b0536f2015-11-06 14:10:42 -0800191 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
192 if (plot->addSubImage(width, height, image, loc)) {
bsalomon342bfc22016-04-01 06:06:20 -0700193 this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700194 return true;
195 }
196 plotIter.next();
197 }
198
199 // If the above fails, then see if the least recently refed plot has already been flushed to the
200 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800201 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700202 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700203 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700204 this->processEviction(plot->id());
205 plot->resetRects();
robertphillips2b0536f2015-11-06 14:10:42 -0800206 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
207 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700208 SkASSERT(verify);
bsalomon342bfc22016-04-01 06:06:20 -0700209 this->updatePlot(target, id, plot);
joshualitt7c3a2f82015-03-31 13:32:05 -0700210 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700211 return true;
212 }
213
bsalomon342bfc22016-04-01 06:06:20 -0700214 // If this plot has been used in a draw that is currently being prepared by a batch, then we
215 // have to fail. This gives the batch a chance to enqueue the draw, and call back into this
216 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
217 // continue past this branch and prepare an inline upload that will occur after the enqueued
218 // draw which references the plot's pre-upload content.
219 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700220 return false;
221 }
222
robertphillips2b0536f2015-11-06 14:10:42 -0800223 SkASSERT(!plot->unique()); // The GrPlotUpdater should have a ref too
joshualitt5bf99f12015-03-13 11:47:42 -0700224
225 this->processEviction(plot->id());
226 fPlotList.remove(plot);
227 SkAutoTUnref<BatchPlot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800228 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700229
230 fPlotList.addToHead(newPlot.get());
robertphillips2b0536f2015-11-06 14:10:42 -0800231 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp());
232 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700233 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800234
robertphillips1f0e3502015-11-10 10:19:50 -0800235 // Note that this plot will be uploaded inline with the draws whereas the
236 // one it displaced most likely was uploaded asap.
bsalomon342bfc22016-04-01 06:06:20 -0700237 // With c+14 we could move sk_sp into lamba to only ref once.
238 sk_sp<BatchPlot> plotsp(SkRef(newPlot.get()));
239 GrTexture* texture = fTexture;
240 GrBatchDrawToken lastUploadToken = target->addInlineUpload(
jvanverthc3d706f2016-04-20 10:33:27 -0700241 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) {
242 plotsp->uploadToTexture(writePixels, texture);
bsalomon342bfc22016-04-01 06:06:20 -0700243 }
244 );
245 newPlot->setLastUploadToken(lastUploadToken);
246
joshualitt5bf99f12015-03-13 11:47:42 -0700247 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800248
joshualitt7c3a2f82015-03-31 13:32:05 -0700249 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700250 return true;
251}