blob: feb66914394b19521413e7d2575548b4c2d4fedd [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
Brian Salomon2ee084e2016-12-16 18:59:19 -050015GrDrawOpAtlas::Plot::Plot(int index, uint64_t genID, int offX, int offY, int width, int height,
16 GrPixelConfig config)
17 : fLastUpload(GrDrawOpUploadToken::AlreadyFlushedToken())
18 , fLastUse(GrDrawOpUploadToken::AlreadyFlushedToken())
19 , 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))
joshualitt5df175e2015-11-18 13:37:54 -080031#ifdef SK_DEBUG
Brian Salomon2ee084e2016-12-16 18:59:19 -050032 , fDirty(false)
joshualitt5df175e2015-11-18 13:37:54 -080033#endif
34{
35 fDirtyRect.setEmpty();
36}
joshualitt5bf99f12015-03-13 11:47:42 -070037
Brian Salomon2ee084e2016-12-16 18:59:19 -050038GrDrawOpAtlas::Plot::~Plot() {
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
Brian Salomon2ee084e2016-12-16 18:59:19 -050043bool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
joshualitt5df175e2015-11-18 13:37:54 -080044 SkASSERT(width <= fWidth && height <= fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070045
joshualitt5df175e2015-11-18 13:37:54 -080046 if (!fRects) {
47 fRects = GrRectanizer::Factory(fWidth, fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -070048 }
49
joshualitt5df175e2015-11-18 13:37:54 -080050 if (!fRects->addRect(width, height, loc)) {
51 return false;
joshualittb4c507e2015-04-08 08:07:59 -070052 }
joshualitt5bf99f12015-03-13 11:47:42 -070053
jvanverthc3d706f2016-04-20 10:33:27 -070054 if (!fData) {
55 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
56 fHeight));
joshualitt5df175e2015-11-18 13:37:54 -080057 }
58 size_t rowBytes = width * fBytesPerPixel;
59 const unsigned char* imagePtr = (const unsigned char*)image;
60 // point ourselves at the right starting spot
jvanverthc3d706f2016-04-20 10:33:27 -070061 unsigned char* dataPtr = fData;
joshualitt5df175e2015-11-18 13:37:54 -080062 dataPtr += fBytesPerPixel * fWidth * loc->fY;
63 dataPtr += fBytesPerPixel * loc->fX;
Brian Osmancce3e582016-10-14 11:42:20 -040064 // copy into the data buffer, swizzling as we go if this is ARGB data
65 if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
66 for (int i = 0; i < height; ++i) {
67 SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width);
68 dataPtr += fBytesPerPixel * fWidth;
69 imagePtr += rowBytes;
70 }
71 } else {
72 for (int i = 0; i < height; ++i) {
73 memcpy(dataPtr, imagePtr, rowBytes);
74 dataPtr += fBytesPerPixel * fWidth;
75 imagePtr += rowBytes;
76 }
joshualitt5bf99f12015-03-13 11:47:42 -070077 }
78
joshualitt5df175e2015-11-18 13:37:54 -080079 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
robertphillips2b0536f2015-11-06 14:10:42 -080080
joshualitt5df175e2015-11-18 13:37:54 -080081 loc->fX += fOffset.fX;
82 loc->fY += fOffset.fY;
83 SkDEBUGCODE(fDirty = true;)
joshualitt5bf99f12015-03-13 11:47:42 -070084
joshualitt5df175e2015-11-18 13:37:54 -080085 return true;
86}
joshualitt5bf99f12015-03-13 11:47:42 -070087
Brian Salomon2ee084e2016-12-16 18:59:19 -050088void GrDrawOpAtlas::Plot::uploadToTexture(GrDrawOp::WritePixelsFn& writePixels,
89 GrTexture* texture) {
joshualitt5df175e2015-11-18 13:37:54 -080090 // We should only be issuing uploads if we are in fact dirty
jvanverthc3d706f2016-04-20 10:33:27 -070091 SkASSERT(fDirty && fData && texture);
Brian Salomon2ee084e2016-12-16 18:59:19 -050092 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrDrawOpAtlas::Plot::uploadToTexture");
joshualitt5df175e2015-11-18 13:37:54 -080093 size_t rowBytes = fBytesPerPixel * fWidth;
jvanverthc3d706f2016-04-20 10:33:27 -070094 const unsigned char* dataPtr = fData;
95 dataPtr += rowBytes * fDirtyRect.fTop;
96 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
97 writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
98 fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes);
joshualitt5df175e2015-11-18 13:37:54 -080099 fDirtyRect.setEmpty();
100 SkDEBUGCODE(fDirty = false;)
101}
102
Brian Salomon2ee084e2016-12-16 18:59:19 -0500103void GrDrawOpAtlas::Plot::resetRects() {
joshualitt5df175e2015-11-18 13:37:54 -0800104 if (fRects) {
105 fRects->reset();
joshualitt5bf99f12015-03-13 11:47:42 -0700106 }
107
joshualitt5df175e2015-11-18 13:37:54 -0800108 fGenID++;
109 fID = CreateId(fIndex, fGenID);
110
111 // zero out the plot
jvanverthc3d706f2016-04-20 10:33:27 -0700112 if (fData) {
113 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
joshualitt5bf99f12015-03-13 11:47:42 -0700114 }
115
joshualitt5df175e2015-11-18 13:37:54 -0800116 fDirtyRect.setEmpty();
117 SkDEBUGCODE(fDirty = false;)
118}
joshualitt5bf99f12015-03-13 11:47:42 -0700119
joshualitt5bf99f12015-03-13 11:47:42 -0700120///////////////////////////////////////////////////////////////////////////////
121
Brian Salomon2ee084e2016-12-16 18:59:19 -0500122GrDrawOpAtlas::GrDrawOpAtlas(sk_sp<GrTexture> texture, int numPlotsX, int numPlotsY)
123 : fTexture(std::move(texture)), fAtlasGeneration(kInvalidAtlasGeneration + 1) {
Ben Wagner594f9ed2016-11-08 14:13:39 -0500124 fPlotWidth = fTexture->width() / numPlotsX;
125 fPlotHeight = fTexture->height() / numPlotsY;
robertphillips2b0536f2015-11-06 14:10:42 -0800126 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
Ben Wagner594f9ed2016-11-08 14:13:39 -0500127 SkASSERT(fPlotWidth * numPlotsX == fTexture->width());
128 SkASSERT(fPlotHeight * numPlotsY == fTexture->height());
robertphillips2b0536f2015-11-06 14:10:42 -0800129
130 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
joshualitt5bf99f12015-03-13 11:47:42 -0700131
132 // We currently do not support compressed atlases...
Ben Wagner594f9ed2016-11-08 14:13:39 -0500133 SkASSERT(!GrPixelConfigIsCompressed(fTexture->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700134
135 // set up allocated plots
Brian Salomon2ee084e2016-12-16 18:59:19 -0500136 fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
joshualitt5bf99f12015-03-13 11:47:42 -0700137
Brian Salomon2ee084e2016-12-16 18:59:19 -0500138 sk_sp<Plot>* currPlot = fPlotArray.get();
robertphillips2b0536f2015-11-06 14:10:42 -0800139 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
140 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
141 uint32_t index = r * numPlotsX + c;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500142 currPlot->reset(
143 new Plot(index, 1, x, y, fPlotWidth, fPlotHeight, fTexture->desc().fConfig));
joshualitt5bf99f12015-03-13 11:47:42 -0700144
145 // build LRU list
146 fPlotList.addToHead(currPlot->get());
147 ++currPlot;
148 }
149 }
150}
151
Brian Salomon2ee084e2016-12-16 18:59:19 -0500152void GrDrawOpAtlas::processEviction(AtlasID id) {
joshualitt5bf99f12015-03-13 11:47:42 -0700153 for (int i = 0; i < fEvictionCallbacks.count(); i++) {
154 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
155 }
156}
157
Brian Salomon2ee084e2016-12-16 18:59:19 -0500158inline void GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* 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.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500166 sk_sp<Plot> plotsp(SkRef(plot));
Ben Wagner594f9ed2016-11-08 14:13:39 -0500167 GrTexture* texture = fTexture.get();
Brian Salomon9afd3712016-12-01 10:59:09 -0500168 GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
169 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
jvanverthc3d706f2016-04-20 10:33:27 -0700170 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
Brian Salomon2ee084e2016-12-16 18:59:19 -0500178bool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height,
179 const void* image, SkIPoint16* loc) {
joshualitt5bf99f12015-03-13 11:47:42 -0700180 // 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
Brian Salomon2ee084e2016-12-16 18:59:19 -0500187 PlotList::Iter plotIter;
188 plotIter.init(fPlotList, PlotList::Iter::kHead_IterStart);
189 Plot* plot;
joshualitt5bf99f12015-03-13 11:47:42 -0700190 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
Brian Salomon2ee084e2016-12-16 18:59:19 -0500214 // If this plot has been used in a draw that is currently being prepared by an op, then we have
215 // to fail. This gives the op a chance to enqueue the draw, and call back into this function.
216 // When that draw is enqueued, the draw token advances, and the subsequent call will continue
217 // past this branch and prepare an inline upload that will occur after the enqueued draw which
218 // references the plot's pre-upload content.
bsalomon342bfc22016-04-01 06:06:20 -0700219 if (plot->lastUseToken() == target->nextDrawToken()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700220 return false;
221 }
222
joshualitt5bf99f12015-03-13 11:47:42 -0700223 this->processEviction(plot->id());
224 fPlotList.remove(plot);
Brian Salomon2ee084e2016-12-16 18:59:19 -0500225 sk_sp<Plot>& newPlot = fPlotArray[plot->index()];
robertphillips2b0536f2015-11-06 14:10:42 -0800226 newPlot.reset(plot->clone());
joshualitt5bf99f12015-03-13 11:47:42 -0700227
228 fPlotList.addToHead(newPlot.get());
robertphillips2b0536f2015-11-06 14:10:42 -0800229 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp());
230 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
joshualitt5bf99f12015-03-13 11:47:42 -0700231 SkASSERT(verify);
robertphillips2b0536f2015-11-06 14:10:42 -0800232
robertphillips1f0e3502015-11-10 10:19:50 -0800233 // Note that this plot will be uploaded inline with the draws whereas the
234 // one it displaced most likely was uploaded asap.
Brian Salomon2ee084e2016-12-16 18:59:19 -0500235 // With c+14 we could move sk_sp into lambda to only ref once.
236 sk_sp<Plot> plotsp(SkRef(newPlot.get()));
Ben Wagner594f9ed2016-11-08 14:13:39 -0500237 GrTexture* texture = fTexture.get();
Brian Salomon9afd3712016-12-01 10:59:09 -0500238 GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
239 [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
jvanverthc3d706f2016-04-20 10:33:27 -0700240 plotsp->uploadToTexture(writePixels, texture);
bsalomon342bfc22016-04-01 06:06:20 -0700241 }
242 );
243 newPlot->setLastUploadToken(lastUploadToken);
244
joshualitt5bf99f12015-03-13 11:47:42 -0700245 *id = newPlot->id();
robertphillips2b0536f2015-11-06 14:10:42 -0800246
joshualitt7c3a2f82015-03-31 13:32:05 -0700247 fAtlasGeneration++;
joshualitt5bf99f12015-03-13 11:47:42 -0700248 return true;
249}