robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | static const SkScalar kRoot3Over2 = 0.86602545f; // sin(60) |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 16 | static const SkScalar kRoot3 = 1.73205081f; |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 17 | |
| 18 | static const int kHexSide = 30; |
| 19 | static const int kNumHexX = 6; |
| 20 | static const int kNumHexY = 6; |
| 21 | static const int kPicWidth = kNumHexX * kHexSide; |
| 22 | static const int kPicHeight = SkScalarCeilToInt((kNumHexY - 0.5f) * 2 * kHexSide * kRoot3Over2); |
| 23 | static const SkScalar kInset = 20.0f; |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 24 | static const int kNumPictures = 3; |
| 25 | |
| 26 | static const int kTriSide = 40; |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 27 | |
| 28 | // Create a hexagon centered at (originX, originY) |
| 29 | static 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. |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 44 | static const SkPicture* make_hex_plane_picture(SkColor fillColor) { |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 45 | |
| 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; |
| 58 | |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 59 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 60 | SkIntToScalar(kPicHeight)); |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 61 | |
| 62 | SkScalar xPos, yPos = 0; |
| 63 | |
| 64 | for (int y = 0; y < kNumHexY; ++y) { |
| 65 | xPos = 0; |
| 66 | |
| 67 | for (int x = 0; x < kNumHexX; ++x) { |
| 68 | canvas->saveLayer(NULL, NULL); |
| 69 | canvas->translate(xPos, yPos + ((x % 2) ? kRoot3Over2 * kHexSide : 0)); |
| 70 | canvas->drawPath(hex, fill); |
| 71 | canvas->drawPath(hex, stroke); |
| 72 | canvas->restore(); |
| 73 | |
| 74 | xPos += 1.5f * kHexSide; |
| 75 | } |
| 76 | |
| 77 | yPos += 2 * kHexSide * kRoot3Over2; |
| 78 | } |
| 79 | |
| 80 | return recorder.endRecording(); |
| 81 | } |
| 82 | |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 83 | // Make an equilateral triangle path with its top corner at (originX, originY) |
| 84 | static SkPath make_tri_path(SkScalar originX, SkScalar originY) { |
| 85 | SkPath tri; |
| 86 | tri.moveTo(originX, originY); |
| 87 | tri.rLineTo(SkScalarHalf(kTriSide), 1.5f * kTriSide / kRoot3); |
| 88 | tri.rLineTo(-kTriSide, 0); |
| 89 | tri.close(); |
| 90 | return tri; |
| 91 | } |
| 92 | |
| 93 | static const SkPicture* make_tri_picture() { |
robertphillips | aa0c837 | 2014-09-29 05:07:39 -0700 | [diff] [blame] | 94 | SkPath tri = make_tri_path(SkScalarHalf(kTriSide), 0); |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 95 | |
| 96 | SkPaint fill; |
| 97 | fill.setStyle(SkPaint::kFill_Style); |
| 98 | fill.setColor(SK_ColorLTGRAY);; |
| 99 | |
| 100 | SkPaint stroke; |
| 101 | stroke.setStyle(SkPaint::kStroke_Style); |
| 102 | stroke.setStrokeWidth(3); |
| 103 | |
| 104 | SkPictureRecorder recorder; |
| 105 | |
| 106 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth), |
| 107 | SkIntToScalar(kPicHeight)); |
| 108 | // The saveLayer/restore block is to exercise layer hoisting |
| 109 | canvas->saveLayer(NULL, NULL); |
| 110 | canvas->drawPath(tri, fill); |
| 111 | canvas->drawPath(tri, stroke); |
| 112 | canvas->restore(); |
| 113 | |
| 114 | return recorder.endRecording(); |
| 115 | } |
| 116 | |
| 117 | static const SkPicture* make_sub_picture(const SkPicture* tri) { |
| 118 | SkPictureRecorder recorder; |
| 119 | |
| 120 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth), |
| 121 | SkIntToScalar(kPicHeight)); |
| 122 | |
| 123 | canvas->scale(1.0f/2.0f, 1.0f/2.0f); |
| 124 | |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 125 | canvas->save(); |
robertphillips | aa0c837 | 2014-09-29 05:07:39 -0700 | [diff] [blame] | 126 | canvas->translate(SkScalarHalf(kTriSide), 0); |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 127 | canvas->drawPicture(tri); |
| 128 | canvas->restore(); |
| 129 | |
| 130 | canvas->save(); |
robertphillips | aa0c837 | 2014-09-29 05:07:39 -0700 | [diff] [blame] | 131 | canvas->translate(SkIntToScalar(kTriSide), 1.5f * kTriSide / kRoot3); |
| 132 | canvas->drawPicture(tri); |
| 133 | canvas->restore(); |
| 134 | |
| 135 | canvas->save(); |
| 136 | canvas->translate(0, 1.5f * kTriSide / kRoot3); |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 137 | canvas->drawPicture(tri); |
| 138 | canvas->restore(); |
| 139 | |
| 140 | return recorder.endRecording(); |
| 141 | } |
| 142 | |
| 143 | // Create a Sierpinkski-like picture that starts with a top row with a picture |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 144 | // that just contains a triangle. Subsequent rows take the prior row's picture, |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 145 | // shrinks it and replicates it 3 times then draws and appropriate number of |
| 146 | // copies of it. |
| 147 | static const SkPicture* make_sierpinski_picture() { |
| 148 | SkAutoTUnref<const SkPicture> pic(make_tri_picture()); |
| 149 | |
| 150 | SkPictureRecorder recorder; |
| 151 | |
| 152 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kPicWidth), |
| 153 | SkIntToScalar(kPicHeight)); |
| 154 | |
| 155 | static const int kNumLevels = 4; |
| 156 | for (int i = 0; i < kNumLevels; ++i) { |
| 157 | canvas->save(); |
robertphillips | aa0c837 | 2014-09-29 05:07:39 -0700 | [diff] [blame] | 158 | canvas->translate(kPicWidth/2 - (i+1) * (kTriSide/2.0f), 0.0f); |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 159 | for (int j = 0; j < i+1; ++j) { |
| 160 | canvas->drawPicture(pic); |
| 161 | canvas->translate(SkIntToScalar(kTriSide), 0); |
| 162 | } |
| 163 | canvas->restore(); |
| 164 | |
| 165 | pic.reset(make_sub_picture(pic)); |
| 166 | |
| 167 | canvas->translate(0, 1.5f * kTriSide / kRoot3); |
| 168 | } |
| 169 | |
| 170 | return recorder.endRecording(); |
| 171 | } |
| 172 | |
bsalomon | 892f31a | 2014-08-21 14:40:36 -0700 | [diff] [blame] | 173 | static SkSurface* create_compat_surface(SkCanvas* canvas, int width, int height) { |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 174 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 175 | |
| 176 | SkSurface* surface = canvas->newSurface(info); |
| 177 | if (NULL == surface) { |
| 178 | // picture canvas returns NULL so fall back to raster |
| 179 | surface = SkSurface::NewRaster(info); |
| 180 | } |
| 181 | |
| 182 | return surface; |
| 183 | } |
| 184 | |
| 185 | // This class stores the information required to compose all the result |
| 186 | // fragments potentially generated by the MultiPictureDraw object |
| 187 | class ComposeStep { |
| 188 | public: |
| 189 | ComposeStep() : fSurf(NULL), fX(0.0f), fY(0.0f), fPaint(NULL) { } |
| 190 | ~ComposeStep() { SkSafeUnref(fSurf); SkDELETE(fPaint); } |
| 191 | |
| 192 | SkSurface* fSurf; |
| 193 | SkScalar fX; |
| 194 | SkScalar fY; |
| 195 | SkPaint* fPaint; |
| 196 | }; |
| 197 | |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 198 | typedef void (*PFContentMtd)(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]); |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 199 | |
| 200 | // Just a single picture with no clip |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 201 | static void no_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) { |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 202 | canvas->drawPicture(pictures[0]); |
| 203 | } |
| 204 | |
| 205 | // Two pictures with a rect clip on the second one |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 206 | static void rect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) { |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 207 | canvas->drawPicture(pictures[0]); |
| 208 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 209 | SkRect rect = pictures[0]->cullRect(); |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 210 | rect.inset(kInset, kInset); |
| 211 | |
| 212 | canvas->clipRect(rect); |
| 213 | |
| 214 | canvas->drawPicture(pictures[1]); |
| 215 | } |
| 216 | |
| 217 | // Two pictures with a round rect clip on the second one |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 218 | static void rrect_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) { |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 219 | canvas->drawPicture(pictures[0]); |
| 220 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 221 | SkRect rect = pictures[0]->cullRect(); |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 222 | rect.inset(kInset, kInset); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 223 | |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 224 | SkRRect rrect; |
| 225 | rrect.setRectXY(rect, kInset, kInset); |
| 226 | |
| 227 | canvas->clipRRect(rrect); |
| 228 | |
| 229 | canvas->drawPicture(pictures[1]); |
| 230 | } |
| 231 | |
| 232 | // Two pictures with a clip path on the second one |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 233 | static void path_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) { |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 234 | canvas->drawPicture(pictures[0]); |
| 235 | |
| 236 | // Create a hexagon centered on the middle of the hex grid |
| 237 | SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2); |
| 238 | |
| 239 | canvas->clipPath(hex); |
| 240 | |
| 241 | canvas->drawPicture(pictures[1]); |
| 242 | } |
| 243 | |
| 244 | // Two pictures with an inverse clip path on the second one |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 245 | static void invpath_clip(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) { |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 246 | canvas->drawPicture(pictures[0]); |
| 247 | |
| 248 | // Create a hexagon centered on the middle of the hex grid |
| 249 | SkPath hex = make_hex_path((kNumHexX / 2.0f) * kHexSide, kNumHexY * kHexSide * kRoot3Over2); |
| 250 | hex.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 251 | |
| 252 | canvas->clipPath(hex); |
| 253 | |
| 254 | canvas->drawPicture(pictures[1]); |
| 255 | } |
| 256 | |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 257 | // Reuse a single base (triangular) picture a _lot_ (rotated, scaled and translated). |
| 258 | static void sierpinski(SkCanvas* canvas, const SkPicture* pictures[kNumPictures]) { |
| 259 | canvas->save(); |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 260 | canvas->drawPicture(pictures[2]); |
| 261 | |
| 262 | canvas->rotate(180.0f); |
robertphillips | aa0c837 | 2014-09-29 05:07:39 -0700 | [diff] [blame] | 263 | canvas->translate(-SkIntToScalar(kPicWidth), -SkIntToScalar(kPicHeight)); |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 264 | canvas->drawPicture(pictures[2]); |
| 265 | canvas->restore(); |
| 266 | } |
| 267 | |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 268 | static const PFContentMtd gContentMthds[] = { |
| 269 | no_clip, |
| 270 | rect_clip, |
| 271 | rrect_clip, |
| 272 | path_clip, |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 273 | invpath_clip, |
| 274 | sierpinski |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | static void create_content(SkMultiPictureDraw* mpd, PFContentMtd pfGen, |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 278 | const SkPicture* pictures[kNumPictures], |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 279 | SkCanvas* dest, const SkMatrix& xform) { |
| 280 | SkAutoTUnref<SkPicture> composite; |
| 281 | |
| 282 | { |
| 283 | SkPictureRecorder recorder; |
| 284 | |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 285 | SkCanvas* pictureCanvas = recorder.beginRecording(SkIntToScalar(kPicWidth), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 286 | SkIntToScalar(kPicHeight)); |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 287 | |
| 288 | (*pfGen)(pictureCanvas, pictures); |
| 289 | |
| 290 | composite.reset(recorder.endRecording()); |
| 291 | } |
| 292 | |
| 293 | mpd->add(dest, composite, &xform); |
| 294 | } |
| 295 | |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 296 | typedef void(*PFLayoutMtd)(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd, |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 297 | PFContentMtd pfGen, const SkPicture* pictures[kNumPictures], |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 298 | SkTArray<ComposeStep>* composeSteps); |
| 299 | |
| 300 | // Draw the content into a single canvas |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 301 | static void simple(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd, |
| 302 | PFContentMtd pfGen, |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 303 | const SkPicture* pictures[kNumPictures], |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 304 | SkTArray<ComposeStep> *composeSteps) { |
| 305 | |
| 306 | ComposeStep& step = composeSteps->push_back(); |
| 307 | |
bsalomon | 892f31a | 2014-08-21 14:40:36 -0700 | [diff] [blame] | 308 | step.fSurf = create_compat_surface(finalCanvas, kPicWidth, kPicHeight); |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 309 | |
| 310 | SkCanvas* subCanvas = step.fSurf->getCanvas(); |
| 311 | |
| 312 | create_content(mpd, pfGen, pictures, subCanvas, SkMatrix::I()); |
| 313 | } |
| 314 | |
| 315 | // Draw the content into multiple canvases/tiles |
| 316 | static void tiled(SkCanvas* finalCanvas, SkMultiPictureDraw* mpd, |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 317 | PFContentMtd pfGen, |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 318 | const SkPicture* pictures[kNumPictures], |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 319 | SkTArray<ComposeStep> *composeSteps) { |
| 320 | static const int kNumTilesX = 2; |
| 321 | static const int kNumTilesY = 2; |
| 322 | static const int kTileWidth = kPicWidth / kNumTilesX; |
| 323 | static const int kTileHeight = kPicHeight / kNumTilesY; |
| 324 | |
| 325 | SkASSERT(kPicWidth == kNumTilesX * kTileWidth); |
| 326 | SkASSERT(kPicHeight == kNumTilesY * kTileHeight); |
| 327 | |
| 328 | static const SkColor colors[kNumTilesX][kNumTilesY] = { |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 329 | { SK_ColorCYAN, SK_ColorMAGENTA }, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 330 | { SK_ColorYELLOW, SK_ColorGREEN } |
| 331 | }; |
| 332 | |
| 333 | for (int y = 0; y < kNumTilesY; ++y) { |
| 334 | for (int x = 0; x < kNumTilesX; ++x) { |
| 335 | ComposeStep& step = composeSteps->push_back(); |
| 336 | |
| 337 | step.fX = SkIntToScalar(x*kTileWidth); |
| 338 | step.fY = SkIntToScalar(y*kTileHeight); |
| 339 | step.fPaint = SkNEW(SkPaint); |
| 340 | step.fPaint->setColorFilter( |
| 341 | SkColorFilter::CreateModeFilter(colors[x][y], SkXfermode::kModulate_Mode))->unref(); |
| 342 | |
bsalomon | 892f31a | 2014-08-21 14:40:36 -0700 | [diff] [blame] | 343 | step.fSurf = create_compat_surface(finalCanvas, kTileWidth, kTileHeight); |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 344 | |
| 345 | SkCanvas* subCanvas = step.fSurf->getCanvas(); |
| 346 | |
| 347 | SkMatrix trans; |
| 348 | trans.setTranslate(-SkIntToScalar(x*kTileWidth), -SkIntToScalar(y*kTileHeight)); |
| 349 | |
| 350 | create_content(mpd, pfGen, pictures, subCanvas, trans); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static const PFLayoutMtd gLayoutMthds[] = { simple, tiled }; |
| 356 | |
| 357 | namespace skiagm { |
| 358 | /** |
| 359 | * This GM exercises the SkMultiPictureDraw object. It tests the |
| 360 | * cross product of: |
| 361 | * tiled vs. all-at-once rendering (e.g., into many or just 1 canvas) |
| 362 | * different clips (e.g., none, rect, rrect) |
| 363 | * single vs. multiple pictures (e.g., normal vs. picture-pile-style content) |
| 364 | */ |
| 365 | class MultiPictureDraw : public GM { |
| 366 | public: |
| 367 | enum Content { |
| 368 | kNoClipSingle_Content, |
| 369 | kRectClipMulti_Content, |
| 370 | kRRectClipMulti_Content, |
| 371 | kPathClipMulti_Content, |
| 372 | kInvPathClipMulti_Content, |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 373 | kSierpinski_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 374 | |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 375 | kLast_Content = kSierpinski_Content |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | static const int kContentCnt = kLast_Content + 1; |
| 379 | |
| 380 | enum Layout { |
| 381 | kSimple_Layout, |
| 382 | kTiled_Layout, |
| 383 | |
| 384 | kLast_Layout = kTiled_Layout |
| 385 | }; |
| 386 | |
| 387 | static const int kLayoutCnt = kLast_Layout + 1; |
| 388 | |
| 389 | MultiPictureDraw(Content content, Layout layout) : fContent(content), fLayout(layout) { |
| 390 | SkASSERT(SK_ARRAY_COUNT(gLayoutMthds) == kLayoutCnt); |
| 391 | SkASSERT(SK_ARRAY_COUNT(gContentMthds) == kContentCnt); |
| 392 | |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 393 | for (int i = 0; i < kNumPictures; ++i) { |
| 394 | fPictures[i] = NULL; |
| 395 | } |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | virtual ~MultiPictureDraw() { |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 399 | for (int i = 0; i < kNumPictures; ++i) { |
| 400 | SkSafeUnref(fPictures[i]); |
| 401 | } |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | protected: |
| 405 | Content fContent; |
| 406 | Layout fLayout; |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 407 | const SkPicture* fPictures[kNumPictures]; |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 408 | |
| 409 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 410 | fPictures[0] = make_hex_plane_picture(SK_ColorWHITE); |
| 411 | fPictures[1] = make_hex_plane_picture(SK_ColorGRAY); |
robertphillips | ab79ab5 | 2014-09-19 11:30:37 -0700 | [diff] [blame] | 412 | fPictures[2] = make_sierpinski_picture(); |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 413 | } |
| 414 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 415 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 416 | SkMultiPictureDraw mpd; |
| 417 | SkTArray<ComposeStep> composeSteps; |
| 418 | |
| 419 | // Fill up the MultiPictureDraw |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 420 | (*gLayoutMthds[fLayout])(canvas, &mpd, |
| 421 | gContentMthds[fContent], |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 422 | fPictures, &composeSteps); |
| 423 | |
| 424 | mpd.draw(); |
| 425 | |
| 426 | // Compose all the drawn canvases into the final canvas |
| 427 | for (int i = 0; i < composeSteps.count(); ++i) { |
| 428 | const ComposeStep& step = composeSteps[i]; |
| 429 | |
| 430 | SkAutoTUnref<SkImage> image(step.fSurf->newImageSnapshot()); |
| 431 | |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 432 | canvas->drawImage(image, step.fX, step.fY, step.fPaint); |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 436 | virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(kPicWidth, kPicHeight); } |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 437 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 438 | virtual SkString onShortName() SK_OVERRIDE { |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 439 | static const char* gContentNames[] = { |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 440 | "noclip", "rectclip", "rrectclip", "pathclip", "invpathclip", "sierpinski" |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 441 | }; |
| 442 | static const char* gLayoutNames[] = { "simple", "tiled" }; |
| 443 | |
| 444 | SkASSERT(SK_ARRAY_COUNT(gLayoutNames) == kLayoutCnt); |
| 445 | SkASSERT(SK_ARRAY_COUNT(gContentNames) == kContentCnt); |
| 446 | |
| 447 | SkString name("multipicturedraw_"); |
| 448 | |
| 449 | name.append(gContentNames[fContent]); |
| 450 | name.append("_"); |
| 451 | name.append(gLayoutNames[fLayout]); |
| 452 | return name; |
| 453 | } |
| 454 | |
| 455 | virtual uint32_t onGetFlags() const SK_OVERRIDE { return kAsBench_Flag | kSkipTiled_Flag; } |
| 456 | |
| 457 | private: |
| 458 | typedef GM INHERITED; |
| 459 | }; |
| 460 | |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 461 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kNoClipSingle_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 462 | MultiPictureDraw::kSimple_Layout));) |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 463 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRectClipMulti_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 464 | MultiPictureDraw::kSimple_Layout));) |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 465 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRRectClipMulti_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 466 | MultiPictureDraw::kSimple_Layout));) |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 467 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kPathClipMulti_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 468 | MultiPictureDraw::kSimple_Layout));) |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 469 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kInvPathClipMulti_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 470 | MultiPictureDraw::kSimple_Layout));) |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 471 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kSierpinski_Content, |
| 472 | MultiPictureDraw::kSimple_Layout));) |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 473 | |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 474 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kNoClipSingle_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 475 | MultiPictureDraw::kTiled_Layout));) |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 476 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRectClipMulti_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 477 | MultiPictureDraw::kTiled_Layout));) |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 478 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kRRectClipMulti_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 479 | MultiPictureDraw::kTiled_Layout));) |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 480 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kPathClipMulti_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 481 | MultiPictureDraw::kTiled_Layout));) |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 482 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kInvPathClipMulti_Content, |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 483 | MultiPictureDraw::kTiled_Layout));) |
robertphillips | 3bc25e7 | 2014-09-19 08:56:09 -0700 | [diff] [blame] | 484 | DEF_GM(return SkNEW_ARGS(MultiPictureDraw, (MultiPictureDraw::kSierpinski_Content, |
| 485 | MultiPictureDraw::kTiled_Layout));) |
robertphillips | 7eacd77 | 2014-08-21 13:12:42 -0700 | [diff] [blame] | 486 | } |