blob: b93b5ff870e0f19639649b472010723660b2ac09 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/DDLTileHelper.h"
Robert Phillips96601082018-05-29 16:13:26 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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 "src/core/SkDeferredDisplayListPriv.h"
16#include "src/core/SkTaskGroup.h"
17#include "src/image/SkImage_Gpu.h"
18#include "tools/DDLPromiseImageHelper.h"
Robert Phillips96601082018-05-29 16:13:26 -040019
20DDLTileHelper::TileData::TileData(sk_sp<SkSurface> s, const SkIRect& clip)
21 : fSurface(std::move(s))
22 , fClip(clip) {
23 SkAssertResult(fSurface->characterize(&fCharacterization));
24}
25
26void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData,
27 const DDLPromiseImageHelper& helper) {
Robert Phillips7a3197b2018-09-26 21:18:23 +000028 SkASSERT(!fReconstitutedPicture);
Robert Phillips96601082018-05-29 16:13:26 -040029
Robert Phillips7a3197b2018-09-26 21:18:23 +000030 // This is bending the DDLRecorder contract! The promise images in the SKP should be
31 // created by the same recorder used to create the matching DDL.
32 SkDeferredDisplayListRecorder recorder(fCharacterization);
Robert Phillips96601082018-05-29 16:13:26 -040033
Robert Phillips7a3197b2018-09-26 21:18:23 +000034 fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages);
Robert Phillipse8e2bb12018-09-27 14:26:47 -040035
36 std::unique_ptr<SkDeferredDisplayList> ddl = recorder.detach();
Robert Phillipsbfa76f22018-10-03 12:12:26 -040037 if (ddl->priv().numOpLists()) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -040038 // TODO: remove this once skbug.com/8424 is fixed. If the DDL resulting from the
39 // reinflation of the SKPs contains opLists that means some image subset operation
40 // created a draw.
41 fReconstitutedPicture.reset();
42 }
Robert Phillips96601082018-05-29 16:13:26 -040043}
44
45void DDLTileHelper::TileData::createDDL() {
Robert Phillips96601082018-05-29 16:13:26 -040046 SkASSERT(!fDisplayList);
47
Robert Phillips7a3197b2018-09-26 21:18:23 +000048 SkDeferredDisplayListRecorder recorder(fCharacterization);
49
50 // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called.
51 // Maybe set it up in the ctor?
52 SkCanvas* subCanvas = recorder.getCanvas();
53
54 // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's
55 // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct
56 // one now.
57 for (int i = 0; i < fPromiseImages.count(); ++i) {
58 GrContext* newContext = subCanvas->getGrContext();
59
60 if (fPromiseImages[i]->isTextureBacked()) {
Jim Van Verth21bd60d2018-10-12 15:00:20 -040061 SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get();
Robert Phillips7a3197b2018-09-26 21:18:23 +000062 gpuImage->resetContext(sk_ref_sp(newContext));
63 }
64 }
Robert Phillips96601082018-05-29 16:13:26 -040065
66 subCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
67 subCanvas->translate(-fClip.fLeft, -fClip.fTop);
68
69 // Note: in this use case we only render a picture to the deferred canvas
70 // but, more generally, clients will use arbitrary draw calls.
Robert Phillipse8e2bb12018-09-27 14:26:47 -040071 if (fReconstitutedPicture) {
72 subCanvas->drawPicture(fReconstitutedPicture);
73 }
Robert Phillips96601082018-05-29 16:13:26 -040074
Robert Phillips7a3197b2018-09-26 21:18:23 +000075 fDisplayList = recorder.detach();
Robert Phillips96601082018-05-29 16:13:26 -040076}
77
78void DDLTileHelper::TileData::draw() {
79 SkASSERT(fDisplayList);
80
81 fSurface->draw(fDisplayList.get());
82}
83
84void DDLTileHelper::TileData::compose(SkCanvas* dst) {
85 sk_sp<SkImage> img = fSurface->makeImageSnapshot();
86 dst->save();
87 dst->clipRect(SkRect::Make(fClip));
88 dst->drawImage(std::move(img), fClip.fLeft, fClip.fTop);
89 dst->restore();
90}
91
92void DDLTileHelper::TileData::reset() {
93 // TODO: when DDLs are re-renderable we don't need to do this
94 fDisplayList = nullptr;
95}
96
97///////////////////////////////////////////////////////////////////////////////////////////////////
98
99DDLTileHelper::DDLTileHelper(SkCanvas* canvas, const SkIRect& viewport, int numDivisions)
100 : fNumDivisions(numDivisions) {
101 SkASSERT(fNumDivisions > 0);
102 fTiles.reserve(fNumDivisions*fNumDivisions);
103
104 int xTileSize = viewport.width()/fNumDivisions;
105 int yTileSize = viewport.height()/fNumDivisions;
106
107 // Create the destination tiles
108 for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) {
109 int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff;
110
111 for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) {
112 int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff;
113
114 SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
115
116 SkASSERT(viewport.contains(clip));
117
118 SkImageInfo tileII = SkImageInfo::MakeN32Premul(xSize, ySize);
119
120 sk_sp<SkSurface> tileSurface = canvas->makeSurface(tileII);
121
122 // TODO: this is here to deal w/ a resource allocator bug (skbug.com/8007). If all
123 // the DDLs are flushed at the same time (w/o the composition draws) the allocator
124 // feels free to reuse the backing GrSurfaces!
125 tileSurface->flush();
126
127 fTiles.push_back(TileData(std::move(tileSurface), clip));
128 }
129 }
130}
131
132void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData,
133 const DDLPromiseImageHelper& helper) {
134 for (int i = 0; i < fTiles.count(); ++i) {
135 fTiles[i].createTileSpecificSKP(compressedPictureData, helper);
136 }
137}
138
139void DDLTileHelper::createDDLsInParallel() {
140#if 1
Robert Phillipsf18c7562018-06-13 09:01:36 -0400141 SkTaskGroup().batch(fTiles.count(), [&](int i) { fTiles[i].createDDL(); });
142 SkTaskGroup().wait();
Robert Phillips96601082018-05-29 16:13:26 -0400143#else
144 // Use this code path to debug w/o threads
145 for (int i = 0; i < fTiles.count(); ++i) {
146 fTiles[i].createDDL();
147 }
148#endif
149
150}
151
152void DDLTileHelper::drawAllTilesAndFlush(GrContext* context, bool flush) {
153 for (int i = 0; i < fTiles.count(); ++i) {
154 fTiles[i].draw();
155 }
156 if (flush) {
157 context->flush();
158 }
159}
160
161void DDLTileHelper::composeAllTiles(SkCanvas* dstCanvas) {
162 for (int i = 0; i < fTiles.count(); ++i) {
163 fTiles[i].compose(dstCanvas);
164 }
165}
166
167void DDLTileHelper::resetAllTiles() {
168 for (int i = 0; i < fTiles.count(); ++i) {
169 fTiles[i].reset();
170 }
171}