Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
DDLTileHelper.cpp
Go to the documentation of this file.
1/*
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
9
20#include "src/base/SkRandom.h"
27
29 GrDirectContext* direct,
30 const GrSurfaceCharacterization& dstSurfaceCharacterization,
31 const SkIRect& clip,
32 const SkIRect& paddingOutsets) {
33 fID = id;
34 fClip = clip;
35 fPaddingOutsets = paddingOutsets;
36
37 fPlaybackChar = dstSurfaceCharacterization.createResized(this->paddedRectSize().width(),
38 this->paddedRectSize().height());
39 SkASSERT(fPlaybackChar.isValid());
40
41 SkDEBUGCODE(const GrCaps* caps = direct->priv().caps());
42 SkASSERT(caps->isFormatTexturable(fPlaybackChar.backendFormat(),
43 fPlaybackChar.backendFormat().textureType()));
44
45 fCallbackContext.reset(new PromiseImageCallbackContext(direct, fPlaybackChar.backendFormat()));
46}
47
50
52 SkASSERT(!fDisplayList && picture);
53
54 auto recordingChar = fPlaybackChar.createResized(fClip.width(), fClip.height());
55 SkASSERT(recordingChar.isValid());
56
57 GrDeferredDisplayListRecorder recorder(recordingChar);
58
59 // DDL TODO: the DDLRecorder's rContext isn't initialized until getCanvas is called.
60 // Maybe set it up in the ctor?
61 SkCanvas* recordingCanvas = recorder.getCanvas();
62
63 // We always record the DDL in the (0,0) .. (clipWidth, clipHeight) coordinates
64 recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
65 recordingCanvas->translate(-fClip.fLeft, -fClip.fTop);
66
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.
69 recordingCanvas->drawPicture(picture);
70
71 fDisplayList = recorder.detach();
72}
73
75 SkASSERT(!fComposeDDL);
76
77 GrDeferredDisplayListRecorder recorder(fDstCharacterization);
78
79 SkCanvas* recordingCanvas = recorder.getCanvas();
80
81 for (int i = 0; i < this->numTiles(); ++i) {
82 TileData* tile = &fTiles[i];
83 if (!tile->initialized()) {
84 continue;
85 }
86
87 sk_sp<SkImage> promiseImage = tile->makePromiseImageForDst(
88 recordingCanvas->recordingContext()->threadSafeProxy());
89
90 SkRect dstRect = SkRect::Make(tile->clipRect());
91 SkIRect srcRect = tile->clipRect();
92 srcRect.offsetTo(tile->padOffset().x(), tile->padOffset().y());
93
94 SkASSERT(promiseImage->bounds().contains(srcRect));
95
96 recordingCanvas->drawImageRect(promiseImage.get(), SkRect::Make(srcRect), dstRect,
97 SkSamplingOptions(), nullptr,
99 }
100
101 fComposeDDL = recorder.detach();
102 SkASSERT(fComposeDDL);
103}
104
106 if (!this->initialized()) {
107 return;
108 }
109
110 SkASSERT(fDisplayList);
111
112 GrDeferredDisplayList::ProgramIterator iter(direct, fDisplayList.get());
113 for (; !iter.done(); iter.next()) {
114 iter.compile();
115 }
116}
117
118sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext* rContext) {
119 SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture());
120
121 auto promiseImageTexture = fCallbackContext->promiseImageTexture();
122 if (!promiseImageTexture->backendTexture().isValid()) {
123 return nullptr;
124 }
125
126 // Here we are, unfortunately, aliasing the backend texture held by the GrPromiseImageTexture.
127 // 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.
129 return SkSurfaces::WrapBackendTexture(rContext,
130 promiseImageTexture->backendTexture(),
131 fPlaybackChar.origin(),
132 fPlaybackChar.sampleCount(),
133 fPlaybackChar.colorType(),
134 fPlaybackChar.refColorSpace(),
135 &fPlaybackChar.surfaceProps());
136}
137
139 const SkPicture* picture) {
140 SkASSERT(!fDisplayList && !fTileSurface && picture);
141
142 fTileSurface = this->makeWrappedTileDest(dContext);
143 if (fTileSurface) {
144 SkCanvas* tileCanvas = fTileSurface->getCanvas();
145
146 SkASSERT(this->padOffset().isZero() && this->paddedRectSize() == fClip.size());
147 tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
148 tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
149
150 tileCanvas->drawPicture(picture);
151
152 // We can't snap an image here bc, since we're using wrapped backend textures for the
153 // surfaces, that would incur a copy.
154 }
155}
156
158 SkASSERT(fDisplayList && !fTileSurface);
159
160 fTileSurface = this->makeWrappedTileDest(direct);
161 if (fTileSurface) {
162 skgpu::ganesh::DrawDDL(fTileSurface, fDisplayList);
163
164 // We can't snap an image here bc, since we're using wrapped backend textures for the
165 // surfaces, that would incur a copy.
166 }
167}
168
170 // TODO: when DDLs are re-renderable we don't need to do this
171 fDisplayList = nullptr;
172
173 fTileSurface = nullptr;
174}
175
177 sk_sp<GrContextThreadSafeProxy> threadSafeProxy) {
178 SkASSERT(fCallbackContext);
179
180 // The promise image gets a ref on the promise callback context
181 sk_sp<SkImage> promiseImage =
182 SkImages::PromiseTextureFrom(std::move(threadSafeProxy),
183 fCallbackContext->backendFormat(),
184 this->paddedRectSize(),
185 skgpu::Mipmapped::kNo,
187 fPlaybackChar.colorType(),
189 fPlaybackChar.refColorSpace(),
192 (void*)this->refCallbackContext().release());
193 fCallbackContext->wasAddedToImage();
194
195 return promiseImage;
196}
197
199 SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture());
200
201 const GrSurfaceCharacterization& c = tile->fPlaybackChar;
202 GrBackendTexture beTex =
203 direct->createBackendTexture(c.width(),
204 c.height(),
205 c.colorType(),
207 GrRenderable::kYes,
208 GrProtected::kNo,
209 /*label=*/"DDLTile_TileData_CreateBackendTexture");
210 tile->fCallbackContext->setBackendTexture(beTex);
211}
212
214 if (!tile->initialized() || dContext->abandoned()) {
215 return;
216 }
217
218 SkASSERT(tile->fCallbackContext);
219
220 // TODO: it seems that, on the Linux bots, backend texture creation is failing
221 // a lot (skbug.com/10142)
222 SkASSERT(!tile->fCallbackContext->promiseImageTexture() ||
223 tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid());
224
225 tile->fTileSurface = nullptr;
226
227 SkASSERT(tile->fCallbackContext->unique());
228 tile->fCallbackContext.reset();
229}
230
231///////////////////////////////////////////////////////////////////////////////////////////////////
232
234 const GrSurfaceCharacterization& dstChar,
235 const SkIRect& viewport,
236 int numXDivisions, int numYDivisions,
237 bool addRandomPaddingToDst)
238 : fNumXDivisions(numXDivisions)
239 , fNumYDivisions(numYDivisions)
240 , fTiles(numXDivisions * numYDivisions)
241 , fDstCharacterization(dstChar) {
242 SkASSERT(fNumXDivisions > 0 && fNumYDivisions > 0);
243
244 int xTileSize = viewport.width()/fNumXDivisions;
245 int yTileSize = viewport.height()/fNumYDivisions;
246
247 SkRandom rand;
248
249 // Create the destination tiles
250 for (int y = 0, yOff = 0; y < fNumYDivisions; ++y, yOff += yTileSize) {
251 int ySize = (y < fNumYDivisions-1) ? yTileSize : viewport.height()-yOff;
252
253 for (int x = 0, xOff = 0; x < fNumXDivisions; ++x, xOff += xTileSize) {
254 int xSize = (x < fNumXDivisions-1) ? xTileSize : viewport.width()-xOff;
255
256 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
257
258 SkASSERT(viewport.contains(clip));
259
260 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
266 fTiles[y*fNumXDivisions+x].init(y*fNumXDivisions+x, direct, dstChar, clip,
267 {lPad, tPad, rPad, bPad});
268 }
269 }
270}
271
273#if 1
274 SkTaskGroup().batch(this->numTiles(), [&](int i) {
275 fTiles[i].createDDL(picture);
276 });
277 SkTaskGroup().add([this]{ this->createComposeDDL(); });
278 SkTaskGroup().wait();
279#else
280 // Use this code path to debug w/o threads
281 for (int i = 0; i < this->numTiles(); ++i) {
282 fTiles[i].createDDL(picture);
283 }
284 this->createComposeDDL();
285#endif
286}
287
288// 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
293
294 // TODO: schedule program compilation as their own tasks
295 tile->precompile(direct);
296
297 tile->draw(direct);
298
299 tile->dropDDL();
300}
301
302// We expect to have more than one recording thread but just one gpu thread
304 SkTaskGroup* gpuTaskGroup,
305 GrDirectContext* dContext,
306 SkPicture* picture) {
307 SkASSERT(recordingTaskGroup && gpuTaskGroup && dContext);
308
309 for (int i = 0; i < this->numTiles(); ++i) {
310 TileData* tile = &fTiles[i];
311 if (!tile->initialized()) {
312 continue;
313 }
314
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
320 recordingTaskGroup->add([tile, gpuTaskGroup, dContext, picture]() {
321 tile->createDDL(picture);
322
323 gpuTaskGroup->add([dContext, tile]() {
324 do_gpu_stuff(dContext, tile);
325 });
326 });
327 }
328
329 recordingTaskGroup->add([this] { this->createComposeDDL(); });
330}
331
332// Only called from skpbench
334 for (int i = 0; i < this->numTiles(); ++i) {
335 fTiles[i].createDDL(picture);
336 fTiles[i].draw(dContext);
337 }
338}
339
340// Only called from skpbench
342 for (int i = 0; i < this->numTiles(); ++i) {
343 fTiles[i].drawSKPDirectly(dContext, picture);
344 }
345}
346
348 for (int i = 0; i < this->numTiles(); ++i) {
349 fTiles[i].dropCallbackContext();
350 }
351}
352
354 for (int i = 0; i < this->numTiles(); ++i) {
355 fTiles[i].reset();
356 }
357 fComposeDDL.reset();
358}
359
361
362 if (taskGroup) {
363 for (int i = 0; i < this->numTiles(); ++i) {
364 TileData* tile = &fTiles[i];
365 if (!tile->initialized()) {
366 continue;
367 }
368
369 taskGroup->add([direct, tile]() { TileData::CreateBackendTexture(direct, tile); });
370 }
371 } else {
372 for (int i = 0; i < this->numTiles(); ++i) {
373 TileData::CreateBackendTexture(direct, &fTiles[i]);
374 }
375 }
376}
377
379 if (taskGroup) {
380 for (int i = 0; i < this->numTiles(); ++i) {
381 TileData* tile = &fTiles[i];
382
383 taskGroup->add([direct, tile]() { TileData::DeleteBackendTexture(direct, tile); });
384 }
385 } else {
386 for (int i = 0; i < this->numTiles(); ++i) {
387 TileData::DeleteBackendTexture(direct, &fTiles[i]);
388 }
389 }
390}
static void do_gpu_stuff(GrDirectContext *direct, DDLTileHelper::TileData *tile)
@ kBottomLeft_GrSurfaceOrigin
Definition GrTypes.h:149
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkDEBUGCODE(...)
Definition SkDebug.h:23
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
static void DeleteBackendTexture(GrDirectContext *, TileData *)
void createDDL(const SkPicture *)
SkIRect clipRect() const
void drawSKPDirectly(GrDirectContext *, const SkPicture *)
void init(int id, GrDirectContext *, const GrSurfaceCharacterization &dstChar, const SkIRect &clip, const SkIRect &paddingOutsets)
static void CreateBackendTexture(GrDirectContext *, TileData *)
sk_sp< SkImage > makePromiseImageForDst(sk_sp< GrContextThreadSafeProxy >)
void draw(GrDirectContext *)
SkISize paddedRectSize() const
void precompile(GrDirectContext *)
SkIVector padOffset() const
void createBackendTextures(SkTaskGroup *, GrDirectContext *)
void deleteBackendTextures(SkTaskGroup *, GrDirectContext *)
void createComposeDDL()
void drawAllTilesDirectly(GrDirectContext *, SkPicture *)
void dropCallbackContexts()
DDLTileHelper(GrDirectContext *, const GrSurfaceCharacterization &dstChar, const SkIRect &viewport, int numXDivisions, int numYDivisions, bool addRandomPaddingToDst)
void interleaveDDLCreationAndDraw(GrDirectContext *, SkPicture *)
int numTiles() const
void kickOffThreadedWork(SkTaskGroup *recordingTaskGroup, SkTaskGroup *gpuTaskGroup, GrDirectContext *, SkPicture *)
void createDDLsInParallel(SkPicture *)
const GrCaps * caps() const
sk_sp< GrContextThreadSafeProxy > threadSafeProxy()
sk_sp< GrDeferredDisplayList > detach()
bool abandoned() override
GrBackendTexture createBackendTexture(int width, int height, const GrBackendFormat &, skgpu::Mipmapped, GrRenderable, GrProtected=GrProtected::kNo, std::string_view label={})
GrDirectContextPriv priv()
GrBackendTexture backendTexture() const
GrSurfaceCharacterization createResized(int width, int height) const
static sk_sp< GrPromiseImageTexture > PromiseImageFulfillProc(void *textureContext)
void setBackendTexture(const GrBackendTexture &backendTexture)
static void PromiseImageReleaseProc(void *textureContext)
const GrPromiseImageTexture * promiseImageTexture() const
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void translate(SkScalar dx, SkScalar dy)
virtual GrRecordingContext * recordingContext() const
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition SkCanvas.h:1542
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
void drawPicture(const SkPicture *picture)
Definition SkCanvas.h:1961
uint32_t nextRangeU(uint32_t min, uint32_t max)
Definition SkRandom.h:80
bool unique() const
Definition SkRefCnt.h:50
void batch(int N, std::function< void(int)> fn)
void add(std::function< void(void)> fn)
T * get() const
Definition SkRefCnt.h:303
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
double y
double x
SK_API sk_sp< SkImage > PromiseTextureFrom(skgpu::graphite::Recorder *, SkISize dimensions, const skgpu::graphite::TextureInfo &, const SkColorInfo &, skgpu::Origin origin, skgpu::graphite::Volatile, GraphitePromiseTextureFulfillProc, GraphitePromiseImageReleaseProc, GraphitePromiseTextureReleaseProc, GraphitePromiseImageContext)
SK_API sk_sp< SkSurface > WrapBackendTexture(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, int sampleCnt, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
SK_API bool DrawDDL(SkSurface *, sk_sp< const GrDeferredDisplayList > ddl)
Mipmapped
Definition GpuTypes.h:53
int32_t height
int32_t width
constexpr int32_t y() const
constexpr int32_t x() const
void offsetTo(int32_t newX, int32_t newY)
Definition SkRect.h:394
constexpr int32_t height() const
Definition SkRect.h:165
constexpr int32_t width() const
Definition SkRect.h:158
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
bool contains(int32_t x, int32_t y) const
Definition SkRect.h:463
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609