Coverage Report

Created: 2024-05-20 07:14

/src/skia/tools/DDLTileHelper.cpp
Line
Count
Source (jump to first uncovered line)
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
8
#include "tools/DDLTileHelper.h"
9
10
#include "include/core/SkCanvas.h"
11
#include "include/core/SkPicture.h"
12
#include "include/core/SkSurface.h"
13
#include "include/gpu/GrDirectContext.h"
14
#include "include/gpu/ganesh/SkImageGanesh.h"
15
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
16
#include "include/private/chromium/GrDeferredDisplayList.h"
17
#include "include/private/chromium/GrDeferredDisplayListRecorder.h"
18
#include "include/private/chromium/GrSurfaceCharacterization.h"
19
#include "include/private/chromium/SkImageChromium.h"
20
#include "src/base/SkRandom.h"
21
#include "src/core/SkTaskGroup.h"
22
#include "src/gpu/ganesh/GrCaps.h"
23
#include "src/gpu/ganesh/GrDeferredDisplayListPriv.h"
24
#include "src/gpu/ganesh/GrDirectContextPriv.h"
25
#include "src/gpu/ganesh/image/SkImage_Ganesh.h"
26
#include "tools/DDLPromiseImageHelper.h"
27
28
void DDLTileHelper::TileData::init(int id,
29
                                   GrDirectContext* direct,
30
                                   const GrSurfaceCharacterization& dstSurfaceCharacterization,
31
                                   const SkIRect& clip,
32
0
                                   const SkIRect& paddingOutsets) {
33
0
    fID = id;
34
0
    fClip = clip;
35
0
    fPaddingOutsets = paddingOutsets;
36
37
0
    fPlaybackChar  = dstSurfaceCharacterization.createResized(this->paddedRectSize().width(),
38
0
                                                              this->paddedRectSize().height());
39
0
    SkASSERT(fPlaybackChar.isValid());
40
41
0
    SkDEBUGCODE(const GrCaps* caps = direct->priv().caps());
42
0
    SkASSERT(caps->isFormatTexturable(fPlaybackChar.backendFormat(),
43
0
                                      fPlaybackChar.backendFormat().textureType()));
44
45
0
    fCallbackContext.reset(new PromiseImageCallbackContext(direct, fPlaybackChar.backendFormat()));
46
0
}
Unexecuted instantiation: DDLTileHelper::TileData::init(int, GrDirectContext*, GrSurfaceCharacterization const&, SkIRect const&, SkIRect const&)
Unexecuted instantiation: DDLTileHelper::TileData::init(int, GrDirectContext*, GrSurfaceCharacterization const&, SkIRect const&, SkIRect const&)
47
48
0
DDLTileHelper::TileData::TileData() {}
49
0
DDLTileHelper::TileData::~TileData() {}
50
51
0
void DDLTileHelper::TileData::createDDL(const SkPicture* picture) {
52
0
    SkASSERT(!fDisplayList && picture);
53
54
0
    auto recordingChar = fPlaybackChar.createResized(fClip.width(), fClip.height());
55
0
    SkASSERT(recordingChar.isValid());
56
57
0
    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
0
    SkCanvas* recordingCanvas = recorder.getCanvas();
62
63
    // We always record the DDL in the (0,0) .. (clipWidth, clipHeight) coordinates
64
0
    recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
65
0
    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
0
    recordingCanvas->drawPicture(picture);
70
71
0
    fDisplayList = recorder.detach();
72
0
}
Unexecuted instantiation: DDLTileHelper::TileData::createDDL(SkPicture const*)
Unexecuted instantiation: DDLTileHelper::TileData::createDDL(SkPicture const*)
73
74
0
void DDLTileHelper::createComposeDDL() {
75
0
    SkASSERT(!fComposeDDL);
76
77
0
    GrDeferredDisplayListRecorder recorder(fDstCharacterization);
78
79
0
    SkCanvas* recordingCanvas = recorder.getCanvas();
80
81
0
    for (int i = 0; i < this->numTiles(); ++i) {
82
0
        TileData* tile = &fTiles[i];
83
0
        if (!tile->initialized()) {
84
0
            continue;
85
0
        }
86
87
0
        sk_sp<SkImage> promiseImage = tile->makePromiseImageForDst(
88
0
                                           recordingCanvas->recordingContext()->threadSafeProxy());
89
90
0
        SkRect dstRect = SkRect::Make(tile->clipRect());
91
0
        SkIRect srcRect = tile->clipRect();
92
0
        srcRect.offsetTo(tile->padOffset().x(), tile->padOffset().y());
93
94
0
        SkASSERT(promiseImage->bounds().contains(srcRect));
95
96
0
        recordingCanvas->drawImageRect(promiseImage.get(), SkRect::Make(srcRect), dstRect,
97
0
                                       SkSamplingOptions(), nullptr,
98
0
                                       SkCanvas::kStrict_SrcRectConstraint);
99
0
    }
100
101
0
    fComposeDDL = recorder.detach();
102
0
    SkASSERT(fComposeDDL);
103
0
}
Unexecuted instantiation: DDLTileHelper::createComposeDDL()
Unexecuted instantiation: DDLTileHelper::createComposeDDL()
104
105
0
void DDLTileHelper::TileData::precompile(GrDirectContext* direct) {
106
0
    if (!this->initialized()) {
107
0
        return;
108
0
    }
109
110
0
    SkASSERT(fDisplayList);
111
112
0
    GrDeferredDisplayList::ProgramIterator iter(direct, fDisplayList.get());
113
0
    for (; !iter.done(); iter.next()) {
114
0
        iter.compile();
115
0
    }
116
0
}
Unexecuted instantiation: DDLTileHelper::TileData::precompile(GrDirectContext*)
Unexecuted instantiation: DDLTileHelper::TileData::precompile(GrDirectContext*)
117
118
0
sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext* rContext) {
119
0
    SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture());
