• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkDeferredDisplayListRecorder.h"
12 #include "include/core/SkPicture.h"
13 #include "include/core/SkSurface.h"
14 #include "include/core/SkSurfaceCharacterization.h"
15 #include "include/gpu/GrDirectContext.h"
16 #include "src/core/SkDeferredDisplayListPriv.h"
17 #include "src/core/SkTaskGroup.h"
18 #include "src/gpu/GrDirectContextPriv.h"
19 #include "src/image/SkImage_Gpu.h"
20 #include "tools/DDLPromiseImageHelper.h"
21 
init(int id,GrDirectContext * direct,const SkSurfaceCharacterization & dstSurfaceCharacterization,const SkIRect & clip,const SkIRect & paddingOutsets)22 void DDLTileHelper::TileData::init(int id,
23                                    GrDirectContext* direct,
24                                    const SkSurfaceCharacterization& dstSurfaceCharacterization,
25                                    const SkIRect& clip,
26                                    const SkIRect& paddingOutsets) {
27     fID = id;
28     fClip = clip;
29     fPaddingOutsets = paddingOutsets;
30 
31     fPlaybackChar  = dstSurfaceCharacterization.createResized(this->paddedRectSize().width(),
32                                                               this->paddedRectSize().height());
33     SkASSERT(fPlaybackChar.isValid());
34 
35     SkDEBUGCODE(const GrCaps* caps = direct->priv().caps());
36     SkASSERT(caps->isFormatTexturable(fPlaybackChar.backendFormat(),
37                                       fPlaybackChar.backendFormat().textureType()));
38 
39     fCallbackContext.reset(new PromiseImageCallbackContext(direct, fPlaybackChar.backendFormat()));
40 }
41 
TileData()42 DDLTileHelper::TileData::TileData() {}
~TileData()43 DDLTileHelper::TileData::~TileData() {}
44 
createDDL(const SkPicture * picture)45 void DDLTileHelper::TileData::createDDL(const SkPicture* picture) {
46     SkASSERT(!fDisplayList && picture);
47 
48     auto recordingChar = fPlaybackChar.createResized(fClip.width(), fClip.height());
49     SkASSERT(recordingChar.isValid());
50 
51     SkDeferredDisplayListRecorder recorder(recordingChar);
52 
53     // DDL TODO: the DDLRecorder's rContext isn't initialized until getCanvas is called.
54     // Maybe set it up in the ctor?
55     SkCanvas* recordingCanvas = recorder.getCanvas();
56 
57     // We always record the DDL in the (0,0) .. (clipWidth, clipHeight) coordinates
58     recordingCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
59     recordingCanvas->translate(-fClip.fLeft, -fClip.fTop);
60 
61     // Note: in this use case we only render a picture to the deferred canvas
62     // but, more generally, clients will use arbitrary draw calls.
63     recordingCanvas->drawPicture(picture);
64 
65     fDisplayList = recorder.detach();
66 }
67 
createComposeDDL()68 void DDLTileHelper::createComposeDDL() {
69     SkASSERT(!fComposeDDL);
70 
71     SkDeferredDisplayListRecorder recorder(fDstCharacterization);
72 
73     SkCanvas* recordingCanvas = recorder.getCanvas();
74 
75     for (int i = 0; i < this->numTiles(); ++i) {
76         TileData* tile = &fTiles[i];
77         if (!tile->initialized()) {
78             continue;
79         }
80 
81         sk_sp<SkImage> promiseImage = tile->makePromiseImageForDst(
82                                            recordingCanvas->recordingContext()->threadSafeProxy());
83 
84         SkRect dstRect = SkRect::Make(tile->clipRect());
85         SkIRect srcRect = tile->clipRect();
86         srcRect.offsetTo(tile->padOffset().x(), tile->padOffset().y());
87 
88         SkASSERT(promiseImage->bounds().contains(srcRect));
89 
90         recordingCanvas->drawImageRect(promiseImage.get(), SkRect::Make(srcRect), dstRect,
91                                        SkSamplingOptions(), nullptr,
92                                        SkCanvas::kStrict_SrcRectConstraint);
93     }
94 
95     fComposeDDL = recorder.detach();
96     SkASSERT(fComposeDDL);
97 }
98 
precompile(GrDirectContext * direct)99 void DDLTileHelper::TileData::precompile(GrDirectContext* direct) {
100     if (!this->initialized()) {
101         return;
102     }
103 
104     SkASSERT(fDisplayList);
105 
106     SkDeferredDisplayList::ProgramIterator iter(direct, fDisplayList.get());
107     for (; !iter.done(); iter.next()) {
108         iter.compile();
109     }
110 }
111 
makeWrappedTileDest(GrRecordingContext * rContext)112 sk_sp<SkSurface> DDLTileHelper::TileData::makeWrappedTileDest(GrRecordingContext* rContext) {
113     SkASSERT(fCallbackContext && fCallbackContext->promiseImageTexture());
114 
115     auto promiseImageTexture = fCallbackContext->promiseImageTexture();
116     if (!promiseImageTexture->backendTexture().isValid()) {
117         return nullptr;
118     }
119 
120     // Here we are, unfortunately, aliasing the backend texture held by the SkPromiseImageTexture.
121     // Both the tile's destination surface and the promise image used to draw the tile will be
122     // backed by the same backendTexture - unbeknownst to Ganesh.
123     return SkSurface::MakeFromBackendTexture(rContext,
124                                              promiseImageTexture->backendTexture(),
125                                              fPlaybackChar.origin(),
126                                              fPlaybackChar.sampleCount(),
127                                              fPlaybackChar.colorType(),
128                                              fPlaybackChar.refColorSpace(),
129                                              &fPlaybackChar.surfaceProps());
130 }
131 
drawSKPDirectly(GrDirectContext * dContext,const SkPicture * picture)132 void DDLTileHelper::TileData::drawSKPDirectly(GrDirectContext* dContext,
133                                               const SkPicture* picture) {
134     SkASSERT(!fDisplayList && !fTileSurface && picture);
135 
136     fTileSurface = this->makeWrappedTileDest(dContext);
137     if (fTileSurface) {
138         SkCanvas* tileCanvas = fTileSurface->getCanvas();
139 
140         SkASSERT(this->padOffset().isZero() && this->paddedRectSize() == fClip.size());
141         tileCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
142         tileCanvas->translate(-fClip.fLeft, -fClip.fTop);
143 
144         tileCanvas->drawPicture(picture);
145 
146         // We can't snap an image here bc, since we're using wrapped backend textures for the
147         // surfaces, that would incur a copy.
148     }
149 }
150 
draw(GrDirectContext * direct)151 void DDLTileHelper::TileData::draw(GrDirectContext* direct) {
152     SkASSERT(fDisplayList && !fTileSurface);
153 
154     fTileSurface = this->makeWrappedTileDest(direct);
155     if (fTileSurface) {
156         fTileSurface->draw(fDisplayList, this->padOffset().x(), this->padOffset().y());
157 
158         // We can't snap an image here bc, since we're using wrapped backend textures for the
159         // surfaces, that would incur a copy.
160     }
161 }
162 
reset()163 void DDLTileHelper::TileData::reset() {
164     // TODO: when DDLs are re-renderable we don't need to do this
165     fDisplayList = nullptr;
166 
167     fTileSurface = nullptr;
168 }
169 
makePromiseImageForDst(sk_sp<GrContextThreadSafeProxy> threadSafeProxy)170 sk_sp<SkImage> DDLTileHelper::TileData::makePromiseImageForDst(
171                                                 sk_sp<GrContextThreadSafeProxy> threadSafeProxy) {
172     SkASSERT(fCallbackContext);
173 
174     // The promise image gets a ref on the promise callback context
175     sk_sp<SkImage> promiseImage =
176                 SkImage::MakePromiseTexture(std::move(threadSafeProxy),
177                                             fCallbackContext->backendFormat(),
178                                             this->paddedRectSize(),
179                                             GrMipmapped::kNo,
180                                             GrSurfaceOrigin::kBottomLeft_GrSurfaceOrigin,
181                                             fPlaybackChar.colorType(),
182                                             kPremul_SkAlphaType,
183                                             fPlaybackChar.refColorSpace(),
184                                             PromiseImageCallbackContext::PromiseImageFulfillProc,
185                                             PromiseImageCallbackContext::PromiseImageReleaseProc,
186                                             (void*)this->refCallbackContext().release());
187     fCallbackContext->wasAddedToImage();
188 
189     return promiseImage;
190 }
191 
CreateBackendTexture(GrDirectContext * direct,TileData * tile)192 void DDLTileHelper::TileData::CreateBackendTexture(GrDirectContext* direct, TileData* tile) {
193     SkASSERT(tile->fCallbackContext && !tile->fCallbackContext->promiseImageTexture());
194 
195     const SkSurfaceCharacterization& c = tile->fPlaybackChar;
196     GrBackendTexture beTex = direct->createBackendTexture(c.width(), c.height(), c.colorType(),
197                                                           GrMipMapped(c.isMipMapped()),
198                                                           GrRenderable::kYes);
199     tile->fCallbackContext->setBackendTexture(beTex);
200 }
201 
DeleteBackendTexture(GrDirectContext *,TileData * tile)202 void DDLTileHelper::TileData::DeleteBackendTexture(GrDirectContext*, TileData* tile) {
203     if (!tile->initialized()) {
204         return;
205     }
206 
207     SkASSERT(tile->fCallbackContext);
208 
209     // TODO: it seems that, on the Linux bots, backend texture creation is failing
210     // a lot (skbug.com/10142)
211     SkASSERT(!tile->fCallbackContext->promiseImageTexture() ||
212              tile->fCallbackContext->promiseImageTexture()->backendTexture().isValid());
213 
214     tile->fTileSurface = nullptr;
215 
216     SkASSERT(tile->fCallbackContext->unique());
217     tile->fCallbackContext.reset();
218 }
219 
220 ///////////////////////////////////////////////////////////////////////////////////////////////////
221 
DDLTileHelper(GrDirectContext * direct,const SkSurfaceCharacterization & dstChar,const SkIRect & viewport,int numXDivisions,int numYDivisions,bool addRandomPaddingToDst)222 DDLTileHelper::DDLTileHelper(GrDirectContext* direct,
223                              const SkSurfaceCharacterization& dstChar,
224                              const SkIRect& viewport,
225                              int numXDivisions, int numYDivisions,
226                              bool addRandomPaddingToDst)
227         : fNumXDivisions(numXDivisions)
228         , fNumYDivisions(numYDivisions)
229         , fTiles(numXDivisions * numYDivisions)
230         , fDstCharacterization(dstChar) {
231     SkASSERT(fNumXDivisions > 0 && fNumYDivisions > 0);
232 
233     int xTileSize = viewport.width()/fNumXDivisions;
234     int yTileSize = viewport.height()/fNumYDivisions;
235 
236     SkRandom rand;
237 
238     // Create the destination tiles
239     for (int y = 0, yOff = 0; y < fNumYDivisions; ++y, yOff += yTileSize) {
240         int ySize = (y < fNumYDivisions-1) ? yTileSize : viewport.height()-yOff;
241 
242         for (int x = 0, xOff = 0; x < fNumXDivisions; ++x, xOff += xTileSize) {
243             int xSize = (x < fNumXDivisions-1) ? xTileSize : viewport.width()-xOff;
244 
245             SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
246 
247             SkASSERT(viewport.contains(clip));
248 
249             static const uint32_t kMaxPad = 64;
250             int32_t lPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
251             int32_t tPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
252             int32_t rPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
253             int32_t bPad = addRandomPaddingToDst ? rand.nextRangeU(0, kMaxPad) : 0;
254 
255             fTiles[y*fNumXDivisions+x].init(y*fNumXDivisions+x, direct, dstChar, clip,
256                                            {lPad, tPad, rPad, bPad});
257         }
258     }
259 }
260 
createDDLsInParallel(SkPicture * picture)261 void DDLTileHelper::createDDLsInParallel(SkPicture* picture) {
262 #if 1
263     SkTaskGroup().batch(this->numTiles(), [&](int i) {
264         fTiles[i].createDDL(picture);
265     });
266     SkTaskGroup().add([this]{ this->createComposeDDL(); });
267     SkTaskGroup().wait();
268 #else
269     // Use this code path to debug w/o threads
270     for (int i = 0; i < this->numTiles(); ++i) {
271         fTiles[i].createDDL(picture);
272     }
273     this->createComposeDDL();
274 #endif
275 }
276 
277 // On the gpu thread:
278 //    precompile any programs
279 //    replay the DDL into a surface to make the tile image
280 //    compose the tile image into the main canvas
do_gpu_stuff(GrDirectContext * direct,DDLTileHelper::TileData * tile)281 static void do_gpu_stuff(GrDirectContext* direct, DDLTileHelper::TileData* tile) {
282 
283     // TODO: schedule program compilation as their own tasks
284     tile->precompile(direct);
285 
286     tile->draw(direct);
287 
288     tile->dropDDL();
289 }
290 
291 // We expect to have more than one recording thread but just one gpu thread
kickOffThreadedWork(SkTaskGroup * recordingTaskGroup,SkTaskGroup * gpuTaskGroup,GrDirectContext * dContext,SkPicture * picture)292 void DDLTileHelper::kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
293                                         SkTaskGroup* gpuTaskGroup,
294                                         GrDirectContext* dContext,
295                                         SkPicture* picture) {
296     SkASSERT(recordingTaskGroup && gpuTaskGroup && dContext);
297 
298     for (int i = 0; i < this->numTiles(); ++i) {
299         TileData* tile = &fTiles[i];
300         if (!tile->initialized()) {
301             continue;
302         }
303 
304         // On a recording thread:
305         //    generate the tile's DDL
306         //    schedule gpu-thread processing of the DDL
307         // Note: a finer grained approach would be add a scheduling task which would evaluate
308         //       which DDLs were ready to be rendered based on their prerequisites
309         recordingTaskGroup->add([tile, gpuTaskGroup, dContext, picture]() {
310                                     tile->createDDL(picture);
311 
312                                     gpuTaskGroup->add([dContext, tile]() {
313                                         do_gpu_stuff(dContext, tile);
314                                     });
315                                 });
316     }
317 
318     recordingTaskGroup->add([this] { this->createComposeDDL(); });
319 }
320 
321 // Only called from skpbench
interleaveDDLCreationAndDraw(GrDirectContext * dContext,SkPicture * picture)322 void DDLTileHelper::interleaveDDLCreationAndDraw(GrDirectContext* dContext, SkPicture* picture) {
323     for (int i = 0; i < this->numTiles(); ++i) {
324         fTiles[i].createDDL(picture);
325         fTiles[i].draw(dContext);
326     }
327 }
328 
329 // Only called from skpbench
drawAllTilesDirectly(GrDirectContext * dContext,SkPicture * picture)330 void DDLTileHelper::drawAllTilesDirectly(GrDirectContext* dContext, SkPicture* picture) {
331     for (int i = 0; i < this->numTiles(); ++i) {
332         fTiles[i].drawSKPDirectly(dContext, picture);
333     }
334 }
335 
dropCallbackContexts()336 void DDLTileHelper::dropCallbackContexts() {
337     for (int i = 0; i < this->numTiles(); ++i) {
338         fTiles[i].dropCallbackContext();
339     }
340 }
341 
resetAllTiles()342 void DDLTileHelper::resetAllTiles() {
343     for (int i = 0; i < this->numTiles(); ++i) {
344         fTiles[i].reset();
345     }
346     fComposeDDL.reset();
347 }
348 
createBackendTextures(SkTaskGroup * taskGroup,GrDirectContext * direct)349 void DDLTileHelper::createBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
350 
351     if (taskGroup) {
352         for (int i = 0; i < this->numTiles(); ++i) {
353             TileData* tile = &fTiles[i];
354             if (!tile->initialized()) {
355                 continue;
356             }
357 
358             taskGroup->add([direct, tile]() { TileData::CreateBackendTexture(direct, tile); });
359         }
360     } else {
361         for (int i = 0; i < this->numTiles(); ++i) {
362             TileData::CreateBackendTexture(direct, &fTiles[i]);
363         }
364     }
365 }
366 
deleteBackendTextures(SkTaskGroup * taskGroup,GrDirectContext * direct)367 void DDLTileHelper::deleteBackendTextures(SkTaskGroup* taskGroup, GrDirectContext* direct) {
368     if (taskGroup) {
369         for (int i = 0; i < this->numTiles(); ++i) {
370             TileData* tile = &fTiles[i];
371 
372             taskGroup->add([direct, tile]() { TileData::DeleteBackendTexture(direct, tile); });
373         }
374     } else {
375         for (int i = 0; i < this->numTiles(); ++i) {
376             TileData::DeleteBackendTexture(direct, &fTiles[i]);
377         }
378     }
379 }
380