blob: 1a4ba4552e93369fd1b85834fd7e58e4d4046600 [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
Brian Salomon903da792016-12-16 14:24:46 -05008#include "GrDrawOpAtlas.h"
Brian Salomon742e31d2016-12-07 17:06:19 -05009#include "GrOpFlushState.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)
Brian Salomon9afd3712016-12-01 10:59:09 -050017 : fLastUpload(GrDrawOpUploadToken::AlreadyFlushedToken())
18 , fLastUse(GrDrawOpUploadToken::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
Brian Salomon9afd3712016-12-01 10:59:09 -050089void GrBatchAtlas::BatchPlot::uploadToTexture(GrDrawOp::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
Ben Wagner594f9ed2016-11-08 14:13:39 -0500123GrBatchAtlas::GrBatchAtlas(sk_sp<GrTexture> texture, int numPlotsX, int numPlotsY)
124 : fTexture(std::move(texture))
joshualitt7c3a2f82015-03-31 13:32:05 -0700125 , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
robertphillips2b0536f2015-11-06 14:10:42 -0800126
Ben Wagner594f9ed2016-11-08 14:13:39 -0500127 fPlotWidth = fTexture->width() / numPlotsX;
128 fPlotHeight = fTexture->height() / numPlotsY;
robertphillips2b0536f2015-11-06 14:10:42 -0800129 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
Ben Wagner594f9ed2016-11-08 14:13:39 -0500130 SkASSERT(fPlotWidth * numPlotsX == fTexture->width());
131 SkASSERT(fPlotHeight * numPlotsY == fTexture->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...
Ben Wagner594f9ed2016-11-08 14:13:39 -0500136 SkASSERT(!GrPixelConfigIsCompressed(fTexture->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700137
138 // set up allocated plots
Ben Wagner594f9ed2016-11-08 14:13:39 -0500139 fPlotArray.reset(new sk_sp<BatchPlot>[numPlotsX * numPlotsY]);
joshualitt5bf99f12015-03-13 11:47:42 -0700140
Ben Wagner594f9ed2016-11-08 14:13:39 -0500141 sk_sp<BatchPlot>* currPlot = fPlotArray.get();
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,
Ben Wagner594f9ed2016-11-08 14:13:39 -0500146 fTexture->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700147
148 // build LRU list
149 fPlotList.addToHead(currPlot->get());
150 ++currPlot;
151 }
152 }
153}
154
joshualitt5bf99f12015-03-13 11:47:42 -0700155void GrBatchAtlas::processEviction(AtlasID id) {
156 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
157 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
158 }
159}
160
Brian Salomon9afd3712016-12-01 10:59:09 -0500161inline void GrBatchAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, BatchPlot* plot) {
joshualitt5bf99f12015-03-13 11:47:42 -0700162 this->makeMRU(plot);
163
164 // If our most recent upload has already occurred then we have to insert a new
165 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
166 // This new update will piggy back on that previously scheduled update.
bsalomon342bfc22016-04-01 06:06:20 -0700167 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
jvanverthc3d706f2016-04-20 10:33:27 -0700168 // With c+14 we could move sk_sp into lamba to only ref once.
bsalomon342bfc22016-04-01 06:06:20 -0700169 sk_sp<BatchPlot> plotsp(SkRef(plot));
Ben Wagner594f9ed2016-11-08 14:13:39 -0500170 GrTexture* texture = fTexture.get();
Brian Salomon9afd3712016-12-01 10:59:09 -0500171 GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
172 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
jvanverthc3d706f2016-04-20 10:33:27 -0700173 plotsp->uploadToTexture(writePixels, texture);
bsalomon342bfc22016-04-01 06:06:20 -0700174 }
175 );
176 plot->setLastUploadToken(lastUploadToken);
joshualitt5bf99f12015-03-13 11:47:42 -0700177 }
178 *id = plot->id();
179}
180
Brian Salomon9afd3712016-12-01 10:59:09 -0500181bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target,
joshualitt5bf99f12015-03-13 11:47:42 -0700182 int width, int height, const void* image, SkIPoint16* loc) {
183 // We should already have a texture, TODO clean this up
robertphillips2b0536f2015-11-06 14:10:42 -0800184 SkASSERT(fTexture);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700185 if (width > fPlotWidth || height > fPlotHeight) {
186 return false;
187 }
joshualitt5bf99f12015-03-13 11:47:42 -0700188
189 // now look through all allocated plots for one we can share, in Most Recently Refed order
190 GrBatchPlotList::Iter plotIter;
191 plotIter.init(fPlotList, GrBatchPlotList::Iter::kHead_IterStart);
192 BatchPlot* plot;
193 while ((plot = plotIter.get())) {
robertphillips2b0536f2015-11-06 14:10:42 -0800194 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
195 if (plot->addSubImage(width, height, image, loc)) {
bsalomon342bfc22016-04-01 06:06:20 -0700196 this->updatePlot(target, id, plot);
joshualitt5bf99f12015-03-13 11:47:42 -0700197 return true;
198 }
199 plotIter.next();
200 }
201
202 // If the above fails, then see if the least recently refed plot has already been flushed to the
203 // gpu
robertphillips2b0536f2015-11-06 14:10:42 -0800204 plot = fPlotList.tail();
joshualitt5bf99f12015-03-13 11:47:42 -0700205 SkASSERT(plot);
bsalomon342bfc22016-04-01 06:06:20 -0700206 if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700207 this->processEviction(plot->id());
208 plot->resetRects();
robertphillips2b0536f2015-11-06 14:10:42 -0800209 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == plot->bpp());
210 SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700211 SkASSERT(verify);
bsalomon342bfc22016-04-01 06:06:20 -0700212 this->updatePlot(target, id, plot);
joshualitt7c3a2f82015-03-31 13:32:05 -0700213 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700214 return true;
215 }
216
bsalomon342bfc22016-04-01 06:06:20 -0700217 // If this plot has been used in a draw that is currently being prepared by a batch, then we
218 // have to fail. This gives the batch a chance to enqueue the draw, and call back into this
219 // function. When that draw is enqueued, the draw token advances, and the subsequent call will
220 // continue past this branch and prepare an inline upload that will occur after the enqueued
221 // draw which references the plot's pre-upload content.
222 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700223 return false;
224 }
225
joshualitt5bf99f12015-03-13 11:47:42 -0700226 this->processEviction(plot->id());
227 fPlotList.remove(plot);
Hal Canary144caf52016-11-07 17:57:18 -0500228 sk_sp<BatchPlot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800229 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700230
231 fPlotList.addToHead(newPlot.get());
robertphillips2b0536f2015-11-06 14:10:42 -0800232 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp());
233 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700234 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800235
robertphillips1f0e3502015-11-10 10:19:50 -0800236 // Note that this plot will be uploaded inline with the draws whereas the
237 // one it displaced most likely was uploaded asap.
bsalomon342bfc22016-04-01 06:06:20 -0700238 // With c+14 we could move sk_sp into lamba to only ref once.
239 sk_sp<BatchPlot> plotsp(SkRef(newPlot.get()));
Ben Wagner594f9ed2016-11-08 14:13:39 -0500240 GrTexture* texture = fTexture.get();
Brian Salomon9afd3712016-12-01 10:59:09 -0500241 GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
242 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
jvanverthc3d706f2016-04-20 10:33:27 -0700243 plotsp->uploadToTexture(writePixels, texture);
bsalomon342bfc22016-04-01 06:06:20 -0700244 }
245 );
246 newPlot->setLastUploadToken(lastUploadToken);
247
joshualitt5bf99f12015-03-13 11:47:42 -0700248 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800249
joshualitt7c3a2f82015-03-31 13:32:05 -0700250 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700251 return true;
252}