hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 9 | /* |
| 10 | * This GM exercises stroking of paths with large stroke lengths, which is |
| 11 | * referred to as "overstroke" for brevity. In Skia as of 8/2016 we offset |
| 12 | * each part of the curve the request amount even if it makes the offsets |
| 13 | * overlap and create holes. There is not a really great algorithm for this |
| 14 | * and several other 2D graphics engines have the same bug. |
| 15 | * |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 16 | * If we run this using Nvidia Path Renderer with: |
| 17 | * `path/to/dm --match OverStroke -w gm_out --gpu --config nvpr16` |
| 18 | * then we get correct results, so that is a possible direction of attack - |
| 19 | * use the GPU and a completely different algorithm to get correctness in |
| 20 | * Skia. |
| 21 | * |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 22 | * See crbug.com/589769 skbug.com/5405 skbug.com/5406 |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "gm.h" |
| 27 | #include "SkPaint.h" |
| 28 | #include "SkPath.h" |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 29 | #include "SkPathMeasure.h" |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 30 | #include "SkPointPriv.h" |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 31 | |
| 32 | const SkScalar OVERSTROKE_WIDTH = 500.0f; |
| 33 | const SkScalar NORMALSTROKE_WIDTH = 3.0f; |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 34 | |
| 35 | //////// path and paint builders |
| 36 | |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 37 | SkPaint make_normal_paint() { |
| 38 | SkPaint p; |
| 39 | p.setAntiAlias(true); |
| 40 | p.setStyle(SkPaint::kStroke_Style); |
| 41 | p.setStrokeWidth(NORMALSTROKE_WIDTH); |
| 42 | p.setColor(SK_ColorBLUE); |
| 43 | |
| 44 | return p; |
| 45 | } |
| 46 | |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 47 | SkPaint make_overstroke_paint() { |
| 48 | SkPaint p; |
| 49 | p.setAntiAlias(true); |
| 50 | p.setStyle(SkPaint::kStroke_Style); |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 51 | p.setStrokeWidth(OVERSTROKE_WIDTH); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 52 | |
| 53 | return p; |
| 54 | } |
| 55 | |
| 56 | SkPath quad_path() { |
| 57 | SkPath path; |
| 58 | path.moveTo(0, 0); |
| 59 | path.lineTo(100, 0); |
| 60 | path.quadTo(50, -40, |
| 61 | 0, 0); |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 62 | path.close(); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 63 | |
| 64 | return path; |
| 65 | } |
| 66 | |
| 67 | SkPath cubic_path() { |
| 68 | SkPath path; |
| 69 | path.moveTo(0, 0); |
| 70 | path.cubicTo(25, 75, |
| 71 | 75, -50, |
| 72 | 100, 0); |
| 73 | |
| 74 | return path; |
| 75 | } |
| 76 | |
| 77 | SkPath oval_path() { |
| 78 | SkRect oval = SkRect::MakeXYWH(0, -25, 100, 50); |
| 79 | |
| 80 | SkPath path; |
| 81 | path.arcTo(oval, 0, 359, true); |
| 82 | path.close(); |
| 83 | |
| 84 | return path; |
| 85 | } |
| 86 | |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 87 | SkPath ribs_path(SkPath path, SkScalar radius) { |
| 88 | SkPath ribs; |
| 89 | |
| 90 | const SkScalar spacing = 5.0f; |
| 91 | float accum = 0.0f; |
| 92 | |
| 93 | SkPathMeasure meas(path, false); |
| 94 | SkScalar length = meas.getLength(); |
| 95 | SkPoint pos; |
| 96 | SkVector tan; |
| 97 | while (accum < length) { |
| 98 | if (meas.getPosTan(accum, &pos, &tan)) { |
| 99 | tan.scale(radius); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 100 | SkPointPriv::RotateCCW(&tan); |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 101 | |
| 102 | ribs.moveTo(pos.x() + tan.x(), pos.y() + tan.y()); |
| 103 | ribs.lineTo(pos.x() - tan.x(), pos.y() - tan.y()); |
| 104 | } |
| 105 | accum += spacing; |
| 106 | } |
| 107 | |
| 108 | return ribs; |
| 109 | } |
| 110 | |
| 111 | void draw_ribs(SkCanvas *canvas, SkPath path) { |
| 112 | SkPath ribs = ribs_path(path, OVERSTROKE_WIDTH/2.0f); |
| 113 | SkPaint p = make_normal_paint(); |
| 114 | p.setStrokeWidth(1); |
| 115 | p.setColor(SK_ColorBLUE); |
| 116 | p.setColor(SK_ColorGREEN); |
| 117 | |
| 118 | canvas->drawPath(ribs, p); |
| 119 | } |
| 120 | |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 121 | ///////// quads |
| 122 | |
| 123 | void draw_small_quad(SkCanvas *canvas) { |
| 124 | // scaled so it's visible |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 125 | // canvas->scale(8, 8); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 126 | |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 127 | SkPaint p = make_normal_paint(); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 128 | SkPath path = quad_path(); |
| 129 | |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 130 | draw_ribs(canvas, path); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 131 | canvas->drawPath(path, p); |
| 132 | } |
| 133 | |
| 134 | void draw_large_quad(SkCanvas *canvas) { |
| 135 | SkPaint p = make_overstroke_paint(); |
| 136 | SkPath path = quad_path(); |
| 137 | |
| 138 | canvas->drawPath(path, p); |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 139 | draw_ribs(canvas, path); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void draw_quad_fillpath(SkCanvas *canvas) { |
| 143 | SkPath path = quad_path(); |
| 144 | SkPaint p = make_overstroke_paint(); |
| 145 | |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 146 | SkPaint fillp = make_normal_paint(); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 147 | fillp.setColor(SK_ColorMAGENTA); |
| 148 | |
| 149 | SkPath fillpath; |
| 150 | p.getFillPath(path, &fillpath); |
| 151 | |
| 152 | canvas->drawPath(fillpath, fillp); |
| 153 | } |
| 154 | |
| 155 | void draw_stroked_quad(SkCanvas *canvas) { |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 156 | canvas->translate(400, 0); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 157 | draw_large_quad(canvas); |
| 158 | draw_quad_fillpath(canvas); |
| 159 | } |
| 160 | |
| 161 | ////////// cubics |
| 162 | |
| 163 | void draw_small_cubic(SkCanvas *canvas) { |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 164 | SkPaint p = make_normal_paint(); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 165 | SkPath path = cubic_path(); |
| 166 | |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 167 | draw_ribs(canvas, path); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 168 | canvas->drawPath(path, p); |
| 169 | } |
| 170 | |
| 171 | void draw_large_cubic(SkCanvas *canvas) { |
| 172 | SkPaint p = make_overstroke_paint(); |
| 173 | SkPath path = cubic_path(); |
| 174 | |
| 175 | canvas->drawPath(path, p); |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 176 | draw_ribs(canvas, path); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void draw_cubic_fillpath(SkCanvas *canvas) { |
| 180 | SkPath path = cubic_path(); |
| 181 | SkPaint p = make_overstroke_paint(); |
| 182 | |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 183 | SkPaint fillp = make_normal_paint(); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 184 | fillp.setColor(SK_ColorMAGENTA); |
| 185 | |
| 186 | SkPath fillpath; |
| 187 | p.getFillPath(path, &fillpath); |
| 188 | |
| 189 | canvas->drawPath(fillpath, fillp); |
| 190 | } |
| 191 | |
| 192 | void draw_stroked_cubic(SkCanvas *canvas) { |
| 193 | canvas->translate(400, 0); |
| 194 | draw_large_cubic(canvas); |
| 195 | draw_cubic_fillpath(canvas); |
| 196 | } |
| 197 | |
| 198 | ////////// ovals |
| 199 | |
| 200 | void draw_small_oval(SkCanvas *canvas) { |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 201 | SkPaint p = make_normal_paint(); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 202 | |
| 203 | SkPath path = oval_path(); |
| 204 | |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 205 | draw_ribs(canvas, path); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 206 | canvas->drawPath(path, p); |
| 207 | } |
| 208 | |
| 209 | void draw_large_oval(SkCanvas *canvas) { |
| 210 | SkPaint p = make_overstroke_paint(); |
| 211 | SkPath path = oval_path(); |
| 212 | |
| 213 | canvas->drawPath(path, p); |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 214 | draw_ribs(canvas, path); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void draw_oval_fillpath(SkCanvas *canvas) { |
| 218 | SkPath path = oval_path(); |
| 219 | SkPaint p = make_overstroke_paint(); |
| 220 | |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 221 | SkPaint fillp = make_normal_paint(); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 222 | fillp.setColor(SK_ColorMAGENTA); |
| 223 | |
| 224 | SkPath fillpath; |
| 225 | p.getFillPath(path, &fillpath); |
| 226 | |
| 227 | canvas->drawPath(fillpath, fillp); |
| 228 | } |
| 229 | |
| 230 | void draw_stroked_oval(SkCanvas *canvas) { |
| 231 | canvas->translate(400, 0); |
| 232 | draw_large_oval(canvas); |
| 233 | draw_oval_fillpath(canvas); |
| 234 | } |
| 235 | |
| 236 | ////////// gm |
| 237 | |
| 238 | void (*examples[])(SkCanvas *canvas) = { |
| 239 | draw_small_quad, draw_stroked_quad, draw_small_cubic, |
| 240 | draw_stroked_cubic, draw_small_oval, draw_stroked_oval, |
| 241 | }; |
| 242 | |
| 243 | DEF_SIMPLE_GM(OverStroke, canvas, 500, 500) { |
| 244 | const size_t length = sizeof(examples) / sizeof(examples[0]); |
| 245 | const size_t width = 2; |
| 246 | |
| 247 | for (size_t i = 0; i < length; i++) { |
| 248 | int x = (int)(i % width); |
| 249 | int y = (int)(i / width); |
| 250 | |
| 251 | canvas->save(); |
hstern | bb9b224 | 2016-08-09 08:53:30 -0700 | [diff] [blame] | 252 | canvas->translate(150.0f * x, 150.0f * y); |
| 253 | canvas->scale(0.2f, 0.2f); |
| 254 | canvas->translate(300.0f, 400.0f); |
hstern | 02aea1c | 2016-08-02 10:35:57 -0700 | [diff] [blame] | 255 | |
| 256 | examples[i](canvas); |
| 257 | |
| 258 | canvas->restore(); |
| 259 | } |
| 260 | } |