2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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.
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
22 * See crbug.com/589769 skbug.com/5405 skbug.com/5406
29 #include "SkPathMeasure.h"
31 const SkScalar OVERSTROKE_WIDTH = 500.0f;
32 const SkScalar NORMALSTROKE_WIDTH = 3.0f;
34 //////// path and paint builders
36 SkPaint make_normal_paint() {
39 p.setStyle(SkPaint::kStroke_Style);
40 p.setStrokeWidth(NORMALSTROKE_WIDTH);
41 p.setColor(SK_ColorBLUE);
46 SkPaint make_overstroke_paint() {
49 p.setStyle(SkPaint::kStroke_Style);
50 p.setStrokeWidth(OVERSTROKE_WIDTH);
77 SkRect oval = SkRect::MakeXYWH(0, -25, 100, 50);
80 path.arcTo(oval, 0, 359, true);
86 SkPath ribs_path(SkPath path, SkScalar radius) {
89 const SkScalar spacing = 5.0f;
92 SkPathMeasure meas(path, false);
93 SkScalar length = meas.getLength();
96 while (accum < length) {
97 if (meas.getPosTan(accum, &pos, &tan)) {
101 ribs.moveTo(pos.x() + tan.x(), pos.y() + tan.y());
102 ribs.lineTo(pos.x() - tan.x(), pos.y() - tan.y());
110 void draw_ribs(SkCanvas *canvas, SkPath path) {
111 SkPath ribs = ribs_path(path, OVERSTROKE_WIDTH/2.0f);
112 SkPaint p = make_normal_paint();
114 p.setColor(SK_ColorBLUE);
115 p.setColor(SK_ColorGREEN);
117 canvas->drawPath(ribs, p);
122 void draw_small_quad(SkCanvas *canvas) {
123 // scaled so it's visible
124 // canvas->scale(8, 8);
126 SkPaint p = make_normal_paint();
127 SkPath path = quad_path();
129 draw_ribs(canvas, path);
130 canvas->drawPath(path, p);
133 void draw_large_quad(SkCanvas *canvas) {
134 SkPaint p = make_overstroke_paint();
135 SkPath path = quad_path();
137 canvas->drawPath(path, p);
138 draw_ribs(canvas, path);
141 void draw_quad_fillpath(SkCanvas *canvas) {
142 SkPath path = quad_path();
143 SkPaint p = make_overstroke_paint();
145 SkPaint fillp = make_normal_paint();
146 fillp.setColor(SK_ColorMAGENTA);
149 p.getFillPath(path, &fillpath);
151 canvas->drawPath(fillpath, fillp);
154 void draw_stroked_quad(SkCanvas *canvas) {
155 canvas->translate(400, 0);
156 draw_large_quad(canvas);
157 draw_quad_fillpath(canvas);
162 void draw_small_cubic(SkCanvas *canvas) {
163 SkPaint p = make_normal_paint();
164 SkPath path = cubic_path();
166 draw_ribs(canvas, path);
167 canvas->drawPath(path, p);
170 void draw_large_cubic(SkCanvas *canvas) {
171 SkPaint p = make_overstroke_paint();
172 SkPath path = cubic_path();
174 canvas->drawPath(path, p);
175 draw_ribs(canvas, path);
178 void draw_cubic_fillpath(SkCanvas *canvas) {
179 SkPath path = cubic_path();
180 SkPaint p = make_overstroke_paint();
182 SkPaint fillp = make_normal_paint();
183 fillp.setColor(SK_ColorMAGENTA);
186 p.getFillPath(path, &fillpath);
188 canvas->drawPath(fillpath, fillp);
191 void draw_stroked_cubic(SkCanvas *canvas) {
192 canvas->translate(400, 0);
193 draw_large_cubic(canvas);
194 draw_cubic_fillpath(canvas);
199 void draw_small_oval(SkCanvas *canvas) {
200 SkPaint p = make_normal_paint();
202 SkPath path = oval_path();
204 draw_ribs(canvas, path);
205 canvas->drawPath(path, p);
208 void draw_large_oval(SkCanvas *canvas) {
209 SkPaint p = make_overstroke_paint();
210 SkPath path = oval_path();
212 canvas->drawPath(path, p);
213 draw_ribs(canvas, path);
216 void draw_oval_fillpath(SkCanvas *canvas) {
217 SkPath path = oval_path();
218 SkPaint p = make_overstroke_paint();
220 SkPaint fillp = make_normal_paint();
221 fillp.setColor(SK_ColorMAGENTA);
224 p.getFillPath(path, &fillpath);
226 canvas->drawPath(fillpath, fillp);
229 void draw_stroked_oval(SkCanvas *canvas) {
230 canvas->translate(400, 0);
231 draw_large_oval(canvas);
232 draw_oval_fillpath(canvas);
237 void (*examples[])(SkCanvas *canvas) = {
238 draw_small_quad, draw_stroked_quad, draw_small_cubic,
239 draw_stroked_cubic, draw_small_oval, draw_stroked_oval,
242 DEF_SIMPLE_GM(OverStroke, canvas, 500, 500) {
243 const size_t length = sizeof(examples) / sizeof(examples[0]);
244 const size_t width = 2;
246 for (size_t i = 0; i < length; i++) {
247 int x = (int)(i % width);
248 int y = (int)(i / width);
251 canvas->translate(150.0f * x, 150.0f * y);
252 canvas->scale(0.2f, 0.2f);
253 canvas->translate(300.0f, 400.0f);