120
121
0
    auto promiseImageTexture = fCallbackContext->promiseImageTexture();
122
0
    if (!promiseImageTexture->backendTexture().isValid()) {
123
0
        return nullptr;
124
0
    }
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
0
    return SkSurfaces::WrapBackendTexture(rContext,
130
0
                                          promiseImageTexture->backendTexture(),
131
0
                                          fPlaybackChar.origin(),
132
0
                                          fPlaybackChar.sampleCount(),
133
0
                                          fPlaybackChar.colorType(),
134
0
                                          fPlaybackChar.refColorSpace(),
135
0
                                          &fPlaybackChar.surfaceProps());
136
0
}
Unexecuted instantiation: DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext*)
Unexecuted instantiation: DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext*)
137
138
void DDLTileHelper::TileData::drawSKPDirectly(GrDirectContext* dContext,
139
0
                                              const SkPicture* picture) {
140
0
    SkASSERT(!fDisplayList && !fTileSurface && picture);
141
142
0
    fTileSurface = this->makeWrappedTileDest(dContext);
143
0
    if (fTileSurface) {
144
0
        SkCanvas* tileCanvas = fTileSurface->getCanvas();
145
146
0
        SkASSERT(this->padOffset().isZero() && this->paddedRectSize() == fClip.size());
147
0
        tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
148
0
        tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
149
150
0
        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
0
    }
155
0
}
Unexecuted instantiation: DDLTileHelper::TileData::drawSKPDirectly(GrDirectContext*, SkPicture const*)
Unexecuted instantiation: DDLTileHelper::TileData::drawSKPDirectly(GrDirectContext*, SkPicture const*)
156
157
0
void DDLTileHelper::TileData::draw(GrDirectContext* direct) {
158
0
    SkASSERT(fDisplayList && !fTileSurface);
159
160
0
    fTileSurface = this->makeWrappedTileDest(direct);
161
0
    if (fTileSurface) {
162
0
        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
0
    }
167
0
}
Unexecuted instantiation: DDLTileHelper::TileData::draw(GrDirectContext*)
Unexecuted instantiation: DDLTileHelper::TileData::draw(GrDirectContext*)
168
169
0
void DDLTileHelper::TileData::reset() {
170
    // TODO: when DDLs are re-renderable we don't need to do this
171
0
    fDisplayList = nullptr;
172
173
0
    fTileSurface = nullptr;
174
0
}
175
176
sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImageForDst(
177
0
                                                sk_sp<GrContextThreadSafeProxy> threadSafeProxy) {
178
0
    SkASSERT(fCallbackContext);
179
180
    // The promise image gets a ref on the promise callback context
181
0
    sk_sp<SkImage> promiseImage =
182
0
            SkImages::PromiseTextureFrom(std::move(threadSafeProxy),
183
0
                                         fCallbackContext->backendFormat(),
184
0
                                         this->paddedRectSize(),
185
0
                                         skgpu::Mipmapped::kNo,
186
0
                                         GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin,
187
0
                                         fPlaybackChar.colorType(),
188
0
                                         kPremul_SkAlphaType,
189
0
                                         fPlaybackChar.refColorSpace(),
190
0
                                         PromiseImageCallbackContext::PromiseImageFulfillProc,
191
0
                                         PromiseImageCallbackContext::PromiseImageReleaseProc,
192
0
                                         (void*)this->refCallbackContext().release());
193
0
    fCallbackContext->wasAddedToImage();
194
195
0
    return promiseImage;
196
0
}
Unexecuted instantiation: DDLTileHelper::TileData::makePromiseImageForDst(sk_sp<GrContextThreadSafeProxy>)
Unexecuted instantiation: DDLTileHelper::TileData::makePromiseImageForDst(sk_sp<GrContextThreadSafeProxy>)
197
198
0
void DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext* direct, TileData* tile) {
199
0
    SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture());
