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