blob: 1ebf6f418ccab389a6669a58081cfbd60f895f6c [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"
Robert Phillipsd5f3c982020-07-07 13:18:47 -040015#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkDeferredDisplayListPriv.h"
17#include "src/core/SkTaskGroup.h"
Adlai Hollera0693042020-10-14 11:23:11 -040018#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/image/SkImage_Gpu.h"
20#include "tools/DDLPromiseImageHelper.h"
Robert Phillips96601082018-05-29 16:13:26 -040021
Robert Phillips19f466d2020-02-26 10:27:07 -050022void DDLTileHelper::TileData::init(int id,
Robert Phillipsd5f3c982020-07-07 13:18:47 -040023 GrDirectContext* direct,
Robert Phillips19f466d2020-02-26 10:27:07 -050024 const SkSurfaceCharacterization& dstSurfaceCharacterization,
Robert Phillips0c088492020-11-10 08:30:50 -050025 const SkIRect& clip,
26 const SkIRect& paddingOutsets) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -050027 fID = id;
Robert Phillipsa865a3a2020-02-14 10:49:39 -050028 fClip = clip;
Robert Phillips0c088492020-11-10 08:30:50 -050029 fPaddingOutsets = paddingOutsets;
Robert Phillipsa865a3a2020-02-14 10:49:39 -050030
Robert Phillipsbc688322020-12-14 11:38:59 -050031 fPlaybackChar = dstSurfaceCharacterization.createResized(this->paddedRectSize().width(),
32 this->paddedRectSize().height());
33 SkASSERT(fPlaybackChar.isValid());
Robert Phillips11c67672020-04-23 15:10:03 -040034
Robert Phillipsbc688322020-12-14 11:38:59 -050035 GrBackendFormat backendFormat = direct->defaultBackendFormat(fPlaybackChar.colorType(),
Robert Phillipsd5f3c982020-07-07 13:18:47 -040036 GrRenderable::kYes);
37 SkDEBUGCODE(const GrCaps* caps = direct->priv().caps());
Robert Phillips11c67672020-04-23 15:10:03 -040038 SkASSERT(caps->isFormatTexturable(backendFormat));
39
Robert Phillipsd5f3c982020-07-07 13:18:47 -040040 fCallbackContext.reset(new PromiseImageCallbackContext(direct, backendFormat));
Robert Phillips96601082018-05-29 16:13:26 -040041}
42
Robert Phillipscd1e3972021-02-22 17:24:22 -050043DDLTileHelper::TileData::TileData() {}
Robert Phillipsa865a3a2020-02-14 10:49:39 -050044DDLTileHelper::TileData::~TileData() {}
45
Robert Phillips96601082018-05-29 16:13:26 -040046void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData,
47 const DDLPromiseImageHelper& helper) {
Robert Phillips559f9c12021-01-11 12:29:20 -050048 if (!this->initialized()) {
49 return;
50 }
51
Robert Phillips7a3197b2018-09-26 21:18:23 +000052 SkASSERT(!fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040053
Robert Phillipsbc688322020-12-14 11:38:59 -050054 auto recordingChar = fPlaybackChar.createResized(fClip.width(), fClip.height());
55 SkASSERT(recordingChar.isValid());
56
Robert Phillips7a3197b2018-09-26 21:18:23 +000057 // This is bending the DDLRecorder contract! The promise images in the SKP should be
58 // created by the same recorder used to create the matching DDL.
Robert Phillipsbc688322020-12-14 11:38:59 -050059 SkDeferredDisplayListRecorder recorder(recordingChar);
Robert Phillips96601082018-05-29 16:13:26 -040060
Robert Phillips7a3197b2018-09-26 21:18:23 +000061 fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages);
Robert Phillipse8e2bb12018-09-27 14:26:47 -040062
Adlai Holler7580ad42020-06-24 13:45:25 -040063 auto ddl = recorder.detach();
Robert Phillipsdc369702020-08-13 13:34:27 -040064 if (ddl && ddl->priv().numRenderTasks()) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -040065 // TODO: remove this once skbug.com/8424 is fixed. If the DDL resulting from the
Greg Danielf41b2bd2019-08-22 16:19:24 -040066 // reinflation of the SKPs contains opsTasks that means some image subset operation
Robert Phillipse8e2bb12018-09-27 14:26:47 -040067 // created a draw.
68 fReconstitutedPicture.reset();
69 }
Robert Phillips96601082018-05-29 16:13:26 -040070}
71
72void DDLTileHelper::TileData::createDDL() {
Robert Phillips559f9c12021-01-11 12:29:20 -050073 if (!this->initialized()) {
74 return;
75 }
76
Robert Phillips24a8e9e2020-03-06 20:26:28 +000077 SkASSERT(!fDisplayList && fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040078
Robert Phillipsbc688322020-12-14 11:38:59 -050079 auto recordingChar = fPlaybackChar.createResized(fClip.width(), fClip.height());
80 SkASSERT(recordingChar.isValid());
81
82 SkDeferredDisplayListRecorder recorder(recordingChar);
Robert Phillips7a3197b2018-09-26 21:18:23 +000083
84 // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called.
85 // Maybe set it up in the ctor?
Robert Phillips24a8e9e2020-03-06 20:26:28 +000086 SkCanvas* recordingCanvas = recorder.getCanvas();
Robert Phillips7a3197b2018-09-26 21:18:23 +000087
88 // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's
89 // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct
90 // one now.
91 for (int i = 0; i < fPromiseImages.count(); ++i) {
Robert Phillips7a3197b2018-09-26 21:18:23 +000092 if (fPromiseImages[i]->isTextureBacked()) {
Adlai Holler302e8fb2020-09-14 11:58:06 -040093 auto rContext = recordingCanvas->recordingContext();
Jim Van Verth21bd60d2018-10-12 15:00:20 -040094 SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get();
Adlai Holler302e8fb2020-09-14 11:58:06 -040095 gpuImage->resetContext(sk_ref_sp(rContext));
Robert Phillips7a3197b2018-09-26 21:18:23 +000096 }
97 }
Robert Phillips96601082018-05-29 16:13:26 -040098
Robert Phillips0c088492020-11-10 08:30:50 -050099 // We always record the DDL in the (0,0) .. (clipWidth, clipHeight) coordinates
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000100 recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
101 recordingCanvas->translate(-fClip.fLeft, -fClip.fTop);
Robert Phillips96601082018-05-29 16:13:26 -0400102
103 // Note: in this use case we only render a picture to the deferred canvas
104 // but, more generally, clients will use arbitrary draw calls.
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000105 recordingCanvas->drawPicture(fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -0400106
Robert Phillips7a3197b2018-09-26 21:18:23 +0000107 fDisplayList = recorder.detach();
Robert Phillips96601082018-05-29 16:13:26 -0400108}
109
Robert Phillips11c67672020-04-23 15:10:03 -0400110void DDLTileHelper::createComposeDDL() {
111 SkASSERT(!fComposeDDL);
112
113 SkDeferredDisplayListRecorder recorder(fDstCharacterization);
114
115 SkCanvas* recordingCanvas = recorder.getCanvas();
116
117 for (int i = 0; i < this->numTiles(); ++i) {
118 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500119 if (!tile->initialized()) {
120 continue;
121 }
Robert Phillips11c67672020-04-23 15:10:03 -0400122
Robert Phillips0c088492020-11-10 08:30:50 -0500123 sk_sp<SkImage> promiseImage = tile->makePromiseImageForDst(&recorder);
Robert Phillips11c67672020-04-23 15:10:03 -0400124
Robert Phillips0c088492020-11-10 08:30:50 -0500125 SkRect dstRect = SkRect::Make(tile->clipRect());
126 SkIRect srcRect = tile->clipRect();
127 srcRect.offsetTo(tile->padOffset().x(), tile->padOffset().y());
Robert Phillips11c67672020-04-23 15:10:03 -0400128
Robert Phillips0c088492020-11-10 08:30:50 -0500129 SkASSERT(promiseImage->bounds().contains(srcRect));
Robert Phillips11c67672020-04-23 15:10:03 -0400130
Mike Reede02d7f82021-01-21 22:25:21 -0500131 recordingCanvas->drawImageRect(promiseImage.get(), SkRect::Make(srcRect), dstRect,
132 SkSamplingOptions(), nullptr,
133 SkCanvas::kStrict_SrcRectConstraint);
Robert Phillips11c67672020-04-23 15:10:03 -0400134 }
135
136 fComposeDDL = recorder.detach();
Robert Phillips889d6132020-06-16 11:11:33 -0400137 SkASSERT(fComposeDDL);
Robert Phillips11c67672020-04-23 15:10:03 -0400138}
139
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400140void DDLTileHelper::TileData::precompile(GrDirectContext* direct) {
Robert Phillips559f9c12021-01-11 12:29:20 -0500141 if (!this->initialized()) {
142 return;
143 }
144
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500145 SkASSERT(fDisplayList);
146
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400147 SkDeferredDisplayList::ProgramIterator iter(direct, fDisplayList.get());
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500148 for (; !iter.done(); iter.next()) {
149 iter.compile();
150 }
151}
152
Adlai Hollerb2705682020-10-20 10:11:53 -0400153sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext* context) {
Robert Phillips11c67672020-04-23 15:10:03 -0400154 SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture());
155
156 auto promiseImageTexture = fCallbackContext->promiseImageTexture();
157 if (!promiseImageTexture->backendTexture().isValid()) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400158 return nullptr;
159 }
Robert Phillips7ae9d2f2020-04-15 10:58:15 -0400160
Robert Phillips11c67672020-04-23 15:10:03 -0400161 // Here we are, unfortunately, aliasing the backend texture held by the SkPromiseImageTexture.
162 // Both the tile's destination surface and the promise image used to draw the tile will be
163 // backed by the same backendTexture - unbeknownst to Ganesh.
Robert Phillips8472a3d2020-04-16 16:27:45 -0400164 return SkSurface::MakeFromBackendTexture(context,
Robert Phillips11c67672020-04-23 15:10:03 -0400165 promiseImageTexture->backendTexture(),
Robert Phillipsbc688322020-12-14 11:38:59 -0500166 fPlaybackChar.origin(),
167 fPlaybackChar.sampleCount(),
168 fPlaybackChar.colorType(),
169 fPlaybackChar.refColorSpace(),
170 &fPlaybackChar.surfaceProps());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400171}
172
Adlai Hollerb2705682020-10-20 10:11:53 -0400173void DDLTileHelper::TileData::drawSKPDirectly(GrRecordingContext* context) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400174 SkASSERT(!fDisplayList && !fTileSurface && fReconstitutedPicture);
175
176 fTileSurface = this->makeWrappedTileDest(context);
177 if (fTileSurface) {
178 SkCanvas* tileCanvas = fTileSurface->getCanvas();
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000179
Robert Phillips0c088492020-11-10 08:30:50 -0500180 SkASSERT(this->padOffset().isZero() && this->paddedRectSize() == fClip.size());
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000181 tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
182 tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
183
184 tileCanvas->drawPicture(fReconstitutedPicture);
185
Robert Phillips8472a3d2020-04-16 16:27:45 -0400186 // We can't snap an image here bc, since we're using wrapped backend textures for the
187 // surfaces, that would incur a copy.
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000188 }
189}
190
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400191void DDLTileHelper::TileData::draw(GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400192 SkASSERT(fDisplayList && !fTileSurface);
Robert Phillips96601082018-05-29 16:13:26 -0400193
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400194 fTileSurface = this->makeWrappedTileDest(direct);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400195 if (fTileSurface) {
Robert Phillips0c088492020-11-10 08:30:50 -0500196 fTileSurface->draw(fDisplayList, this->padOffset().x(), this->padOffset().y());
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500197
Robert Phillips8472a3d2020-04-16 16:27:45 -0400198 // We can't snap an image here bc, since we're using wrapped backend textures for the
199 // surfaces, that would incur a copy.
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500200 }
Robert Phillips96601082018-05-29 16:13:26 -0400201}
202
Robert Phillips96601082018-05-29 16:13:26 -0400203void DDLTileHelper::TileData::reset() {
204 // TODO: when DDLs are re-renderable we don't need to do this
205 fDisplayList = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400206
Robert Phillips8472a3d2020-04-16 16:27:45 -0400207 fTileSurface = nullptr;
208}
209
Robert Phillips0c088492020-11-10 08:30:50 -0500210sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImageForDst(
211 SkDeferredDisplayListRecorder* recorder) {
Robert Phillips11c67672020-04-23 15:10:03 -0400212 SkASSERT(fCallbackContext);
213
214 // The promise image gets a ref on the promise callback context
Brian Salomonf1432742020-11-09 15:40:27 -0500215 sk_sp<SkImage> promiseImage =
216 recorder->makePromiseTexture(fCallbackContext->backendFormat(),
Robert Phillips0c088492020-11-10 08:30:50 -0500217 this->paddedRectSize().width(),
218 this->paddedRectSize().height(),
Brian Salomonf1432742020-11-09 15:40:27 -0500219 GrMipmapped::kNo,
220 GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin,
Robert Phillipsbc688322020-12-14 11:38:59 -0500221 fPlaybackChar.colorType(),
Brian Salomonf1432742020-11-09 15:40:27 -0500222 kPremul_SkAlphaType,
Robert Phillipsbc688322020-12-14 11:38:59 -0500223 fPlaybackChar.refColorSpace(),
Brian Salomonf1432742020-11-09 15:40:27 -0500224 PromiseImageCallbackContext::PromiseImageFulfillProc,
225 PromiseImageCallbackContext::PromiseImageReleaseProc,
226 (void*)this->refCallbackContext().release());
Robert Phillips11c67672020-04-23 15:10:03 -0400227 fCallbackContext->wasAddedToImage();
228
229 return promiseImage;
230}
231
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400232void DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext* direct, TileData* tile) {
Robert Phillips11c67672020-04-23 15:10:03 -0400233 SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture());
Robert Phillipsbc688322020-12-14 11:38:59 -0500234
235 const SkSurfaceCharacterization& c = tile->fPlaybackChar;
Greg Daniel398ecf12020-08-26 16:33:54 -0400236 GrBackendTexture beTex = direct->createBackendTexture(c.width(), c.height(), c.colorType(),
237 GrMipMapped(c.isMipMapped()),
238 GrRenderable::kYes);
Robert Phillips11c67672020-04-23 15:10:03 -0400239 tile->fCallbackContext->setBackendTexture(beTex);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400240}
241
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400242void DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext*, TileData* tile) {
Robert Phillips559f9c12021-01-11 12:29:20 -0500243 if (!tile->initialized()) {
244 return;
245 }
246
Robert Phillips11c67672020-04-23 15:10:03 -0400247 SkASSERT(tile->fCallbackContext);
248
Robert Phillips8472a3d2020-04-16 16:27:45 -0400249 // TODO: it seems that, on the Linux bots, backend texture creation is failing
250 // a lot (skbug.com/10142)
Robert Phillips11c67672020-04-23 15:10:03 -0400251 SkASSERT(!tile->fCallbackContext->promiseImageTexture() ||
252 tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400253
254 tile->fTileSurface = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400255
256 SkASSERT(tile->fCallbackContext->unique());
257 tile->fCallbackContext.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400258}
259
260///////////////////////////////////////////////////////////////////////////////////////////////////
261
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400262DDLTileHelper::DDLTileHelper(GrDirectContext* direct,
Robert Phillips19f466d2020-02-26 10:27:07 -0500263 const SkSurfaceCharacterization& dstChar,
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500264 const SkIRect& viewport,
Robert Phillips0c088492020-11-10 08:30:50 -0500265 int numDivisions,
266 bool addRandomPaddingToDst)
Robert Phillips11c67672020-04-23 15:10:03 -0400267 : fNumDivisions(numDivisions)
Adlai Holler8e821232020-05-11 13:33:59 -0400268 , fTiles(numDivisions * numDivisions)
Robert Phillips11c67672020-04-23 15:10:03 -0400269 , fDstCharacterization(dstChar) {
Robert Phillips96601082018-05-29 16:13:26 -0400270 SkASSERT(fNumDivisions > 0);
Robert Phillips96601082018-05-29 16:13:26 -0400271
272 int xTileSize = viewport.width()/fNumDivisions;
273 int yTileSize = viewport.height()/fNumDivisions;
274
Robert Phillips0c088492020-11-10 08:30:50 -0500275 SkRandom rand;
276
Robert Phillips96601082018-05-29 16:13:26 -0400277 // Create the destination tiles
278 for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) {
279 int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff;
280
281 for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) {
282 int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff;
283
284 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
285
286 SkASSERT(viewport.contains(clip));
287
Robert Phillips0c088492020-11-10 08:30:50 -0500288 static const uint32_t kMaxPad = 64;
289 int32_t lPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
290 int32_t tPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
291 int32_t rPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
292 int32_t bPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
293
294 fTiles[y*fNumDivisions+x].init(y*fNumDivisions+x, direct, dstChar, clip,
295 {lPad, tPad, rPad, bPad});
Robert Phillips96601082018-05-29 16:13:26 -0400296 }
297 }
298}
299
300void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData,
301 const DDLPromiseImageHelper& helper) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500302 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400303 fTiles[i].createTileSpecificSKP(compressedPictureData, helper);
304 }
305}
306
307void DDLTileHelper::createDDLsInParallel() {
308#if 1
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500309 SkTaskGroup().batch(this->numTiles(), [&](int i) { fTiles[i].createDDL(); });
Robert Phillips11c67672020-04-23 15:10:03 -0400310 SkTaskGroup().add([this]{ this->createComposeDDL(); });
Robert Phillipsf18c7562018-06-13 09:01:36 -0400311 SkTaskGroup().wait();
Robert Phillips96601082018-05-29 16:13:26 -0400312#else
313 // Use this code path to debug w/o threads
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000314 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400315 fTiles[i].createDDL();
316 }
Robert Phillips11c67672020-04-23 15:10:03 -0400317 this->createComposeDDL();
Robert Phillips96601082018-05-29 16:13:26 -0400318#endif
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500319}
Robert Phillips96601082018-05-29 16:13:26 -0400320
Robert Phillips7b0ed552020-02-20 12:45:19 -0500321// On the gpu thread:
322// precompile any programs
323// replay the DDL into a surface to make the tile image
324// compose the tile image into the main canvas
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400325static void do_gpu_stuff(GrDirectContext* direct, DDLTileHelper::TileData* tile) {
Robert Phillips7b0ed552020-02-20 12:45:19 -0500326
327 // TODO: schedule program compilation as their own tasks
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400328 tile->precompile(direct);
Robert Phillips7b0ed552020-02-20 12:45:19 -0500329
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400330 tile->draw(direct);
Robert Phillips5dbcca52020-05-29 10:41:33 -0400331
Robert Phillips5dbcca52020-05-29 10:41:33 -0400332 tile->dropDDL();
Robert Phillips7b0ed552020-02-20 12:45:19 -0500333}
334
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500335// We expect to have more than one recording thread but just one gpu thread
336void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
337 SkTaskGroup* gpuTaskGroup,
Robert Phillipscd1e3972021-02-22 17:24:22 -0500338 GrDirectContext* dContext) {
339 SkASSERT(recordingTaskGroup && gpuTaskGroup && dContext);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500340
341 for (int i = 0; i < this->numTiles(); ++i) {
342 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500343 if (!tile->initialized()) {
344 continue;
345 }
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500346
347 // On a recording thread:
348 // generate the tile's DDL
349 // schedule gpu-thread processing of the DDL
350 // Note: a finer grained approach would be add a scheduling task which would evaluate
351 // which DDLs were ready to be rendered based on their prerequisites
Robert Phillipscd1e3972021-02-22 17:24:22 -0500352 recordingTaskGroup->add([tile, gpuTaskGroup, dContext]() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500353 tile->createDDL();
354
Robert Phillipscd1e3972021-02-22 17:24:22 -0500355 gpuTaskGroup->add([dContext, tile]() {
356 do_gpu_stuff(dContext, tile);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500357 });
358 });
359 }
Robert Phillips11c67672020-04-23 15:10:03 -0400360
361 recordingTaskGroup->add([this] { this->createComposeDDL(); });
Robert Phillips96601082018-05-29 16:13:26 -0400362}
363
Robert Phillips5dbcca52020-05-29 10:41:33 -0400364// Only called from ViaDDL
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400365void DDLTileHelper::precompileAndDrawAllTiles(GrDirectContext* direct) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500366 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400367 fTiles[i].precompile(direct);
368 fTiles[i].draw(direct);
Robert Phillips96601082018-05-29 16:13:26 -0400369 }
Robert Phillips96601082018-05-29 16:13:26 -0400370}
371
Robert Phillips5dbcca52020-05-29 10:41:33 -0400372// Only called from skpbench
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400373void DDLTileHelper::interleaveDDLCreationAndDraw(GrDirectContext* direct) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000374 for (int i = 0; i < this->numTiles(); ++i) {
375 fTiles[i].createDDL();
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400376 fTiles[i].draw(direct);
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000377 }
378}
379
Robert Phillips5dbcca52020-05-29 10:41:33 -0400380// Only called from skpbench
Adlai Hollerb2705682020-10-20 10:11:53 -0400381void DDLTileHelper::drawAllTilesDirectly(GrDirectContext* context) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000382 for (int i = 0; i < this->numTiles(); ++i) {
383 fTiles[i].drawSKPDirectly(context);
384 }
385}
386
Robert Phillips11c67672020-04-23 15:10:03 -0400387void DDLTileHelper::dropCallbackContexts() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500388 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips11c67672020-04-23 15:10:03 -0400389 fTiles[i].dropCallbackContext();
Robert Phillips96601082018-05-29 16:13:26 -0400390 }
391}
392
393void DDLTileHelper::resetAllTiles() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500394 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400395 fTiles[i].reset();
396 }
Robert Phillips11c67672020-04-23 15:10:03 -0400397 fComposeDDL.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400398}
Robert Phillips8472a3d2020-04-16 16:27:45 -0400399
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400400void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400401
402 if (taskGroup) {
403 for (int i = 0; i < this->numTiles(); ++i) {
404 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500405 if (!tile->initialized()) {
406 continue;
407 }
Robert Phillips8472a3d2020-04-16 16:27:45 -0400408
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400409 taskGroup->add([direct, tile]() { TileData::CreateBackendTexture(direct, tile); });
Robert Phillips8472a3d2020-04-16 16:27:45 -0400410 }
411 } else {
412 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400413 TileData::CreateBackendTexture(direct, &fTiles[i]);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400414 }
415 }
416}
417
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400418void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400419 if (taskGroup) {
420 for (int i = 0; i < this->numTiles(); ++i) {
421 TileData* tile = &fTiles[i];
422
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400423 taskGroup->add([direct, tile]() { TileData::DeleteBackendTexture(direct, tile); });
Robert Phillips8472a3d2020-04-16 16:27:45 -0400424 }
425 } else {
426 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400427 TileData::DeleteBackendTexture(direct, &fTiles[i]);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400428 }
429 }
430}