blob: 2403b43fc30433b38942c229a789e0bdbdce4e69 [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
8#include "DDLTileHelper.h"
9
Robert Phillips96601082018-05-29 16:13:26 -040010#include "DDLPromiseImageHelper.h"
11#include "SkCanvas.h"
12#include "SkDeferredDisplayListRecorder.h"
13#include "SkImage_Gpu.h"
14#include "SkPicture.h"
15#include "SkSurface.h"
16#include "SkSurfaceCharacterization.h"
17#include "SkTaskGroup.h"
18
19DDLTileHelper::TileData::TileData(sk_sp<SkSurface> s, const SkIRect& clip)
20 : fSurface(std::move(s))
21 , fClip(clip) {
22 SkAssertResult(fSurface->characterize(&fCharacterization));
23}
24
25void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData,
26 const DDLPromiseImageHelper& helper) {
27 SkASSERT(!fReconstitutedPicture);
28
29 // This is bending the DDLRecorder contract! The promise images in the SKP should be
30 // created by the same recorder used to create the matching DDL.
31 SkDeferredDisplayListRecorder recorder(fCharacterization);
32
33 fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages);
34}
35
36void DDLTileHelper::TileData::createDDL() {
37 SkASSERT(fReconstitutedPicture);
38 SkASSERT(!fDisplayList);
39
40 SkDeferredDisplayListRecorder recorder(fCharacterization);
41
42 // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called.
43 // Maybe set it up in the ctor?
44 SkCanvas* subCanvas = recorder.getCanvas();
45
46 // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's
47 // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct
48 // one now.
49 for (int i = 0; i < fPromiseImages.count(); ++i) {
50 GrContext* newContext = subCanvas->getGrContext();
51
52 if (fPromiseImages[i]->isTextureBacked()) {
53 SkImage_Gpu* gpuImage = (SkImage_Gpu*) fPromiseImages[i].get();
54 gpuImage->resetContext(sk_ref_sp(newContext));
55 }
56 }
57
58 subCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
59 subCanvas->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 subCanvas->drawPicture(fReconstitutedPicture);
64
65 fDisplayList = recorder.detach();
66}
67
68void DDLTileHelper::TileData::draw() {
69 SkASSERT(fDisplayList);
70
71 fSurface->draw(fDisplayList.get());
72}
73
74void DDLTileHelper::TileData::compose(SkCanvas* dst) {
75 sk_sp<SkImage> img = fSurface->makeImageSnapshot();
76 dst->save();
77 dst->clipRect(SkRect::Make(fClip));
78 dst->drawImage(std::move(img), fClip.fLeft, fClip.fTop);
79 dst->restore();
80}
81
82void DDLTileHelper::TileData::reset() {
83 // TODO: when DDLs are re-renderable we don't need to do this
84 fDisplayList = nullptr;
85}
86
87///////////////////////////////////////////////////////////////////////////////////////////////////
88
89DDLTileHelper::DDLTileHelper(SkCanvas* canvas, const SkIRect& viewport, int numDivisions)
90 : fNumDivisions(numDivisions) {
91 SkASSERT(fNumDivisions > 0);
92 fTiles.reserve(fNumDivisions*fNumDivisions);
93
94 int xTileSize = viewport.width()/fNumDivisions;
95 int yTileSize = viewport.height()/fNumDivisions;
96
97 // Create the destination tiles
98 for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) {
99 int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff;
100
101 for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) {
102 int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff;
103
104 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
105
106 SkASSERT(viewport.contains(clip));
107
108 SkImageInfo tileII = SkImageInfo::MakeN32Premul(xSize, ySize);
109
110 sk_sp<SkSurface> tileSurface = canvas->makeSurface(tileII);
111
112 // TODO: this is here to deal w/ a resource allocator bug (skbug.com/8007). If all
113 // the DDLs are flushed at the same time (w/o the composition draws) the allocator
114 // feels free to reuse the backing GrSurfaces!
115 tileSurface->flush();
116
117 fTiles.push_back(TileData(std::move(tileSurface), clip));
118 }
119 }
120}
121
122void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData,
123 const DDLPromiseImageHelper& helper) {
124 for (int i = 0; i < fTiles.count(); ++i) {
125 fTiles[i].createTileSpecificSKP(compressedPictureData, helper);
126 }
127}
128
129void DDLTileHelper::createDDLsInParallel() {
130#if 1
Robert Phillipsf18c7562018-06-13 09:01:36 -0400131 SkTaskGroup().batch(fTiles.count(), [&](int i) { fTiles[i].createDDL(); });
132 SkTaskGroup().wait();
Robert Phillips96601082018-05-29 16:13:26 -0400133#else
134 // Use this code path to debug w/o threads
135 for (int i = 0; i < fTiles.count(); ++i) {
136 fTiles[i].createDDL();
137 }
138#endif
139
140}
141
142void DDLTileHelper::drawAllTilesAndFlush(GrContext* context, bool flush) {
143 for (int i = 0; i < fTiles.count(); ++i) {
144 fTiles[i].draw();
145 }
146 if (flush) {
147 context->flush();
148 }
149}
150
151void DDLTileHelper::composeAllTiles(SkCanvas* dstCanvas) {
152 for (int i = 0; i < fTiles.count(); ++i) {
153 fTiles[i].compose(dstCanvas);
154 }
155}
156
157void DDLTileHelper::resetAllTiles() {
158 for (int i = 0; i < fTiles.count(); ++i) {
159 fTiles[i].reset();
160 }
161}