blob: f31b81202393ea9c4dab741cc53a2d598b524b4b [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPicture.h"
12#include "include/core/SkSurface.h"
Robert Phillipsd5f3c982020-07-07 13:18:47 -040013#include "include/gpu/GrDirectContext.h"
Kevin Lubick77472bf2023-03-24 07:11:17 -040014#include "include/gpu/ganesh/SkImageGanesh.h"
Kevin Lubick5c93acf2023-05-09 12:11:43 -040015#include "include/gpu/ganesh/SkSurfaceGanesh.h"
Kevin Lubick0bff57e2023-06-09 14:29:17 -040016#include "include/private/chromium/GrDeferredDisplayList.h"
17#include "include/private/chromium/GrDeferredDisplayListRecorder.h"
18#include "include/private/chromium/GrSurfaceCharacterization.h"
Kevin Lubickeb981252023-06-13 07:51:26 -040019#include "include/private/chromium/SkImageChromium.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050020#include "src/base/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkTaskGroup.h"
Kevin Lubick502553f2022-11-28 12:42:38 -050022#include "src/gpu/ganesh/GrCaps.h"
Kevin Lubick0bff57e2023-06-09 14:29:17 -040023#include "src/gpu/ganesh/GrDeferredDisplayListPriv.h"
Greg Daniel719239c2022-04-07 11:20:24 -040024#include "src/gpu/ganesh/GrDirectContextPriv.h"
Kevin Lubickbf174bc2023-03-27 11:24:20 -040025#include "src/gpu/ganesh/image/SkImage_Ganesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "tools/DDLPromiseImageHelper.h"
Robert Phillips96601082018-05-29 16:13:26 -040027
Robert Phillips19f466d2020-02-26 10:27:07 -050028void DDLTileHelper::TileData::init(int id,
Robert Phillipsd5f3c982020-07-07 13:18:47 -040029 GrDirectContext* direct,
Kevin Lubick0bff57e2023-06-09 14:29:17 -040030 const GrSurfaceCharacterization& dstSurfaceCharacterization,
Robert Phillips0c088492020-11-10 08:30:50 -050031 const SkIRect& clip,
32 const SkIRect& paddingOutsets) {
Robert Phillipsa865a3a2020-02-14 10:49:39 -050033 fID = id;
Robert Phillipsa865a3a2020-02-14 10:49:39 -050034 fClip = clip;
Robert Phillips0c088492020-11-10 08:30:50 -050035 fPaddingOutsets = paddingOutsets;
Robert Phillipsa865a3a2020-02-14 10:49:39 -050036
Robert Phillipsbc688322020-12-14 11:38:59 -050037 fPlaybackChar = dstSurfaceCharacterization.createResized(this->paddedRectSize().width(),
38 this->paddedRectSize().height());
39 SkASSERT(fPlaybackChar.isValid());
Robert Phillips11c67672020-04-23 15:10:03 -040040
Robert Phillipsd5f3c982020-07-07 13:18:47 -040041 SkDEBUGCODE(const GrCaps* caps = direct->priv().caps());
Greg Daniel0e9d34d2021-08-13 16:20:18 -040042 SkASSERT(caps->isFormatTexturable(fPlaybackChar.backendFormat(),
43 fPlaybackChar.backendFormat().textureType()));
Robert Phillips11c67672020-04-23 15:10:03 -040044
Greg Daniel0e9d34d2021-08-13 16:20:18 -040045 fCallbackContext.reset(new PromiseImageCallbackContext(direct, fPlaybackChar.backendFormat()));
Robert Phillips96601082018-05-29 16:13:26 -040046}
47
Robert Phillipscd1e3972021-02-22 17:24:22 -050048DDLTileHelper::TileData::TileData() {}
Robert Phillipsa865a3a2020-02-14 10:49:39 -050049DDLTileHelper::TileData::~TileData() {}
50
Adlai Holler55aaefe2021-03-03 16:12:56 -070051void DDLTileHelper::TileData::createDDL(const SkPicture* picture) {
52 SkASSERT(!fDisplayList && picture);
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
Kevin Lubick0bff57e2023-06-09 14:29:17 -040057 GrDeferredDisplayListRecorder recorder(recordingChar);
Robert Phillips7a3197b2018-09-26 21:18:23 +000058
Adlai Holler55aaefe2021-03-03 16:12:56 -070059 // DDL TODO: the DDLRecorder's rContext isn't initialized until getCanvas is called.
Robert Phillips7a3197b2018-09-26 21:18:23 +000060 // Maybe set it up in the ctor?
Robert Phillips24a8e9e2020-03-06 20:26:28 +000061 SkCanvas* recordingCanvas = recorder.getCanvas();
Robert Phillips7a3197b2018-09-26 21:18:23 +000062
Robert Phillips0c088492020-11-10 08:30:50 -050063 // We always record the DDL in the (0,0) .. (clipWidth, clipHeight) coordinates
Robert Phillips24a8e9e2020-03-06 20:26:28 +000064 recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
65 recordingCanvas->translate(-fClip.fLeft, -fClip.fTop);
Robert Phillips96601082018-05-29 16:13:26 -040066
67 // Note: in this use case we only render a picture to the deferred canvas
68 // but, more generally, clients will use arbitrary draw calls.
Adlai Holler55aaefe2021-03-03 16:12:56 -070069 recordingCanvas->drawPicture(picture);
Robert Phillips96601082018-05-29 16:13:26 -040070
Robert Phillips7a3197b2018-09-26 21:18:23 +000071 fDisplayList = recorder.detach();
Robert Phillips96601082018-05-29 16:13:26 -040072}
73
Robert Phillips11c67672020-04-23 15:10:03 -040074void DDLTileHelper::createComposeDDL() {
75 SkASSERT(!fComposeDDL);
76
Kevin Lubick0bff57e2023-06-09 14:29:17 -040077 GrDeferredDisplayListRecorder recorder(fDstCharacterization);
Robert Phillips11c67672020-04-23 15:10:03 -040078
79 SkCanvas* recordingCanvas = recorder.getCanvas();
80
81 for (int i = 0; i < this->numTiles(); ++i) {
82 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -050083 if (!tile->initialized()) {
84 continue;
85 }
Robert Phillips11c67672020-04-23 15:10:03 -040086
Adlai Holler55aaefe2021-03-03 16:12:56 -070087 sk_sp<SkImage> promiseImage = tile->makePromiseImageForDst(
88 recordingCanvas->recordingContext()->threadSafeProxy());
Robert Phillips11c67672020-04-23 15:10:03 -040089
Robert Phillips0c088492020-11-10 08:30:50 -050090 SkRect dstRect = SkRect::Make(tile->clipRect());
91 SkIRect srcRect = tile->clipRect();
92 srcRect.offsetTo(tile->padOffset().x(), tile->padOffset().y());
Robert Phillips11c67672020-04-23 15:10:03 -040093
Robert Phillips0c088492020-11-10 08:30:50 -050094 SkASSERT(promiseImage->bounds().contains(srcRect));
Robert Phillips11c67672020-04-23 15:10:03 -040095
Mike Reede02d7f82021-01-21 22:25:21 -050096 recordingCanvas->drawImageRect(promiseImage.get(), SkRect::Make(srcRect), dstRect,
97 SkSamplingOptions(), nullptr,
98 SkCanvas::kStrict_SrcRectConstraint);
Robert Phillips11c67672020-04-23 15:10:03 -040099 }
100
101 fComposeDDL = recorder.detach();
Robert Phillips889d6132020-06-16 11:11:33 -0400102 SkASSERT(fComposeDDL);
Robert Phillips11c67672020-04-23 15:10:03 -0400103}
104
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400105void DDLTileHelper::TileData::precompile(GrDirectContext* direct) {
Robert Phillips559f9c12021-01-11 12:29:20 -0500106 if (!this->initialized()) {
107 return;
108 }
109
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500110 SkASSERT(fDisplayList);
111
Kevin Lubick0bff57e2023-06-09 14:29:17 -0400112 GrDeferredDisplayList::ProgramIterator iter(direct, fDisplayList.get());
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500113 for (; !iter.done(); iter.next()) {
114 iter.compile();
115 }
116}
117
Adlai Holler55aaefe2021-03-03 16:12:56 -0700118sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext* rContext) {
Robert Phillips11c67672020-04-23 15:10:03 -0400119 SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture());
120
121 auto promiseImageTexture = fCallbackContext->promiseImageTexture();
122 if (!promiseImageTexture->backendTexture().isValid()) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400123 return nullptr;
124 }
Robert Phillips7ae9d2f2020-04-15 10:58:15 -0400125
Kevin Lubickeb981252023-06-13 07:51:26 -0400126 // Here we are, unfortunately, aliasing the backend texture held by the GrPromiseImageTexture.
Robert Phillips11c67672020-04-23 15:10:03 -0400127 // Both the tile's destination surface and the promise image used to draw the tile will be
128 // backed by the same backendTexture - unbeknownst to Ganesh.
Kevin Lubick5c93acf2023-05-09 12:11:43 -0400129 return SkSurfaces::WrapBackendTexture(rContext,
130 promiseImageTexture->backendTexture(),
131 fPlaybackChar.origin(),
132 fPlaybackChar.sampleCount(),
133 fPlaybackChar.colorType(),
134 fPlaybackChar.refColorSpace(),
135 &fPlaybackChar.surfaceProps());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400136}
137
Adlai Holler55aaefe2021-03-03 16:12:56 -0700138void DDLTileHelper::TileData::drawSKPDirectly(GrDirectContext* dContext,
139 const SkPicture* picture) {
140 SkASSERT(!fDisplayList && !fTileSurface && picture);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400141
Adlai Holler55aaefe2021-03-03 16:12:56 -0700142 fTileSurface = this->makeWrappedTileDest(dContext);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400143 if (fTileSurface) {
144 SkCanvas* tileCanvas = fTileSurface->getCanvas();
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000145
Robert Phillips0c088492020-11-10 08:30:50 -0500146 SkASSERT(this->padOffset().isZero() && this->paddedRectSize() == fClip.size());
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000147 tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
148 tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
149
Adlai Holler55aaefe2021-03-03 16:12:56 -0700150 tileCanvas->drawPicture(picture);
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000151
Robert Phillips8472a3d2020-04-16 16:27:45 -0400152 // We can't snap an image here bc, since we're using wrapped backend textures for the
153 // surfaces, that would incur a copy.
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000154 }
155}
156
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400157void DDLTileHelper::TileData::draw(GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400158 SkASSERT(fDisplayList && !fTileSurface);
Robert Phillips96601082018-05-29 16:13:26 -0400159
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400160 fTileSurface = this->makeWrappedTileDest(direct);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400161 if (fTileSurface) {
Kevin Lubick0bff57e2023-06-09 14:29:17 -0400162 skgpu::ganesh::DrawDDL(fTileSurface, fDisplayList);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500163
Robert Phillips8472a3d2020-04-16 16:27:45 -0400164 // We can't snap an image here bc, since we're using wrapped backend textures for the
165 // surfaces, that would incur a copy.
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500166 }
Robert Phillips96601082018-05-29 16:13:26 -0400167}
168
Robert Phillips96601082018-05-29 16:13:26 -0400169void DDLTileHelper::TileData::reset() {
170 // TODO: when DDLs are re-renderable we don't need to do this
171 fDisplayList = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400172
Robert Phillips8472a3d2020-04-16 16:27:45 -0400173 fTileSurface = nullptr;
174}
175
Robert Phillips0c088492020-11-10 08:30:50 -0500176sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImageForDst(
Adlai Holler55aaefe2021-03-03 16:12:56 -0700177 sk_sp<GrContextThreadSafeProxy> threadSafeProxy) {
Robert Phillips11c67672020-04-23 15:10:03 -0400178 SkASSERT(fCallbackContext);
179
180 // The promise image gets a ref on the promise callback context
Brian Salomonf1432742020-11-09 15:40:27 -0500181 sk_sp<SkImage> promiseImage =
Kevin Lubick77472bf2023-03-24 07:11:17 -0400182 SkImages::PromiseTextureFrom(std::move(threadSafeProxy),
183 fCallbackContext->backendFormat(),
184 this->paddedRectSize(),
Kevin Lubickdf73d162023-09-11 11:56:53 -0400185 skgpu::Mipmapped::kNo,
Kevin Lubick77472bf2023-03-24 07:11:17 -0400186 GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin,
187 fPlaybackChar.colorType(),
188 kPremul_SkAlphaType,
189 fPlaybackChar.refColorSpace(),
190 PromiseImageCallbackContext::PromiseImageFulfillProc,
191 PromiseImageCallbackContext::PromiseImageReleaseProc,
192 (void*)this->refCallbackContext().release());
Robert Phillips11c67672020-04-23 15:10:03 -0400193 fCallbackContext->wasAddedToImage();
194
195 return promiseImage;
196}
197
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400198void DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext* direct, TileData* tile) {
Robert Phillips11c67672020-04-23 15:10:03 -0400199 SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture());
Robert Phillipsbc688322020-12-14 11:38:59 -0500200
Kevin Lubick0bff57e2023-06-09 14:29:17 -0400201 const GrSurfaceCharacterization& c = tile->fPlaybackChar;
Aditya Kushwah8acbc3c2022-08-24 09:31:11 -0700202 GrBackendTexture beTex =
203 direct->createBackendTexture(c.width(),
204 c.height(),
205 c.colorType(),
Kevin Lubickdf73d162023-09-11 11:56:53 -0400206 skgpu::Mipmapped(c.isMipMapped()),
Aditya Kushwah8acbc3c2022-08-24 09:31:11 -0700207 GrRenderable::kYes,
208 GrProtected::kNo,
209 /*label=*/"DDLTile_TileData_CreateBackendTexture");
Robert Phillips11c67672020-04-23 15:10:03 -0400210 tile->fCallbackContext->setBackendTexture(beTex);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400211}
212
Robert Phillips27613ee2023-02-08 14:02:20 -0500213void DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext* dContext, TileData* tile) {
214 if (!tile->initialized() || dContext->abandoned()) {
Robert Phillips559f9c12021-01-11 12:29:20 -0500215 return;
216 }
217
Robert Phillips11c67672020-04-23 15:10:03 -0400218 SkASSERT(tile->fCallbackContext);
219
Robert Phillips8472a3d2020-04-16 16:27:45 -0400220 // TODO: it seems that, on the Linux bots, backend texture creation is failing
221 // a lot (skbug.com/10142)
Robert Phillips11c67672020-04-23 15:10:03 -0400222 SkASSERT(!tile->fCallbackContext->promiseImageTexture() ||
223 tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid());
Robert Phillips8472a3d2020-04-16 16:27:45 -0400224
225 tile->fTileSurface = nullptr;
Robert Phillips11c67672020-04-23 15:10:03 -0400226
227 SkASSERT(tile->fCallbackContext->unique());
228 tile->fCallbackContext.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400229}
230
231///////////////////////////////////////////////////////////////////////////////////////////////////
232
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400233DDLTileHelper::DDLTileHelper(GrDirectContext* direct,
Kevin Lubick0bff57e2023-06-09 14:29:17 -0400234 const GrSurfaceCharacterization& dstChar,
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500235 const SkIRect& viewport,
Robert Phillips96f6d9a2021-02-26 10:41:06 -0500236 int numXDivisions, int numYDivisions,
Robert Phillips0c088492020-11-10 08:30:50 -0500237 bool addRandomPaddingToDst)
Robert Phillips96f6d9a2021-02-26 10:41:06 -0500238 : fNumXDivisions(numXDivisions)
239 , fNumYDivisions(numYDivisions)
240 , fTiles(numXDivisions * numYDivisions)
Robert Phillips11c67672020-04-23 15:10:03 -0400241 , fDstCharacterization(dstChar) {
Robert Phillips96f6d9a2021-02-26 10:41:06 -0500242 SkASSERT(fNumXDivisions > 0 && fNumYDivisions > 0);
Robert Phillips96601082018-05-29 16:13:26 -0400243
Robert Phillips96f6d9a2021-02-26 10:41:06 -0500244 int xTileSize = viewport.width()/fNumXDivisions;
245 int yTileSize = viewport.height()/fNumYDivisions;
Robert Phillips96601082018-05-29 16:13:26 -0400246
Robert Phillips0c088492020-11-10 08:30:50 -0500247 SkRandom rand;
248
Robert Phillips96601082018-05-29 16:13:26 -0400249 // Create the destination tiles
Robert Phillips96f6d9a2021-02-26 10:41:06 -0500250 for (int y = 0, yOff = 0; y < fNumYDivisions; ++y, yOff += yTileSize) {
251 int ySize = (y < fNumYDivisions-1) ? yTileSize : viewport.height()-yOff;
Robert Phillips96601082018-05-29 16:13:26 -0400252
Robert Phillips96f6d9a2021-02-26 10:41:06 -0500253 for (int x = 0, xOff = 0; x < fNumXDivisions; ++x, xOff += xTileSize) {
254 int xSize = (x < fNumXDivisions-1) ? xTileSize : viewport.width()-xOff;
Robert Phillips96601082018-05-29 16:13:26 -0400255
256 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
257
258 SkASSERT(viewport.contains(clip));
259
Robert Phillips0c088492020-11-10 08:30:50 -0500260 static const uint32_t kMaxPad = 64;
261 int32_t lPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
262 int32_t tPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
263 int32_t rPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
264 int32_t bPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
265
Robert Phillips96f6d9a2021-02-26 10:41:06 -0500266 fTiles[y*fNumXDivisions+x].init(y*fNumXDivisions+x, direct, dstChar, clip,
Robert Phillips0c088492020-11-10 08:30:50 -0500267 {lPad, tPad, rPad, bPad});
Robert Phillips96601082018-05-29 16:13:26 -0400268 }
269 }
270}
271
Robert Phillips0d8722c2021-03-29 13:29:40 -0400272void DDLTileHelper::createDDLsInParallel(SkPicture* picture) {
Robert Phillips96601082018-05-29 16:13:26 -0400273#if 1
Adlai Holler55aaefe2021-03-03 16:12:56 -0700274 SkTaskGroup().batch(this->numTiles(), [&](int i) {
Robert Phillips0d8722c2021-03-29 13:29:40 -0400275 fTiles[i].createDDL(picture);
Adlai Holler55aaefe2021-03-03 16:12:56 -0700276 });
Robert Phillips11c67672020-04-23 15:10:03 -0400277 SkTaskGroup().add([this]{ this->createComposeDDL(); });
Robert Phillipsf18c7562018-06-13 09:01:36 -0400278 SkTaskGroup().wait();
Robert Phillips96601082018-05-29 16:13:26 -0400279#else
280 // Use this code path to debug w/o threads
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000281 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips0d8722c2021-03-29 13:29:40 -0400282 fTiles[i].createDDL(picture);
Robert Phillips96601082018-05-29 16:13:26 -0400283 }
Robert Phillips11c67672020-04-23 15:10:03 -0400284 this->createComposeDDL();
Robert Phillips96601082018-05-29 16:13:26 -0400285#endif
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500286}
Robert Phillips96601082018-05-29 16:13:26 -0400287
Robert Phillips7b0ed552020-02-20 12:45:19 -0500288// On the gpu thread:
289// precompile any programs
290// replay the DDL into a surface to make the tile image
291// compose the tile image into the main canvas
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400292static void do_gpu_stuff(GrDirectContext* direct, DDLTileHelper::TileData* tile) {
Robert Phillips7b0ed552020-02-20 12:45:19 -0500293
294 // TODO: schedule program compilation as their own tasks
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400295 tile->precompile(direct);
Robert Phillips7b0ed552020-02-20 12:45:19 -0500296
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400297 tile->draw(direct);
Robert Phillips5dbcca52020-05-29 10:41:33 -0400298
Robert Phillips5dbcca52020-05-29 10:41:33 -0400299 tile->dropDDL();
Robert Phillips7b0ed552020-02-20 12:45:19 -0500300}
301
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500302// We expect to have more than one recording thread but just one gpu thread
303void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
304 SkTaskGroup* gpuTaskGroup,
Robert Phillips0d8722c2021-03-29 13:29:40 -0400305 GrDirectContext* dContext,
306 SkPicture* picture) {
Robert Phillipscd1e3972021-02-22 17:24:22 -0500307 SkASSERT(recordingTaskGroup && gpuTaskGroup && dContext);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500308
309 for (int i = 0; i < this->numTiles(); ++i) {
310 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500311 if (!tile->initialized()) {
312 continue;
313 }
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500314
315 // On a recording thread:
316 // generate the tile's DDL
317 // schedule gpu-thread processing of the DDL
318 // Note: a finer grained approach would be add a scheduling task which would evaluate
319 // which DDLs were ready to be rendered based on their prerequisites
Robert Phillips0d8722c2021-03-29 13:29:40 -0400320 recordingTaskGroup->add([tile, gpuTaskGroup, dContext, picture]() {
321 tile->createDDL(picture);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500322
Robert Phillipscd1e3972021-02-22 17:24:22 -0500323 gpuTaskGroup->add([dContext, tile]() {
324 do_gpu_stuff(dContext, tile);
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500325 });
326 });
327 }
Robert Phillips11c67672020-04-23 15:10:03 -0400328
329 recordingTaskGroup->add([this] { this->createComposeDDL(); });
Robert Phillips96601082018-05-29 16:13:26 -0400330}
331
Robert Phillips5dbcca52020-05-29 10:41:33 -0400332// Only called from skpbench
Robert Phillips0d8722c2021-03-29 13:29:40 -0400333void DDLTileHelper::interleaveDDLCreationAndDraw(GrDirectContext* dContext, SkPicture* picture) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000334 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips0d8722c2021-03-29 13:29:40 -0400335 fTiles[i].createDDL(picture);
336 fTiles[i].draw(dContext);
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000337 }
338}
339
Robert Phillips5dbcca52020-05-29 10:41:33 -0400340// Only called from skpbench
Robert Phillips0d8722c2021-03-29 13:29:40 -0400341void DDLTileHelper::drawAllTilesDirectly(GrDirectContext* dContext, SkPicture* picture) {
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000342 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips0d8722c2021-03-29 13:29:40 -0400343 fTiles[i].drawSKPDirectly(dContext, picture);
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000344 }
345}
346
Robert Phillips11c67672020-04-23 15:10:03 -0400347void DDLTileHelper::dropCallbackContexts() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500348 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips11c67672020-04-23 15:10:03 -0400349 fTiles[i].dropCallbackContext();
Robert Phillips96601082018-05-29 16:13:26 -0400350 }
351}
352
353void DDLTileHelper::resetAllTiles() {
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500354 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -0400355 fTiles[i].reset();
356 }
Robert Phillips11c67672020-04-23 15:10:03 -0400357 fComposeDDL.reset();
Robert Phillips96601082018-05-29 16:13:26 -0400358}
Robert Phillips8472a3d2020-04-16 16:27:45 -0400359
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400360void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400361
362 if (taskGroup) {
363 for (int i = 0; i < this->numTiles(); ++i) {
364 TileData* tile = &fTiles[i];
Robert Phillips559f9c12021-01-11 12:29:20 -0500365 if (!tile->initialized()) {
366 continue;
367 }
Robert Phillips8472a3d2020-04-16 16:27:45 -0400368
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400369 taskGroup->add([direct, tile]() { TileData::CreateBackendTexture(direct, tile); });
Robert Phillips8472a3d2020-04-16 16:27:45 -0400370 }
371 } else {
372 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400373 TileData::CreateBackendTexture(direct, &fTiles[i]);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400374 }
375 }
376}
377
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400378void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips8472a3d2020-04-16 16:27:45 -0400379 if (taskGroup) {
380 for (int i = 0; i < this->numTiles(); ++i) {
381 TileData* tile = &fTiles[i];
382
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400383 taskGroup->add([direct, tile]() { TileData::DeleteBackendTexture(direct, tile); });
Robert Phillips8472a3d2020-04-16 16:27:45 -0400384 }
385 } else {
386 for (int i = 0; i < this->numTiles(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400387 TileData::DeleteBackendTexture(direct, &fTiles[i]);
Robert Phillips8472a3d2020-04-16 16:27:45 -0400388 }
389 }
390}