blob: 7771a6ec96120b71af0ec96a0fc842707e82f5b0 [file] [log] [blame]
Robert Phillips96601082018-05-29 16:13:26 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/DDLTileHelper.h"
Robert Phillips96601082018-05-29 16:13:26 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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 "src/core/SkDeferredDisplayListPriv.h"
16#include "src/core/SkTaskGroup.h"
Robert Phillips7b0ed552020-02-20 12:45:19 -050017#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/image/SkImage_Gpu.h"
19#include "tools/DDLPromiseImageHelper.h"
Robert Phillips96601082018-05-29 16:13:26 -040020
Robert Phillips19f466d2020-02-26 10:27:07 -050021void DDLTileHelper::TileData::init(int id,
Robert Phillips11c67672020-04-23 15:10:03 -040022 GrContext* context,
Robert Phillips19f466d2020-02-26 10:27:07 -050023 const SkSurfaceCharacterization& dstSurfaceCharacterization,
24 const SkIRect& clip) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -050025 fID = id;
Robert Phillipsa865a3a2020-02-14 10:49:39 -050026 fClip = clip;
27
Robert Phillips19f466d2020-02-26 10:27:07 -050028 fCharacterization = dstSurfaceCharacterization.createResized(clip.width(), clip.height());
Robert Phillipsa865a3a2020-02-14 10:49:39 -050029 SkASSERT(fCharacterization.isValid());
Robert Phillips11c67672020-04-23 15:10:03 -040030
31 GrBackendFormat backendFormat = context->defaultBackendFormat(fCharacterization.colorType(),
32 GrRenderable::kYes);
33 SkDEBUGCODE(const GrCaps* caps = context->priv().caps());
34 SkASSERT(caps->isFormatTexturable(backendFormat));
35
36 fCallbackContext.reset(new PromiseImageCallbackContext(context, backendFormat));
Robert Phillips96601082018-05-29 16:13:26 -040037}
38
Robert Phillipsa865a3a2020-02-14 10:49:39 -050039DDLTileHelper::TileData::~TileData() {}
40
Robert Phillips96601082018-05-29 16:13:26 -040041void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData,
42 const DDLPromiseImageHelper& helper) {
Robert Phillips7a3197b2018-09-26 21:18:23 +000043 SkASSERT(!fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040044
Robert Phillips7a3197b2018-09-26 21:18:23 +000045 // This is bending the DDLRecorder contract! The promise images in the SKP should be
46 // created by the same recorder used to create the matching DDL.
47 SkDeferredDisplayListRecorder recorder(fCharacterization);
Robert Phillips96601082018-05-29 16:13:26 -040048
Robert Phillips7a3197b2018-09-26 21:18:23 +000049 fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages);
Robert Phillipse8e2bb12018-09-27 14:26:47 -040050
51 std::unique_ptr<SkDeferredDisplayList> ddl = recorder.detach();
Chris Dalton6b498102019-08-01 14:14:52 -060052 if (ddl->priv().numRenderTasks()) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -040053 // TODO: remove this once skbug.com/8424 is fixed. If the DDL resulting from the
Greg Danielf41b2bd2019-08-22 16:19:24 -040054 // reinflation of the SKPs contains opsTasks that means some image subset operation
Robert Phillipse8e2bb12018-09-27 14:26:47 -040055 // created a draw.
56 fReconstitutedPicture.reset();
57 }
Robert Phillips96601082018-05-29 16:13:26 -040058}
59
60void DDLTileHelper::TileData::createDDL() {
Robert Phillips24a8e9e2020-03-06 20:26:28 +000061 SkASSERT(!fDisplayList && fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040062
Robert Phillips7a3197b2018-09-26 21:18:23 +000063 SkDeferredDisplayListRecorder recorder(fCharacterization);
64
65 // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called.
66 // Maybe set it up in the ctor?
Robert Phillips24a8e9e2020-03-06 20:26:28 +000067 SkCanvas* recordingCanvas = recorder.getCanvas();
Robert Phillips7a3197b2018-09-26 21:18:23 +000068
69 // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's
70 // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct
71 // one now.
72 for (int i = 0; i < fPromiseImages.count(); ++i) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +000073 GrContext* newContext = recordingCanvas->getGrContext();
Robert Phillips7a3197b2018-09-26 21:18:23 +000074
75 if (fPromiseImages[i]->isTextureBacked()) {
Jim Van Verth21bd60d2018-10-12 15:00:20 -040076 SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get();
Robert Phillips7a3197b2018-09-26 21:18:23 +000077 gpuImage->resetContext(sk_ref_sp(newContext));
78 }
79 }
Robert Phillips96601082018-05-29 16:13:26 -040080
Robert Phillips24a8e9e2020-03-06 20:26:28 +000081 recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
82 recordingCanvas->translate(-fClip.fLeft, -fClip.fTop);
Robert Phillips96601082018-05-29 16:13:26 -040083
84 // Note: in this use case we only render a picture to the deferred canvas
85 // but, more generally, clients will use arbitrary draw calls.
Robert Phillips24a8e9e2020-03-06 20:26:28 +000086 recordingCanvas->drawPicture(fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040087
Robert Phillips7a3197b2018-09-26 21:18:23 +000088 fDisplayList = recorder.detach();
Robert Phillips96601082018-05-29 16:13:26 -040089}
90
Robert Phillips11c67672020-04-23 15:10:03 -040091void DDLTileHelper::createComposeDDL() {
92 SkASSERT(!fComposeDDL);
93
94 SkDeferredDisplayListRecorder recorder(fDstCharacterization);
95
96 SkCanvas* recordingCanvas = recorder.getCanvas();
97
98 for (int i = 0; i < this->numTiles(); ++i) {
99 TileData* tile = &fTiles[i];
100
101 sk_sp<SkImage> promiseImage = tile->makePromiseImage(&recorder);
102
103 SkIRect clipRect = tile->clipRect();
104
105 SkASSERT(clipRect.width() == promiseImage->width() &&
106 clipRect.height() == promiseImage->height());
107
108 recordingCanvas->drawImage(promiseImage, clipRect.fLeft, clipRect.fTop);
109 }
110
111 fComposeDDL = recorder.detach();
112}
113
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500114void DDLTileHelper::TileData::precompile(GrContext* context) {
115 SkASSERT(fDisplayList);
116
117 SkDeferredDisplayList::ProgramIterator iter(context, fDisplayList.get());
118 for (; !iter.done(); iter.next()) {
119 iter.compile();
120 }
121}
122
Robert Phillips8472a3d2020-04-16 16:27:45 -0400123sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrContext* context) {
Robert Phillips11c67672020-04-23 15:10:03 -0400124 SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture());
125
126 auto promiseImageTexture = fCallbackContext->promiseImageTexture();
127 if (!promiseImageTexture->backendTexture().isValid()) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400128 return nullptr;
129 }
Robert Phillips7ae9d2f2020-04-15 10:58:15 -0400130
Robert Phillips11c67672020-04-23 15:10:03 -0400131 // Here we are, unfortunately, aliasing the backend texture held by the SkPromiseImageTexture.
132 // Both the tile's destination surface and the promise image used to draw the tile will be
133 // backed by the same backendTexture - unbeknownst to Ganesh.
Robert Phillips8472a3d2020-04-16 16:27:45 -0400134 return SkSurface::MakeFromBackendTexture(context,
Robert Phillips11c67672020-04-23 15:10:03 -0400135 promiseImageTexture->backendTexture(),
Robert Phillips8472a3d2020-04-16 16:27:45 -0400136 fCharacterization.origin(),
137 fCharacterization.sampleCount(),
138 fCharacterization.colorType(),
139 fCharacterization.refColorSpace(),
140 &fCharacterization.surfaceProps());
141}
142
143void DDLTileHelper::TileData::drawSKPDirectly(GrContext* context) {
144 SkASSERT(!fDisplayList && !fTileSurface && fReconstitutedPicture);
145
146 fTileSurface = this->makeWrappedTileDest(context);
147 if (fTileSurface) {
148 SkCanvas* tileCanvas = fTileSurface->getCanvas();
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000149
150 tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
151 tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
152
153 tileCanvas->drawPicture(fReconstitutedPicture);
154
Robert Phillips8472a3d2020-04-16 16:27:45 -0400155 // We can't snap an image here bc, since we're using wrapped backend textures for the
156 // surfaces, that would incur a copy.
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000157 }
158}
159
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500160void DDLTileHelper::TileData::draw(GrContext* context) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400161 SkASSERT(fDisplayList && !fTileSurface);
Robert Phillips96601082018-05-29 16:13:26 -0400162
Robert Phillips11c67672020-04-23 15:10:03 -0400163 // The tile's surface needs to be held until after the DDL is flushed bc the DDL doesn't take
164 // a ref on its destination proxy.
165 // TODO: make the DDL (or probably the drawing manager) take a ref on the destination proxy
166 // (maybe in GrDrawingManager::addDDLTarget).
Robert Phillips8472a3d2020-04-16 16:27:45 -0400167 fTileSurface = this->makeWrappedTileDest(context);
168 if (fTileSurface) {
169 fTileSurface->draw(fDisplayList.get());
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500170
Robert Phillips8472a3d2020-04-16 16:27:45 -0400171 // We can't snap an image here bc, since we're using wrapped backend textures for the
172 // surfaces, that would incur a copy.
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500173 }
Robert Phillips96601082018-05-29 16:13:26 -0400174}
175
Robert Phillips96601082018-05-29 16:13:26 -0400176void DDLTileHelper::TileData::reset() {
177 // TODO: when DDLs are re-renderable we don't need to do this
178 fDisplayList = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400179
Robert Phillips8472a3d2020-04-16 16:27:45 -0400180 fTileSurface = nullptr;
181}
182
Robert Phillips11c67672020-04-23 15:10:03 -0400183sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImage(SkDeferredDisplayListRecorder* recorder) {
184 SkASSERT(fCallbackContext);
185
186 // The promise image gets a ref on the promise callback context
187 sk_sp<SkImage> promiseImage = recorder->makePromiseTexture(
188 fCallbackContext->backendFormat(),
189 fClip.width(),
190 fClip.height(),
191 GrMipMapped::kNo,
192 GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin,
193 fCharacterization.colorType(),
194 kPremul_SkAlphaType,
195 fCharacterization.refColorSpace(),
196 PromiseImageCallbackContext::PromiseImageFulfillProc,
197 PromiseImageCallbackContext::PromiseImageReleaseProc,
198 PromiseImageCallbackContext::PromiseImageDoneProc,
199 (void*)this->refCallbackContext().release(),
200 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
201 fCallbackContext->wasAddedToImage();
202
203 return promiseImage;
204}
205
Robert Phillips8472a3d2020-04-16 16:27:45 -0400206void DDLTileHelper::TileData::CreateBackendTexture(GrContext* context, TileData* tile) {
207 SkASSERT(context->priv().asDirectContext());
Robert Phillips11c67672020-04-23 15:10:03 -0400208 SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400209
Robert Phillips11c67672020-04-23 15:10:03 -0400210 GrBackendTexture beTex = context->createBackendTexture(tile->fCharacterization);
211 tile->fCallbackContext->setBackendTexture(beTex);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400212}
213
214void DDLTileHelper::TileData::DeleteBackendTexture(GrContext* context, TileData* tile) {
215 SkASSERT(context->priv().asDirectContext());
Robert Phillips11c67672020-04-23 15:10:03 -0400216 SkASSERT(tile->fCallbackContext);
217
Robert Phillips8472a3d2020-04-16 16:27:45 -0400218 // TODO: it seems that, on the Linux bots, backend texture creation is failing
219 // a lot (skbug.com/10142)
Robert Phillips11c67672020-04-23 15:10:03 -0400220 SkASSERT(!tile->fCallbackContext->promiseImageTexture() ||
221 tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400222
223 tile->fTileSurface = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400224
225 SkASSERT(tile->fCallbackContext->unique());
226 tile->fCallbackContext.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400227}
228
229///////////////////////////////////////////////////////////////////////////////////////////////////
230
Robert Phillips11c67672020-04-23 15:10:03 -0400231DDLTileHelper::DDLTileHelper(GrContext* context,
Robert Phillips19f466d2020-02-26 10:27:07 -0500232 const SkSurfaceCharacterization& dstChar,
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500233 const SkIRect& viewport,
234 int numDivisions)
Robert Phillips11c67672020-04-23 15:10:03 -0400235 : fNumDivisions(numDivisions)
Adlai Holler8e821232020-05-11 13:33:59 -0400236 , fTiles(numDivisions * numDivisions)
Robert Phillips11c67672020-04-23 15:10:03 -0400237 , fDstCharacterization(dstChar) {
Robert Phillips96601082018-05-29 16:13:26 -0400238 SkASSERT(fNumDivisions > 0);
Robert Phillips96601082018-05-29 16:13:26 -0400239
240 int xTileSize = viewport.width()/fNumDivisions;
241 int yTileSize = viewport.height()/fNumDivisions;
242
243 // Create the destination tiles
244 for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) {
245 int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff;
246
247 for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) {
248 int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff;
249
250 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
251
252 SkASSERT(viewport.contains(clip));
253
Robert Phillips11c67672020-04-23 15:10:03 -0400254 fTiles[y*fNumDivisions+x].init(y*fNumDivisions+x, context, dstChar, clip);
Robert Phillips96601082018-05-29 16:13:26 -0400255 }
256 }
257}
258
259void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData,
260 const DDLPromiseImageHelper& helper) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500261 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400262 fTiles[i].createTileSpecificSKP(compressedPictureData, helper);
263 }
264}
265
266void DDLTileHelper::createDDLsInParallel() {
267#if 1
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500268 SkTaskGroup().batch(this->numTiles(), [&](int i) { fTiles[i].createDDL(); });
Robert Phillips11c67672020-04-23 15:10:03 -0400269 SkTaskGroup().add([this]{ this->createComposeDDL(); });
Robert Phillipsf18c7562018-06-13 09:01:36 -0400270 SkTaskGroup().wait();
Robert Phillips96601082018-05-29 16:13:26 -0400271#else
272 // Use this code path to debug w/o threads
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000273 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400274 fTiles[i].createDDL();
275 }
Robert Phillips11c67672020-04-23 15:10:03 -0400276 this->createComposeDDL();
Robert Phillips96601082018-05-29 16:13:26 -0400277#endif
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500278}
Robert Phillips96601082018-05-29 16:13:26 -0400279
Robert Phillips7b0ed552020-02-20 12:45:19 -0500280// On the gpu thread:
281// precompile any programs
282// replay the DDL into a surface to make the tile image
283// compose the tile image into the main canvas
284static void do_gpu_stuff(GrContext* context, DDLTileHelper::TileData* tile) {
Robert Phillips7b0ed552020-02-20 12:45:19 -0500285
286 // TODO: schedule program compilation as their own tasks
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500287 tile->precompile(context);
Robert Phillips7b0ed552020-02-20 12:45:19 -0500288
289 tile->draw(context);
Robert Phillips7b0ed552020-02-20 12:45:19 -0500290}
291
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500292// We expect to have more than one recording thread but just one gpu thread
293void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
294 SkTaskGroup* gpuTaskGroup,
295 GrContext* gpuThreadContext) {
296 SkASSERT(recordingTaskGroup && gpuTaskGroup && gpuThreadContext);
297
298 for (int i = 0; i < this->numTiles(); ++i) {
299 TileData* tile = &fTiles[i];
300
301 // On a recording thread:
302 // generate the tile's DDL
303 // schedule gpu-thread processing of the DDL
304 // Note: a finer grained approach would be add a scheduling task which would evaluate
305 // which DDLs were ready to be rendered based on their prerequisites
306 recordingTaskGroup->add([tile, gpuTaskGroup, gpuThreadContext]() {
307 tile->createDDL();
308
309 gpuTaskGroup->add([gpuThreadContext, tile]() {
Robert Phillips7b0ed552020-02-20 12:45:19 -0500310 do_gpu_stuff(gpuThreadContext, tile);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500311 });
312 });
313 }
Robert Phillips11c67672020-04-23 15:10:03 -0400314
315 recordingTaskGroup->add([this] { this->createComposeDDL(); });
Robert Phillips96601082018-05-29 16:13:26 -0400316}
317
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500318void DDLTileHelper::precompileAndDrawAllTiles(GrContext* context) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500319 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500320 fTiles[i].precompile(context);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500321 fTiles[i].draw(context);
Robert Phillips96601082018-05-29 16:13:26 -0400322 }
Robert Phillips96601082018-05-29 16:13:26 -0400323}
324
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000325void DDLTileHelper::interleaveDDLCreationAndDraw(GrContext* context) {
326 for (int i = 0; i < this->numTiles(); ++i) {
327 fTiles[i].createDDL();
328 fTiles[i].draw(context);
329 }
330}
331
332void DDLTileHelper::drawAllTilesDirectly(GrContext* context) {
333 for (int i = 0; i < this->numTiles(); ++i) {
334 fTiles[i].drawSKPDirectly(context);
335 }
336}
337
Robert Phillips11c67672020-04-23 15:10:03 -0400338void DDLTileHelper::dropCallbackContexts() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500339 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips11c67672020-04-23 15:10:03 -0400340 fTiles[i].dropCallbackContext();
Robert Phillips96601082018-05-29 16:13:26 -0400341 }
342}
343
344void DDLTileHelper::resetAllTiles() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500345 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400346 fTiles[i].reset();
347 }
Robert Phillips11c67672020-04-23 15:10:03 -0400348 fComposeDDL.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400349}
Robert Phillips8472a3d2020-04-16 16:27:45 -0400350
351void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrContext* context) {
352 SkASSERT(context->priv().asDirectContext());
353
354 if (taskGroup) {
355 for (int i = 0; i < this->numTiles(); ++i) {
356 TileData* tile = &fTiles[i];
357
358 taskGroup->add([context, tile]() { TileData::CreateBackendTexture(context, tile); });
359 }
360 } else {
361 for (int i = 0; i < this->numTiles(); ++i) {
362 TileData::CreateBackendTexture(context, &fTiles[i]);
363 }
364 }
365}
366
367void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrContext* context) {
368 SkASSERT(context->priv().asDirectContext());
369
370 if (taskGroup) {
371 for (int i = 0; i < this->numTiles(); ++i) {
372 TileData* tile = &fTiles[i];
373
374 taskGroup->add([context, tile]() { TileData::DeleteBackendTexture(context, tile); });
375 }
376 } else {
377 for (int i = 0; i < this->numTiles(); ++i) {
378 TileData::DeleteBackendTexture(context, &fTiles[i]);
379 }
380 }
381}