2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
11 #include "SkColorFilter.h"
12 #include "SkMultiPictureDraw.h"
14 #include "SkPictureRecorder.h"
15 #include "SkSurface.h"
17 constexpr SkScalar kRoot3Over2 = 0.86602545f; // sin(60)
18 constexpr SkScalar kRoot3 = 1.73205081f;
20 constexpr int kHexSide = 30;
21 constexpr int kNumHexX = 6;
22 constexpr int kNumHexY = 6;
23 constexpr int kPicWidth = kNumHexX * kHexSide;
24 constexpr int kPicHeight = (int)((kNumHexY - 0.5f) * 2 * kHexSide * kRoot3Over2 + 0.5f);
25 constexpr SkScalar kInset = 20.0f;
26 constexpr int kNumPictures = 4;
28 constexpr int kTriSide = 40;
30 // Create a hexagon centered at (originX, originY)
31 static SkPath make_hex_path(SkScalar originX, SkScalar originY) {
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);
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
46 static sk_sp<SkPicture> make_hex_plane_picture(SkColor fillColor) {
48 // Create a hexagon with its center at the origin
49 SkPath hex = make_hex_path(0, 0);
52 fill.setStyle(SkPaint::kFill_Style);
53 fill.setColor(fillColor);
56 stroke.setStyle(SkPaint::kStroke_Style);
57 stroke.setStrokeWidth(3);
59 SkPictureRecorder recorder;
60 SkRTreeFactory bbhFactory;
62 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
63 SkIntToScalar(kPicHeight),
66 SkScalar xPos, yPos = 0;
68 for (int y = 0; y < kNumHexY; ++y) {
71 for (int x = 0; x < kNumHexX; ++x) {
72 canvas->saveLayer(nullptr, nullptr);
73 canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0));
74 canvas->drawPath(hex, fill);
75 canvas->drawPath(hex, stroke);
78 xPos += 1.5f * kHexSide;
81 yPos += 2 * kHexSide * kRoot3Over2;
84 return recorder.finishRecordingAsPicture();
87 // Create a picture that consists of a single large layer that is tiled
89 // This is intended to exercise the layer hoisting code's clip handling (in
91 static sk_sp<SkPicture> make_single_layer_hex_plane_picture() {
93 // Create a hexagon with its center at the origin
94 SkPath hex = make_hex_path(0, 0);
97 whiteFill.setStyle(SkPaint::kFill_Style);
98 whiteFill.setColor(SK_ColorWHITE);
101 greyFill.setStyle(SkPaint::kFill_Style);
102 greyFill.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
105 stroke.setStyle(SkPaint::kStroke_Style);
106 stroke.setStrokeWidth(3);
108 SkPictureRecorder recorder;
109 SkRTreeFactory bbhFactory;
111 constexpr SkScalar kBig = 10000.0f;
112 SkCanvas* canvas = recorder.beginRecording(kBig, kBig, &bbhFactory);
114 canvas->saveLayer(nullptr, nullptr);
116 SkScalar xPos = 0.0f, yPos = 0.0f;
118 for (int y = 0; yPos < kBig; ++y) {
121 for (int x = 0; xPos < kBig; ++x) {
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);
131 xPos += 1.5f * kHexSide;
134 yPos += 2 * kHexSide * kRoot3Over2;
139 return recorder.finishRecordingAsPicture();
142 // Make an equilateral triangle path with its top corner at (originX, originY)
143 static SkPath make_tri_path(SkScalar originX, SkScalar originY) {
145 tri.moveTo(originX, originY);
146 tri.rLineTo(SkScalarHalf(kTriSide), 1.5f * kTriSide / kRoot3);
147 tri.rLineTo(-kTriSide, 0);
152 static sk_sp<SkPicture> make_tri_picture() {
153 SkPath tri = make_tri_path(SkScalarHalf(kTriSide), 0);
156 fill.setStyle(SkPaint::kFill_Style);
157 fill.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
160 stroke.setStyle(SkPaint::kStroke_Style);
161 stroke.setStrokeWidth(3);
163 SkPictureRecorder recorder;
164 SkRTreeFactory bbhFactory;
166 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
167 SkIntToScalar(kPicHeight),
169 SkRect r = tri.getBounds();
170 r.outset(2.0f, 2.0f); // outset for stroke
172 // The saveLayer/restore block is to exercise layer hoisting
173 canvas->saveLayer(nullptr, nullptr);
174 canvas->drawPath(tri, fill);
175 canvas->drawPath(tri, stroke);
178 return recorder.finishRecordingAsPicture();
181 static sk_sp<SkPicture> make_sub_picture(const SkPicture* tri) {
182 SkPictureRecorder recorder;
183 SkRTreeFactory bbhFactory;
185 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
186 SkIntToScalar(kPicHeight),
189 canvas->scale(1.0f/2.0f, 1.0f/2.0f);
192 canvas->translate(SkScalarHalf(kTriSide), 0);
193 canvas->drawPicture(tri);
197 canvas->translate(SkIntToScalar(kTriSide), 1.5f * kTriSide / kRoot3);
198 canvas->drawPicture(tri);
202 canvas->translate(0, 1.5f * kTriSide / kRoot3);
203 canvas->drawPicture(tri);
206 return recorder.finishRecordingAsPicture();
209 // Create a Sierpinkski-like picture that starts with a top row with a picture
210 // that just contains a triangle. Subsequent rows take the prior row's picture,
211 // shrinks it and replicates it 3 times then draws and appropriate number of
213 static sk_sp<SkPicture> make_sierpinski_picture() {
214 sk_sp<SkPicture> pic(make_tri_picture());
216 SkPictureRecorder recorder;
217 SkRTreeFactory bbhFactory;
219 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
220 SkIntToScalar(kPicHeight),
223 constexpr int kNumLevels = 4;
224 for (int i = 0; i < kNumLevels; ++i) {
226 canvas->translate(kPicWidth/2 - (i+1) * (kTriSide/2.0f), 0.0f);
227 for (int j = 0; j < i+1; ++j) {
228 canvas->drawPicture(pic);
229 canvas->translate(SkIntToScalar(kTriSide), 0);
233 pic = make_sub_picture(pic.get());
235 canvas->translate(0, 1.5f * kTriSide / kRoot3);
238 return recorder.finishRecordingAsPicture();
241 static sk_sp<SkSurface> create_compat_surface(SkCanvas* canvas, int width, int height) {
242 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
244 auto surface = canvas->makeSurface(info);
245 if (nullptr == surface) {
246 // picture canvas returns nullptr so fall back to raster
247 surface = SkSurface::MakeRaster(info);
252 // This class stores the information required to compose all the result
253 // fragments potentially generated by the MultiPictureDraw object
256 ComposeStep() : fX(0.0f), fY(0.0f), fPaint(nullptr) { }
261 sk_sp<SkSurface> fSurf;
267 typedef void (*PFContentMtd)(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]);
269 // Just a single picture with no clip
270 static void no_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
271 canvas->drawPicture(pictures[0]);
274 // Two pictures with a rect clip on the second one
275 static void rect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
276 canvas->drawPicture(pictures[0]);
278 SkRect rect = pictures[0]->cullRect();
279 rect.inset(kInset, kInset);
281 canvas->clipRect(rect);
283 canvas->drawPicture(pictures[1]);
286 // Two pictures with a round rect clip on the second one
287 static void rrect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
288 canvas->drawPicture(pictures[0]);
290 SkRect rect = pictures[0]->cullRect();
291 rect.inset(kInset, kInset);
294 rrect.setRectXY(rect, kInset, kInset);
296 canvas->clipRRect(rrect);
298 canvas->drawPicture(pictures[1]);
301 // Two pictures with a clip path on the second one
302 static void path_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
303 canvas->drawPicture(pictures[0]);
305 // Create a hexagon centered on the middle of the hex grid
306 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
308 canvas->clipPath(hex);
310 canvas->drawPicture(pictures[1]);
313 // Two pictures with an inverse clip path on the second one
314 static void invpath_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
315 canvas->drawPicture(pictures[0]);
317 // Create a hexagon centered on the middle of the hex grid
318 SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2);
319 hex.setFillType(SkPath::kInverseEvenOdd_FillType);
321 canvas->clipPath(hex);
323 canvas->drawPicture(pictures[1]);
326 // Reuse a single base (triangular) picture a _lot_ (rotated, scaled and translated).
327 static void sierpinski(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
329 canvas->drawPicture(pictures[2]);
331 canvas->rotate(180.0f);
332 canvas->translate(-SkIntToScalar(kPicWidth), -SkIntToScalar(kPicHeight));
333 canvas->drawPicture(pictures[2]);
337 static void big_layer(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) {
338 canvas->drawPicture(pictures[3]);
341 constexpr PFContentMtd gContentMthds[] = {
351 static void create_content(SkMultiPictureDraw* mpd, PFContentMtd pfGen,
352 const SkPicture* pictures[kNumPictures],
353 SkCanvas* dest, const SkMatrix& xform) {
354 sk_sp<SkPicture> composite;
357 SkPictureRecorder recorder;
358 SkRTreeFactory bbhFactory;
360 SkCanvas* pictureCanvas = recorder.beginRecording(SkIntToScalar(kPicWidth),
361 SkIntToScalar(kPicHeight),
364 (*pfGen)(pictureCanvas, pictures);
366 composite = recorder.finishRecordingAsPicture();
369 mpd->add(dest, composite.get(), &xform);
372 typedef void(*PFLayoutMtd)(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
373 PFContentMtd pfGen, const SkPicture* pictures[kNumPictures],
374 SkTArray<ComposeStep>* composeSteps);
376 // Draw the content into a single canvas
377 static void simple(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
379 const SkPicture* pictures[kNumPictures],
380 SkTArray<ComposeStep> *composeSteps) {
382 ComposeStep& step = composeSteps->push_back();
384 step.fSurf = create_compat_surface(finalCanvas, kPicWidth, kPicHeight);
386 SkCanvas* subCanvas = step.fSurf->getCanvas();
388 create_content(mpd, pfGen, pictures, subCanvas, SkMatrix::I());
391 // Draw the content into multiple canvases/tiles
392 static void tiled(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd,
394 const SkPicture* pictures[kNumPictures],
395 SkTArray<ComposeStep> *composeSteps) {
396 const int kNumTilesX = 2;
397 const int kNumTilesY = 2;
398 const int kTileWidth = kPicWidth / kNumTilesX;
399 const int kTileHeight = kPicHeight / kNumTilesY;
401 SkASSERT(kPicWidth == kNumTilesX * kTileWidth);
402 SkASSERT(kPicHeight == kNumTilesY * kTileHeight);
404 const SkColor colors[kNumTilesX][kNumTilesY] = {
405 { SK_ColorCYAN, SK_ColorMAGENTA },
406 { SK_ColorYELLOW, SK_ColorGREEN }
409 for (int y = 0; y < kNumTilesY; ++y) {
410 for (int x = 0; x < kNumTilesX; ++x) {
411 ComposeStep& step = composeSteps->push_back();
413 step.fX = SkIntToScalar(x*kTileWidth);
414 step.fY = SkIntToScalar(y*kTileHeight);
415 step.fPaint = new SkPaint;
416 step.fPaint->setColorFilter(
417 SkColorFilter::MakeModeFilter(colors[x][y], SkBlendMode::kModulate));
419 step.fSurf = create_compat_surface(finalCanvas, kTileWidth, kTileHeight);
421 SkCanvas* subCanvas = step.fSurf->getCanvas();
423 const SkMatrix trans = SkMatrix::MakeTrans(-SkIntToScalar(x*kTileWidth),
424 -SkIntToScalar(y*kTileHeight));
426 create_content(mpd, pfGen, pictures, subCanvas, trans);
431 constexpr PFLayoutMtd gLayoutMthds[] = { simple, tiled };
435 * This GM exercises the SkMultiPictureDraw object. It tests the
437 * tiled vs. all-at-once rendering (e.g., into many or just 1 canvas)
438 * different clips (e.g., none, rect, rrect)
439 * single vs. multiple pictures (e.g., normal vs. picture-pile-style content)
441 class MultiPictureDraw : public GM {
444 kNoClipSingle_Content,
445 kRectClipMulti_Content,
446 kRRectClipMulti_Content,
447 kPathClipMulti_Content,
448 kInvPathClipMulti_Content,
452 kLast_Content = kBigLayer_Content
455 const int kContentCnt = kLast_Content + 1;
461 kLast_Layout = kTiled_Layout
464 const int kLayoutCnt = kLast_Layout + 1;
466 MultiPictureDraw(Content content, Layout layout) : fContent(content), fLayout(layout) {
467 SkASSERT(SK_ARRAY_COUNT(gLayoutMthds) == kLayoutCnt);
468 SkASSERT(SK_ARRAY_COUNT(gContentMthds) == kContentCnt);
470 for (int i = 0; i < kNumPictures; ++i) {
471 fPictures[i] = nullptr;
475 ~MultiPictureDraw() override {
476 for (int i = 0; i < kNumPictures; ++i) {
477 SkSafeUnref(fPictures[i]);
484 const SkPicture* fPictures[kNumPictures];
486 void onOnceBeforeDraw() override {
487 fPictures[0] = make_hex_plane_picture(SK_ColorWHITE).release();
488 fPictures[1] = make_hex_plane_picture(sk_tool_utils::color_to_565(SK_ColorGRAY)).release();
489 fPictures[2] = make_sierpinski_picture().release();
490 fPictures[3] = make_single_layer_hex_plane_picture().release();
493 void onDraw(SkCanvas* canvas) override {
494 SkMultiPictureDraw mpd;
495 SkTArray<ComposeStep> composeSteps;
497 // Fill up the MultiPictureDraw
498 (*gLayoutMthds[fLayout])(canvas, &mpd,
499 gContentMthds[fContent],
500 fPictures, &composeSteps);
504 // Compose all the drawn canvases into the final canvas
505 for (int i = 0; i < composeSteps.count(); ++i) {
506 const ComposeStep& step = composeSteps[i];
508 canvas->drawImage(step.fSurf->makeImageSnapshot().get(),
509 step.fX, step.fY, step.fPaint);
513 SkISize onISize() override { return SkISize::Make(kPicWidth, kPicHeight); }
515 SkString onShortName() override {
516 const char* gContentNames[] = {
517 "noclip", "rectclip", "rrectclip", "pathclip",
518 "invpathclip", "sierpinski", "biglayer"
520 const char* gLayoutNames[] = { "simple", "tiled" };
522 SkASSERT(SK_ARRAY_COUNT(gLayoutNames) == kLayoutCnt);
523 SkASSERT(SK_ARRAY_COUNT(gContentNames) == kContentCnt);
525 SkString name("multipicturedraw_");
527 name.append(gContentNames[fContent]);
529 name.append(gLayoutNames[fLayout]);
533 bool runAsBench() const override { return true; }
536 typedef GM INHERITED;
539 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
540 MultiPictureDraw::kSimple_Layout);)
541 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
542 MultiPictureDraw::kSimple_Layout);)
543 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
544 MultiPictureDraw::kSimple_Layout);)
545 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
546 MultiPictureDraw::kSimple_Layout);)
547 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
548 MultiPictureDraw::kSimple_Layout);)
549 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
550 MultiPictureDraw::kSimple_Layout);)
551 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
552 MultiPictureDraw::kSimple_Layout);)
554 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kNoClipSingle_Content,
555 MultiPictureDraw::kTiled_Layout);)
556 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRectClipMulti_Content,
557 MultiPictureDraw::kTiled_Layout);)
558 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kRRectClipMulti_Content,
559 MultiPictureDraw::kTiled_Layout);)
560 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kPathClipMulti_Content,
561 MultiPictureDraw::kTiled_Layout);)
562 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kInvPathClipMulti_Content,
563 MultiPictureDraw::kTiled_Layout);)
564 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kSierpinski_Content,
565 MultiPictureDraw::kTiled_Layout);)
566 DEF_GM(return new MultiPictureDraw(MultiPictureDraw::kBigLayer_Content,
567 MultiPictureDraw::kTiled_Layout);)