blob: fe2281ae20f528592118f960945ac4a9423c696d [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"
13#include "SkPictureRecorder.h"
14#include "SkSurface.h"
15
mtkleindbfd7ab2016-09-01 11:24:54 -070016constexpr SkScalar kRoot3Over2 = 0.86602545f; // sin(60)
17constexpr SkScalar kRoot3 = 1.73205081f;
robertphillips7eacd772014-08-21 13:12:42 -070018
mtkleindbfd7ab2016-09-01 11:24:54 -070019constexpr int kHexSide = 30;
20constexpr int kNumHexX = 6;
21constexpr int kNumHexY = 6;
22constexpr int kPicWidth = kNumHexX * kHexSide;
23constexpr int kPicHeight = (int)((kNumHexY - 0.5f) * 2 * kHexSide * kRoot3Over2 + 0.5f);
24constexpr SkScalar kInset = 20.0f;
25constexpr int kNumPictures = 4;
robertphillips3bc25e72014-09-19 08:56:09 -070026
mtkleindbfd7ab2016-09-01 11:24:54 -070027constexpr int kTriSide = 40;
robertphillips7eacd772014-08-21 13:12:42 -070028
29// Create a hexagon centered at (originX, originY)
30static SkPath make_hex_path(SkScalar originX, SkScalar originY) {
31 SkPath hex;
32 hex.moveTo(originX-kHexSide, originY);
33 hex.rLineTo(SkScalarHalf(kHexSide), kRoot3Over2 * kHexSide);
34 hex.rLineTo(SkIntToScalar(kHexSide), 0);
35 hex.rLineTo(SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
36 hex.rLineTo(-SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
37 hex.rLineTo(-SkIntToScalar(kHexSide), 0);
38 hex.close();
39 return hex;
40}
41
42// Make a picture that is a tiling of the plane with stroked hexagons where
43// each hexagon is in its own layer. The layers are to exercise Ganesh's
44// layer hoisting.
reedca2622b2016-03-18 07:25:55 -070045static sk_sp<SkPicture> make_hex_plane_picture(SkColor fillColor) {
robertphillips7eacd772014-08-21 13:12:42 -070046
47 // Create a hexagon with its center at the origin
48 SkPath hex = make_hex_path(0, 0);
49
50 SkPaint fill;
51 fill.setStyle(SkPaint::kFill_Style);
52 fill.setColor(fillColor);
53
54 SkPaint stroke;
55 stroke.setStyle(SkPaint::kStroke_Style);
56 stroke.setStrokeWidth(3);
57
58 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -080059 SkRTreeFactory bbhFactory;
robertphillips7eacd772014-08-21 13:12:42 -070060
piotaixrb5fae932014-09-24 13:03:30 -070061 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -080062 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -070063 &bbhFactory);
robertphillips7eacd772014-08-21 13:12:42 -070064
65 SkScalar xPos, yPos = 0;
66
67 for (int y = 0; y < kNumHexY; ++y) {
68 xPos = 0;
69
70 for (int x = 0; x < kNumHexX; ++x) {
halcanary96fcdcc2015-08-27 07:41:13 -070071 canvas->saveLayer(nullptr, nullptr);
robertphillips7eacd772014-08-21 13:12:42 -070072 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
73 canvas->drawPath(hex, fill);
74 canvas->drawPath(hex, stroke);
75 canvas->restore();
76
77 xPos += 1.5f * kHexSide;
78 }
79
80 yPos += 2 * kHexSide * kRoot3Over2;
81 }
82
reedca2622b2016-03-18 07:25:55 -070083 return recorder.finishRecordingAsPicture();
robertphillips7eacd772014-08-21 13:12:42 -070084}
85
robertphillipsb1fc64b2014-10-02 08:32:43 -070086// Create a picture that consists of a single large layer that is tiled
87// with hexagons.
88// This is intended to exercise the layer hoisting code's clip handling (in
89// tile mode).
reedca2622b2016-03-18 07:25:55 -070090static sk_sp<SkPicture> make_single_layer_hex_plane_picture() {
robertphillipsb1fc64b2014-10-02 08:32:43 -070091
92 // Create a hexagon with its center at the origin
93 SkPath hex = make_hex_path(0, 0);
94
95 SkPaint whiteFill;
96 whiteFill.setStyle(SkPaint::kFill_Style);
97 whiteFill.setColor(SK_ColorWHITE);
98
99 SkPaint greyFill;
100 greyFill.setStyle(SkPaint::kFill_Style);
caryclarkf597c422015-07-28 10:37:53 -0700101 greyFill.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
robertphillipsb1fc64b2014-10-02 08:32:43 -0700102
103 SkPaint stroke;
104 stroke.setStyle(SkPaint::kStroke_Style);
105 stroke.setStrokeWidth(3);
106
107 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800108 SkRTreeFactory bbhFactory;
robertphillipsb1fc64b2014-10-02 08:32:43 -0700109
mtkleindbfd7ab2016-09-01 11:24:54 -0700110 constexpr SkScalar kBig = 10000.0f;
robertphillipsdda54452016-07-13 13:27:16 -0700111 SkCanvas* canvas = recorder.beginRecording(kBig, kBig, &bbhFactory);
robertphillipsb1fc64b2014-10-02 08:32:43 -0700112
halcanary96fcdcc2015-08-27 07:41:13 -0700113 canvas->saveLayer(nullptr, nullptr);
robertphillips1564dde2014-10-06 11:50:31 -0700114
robertphillipsb1fc64b2014-10-02 08:32:43 -0700115 SkScalar xPos = 0.0f, yPos = 0.0f;
116
117 for (int y = 0; yPos < kBig; ++y) {
118 xPos = 0;
119
120 for (int x = 0; xPos < kBig; ++x) {
121 canvas->save();
122 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
123 // The color of the filled hex is swapped to yield a different
124 // pattern in each tile. This allows an error in layer hoisting (e.g.,
125 // the clip isn't blocking cache reuse) to cause a visual discrepancy.
126 canvas->drawPath(hex, ((x+y) % 3) ? whiteFill : greyFill);
127 canvas->drawPath(hex, stroke);
128 canvas->restore();
129
130 xPos += 1.5f * kHexSide;
131 }
132
133 yPos += 2 * kHexSide * kRoot3Over2;
134 }
135
robertphillips1564dde2014-10-06 11:50:31 -0700136 canvas->restore();
137
reedca2622b2016-03-18 07:25:55 -0700138 return recorder.finishRecordingAsPicture();
robertphillipsb1fc64b2014-10-02 08:32:43 -0700139}
140
robertphillips3bc25e72014-09-19 08:56:09 -0700141// Make an equilateral triangle path with its top corner at (originX, originY)
142static SkPath make_tri_path(SkScalar originX, SkScalar originY) {
143 SkPath tri;
144 tri.moveTo(originX, originY);
145 tri.rLineTo(SkScalarHalf(kTriSide), 1.5f * kTriSide / kRoot3);
146 tri.rLineTo(-kTriSide, 0);
147 tri.close();
148 return tri;
149}
150
reedca2622b2016-03-18 07:25:55 -0700151static sk_sp<SkPicture> make_tri_picture() {
robertphillipsaa0c8372014-09-29 05:07:39 -0700152 SkPath tri = make_tri_path(SkScalarHalf(kTriSide), 0);
robertphillips3bc25e72014-09-19 08:56:09 -0700153
154 SkPaint fill;
155 fill.setStyle(SkPaint::kFill_Style);
caryclarkf597c422015-07-28 10:37:53 -0700156 fill.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
robertphillips3bc25e72014-09-19 08:56:09 -0700157
158 SkPaint stroke;
159 stroke.setStyle(SkPaint::kStroke_Style);
160 stroke.setStrokeWidth(3);
161
162 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800163 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700164
165 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800166 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700167 &bbhFactory);
robertphillips7f1ed832014-10-03 07:29:20 -0700168 SkRect r = tri.getBounds();
169 r.outset(2.0f, 2.0f); // outset for stroke
170 canvas->clipRect(r);
robertphillips3bc25e72014-09-19 08:56:09 -0700171 // The saveLayer/restore block is to exercise layer hoisting
halcanary96fcdcc2015-08-27 07:41:13 -0700172 canvas->saveLayer(nullptr, nullptr);
robertphillips3bc25e72014-09-19 08:56:09 -0700173 canvas->drawPath(tri, fill);
174 canvas->drawPath(tri, stroke);
175 canvas->restore();
176
reedca2622b2016-03-18 07:25:55 -0700177 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700178}
179
reedca2622b2016-03-18 07:25:55 -0700180static sk_sp<SkPicture> make_sub_picture(const SkPicture* tri) {
robertphillips3bc25e72014-09-19 08:56:09 -0700181 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800182 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700183
184 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800185 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700186 &bbhFactory);
robertphillips3bc25e72014-09-19 08:56:09 -0700187
188 canvas->scale(1.0f/2.0f, 1.0f/2.0f);
189
robertphillips3bc25e72014-09-19 08:56:09 -0700190 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700191 canvas->translate(SkScalarHalf(kTriSide), 0);
robertphillips3bc25e72014-09-19 08:56:09 -0700192 canvas->drawPicture(tri);
193 canvas->restore();
194
195 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700196 canvas->translate(SkIntToScalar(kTriSide), 1.5f * kTriSide / kRoot3);
197 canvas->drawPicture(tri);
198 canvas->restore();
199
200 canvas->save();
201 canvas->translate(0, 1.5f * kTriSide / kRoot3);
robertphillips3bc25e72014-09-19 08:56:09 -0700202 canvas->drawPicture(tri);
203 canvas->restore();
204
reedca2622b2016-03-18 07:25:55 -0700205 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700206}
207
208// Create a Sierpinkski-like picture that starts with a top row with a picture
piotaixrb5fae932014-09-24 13:03:30 -0700209// that just contains a triangle. Subsequent rows take the prior row's picture,
robertphillips3bc25e72014-09-19 08:56:09 -0700210// shrinks it and replicates it 3 times then draws and appropriate number of
211// copies of it.
reedca2622b2016-03-18 07:25:55 -0700212static sk_sp<SkPicture> make_sierpinski_picture() {
213 sk_sp<SkPicture> pic(make_tri_picture());
robertphillips3bc25e72014-09-19 08:56:09 -0700214
215 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800216 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700217
218 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800219 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700220 &bbhFactory);
robertphillips3bc25e72014-09-19 08:56:09 -0700221
mtkleindbfd7ab2016-09-01 11:24:54 -0700222 constexpr int kNumLevels = 4;
robertphillips3bc25e72014-09-19 08:56:09 -0700223 for (int i = 0; i < kNumLevels; ++i) {
224 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700225 canvas->translate(kPicWidth/2 - (i+1) * (kTriSide/2.0f), 0.0f);
robertphillips3bc25e72014-09-19 08:56:09 -0700226 for (int j = 0; j < i+1; ++j) {
227 canvas->drawPicture(pic);
228 canvas->translate(SkIntToScalar(kTriSide), 0);
229 }
230 canvas->restore();
231
reedca2622b2016-03-18 07:25:55 -0700232 pic = make_sub_picture(pic.get());
robertphillips3bc25e72014-09-19 08:56:09 -0700233
234 canvas->translate(0, 1.5f * kTriSide / kRoot3);
235 }
236
reedca2622b2016-03-18 07:25:55 -0700237 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700238}
239
reede8f30622016-03-23 18:59:25 -0700240static sk_sp<SkSurface> create_compat_surface(SkCanvas* canvas, int width, int height) {
robertphillips7eacd772014-08-21 13:12:42 -0700241 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
242
reede8f30622016-03-23 18:59:25 -0700243 auto surface = canvas->makeSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -0700244 if (nullptr == surface) {
245 // picture canvas returns nullptr so fall back to raster
reede8f30622016-03-23 18:59:25 -0700246 surface = SkSurface::MakeRaster(info);
robertphillips7eacd772014-08-21 13:12:42 -0700247 }
robertphillips7eacd772014-08-21 13:12:42 -0700248 return surface;
249}
250
251// This class stores the information required to compose all the result
252// fragments potentially generated by the MultiPictureDraw object
253class ComposeStep {
254public:
reede8f30622016-03-23 18:59:25 -0700255 ComposeStep() : fX(0.0f), fY(0.0f), fPaint(nullptr) { }
halcanary385fe4d2015-08-26 13:07:48 -0700256 ~ComposeStep() {
halcanary385fe4d2015-08-26 13:07:48 -0700257 delete fPaint;
258 }
robertphillips7eacd772014-08-21 13:12:42 -0700259
reede8f30622016-03-23 18:59:25 -0700260 sk_sp<SkSurface> fSurf;
robertphillips7eacd772014-08-21 13:12:42 -0700261 SkScalar fX;
262 SkScalar fY;
263 SkPaint* fPaint;
264};
265
robertphillips3bc25e72014-09-19 08:56:09 -0700266typedef void (*PFContentMtd)(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]);
robertphillips7eacd772014-08-21 13:12:42 -0700267
268// Just a single picture with no clip
robertphillips3bc25e72014-09-19 08:56:09 -0700269static void no_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700270 canvas->drawPicture(pictures[0]);
271}
272
273// Two pictures with a rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700274static void rect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700275 canvas->drawPicture(pictures[0]);
276
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700277 SkRect rect = pictures[0]->cullRect();
robertphillips7eacd772014-08-21 13:12:42 -0700278 rect.inset(kInset, kInset);
279
280 canvas->clipRect(rect);
281
282 canvas->drawPicture(pictures[1]);
283}
284
285// Two pictures with a round rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700286static void rrect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700287 canvas->drawPicture(pictures[0]);
288
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700289 SkRect rect = pictures[0]->cullRect();
robertphillips7eacd772014-08-21 13:12:42 -0700290 rect.inset(kInset, kInset);
piotaixrb5fae932014-09-24 13:03:30 -0700291
robertphillips7eacd772014-08-21 13:12:42 -0700292 SkRRect rrect;
293 rrect.setRectXY(rect, kInset, kInset);
294
295 canvas->clipRRect(rrect);
296
297 canvas->drawPicture(pictures[1]);
298}
299
300// Two pictures with a clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700301static void path_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700302 canvas->drawPicture(pictures[0]);
303
304 // Create a hexagon centered on the middle of the hex grid
305 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
306
307 canvas->clipPath(hex);
308
309 canvas->drawPicture(pictures[1]);
310}
311
312// Two pictures with an inverse clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700313static void invpath_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700314 canvas->drawPicture(pictures[0]);
315
316 // Create a hexagon centered on the middle of the hex grid
317 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
318 hex.setFillType(SkPath::kInverseEvenOdd_FillType);
319
320 canvas->clipPath(hex);
321
322 canvas->drawPicture(pictures[1]);
323}
324
robertphillips3bc25e72014-09-19 08:56:09 -0700325// Reuse a single base (triangular) picture a _lot_ (rotated, scaled and translated).
326static void sierpinski(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
327 canvas->save();
robertphillips3bc25e72014-09-19 08:56:09 -0700328 canvas->drawPicture(pictures[2]);
329
330 canvas->rotate(180.0f);
robertphillipsaa0c8372014-09-29 05:07:39 -0700331 canvas->translate(-SkIntToScalar(kPicWidth), -SkIntToScalar(kPicHeight));
robertphillips3bc25e72014-09-19 08:56:09 -0700332 canvas->drawPicture(pictures[2]);
333 canvas->restore();
334}
335
robertphillipsb1fc64b2014-10-02 08:32:43 -0700336static void big_layer(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
337 canvas->drawPicture(pictures[3]);
338}
339
mtkleindbfd7ab2016-09-01 11:24:54 -0700340constexpr PFContentMtd gContentMthds[] = {
robertphillips7eacd772014-08-21 13:12:42 -0700341 no_clip,
342 rect_clip,
343 rrect_clip,
344 path_clip,
robertphillips3bc25e72014-09-19 08:56:09 -0700345 invpath_clip,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700346 sierpinski,
347 big_layer,
robertphillips7eacd772014-08-21 13:12:42 -0700348};
349
350static void create_content(SkMultiPictureDraw* mpd, PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700351 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700352 SkCanvas* dest, const SkMatrix& xform) {
reedca2622b2016-03-18 07:25:55 -0700353 sk_sp<SkPicture> composite;
robertphillips7eacd772014-08-21 13:12:42 -0700354
355 {
356 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800357 SkRTreeFactory bbhFactory;
robertphillips7eacd772014-08-21 13:12:42 -0700358
piotaixrb5fae932014-09-24 13:03:30 -0700359 SkCanvas* pictureCanvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800360 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700361 &bbhFactory);
robertphillips7eacd772014-08-21 13:12:42 -0700362
363 (*pfGen)(pictureCanvas, pictures);
364
reedca2622b2016-03-18 07:25:55 -0700365 composite = recorder.finishRecordingAsPicture();
robertphillips7eacd772014-08-21 13:12:42 -0700366 }
367
reedca2622b2016-03-18 07:25:55 -0700368 mpd->add(dest, composite.get(), &xform);
robertphillips7eacd772014-08-21 13:12:42 -0700369}
370
piotaixrb5fae932014-09-24 13:03:30 -0700371typedef void(*PFLayoutMtd)(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
robertphillips3bc25e72014-09-19 08:56:09 -0700372 PFContentMtd pfGen, const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700373 SkTArray<ComposeStep>* composeSteps);
374
375// Draw the content into a single canvas
piotaixrb5fae932014-09-24 13:03:30 -0700376static void simple(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
377 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700378 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700379 SkTArray<ComposeStep> *composeSteps) {
380
381 ComposeStep& step = composeSteps->push_back();
382
bsalomon892f31a2014-08-21 14:40:36 -0700383 step.fSurf = create_compat_surface(finalCanvas, kPicWidth, kPicHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700384
385 SkCanvas* subCanvas = step.fSurf->getCanvas();
386
387 create_content(mpd, pfGen, pictures, subCanvas, SkMatrix::I());
388}
389
390// Draw the content into multiple canvases/tiles
391static void tiled(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
piotaixrb5fae932014-09-24 13:03:30 -0700392 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700393 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700394 SkTArray<ComposeStep> *composeSteps) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700395 const int kNumTilesX = 2;
396 const int kNumTilesY = 2;
397 const int kTileWidth = kPicWidth / kNumTilesX;
398 const int kTileHeight = kPicHeight / kNumTilesY;
robertphillips7eacd772014-08-21 13:12:42 -0700399
400 SkASSERT(kPicWidth == kNumTilesX * kTileWidth);
401 SkASSERT(kPicHeight == kNumTilesY * kTileHeight);
402
mtkleindbfd7ab2016-09-01 11:24:54 -0700403 const SkColor colors[kNumTilesX][kNumTilesY] = {
piotaixrb5fae932014-09-24 13:03:30 -0700404 { SK_ColorCYAN, SK_ColorMAGENTA },
robertphillips7eacd772014-08-21 13:12:42 -0700405 { SK_ColorYELLOW, SK_ColorGREEN }
406 };
407
408 for (int y = 0; y < kNumTilesY; ++y) {
409 for (int x = 0; x < kNumTilesX; ++x) {
410 ComposeStep& step = composeSteps->push_back();
411
412 step.fX = SkIntToScalar(x*kTileWidth);
413 step.fY = SkIntToScalar(y*kTileHeight);
halcanary385fe4d2015-08-26 13:07:48 -0700414 step.fPaint = new SkPaint;
robertphillips7eacd772014-08-21 13:12:42 -0700415 step.fPaint->setColorFilter(
Mike Reed7d954ad2016-10-28 15:42:34 -0400416 SkColorFilter::MakeModeFilter(colors[x][y], SkBlendMode::kModulate));
robertphillips7eacd772014-08-21 13:12:42 -0700417
bsalomon892f31a2014-08-21 14:40:36 -0700418 step.fSurf = create_compat_surface(finalCanvas, kTileWidth, kTileHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700419
420 SkCanvas* subCanvas = step.fSurf->getCanvas();
421
halcanary9d524f22016-03-29 09:03:52 -0700422 const SkMatrix trans = SkMatrix::MakeTrans(-SkIntToScalar(x*kTileWidth),
robertphillips1d24b8d2015-03-26 19:57:08 -0700423 -SkIntToScalar(y*kTileHeight));
robertphillips7eacd772014-08-21 13:12:42 -0700424
425 create_content(mpd, pfGen, pictures, subCanvas, trans);
426 }
427 }
428}
429
mtkleindbfd7ab2016-09-01 11:24:54 -0700430constexpr PFLayoutMtd gLayoutMthds[] = { simple, tiled };
robertphillips7eacd772014-08-21 13:12:42 -0700431
432namespace skiagm {
433 /**
434 * This GM exercises the SkMultiPictureDraw object. It tests the
435 * cross product of:
436 * tiled vs. all-at-once rendering (e.g., into many or just 1 canvas)
437 * different clips (e.g., none, rect, rrect)
438 * single vs. multiple pictures (e.g., normal vs. picture-pile-style content)
439 */
440 class MultiPictureDraw : public GM {
441 public:
442 enum Content {
443 kNoClipSingle_Content,
444 kRectClipMulti_Content,
445 kRRectClipMulti_Content,
446 kPathClipMulti_Content,
447 kInvPathClipMulti_Content,
robertphillips3bc25e72014-09-19 08:56:09 -0700448 kSierpinski_Content,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700449 kBigLayer_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700450
robertphillipsb1fc64b2014-10-02 08:32:43 -0700451 kLast_Content = kBigLayer_Content
robertphillips7eacd772014-08-21 13:12:42 -0700452 };
453
mtkleindbfd7ab2016-09-01 11:24:54 -0700454 const int kContentCnt = kLast_Content + 1;
robertphillips7eacd772014-08-21 13:12:42 -0700455
456 enum Layout {
457 kSimple_Layout,
458 kTiled_Layout,
459
460 kLast_Layout = kTiled_Layout
461 };
462
mtkleindbfd7ab2016-09-01 11:24:54 -0700463 const int kLayoutCnt = kLast_Layout + 1;
robertphillips7eacd772014-08-21 13:12:42 -0700464
465 MultiPictureDraw(Content content, Layout layout) : fContent(content), fLayout(layout) {
466 SkASSERT(SK_ARRAY_COUNT(gLayoutMthds) == kLayoutCnt);
467 SkASSERT(SK_ARRAY_COUNT(gContentMthds) == kContentCnt);
468
robertphillips3bc25e72014-09-19 08:56:09 -0700469 for (int i = 0; i < kNumPictures; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -0700470 fPictures[i] = nullptr;
robertphillips3bc25e72014-09-19 08:56:09 -0700471 }
robertphillips7eacd772014-08-21 13:12:42 -0700472 }
473
Brian Salomond3b65972017-03-22 12:05:03 -0400474 ~MultiPictureDraw() override {
robertphillips3bc25e72014-09-19 08:56:09 -0700475 for (int i = 0; i < kNumPictures; ++i) {
476 SkSafeUnref(fPictures[i]);
477 }
robertphillips7eacd772014-08-21 13:12:42 -0700478 }
479
480 protected:
481 Content fContent;
482 Layout fLayout;
robertphillips3bc25e72014-09-19 08:56:09 -0700483 const SkPicture* fPictures[kNumPictures];
robertphillips7eacd772014-08-21 13:12:42 -0700484
mtklein36352bf2015-03-25 18:17:31 -0700485 void onOnceBeforeDraw() override {
reedca2622b2016-03-18 07:25:55 -0700486 fPictures[0] = make_hex_plane_picture(SK_ColorWHITE).release();
487 fPictures[1] = make_hex_plane_picture(sk_tool_utils::color_to_565(SK_ColorGRAY)).release();
488 fPictures[2] = make_sierpinski_picture().release();
489 fPictures[3] = make_single_layer_hex_plane_picture().release();
robertphillips7eacd772014-08-21 13:12:42 -0700490 }
491
mtklein36352bf2015-03-25 18:17:31 -0700492 void onDraw(SkCanvas* canvas) override {
robertphillips7eacd772014-08-21 13:12:42 -0700493 SkMultiPictureDraw mpd;
494 SkTArray<ComposeStep> composeSteps;
495
496 // Fill up the MultiPictureDraw
piotaixrb5fae932014-09-24 13:03:30 -0700497 (*gLayoutMthds[fLayout])(canvas, &mpd,
498 gContentMthds[fContent],
robertphillips7eacd772014-08-21 13:12:42 -0700499 fPictures, &composeSteps);
500
501 mpd.draw();
502
503 // Compose all the drawn canvases into the final canvas
504 for (int i = 0; i < composeSteps.count(); ++i) {
505 const ComposeStep& step = composeSteps[i];
506
reed9ce9d672016-03-17 10:51:11 -0700507 canvas->drawImage(step.fSurf->makeImageSnapshot().get(),
508 step.fX, step.fY, step.fPaint);
robertphillips7eacd772014-08-21 13:12:42 -0700509 }
510 }
511
mtklein36352bf2015-03-25 18:17:31 -0700512 SkISize onISize() override { return SkISize::Make(kPicWidth, kPicHeight); }
robertphillips7eacd772014-08-21 13:12:42 -0700513
mtklein36352bf2015-03-25 18:17:31 -0700514 SkString onShortName() override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700515 const char* gContentNames[] = {
halcanary9d524f22016-03-29 09:03:52 -0700516 "noclip", "rectclip", "rrectclip", "pathclip",
robertphillipsb1fc64b2014-10-02 08:32:43 -0700517 "invpathclip", "sierpinski", "biglayer"
robertphillips7eacd772014-08-21 13:12:42 -0700518 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700519 const char* gLayoutNames[] = { "simple", "tiled" };
robertphillips7eacd772014-08-21 13:12:42 -0700520
521 SkASSERT(SK_ARRAY_COUNT(gLayoutNames) == kLayoutCnt);
522 SkASSERT(SK_ARRAY_COUNT(gContentNames) == kContentCnt);
523
524 SkString name("multipicturedraw_");
525
526 name.append(gContentNames[fContent]);
527 name.append("_");
528 name.append(gLayoutNames[fLayout]);
529 return name;
530 }
531
mtklein36352bf2015-03-25 18:17:31 -0700532 bool runAsBench() const override { return true; }
robertphillips7eacd772014-08-21 13:12:42 -0700533
534 private:
535 typedef GM INHERITED;
536 };
537
halcanary385fe4d2015-08-26 13:07:48 -0700538 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
539 MultiPictureDraw::kSimple_Layout);)
540 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
541 MultiPictureDraw::kSimple_Layout);)
542 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
543 MultiPictureDraw::kSimple_Layout);)
544 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
545 MultiPictureDraw::kSimple_Layout);)
546 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
547 MultiPictureDraw::kSimple_Layout);)
548 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
549 MultiPictureDraw::kSimple_Layout);)
550 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
551 MultiPictureDraw::kSimple_Layout);)
robertphillips7eacd772014-08-21 13:12:42 -0700552
halcanary385fe4d2015-08-26 13:07:48 -0700553 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
554 MultiPictureDraw::kTiled_Layout);)
555 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
556 MultiPictureDraw::kTiled_Layout);)
557 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
558 MultiPictureDraw::kTiled_Layout);)
559 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
560 MultiPictureDraw::kTiled_Layout);)
561 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
562 MultiPictureDraw::kTiled_Layout);)
563 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
564 MultiPictureDraw::kTiled_Layout);)
565 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
566 MultiPictureDraw::kTiled_Layout);)
robertphillips7eacd772014-08-21 13:12:42 -0700567}