blob: 688154ef9f428f58cf8c4dc05bb2c35ba29434d5 [file] [log] [blame]
robertphillips7eacd772014-08-21 13:12:42 -07001/*
2 * Copyright 2014 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 "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBBHFactory.h"
10#include "include/core/SkBlendMode.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkImageInfo.h"
15#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkMultiPictureDraw.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/core/SkPicture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkRRect.h"
22#include "include/core/SkRect.h"
23#include "include/core/SkRefCnt.h"
24#include "include/core/SkScalar.h"
25#include "include/core/SkSize.h"
26#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "include/core/SkTypes.h"
29#include "include/private/SkTArray.h"
30#include "tools/ToolUtils.h"
robertphillips7eacd772014-08-21 13:12:42 -070031
mtkleindbfd7ab2016-09-01 11:24:54 -070032constexpr SkScalar kRoot3Over2 = 0.86602545f; // sin(60)
33constexpr SkScalar kRoot3 = 1.73205081f;
robertphillips7eacd772014-08-21 13:12:42 -070034
mtkleindbfd7ab2016-09-01 11:24:54 -070035constexpr int kHexSide = 30;
36constexpr int kNumHexX = 6;
37constexpr int kNumHexY = 6;
38constexpr int kPicWidth = kNumHexX * kHexSide;
39constexpr int kPicHeight = (int)((kNumHexY - 0.5f) * 2 * kHexSide * kRoot3Over2 + 0.5f);
40constexpr SkScalar kInset = 20.0f;
41constexpr int kNumPictures = 4;
robertphillips3bc25e72014-09-19 08:56:09 -070042
mtkleindbfd7ab2016-09-01 11:24:54 -070043constexpr int kTriSide = 40;
robertphillips7eacd772014-08-21 13:12:42 -070044
45// Create a hexagon centered at (originX, originY)
46static SkPath make_hex_path(SkScalar originX, SkScalar originY) {
47 SkPath hex;
48 hex.moveTo(originX-kHexSide, originY);
49 hex.rLineTo(SkScalarHalf(kHexSide), kRoot3Over2 * kHexSide);
50 hex.rLineTo(SkIntToScalar(kHexSide), 0);
51 hex.rLineTo(SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
52 hex.rLineTo(-SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
53 hex.rLineTo(-SkIntToScalar(kHexSide), 0);
54 hex.close();
55 return hex;
56}
57
58// Make a picture that is a tiling of the plane with stroked hexagons where
59// each hexagon is in its own layer. The layers are to exercise Ganesh's
60// layer hoisting.
reedca2622b2016-03-18 07:25:55 -070061static sk_sp<SkPicture> make_hex_plane_picture(SkColor fillColor) {
robertphillips7eacd772014-08-21 13:12:42 -070062
63 // Create a hexagon with its center at the origin
64 SkPath hex = make_hex_path(0, 0);
65
66 SkPaint fill;
67 fill.setStyle(SkPaint::kFill_Style);
68 fill.setColor(fillColor);
69
70 SkPaint stroke;
71 stroke.setStyle(SkPaint::kStroke_Style);
72 stroke.setStrokeWidth(3);
73
74 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -080075 SkRTreeFactory bbhFactory;
robertphillips7eacd772014-08-21 13:12:42 -070076
piotaixrb5fae932014-09-24 13:03:30 -070077 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -080078 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -070079 &bbhFactory);
robertphillips7eacd772014-08-21 13:12:42 -070080
81 SkScalar xPos, yPos = 0;
82
83 for (int y = 0; y < kNumHexY; ++y) {
84 xPos = 0;
85
86 for (int x = 0; x < kNumHexX; ++x) {
halcanary96fcdcc2015-08-27 07:41:13 -070087 canvas->saveLayer(nullptr, nullptr);
robertphillips7eacd772014-08-21 13:12:42 -070088 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
89 canvas->drawPath(hex, fill);
90 canvas->drawPath(hex, stroke);
91 canvas->restore();
92
93 xPos += 1.5f * kHexSide;
94 }
95
96 yPos += 2 * kHexSide * kRoot3Over2;
97 }
98
reedca2622b2016-03-18 07:25:55 -070099 return recorder.finishRecordingAsPicture();
robertphillips7eacd772014-08-21 13:12:42 -0700100}
101
robertphillipsb1fc64b2014-10-02 08:32:43 -0700102// Create a picture that consists of a single large layer that is tiled
103// with hexagons.
104// This is intended to exercise the layer hoisting code's clip handling (in
105// tile mode).
reedca2622b2016-03-18 07:25:55 -0700106static sk_sp<SkPicture> make_single_layer_hex_plane_picture() {
robertphillipsb1fc64b2014-10-02 08:32:43 -0700107
108 // Create a hexagon with its center at the origin
109 SkPath hex = make_hex_path(0, 0);
110
111 SkPaint whiteFill;
112 whiteFill.setStyle(SkPaint::kFill_Style);
113 whiteFill.setColor(SK_ColorWHITE);
114
115 SkPaint greyFill;
116 greyFill.setStyle(SkPaint::kFill_Style);
Mike Kleind46dce32018-08-16 10:17:03 -0400117 greyFill.setColor(SK_ColorLTGRAY);
robertphillipsb1fc64b2014-10-02 08:32:43 -0700118
119 SkPaint stroke;
120 stroke.setStyle(SkPaint::kStroke_Style);
121 stroke.setStrokeWidth(3);
122
123 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800124 SkRTreeFactory bbhFactory;
robertphillipsb1fc64b2014-10-02 08:32:43 -0700125
mtkleindbfd7ab2016-09-01 11:24:54 -0700126 constexpr SkScalar kBig = 10000.0f;
robertphillipsdda54452016-07-13 13:27:16 -0700127 SkCanvas* canvas = recorder.beginRecording(kBig, kBig, &bbhFactory);
robertphillipsb1fc64b2014-10-02 08:32:43 -0700128
halcanary96fcdcc2015-08-27 07:41:13 -0700129 canvas->saveLayer(nullptr, nullptr);
robertphillips1564dde2014-10-06 11:50:31 -0700130
robertphillipsb1fc64b2014-10-02 08:32:43 -0700131 SkScalar xPos = 0.0f, yPos = 0.0f;
132
133 for (int y = 0; yPos < kBig; ++y) {
134 xPos = 0;
135
136 for (int x = 0; xPos < kBig; ++x) {
137 canvas->save();
138 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
139 // The color of the filled hex is swapped to yield a different
140 // pattern in each tile. This allows an error in layer hoisting (e.g.,
141 // the clip isn't blocking cache reuse) to cause a visual discrepancy.
142 canvas->drawPath(hex, ((x+y) % 3) ? whiteFill : greyFill);
143 canvas->drawPath(hex, stroke);
144 canvas->restore();
145
146 xPos += 1.5f * kHexSide;
147 }
148
149 yPos += 2 * kHexSide * kRoot3Over2;
150 }
151
robertphillips1564dde2014-10-06 11:50:31 -0700152 canvas->restore();
153
reedca2622b2016-03-18 07:25:55 -0700154 return recorder.finishRecordingAsPicture();
robertphillipsb1fc64b2014-10-02 08:32:43 -0700155}
156
robertphillips3bc25e72014-09-19 08:56:09 -0700157// Make an equilateral triangle path with its top corner at (originX, originY)
158static SkPath make_tri_path(SkScalar originX, SkScalar originY) {
159 SkPath tri;
160 tri.moveTo(originX, originY);
161 tri.rLineTo(SkScalarHalf(kTriSide), 1.5f * kTriSide / kRoot3);
162 tri.rLineTo(-kTriSide, 0);
163 tri.close();
164 return tri;
165}
166
reedca2622b2016-03-18 07:25:55 -0700167static sk_sp<SkPicture> make_tri_picture() {
robertphillipsaa0c8372014-09-29 05:07:39 -0700168 SkPath tri = make_tri_path(SkScalarHalf(kTriSide), 0);
robertphillips3bc25e72014-09-19 08:56:09 -0700169
170 SkPaint fill;
171 fill.setStyle(SkPaint::kFill_Style);
Mike Kleind46dce32018-08-16 10:17:03 -0400172 fill.setColor(SK_ColorLTGRAY);
robertphillips3bc25e72014-09-19 08:56:09 -0700173
174 SkPaint stroke;
175 stroke.setStyle(SkPaint::kStroke_Style);
176 stroke.setStrokeWidth(3);
177
178 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800179 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700180
181 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800182 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700183 &bbhFactory);
robertphillips7f1ed832014-10-03 07:29:20 -0700184 SkRect r = tri.getBounds();
185 r.outset(2.0f, 2.0f); // outset for stroke
186 canvas->clipRect(r);
robertphillips3bc25e72014-09-19 08:56:09 -0700187 // The saveLayer/restore block is to exercise layer hoisting
halcanary96fcdcc2015-08-27 07:41:13 -0700188 canvas->saveLayer(nullptr, nullptr);
robertphillips3bc25e72014-09-19 08:56:09 -0700189 canvas->drawPath(tri, fill);
190 canvas->drawPath(tri, stroke);
191 canvas->restore();
192
reedca2622b2016-03-18 07:25:55 -0700193 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700194}
195
reedca2622b2016-03-18 07:25:55 -0700196static sk_sp<SkPicture> make_sub_picture(const SkPicture* tri) {
robertphillips3bc25e72014-09-19 08:56:09 -0700197 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800198 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700199
200 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800201 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700202 &bbhFactory);
robertphillips3bc25e72014-09-19 08:56:09 -0700203
204 canvas->scale(1.0f/2.0f, 1.0f/2.0f);
205
robertphillips3bc25e72014-09-19 08:56:09 -0700206 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700207 canvas->translate(SkScalarHalf(kTriSide), 0);
robertphillips3bc25e72014-09-19 08:56:09 -0700208 canvas->drawPicture(tri);
209 canvas->restore();
210
211 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700212 canvas->translate(SkIntToScalar(kTriSide), 1.5f * kTriSide / kRoot3);
213 canvas->drawPicture(tri);
214 canvas->restore();
215
216 canvas->save();
217 canvas->translate(0, 1.5f * kTriSide / kRoot3);
robertphillips3bc25e72014-09-19 08:56:09 -0700218 canvas->drawPicture(tri);
219 canvas->restore();
220
reedca2622b2016-03-18 07:25:55 -0700221 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700222}
223
224// Create a Sierpinkski-like picture that starts with a top row with a picture
piotaixrb5fae932014-09-24 13:03:30 -0700225// that just contains a triangle. Subsequent rows take the prior row's picture,
robertphillips3bc25e72014-09-19 08:56:09 -0700226// shrinks it and replicates it 3 times then draws and appropriate number of
227// copies of it.
reedca2622b2016-03-18 07:25:55 -0700228static sk_sp<SkPicture> make_sierpinski_picture() {
229 sk_sp<SkPicture> pic(make_tri_picture());
robertphillips3bc25e72014-09-19 08:56:09 -0700230
231 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800232 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700233
234 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800235 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700236 &bbhFactory);
robertphillips3bc25e72014-09-19 08:56:09 -0700237
mtkleindbfd7ab2016-09-01 11:24:54 -0700238 constexpr int kNumLevels = 4;
robertphillips3bc25e72014-09-19 08:56:09 -0700239 for (int i = 0; i < kNumLevels; ++i) {
240 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700241 canvas->translate(kPicWidth/2 - (i+1) * (kTriSide/2.0f), 0.0f);
robertphillips3bc25e72014-09-19 08:56:09 -0700242 for (int j = 0; j < i+1; ++j) {
243 canvas->drawPicture(pic);
244 canvas->translate(SkIntToScalar(kTriSide), 0);
245 }
246 canvas->restore();
247
reedca2622b2016-03-18 07:25:55 -0700248 pic = make_sub_picture(pic.get());
robertphillips3bc25e72014-09-19 08:56:09 -0700249
250 canvas->translate(0, 1.5f * kTriSide / kRoot3);
251 }
252
reedca2622b2016-03-18 07:25:55 -0700253 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700254}
255
reede8f30622016-03-23 18:59:25 -0700256static sk_sp<SkSurface> create_compat_surface(SkCanvas* canvas, int width, int height) {
robertphillips7eacd772014-08-21 13:12:42 -0700257 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
258
Mike Kleinea3f0142019-03-20 11:12:10 -0500259 return ToolUtils::makeSurface(canvas, info);
robertphillips7eacd772014-08-21 13:12:42 -0700260}
261
262// This class stores the information required to compose all the result
263// fragments potentially generated by the MultiPictureDraw object
264class ComposeStep {
265public:
reede8f30622016-03-23 18:59:25 -0700266 ComposeStep() : fX(0.0f), fY(0.0f), fPaint(nullptr) { }
halcanary385fe4d2015-08-26 13:07:48 -0700267 ~ComposeStep() {
halcanary385fe4d2015-08-26 13:07:48 -0700268 delete fPaint;
269 }
robertphillips7eacd772014-08-21 13:12:42 -0700270
reede8f30622016-03-23 18:59:25 -0700271 sk_sp<SkSurface> fSurf;
robertphillips7eacd772014-08-21 13:12:42 -0700272 SkScalar fX;
273 SkScalar fY;
274 SkPaint* fPaint;
275};
276
robertphillips3bc25e72014-09-19 08:56:09 -0700277typedef void (*PFContentMtd)(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]);
robertphillips7eacd772014-08-21 13:12:42 -0700278
279// Just a single picture with no clip
robertphillips3bc25e72014-09-19 08:56:09 -0700280static void no_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700281 canvas->drawPicture(pictures[0]);
282}
283
284// Two pictures with a rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700285static void rect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700286 canvas->drawPicture(pictures[0]);
287
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700288 SkRect rect = pictures[0]->cullRect();
robertphillips7eacd772014-08-21 13:12:42 -0700289 rect.inset(kInset, kInset);
290
291 canvas->clipRect(rect);
292
293 canvas->drawPicture(pictures[1]);
294}
295
296// Two pictures with a round rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700297static void rrect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700298 canvas->drawPicture(pictures[0]);
299
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700300 SkRect rect = pictures[0]->cullRect();
robertphillips7eacd772014-08-21 13:12:42 -0700301 rect.inset(kInset, kInset);
piotaixrb5fae932014-09-24 13:03:30 -0700302
robertphillips7eacd772014-08-21 13:12:42 -0700303 SkRRect rrect;
304 rrect.setRectXY(rect, kInset, kInset);
305
306 canvas->clipRRect(rrect);
307
308 canvas->drawPicture(pictures[1]);
309}
310
311// Two pictures with a clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700312static void path_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700313 canvas->drawPicture(pictures[0]);
314
315 // Create a hexagon centered on the middle of the hex grid
316 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
317
318 canvas->clipPath(hex);
319
320 canvas->drawPicture(pictures[1]);
321}
322
323// Two pictures with an inverse clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700324static void invpath_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700325 canvas->drawPicture(pictures[0]);
326
327 // Create a hexagon centered on the middle of the hex grid
328 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
329 hex.setFillType(SkPath::kInverseEvenOdd_FillType);
330
331 canvas->clipPath(hex);
332
333 canvas->drawPicture(pictures[1]);
334}
335
robertphillips3bc25e72014-09-19 08:56:09 -0700336// Reuse a single base (triangular) picture a _lot_ (rotated, scaled and translated).
337static void sierpinski(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
338 canvas->save();
robertphillips3bc25e72014-09-19 08:56:09 -0700339 canvas->drawPicture(pictures[2]);
340
341 canvas->rotate(180.0f);
robertphillipsaa0c8372014-09-29 05:07:39 -0700342 canvas->translate(-SkIntToScalar(kPicWidth), -SkIntToScalar(kPicHeight));
robertphillips3bc25e72014-09-19 08:56:09 -0700343 canvas->drawPicture(pictures[2]);
344 canvas->restore();
345}
346
robertphillipsb1fc64b2014-10-02 08:32:43 -0700347static void big_layer(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
348 canvas->drawPicture(pictures[3]);
349}
350
mtkleindbfd7ab2016-09-01 11:24:54 -0700351constexpr PFContentMtd gContentMthds[] = {
robertphillips7eacd772014-08-21 13:12:42 -0700352 no_clip,
353 rect_clip,
354 rrect_clip,
355 path_clip,
robertphillips3bc25e72014-09-19 08:56:09 -0700356 invpath_clip,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700357 sierpinski,
358 big_layer,
robertphillips7eacd772014-08-21 13:12:42 -0700359};
360
361static void create_content(SkMultiPictureDraw* mpd, PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700362 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700363 SkCanvas* dest, const SkMatrix& xform) {
reedca2622b2016-03-18 07:25:55 -0700364 sk_sp<SkPicture> composite;
robertphillips7eacd772014-08-21 13:12:42 -0700365
366 {
367 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800368 SkRTreeFactory bbhFactory;
robertphillips7eacd772014-08-21 13:12:42 -0700369
piotaixrb5fae932014-09-24 13:03:30 -0700370 SkCanvas* pictureCanvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800371 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700372 &bbhFactory);
robertphillips7eacd772014-08-21 13:12:42 -0700373
374 (*pfGen)(pictureCanvas, pictures);
375
reedca2622b2016-03-18 07:25:55 -0700376 composite = recorder.finishRecordingAsPicture();
robertphillips7eacd772014-08-21 13:12:42 -0700377 }
378
reedca2622b2016-03-18 07:25:55 -0700379 mpd->add(dest, composite.get(), &xform);
robertphillips7eacd772014-08-21 13:12:42 -0700380}
381
piotaixrb5fae932014-09-24 13:03:30 -0700382typedef void(*PFLayoutMtd)(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
robertphillips3bc25e72014-09-19 08:56:09 -0700383 PFContentMtd pfGen, const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700384 SkTArray<ComposeStep>* composeSteps);
385
386// Draw the content into a single canvas
piotaixrb5fae932014-09-24 13:03:30 -0700387static void simple(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
388 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700389 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700390 SkTArray<ComposeStep> *composeSteps) {
391
392 ComposeStep& step = composeSteps->push_back();
393
bsalomon892f31a2014-08-21 14:40:36 -0700394 step.fSurf = create_compat_surface(finalCanvas, kPicWidth, kPicHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700395
396 SkCanvas* subCanvas = step.fSurf->getCanvas();
397
398 create_content(mpd, pfGen, pictures, subCanvas, SkMatrix::I());
399}
400
401// Draw the content into multiple canvases/tiles
402static void tiled(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
piotaixrb5fae932014-09-24 13:03:30 -0700403 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700404 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700405 SkTArray<ComposeStep> *composeSteps) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700406 const int kNumTilesX = 2;
407 const int kNumTilesY = 2;
408 const int kTileWidth = kPicWidth / kNumTilesX;
409 const int kTileHeight = kPicHeight / kNumTilesY;
robertphillips7eacd772014-08-21 13:12:42 -0700410
411 SkASSERT(kPicWidth == kNumTilesX * kTileWidth);
412 SkASSERT(kPicHeight == kNumTilesY * kTileHeight);
413
mtkleindbfd7ab2016-09-01 11:24:54 -0700414 const SkColor colors[kNumTilesX][kNumTilesY] = {
piotaixrb5fae932014-09-24 13:03:30 -0700415 { SK_ColorCYAN, SK_ColorMAGENTA },
robertphillips7eacd772014-08-21 13:12:42 -0700416 { SK_ColorYELLOW, SK_ColorGREEN }
417 };
418
419 for (int y = 0; y < kNumTilesY; ++y) {
420 for (int x = 0; x < kNumTilesX; ++x) {
421 ComposeStep& step = composeSteps->push_back();
422
423 step.fX = SkIntToScalar(x*kTileWidth);
424 step.fY = SkIntToScalar(y*kTileHeight);
halcanary385fe4d2015-08-26 13:07:48 -0700425 step.fPaint = new SkPaint;
robertphillips7eacd772014-08-21 13:12:42 -0700426 step.fPaint->setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400427 SkColorFilters::Blend(colors[x][y], SkBlendMode::kModulate));
robertphillips7eacd772014-08-21 13:12:42 -0700428
bsalomon892f31a2014-08-21 14:40:36 -0700429 step.fSurf = create_compat_surface(finalCanvas, kTileWidth, kTileHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700430
431 SkCanvas* subCanvas = step.fSurf->getCanvas();
432
halcanary9d524f22016-03-29 09:03:52 -0700433 const SkMatrix trans = SkMatrix::MakeTrans(-SkIntToScalar(x*kTileWidth),
robertphillips1d24b8d2015-03-26 19:57:08 -0700434 -SkIntToScalar(y*kTileHeight));
robertphillips7eacd772014-08-21 13:12:42 -0700435
436 create_content(mpd, pfGen, pictures, subCanvas, trans);
437 }
438 }
439}
440
mtkleindbfd7ab2016-09-01 11:24:54 -0700441constexpr PFLayoutMtd gLayoutMthds[] = { simple, tiled };
robertphillips7eacd772014-08-21 13:12:42 -0700442
443namespace skiagm {
444 /**
445 * This GM exercises the SkMultiPictureDraw object. It tests the
446 * cross product of:
447 * tiled vs. all-at-once rendering (e.g., into many or just 1 canvas)
448 * different clips (e.g., none, rect, rrect)
449 * single vs. multiple pictures (e.g., normal vs. picture-pile-style content)
450 */
451 class MultiPictureDraw : public GM {
452 public:
453 enum Content {
454 kNoClipSingle_Content,
455 kRectClipMulti_Content,
456 kRRectClipMulti_Content,
457 kPathClipMulti_Content,
458 kInvPathClipMulti_Content,
robertphillips3bc25e72014-09-19 08:56:09 -0700459 kSierpinski_Content,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700460 kBigLayer_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700461
robertphillipsb1fc64b2014-10-02 08:32:43 -0700462 kLast_Content = kBigLayer_Content
robertphillips7eacd772014-08-21 13:12:42 -0700463 };
464
mtkleindbfd7ab2016-09-01 11:24:54 -0700465 const int kContentCnt = kLast_Content + 1;
robertphillips7eacd772014-08-21 13:12:42 -0700466
467 enum Layout {
468 kSimple_Layout,
469 kTiled_Layout,
470
471 kLast_Layout = kTiled_Layout
472 };
473
mtkleindbfd7ab2016-09-01 11:24:54 -0700474 const int kLayoutCnt = kLast_Layout + 1;
robertphillips7eacd772014-08-21 13:12:42 -0700475
476 MultiPictureDraw(Content content, Layout layout) : fContent(content), fLayout(layout) {
477 SkASSERT(SK_ARRAY_COUNT(gLayoutMthds) == kLayoutCnt);
478 SkASSERT(SK_ARRAY_COUNT(gContentMthds) == kContentCnt);
479
robertphillips3bc25e72014-09-19 08:56:09 -0700480 for (int i = 0; i < kNumPictures; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -0700481 fPictures[i] = nullptr;
robertphillips3bc25e72014-09-19 08:56:09 -0700482 }
robertphillips7eacd772014-08-21 13:12:42 -0700483 }
484
Brian Salomond3b65972017-03-22 12:05:03 -0400485 ~MultiPictureDraw() override {
robertphillips3bc25e72014-09-19 08:56:09 -0700486 for (int i = 0; i < kNumPictures; ++i) {
487 SkSafeUnref(fPictures[i]);
488 }
robertphillips7eacd772014-08-21 13:12:42 -0700489 }
490
491 protected:
492 Content fContent;
493 Layout fLayout;
robertphillips3bc25e72014-09-19 08:56:09 -0700494 const SkPicture* fPictures[kNumPictures];
robertphillips7eacd772014-08-21 13:12:42 -0700495
mtklein36352bf2015-03-25 18:17:31 -0700496 void onOnceBeforeDraw() override {
reedca2622b2016-03-18 07:25:55 -0700497 fPictures[0] = make_hex_plane_picture(SK_ColorWHITE).release();
Mike Kleind46dce32018-08-16 10:17:03 -0400498 fPictures[1] = make_hex_plane_picture(SK_ColorGRAY).release();
reedca2622b2016-03-18 07:25:55 -0700499 fPictures[2] = make_sierpinski_picture().release();
500 fPictures[3] = make_single_layer_hex_plane_picture().release();
robertphillips7eacd772014-08-21 13:12:42 -0700501 }
502
mtklein36352bf2015-03-25 18:17:31 -0700503 void onDraw(SkCanvas* canvas) override {
robertphillips7eacd772014-08-21 13:12:42 -0700504 SkMultiPictureDraw mpd;
505 SkTArray<ComposeStep> composeSteps;
506
507 // Fill up the MultiPictureDraw
piotaixrb5fae932014-09-24 13:03:30 -0700508 (*gLayoutMthds[fLayout])(canvas, &mpd,
509 gContentMthds[fContent],
robertphillips7eacd772014-08-21 13:12:42 -0700510 fPictures, &composeSteps);
511
512 mpd.draw();
513
514 // Compose all the drawn canvases into the final canvas
515 for (int i = 0; i < composeSteps.count(); ++i) {
516 const ComposeStep& step = composeSteps[i];
517
reed9ce9d672016-03-17 10:51:11 -0700518 canvas->drawImage(step.fSurf->makeImageSnapshot().get(),
519 step.fX, step.fY, step.fPaint);
robertphillips7eacd772014-08-21 13:12:42 -0700520 }
521 }
522
mtklein36352bf2015-03-25 18:17:31 -0700523 SkISize onISize() override { return SkISize::Make(kPicWidth, kPicHeight); }
robertphillips7eacd772014-08-21 13:12:42 -0700524
mtklein36352bf2015-03-25 18:17:31 -0700525 SkString onShortName() override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700526 const char* gContentNames[] = {
halcanary9d524f22016-03-29 09:03:52 -0700527 "noclip", "rectclip", "rrectclip", "pathclip",
robertphillipsb1fc64b2014-10-02 08:32:43 -0700528 "invpathclip", "sierpinski", "biglayer"
robertphillips7eacd772014-08-21 13:12:42 -0700529 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700530 const char* gLayoutNames[] = { "simple", "tiled" };
robertphillips7eacd772014-08-21 13:12:42 -0700531
532 SkASSERT(SK_ARRAY_COUNT(gLayoutNames) == kLayoutCnt);
533 SkASSERT(SK_ARRAY_COUNT(gContentNames) == kContentCnt);
534
535 SkString name("multipicturedraw_");
536
537 name.append(gContentNames[fContent]);
538 name.append("_");
539 name.append(gLayoutNames[fLayout]);
540 return name;
541 }
542
mtklein36352bf2015-03-25 18:17:31 -0700543 bool runAsBench() const override { return true; }
robertphillips7eacd772014-08-21 13:12:42 -0700544
545 private:
546 typedef GM INHERITED;
547 };
548
halcanary385fe4d2015-08-26 13:07:48 -0700549 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
550 MultiPictureDraw::kSimple_Layout);)
551 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
552 MultiPictureDraw::kSimple_Layout);)
553 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
554 MultiPictureDraw::kSimple_Layout);)
555 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
556 MultiPictureDraw::kSimple_Layout);)
557 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
558 MultiPictureDraw::kSimple_Layout);)
559 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
560 MultiPictureDraw::kSimple_Layout);)
561 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
562 MultiPictureDraw::kSimple_Layout);)
robertphillips7eacd772014-08-21 13:12:42 -0700563
halcanary385fe4d2015-08-26 13:07:48 -0700564 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
565 MultiPictureDraw::kTiled_Layout);)
566 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
567 MultiPictureDraw::kTiled_Layout);)
568 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
569 MultiPictureDraw::kTiled_Layout);)
570 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
571 MultiPictureDraw::kTiled_Layout);)
572 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
573 MultiPictureDraw::kTiled_Layout);)
574 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
575 MultiPictureDraw::kTiled_Layout);)
576 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
577 MultiPictureDraw::kTiled_Layout);)
robertphillips7eacd772014-08-21 13:12:42 -0700578}