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