aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/BezierBench.cpp
diff options
context:
space:
mode:
authorGravatar caryclark <caryclark@google.com>2014-10-09 05:36:03 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-10-09 05:36:04 -0700
commitfeff7d2d7719f52c7ea52db156003e609002bf04 (patch)
tree556123b8794cb89b42bb2a37f8312d910175c728 /bench/BezierBench.cpp
parent5867736b08d3689356b49f505bcf748c2194a0bc (diff)
Draw more accurate thick-stroked Beziers (disabled)
Draw thick-stroked Beziers by computing the outset quadratic, measuring the error, and subdividing until the error is within a predetermined limit. To try this CL out, change src/core/SkStroke.h:18 to #define QUAD_STROKE_APPROXIMATION 1 or from the command line: CPPFLAGS="-D QUAD_STROKE_APPROXIMATION=1" ./gyp_skia Here's what's in this CL: bench/BezierBench.cpp : a microbench for examining where the time is going gm/beziers.cpp : random Beziers with various thicknesses gm/smallarc.cpp : a distillation of bug skia:2769 samplecode/SampleRotateCircles.cpp : controls added for error, limit, width src/core/SkStroke.cpp : the new stroke implementation (disabled) tests/StrokerTest.cpp : a stroke torture test that checks normal and extreme values The new stroke algorithm has a tweakable parameter: stroker.setError(1); (SkStrokeRec.cpp:112) The stroke error is the allowable gap between the midpoint of the stroke quadratic and the center Bezier. As the projection from the quadratic approaches the endpoints, the error is decreased proportionally so that it is always inside the quadratic curve. An overview of how this works: - For a given T range of a Bezier, compute the perpendiculars and find the points outset and inset for some radius. - Construct tangents for the quadratic stroke. - If the tangent don't intersect between them (may happen with cubics), subdivide. - If the quadratic stroke end points are close (again, may happen with cubics), draw a line between them. - Compute the quadratic formed by the intersecting tangents. - If the midpoint of the quadratic is close to the midpoint of the Bezier perpendicular, return the quadratic. - If the end of the stroke at the Bezier midpoint doesn't intersect the quad's bounds, subdivide. - Find where the Bezier midpoint ray intersects the quadratic. - If the intersection is too close to the quad's endpoints, subdivide. - If the error is large proportional to the intersection's distance to the quad's endpoints, subdivide. BUG=skia:723,skia:2769 Review URL: https://codereview.chromium.org/558163005
Diffstat (limited to 'bench/BezierBench.cpp')
-rwxr-xr-xbench/BezierBench.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/bench/BezierBench.cpp b/bench/BezierBench.cpp
new file mode 100755
index 0000000000..ec83d958bb
--- /dev/null
+++ b/bench/BezierBench.cpp
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "Benchmark.h"
+#include "SkCanvas.h"
+#include "SkPaint.h"
+#include "SkString.h"
+
+struct BezierRec {
+ SkCanvas* fCanvas;
+ SkPaint fPaint;
+ SkPath fQuad;
+ SkPath fCubic;
+};
+
+typedef const char* (*DrawProc)(const BezierRec*, int);
+
+static const char* draw_quad(const BezierRec* rec, int count) {
+ if (rec) {
+ SkCanvas* canvas = rec->fCanvas;
+ const SkPaint& paint = rec->fPaint;
+ const SkPath& path = rec->fQuad;
+ for (int i = 0; i < count; ++i) {
+ canvas->drawPath(path, paint);
+ }
+ }
+ return "quad";
+}
+
+static const char* draw_cubic(const BezierRec* rec, int count) {
+ if (rec) {
+ SkCanvas* canvas = rec->fCanvas;
+ const SkPaint& paint = rec->fPaint;
+ const SkPath& path = rec->fCubic;
+ for (int i = 0; i < count; ++i) {
+ canvas->drawPath(path, paint);
+ }
+ }
+ return "cubic";
+}
+
+class BezierBench : public Benchmark {
+ SkString fName;
+ SkPaint::Cap fCap;
+ SkPaint::Join fJoin;
+ BezierRec fRec;
+ DrawProc fProc;
+ SkScalar fWidth;
+public:
+ BezierBench(SkPaint::Cap c, SkPaint::Join j, SkScalar w, DrawProc proc) {
+ static const char* gCapName[] = {
+ "butt", "round", "square"
+ };
+ static const char* gJoinName[] = {
+ "miter", "round", "bevel"
+ };
+
+ fCap = c;
+ fJoin = j;
+ fProc = proc;
+ fWidth = SkIntToScalar(w);
+ fName.printf("draw_stroke_bezier_%s_%s_%s_%g", proc(NULL, 0), gCapName[c], gJoinName[j], w);
+
+ fRec.fQuad.moveTo(20, 20);
+ fRec.fQuad.quadTo(60, 20, 60, 60);
+ fRec.fQuad.quadTo(20, 60, 20, 100);
+ fRec.fCubic.moveTo(20, 20);
+ fRec.fCubic.cubicTo(40, 20, 60, 40, 60, 60);
+ fRec.fCubic.cubicTo(40, 60, 20, 80, 20, 100);
+ }
+
+protected:
+ virtual const char* onGetName() {
+ return fName.c_str();
+ }
+
+ virtual void onDraw(const int loops, SkCanvas* canvas) {
+ fRec.fCanvas = canvas;
+ this->setupPaint(&fRec.fPaint);
+ fRec.fPaint.setStyle(SkPaint::kStroke_Style);
+ fRec.fPaint.setStrokeCap(fCap);
+ fRec.fPaint.setStrokeJoin(fJoin);
+ fRec.fPaint.setStrokeWidth(fWidth);
+ fProc(&fRec, loops);
+ }
+
+private:
+ typedef Benchmark INHERITED;
+};
+
+DEF_BENCH( return new BezierBench(SkPaint::kButt_Cap, SkPaint::kRound_Join, 2, draw_quad); )
+DEF_BENCH( return new BezierBench(SkPaint::kSquare_Cap, SkPaint::kBevel_Join, 10, draw_quad); )
+DEF_BENCH( return new BezierBench(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 50, draw_quad); )
+
+DEF_BENCH( return new BezierBench(SkPaint::kButt_Cap, SkPaint::kRound_Join, 2, draw_cubic); )
+DEF_BENCH( return new BezierBench(SkPaint::kSquare_Cap, SkPaint::kBevel_Join, 10, draw_cubic); )
+DEF_BENCH( return new BezierBench(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 50, draw_cubic); )