blob: 8477b1f3462b330251a4790cd3751641903e46fc [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,
22 sk_sp<SkSurface> dstSurface,
23 const SkSurfaceCharacterization& dstSurfaceCharacterization,
24 const SkIRect& clip) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -050025 fID = id;
26 fDstSurface = dstSurface;
27 fClip = clip;
28
Robert Phillips19f466d2020-02-26 10:27:07 -050029 fCharacterization = dstSurfaceCharacterization.createResized(clip.width(), clip.height());
Robert Phillipsa865a3a2020-02-14 10:49:39 -050030 SkASSERT(fCharacterization.isValid());
Robert Phillips8472a3d2020-04-16 16:27:45 -040031 SkASSERT(!fBackendTexture.isValid());
Robert Phillips96601082018-05-29 16:13:26 -040032}
33
Robert Phillipsa865a3a2020-02-14 10:49:39 -050034DDLTileHelper::TileData::~TileData() {}
35
Robert Phillips96601082018-05-29 16:13:26 -040036void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData,
37 const DDLPromiseImageHelper& helper) {
Robert Phillips7a3197b2018-09-26 21:18:23 +000038 SkASSERT(!fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040039
Robert Phillips7a3197b2018-09-26 21:18:23 +000040 // This is bending the DDLRecorder contract! The promise images in the SKP should be
41 // created by the same recorder used to create the matching DDL.
42 SkDeferredDisplayListRecorder recorder(fCharacterization);
Robert Phillips96601082018-05-29 16:13:26 -040043
Robert Phillips7a3197b2018-09-26 21:18:23 +000044 fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages);
Robert Phillipse8e2bb12018-09-27 14:26:47 -040045
46 std::unique_ptr<SkDeferredDisplayList> ddl = recorder.detach();
Chris Dalton6b498102019-08-01 14:14:52 -060047 if (ddl->priv().numRenderTasks()) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -040048 // TODO: remove this once skbug.com/8424 is fixed. If the DDL resulting from the
Greg Danielf41b2bd2019-08-22 16:19:24 -040049 // reinflation of the SKPs contains opsTasks that means some image subset operation
Robert Phillipse8e2bb12018-09-27 14:26:47 -040050 // created a draw.
51 fReconstitutedPicture.reset();
52 }
Robert Phillips96601082018-05-29 16:13:26 -040053}
54
55void DDLTileHelper::TileData::createDDL() {
Robert Phillips24a8e9e2020-03-06 20:26:28 +000056 SkASSERT(!fDisplayList && fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040057
Robert Phillips7a3197b2018-09-26 21:18:23 +000058 SkDeferredDisplayListRecorder recorder(fCharacterization);
59
60 // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called.
61 // Maybe set it up in the ctor?
Robert Phillips24a8e9e2020-03-06 20:26:28 +000062 SkCanvas* recordingCanvas = recorder.getCanvas();
Robert Phillips7a3197b2018-09-26 21:18:23 +000063
64 // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's
65 // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct
66 // one now.
67 for (int i = 0; i < fPromiseImages.count(); ++i) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +000068 GrContext* newContext = recordingCanvas->getGrContext();
Robert Phillips7a3197b2018-09-26 21:18:23 +000069
70 if (fPromiseImages[i]->isTextureBacked()) {
Jim Van Verth21bd60d2018-10-12 15:00:20 -040071 SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get();
Robert Phillips7a3197b2018-09-26 21:18:23 +000072 gpuImage->resetContext(sk_ref_sp(newContext));
73 }
74 }
Robert Phillips96601082018-05-29 16:13:26 -040075
Robert Phillips24a8e9e2020-03-06 20:26:28 +000076 recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
77 recordingCanvas->translate(-fClip.fLeft, -fClip.fTop);
Robert Phillips96601082018-05-29 16:13:26 -040078
79 // Note: in this use case we only render a picture to the deferred canvas
80 // but, more generally, clients will use arbitrary draw calls.
Robert Phillips24a8e9e2020-03-06 20:26:28 +000081 recordingCanvas->drawPicture(fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040082
Robert Phillips7a3197b2018-09-26 21:18:23 +000083 fDisplayList = recorder.detach();
Robert Phillips96601082018-05-29 16:13:26 -040084}
85
Robert Phillips6eb5cb92020-03-05 12:52:45 -050086void DDLTileHelper::TileData::precompile(GrContext* context) {
87 SkASSERT(fDisplayList);
88
89 SkDeferredDisplayList::ProgramIterator iter(context, fDisplayList.get());
90 for (; !iter.done(); iter.next()) {
91 iter.compile();
92 }
93}
94
Robert Phillips8472a3d2020-04-16 16:27:45 -040095sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrContext* context) {
96 if (!fBackendTexture.isValid()) {
97 return nullptr;
98 }
Robert Phillips7ae9d2f2020-04-15 10:58:15 -040099
Robert Phillips8472a3d2020-04-16 16:27:45 -0400100 return SkSurface::MakeFromBackendTexture(context,
101 fBackendTexture,
102 fCharacterization.origin(),
103 fCharacterization.sampleCount(),
104 fCharacterization.colorType(),
105 fCharacterization.refColorSpace(),
106 &fCharacterization.surfaceProps());
107}
108
109void DDLTileHelper::TileData::drawSKPDirectly(GrContext* context) {
110 SkASSERT(!fDisplayList && !fTileSurface && fReconstitutedPicture);
111
112 fTileSurface = this->makeWrappedTileDest(context);
113 if (fTileSurface) {
114 SkCanvas* tileCanvas = fTileSurface->getCanvas();
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000115
116 tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
117 tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
118
119 tileCanvas->drawPicture(fReconstitutedPicture);
120
Robert Phillips8472a3d2020-04-16 16:27:45 -0400121 // We can't snap an image here bc, since we're using wrapped backend textures for the
122 // surfaces, that would incur a copy.
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000123 }
124}
125
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500126void DDLTileHelper::TileData::draw(GrContext* context) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400127 SkASSERT(fDisplayList && !fTileSurface);
Robert Phillips96601082018-05-29 16:13:26 -0400128
Robert Phillips8472a3d2020-04-16 16:27:45 -0400129 // The tile's surface needs to be held until after the DDL is flushed
130 fTileSurface = this->makeWrappedTileDest(context);
131 if (fTileSurface) {
132 fTileSurface->draw(fDisplayList.get());
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500133
Robert Phillips8472a3d2020-04-16 16:27:45 -0400134 // We can't snap an image here bc, since we're using wrapped backend textures for the
135 // surfaces, that would incur a copy.
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500136 }
Robert Phillips96601082018-05-29 16:13:26 -0400137}
138
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500139// TODO: We should create a single DDL for the composition step and just add replaying it
140// as the last GPU task
Robert Phillips8472a3d2020-04-16 16:27:45 -0400141void DDLTileHelper::TileData::compose(GrContext* context) {
142 SkASSERT(context->priv().asDirectContext());
143 SkASSERT(fDstSurface);
144
145 if (!fBackendTexture.isValid()) {
146 return;
147 }
148
149 // Here we are, unfortunately, aliasing 'fBackendTexture'. It is backing both 'fTileSurface'
150 // and 'tmp'.
151 sk_sp<SkImage> tmp = SkImage::MakeFromTexture(context,
152 fBackendTexture,
153 fCharacterization.origin(),
154 fCharacterization.colorType(),
155 kPremul_SkAlphaType,
156 fCharacterization.refColorSpace());
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500157
158 SkCanvas* canvas = fDstSurface->getCanvas();
159 canvas->save();
160 canvas->clipRect(SkRect::Make(fClip));
Robert Phillips8472a3d2020-04-16 16:27:45 -0400161 canvas->drawImage(tmp, fClip.fLeft, fClip.fTop);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500162 canvas->restore();
Robert Phillips96601082018-05-29 16:13:26 -0400163}
164
165void DDLTileHelper::TileData::reset() {
166 // TODO: when DDLs are re-renderable we don't need to do this
167 fDisplayList = nullptr;
Robert Phillips8472a3d2020-04-16 16:27:45 -0400168 fTileSurface = nullptr;
169}
170
171void DDLTileHelper::TileData::CreateBackendTexture(GrContext* context, TileData* tile) {
172 SkASSERT(context->priv().asDirectContext());
173 SkASSERT(!tile->fBackendTexture.isValid());
174
175 tile->fBackendTexture = context->createBackendTexture(tile->fCharacterization);
176 // TODO: it seems that, on the Linux bots, backend texture creation is failing
177 // a lot (skbug.com/10142)
178 //SkASSERT(tile->fBackendTexture.isValid());
179}
180
181void DDLTileHelper::TileData::DeleteBackendTexture(GrContext* context, TileData* tile) {
182 SkASSERT(context->priv().asDirectContext());
183 // TODO: it seems that, on the Linux bots, backend texture creation is failing
184 // a lot (skbug.com/10142)
185 //SkASSERT(tile->fBackendTexture.isValid());
186
187 tile->fTileSurface = nullptr;
188 context->deleteBackendTexture(tile->fBackendTexture);
Robert Phillips96601082018-05-29 16:13:26 -0400189}
190
191///////////////////////////////////////////////////////////////////////////////////////////////////
192
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500193DDLTileHelper::DDLTileHelper(sk_sp<SkSurface> dstSurface,
Robert Phillips19f466d2020-02-26 10:27:07 -0500194 const SkSurfaceCharacterization& dstChar,
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500195 const SkIRect& viewport,
196 int numDivisions)
Robert Phillips96601082018-05-29 16:13:26 -0400197 : fNumDivisions(numDivisions) {
198 SkASSERT(fNumDivisions > 0);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500199 fTiles = new TileData[this->numTiles()];
Robert Phillips96601082018-05-29 16:13:26 -0400200
201 int xTileSize = viewport.width()/fNumDivisions;
202 int yTileSize = viewport.height()/fNumDivisions;
203
204 // Create the destination tiles
205 for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) {
206 int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff;
207
208 for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) {
209 int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff;
210
211 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
212
213 SkASSERT(viewport.contains(clip));
214
Robert Phillips19f466d2020-02-26 10:27:07 -0500215 fTiles[y*fNumDivisions+x].init(y*fNumDivisions+x, dstSurface, dstChar, clip);
Robert Phillips96601082018-05-29 16:13:26 -0400216 }
217 }
218}
219
220void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData,
221 const DDLPromiseImageHelper& helper) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500222 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400223 fTiles[i].createTileSpecificSKP(compressedPictureData, helper);
224 }
225}
226
227void DDLTileHelper::createDDLsInParallel() {
228#if 1
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500229 SkTaskGroup().batch(this->numTiles(), [&](int i) { fTiles[i].createDDL(); });
Robert Phillipsf18c7562018-06-13 09:01:36 -0400230 SkTaskGroup().wait();
Robert Phillips96601082018-05-29 16:13:26 -0400231#else
232 // Use this code path to debug w/o threads
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000233 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400234 fTiles[i].createDDL();
235 }
236#endif
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500237}
Robert Phillips96601082018-05-29 16:13:26 -0400238
Robert Phillips7b0ed552020-02-20 12:45:19 -0500239// On the gpu thread:
240// precompile any programs
241// replay the DDL into a surface to make the tile image
242// compose the tile image into the main canvas
243static void do_gpu_stuff(GrContext* context, DDLTileHelper::TileData* tile) {
Robert Phillips7b0ed552020-02-20 12:45:19 -0500244
245 // TODO: schedule program compilation as their own tasks
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500246 tile->precompile(context);
Robert Phillips7b0ed552020-02-20 12:45:19 -0500247
248 tile->draw(context);
249
250 // TODO: we should actually have a separate DDL that does
251 // the final composition draw
Robert Phillips8472a3d2020-04-16 16:27:45 -0400252 tile->compose(context);
Robert Phillips7b0ed552020-02-20 12:45:19 -0500253}
254
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500255// We expect to have more than one recording thread but just one gpu thread
256void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
257 SkTaskGroup* gpuTaskGroup,
258 GrContext* gpuThreadContext) {
259 SkASSERT(recordingTaskGroup && gpuTaskGroup && gpuThreadContext);
260
261 for (int i = 0; i < this->numTiles(); ++i) {
262 TileData* tile = &fTiles[i];
263
264 // On a recording thread:
265 // generate the tile's DDL
266 // schedule gpu-thread processing of the DDL
267 // Note: a finer grained approach would be add a scheduling task which would evaluate
268 // which DDLs were ready to be rendered based on their prerequisites
269 recordingTaskGroup->add([tile, gpuTaskGroup, gpuThreadContext]() {
270 tile->createDDL();
271
272 gpuTaskGroup->add([gpuThreadContext, tile]() {
Robert Phillips7b0ed552020-02-20 12:45:19 -0500273 do_gpu_stuff(gpuThreadContext, tile);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500274 });
275 });
276 }
Robert Phillips96601082018-05-29 16:13:26 -0400277}
278
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500279void DDLTileHelper::precompileAndDrawAllTiles(GrContext* context) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500280 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500281 fTiles[i].precompile(context);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500282 fTiles[i].draw(context);
Robert Phillips96601082018-05-29 16:13:26 -0400283 }
Robert Phillips96601082018-05-29 16:13:26 -0400284}
285
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000286void DDLTileHelper::interleaveDDLCreationAndDraw(GrContext* context) {
287 for (int i = 0; i < this->numTiles(); ++i) {
288 fTiles[i].createDDL();
289 fTiles[i].draw(context);
290 }
291}
292
293void DDLTileHelper::drawAllTilesDirectly(GrContext* context) {
294 for (int i = 0; i < this->numTiles(); ++i) {
295 fTiles[i].drawSKPDirectly(context);
296 }
297}
298
Robert Phillips8472a3d2020-04-16 16:27:45 -0400299void DDLTileHelper::composeAllTiles(GrContext* context) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500300 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400301 fTiles[i].compose(context);
Robert Phillips96601082018-05-29 16:13:26 -0400302 }
303}
304
305void DDLTileHelper::resetAllTiles() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500306 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400307 fTiles[i].reset();
308 }
309}
Robert Phillips8472a3d2020-04-16 16:27:45 -0400310
311void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrContext* context) {
312 SkASSERT(context->priv().asDirectContext());
313
314 if (taskGroup) {
315 for (int i = 0; i < this->numTiles(); ++i) {
316 TileData* tile = &fTiles[i];
317
318 taskGroup->add([context, tile]() { TileData::CreateBackendTexture(context, tile); });
319 }
320 } else {
321 for (int i = 0; i < this->numTiles(); ++i) {
322 TileData::CreateBackendTexture(context, &fTiles[i]);
323 }
324 }
325}
326
327void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrContext* context) {
328 SkASSERT(context->priv().asDirectContext());
329
330 if (taskGroup) {
331 for (int i = 0; i < this->numTiles(); ++i) {
332 TileData* tile = &fTiles[i];
333
334 taskGroup->add([context, tile]() { TileData::DeleteBackendTexture(context, tile); });
335 }
336 } else {
337 for (int i = 0; i < this->numTiles(); ++i) {
338 TileData::DeleteBackendTexture(context, &fTiles[i]);
339 }
340 }
341}