blob: 3af6384652e61a27a9924f7e48dabf1fae7ecaf7 [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 Phillipsa865a3a2020-02-14 10:49:39 -050043DDLTileHelper::TileData::~TileData() {}
44
Robert Phillips96601082018-05-29 16:13:26 -040045void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData,
46 const DDLPromiseImageHelper& helper) {
Robert Phillips559f9c12021-01-11 12:29:20 -050047 if (!this->initialized()) {
48 return;
49 }
50
Robert Phillips7a3197b2018-09-26 21:18:23 +000051 SkASSERT(!fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040052
Robert Phillipsbc688322020-12-14 11:38:59 -050053 auto recordingChar = fPlaybackChar.createResized(fClip.width(), fClip.height());
54 SkASSERT(recordingChar.isValid());
55
Robert Phillips7a3197b2018-09-26 21:18:23 +000056 // This is bending the DDLRecorder contract! The promise images in the SKP should be
57 // created by the same recorder used to create the matching DDL.
Robert Phillipsbc688322020-12-14 11:38:59 -050058 SkDeferredDisplayListRecorder recorder(recordingChar);
Robert Phillips96601082018-05-29 16:13:26 -040059
Robert Phillips7a3197b2018-09-26 21:18:23 +000060 fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages);
Robert Phillipse8e2bb12018-09-27 14:26:47 -040061
Adlai Holler7580ad42020-06-24 13:45:25 -040062 auto ddl = recorder.detach();
Robert Phillipsdc369702020-08-13 13:34:27 -040063 if (ddl && ddl->priv().numRenderTasks()) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -040064 // TODO: remove this once skbug.com/8424 is fixed. If the DDL resulting from the
Greg Danielf41b2bd2019-08-22 16:19:24 -040065 // reinflation of the SKPs contains opsTasks that means some image subset operation
Robert Phillipse8e2bb12018-09-27 14:26:47 -040066 // created a draw.
67 fReconstitutedPicture.reset();
68 }
Robert Phillips96601082018-05-29 16:13:26 -040069}
70
71void DDLTileHelper::TileData::createDDL() {
Robert Phillips559f9c12021-01-11 12:29:20 -050072 if (!this->initialized()) {
73 return;
74 }
75
Robert Phillips24a8e9e2020-03-06 20:26:28 +000076 SkASSERT(!fDisplayList && fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040077
Robert Phillipsbc688322020-12-14 11:38:59 -050078 auto recordingChar = fPlaybackChar.createResized(fClip.width(), fClip.height());
79 SkASSERT(recordingChar.isValid());
80
81 SkDeferredDisplayListRecorder recorder(recordingChar);
Robert Phillips7a3197b2018-09-26 21:18:23 +000082
83 // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called.
84 // Maybe set it up in the ctor?
Robert Phillips24a8e9e2020-03-06 20:26:28 +000085 SkCanvas* recordingCanvas = recorder.getCanvas();
Robert Phillips7a3197b2018-09-26 21:18:23 +000086
87 // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's
88 // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct
89 // one now.
90 for (int i = 0; i < fPromiseImages.count(); ++i) {
Robert Phillips7a3197b2018-09-26 21:18:23 +000091 if (fPromiseImages[i]->isTextureBacked()) {
Adlai Holler302e8fb2020-09-14 11:58:06 -040092 auto rContext = recordingCanvas->recordingContext();
Jim Van Verth21bd60d2018-10-12 15:00:20 -040093 SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get();
Adlai Holler302e8fb2020-09-14 11:58:06 -040094 gpuImage->resetContext(sk_ref_sp(rContext));
Robert Phillips7a3197b2018-09-26 21:18:23 +000095 }
96 }
Robert Phillips96601082018-05-29 16:13:26 -040097
Robert Phillips0c088492020-11-10 08:30:50 -050098 // We always record the DDL in the (0,0) .. (clipWidth, clipHeight) coordinates
Robert Phillips24a8e9e2020-03-06 20:26:28 +000099 recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
100 recordingCanvas->translate(-fClip.fLeft, -fClip.fTop);
Robert Phillips96601082018-05-29 16:13:26 -0400101
102 // Note: in this use case we only render a picture to the deferred canvas
103 // but, more generally, clients will use arbitrary draw calls.
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000104 recordingCanvas->drawPicture(fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -0400105
Robert Phillips7a3197b2018-09-26 21:18:23 +0000106 fDisplayList = recorder.detach();
Robert Phillips96601082018-05-29 16:13:26 -0400107}
108
Robert Phillips11c67672020-04-23 15:10:03 -0400109void DDLTileHelper::createComposeDDL() {
110 SkASSERT(!fComposeDDL);
111
112 SkDeferredDisplayListRecorder recorder(fDstCharacterization);
113
114 SkCanvas* recordingCanvas = recorder.getCanvas();
115
116 for (int i = 0; i < this->numTiles(); ++i) {
117 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500118 if (!tile->initialized()) {
119 continue;
120 }
Robert Phillips11c67672020-04-23 15:10:03 -0400121
Robert Phillips0c088492020-11-10 08:30:50 -0500122 sk_sp<SkImage> promiseImage = tile->makePromiseImageForDst(&recorder);
Robert Phillips11c67672020-04-23 15:10:03 -0400123
Robert Phillips0c088492020-11-10 08:30:50 -0500124 SkRect dstRect = SkRect::Make(tile->clipRect());
125 SkIRect srcRect = tile->clipRect();
126 srcRect.offsetTo(tile->padOffset().x(), tile->padOffset().y());
Robert Phillips11c67672020-04-23 15:10:03 -0400127
Robert Phillips0c088492020-11-10 08:30:50 -0500128 SkASSERT(promiseImage->bounds().contains(srcRect));
Robert Phillips11c67672020-04-23 15:10:03 -0400129
Robert Phillips0c088492020-11-10 08:30:50 -0500130 recordingCanvas->drawImageRect(promiseImage.get(), srcRect, dstRect, nullptr);
Robert Phillips11c67672020-04-23 15:10:03 -0400131 }
132
133 fComposeDDL = recorder.detach();
Robert Phillips889d6132020-06-16 11:11:33 -0400134 SkASSERT(fComposeDDL);
Robert Phillips11c67672020-04-23 15:10:03 -0400135}
136
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400137void DDLTileHelper::TileData::precompile(GrDirectContext* direct) {
Robert Phillips559f9c12021-01-11 12:29:20 -0500138 if (!this->initialized()) {
139 return;
140 }
141
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500142 SkASSERT(fDisplayList);
143
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400144 SkDeferredDisplayList::ProgramIterator iter(direct, fDisplayList.get());
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500145 for (; !iter.done(); iter.next()) {
146 iter.compile();
147 }
148}
149
Adlai Hollerb2705682020-10-20 10:11:53 -0400150sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext* context) {
Robert Phillips11c67672020-04-23 15:10:03 -0400151 SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture());
152
153 auto promiseImageTexture = fCallbackContext->promiseImageTexture();
154 if (!promiseImageTexture->backendTexture().isValid()) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400155 return nullptr;
156 }
Robert Phillips7ae9d2f2020-04-15 10:58:15 -0400157
Robert Phillips11c67672020-04-23 15:10:03 -0400158 // Here we are, unfortunately, aliasing the backend texture held by the SkPromiseImageTexture.
159 // Both the tile's destination surface and the promise image used to draw the tile will be
160 // backed by the same backendTexture - unbeknownst to Ganesh.
Robert Phillips8472a3d2020-04-16 16:27:45 -0400161 return SkSurface::MakeFromBackendTexture(context,
Robert Phillips11c67672020-04-23 15:10:03 -0400162 promiseImageTexture->backendTexture(),
Robert Phillipsbc688322020-12-14 11:38:59 -0500163 fPlaybackChar.origin(),
164 fPlaybackChar.sampleCount(),
165 fPlaybackChar.colorType(),
166 fPlaybackChar.refColorSpace(),
167 &fPlaybackChar.surfaceProps());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400168}
169
Adlai Hollerb2705682020-10-20 10:11:53 -0400170void DDLTileHelper::TileData::drawSKPDirectly(GrRecordingContext* context) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400171 SkASSERT(!fDisplayList && !fTileSurface && fReconstitutedPicture);
172
173 fTileSurface = this->makeWrappedTileDest(context);
174 if (fTileSurface) {
175 SkCanvas* tileCanvas = fTileSurface->getCanvas();
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000176
Robert Phillips0c088492020-11-10 08:30:50 -0500177 SkASSERT(this->padOffset().isZero() && this->paddedRectSize() == fClip.size());
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000178 tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
179 tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
180
181 tileCanvas->drawPicture(fReconstitutedPicture);
182
Robert Phillips8472a3d2020-04-16 16:27:45 -0400183 // We can't snap an image here bc, since we're using wrapped backend textures for the
184 // surfaces, that would incur a copy.
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000185 }
186}
187
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400188void DDLTileHelper::TileData::draw(GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400189 SkASSERT(fDisplayList && !fTileSurface);
Robert Phillips96601082018-05-29 16:13:26 -0400190
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400191 fTileSurface = this->makeWrappedTileDest(direct);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400192 if (fTileSurface) {
Robert Phillips0c088492020-11-10 08:30:50 -0500193 fTileSurface->draw(fDisplayList, this->padOffset().x(), this->padOffset().y());
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500194
Robert Phillips8472a3d2020-04-16 16:27:45 -0400195 // We can't snap an image here bc, since we're using wrapped backend textures for the
196 // surfaces, that would incur a copy.
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500197 }
Robert Phillips96601082018-05-29 16:13:26 -0400198}
199
Robert Phillips96601082018-05-29 16:13:26 -0400200void DDLTileHelper::TileData::reset() {
201 // TODO: when DDLs are re-renderable we don't need to do this
202 fDisplayList = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400203
Robert Phillips8472a3d2020-04-16 16:27:45 -0400204 fTileSurface = nullptr;
205}
206
Robert Phillips0c088492020-11-10 08:30:50 -0500207sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImageForDst(
208 SkDeferredDisplayListRecorder* recorder) {
Robert Phillips11c67672020-04-23 15:10:03 -0400209 SkASSERT(fCallbackContext);
210
211 // The promise image gets a ref on the promise callback context
Brian Salomonf1432742020-11-09 15:40:27 -0500212 sk_sp<SkImage> promiseImage =
213 recorder->makePromiseTexture(fCallbackContext->backendFormat(),
Robert Phillips0c088492020-11-10 08:30:50 -0500214 this->paddedRectSize().width(),
215 this->paddedRectSize().height(),
Brian Salomonf1432742020-11-09 15:40:27 -0500216 GrMipmapped::kNo,
217 GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin,
Robert Phillipsbc688322020-12-14 11:38:59 -0500218 fPlaybackChar.colorType(),
Brian Salomonf1432742020-11-09 15:40:27 -0500219 kPremul_SkAlphaType,
Robert Phillipsbc688322020-12-14 11:38:59 -0500220 fPlaybackChar.refColorSpace(),
Brian Salomonf1432742020-11-09 15:40:27 -0500221 PromiseImageCallbackContext::PromiseImageFulfillProc,
222 PromiseImageCallbackContext::PromiseImageReleaseProc,
223 (void*)this->refCallbackContext().release());
Robert Phillips11c67672020-04-23 15:10:03 -0400224 fCallbackContext->wasAddedToImage();
225
226 return promiseImage;
227}
228
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400229void DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext* direct, TileData* tile) {
Robert Phillips11c67672020-04-23 15:10:03 -0400230 SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture());
Robert Phillipsbc688322020-12-14 11:38:59 -0500231
232 const SkSurfaceCharacterization& c = tile->fPlaybackChar;
Greg Daniel398ecf12020-08-26 16:33:54 -0400233 GrBackendTexture beTex = direct->createBackendTexture(c.width(), c.height(), c.colorType(),
234 GrMipMapped(c.isMipMapped()),
235 GrRenderable::kYes);
Robert Phillips11c67672020-04-23 15:10:03 -0400236 tile->fCallbackContext->setBackendTexture(beTex);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400237}
238
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400239void DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext*, TileData* tile) {
Robert Phillips559f9c12021-01-11 12:29:20 -0500240 if (!tile->initialized()) {
241 return;
242 }
243
Robert Phillips11c67672020-04-23 15:10:03 -0400244 SkASSERT(tile->fCallbackContext);
245
Robert Phillips8472a3d2020-04-16 16:27:45 -0400246 // TODO: it seems that, on the Linux bots, backend texture creation is failing
247 // a lot (skbug.com/10142)
Robert Phillips11c67672020-04-23 15:10:03 -0400248 SkASSERT(!tile->fCallbackContext->promiseImageTexture() ||
249 tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400250
251 tile->fTileSurface = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400252
253 SkASSERT(tile->fCallbackContext->unique());
254 tile->fCallbackContext.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400255}
256
257///////////////////////////////////////////////////////////////////////////////////////////////////
258
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400259DDLTileHelper::DDLTileHelper(GrDirectContext* direct,
Robert Phillips19f466d2020-02-26 10:27:07 -0500260 const SkSurfaceCharacterization& dstChar,
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500261 const SkIRect& viewport,
Robert Phillips0c088492020-11-10 08:30:50 -0500262 int numDivisions,
263 bool addRandomPaddingToDst)
Robert Phillips11c67672020-04-23 15:10:03 -0400264 : fNumDivisions(numDivisions)
Adlai Holler8e821232020-05-11 13:33:59 -0400265 , fTiles(numDivisions * numDivisions)
Robert Phillips11c67672020-04-23 15:10:03 -0400266 , fDstCharacterization(dstChar) {
Robert Phillips96601082018-05-29 16:13:26 -0400267 SkASSERT(fNumDivisions > 0);
Robert Phillips96601082018-05-29 16:13:26 -0400268
269 int xTileSize = viewport.width()/fNumDivisions;
270 int yTileSize = viewport.height()/fNumDivisions;
271
Robert Phillips0c088492020-11-10 08:30:50 -0500272 SkRandom rand;
273
Robert Phillips96601082018-05-29 16:13:26 -0400274 // Create the destination tiles
275 for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) {
276 int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff;
277
278 for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) {
279 int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff;
280
281 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
282
283 SkASSERT(viewport.contains(clip));
284
Robert Phillips0c088492020-11-10 08:30:50 -0500285 static const uint32_t kMaxPad = 64;
286 int32_t lPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
287 int32_t tPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
288 int32_t rPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
289 int32_t bPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
290
291 fTiles[y*fNumDivisions+x].init(y*fNumDivisions+x, direct, dstChar, clip,
292 {lPad, tPad, rPad, bPad});
Robert Phillips96601082018-05-29 16:13:26 -0400293 }
294 }
295}
296
297void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData,
298 const DDLPromiseImageHelper& helper) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500299 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400300 fTiles[i].createTileSpecificSKP(compressedPictureData, helper);
301 }
302}
303
304void DDLTileHelper::createDDLsInParallel() {
305#if 1
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500306 SkTaskGroup().batch(this->numTiles(), [&](int i) { fTiles[i].createDDL(); });
Robert Phillips11c67672020-04-23 15:10:03 -0400307 SkTaskGroup().add([this]{ this->createComposeDDL(); });
Robert Phillipsf18c7562018-06-13 09:01:36 -0400308 SkTaskGroup().wait();
Robert Phillips96601082018-05-29 16:13:26 -0400309#else
310 // Use this code path to debug w/o threads
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000311 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400312 fTiles[i].createDDL();
313 }
Robert Phillips11c67672020-04-23 15:10:03 -0400314 this->createComposeDDL();
Robert Phillips96601082018-05-29 16:13:26 -0400315#endif
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500316}
Robert Phillips96601082018-05-29 16:13:26 -0400317
Robert Phillips7b0ed552020-02-20 12:45:19 -0500318// On the gpu thread:
319// precompile any programs
320// replay the DDL into a surface to make the tile image
321// compose the tile image into the main canvas
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400322static void do_gpu_stuff(GrDirectContext* direct, DDLTileHelper::TileData* tile) {
Robert Phillips7b0ed552020-02-20 12:45:19 -0500323
324 // TODO: schedule program compilation as their own tasks
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400325 tile->precompile(direct);
Robert Phillips7b0ed552020-02-20 12:45:19 -0500326
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400327 tile->draw(direct);
Robert Phillips5dbcca52020-05-29 10:41:33 -0400328
Robert Phillips5dbcca52020-05-29 10:41:33 -0400329 tile->dropDDL();
Robert Phillips7b0ed552020-02-20 12:45:19 -0500330}
331
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500332// We expect to have more than one recording thread but just one gpu thread
333void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
334 SkTaskGroup* gpuTaskGroup,
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400335 GrDirectContext* direct) {
336 SkASSERT(recordingTaskGroup && gpuTaskGroup && direct);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500337
338 for (int i = 0; i < this->numTiles(); ++i) {
339 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500340 if (!tile->initialized()) {
341 continue;
342 }
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500343
344 // On a recording thread:
345 // generate the tile's DDL
346 // schedule gpu-thread processing of the DDL
347 // Note: a finer grained approach would be add a scheduling task which would evaluate
348 // which DDLs were ready to be rendered based on their prerequisites
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400349 recordingTaskGroup->add([tile, gpuTaskGroup, direct]() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500350 tile->createDDL();
351
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400352 gpuTaskGroup->add([direct, tile]() {
353 do_gpu_stuff(direct, tile);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500354 });
355 });
356 }
Robert Phillips11c67672020-04-23 15:10:03 -0400357
358 recordingTaskGroup->add([this] { this->createComposeDDL(); });
Robert Phillips96601082018-05-29 16:13:26 -0400359}
360
Robert Phillips5dbcca52020-05-29 10:41:33 -0400361// Only called from ViaDDL
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400362void DDLTileHelper::precompileAndDrawAllTiles(GrDirectContext* direct) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500363 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400364 fTiles[i].precompile(direct);
365 fTiles[i].draw(direct);
Robert Phillips96601082018-05-29 16:13:26 -0400366 }
Robert Phillips96601082018-05-29 16:13:26 -0400367}
368
Robert Phillips5dbcca52020-05-29 10:41:33 -0400369// Only called from skpbench
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400370void DDLTileHelper::interleaveDDLCreationAndDraw(GrDirectContext* direct) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000371 for (int i = 0; i < this->numTiles(); ++i) {
372 fTiles[i].createDDL();
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400373 fTiles[i].draw(direct);
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000374 }
375}
376
Robert Phillips5dbcca52020-05-29 10:41:33 -0400377// Only called from skpbench
Adlai Hollerb2705682020-10-20 10:11:53 -0400378void DDLTileHelper::drawAllTilesDirectly(GrDirectContext* context) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000379 for (int i = 0; i < this->numTiles(); ++i) {
380 fTiles[i].drawSKPDirectly(context);
381 }
382}
383
Robert Phillips11c67672020-04-23 15:10:03 -0400384void DDLTileHelper::dropCallbackContexts() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500385 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips11c67672020-04-23 15:10:03 -0400386 fTiles[i].dropCallbackContext();
Robert Phillips96601082018-05-29 16:13:26 -0400387 }
388}
389
390void DDLTileHelper::resetAllTiles() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500391 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400392 fTiles[i].reset();
393 }
Robert Phillips11c67672020-04-23 15:10:03 -0400394 fComposeDDL.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400395}
Robert Phillips8472a3d2020-04-16 16:27:45 -0400396
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400397void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400398
399 if (taskGroup) {
400 for (int i = 0; i < this->numTiles(); ++i) {
401 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500402 if (!tile->initialized()) {
403 continue;
404 }
Robert Phillips8472a3d2020-04-16 16:27:45 -0400405
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400406 taskGroup->add([direct, tile]() { TileData::CreateBackendTexture(direct, tile); });
Robert Phillips8472a3d2020-04-16 16:27:45 -0400407 }
408 } else {
409 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400410 TileData::CreateBackendTexture(direct, &fTiles[i]);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400411 }
412 }
413}
414
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400415void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400416 if (taskGroup) {
417 for (int i = 0; i < this->numTiles(); ++i) {
418 TileData* tile = &fTiles[i];
419
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400420 taskGroup->add([direct, tile]() { TileData::DeleteBackendTexture(direct, tile); });
Robert Phillips8472a3d2020-04-16 16:27:45 -0400421 }
422 } else {
423 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400424 TileData::DeleteBackendTexture(direct, &fTiles[i]);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400425 }
426 }
427}