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