blob: 4dd0139cd23476da2ff86fcf82f3fe44cd567aa1 [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
robertphillips7eacd772014-08-21 13:12:42 -070010
11#include "SkColorFilter.h"
12#include "SkMultiPictureDraw.h"
Mike Reedc090c642017-05-16 10:39:06 -040013#include "SkPath.h"
robertphillips7eacd772014-08-21 13:12:42 -070014#include "SkPictureRecorder.h"
15#include "SkSurface.h"
16
mtkleindbfd7ab2016-09-01 11:24:54 -070017constexpr SkScalar kRoot3Over2 = 0.86602545f; // sin(60)
18constexpr SkScalar kRoot3 = 1.73205081f;
robertphillips7eacd772014-08-21 13:12:42 -070019
mtkleindbfd7ab2016-09-01 11:24:54 -070020constexpr int kHexSide = 30;
21constexpr int kNumHexX = 6;
22constexpr int kNumHexY = 6;
23constexpr int kPicWidth = kNumHexX * kHexSide;
24constexpr int kPicHeight = (int)((kNumHexY - 0.5f) * 2 * kHexSide * kRoot3Over2 + 0.5f);
25constexpr SkScalar kInset = 20.0f;
26constexpr int kNumPictures = 4;
robertphillips3bc25e72014-09-19 08:56:09 -070027
mtkleindbfd7ab2016-09-01 11:24:54 -070028constexpr int kTriSide = 40;
robertphillips7eacd772014-08-21 13:12:42 -070029
30// Create a hexagon centered at (originX, originY)
31static SkPath make_hex_path(SkScalar originX, SkScalar originY) {
32 SkPath hex;
33 hex.moveTo(originX-kHexSide, originY);
34 hex.rLineTo(SkScalarHalf(kHexSide), kRoot3Over2 * kHexSide);
35 hex.rLineTo(SkIntToScalar(kHexSide), 0);
36 hex.rLineTo(SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
37 hex.rLineTo(-SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
38 hex.rLineTo(-SkIntToScalar(kHexSide), 0);
39 hex.close();
40 return hex;
41}
42
43// Make a picture that is a tiling of the plane with stroked hexagons where
44// each hexagon is in its own layer. The layers are to exercise Ganesh's
45// layer hoisting.
reedca2622b2016-03-18 07:25:55 -070046static sk_sp<SkPicture> make_hex_plane_picture(SkColor fillColor) {
robertphillips7eacd772014-08-21 13:12:42 -070047
48 // Create a hexagon with its center at the origin
49 SkPath hex = make_hex_path(0, 0);
50
51 SkPaint fill;
52 fill.setStyle(SkPaint::kFill_Style);
53 fill.setColor(fillColor);
54
55 SkPaint stroke;
56 stroke.setStyle(SkPaint::kStroke_Style);
57 stroke.setStrokeWidth(3);
58
59 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -080060 SkRTreeFactory bbhFactory;
robertphillips7eacd772014-08-21 13:12:42 -070061
piotaixrb5fae932014-09-24 13:03:30 -070062 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -080063 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -070064 &bbhFactory);
robertphillips7eacd772014-08-21 13:12:42 -070065
66 SkScalar xPos, yPos = 0;
67
68 for (int y = 0; y < kNumHexY; ++y) {
69 xPos = 0;
70
71 for (int x = 0; x < kNumHexX; ++x) {
halcanary96fcdcc2015-08-27 07:41:13 -070072 canvas->saveLayer(nullptr, nullptr);
robertphillips7eacd772014-08-21 13:12:42 -070073 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
74 canvas->drawPath(hex, fill);
75 canvas->drawPath(hex, stroke);
76 canvas->restore();
77
78 xPos += 1.5f * kHexSide;
79 }
80
81 yPos += 2 * kHexSide * kRoot3Over2;
82 }
83
reedca2622b2016-03-18 07:25:55 -070084 return recorder.finishRecordingAsPicture();
robertphillips7eacd772014-08-21 13:12:42 -070085}
86
robertphillipsb1fc64b2014-10-02 08:32:43 -070087// Create a picture that consists of a single large layer that is tiled
88// with hexagons.
89// This is intended to exercise the layer hoisting code's clip handling (in
90// tile mode).
reedca2622b2016-03-18 07:25:55 -070091static sk_sp<SkPicture> make_single_layer_hex_plane_picture() {
robertphillipsb1fc64b2014-10-02 08:32:43 -070092
93 // Create a hexagon with its center at the origin
94 SkPath hex = make_hex_path(0, 0);
95
96 SkPaint whiteFill;
97 whiteFill.setStyle(SkPaint::kFill_Style);
98 whiteFill.setColor(SK_ColorWHITE);
99
100 SkPaint greyFill;
101 greyFill.setStyle(SkPaint::kFill_Style);
caryclarkf597c422015-07-28 10:37:53 -0700102 greyFill.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
robertphillipsb1fc64b2014-10-02 08:32:43 -0700103
104 SkPaint stroke;
105 stroke.setStyle(SkPaint::kStroke_Style);
106 stroke.setStrokeWidth(3);
107
108 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800109 SkRTreeFactory bbhFactory;
robertphillipsb1fc64b2014-10-02 08:32:43 -0700110
mtkleindbfd7ab2016-09-01 11:24:54 -0700111 constexpr SkScalar kBig = 10000.0f;
robertphillipsdda54452016-07-13 13:27:16 -0700112 SkCanvas* canvas = recorder.beginRecording(kBig, kBig, &bbhFactory);
robertphillipsb1fc64b2014-10-02 08:32:43 -0700113
halcanary96fcdcc2015-08-27 07:41:13 -0700114 canvas->saveLayer(nullptr, nullptr);
robertphillips1564dde2014-10-06 11:50:31 -0700115
robertphillipsb1fc64b2014-10-02 08:32:43 -0700116 SkScalar xPos = 0.0f, yPos = 0.0f;
117
118 for (int y = 0; yPos < kBig; ++y) {
119 xPos = 0;
120
121 for (int x = 0; xPos < kBig; ++x) {
122 canvas->save();
123 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
124 // The color of the filled hex is swapped to yield a different
125 // pattern in each tile. This allows an error in layer hoisting (e.g.,
126 // the clip isn't blocking cache reuse) to cause a visual discrepancy.
127 canvas->drawPath(hex, ((x+y) % 3) ? whiteFill : greyFill);
128 canvas->drawPath(hex, stroke);
129 canvas->restore();
130
131 xPos += 1.5f * kHexSide;
132 }
133
134 yPos += 2 * kHexSide * kRoot3Over2;
135 }
136
robertphillips1564dde2014-10-06 11:50:31 -0700137 canvas->restore();
138
reedca2622b2016-03-18 07:25:55 -0700139 return recorder.finishRecordingAsPicture();
robertphillipsb1fc64b2014-10-02 08:32:43 -0700140}
141
robertphillips3bc25e72014-09-19 08:56:09 -0700142// Make an equilateral triangle path with its top corner at (originX, originY)
143static SkPath make_tri_path(SkScalar originX, SkScalar originY) {
144 SkPath tri;
145 tri.moveTo(originX, originY);
146 tri.rLineTo(SkScalarHalf(kTriSide), 1.5f * kTriSide / kRoot3);
147 tri.rLineTo(-kTriSide, 0);
148 tri.close();
149 return tri;
150}
151
reedca2622b2016-03-18 07:25:55 -0700152static sk_sp<SkPicture> make_tri_picture() {
robertphillipsaa0c8372014-09-29 05:07:39 -0700153 SkPath tri = make_tri_path(SkScalarHalf(kTriSide), 0);
robertphillips3bc25e72014-09-19 08:56:09 -0700154
155 SkPaint fill;
156 fill.setStyle(SkPaint::kFill_Style);
caryclarkf597c422015-07-28 10:37:53 -0700157 fill.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
robertphillips3bc25e72014-09-19 08:56:09 -0700158
159 SkPaint stroke;
160 stroke.setStyle(SkPaint::kStroke_Style);
161 stroke.setStrokeWidth(3);
162
163 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800164 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700165
166 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800167 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700168 &bbhFactory);
robertphillips7f1ed832014-10-03 07:29:20 -0700169 SkRect r = tri.getBounds();
170 r.outset(2.0f, 2.0f); // outset for stroke
171 canvas->clipRect(r);
robertphillips3bc25e72014-09-19 08:56:09 -0700172 // The saveLayer/restore block is to exercise layer hoisting
halcanary96fcdcc2015-08-27 07:41:13 -0700173 canvas->saveLayer(nullptr, nullptr);
robertphillips3bc25e72014-09-19 08:56:09 -0700174 canvas->drawPath(tri, fill);
175 canvas->drawPath(tri, stroke);
176 canvas->restore();
177
reedca2622b2016-03-18 07:25:55 -0700178 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700179}
180
reedca2622b2016-03-18 07:25:55 -0700181static sk_sp<SkPicture> make_sub_picture(const SkPicture* tri) {
robertphillips3bc25e72014-09-19 08:56:09 -0700182 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800183 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700184
185 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800186 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700187 &bbhFactory);
robertphillips3bc25e72014-09-19 08:56:09 -0700188
189 canvas->scale(1.0f/2.0f, 1.0f/2.0f);
190
robertphillips3bc25e72014-09-19 08:56:09 -0700191 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700192 canvas->translate(SkScalarHalf(kTriSide), 0);
robertphillips3bc25e72014-09-19 08:56:09 -0700193 canvas->drawPicture(tri);
194 canvas->restore();
195
196 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700197 canvas->translate(SkIntToScalar(kTriSide), 1.5f * kTriSide / kRoot3);
198 canvas->drawPicture(tri);
199 canvas->restore();
200
201 canvas->save();
202 canvas->translate(0, 1.5f * kTriSide / kRoot3);
robertphillips3bc25e72014-09-19 08:56:09 -0700203 canvas->drawPicture(tri);
204 canvas->restore();
205
reedca2622b2016-03-18 07:25:55 -0700206 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700207}
208
209// Create a Sierpinkski-like picture that starts with a top row with a picture
piotaixrb5fae932014-09-24 13:03:30 -0700210// that just contains a triangle. Subsequent rows take the prior row's picture,
robertphillips3bc25e72014-09-19 08:56:09 -0700211// shrinks it and replicates it 3 times then draws and appropriate number of
212// copies of it.
reedca2622b2016-03-18 07:25:55 -0700213static sk_sp<SkPicture> make_sierpinski_picture() {
214 sk_sp<SkPicture> pic(make_tri_picture());
robertphillips3bc25e72014-09-19 08:56:09 -0700215
216 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800217 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700218
219 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800220 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700221 &bbhFactory);
robertphillips3bc25e72014-09-19 08:56:09 -0700222
mtkleindbfd7ab2016-09-01 11:24:54 -0700223 constexpr int kNumLevels = 4;
robertphillips3bc25e72014-09-19 08:56:09 -0700224 for (int i = 0; i < kNumLevels; ++i) {
225 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700226 canvas->translate(kPicWidth/2 - (i+1) * (kTriSide/2.0f), 0.0f);
robertphillips3bc25e72014-09-19 08:56:09 -0700227 for (int j = 0; j < i+1; ++j) {
228 canvas->drawPicture(pic);
229 canvas->translate(SkIntToScalar(kTriSide), 0);
230 }
231 canvas->restore();
232
reedca2622b2016-03-18 07:25:55 -0700233 pic = make_sub_picture(pic.get());
robertphillips3bc25e72014-09-19 08:56:09 -0700234
235 canvas->translate(0, 1.5f * kTriSide / kRoot3);
236 }
237
reedca2622b2016-03-18 07:25:55 -0700238 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700239}
240
reede8f30622016-03-23 18:59:25 -0700241static sk_sp<SkSurface> create_compat_surface(SkCanvas* canvas, int width, int height) {
robertphillips7eacd772014-08-21 13:12:42 -0700242 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
243
Mike Reed46596ae2018-01-02 15:40:29 -0500244 return sk_tool_utils::makeSurface(canvas, info);
robertphillips7eacd772014-08-21 13:12:42 -0700245}
246
247// This class stores the information required to compose all the result
248// fragments potentially generated by the MultiPictureDraw object
249class ComposeStep {
250public:
reede8f30622016-03-23 18:59:25 -0700251 ComposeStep() : fX(0.0f), fY(0.0f), fPaint(nullptr) { }
halcanary385fe4d2015-08-26 13:07:48 -0700252 ~ComposeStep() {
halcanary385fe4d2015-08-26 13:07:48 -0700253 delete fPaint;
254 }
robertphillips7eacd772014-08-21 13:12:42 -0700255
reede8f30622016-03-23 18:59:25 -0700256 sk_sp<SkSurface> fSurf;
robertphillips7eacd772014-08-21 13:12:42 -0700257 SkScalar fX;
258 SkScalar fY;
259 SkPaint* fPaint;
260};
261
robertphillips3bc25e72014-09-19 08:56:09 -0700262typedef void (*PFContentMtd)(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]);
robertphillips7eacd772014-08-21 13:12:42 -0700263
264// Just a single picture with no clip
robertphillips3bc25e72014-09-19 08:56:09 -0700265static void no_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700266 canvas->drawPicture(pictures[0]);
267}
268
269// Two pictures with a rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700270static void rect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700271 canvas->drawPicture(pictures[0]);
272
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700273 SkRect rect = pictures[0]->cullRect();
robertphillips7eacd772014-08-21 13:12:42 -0700274 rect.inset(kInset, kInset);
275
276 canvas->clipRect(rect);
277
278 canvas->drawPicture(pictures[1]);
279}
280
281// Two pictures with a round rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700282static void rrect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700283 canvas->drawPicture(pictures[0]);
284
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700285 SkRect rect = pictures[0]->cullRect();
robertphillips7eacd772014-08-21 13:12:42 -0700286 rect.inset(kInset, kInset);
piotaixrb5fae932014-09-24 13:03:30 -0700287
robertphillips7eacd772014-08-21 13:12:42 -0700288 SkRRect rrect;
289 rrect.setRectXY(rect, kInset, kInset);
290
291 canvas->clipRRect(rrect);
292
293 canvas->drawPicture(pictures[1]);
294}
295
296// Two pictures with a clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700297static void path_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700298 canvas->drawPicture(pictures[0]);
299
300 // Create a hexagon centered on the middle of the hex grid
301 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
302
303 canvas->clipPath(hex);
304
305 canvas->drawPicture(pictures[1]);
306}
307
308// Two pictures with an inverse clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700309static void invpath_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700310 canvas->drawPicture(pictures[0]);
311
312 // Create a hexagon centered on the middle of the hex grid
313 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
314 hex.setFillType(SkPath::kInverseEvenOdd_FillType);
315
316 canvas->clipPath(hex);
317
318 canvas->drawPicture(pictures[1]);
319}
320
robertphillips3bc25e72014-09-19 08:56:09 -0700321// Reuse a single base (triangular) picture a _lot_ (rotated, scaled and translated).
322static void sierpinski(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
323 canvas->save();
robertphillips3bc25e72014-09-19 08:56:09 -0700324 canvas->drawPicture(pictures[2]);
325
326 canvas->rotate(180.0f);
robertphillipsaa0c8372014-09-29 05:07:39 -0700327 canvas->translate(-SkIntToScalar(kPicWidth), -SkIntToScalar(kPicHeight));
robertphillips3bc25e72014-09-19 08:56:09 -0700328 canvas->drawPicture(pictures[2]);
329 canvas->restore();
330}
331
robertphillipsb1fc64b2014-10-02 08:32:43 -0700332static void big_layer(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
333 canvas->drawPicture(pictures[3]);
334}
335
mtkleindbfd7ab2016-09-01 11:24:54 -0700336constexpr PFContentMtd gContentMthds[] = {
robertphillips7eacd772014-08-21 13:12:42 -0700337 no_clip,
338 rect_clip,
339 rrect_clip,
340 path_clip,
robertphillips3bc25e72014-09-19 08:56:09 -0700341 invpath_clip,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700342 sierpinski,
343 big_layer,
robertphillips7eacd772014-08-21 13:12:42 -0700344};
345
346static void create_content(SkMultiPictureDraw* mpd, PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700347 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700348 SkCanvas* dest, const SkMatrix& xform) {
reedca2622b2016-03-18 07:25:55 -0700349 sk_sp<SkPicture> composite;
robertphillips7eacd772014-08-21 13:12:42 -0700350
351 {
352 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800353 SkRTreeFactory bbhFactory;
robertphillips7eacd772014-08-21 13:12:42 -0700354
piotaixrb5fae932014-09-24 13:03:30 -0700355 SkCanvas* pictureCanvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800356 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700357 &bbhFactory);
robertphillips7eacd772014-08-21 13:12:42 -0700358
359 (*pfGen)(pictureCanvas, pictures);
360
reedca2622b2016-03-18 07:25:55 -0700361 composite = recorder.finishRecordingAsPicture();
robertphillips7eacd772014-08-21 13:12:42 -0700362 }
363
reedca2622b2016-03-18 07:25:55 -0700364 mpd->add(dest, composite.get(), &xform);
robertphillips7eacd772014-08-21 13:12:42 -0700365}
366
piotaixrb5fae932014-09-24 13:03:30 -0700367typedef void(*PFLayoutMtd)(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
robertphillips3bc25e72014-09-19 08:56:09 -0700368 PFContentMtd pfGen, const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700369 SkTArray<ComposeStep>* composeSteps);
370
371// Draw the content into a single canvas
piotaixrb5fae932014-09-24 13:03:30 -0700372static void simple(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
373 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700374 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700375 SkTArray<ComposeStep> *composeSteps) {
376
377 ComposeStep& step = composeSteps->push_back();
378
bsalomon892f31a2014-08-21 14:40:36 -0700379 step.fSurf = create_compat_surface(finalCanvas, kPicWidth, kPicHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700380
381 SkCanvas* subCanvas = step.fSurf->getCanvas();
382
383 create_content(mpd, pfGen, pictures, subCanvas, SkMatrix::I());
384}
385
386// Draw the content into multiple canvases/tiles
387static void tiled(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
piotaixrb5fae932014-09-24 13:03:30 -0700388 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700389 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700390 SkTArray<ComposeStep> *composeSteps) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700391 const int kNumTilesX = 2;
392 const int kNumTilesY = 2;
393 const int kTileWidth = kPicWidth / kNumTilesX;
394 const int kTileHeight = kPicHeight / kNumTilesY;
robertphillips7eacd772014-08-21 13:12:42 -0700395
396 SkASSERT(kPicWidth == kNumTilesX * kTileWidth);
397 SkASSERT(kPicHeight == kNumTilesY * kTileHeight);
398
mtkleindbfd7ab2016-09-01 11:24:54 -0700399 const SkColor colors[kNumTilesX][kNumTilesY] = {
piotaixrb5fae932014-09-24 13:03:30 -0700400 { SK_ColorCYAN, SK_ColorMAGENTA },
robertphillips7eacd772014-08-21 13:12:42 -0700401 { SK_ColorYELLOW, SK_ColorGREEN }
402 };
403
404 for (int y = 0; y < kNumTilesY; ++y) {
405 for (int x = 0; x < kNumTilesX; ++x) {
406 ComposeStep& step = composeSteps->push_back();
407
408 step.fX = SkIntToScalar(x*kTileWidth);
409 step.fY = SkIntToScalar(y*kTileHeight);
halcanary385fe4d2015-08-26 13:07:48 -0700410 step.fPaint = new SkPaint;
robertphillips7eacd772014-08-21 13:12:42 -0700411 step.fPaint->setColorFilter(
Mike Reed7d954ad2016-10-28 15:42:34 -0400412 SkColorFilter::MakeModeFilter(colors[x][y], SkBlendMode::kModulate));
robertphillips7eacd772014-08-21 13:12:42 -0700413
bsalomon892f31a2014-08-21 14:40:36 -0700414 step.fSurf = create_compat_surface(finalCanvas, kTileWidth, kTileHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700415
416 SkCanvas* subCanvas = step.fSurf->getCanvas();
417
halcanary9d524f22016-03-29 09:03:52 -0700418 const SkMatrix trans = SkMatrix::MakeTrans(-SkIntToScalar(x*kTileWidth),
robertphillips1d24b8d2015-03-26 19:57:08 -0700419 -SkIntToScalar(y*kTileHeight));
robertphillips7eacd772014-08-21 13:12:42 -0700420
421 create_content(mpd, pfGen, pictures, subCanvas, trans);
422 }
423 }
424}
425
mtkleindbfd7ab2016-09-01 11:24:54 -0700426constexpr PFLayoutMtd gLayoutMthds[] = { simple, tiled };
robertphillips7eacd772014-08-21 13:12:42 -0700427
428namespace skiagm {
429 /**
430 * This GM exercises the SkMultiPictureDraw object. It tests the
431 * cross product of:
432 * tiled vs. all-at-once rendering (e.g., into many or just 1 canvas)
433 * different clips (e.g., none, rect, rrect)
434 * single vs. multiple pictures (e.g., normal vs. picture-pile-style content)
435 */
436 class MultiPictureDraw : public GM {
437 public:
438 enum Content {
439 kNoClipSingle_Content,
440 kRectClipMulti_Content,
441 kRRectClipMulti_Content,
442 kPathClipMulti_Content,
443 kInvPathClipMulti_Content,
robertphillips3bc25e72014-09-19 08:56:09 -0700444 kSierpinski_Content,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700445 kBigLayer_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700446
robertphillipsb1fc64b2014-10-02 08:32:43 -0700447 kLast_Content = kBigLayer_Content
robertphillips7eacd772014-08-21 13:12:42 -0700448 };
449
mtkleindbfd7ab2016-09-01 11:24:54 -0700450 const int kContentCnt = kLast_Content + 1;
robertphillips7eacd772014-08-21 13:12:42 -0700451
452 enum Layout {
453 kSimple_Layout,
454 kTiled_Layout,
455
456 kLast_Layout = kTiled_Layout
457 };
458
mtkleindbfd7ab2016-09-01 11:24:54 -0700459 const int kLayoutCnt = kLast_Layout + 1;
robertphillips7eacd772014-08-21 13:12:42 -0700460
461 MultiPictureDraw(Content content, Layout layout) : fContent(content), fLayout(layout) {
462 SkASSERT(SK_ARRAY_COUNT(gLayoutMthds) == kLayoutCnt);
463 SkASSERT(SK_ARRAY_COUNT(gContentMthds) == kContentCnt);
464
robertphillips3bc25e72014-09-19 08:56:09 -0700465 for (int i = 0; i < kNumPictures; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -0700466 fPictures[i] = nullptr;
robertphillips3bc25e72014-09-19 08:56:09 -0700467 }
robertphillips7eacd772014-08-21 13:12:42 -0700468 }
469
Brian Salomond3b65972017-03-22 12:05:03 -0400470 ~MultiPictureDraw() override {
robertphillips3bc25e72014-09-19 08:56:09 -0700471 for (int i = 0; i < kNumPictures; ++i) {
472 SkSafeUnref(fPictures[i]);
473 }
robertphillips7eacd772014-08-21 13:12:42 -0700474 }
475
476 protected:
477 Content fContent;
478 Layout fLayout;
robertphillips3bc25e72014-09-19 08:56:09 -0700479 const SkPicture* fPictures[kNumPictures];
robertphillips7eacd772014-08-21 13:12:42 -0700480
mtklein36352bf2015-03-25 18:17:31 -0700481 void onOnceBeforeDraw() override {
reedca2622b2016-03-18 07:25:55 -0700482 fPictures[0] = make_hex_plane_picture(SK_ColorWHITE).release();
483 fPictures[1] = make_hex_plane_picture(sk_tool_utils::color_to_565(SK_ColorGRAY)).release();
484 fPictures[2] = make_sierpinski_picture().release();
485 fPictures[3] = make_single_layer_hex_plane_picture().release();
robertphillips7eacd772014-08-21 13:12:42 -0700486 }
487
mtklein36352bf2015-03-25 18:17:31 -0700488 void onDraw(SkCanvas* canvas) override {
robertphillips7eacd772014-08-21 13:12:42 -0700489 SkMultiPictureDraw mpd;
490 SkTArray<ComposeStep> composeSteps;
491
492 // Fill up the MultiPictureDraw
piotaixrb5fae932014-09-24 13:03:30 -0700493 (*gLayoutMthds[fLayout])(canvas, &mpd,
494 gContentMthds[fContent],
robertphillips7eacd772014-08-21 13:12:42 -0700495 fPictures, &composeSteps);
496
497 mpd.draw();
498
499 // Compose all the drawn canvases into the final canvas
500 for (int i = 0; i < composeSteps.count(); ++i) {
501 const ComposeStep& step = composeSteps[i];
502
reed9ce9d672016-03-17 10:51:11 -0700503 canvas->drawImage(step.fSurf->makeImageSnapshot().get(),
504 step.fX, step.fY, step.fPaint);
robertphillips7eacd772014-08-21 13:12:42 -0700505 }
506 }
507
mtklein36352bf2015-03-25 18:17:31 -0700508 SkISize onISize() override { return SkISize::Make(kPicWidth, kPicHeight); }
robertphillips7eacd772014-08-21 13:12:42 -0700509
mtklein36352bf2015-03-25 18:17:31 -0700510 SkString onShortName() override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700511 const char* gContentNames[] = {
halcanary9d524f22016-03-29 09:03:52 -0700512 "noclip", "rectclip", "rrectclip", "pathclip",
robertphillipsb1fc64b2014-10-02 08:32:43 -0700513 "invpathclip", "sierpinski", "biglayer"
robertphillips7eacd772014-08-21 13:12:42 -0700514 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700515 const char* gLayoutNames[] = { "simple", "tiled" };
robertphillips7eacd772014-08-21 13:12:42 -0700516
517 SkASSERT(SK_ARRAY_COUNT(gLayoutNames) == kLayoutCnt);
518 SkASSERT(SK_ARRAY_COUNT(gContentNames) == kContentCnt);
519
520 SkString name("multipicturedraw_");
521
522 name.append(gContentNames[fContent]);
523 name.append("_");
524 name.append(gLayoutNames[fLayout]);
525 return name;
526 }
527
mtklein36352bf2015-03-25 18:17:31 -0700528 bool runAsBench() const override { return true; }
robertphillips7eacd772014-08-21 13:12:42 -0700529
530 private:
531 typedef GM INHERITED;
532 };
533
halcanary385fe4d2015-08-26 13:07:48 -0700534 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
535 MultiPictureDraw::kSimple_Layout);)
536 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
537 MultiPictureDraw::kSimple_Layout);)
538 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
539 MultiPictureDraw::kSimple_Layout);)
540 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
541 MultiPictureDraw::kSimple_Layout);)
542 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
543 MultiPictureDraw::kSimple_Layout);)
544 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
545 MultiPictureDraw::kSimple_Layout);)
546 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
547 MultiPictureDraw::kSimple_Layout);)
robertphillips7eacd772014-08-21 13:12:42 -0700548
halcanary385fe4d2015-08-26 13:07:48 -0700549 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
550 MultiPictureDraw::kTiled_Layout);)
551 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
552 MultiPictureDraw::kTiled_Layout);)
553 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
554 MultiPictureDraw::kTiled_Layout);)
555 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
556 MultiPictureDraw::kTiled_Layout);)
557 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
558 MultiPictureDraw::kTiled_Layout);)
559 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
560 MultiPictureDraw::kTiled_Layout);)
561 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
562 MultiPictureDraw::kTiled_Layout);)
robertphillips7eacd772014-08-21 13:12:42 -0700563}