200
201
0
    const GrSurfaceCharacterization& c = tile->fPlaybackChar;
202
0
    GrBackendTexture beTex =
203
0
            direct->createBackendTexture(c.width(),
204
0
                                         c.height(),
205
0
                                         c.colorType(),
206
0
                                         skgpu::Mipmapped(c.isMipMapped()),
207
0
                                         GrRenderable::kYes,
208
0
                                         GrProtected::kNo,
209
0
                                         /*label=*/"DDLTile_TileData_CreateBackendTexture");
210
0
    tile->fCallbackContext->setBackendTexture(beTex);
211
0
}
Unexecuted instantiation: DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext*, DDLTileHelper::TileData*)
Unexecuted instantiation: DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext*, DDLTileHelper::TileData*)
212
213
0
void DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext* dContext, TileData* tile) {
214
0
    if (!tile->initialized() || dContext->abandoned()) {
215
0
        return;
216
0
    }
217
218
0
    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
0
    SkASSERT(!tile->fCallbackContext->promiseImageTexture() ||
223
0
             tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid());
224
225
0
    tile->fTileSurface = nullptr;
226
227
0
    SkASSERT(tile->fCallbackContext->unique());
228
0
    tile->fCallbackContext.reset();
229
0
}
Unexecuted instantiation: DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext*, DDLTileHelper::TileData*)
Unexecuted instantiation: DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext*, DDLTileHelper::TileData*)
230
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
233
DDLTileHelper::DDLTileHelper(GrDirectContext* direct,
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
0
        , fDstCharacterization(dstChar) {
242
0
    SkASSERT(fNumXDivisions > 0 && fNumYDivisions > 0);
243
244
0
    int xTileSize = viewport.width()/fNumXDivisions;
245
0
    int yTileSize = viewport.height()/fNumYDivisions;
246
247
0
    SkRandom rand;
248
249
    // Create the destination tiles
250
0
    for (int y = 0, yOff = 0; y < fNumYDivisions; ++y, yOff += yTileSize) {
251
0
        int ySize = (y < fNumYDivisions-1) ? yTileSize : viewport.height()-yOff;
252
253
0
        for (int x = 0, xOff = 0; x < fNumXDivisions; ++x, xOff += xTileSize) {
254
0
            int xSize = (x < fNumXDivisions-1) ? xTileSize : viewport.width()-xOff;
255
256
0
            SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
257
258
0
            SkASSERT(viewport.contains(clip));
259
260
0
            static const uint32_t kMaxPad = 64;
261
0
            int32_t lPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
262
0
            int32_t tPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
263
0
            int32_t rPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
264
0
            int32_t bPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
265
266
0
            fTiles[y*fNumXDivisions+x].init(y*fNumXDivisions+x, direct, dstChar, clip,
267
0
                                           {lPad, tPad, rPad, bPad});
268
0
        }
269
0
    }
270
0
}
Unexecuted instantiation: DDLTileHelper::DDLTileHelper(GrDirectContext*, GrSurfaceCharacterization const&, SkIRect const&, int, int, bool)
Unexecuted instantiation: DDLTileHelper::DDLTileHelper(GrDirectContext*, GrSurfaceCharacterization const&, SkIRect const&, int, int, bool)
271
272
0
void DDLTileHelper::createDDLsInParallel(SkPicture* picture) {
273
0
#if 1
274
0
    SkTaskGroup().batch(this->numTiles(), [&](int i) {
275
0
        fTiles[i].createDDL(picture);
276
0
    });
277
0
    SkTaskGroup().add([this]{ this->createComposeDDL(); });
278
0
    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
0
}
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
292
0
static void do_gpu_stuff(GrDirectContext* direct, DDLTileHelper::TileData* tile) {
293
294
    // TODO: schedule program compilation as their own tasks
295
0
    tile->precompile(direct);
296
297
0
    tile->draw(direct);
298
299
0
    tile->dropDDL();
300
0
}
301
302
// We expect to have more than one recording thread but just one gpu thread
303
void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
304
                                        SkTaskGroup* gpuTaskGroup,
305
                                        GrDirectContext* dContext,
306
0
                                        SkPicture* picture) {
307
0
    SkASSERT(recordingTaskGroup && gpuTaskGroup && dContext);
308
309
0
    for (int i = 0; i < this->numTiles(); ++i) {
310
0
        TileData* tile = &fTiles[i];
311
0
        if (!tile->initialized()) {
312
0
            continue;
313
0
        }
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
0
        recordingTaskGroup->add([tile, gpuTaskGroup, dContext, picture]() {
321
0
                                    tile->createDDL(picture);
322
323
0
                                    gpuTaskGroup->add([dContext, tile]() {
324
0
                                        do_gpu_stuff(dContext, tile);
325
0
                                    });
Unexecuted instantiation: DDLTileHelper.cpp:DDLTileHelper::kickOffThreadedWork(SkTaskGroup*, SkTaskGroup*, GrDirectContext*, SkPicture*)::$_0::operator()() const::{lambda()#1}::operator()() const
Unexecuted instantiation: DDLTileHelper.cpp:DDLTileHelper::kickOffThreadedWork(SkTaskGroup*, SkTaskGroup*, GrDirectContext*, SkPicture*)::$_1::operator()() const::{lambda()#1}::operator()() const
326
0
                                });
327
0
    }
328
329
0
    recordingTaskGroup->add([this] { this->createComposeDDL(); });
Unexecuted instantiation: DDLTileHelper.cpp:DDLTileHelper::kickOffThreadedWork(SkTaskGroup*, SkTaskGroup*, GrDirectContext*, SkPicture*)::$_1::operator()() const
Unexecuted instantiation: DDLTileHelper.cpp:DDLTileHelper::kickOffThreadedWork(SkTaskGroup*, SkTaskGroup*, GrDirectContext*, SkPicture*)::$_2::operator()() const
330
0
}
Unexecuted instantiation: DDLTileHelper::kickOffThreadedWork(SkTaskGroup*, SkTaskGroup*, GrDirectContext*, SkPicture*)
Unexecuted instantiation: DDLTileHelper::kickOffThreadedWork(SkTaskGroup*, SkTaskGroup*, GrDirectContext*, SkPicture*)
331
332
// Only called from skpbench
333
0
void DDLTileHelper::interleaveDDLCreationAndDraw(GrDirectContext* dContext, SkPicture* picture) {
334
0
    for (int i = 0; i < this->numTiles(); ++i) {
335
0
        fTiles[i].createDDL(picture);
336
0
        fTiles[i].draw(dContext);
337
0
    }
338
0
}
339
340
// Only called from skpbench
341
0
void DDLTileHelper::drawAllTilesDirectly(GrDirectContext* dContext, SkPicture* picture) {
342
0
    for (int i = 0; i < this->numTiles(); ++i) {
343
0
        fTiles[i].drawSKPDirectly(dContext, picture);
344
0
    }
345
0
}
346
347
0
void DDLTileHelper::dropCallbackContexts() {
348
0
    for (int i = 0; i < this->numTiles(); ++i) {
349
0
        fTiles[i].dropCallbackContext();
350
0
    }
351
0
}
352
353
0
void DDLTileHelper::resetAllTiles() {
354
0
    for (int i = 0; i < this->numTiles(); ++i) {
355
0
        fTiles[i].reset();
356
0
    }
357
0
    fComposeDDL.reset();
358
0
}
359
360
0
void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
361
362
0
    if (taskGroup) {
363
0
        for (int i = 0; i < this->numTiles(); ++i) {
364
0
            TileData* tile = &fTiles[i];
365
0
            if (!tile->initialized()) {
366
0
                continue;
367
0
            }
368
369
0
            taskGroup->add([direct, tile]() { TileData::CreateBackendTexture(direct, tile); });
370
0
        }
371
0
    } else {
372
0
        for (int i = 0; i < this->numTiles(); ++i) {
373
0
            TileData::CreateBackendTexture(direct, &fTiles[i]);
374
0
        }
375
0
    }
376
0
}
377
378
0
void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
379
0
    if (taskGroup) {
380
0
        for (int i = 0; i < this->numTiles(); ++i) {
381
0
            TileData* tile = &fTiles[i];
382
383
0
            taskGroup->add([direct, tile]() { TileData::DeleteBackendTexture(direct, tile); });
384
0
        }
385
0
    } else {
386
0
        for (int i = 0; i < this->numTiles(); ++i) {
387
0
            TileData::DeleteBackendTexture(direct, &fTiles[i]);
388
0
        }
389
0
    }
390
0
}