blob: f62513b2b6281b3c5ecc0dd18cf684f8efbc9a7b [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
Mike Reede02d7f82021-01-21 22:25:21 -0500130 recordingCanvas->drawImageRect(promiseImage.get(), SkRect::Make(srcRect), dstRect,
131 SkSamplingOptions(), nullptr,
132 SkCanvas::kStrict_SrcRectConstraint);
Robert Phillips11c67672020-04-23 15:10:03 -0400133 }
134
135 fComposeDDL = recorder.detach();
Robert Phillips889d6132020-06-16 11:11:33 -0400136 SkASSERT(fComposeDDL);
Robert Phillips11c67672020-04-23 15:10:03 -0400137}
138
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400139void DDLTileHelper::TileData::precompile(GrDirectContext* direct) {
Robert Phillips559f9c12021-01-11 12:29:20 -0500140 if (!this->initialized()) {
141 return;
142 }
143
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500144 SkASSERT(fDisplayList);
145
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400146 SkDeferredDisplayList::ProgramIterator iter(direct, fDisplayList.get());
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500147 for (; !iter.done(); iter.next()) {
148 iter.compile();
149 }
150}
151
Adlai Hollerb2705682020-10-20 10:11:53 -0400152sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext* context) {
Robert Phillips11c67672020-04-23 15:10:03 -0400153 SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture());
154
155 auto promiseImageTexture = fCallbackContext->promiseImageTexture();
156 if (!promiseImageTexture->backendTexture().isValid()) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400157 return nullptr;
158 }
Robert Phillips7ae9d2f2020-04-15 10:58:15 -0400159
Robert Phillips11c67672020-04-23 15:10:03 -0400160 // Here we are, unfortunately, aliasing the backend texture held by the SkPromiseImageTexture.
161 // Both the tile's destination surface and the promise image used to draw the tile will be
162 // backed by the same backendTexture - unbeknownst to Ganesh.
Robert Phillips8472a3d2020-04-16 16:27:45 -0400163 return SkSurface::MakeFromBackendTexture(context,
Robert Phillips11c67672020-04-23 15:10:03 -0400164 promiseImageTexture->backendTexture(),
Robert Phillipsbc688322020-12-14 11:38:59 -0500165 fPlaybackChar.origin(),
166 fPlaybackChar.sampleCount(),
167 fPlaybackChar.colorType(),
168 fPlaybackChar.refColorSpace(),
169 &fPlaybackChar.surfaceProps());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400170}
171
Adlai Hollerb2705682020-10-20 10:11:53 -0400172void DDLTileHelper::TileData::drawSKPDirectly(GrRecordingContext* context) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400173 SkASSERT(!fDisplayList && !fTileSurface && fReconstitutedPicture);
174
175 fTileSurface = this->makeWrappedTileDest(context);
176 if (fTileSurface) {
177 SkCanvas* tileCanvas = fTileSurface->getCanvas();
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000178
Robert Phillips0c088492020-11-10 08:30:50 -0500179 SkASSERT(this->padOffset().isZero() && this->paddedRectSize() == fClip.size());
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000180 tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
181 tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
182
183 tileCanvas->drawPicture(fReconstitutedPicture);
184
Robert Phillips8472a3d2020-04-16 16:27:45 -0400185 // We can't snap an image here bc, since we're using wrapped backend textures for the
186 // surfaces, that would incur a copy.
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000187 }
188}
189
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400190void DDLTileHelper::TileData::draw(GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400191 SkASSERT(fDisplayList && !fTileSurface);
Robert Phillips96601082018-05-29 16:13:26 -0400192
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400193 fTileSurface = this->makeWrappedTileDest(direct);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400194 if (fTileSurface) {
Robert Phillips0c088492020-11-10 08:30:50 -0500195 fTileSurface->draw(fDisplayList, this->padOffset().x(), this->padOffset().y());
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500196
Robert Phillips8472a3d2020-04-16 16:27:45 -0400197 // We can't snap an image here bc, since we're using wrapped backend textures for the
198 // surfaces, that would incur a copy.
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500199 }
Robert Phillips96601082018-05-29 16:13:26 -0400200}
201
Robert Phillips96601082018-05-29 16:13:26 -0400202void DDLTileHelper::TileData::reset() {
203 // TODO: when DDLs are re-renderable we don't need to do this
204 fDisplayList = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400205
Robert Phillips8472a3d2020-04-16 16:27:45 -0400206 fTileSurface = nullptr;
207}
208
Robert Phillips0c088492020-11-10 08:30:50 -0500209sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImageForDst(
210 SkDeferredDisplayListRecorder* recorder) {
Robert Phillips11c67672020-04-23 15:10:03 -0400211 SkASSERT(fCallbackContext);
212
213 // The promise image gets a ref on the promise callback context
Brian Salomonf1432742020-11-09 15:40:27 -0500214 sk_sp<SkImage> promiseImage =
215 recorder->makePromiseTexture(fCallbackContext->backendFormat(),
Robert Phillips0c088492020-11-10 08:30:50 -0500216 this->paddedRectSize().width(),
217 this->paddedRectSize().height(),
Brian Salomonf1432742020-11-09 15:40:27 -0500218 GrMipmapped::kNo,
219 GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin,
Robert Phillipsbc688322020-12-14 11:38:59 -0500220 fPlaybackChar.colorType(),
Brian Salomonf1432742020-11-09 15:40:27 -0500221 kPremul_SkAlphaType,
Robert Phillipsbc688322020-12-14 11:38:59 -0500222 fPlaybackChar.refColorSpace(),
Brian Salomonf1432742020-11-09 15:40:27 -0500223 PromiseImageCallbackContext::PromiseImageFulfillProc,
224 PromiseImageCallbackContext::PromiseImageReleaseProc,
225 (void*)this->refCallbackContext().release());
Robert Phillips11c67672020-04-23 15:10:03 -0400226 fCallbackContext->wasAddedToImage();
227
228 return promiseImage;
229}
230
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400231void DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext* direct, TileData* tile) {
Robert Phillips11c67672020-04-23 15:10:03 -0400232 SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture());
Robert Phillipsbc688322020-12-14 11:38:59 -0500233
234 const SkSurfaceCharacterization& c = tile->fPlaybackChar;
Greg Daniel398ecf12020-08-26 16:33:54 -0400235 GrBackendTexture beTex = direct->createBackendTexture(c.width(), c.height(), c.colorType(),
236 GrMipMapped(c.isMipMapped()),
237 GrRenderable::kYes);
Robert Phillips11c67672020-04-23 15:10:03 -0400238 tile->fCallbackContext->setBackendTexture(beTex);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400239}
240
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400241void DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext*, TileData* tile) {
Robert Phillips559f9c12021-01-11 12:29:20 -0500242 if (!tile->initialized()) {
243 return;
244 }
245
Robert Phillips11c67672020-04-23 15:10:03 -0400246 SkASSERT(tile->fCallbackContext);
247
Robert Phillips8472a3d2020-04-16 16:27:45 -0400248 // TODO: it seems that, on the Linux bots, backend texture creation is failing
249 // a lot (skbug.com/10142)
Robert Phillips11c67672020-04-23 15:10:03 -0400250 SkASSERT(!tile->fCallbackContext->promiseImageTexture() ||
251 tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400252
253 tile->fTileSurface = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400254
255 SkASSERT(tile->fCallbackContext->unique());
256 tile->fCallbackContext.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400257}
258
259///////////////////////////////////////////////////////////////////////////////////////////////////
260
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400261DDLTileHelper::DDLTileHelper(GrDirectContext* direct,
Robert Phillips19f466d2020-02-26 10:27:07 -0500262 const SkSurfaceCharacterization& dstChar,
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500263 const SkIRect& viewport,
Robert Phillips0c088492020-11-10 08:30:50 -0500264 int numDivisions,
265 bool addRandomPaddingToDst)
Robert Phillips11c67672020-04-23 15:10:03 -0400266 : fNumDivisions(numDivisions)
Adlai Holler8e821232020-05-11 13:33:59 -0400267 , fTiles(numDivisions * numDivisions)
Robert Phillips11c67672020-04-23 15:10:03 -0400268 , fDstCharacterization(dstChar) {
Robert Phillips96601082018-05-29 16:13:26 -0400269 SkASSERT(fNumDivisions > 0);
Robert Phillips96601082018-05-29 16:13:26 -0400270
271 int xTileSize = viewport.width()/fNumDivisions;
272 int yTileSize = viewport.height()/fNumDivisions;
273
Robert Phillips0c088492020-11-10 08:30:50 -0500274 SkRandom rand;
275
Robert Phillips96601082018-05-29 16:13:26 -0400276 // Create the destination tiles
277 for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) {
278 int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff;
279
280 for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) {
281 int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff;
282
283 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
284
285 SkASSERT(viewport.contains(clip));
286
Robert Phillips0c088492020-11-10 08:30:50 -0500287 static const uint32_t kMaxPad = 64;
288 int32_t lPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
289 int32_t tPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
290 int32_t rPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
291 int32_t bPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
292
293 fTiles[y*fNumDivisions+x].init(y*fNumDivisions+x, direct, dstChar, clip,
294 {lPad, tPad, rPad, bPad});
Robert Phillips96601082018-05-29 16:13:26 -0400295 }
296 }
297}
298
299void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData,
300 const DDLPromiseImageHelper& helper) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500301 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400302 fTiles[i].createTileSpecificSKP(compressedPictureData, helper);
303 }
304}
305
306void DDLTileHelper::createDDLsInParallel() {
307#if 1
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500308 SkTaskGroup().batch(this->numTiles(), [&](int i) { fTiles[i].createDDL(); });
Robert Phillips11c67672020-04-23 15:10:03 -0400309 SkTaskGroup().add([this]{ this->createComposeDDL(); });
Robert Phillipsf18c7562018-06-13 09:01:36 -0400310 SkTaskGroup().wait();
Robert Phillips96601082018-05-29 16:13:26 -0400311#else
312 // Use this code path to debug w/o threads
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000313 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400314 fTiles[i].createDDL();
315 }
Robert Phillips11c67672020-04-23 15:10:03 -0400316 this->createComposeDDL();
Robert Phillips96601082018-05-29 16:13:26 -0400317#endif
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500318}
Robert Phillips96601082018-05-29 16:13:26 -0400319
Robert Phillips7b0ed552020-02-20 12:45:19 -0500320// On the gpu thread:
321// precompile any programs
322// replay the DDL into a surface to make the tile image
323// compose the tile image into the main canvas
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400324static void do_gpu_stuff(GrDirectContext* direct, DDLTileHelper::TileData* tile) {
Robert Phillips7b0ed552020-02-20 12:45:19 -0500325
326 // TODO: schedule program compilation as their own tasks
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400327 tile->precompile(direct);
Robert Phillips7b0ed552020-02-20 12:45:19 -0500328
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400329 tile->draw(direct);
Robert Phillips5dbcca52020-05-29 10:41:33 -0400330
Robert Phillips5dbcca52020-05-29 10:41:33 -0400331 tile->dropDDL();
Robert Phillips7b0ed552020-02-20 12:45:19 -0500332}
333
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500334// We expect to have more than one recording thread but just one gpu thread
335void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
336 SkTaskGroup* gpuTaskGroup,
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400337 GrDirectContext* direct) {
338 SkASSERT(recordingTaskGroup && gpuTaskGroup && direct);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500339
340 for (int i = 0; i < this->numTiles(); ++i) {
341 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500342 if (!tile->initialized()) {
343 continue;
344 }
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500345
346 // On a recording thread:
347 // generate the tile's DDL
348 // schedule gpu-thread processing of the DDL
349 // Note: a finer grained approach would be add a scheduling task which would evaluate
350 // which DDLs were ready to be rendered based on their prerequisites
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400351 recordingTaskGroup->add([tile, gpuTaskGroup, direct]() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500352 tile->createDDL();
353
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400354 gpuTaskGroup->add([direct, tile]() {
355 do_gpu_stuff(direct, tile);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500356 });
357 });
358 }
Robert Phillips11c67672020-04-23 15:10:03 -0400359
360 recordingTaskGroup->add([this] { this->createComposeDDL(); });
Robert Phillips96601082018-05-29 16:13:26 -0400361}
362
Robert Phillips5dbcca52020-05-29 10:41:33 -0400363// Only called from ViaDDL
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400364void DDLTileHelper::precompileAndDrawAllTiles(GrDirectContext* direct) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500365 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400366 fTiles[i].precompile(direct);
367 fTiles[i].draw(direct);
Robert Phillips96601082018-05-29 16:13:26 -0400368 }
Robert Phillips96601082018-05-29 16:13:26 -0400369}
370
Robert Phillips5dbcca52020-05-29 10:41:33 -0400371// Only called from skpbench
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400372void DDLTileHelper::interleaveDDLCreationAndDraw(GrDirectContext* direct) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000373 for (int i = 0; i < this->numTiles(); ++i) {
374 fTiles[i].createDDL();
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400375 fTiles[i].draw(direct);
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000376 }
377}
378
Robert Phillips5dbcca52020-05-29 10:41:33 -0400379// Only called from skpbench
Adlai Hollerb2705682020-10-20 10:11:53 -0400380void DDLTileHelper::drawAllTilesDirectly(GrDirectContext* context) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000381 for (int i = 0; i < this->numTiles(); ++i) {
382 fTiles[i].drawSKPDirectly(context);
383 }
384}
385
Robert Phillips11c67672020-04-23 15:10:03 -0400386void DDLTileHelper::dropCallbackContexts() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500387 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips11c67672020-04-23 15:10:03 -0400388 fTiles[i].dropCallbackContext();
Robert Phillips96601082018-05-29 16:13:26 -0400389 }
390}
391
392void DDLTileHelper::resetAllTiles() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500393 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400394 fTiles[i].reset();
395 }
Robert Phillips11c67672020-04-23 15:10:03 -0400396 fComposeDDL.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400397}
Robert Phillips8472a3d2020-04-16 16:27:45 -0400398
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400399void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400400
401 if (taskGroup) {
402 for (int i = 0; i < this->numTiles(); ++i) {
403 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500404 if (!tile->initialized()) {
405 continue;
406 }
Robert Phillips8472a3d2020-04-16 16:27:45 -0400407
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400408 taskGroup->add([direct, tile]() { TileData::CreateBackendTexture(direct, tile); });
Robert Phillips8472a3d2020-04-16 16:27:45 -0400409 }
410 } else {
411 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400412 TileData::CreateBackendTexture(direct, &fTiles[i]);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400413 }
414 }
415}
416
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400417void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400418 if (taskGroup) {
419 for (int i = 0; i < this->numTiles(); ++i) {
420 TileData* tile = &fTiles[i];
421
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400422 taskGroup->add([direct, tile]() { TileData::DeleteBackendTexture(direct, tile); });
Robert Phillips8472a3d2020-04-16 16:27:45 -0400423 }
424 } else {
425 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400426 TileData::DeleteBackendTexture(direct, &fTiles[i]);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400427 }
428 }
429}