1 /*
2 * Copyright 2018 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 "tools/DDLTileHelper.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkDeferredDisplayListRecorder.h"
12 #include "include/core/SkPicture.h"
13 #include "include/core/SkSurface.h"
14 #include "include/core/SkSurfaceCharacterization.h"
15 #include "include/gpu/GrDirectContext.h"
16 #include "src/base/SkRandom.h"
17 #include "src/core/SkDeferredDisplayListPriv.h"
18 #include "src/core/SkTaskGroup.h"
19 #include "src/gpu/ganesh/GrCaps.h"
20 #include "src/gpu/ganesh/GrDirectContextPriv.h"
21 #include "src/image/SkImage_Gpu.h"
22 #include "tools/DDLPromiseImageHelper.h"
23
init(int id,GrDirectContext * direct,const SkSurfaceCharacterization & dstSurfaceCharacterization,const SkIRect & clip,const SkIRect & paddingOutsets)24 void DDLTileHelper::TileData::init(int id,
25 GrDirectContext* direct,
26 const SkSurfaceCharacterization& dstSurfaceCharacterization,
27 const SkIRect& clip,
28 const SkIRect& paddingOutsets) {
29 fID = id;
30 fClip = clip;
31 fPaddingOutsets = paddingOutsets;
32
33 fPlaybackChar = dstSurfaceCharacterization.createResized(this->paddedRectSize().width(),
34 this->paddedRectSize().height());
35 SkASSERT(fPlaybackChar.isValid());
36
37 SkDEBUGCODE(const GrCaps* caps = direct->priv().caps());
38 SkASSERT(caps->isFormatTexturable(fPlaybackChar.backendFormat(),
39 fPlaybackChar.backendFormat().textureType()));
40
41 fCallbackContext.reset(new PromiseImageCallbackContext(direct, fPlaybackChar.backendFormat()));
42 }
43
TileData()44 DDLTileHelper::TileData::TileData() {}
~TileData()45 DDLTileHelper::TileData::~TileData() {}
46
createDDL(const SkPicture * picture)47 void DDLTileHelper::TileData::createDDL(const SkPicture* picture) {
48 SkASSERT(!fDisplayList && picture);
49
50 auto recordingChar = fPlaybackChar.createResized(fClip.width(), fClip.height());
51 SkASSERT(recordingChar.isValid());
52
53 SkDeferredDisplayListRecorder recorder(recordingChar);
54
55 // DDL TODO: the DDLRecorder's rContext isn't initialized until getCanvas is called.
56 // Maybe set it up in the ctor?
57 SkCanvas* recordingCanvas = recorder.getCanvas();
58
59 // We always record the DDL in the (0,0) .. (clipWidth, clipHeight) coordinates
60 recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
61 recordingCanvas->translate(-fClip.fLeft, -fClip.fTop);
62
63 // Note: in this use case we only render a picture to the deferred canvas
64 // but, more generally, clients will use arbitrary draw calls.
65 recordingCanvas->drawPicture(picture);
66
67 fDisplayList = recorder.detach();
68 }
69
createComposeDDL()70 void DDLTileHelper::createComposeDDL() {
71 SkASSERT(!fComposeDDL);
72
73 SkDeferredDisplayListRecorder recorder(fDstCharacterization);
74
75 SkCanvas* recordingCanvas = recorder.getCanvas();
76
77 for (int i = 0; i < this->numTiles(); ++i) {
78 TileData* tile = &fTiles[i];
79 if (!tile->initialized()) {
80 continue;
81 }
82
83 sk_sp<SkImage> promiseImage = tile->makePromiseImageForDst(
84 recordingCanvas->recordingContext()->threadSafeProxy());
85
86 SkRect dstRect = SkRect::Make(tile->clipRect());
87 SkIRect srcRect = tile->clipRect();
88 srcRect.offsetTo(tile->padOffset().x(), tile->padOffset().y());
89
90 SkASSERT(promiseImage->bounds().contains(srcRect));
91
92 recordingCanvas->drawImageRect(promiseImage.get(), SkRect::Make(srcRect), dstRect,
93 SkSamplingOptions(), nullptr,
94 SkCanvas::kStrict_SrcRectConstraint);
95 }
96
97 fComposeDDL = recorder.detach();
98 SkASSERT(fComposeDDL);
99 }
100
precompile(GrDirectContext * direct)101 void DDLTileHelper::TileData::precompile(GrDirectContext* direct) {
102 if (!this->initialized()) {
103 return;
104 }
105
106 SkASSERT(fDisplayList);
107
108 SkDeferredDisplayList::ProgramIterator iter(direct, fDisplayList.get());
109 for (; !iter.done(); iter.next()) {
110 iter.compile();
111 }
112 }
113
makeWrappedTileDest(GrRecordingContext * rContext)114 sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext* rContext) {
115 SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture());
116
117 auto promiseImageTexture = fCallbackContext->promiseImageTexture();
118 if (!promiseImageTexture->backendTexture().isValid()) {
119 return nullptr;
120 }
121
122 // Here we are, unfortunately, aliasing the backend texture held by the SkPromiseImageTexture.
123 // Both the tile's destination surface and the promise image used to draw the tile will be
124 // backed by the same backendTexture - unbeknownst to Ganesh.
125 return SkSurface::MakeFromBackendTexture(rContext,
126 promiseImageTexture->backendTexture(),
127 fPlaybackChar.origin(),
128 fPlaybackChar.sampleCount(),
129 fPlaybackChar.colorType(),
130 fPlaybackChar.refColorSpace(),
131 &fPlaybackChar.surfaceProps());
132 }
133
drawSKPDirectly(GrDirectContext * dContext,const SkPicture * picture)134 void DDLTileHelper::TileData::drawSKPDirectly(GrDirectContext* dContext,
135 const SkPicture* picture) {
136 SkASSERT(!fDisplayList && !fTileSurface && picture);
137
138 fTileSurface = this->makeWrappedTileDest(dContext);
139 if (fTileSurface) {
140 SkCanvas* tileCanvas = fTileSurface->getCanvas();
141
142 SkASSERT(this->padOffset().isZero() && this->paddedRectSize() == fClip.size());
143 tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
144 tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
145
146 tileCanvas->drawPicture(picture);
147
148 // We can't snap an image here bc, since we're using wrapped backend textures for the
149 // surfaces, that would incur a copy.
150 }
151 }
152
draw(GrDirectContext * direct)153 void DDLTileHelper::TileData::draw(GrDirectContext* direct) {
154 SkASSERT(fDisplayList && !fTileSurface);
155
156 fTileSurface = this->makeWrappedTileDest(direct);
157 if (fTileSurface) {
158 fTileSurface->draw(fDisplayList, this->padOffset().x(), this->padOffset().y());
159
160 // We can't snap an image here bc, since we're using wrapped backend textures for the
161 // surfaces, that would incur a copy.
162 }
163 }
164
reset()165 void DDLTileHelper::TileData::reset() {
166 // TODO: when DDLs are re-renderable we don't need to do this
167 fDisplayList = nullptr;
168
169 fTileSurface = nullptr;
170 }
171
makePromiseImageForDst(sk_sp<GrContextThreadSafeProxy> threadSafeProxy)172 sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImageForDst(
173 sk_sp<GrContextThreadSafeProxy> threadSafeProxy) {
174 SkASSERT(fCallbackContext);
175
176 // The promise image gets a ref on the promise callback context
177 sk_sp<SkImage> promiseImage =
178 SkImage::MakePromiseTexture(std::move(threadSafeProxy),
179 fCallbackContext->backendFormat(),
180 this->paddedRectSize(),
181 GrMipmapped::kNo,
182 GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin,
183 fPlaybackChar.colorType(),
184 kPremul_SkAlphaType,
185 fPlaybackChar.refColorSpace(),
186 PromiseImageCallbackContext::PromiseImageFulfillProc,
187 PromiseImageCallbackContext::PromiseImageReleaseProc,
188 (void*)this->refCallbackContext().release());
189 fCallbackContext->wasAddedToImage();
190
191 return promiseImage;
192 }
193
CreateBackendTexture(GrDirectContext * direct,TileData * tile)194 void DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext* direct, TileData* tile) {
195 SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture());
196
197 const SkSurfaceCharacterization& c = tile->fPlaybackChar;
198 GrBackendTexture beTex =
199 direct->createBackendTexture(c.width(),
200 c.height(),
201 c.colorType(),
202 GrMipmapped(c.isMipMapped()),
203 GrRenderable::kYes,
204 GrProtected::kNo,
205 /*label=*/"DDLTile_TileData_CreateBackendTexture");
206 tile->fCallbackContext->setBackendTexture(beTex);
207 }
208
DeleteBackendTexture(GrDirectContext * dContext,TileData * tile)209 void DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext* dContext, TileData* tile) {
210 if (!tile->initialized() || dContext->abandoned()) {
211 return;
212 }
213
214 SkASSERT(tile->fCallbackContext);
215
216 // TODO: it seems that, on the Linux bots, backend texture creation is failing
217 // a lot (skbug.com/10142)
218 SkASSERT(!tile->fCallbackContext->promiseImageTexture() ||
219 tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid());
220
221 tile->fTileSurface = nullptr;
222
223 SkASSERT(tile->fCallbackContext->unique());
224 tile->fCallbackContext.reset();
225 }
226
227 ///////////////////////////////////////////////////////////////////////////////////////////////////
228
DDLTileHelper(GrDirectContext * direct,const SkSurfaceCharacterization & dstChar,const SkIRect & viewport,int numXDivisions,int numYDivisions,bool addRandomPaddingToDst)229 DDLTileHelper::DDLTileHelper(GrDirectContext* direct,
230 const SkSurfaceCharacterization& dstChar,
231 const SkIRect& viewport,
232 int numXDivisions, int numYDivisions,
233 bool addRandomPaddingToDst)
234 : fNumXDivisions(numXDivisions)
235 , fNumYDivisions(numYDivisions)
236 , fTiles(numXDivisions * numYDivisions)
237 , fDstCharacterization(dstChar) {
238 SkASSERT(fNumXDivisions > 0 && fNumYDivisions > 0);
239
240 int xTileSize = viewport.width()/fNumXDivisions;
241 int yTileSize = viewport.height()/fNumYDivisions;
242
243 SkRandom rand;
244
245 // Create the destination tiles
246 for (int y = 0, yOff = 0; y < fNumYDivisions; ++y, yOff += yTileSize) {
247 int ySize = (y < fNumYDivisions-1) ? yTileSize : viewport.height()-yOff;
248
249 for (int x = 0, xOff = 0; x < fNumXDivisions; ++x, xOff += xTileSize) {
250 int xSize = (x < fNumXDivisions-1) ? xTileSize : viewport.width()-xOff;
251
252 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
253
254 SkASSERT(viewport.contains(clip));
255
256 static const uint32_t kMaxPad = 64;
257 int32_t lPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
258 int32_t tPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
259 int32_t rPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
260 int32_t bPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
261
262 fTiles[y*fNumXDivisions+x].init(y*fNumXDivisions+x, direct, dstChar, clip,
263 {lPad, tPad, rPad, bPad});
264 }
265 }
266 }
267
createDDLsInParallel(SkPicture * picture)268 void DDLTileHelper::createDDLsInParallel(SkPicture* picture) {
269 #if 1
270 SkTaskGroup().batch(this->numTiles(), [&](int i) {
271 fTiles[i].createDDL(picture);
272 });
273 SkTaskGroup().add([this]{ this->createComposeDDL(); });
274 SkTaskGroup().wait();
275 #else
276 // Use this code path to debug w/o threads
277 for (int i = 0; i < this->numTiles(); ++i) {
278 fTiles[i].createDDL(picture);
279 }
280 this->createComposeDDL();
281 #endif
282 }
283
284 // On the gpu thread:
285 // precompile any programs
286 // replay the DDL into a surface to make the tile image
287 // compose the tile image into the main canvas
do_gpu_stuff(GrDirectContext * direct,DDLTileHelper::TileData * tile)288 static void do_gpu_stuff(GrDirectContext* direct, DDLTileHelper::TileData* tile) {
289
290 // TODO: schedule program compilation as their own tasks
291 tile->precompile(direct);
292
293 tile->draw(direct);
294
295 tile->dropDDL();
296 }
297
298 // We expect to have more than one recording thread but just one gpu thread
kickOffThreadedWork(SkTaskGroup * recordingTaskGroup,SkTaskGroup * gpuTaskGroup,GrDirectContext * dContext,SkPicture * picture)299 void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
300 SkTaskGroup* gpuTaskGroup,
301 GrDirectContext* dContext,
302 SkPicture* picture) {
303 SkASSERT(recordingTaskGroup && gpuTaskGroup && dContext);
304
305 for (int i = 0; i < this->numTiles(); ++i) {
306 TileData* tile = &fTiles[i];
307 if (!tile->initialized()) {
308 continue;
309 }
310
311 // On a recording thread:
312 // generate the tile's DDL
313 // schedule gpu-thread processing of the DDL
314 // Note: a finer grained approach would be add a scheduling task which would evaluate
315 // which DDLs were ready to be rendered based on their prerequisites
316 recordingTaskGroup->add([tile, gpuTaskGroup, dContext, picture]() {
317 tile->createDDL(picture);
318
319 gpuTaskGroup->add([dContext, tile]() {
320 do_gpu_stuff(dContext, tile);
321 });
322 });
323 }
324
325 recordingTaskGroup->add([this] { this->createComposeDDL(); });
326 }
327
328 // Only called from skpbench
interleaveDDLCreationAndDraw(GrDirectContext * dContext,SkPicture * picture)329 void DDLTileHelper::interleaveDDLCreationAndDraw(GrDirectContext* dContext, SkPicture* picture) {
330 for (int i = 0; i < this->numTiles(); ++i) {
331 fTiles[i].createDDL(picture);
332 fTiles[i].draw(dContext);
333 }
334 }
335
336 // Only called from skpbench
drawAllTilesDirectly(GrDirectContext * dContext,SkPicture * picture)337 void DDLTileHelper::drawAllTilesDirectly(GrDirectContext* dContext, SkPicture* picture) {
338 for (int i = 0; i < this->numTiles(); ++i) {
339 fTiles[i].drawSKPDirectly(dContext, picture);
340 }
341 }
342
dropCallbackContexts()343 void DDLTileHelper::dropCallbackContexts() {
344 for (int i = 0; i < this->numTiles(); ++i) {
345 fTiles[i].dropCallbackContext();
346 }
347 }
348
resetAllTiles()349 void DDLTileHelper::resetAllTiles() {
350 for (int i = 0; i < this->numTiles(); ++i) {
351 fTiles[i].reset();
352 }
353 fComposeDDL.reset();
354 }
355
createBackendTextures(SkTaskGroup * taskGroup,GrDirectContext * direct)356 void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
357
358 if (taskGroup) {
359 for (int i = 0; i < this->numTiles(); ++i) {
360 TileData* tile = &fTiles[i];
361 if (!tile->initialized()) {
362 continue;
363 }
364
365 taskGroup->add([direct, tile]() { TileData::CreateBackendTexture(direct, tile); });
366 }
367 } else {
368 for (int i = 0; i < this->numTiles(); ++i) {
369 TileData::CreateBackendTexture(direct, &fTiles[i]);
370 }
371 }
372 }
373
deleteBackendTextures(SkTaskGroup * taskGroup,GrDirectContext * direct)374 void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
375 if (taskGroup) {
376 for (int i = 0; i < this->numTiles(); ++i) {
377 TileData* tile = &fTiles[i];
378
379 taskGroup->add([direct, tile]() { TileData::DeleteBackendTexture(direct, tile); });
380 }
381 } else {
382 for (int i = 0; i < this->numTiles(); ++i) {
383 TileData::DeleteBackendTexture(direct, &fTiles[i]);
384 }
385 }
386 }
387