skia2/bench/BezierBench.cpp
caryclark feff7d2d77 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
2014-10-09 05:36:04 -07:00

102 lines
3.0 KiB
C++
Executable File

/*
* 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); )