xref: /aosp_15_r20/external/skia/tools/DDLTileHelper.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/ganesh/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 
init(int id,GrDirectContext * direct,const GrSurfaceCharacterization & dstSurfaceCharacterization,const SkIRect & clip,const SkIRect & paddingOutsets)28 void DDLTileHelper::TileData::init(int id,
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 
TileData()48 DDLTileHelper::TileData::TileData() {}
~TileData()49 DDLTileHelper::TileData::~TileData() {}
50 
createDDL(const SkPicture * picture)51 void DDLTileHelper::TileData::createDDL(const SkPicture* picture) {
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 
createComposeDDL()74 void DDLTileHelper::createComposeDDL() {
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,
98                                        SkCanvas::kStrict_SrcRectConstraint);
99     }
100 
101     fComposeDDL = recorder.detach();
102     SkASSERT(fComposeDDL);
103 }
104 
precompile(GrDirectContext * direct)105 void DDLTileHelper::TileData::precompile(GrDirectContext* direct) {
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 
makeWrappedTileDest(GrRecordingContext * rContext)118 sk_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 
drawSKPDirectly(GrDirectContext * dContext,const SkPicture * picture)138 void DDLTileHelper::TileData::drawSKPDirectly(GrDirectContext* dContext,
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 
draw(GrDirectContext * direct)157 void DDLTileHelper::TileData::draw(GrDirectContext* direct) {
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 
reset()169 void DDLTileHelper::TileData::reset() {
170     // TODO: when DDLs are re-renderable we don't need to do this
171     fDisplayList = nullptr;
172 
173     fTileSurface = nullptr;
174 }
175 
makePromiseImageForDst(sk_sp<GrContextThreadSafeProxy> threadSafeProxy)176 sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImageForDst(
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,
186                                          GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin,
187                                          fPlaybackChar.colorType(),
188                                          kPremul_SkAlphaType,
189                                          fPlaybackChar.refColorSpace(),
190                                          PromiseImageCallbackContext::PromiseImageFulfillProc,
191                                          PromiseImageCallbackContext::PromiseImageReleaseProc,
192                                          (void*)this->refCallbackContext().release());
193     fCallbackContext->wasAddedToImage();
194 
195     return promiseImage;
196 }
197 
CreateBackendTexture(GrDirectContext * direct,TileData * tile)198 void DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext* direct, TileData* tile) {
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(),
206                                          skgpu::Mipmapped(c.isMipMapped()),
207                                          GrRenderable::kYes,
208                                          GrProtected::kNo,
209                                          /*label=*/"DDLTile_TileData_CreateBackendTexture");
210     tile->fCallbackContext->setBackendTexture(beTex);
211 }
212 
DeleteBackendTexture(GrDirectContext * dContext,TileData * tile)213 void DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext* dContext, TileData* tile) {
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 
DDLTileHelper(GrDirectContext * direct,const GrSurfaceCharacterization & dstChar,const SkIRect & viewport,int numXDivisions,int numYDivisions,bool addRandomPaddingToDst)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         , 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 
createDDLsInParallel(SkPicture * picture)272 void DDLTileHelper::createDDLsInParallel(SkPicture* picture) {
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
do_gpu_stuff(GrDirectContext * direct,DDLTileHelper::TileData * tile)292 static void do_gpu_stuff(GrDirectContext* direct, DDLTileHelper::TileData* tile) {
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
kickOffThreadedWork(SkTaskGroup * recordingTaskGroup,SkTaskGroup * gpuTaskGroup,GrDirectContext * dContext,SkPicture * picture)303 void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
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
interleaveDDLCreationAndDraw(GrDirectContext * dContext,SkPicture * picture)333 void DDLTileHelper::interleaveDDLCreationAndDraw(GrDirectContext* dContext, SkPicture* picture) {
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
drawAllTilesDirectly(GrDirectContext * dContext,SkPicture * picture)341 void DDLTileHelper::drawAllTilesDirectly(GrDirectContext* dContext, SkPicture* picture) {
342     for (int i = 0; i < this->numTiles(); ++i) {
343         fTiles[i].drawSKPDirectly(dContext, picture);
344     }
345 }
346 
dropCallbackContexts()347 void DDLTileHelper::dropCallbackContexts() {
348     for (int i = 0; i < this->numTiles(); ++i) {
349         fTiles[i].dropCallbackContext();
350     }
351 }
352 
resetAllTiles()353 void DDLTileHelper::resetAllTiles() {
354     for (int i = 0; i < this->numTiles(); ++i) {
355         fTiles[i].reset();
356     }
357     fComposeDDL.reset();
358 }
359 
createBackendTextures(SkTaskGroup * taskGroup,GrDirectContext * direct)360 void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
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 
deleteBackendTextures(SkTaskGroup * taskGroup,GrDirectContext * direct)378 void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
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 }
391