blob: 0e11b0653f1304b7559da92819becbf4c9402be7 [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
15static const SkScalar kRoot3Over2 = 0.86602545f; // sin(60)
robertphillips3bc25e72014-09-19 08:56:09 -070016static const SkScalar kRoot3 = 1.73205081f;
robertphillips7eacd772014-08-21 13:12:42 -070017
18static const int kHexSide = 30;
19static const int kNumHexX = 6;
20static const int kNumHexY = 6;
21static const int kPicWidth = kNumHexX * kHexSide;
22static const int kPicHeight = SkScalarCeilToInt((kNumHexY - 0.5f) * 2 * kHexSide * kRoot3Over2);
23static const SkScalar kInset = 20.0f;
robertphillipsb1fc64b2014-10-02 08:32:43 -070024static const int kNumPictures = 4;
robertphillips3bc25e72014-09-19 08:56:09 -070025
26static const 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.
robertphillips3bc25e72014-09-19 08:56:09 -070044static const 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),
62 &bbhFactory,
63 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag);
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) {
71 canvas->saveLayer(NULL, NULL);
72 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
83 return recorder.endRecording();
84}
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).
90static const SkPicture* make_single_layer_hex_plane_picture() {
91
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);
101 greyFill.setColor(SK_ColorLTGRAY);
102
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
110 static const SkScalar kBig = 10000.0f;
robertphillips81f71b62014-11-11 04:54:49 -0800111 SkCanvas* canvas = recorder.beginRecording(kBig, kBig, &bbhFactory,
112 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag);
robertphillipsb1fc64b2014-10-02 08:32:43 -0700113
robertphillips1564dde2014-10-06 11:50:31 -0700114 canvas->saveLayer(NULL, NULL);
115
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
robertphillipsb1fc64b2014-10-02 08:32:43 -0700139 return recorder.endRecording();
140}
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
152static const 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);
tfarina567ff2f2015-04-27 07:01:44 -0700157 fill.setColor(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),
168 &bbhFactory,
169 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag);
robertphillips7f1ed832014-10-03 07:29:20 -0700170 SkRect r = tri.getBounds();
171 r.outset(2.0f, 2.0f); // outset for stroke
172 canvas->clipRect(r);
robertphillips3bc25e72014-09-19 08:56:09 -0700173 // The saveLayer/restore block is to exercise layer hoisting
174 canvas->saveLayer(NULL, NULL);
175 canvas->drawPath(tri, fill);
176 canvas->drawPath(tri, stroke);
177 canvas->restore();
178
179 return recorder.endRecording();
180}
181
182static const SkPicture* make_sub_picture(const SkPicture* tri) {
183 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800184 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700185
186 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800187 SkIntToScalar(kPicHeight),
188 &bbhFactory,
189 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag);
robertphillips3bc25e72014-09-19 08:56:09 -0700190
191 canvas->scale(1.0f/2.0f, 1.0f/2.0f);
192
robertphillips3bc25e72014-09-19 08:56:09 -0700193 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700194 canvas->translate(SkScalarHalf(kTriSide), 0);
robertphillips3bc25e72014-09-19 08:56:09 -0700195 canvas->drawPicture(tri);
196 canvas->restore();
197
198 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700199 canvas->translate(SkIntToScalar(kTriSide), 1.5f * kTriSide / kRoot3);
200 canvas->drawPicture(tri);
201 canvas->restore();
202
203 canvas->save();
204 canvas->translate(0, 1.5f * kTriSide / kRoot3);
robertphillips3bc25e72014-09-19 08:56:09 -0700205 canvas->drawPicture(tri);
206 canvas->restore();
207
208 return recorder.endRecording();
209}
210
211// Create a Sierpinkski-like picture that starts with a top row with a picture
piotaixrb5fae932014-09-24 13:03:30 -0700212// that just contains a triangle. Subsequent rows take the prior row's picture,
robertphillips3bc25e72014-09-19 08:56:09 -0700213// shrinks it and replicates it 3 times then draws and appropriate number of
214// copies of it.
215static const SkPicture* make_sierpinski_picture() {
216 SkAutoTUnref<const SkPicture> pic(make_tri_picture());
217
218 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800219 SkRTreeFactory bbhFactory;
robertphillips3bc25e72014-09-19 08:56:09 -0700220
221 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800222 SkIntToScalar(kPicHeight),
223 &bbhFactory,
224 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag);
robertphillips3bc25e72014-09-19 08:56:09 -0700225
226 static const int kNumLevels = 4;
227 for (int i = 0; i < kNumLevels; ++i) {
228 canvas->save();
robertphillipsaa0c8372014-09-29 05:07:39 -0700229 canvas->translate(kPicWidth/2 - (i+1) * (kTriSide/2.0f), 0.0f);
robertphillips3bc25e72014-09-19 08:56:09 -0700230 for (int j = 0; j < i+1; ++j) {
231 canvas->drawPicture(pic);
232 canvas->translate(SkIntToScalar(kTriSide), 0);
233 }
234 canvas->restore();
235
236 pic.reset(make_sub_picture(pic));
237
238 canvas->translate(0, 1.5f * kTriSide / kRoot3);
239 }
240
241 return recorder.endRecording();
242}
243
bsalomon892f31a2014-08-21 14:40:36 -0700244static SkSurface* create_compat_surface(SkCanvas* canvas, int width, int height) {
robertphillips7eacd772014-08-21 13:12:42 -0700245 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
246
247 SkSurface* surface = canvas->newSurface(info);
248 if (NULL == surface) {
249 // picture canvas returns NULL so fall back to raster
250 surface = SkSurface::NewRaster(info);
251 }
252
253 return surface;
254}
255
256// This class stores the information required to compose all the result
257// fragments potentially generated by the MultiPictureDraw object
258class ComposeStep {
259public:
260 ComposeStep() : fSurf(NULL), fX(0.0f), fY(0.0f), fPaint(NULL) { }
261 ~ComposeStep() { SkSafeUnref(fSurf); SkDELETE(fPaint); }
262
263 SkSurface* fSurf;
264 SkScalar fX;
265 SkScalar fY;
266 SkPaint* fPaint;
267};
268
robertphillips3bc25e72014-09-19 08:56:09 -0700269typedef void (*PFContentMtd)(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]);
robertphillips7eacd772014-08-21 13:12:42 -0700270
271// Just a single picture with no clip
robertphillips3bc25e72014-09-19 08:56:09 -0700272static void no_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700273 canvas->drawPicture(pictures[0]);
274}
275
276// Two pictures with a rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700277static void rect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700278 canvas->drawPicture(pictures[0]);
279
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700280 SkRect rect = pictures[0]->cullRect();
robertphillips7eacd772014-08-21 13:12:42 -0700281 rect.inset(kInset, kInset);
282
283 canvas->clipRect(rect);
284
285 canvas->drawPicture(pictures[1]);
286}
287
288// Two pictures with a round rect clip on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700289static void rrect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700290 canvas->drawPicture(pictures[0]);
291
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700292 SkRect rect = pictures[0]->cullRect();
robertphillips7eacd772014-08-21 13:12:42 -0700293 rect.inset(kInset, kInset);
piotaixrb5fae932014-09-24 13:03:30 -0700294
robertphillips7eacd772014-08-21 13:12:42 -0700295 SkRRect rrect;
296 rrect.setRectXY(rect, kInset, kInset);
297
298 canvas->clipRRect(rrect);
299
300 canvas->drawPicture(pictures[1]);
301}
302
303// Two pictures with a clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700304static void path_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700305 canvas->drawPicture(pictures[0]);
306
307 // Create a hexagon centered on the middle of the hex grid
308 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
309
310 canvas->clipPath(hex);
311
312 canvas->drawPicture(pictures[1]);
313}
314
315// Two pictures with an inverse clip path on the second one
robertphillips3bc25e72014-09-19 08:56:09 -0700316static void invpath_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
robertphillips7eacd772014-08-21 13:12:42 -0700317 canvas->drawPicture(pictures[0]);
318
319 // Create a hexagon centered on the middle of the hex grid
320 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
321 hex.setFillType(SkPath::kInverseEvenOdd_FillType);
322
323 canvas->clipPath(hex);
324
325 canvas->drawPicture(pictures[1]);
326}
327
robertphillips3bc25e72014-09-19 08:56:09 -0700328// Reuse a single base (triangular) picture a _lot_ (rotated, scaled and translated).
329static void sierpinski(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
330 canvas->save();
robertphillips3bc25e72014-09-19 08:56:09 -0700331 canvas->drawPicture(pictures[2]);
332
333 canvas->rotate(180.0f);
robertphillipsaa0c8372014-09-29 05:07:39 -0700334 canvas->translate(-SkIntToScalar(kPicWidth), -SkIntToScalar(kPicHeight));
robertphillips3bc25e72014-09-19 08:56:09 -0700335 canvas->drawPicture(pictures[2]);
336 canvas->restore();
337}
338
robertphillipsb1fc64b2014-10-02 08:32:43 -0700339static void big_layer(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
340 canvas->drawPicture(pictures[3]);
341}
342
robertphillips7eacd772014-08-21 13:12:42 -0700343static const PFContentMtd gContentMthds[] = {
344 no_clip,
345 rect_clip,
346 rrect_clip,
347 path_clip,
robertphillips3bc25e72014-09-19 08:56:09 -0700348 invpath_clip,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700349 sierpinski,
350 big_layer,
robertphillips7eacd772014-08-21 13:12:42 -0700351};
352
353static void create_content(SkMultiPictureDraw* mpd, PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700354 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700355 SkCanvas* dest, const SkMatrix& xform) {
356 SkAutoTUnref<SkPicture> composite;
357
358 {
359 SkPictureRecorder recorder;
robertphillips81f71b62014-11-11 04:54:49 -0800360 SkRTreeFactory bbhFactory;
robertphillips7eacd772014-08-21 13:12:42 -0700361
piotaixrb5fae932014-09-24 13:03:30 -0700362 SkCanvas* pictureCanvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
robertphillips81f71b62014-11-11 04:54:49 -0800363 SkIntToScalar(kPicHeight),
364 &bbhFactory,
365 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag);
robertphillips7eacd772014-08-21 13:12:42 -0700366
367 (*pfGen)(pictureCanvas, pictures);
368
369 composite.reset(recorder.endRecording());
370 }
371
372 mpd->add(dest, composite, &xform);
373}
374
piotaixrb5fae932014-09-24 13:03:30 -0700375typedef void(*PFLayoutMtd)(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
robertphillips3bc25e72014-09-19 08:56:09 -0700376 PFContentMtd pfGen, const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700377 SkTArray<ComposeStep>* composeSteps);
378
379// Draw the content into a single canvas
piotaixrb5fae932014-09-24 13:03:30 -0700380static void simple(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
381 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700382 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700383 SkTArray<ComposeStep> *composeSteps) {
384
385 ComposeStep& step = composeSteps->push_back();
386
bsalomon892f31a2014-08-21 14:40:36 -0700387 step.fSurf = create_compat_surface(finalCanvas, kPicWidth, kPicHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700388
389 SkCanvas* subCanvas = step.fSurf->getCanvas();
390
391 create_content(mpd, pfGen, pictures, subCanvas, SkMatrix::I());
392}
393
394// Draw the content into multiple canvases/tiles
395static void tiled(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
piotaixrb5fae932014-09-24 13:03:30 -0700396 PFContentMtd pfGen,
robertphillips3bc25e72014-09-19 08:56:09 -0700397 const SkPicture* pictures[kNumPictures],
robertphillips7eacd772014-08-21 13:12:42 -0700398 SkTArray<ComposeStep> *composeSteps) {
399 static const int kNumTilesX = 2;
400 static const int kNumTilesY = 2;
401 static const int kTileWidth = kPicWidth / kNumTilesX;
402 static const int kTileHeight = kPicHeight / kNumTilesY;
403
404 SkASSERT(kPicWidth == kNumTilesX * kTileWidth);
405 SkASSERT(kPicHeight == kNumTilesY * kTileHeight);
406
407 static const SkColor colors[kNumTilesX][kNumTilesY] = {
piotaixrb5fae932014-09-24 13:03:30 -0700408 { SK_ColorCYAN, SK_ColorMAGENTA },
robertphillips7eacd772014-08-21 13:12:42 -0700409 { SK_ColorYELLOW, SK_ColorGREEN }
410 };
411
412 for (int y = 0; y < kNumTilesY; ++y) {
413 for (int x = 0; x < kNumTilesX; ++x) {
414 ComposeStep& step = composeSteps->push_back();
415
416 step.fX = SkIntToScalar(x*kTileWidth);
417 step.fY = SkIntToScalar(y*kTileHeight);
418 step.fPaint = SkNEW(SkPaint);
419 step.fPaint->setColorFilter(
420 SkColorFilter::CreateModeFilter(colors[x][y], SkXfermode::kModulate_Mode))->unref();
421
bsalomon892f31a2014-08-21 14:40:36 -0700422 step.fSurf = create_compat_surface(finalCanvas, kTileWidth, kTileHeight);
robertphillips7eacd772014-08-21 13:12:42 -0700423
424 SkCanvas* subCanvas = step.fSurf->getCanvas();
425
robertphillips1d24b8d2015-03-26 19:57:08 -0700426 const SkMatrix trans = SkMatrix::MakeTrans(-SkIntToScalar(x*kTileWidth),
427 -SkIntToScalar(y*kTileHeight));
robertphillips7eacd772014-08-21 13:12:42 -0700428
429 create_content(mpd, pfGen, pictures, subCanvas, trans);
430 }
431 }
432}
433
434static const PFLayoutMtd gLayoutMthds[] = { simple, tiled };
435
436namespace skiagm {
437 /**
438 * This GM exercises the SkMultiPictureDraw object. It tests the
439 * cross product of:
440 * tiled vs. all-at-once rendering (e.g., into many or just 1 canvas)
441 * different clips (e.g., none, rect, rrect)
442 * single vs. multiple pictures (e.g., normal vs. picture-pile-style content)
443 */
444 class MultiPictureDraw : public GM {
445 public:
446 enum Content {
447 kNoClipSingle_Content,
448 kRectClipMulti_Content,
449 kRRectClipMulti_Content,
450 kPathClipMulti_Content,
451 kInvPathClipMulti_Content,
robertphillips3bc25e72014-09-19 08:56:09 -0700452 kSierpinski_Content,
robertphillipsb1fc64b2014-10-02 08:32:43 -0700453 kBigLayer_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700454
robertphillipsb1fc64b2014-10-02 08:32:43 -0700455 kLast_Content = kBigLayer_Content
robertphillips7eacd772014-08-21 13:12:42 -0700456 };
457
458 static const int kContentCnt = kLast_Content + 1;
459
460 enum Layout {
461 kSimple_Layout,
462 kTiled_Layout,
463
464 kLast_Layout = kTiled_Layout
465 };
466
467 static const int kLayoutCnt = kLast_Layout + 1;
468
469 MultiPictureDraw(Content content, Layout layout) : fContent(content), fLayout(layout) {
470 SkASSERT(SK_ARRAY_COUNT(gLayoutMthds) == kLayoutCnt);
471 SkASSERT(SK_ARRAY_COUNT(gContentMthds) == kContentCnt);
472
robertphillips3bc25e72014-09-19 08:56:09 -0700473 for (int i = 0; i < kNumPictures; ++i) {
474 fPictures[i] = NULL;
475 }
robertphillips7eacd772014-08-21 13:12:42 -0700476 }
477
478 virtual ~MultiPictureDraw() {
robertphillips3bc25e72014-09-19 08:56:09 -0700479 for (int i = 0; i < kNumPictures; ++i) {
480 SkSafeUnref(fPictures[i]);
481 }
robertphillips7eacd772014-08-21 13:12:42 -0700482 }
483
484 protected:
485 Content fContent;
486 Layout fLayout;
robertphillips3bc25e72014-09-19 08:56:09 -0700487 const SkPicture* fPictures[kNumPictures];
robertphillips7eacd772014-08-21 13:12:42 -0700488
mtklein36352bf2015-03-25 18:17:31 -0700489 void onOnceBeforeDraw() override {
robertphillips3bc25e72014-09-19 08:56:09 -0700490 fPictures[0] = make_hex_plane_picture(SK_ColorWHITE);
491 fPictures[1] = make_hex_plane_picture(SK_ColorGRAY);
robertphillipsab79ab52014-09-19 11:30:37 -0700492 fPictures[2] = make_sierpinski_picture();
robertphillipsb1fc64b2014-10-02 08:32:43 -0700493 fPictures[3] = make_single_layer_hex_plane_picture();
robertphillips7eacd772014-08-21 13:12:42 -0700494 }
495
mtklein36352bf2015-03-25 18:17:31 -0700496 void onDraw(SkCanvas* canvas) override {
robertphillips7eacd772014-08-21 13:12:42 -0700497 SkMultiPictureDraw mpd;
498 SkTArray<ComposeStep> composeSteps;
499
500 // Fill up the MultiPictureDraw
piotaixrb5fae932014-09-24 13:03:30 -0700501 (*gLayoutMthds[fLayout])(canvas, &mpd,
502 gContentMthds[fContent],
robertphillips7eacd772014-08-21 13:12:42 -0700503 fPictures, &composeSteps);
504
505 mpd.draw();
506
507 // Compose all the drawn canvases into the final canvas
508 for (int i = 0; i < composeSteps.count(); ++i) {
509 const ComposeStep& step = composeSteps[i];
510
511 SkAutoTUnref<SkImage> image(step.fSurf->newImageSnapshot());
512
piotaixrb5fae932014-09-24 13:03:30 -0700513 canvas->drawImage(image, step.fX, step.fY, step.fPaint);
robertphillips7eacd772014-08-21 13:12:42 -0700514 }
515 }
516
mtklein36352bf2015-03-25 18:17:31 -0700517 SkISize onISize() override { return SkISize::Make(kPicWidth, kPicHeight); }
robertphillips7eacd772014-08-21 13:12:42 -0700518
mtklein36352bf2015-03-25 18:17:31 -0700519 SkString onShortName() override {
piotaixrb5fae932014-09-24 13:03:30 -0700520 static const char* gContentNames[] = {
robertphillipsb1fc64b2014-10-02 08:32:43 -0700521 "noclip", "rectclip", "rrectclip", "pathclip",
522 "invpathclip", "sierpinski", "biglayer"
robertphillips7eacd772014-08-21 13:12:42 -0700523 };
524 static const char* gLayoutNames[] = { "simple", "tiled" };
525
526 SkASSERT(SK_ARRAY_COUNT(gLayoutNames) == kLayoutCnt);
527 SkASSERT(SK_ARRAY_COUNT(gContentNames) == kContentCnt);
528
529 SkString name("multipicturedraw_");
530
531 name.append(gContentNames[fContent]);
532 name.append("_");
533 name.append(gLayoutNames[fLayout]);
534 return name;
535 }
536
mtklein36352bf2015-03-25 18:17:31 -0700537 bool runAsBench() const override { return true; }
robertphillips7eacd772014-08-21 13:12:42 -0700538
539 private:
540 typedef GM INHERITED;
541 };
542
piotaixrb5fae932014-09-24 13:03:30 -0700543 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kNoClipSingle_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700544 MultiPictureDraw::kSimple_Layout));)
piotaixrb5fae932014-09-24 13:03:30 -0700545 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRectClipMulti_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700546 MultiPictureDraw::kSimple_Layout));)
piotaixrb5fae932014-09-24 13:03:30 -0700547 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRRectClipMulti_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700548 MultiPictureDraw::kSimple_Layout));)
piotaixrb5fae932014-09-24 13:03:30 -0700549 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kPathClipMulti_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700550 MultiPictureDraw::kSimple_Layout));)
piotaixrb5fae932014-09-24 13:03:30 -0700551 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kInvPathClipMulti_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700552 MultiPictureDraw::kSimple_Layout));)
robertphillips3bc25e72014-09-19 08:56:09 -0700553 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kSierpinski_Content,
554 MultiPictureDraw::kSimple_Layout));)
robertphillipsb1fc64b2014-10-02 08:32:43 -0700555 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kBigLayer_Content,
556 MultiPictureDraw::kSimple_Layout));)
robertphillips7eacd772014-08-21 13:12:42 -0700557
piotaixrb5fae932014-09-24 13:03:30 -0700558 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kNoClipSingle_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700559 MultiPictureDraw::kTiled_Layout));)
piotaixrb5fae932014-09-24 13:03:30 -0700560 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRectClipMulti_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700561 MultiPictureDraw::kTiled_Layout));)
piotaixrb5fae932014-09-24 13:03:30 -0700562 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRRectClipMulti_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700563 MultiPictureDraw::kTiled_Layout));)
piotaixrb5fae932014-09-24 13:03:30 -0700564 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kPathClipMulti_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700565 MultiPictureDraw::kTiled_Layout));)
piotaixrb5fae932014-09-24 13:03:30 -0700566 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kInvPathClipMulti_Content,
robertphillips7eacd772014-08-21 13:12:42 -0700567 MultiPictureDraw::kTiled_Layout));)
robertphillips3bc25e72014-09-19 08:56:09 -0700568 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kSierpinski_Content,
569 MultiPictureDraw::kTiled_Layout));)
robertphillipsb1fc64b2014-10-02 08:32:43 -0700570 DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kBigLayer_Content,
571 MultiPictureDraw::kTiled_Layout));)
robertphillips7eacd772014-08-21 13:12:42 -0700572}