blob: 35f4c12986a1b49a9480719a0c5bd5a4c2b8c5f9 [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"
9
10#include "SkColorFilter.h"
11#include "SkMultiPictureDraw.h"
12#include "SkPictureRecorder.h"
13#include "SkSurface.h"
14
mtkleindbfd7ab2016-09-01 11:24:54 -070015constexpr SkScalar kRoot3Over2 = 0.86602545f; // sin(60)
16constexpr SkScalar kRoot3 = 1.73205081f;
robertphillips7eacd772014-08-21 13:12:42 -070017
mtkleindbfd7ab2016-09-01 11:24:54 -070018constexpr int kHexSide = 30;
19constexpr int kNumHexX = 6;
20constexpr int kNumHexY = 6;
21constexpr int kPicWidth = kNumHexX * kHexSide;
22constexpr int kPicHeight = (int)((kNumHexY - 0.5f) * 2 * kHexSide * kRoot3Over2 + 0.5f);
23constexpr SkScalar kInset = 20.0f;
24constexpr int kNumPictures = 4;
robertphillips3bc25e72014-09-19 08:56:09 -070025
mtkleindbfd7ab2016-09-01 11:24:54 -070026constexpr int kTriSide = 40;
robertphillips7eacd772014-08-21 13:12:42 -070027
28// Create a hexagon centered at (originX, originY)
29static SkPath make_hex_path(SkScalar originX, SkScalar originY) {
30 SkPath hex;
31 hex.moveTo(originX-kHexSide, originY);
32 hex.rLineTo(SkScalarHalf(kHexSide), kRoot3Over2 * kHexSide);
33 hex.rLineTo(SkIntToScalar(kHexSide), 0);
34 hex.rLineTo(SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
35 hex.rLineTo(-SkScalarHalf(kHexSide), -kHexSide * kRoot3Over2);
36 hex.rLineTo(-SkIntToScalar(kHexSide), 0);
37 hex.close();
38 return hex;
39}
40
41// Make a picture that is a tiling of the plane with stroked hexagons where
42// each hexagon is in its own layer. The layers are to exercise Ganesh's
43// layer hoisting.
reedca2622b2016-03-18 07:25:55 -070044static sk_sp<SkPicture> make_hex_plane_picture(SkColor fillColor) {
robertphillips7eacd772014-08-21 13:12:42 -070045
46 // Create a hexagon with its center at the origin
47 SkPath hex = make_hex_path(0, 0);
48
49 SkPaint fill;
50 fill.setStyle(SkPaint::kFill_Style);
51 fill.setColor(fillColor);
52
53 SkPaint stroke;
54 stroke.setStyle(SkPaint::kStroke_Style);
55 stroke.setStrokeWidth(3);
56
57 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -080058 SkRTreeFactory bbhFactory;
robertphillips7eacd772014-08-21 13:12:42 -070059
piotaixrb5fae932014-09-24 13:03:30 -070060 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -080061 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -070062 &bbhFactory);
robertphillips7eacd772014-08-21 13:12:42 -070063
64 SkScalar xPos, yPos = 0;
65
66 for (int y = 0; y < kNumHexY; ++y) {
67 xPos = 0;
68
69 for (int x = 0; x < kNumHexX; ++x) {
halcanary96fcdcc2015-08-27 07:41:13 -070070 canvas->saveLayer(nullptr, nullptr);
robertphillips7eacd772014-08-21 13:12:42 -070071 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
72 canvas->drawPath(hex, fill);
73 canvas->drawPath(hex, stroke);
74 canvas->restore();
75
76 xPos += 1.5f * kHexSide;
77 }
78
79 yPos += 2 * kHexSide * kRoot3Over2;
80 }
81
reedca2622b2016-03-18 07:25:55 -070082 return recorder.finishRecordingAsPicture();
robertphillips7eacd772014-08-21 13:12:42 -070083}
84
robertphillipsb1fc64b2014-10-02 08:32:43 -070085// Create a picture that consists of a single large layer that is tiled
86// with hexagons.
87// This is intended to exercise the layer hoisting code's clip handling (in
88// tile mode).
reedca2622b2016-03-18 07:25:55 -070089static sk_sp<SkPicture> make_single_layer_hex_plane_picture() {
robertphillipsb1fc64b2014-10-02 08:32:43 -070090
91 // Create a hexagon with its center at the origin
92 SkPath hex = make_hex_path(0, 0);
93
94 SkPaint whiteFill;
95 whiteFill.setStyle(SkPaint::kFill_Style);
96 whiteFill.setColor(SK_ColorWHITE);
97
98 SkPaint greyFill;
99 greyFill.setStyle(SkPaint::kFill_Style);
caryclarkf597c422015-07-28 10:37:53 -0700100 greyFill.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
robertphillipsb1fc64b2014-10-02 08:32:43 -0700101
102 SkPaint stroke;
103 stroke.setStyle(SkPaint::kStroke_Style);
104 stroke.setStrokeWidth(3);
105
106 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800107 SkRTreeFactory bbhFactory;
robertphillipsb1fc64b2014-10-02 08:32:43 -0700108
mtkleindbfd7ab2016-09-01 11:24:54 -0700109 constexpr SkScalar kBig = 10000.0f;
robertphillipsdda54452016-07-13 13:27:16 -0700110 SkCanvas* canvas = recorder.beginRecording(kBig, kBig, &bbhFactory);
robertphillipsb1fc64b2014-10-02 08:32:43 -0700111
halcanary96fcdcc2015-08-27 07:41:13 -0700112 canvas->saveLayer(nullptr, nullptr);
robertphillips1564dde2014-10-06 11:50:31 -0700113
robertphillipsb1fc64b2014-10-02 08:32:43 -0700114 SkScalar xPos = 0.0f, yPos = 0.0f;
115
116 for (int y = 0; yPos < kBig; ++y) {
117 xPos = 0;
118
119 for (int x = 0; xPos < kBig; ++x) {
120 canvas->save();
121 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
122 // The color of the filled hex is swapped to yield a different
123 // pattern in each tile. This allows an error in layer hoisting (e.g.,
124 // the clip isn't blocking cache reuse) to cause a visual discrepancy.
125 canvas->drawPath(hex, ((x+y) % 3) ? whiteFill : greyFill);
126 canvas->drawPath(hex, stroke);
127 canvas->restore();
128
129 xPos += 1.5f * kHexSide;
130 }
131
132 yPos += 2 * kHexSide * kRoot3Over2;
133 }
134
robertphillips1564dde2014-10-06 11:50:31 -0700135 canvas->restore();
136
reedca2622b2016-03-18 07:25:55 -0700137 return recorder.finishRecordingAsPicture();
robertphillipsb1fc64b2014-10-02 08:32:43 -0700138}
139
robertphillips3bc25e72014-09-19 08:56:09 -0700140// Make an equilateral triangle path with its top corner at (originX, originY)
141static SkPath make_tri_path(SkScalar originX, SkScalar originY) {
142 SkPath tri;
143 tri.moveTo(originX, originY);
144 tri.rLineTo(SkScalarHalf(kTriSide), 1.5f * kTriSide / kRoot3);
145 tri.rLineTo(-kTriSide, 0);
146 tri.close();
147 return tri;
148}
149
reedca2622b2016-03-18 07:25:55 -0700150static sk_sp<SkPicture> make_tri_picture() {
robertphillipsaa0c8372014-09-29 05:07:39 -0700151 SkPath tri = make_tri_path(SkScalarHalf(kTriSide), 0);
robertphillips3bc25e72014-09-19 08:56:09 -0700152
153 SkPaint fill;
154 fill.setStyle(SkPaint::kFill_Style);
caryclarkf597c422015-07-28 10:37:53 -0700155 fill.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
robertphillips3bc25e72014-09-19 08:56:09 -0700156
157 SkPaint stroke;
158 stroke.setStyle(SkPaint::kStroke_Style);
159 stroke.setStrokeWidth(3);
160
161 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800162 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700163
164 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800165 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700166 &bbhFactory);
robertphillips7f1ed832014-10-03 07:29:20 -0700167 SkRect r = tri.getBounds();
168 r.outset(2.0f, 2.0f); // outset for stroke
169 canvas->clipRect(r);
robertphillips3bc25e72014-09-19 08:56:09 -0700170 // The saveLayer/restore block is to exercise layer hoisting
halcanary96fcdcc2015-08-27 07:41:13 -0700171 canvas->saveLayer(nullptr, nullptr);
robertphillips3bc25e72014-09-19 08:56:09 -0700172 canvas->drawPath(tri, fill);
173 canvas->drawPath(tri, stroke);
174 canvas->restore();
175
reedca2622b2016-03-18 07:25:55 -0700176 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700177}
178
reedca2622b2016-03-18 07:25:55 -0700179static sk_sp<SkPicture> make_sub_picture(const SkPicture* tri) {
robertphillips3bc25e72014-09-19 08:56:09 -0700180 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800181 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700182
183 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800184 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700185 &bbhFactory);
robertphillips3bc25e72014-09-19 08:56:09 -0700186
187 canvas->scale(1.0f/2.0f, 1.0f/2.0f);
188
robertphillips3bc25e72014-09-19 08:56:09 -0700189 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700190 canvas->translate(SkScalarHalf(kTriSide), 0);
robertphillips3bc25e72014-09-19 08:56:09 -0700191 canvas->drawPicture(tri);
192 canvas->restore();
193
194 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700195 canvas->translate(SkIntToScalar(kTriSide), 1.5f * kTriSide / kRoot3);
196 canvas->drawPicture(tri);
197 canvas->restore();
198
199 canvas->save();
200 canvas->translate(0, 1.5f * kTriSide / kRoot3);
robertphillips3bc25e72014-09-19 08:56:09 -0700201 canvas->drawPicture(tri);
202 canvas->restore();
203
reedca2622b2016-03-18 07:25:55 -0700204 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700205}
206
207// Create a Sierpinkski-like picture that starts with a top row with a picture
piotaixrb5fae932014-09-24 13:03:30 -0700208// that just contains a triangle. Subsequent rows take the prior row's picture,
robertphillips3bc25e72014-09-19 08:56:09 -0700209// shrinks it and replicates it 3 times then draws and appropriate number of
210// copies of it.
reedca2622b2016-03-18 07:25:55 -0700211static sk_sp<SkPicture> make_sierpinski_picture() {
212 sk_sp<SkPicture> pic(make_tri_picture());
robertphillips3bc25e72014-09-19 08:56:09 -0700213
214 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800215 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700216
217 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800218 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700219 &bbhFactory);
robertphillips3bc25e72014-09-19 08:56:09 -0700220
mtkleindbfd7ab2016-09-01 11:24:54 -0700221 constexpr int kNumLevels = 4;
robertphillips3bc25e72014-09-19 08:56:09 -0700222 for (int i = 0; i < kNumLevels; ++i) {
223 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700224 canvas->translate(kPicWidth/2 - (i+1) * (kTriSide/2.0f), 0.0f);
robertphillips3bc25e72014-09-19 08:56:09 -0700225 for (int j = 0; j < i+1; ++j) {
226 canvas->drawPicture(pic);
227 canvas->translate(SkIntToScalar(kTriSide), 0);
228 }
229 canvas->restore();
230
reedca2622b2016-03-18 07:25:55 -0700231 pic = make_sub_picture(pic.get());
robertphillips3bc25e72014-09-19 08:56:09 -0700232
233 canvas->translate(0, 1.5f * kTriSide / kRoot3);
234 }
235
reedca2622b2016-03-18 07:25:55 -0700236 return recorder.finishRecordingAsPicture();
robertphillips3bc25e72014-09-19 08:56:09 -0700237}
238
reede8f30622016-03-23 18:59:25 -0700239static sk_sp<SkSurface> create_compat_surface(SkCanvas* canvas, int width, int height) {
robertphillips7eacd772014-08-21 13:12:42 -0700240 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
241
reede8f30622016-03-23 18:59:25 -0700242 auto surface = canvas->makeSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -0700243 if (nullptr == surface) {
244 // picture canvas returns nullptr so fall back to raster
reede8f30622016-03-23 18:59:25 -0700245 surface = SkSurface::MakeRaster(info);
robertphillips7eacd772014-08-21 13:12:42 -0700246 }
robertphillips7eacd772014-08-21 13:12:42 -0700247 return surface;
248}
249
250// This class stores the information required to compose all the result
251// fragments potentially generated by the MultiPictureDraw object
252class ComposeStep {
253public:
reede8f30622016-03-23 18:59:25 -0700254 ComposeStep() : fX(0.0f), fY(0.0f), fPaint(nullptr) { }
halcanary385fe4d2015-08-26 13:07:48 -0700255 ~ComposeStep() {
halcanary385fe4d2015-08-26 13:07:48 -0700256 delete fPaint;
257 }
robertphillips7eacd772014-08-21 13:12:42 -0700258
reede8f30622016-03-23 18:59:25 -0700259 sk_sp<SkSurface> fSurf;
robertphillips7eacd772014-08-21 13:12:42 -0700260 SkScalar fX;
261 SkScalar fY;
262 SkPaint* fPaint;
263};
264
robertphillips3bc25e72014-09-19 08:56:09 -0700265typedef void (*PFContentMtd)(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]);
robertphillips7eacd772014-08-21 13:12:42 -0700266
267// Just a single picture with no clip
robertphillips3bc25e72014-09-19 08:56:09 -0700268static void no_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700269 canvas->drawPicture(pictures[0]);
270}
271
272// Two pictures with a rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700273static void rect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700274 canvas->drawPicture(pictures[0]);
275
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700276 SkRect rect = pictures[0]->cullRect();
robertphillips7eacd772014-08-21 13:12:42 -0700277 rect.inset(kInset, kInset);
278
279 canvas->clipRect(rect);
280
281 canvas->drawPicture(pictures[1]);
282}
283
284// Two pictures with a round rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700285static void rrect_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);
piotaixrb5fae932014-09-24 13:03:30 -0700290
robertphillips7eacd772014-08-21 13:12:42 -0700291 SkRRect rrect;
292 rrect.setRectXY(rect, kInset, kInset);
293
294 canvas->clipRRect(rrect);
295
296 canvas->drawPicture(pictures[1]);
297}
298
299// Two pictures with a clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700300static void path_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700301 canvas->drawPicture(pictures[0]);
302
303 // Create a hexagon centered on the middle of the hex grid
304 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
305
306 canvas->clipPath(hex);
307
308 canvas->drawPicture(pictures[1]);
309}
310
311// Two pictures with an inverse clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700312static void invpath_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 hex.setFillType(SkPath::kInverseEvenOdd_FillType);
318
319 canvas->clipPath(hex);
320
321 canvas->drawPicture(pictures[1]);
322}
323
robertphillips3bc25e72014-09-19 08:56:09 -0700324// Reuse a single base (triangular) picture a _lot_ (rotated, scaled and translated).
325static void sierpinski(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
326 canvas->save();
robertphillips3bc25e72014-09-19 08:56:09 -0700327 canvas->drawPicture(pictures[2]);
328
329 canvas->rotate(180.0f);
robertphillipsaa0c8372014-09-29 05:07:39 -0700330 canvas->translate(-SkIntToScalar(kPicWidth), -SkIntToScalar(kPicHeight));
robertphillips3bc25e72014-09-19 08:56:09 -0700331 canvas->drawPicture(pictures[2]);
332 canvas->restore();
333}
334
robertphillipsb1fc64b2014-10-02 08:32:43 -0700335static void big_layer(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
336 canvas->drawPicture(pictures[3]);
337}
338
mtkleindbfd7ab2016-09-01 11:24:54 -0700339constexpr PFContentMtd gContentMthds[] = {
robertphillips7eacd772014-08-21 13:12:42 -0700340 no_clip,
341 rect_clip,
342 rrect_clip,
343 path_clip,
robertphillips3bc25e72014-09-19 08:56:09 -0700344 invpath_clip,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700345 sierpinski,
346 big_layer,
robertphillips7eacd772014-08-21 13:12:42 -0700347};
348
349static void create_content(SkMultiPictureDraw* mpd, PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700350 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700351 SkCanvas* dest, const SkMatrix& xform) {
reedca2622b2016-03-18 07:25:55 -0700352 sk_sp<SkPicture> composite;
robertphillips7eacd772014-08-21 13:12:42 -0700353
354 {
355 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800356 SkRTreeFactory bbhFactory;
robertphillips7eacd772014-08-21 13:12:42 -0700357
piotaixrb5fae932014-09-24 13:03:30 -0700358 SkCanvas* pictureCanvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800359 SkIntToScalar(kPicHeight),
robertphillipsdda54452016-07-13 13:27:16 -0700360 &bbhFactory);
robertphillips7eacd772014-08-21 13:12:42 -0700361
362 (*pfGen)(pictureCanvas, pictures);
363
reedca2622b2016-03-18 07:25:55 -0700364 composite = recorder.finishRecordingAsPicture();
robertphillips7eacd772014-08-21 13:12:42 -0700365 }
366
reedca2622b2016-03-18 07:25:55 -0700367 mpd->add(dest, composite.get(), &xform);
robertphillips7eacd772014-08-21 13:12:42 -0700368}
369
piotaixrb5fae932014-09-24 13:03:30 -0700370typedef void(*PFLayoutMtd)(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
robertphillips3bc25e72014-09-19 08:56:09 -0700371 PFContentMtd pfGen, const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700372 SkTArray<ComposeStep>* composeSteps);
373
374// Draw the content into a single canvas
piotaixrb5fae932014-09-24 13:03:30 -0700375static void simple(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
376 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700377 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700378 SkTArray<ComposeStep> *composeSteps) {
379
380 ComposeStep& step = composeSteps->push_back();
381
bsalomon892f31a2014-08-21 14:40:36 -0700382 step.fSurf = create_compat_surface(finalCanvas, kPicWidth, kPicHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700383
384 SkCanvas* subCanvas = step.fSurf->getCanvas();
385
386 create_content(mpd, pfGen, pictures, subCanvas, SkMatrix::I());
387}
388
389// Draw the content into multiple canvases/tiles
390static void tiled(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
piotaixrb5fae932014-09-24 13:03:30 -0700391 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700392 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700393 SkTArray<ComposeStep> *composeSteps) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700394 const int kNumTilesX = 2;
395 const int kNumTilesY = 2;
396 const int kTileWidth = kPicWidth / kNumTilesX;
397 const int kTileHeight = kPicHeight / kNumTilesY;
robertphillips7eacd772014-08-21 13:12:42 -0700398
399 SkASSERT(kPicWidth == kNumTilesX * kTileWidth);
400 SkASSERT(kPicHeight == kNumTilesY * kTileHeight);
401
mtkleindbfd7ab2016-09-01 11:24:54 -0700402 const SkColor colors[kNumTilesX][kNumTilesY] = {
piotaixrb5fae932014-09-24 13:03:30 -0700403 { SK_ColorCYAN, SK_ColorMAGENTA },
robertphillips7eacd772014-08-21 13:12:42 -0700404 { SK_ColorYELLOW, SK_ColorGREEN }
405 };
406
407 for (int y = 0; y < kNumTilesY; ++y) {
408 for (int x = 0; x < kNumTilesX; ++x) {
409 ComposeStep& step = composeSteps->push_back();
410
411 step.fX = SkIntToScalar(x*kTileWidth);
412 step.fY = SkIntToScalar(y*kTileHeight);
halcanary385fe4d2015-08-26 13:07:48 -0700413 step.fPaint = new SkPaint;
robertphillips7eacd772014-08-21 13:12:42 -0700414 step.fPaint->setColorFilter(
Mike Reed7d954ad2016-10-28 15:42:34 -0400415 SkColorFilter::MakeModeFilter(colors[x][y], SkBlendMode::kModulate));
robertphillips7eacd772014-08-21 13:12:42 -0700416
bsalomon892f31a2014-08-21 14:40:36 -0700417 step.fSurf = create_compat_surface(finalCanvas, kTileWidth, kTileHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700418
419 SkCanvas* subCanvas = step.fSurf->getCanvas();
420
halcanary9d524f22016-03-29 09:03:52 -0700421 const SkMatrix trans = SkMatrix::MakeTrans(-SkIntToScalar(x*kTileWidth),
robertphillips1d24b8d2015-03-26 19:57:08 -0700422 -SkIntToScalar(y*kTileHeight));
robertphillips7eacd772014-08-21 13:12:42 -0700423
424 create_content(mpd, pfGen, pictures, subCanvas, trans);
425 }
426 }
427}
428
mtkleindbfd7ab2016-09-01 11:24:54 -0700429constexpr PFLayoutMtd gLayoutMthds[] = { simple, tiled };
robertphillips7eacd772014-08-21 13:12:42 -0700430
431namespace skiagm {
432 /**
433 * This GM exercises the SkMultiPictureDraw object. It tests the
434 * cross product of:
435 * tiled vs. all-at-once rendering (e.g., into many or just 1 canvas)
436 * different clips (e.g., none, rect, rrect)
437 * single vs. multiple pictures (e.g., normal vs. picture-pile-style content)
438 */
439 class MultiPictureDraw : public GM {
440 public:
441 enum Content {
442 kNoClipSingle_Content,
443 kRectClipMulti_Content,
444 kRRectClipMulti_Content,
445 kPathClipMulti_Content,
446 kInvPathClipMulti_Content,
robertphillips3bc25e72014-09-19 08:56:09 -0700447 kSierpinski_Content,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700448 kBigLayer_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700449
robertphillipsb1fc64b2014-10-02 08:32:43 -0700450 kLast_Content = kBigLayer_Content
robertphillips7eacd772014-08-21 13:12:42 -0700451 };
452
mtkleindbfd7ab2016-09-01 11:24:54 -0700453 const int kContentCnt = kLast_Content + 1;
robertphillips7eacd772014-08-21 13:12:42 -0700454
455 enum Layout {
456 kSimple_Layout,
457 kTiled_Layout,
458
459 kLast_Layout = kTiled_Layout
460 };
461
mtkleindbfd7ab2016-09-01 11:24:54 -0700462 const int kLayoutCnt = kLast_Layout + 1;
robertphillips7eacd772014-08-21 13:12:42 -0700463
464 MultiPictureDraw(Content content, Layout layout) : fContent(content), fLayout(layout) {
465 SkASSERT(SK_ARRAY_COUNT(gLayoutMthds) == kLayoutCnt);
466 SkASSERT(SK_ARRAY_COUNT(gContentMthds) == kContentCnt);
467
robertphillips3bc25e72014-09-19 08:56:09 -0700468 for (int i = 0; i < kNumPictures; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -0700469 fPictures[i] = nullptr;
robertphillips3bc25e72014-09-19 08:56:09 -0700470 }
robertphillips7eacd772014-08-21 13:12:42 -0700471 }
472
473 virtual ~MultiPictureDraw() {
robertphillips3bc25e72014-09-19 08:56:09 -0700474 for (int i = 0; i < kNumPictures; ++i) {
475 SkSafeUnref(fPictures[i]);
476 }
robertphillips7eacd772014-08-21 13:12:42 -0700477 }
478
479 protected:
480 Content fContent;
481 Layout fLayout;
robertphillips3bc25e72014-09-19 08:56:09 -0700482 const SkPicture* fPictures[kNumPictures];
robertphillips7eacd772014-08-21 13:12:42 -0700483
mtklein36352bf2015-03-25 18:17:31 -0700484 void onOnceBeforeDraw() override {
reedca2622b2016-03-18 07:25:55 -0700485 fPictures[0] = make_hex_plane_picture(SK_ColorWHITE).release();
486 fPictures[1] = make_hex_plane_picture(sk_tool_utils::color_to_565(SK_ColorGRAY)).release();
487 fPictures[2] = make_sierpinski_picture().release();
488 fPictures[3] = make_single_layer_hex_plane_picture().release();
robertphillips7eacd772014-08-21 13:12:42 -0700489 }
490
mtklein36352bf2015-03-25 18:17:31 -0700491 void onDraw(SkCanvas* canvas) override {
robertphillips7eacd772014-08-21 13:12:42 -0700492 SkMultiPictureDraw mpd;
493 SkTArray<ComposeStep> composeSteps;
494
495 // Fill up the MultiPictureDraw
piotaixrb5fae932014-09-24 13:03:30 -0700496 (*gLayoutMthds[fLayout])(canvas, &mpd,
497 gContentMthds[fContent],
robertphillips7eacd772014-08-21 13:12:42 -0700498 fPictures, &composeSteps);
499
500 mpd.draw();
501
502 // Compose all the drawn canvases into the final canvas
503 for (int i = 0; i < composeSteps.count(); ++i) {
504 const ComposeStep& step = composeSteps[i];
505
reed9ce9d672016-03-17 10:51:11 -0700506 canvas->drawImage(step.fSurf->makeImageSnapshot().get(),
507 step.fX, step.fY, step.fPaint);
robertphillips7eacd772014-08-21 13:12:42 -0700508 }
509 }
510
mtklein36352bf2015-03-25 18:17:31 -0700511 SkISize onISize() override { return SkISize::Make(kPicWidth, kPicHeight); }
robertphillips7eacd772014-08-21 13:12:42 -0700512
mtklein36352bf2015-03-25 18:17:31 -0700513 SkString onShortName() override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700514 const char* gContentNames[] = {
halcanary9d524f22016-03-29 09:03:52 -0700515 "noclip", "rectclip", "rrectclip", "pathclip",
robertphillipsb1fc64b2014-10-02 08:32:43 -0700516 "invpathclip", "sierpinski", "biglayer"
robertphillips7eacd772014-08-21 13:12:42 -0700517 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700518 const char* gLayoutNames[] = { "simple", "tiled" };
robertphillips7eacd772014-08-21 13:12:42 -0700519
520 SkASSERT(SK_ARRAY_COUNT(gLayoutNames) == kLayoutCnt);
521 SkASSERT(SK_ARRAY_COUNT(gContentNames) == kContentCnt);
522
523 SkString name("multipicturedraw_");
524
525 name.append(gContentNames[fContent]);
526 name.append("_");
527 name.append(gLayoutNames[fLayout]);
528 return name;
529 }
530
mtklein36352bf2015-03-25 18:17:31 -0700531 bool runAsBench() const override { return true; }
robertphillips7eacd772014-08-21 13:12:42 -0700532
533 private:
534 typedef GM INHERITED;
535 };
536
halcanary385fe4d2015-08-26 13:07:48 -0700537 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
538 MultiPictureDraw::kSimple_Layout);)
539 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
540 MultiPictureDraw::kSimple_Layout);)
541 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
542 MultiPictureDraw::kSimple_Layout);)
543 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
544 MultiPictureDraw::kSimple_Layout);)
545 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
546 MultiPictureDraw::kSimple_Layout);)
547 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
548 MultiPictureDraw::kSimple_Layout);)
549 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
550 MultiPictureDraw::kSimple_Layout);)
robertphillips7eacd772014-08-21 13:12:42 -0700551
halcanary385fe4d2015-08-26 13:07:48 -0700552 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
553 MultiPictureDraw::kTiled_Layout);)
554 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
555 MultiPictureDraw::kTiled_Layout);)
556 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
557 MultiPictureDraw::kTiled_Layout);)
558 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
559 MultiPictureDraw::kTiled_Layout);)
560 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
561 MultiPictureDraw::kTiled_Layout);)
562 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
563 MultiPictureDraw::kTiled_Layout);)
564 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
565 MultiPictureDraw::kTiled_Layout);)
robertphillips7eacd772014-08-21 13:12:42 -0700566}