2012-05-04 16:37:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "SkBenchmark.h"
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDashPathEffect.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkTDArray.h"
|
|
|
|
|
2012-05-10 15:40:57 +00:00
|
|
|
|
2012-05-04 16:37:45 +00:00
|
|
|
/*
|
|
|
|
* Cases to consider:
|
|
|
|
*
|
|
|
|
* 1. antialiasing on/off (esp. width <= 1)
|
|
|
|
* 2. strokewidth == 0, 1, 2
|
|
|
|
* 3. hline, vline, diagonal, rect, oval
|
|
|
|
* 4. dots [1,1] ([N,N] where N=strokeWidth?) or arbitrary (e.g. [2,1] or [1,2,3,2])
|
|
|
|
*/
|
|
|
|
static void path_hline(SkPath* path) {
|
|
|
|
path->moveTo(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
path->lineTo(SkIntToScalar(600), SkIntToScalar(10));
|
|
|
|
}
|
|
|
|
|
|
|
|
class DashBench : public SkBenchmark {
|
2012-05-10 15:40:57 +00:00
|
|
|
protected:
|
2012-05-05 13:46:10 +00:00
|
|
|
SkString fName;
|
2012-05-04 16:37:45 +00:00
|
|
|
SkTDArray<SkScalar> fIntervals;
|
2012-05-05 13:46:10 +00:00
|
|
|
int fWidth;
|
2012-05-16 14:06:02 +00:00
|
|
|
SkPoint fPts[2];
|
|
|
|
bool fDoClip;
|
2012-05-04 16:37:45 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
N = SkBENCHLOOP(100)
|
|
|
|
};
|
|
|
|
public:
|
2012-05-15 19:50:58 +00:00
|
|
|
DashBench(void* param, const SkScalar intervals[], int count, int width,
|
|
|
|
bool doClip = false) : INHERITED(param) {
|
2012-05-04 16:37:45 +00:00
|
|
|
fIntervals.append(count, intervals);
|
2012-05-05 13:46:10 +00:00
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
fIntervals[i] *= width;
|
|
|
|
}
|
|
|
|
fWidth = width;
|
2012-05-15 19:50:58 +00:00
|
|
|
fName.printf("dash_%d_%s", width, doClip ? "clipped" : "noclip");
|
|
|
|
fDoClip = doClip;
|
2012-05-16 14:06:02 +00:00
|
|
|
|
|
|
|
fPts[0].set(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
fPts[1].set(SkIntToScalar(600), SkIntToScalar(10));
|
2012-05-04 16:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void makePath(SkPath* path) {
|
|
|
|
path_hline(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() SK_OVERRIDE {
|
2012-05-05 13:46:10 +00:00
|
|
|
return fName.c_str();
|
2012-05-04 16:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
SkPaint paint;
|
|
|
|
this->setupPaint(&paint);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2012-05-05 13:46:10 +00:00
|
|
|
paint.setStrokeWidth(SkIntToScalar(fWidth));
|
2012-05-04 16:37:45 +00:00
|
|
|
paint.setAntiAlias(false);
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
this->makePath(&path);
|
|
|
|
|
|
|
|
paint.setPathEffect(new SkDashPathEffect(fIntervals.begin(),
|
|
|
|
fIntervals.count(), 0))->unref();
|
2012-05-15 19:50:58 +00:00
|
|
|
|
|
|
|
if (fDoClip) {
|
|
|
|
SkRect r = path.getBounds();
|
|
|
|
r.inset(-SkIntToScalar(20), -SkIntToScalar(20));
|
|
|
|
// now move it so we don't intersect
|
|
|
|
r.offset(0, r.height() * 3 / 2);
|
|
|
|
canvas->clipRect(r);
|
|
|
|
}
|
|
|
|
|
2012-05-10 15:40:57 +00:00
|
|
|
this->handlePath(canvas, path, paint, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void handlePath(SkCanvas* canvas, const SkPath& path,
|
|
|
|
const SkPaint& paint, int N) {
|
2012-05-04 16:37:45 +00:00
|
|
|
for (int i = 0; i < N; ++i) {
|
2012-05-16 14:06:02 +00:00
|
|
|
// canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, paint);
|
2012-05-04 16:37:45 +00:00
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkBenchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-05-10 15:40:57 +00:00
|
|
|
class RectDashBench : public DashBench {
|
|
|
|
public:
|
2012-05-15 19:50:58 +00:00
|
|
|
RectDashBench(void* param, const SkScalar intervals[], int count, int width, bool doClip = false)
|
2012-05-10 15:40:57 +00:00
|
|
|
: INHERITED(param, intervals, count, width) {
|
|
|
|
fName.append("_rect");
|
|
|
|
}
|
2012-05-18 18:32:54 +00:00
|
|
|
|
2012-05-10 15:40:57 +00:00
|
|
|
protected:
|
|
|
|
virtual void handlePath(SkCanvas* canvas, const SkPath& path,
|
|
|
|
const SkPaint& paint, int N) SK_OVERRIDE {
|
|
|
|
SkPoint pts[2];
|
|
|
|
if (!path.isLine(pts) || pts[0].fY != pts[1].fY) {
|
|
|
|
this->INHERITED::handlePath(canvas, path, paint, N);
|
|
|
|
} else {
|
|
|
|
SkRect rect;
|
|
|
|
rect.fLeft = pts[0].fX;
|
|
|
|
rect.fTop = pts[0].fY - paint.getStrokeWidth() / 2;
|
|
|
|
rect.fRight = rect.fLeft + SkIntToScalar(fWidth);
|
|
|
|
rect.fBottom = rect.fTop + paint.getStrokeWidth();
|
2012-05-18 18:32:54 +00:00
|
|
|
|
2012-05-10 15:40:57 +00:00
|
|
|
SkPaint p(paint);
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
|
|
|
p.setPathEffect(NULL);
|
2012-05-18 18:32:54 +00:00
|
|
|
|
2012-05-10 15:40:57 +00:00
|
|
|
int count = SkScalarRoundToInt((pts[1].fX - pts[0].fX) / (2*fWidth));
|
|
|
|
SkScalar dx = SkIntToScalar(2 * fWidth);
|
2012-05-18 18:32:54 +00:00
|
|
|
|
2012-05-10 15:40:57 +00:00
|
|
|
for (int i = 0; i < N*10; ++i) {
|
|
|
|
SkRect r = rect;
|
|
|
|
for (int j = 0; j < count; ++j) {
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
r.offset(dx, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef DashBench INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-05-18 18:32:54 +00:00
|
|
|
static void make_unit_star(SkPath* path, int n) {
|
|
|
|
SkScalar rad = -SK_ScalarPI / 2;
|
|
|
|
const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
|
|
|
|
|
|
|
|
path->moveTo(0, -SK_Scalar1);
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
|
|
rad += drad;
|
|
|
|
SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
|
|
|
|
path->lineTo(cosV, sinV);
|
|
|
|
}
|
|
|
|
path->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_poly(SkPath* path) {
|
|
|
|
make_unit_star(path, 9);
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
path->transform(matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_quad(SkPath* path) {
|
|
|
|
SkScalar x0 = SkIntToScalar(10);
|
|
|
|
SkScalar y0 = SkIntToScalar(10);
|
|
|
|
path->moveTo(x0, y0);
|
|
|
|
path->quadTo(x0, y0 + 400 * SK_Scalar1,
|
|
|
|
x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_cubic(SkPath* path) {
|
|
|
|
SkScalar x0 = SkIntToScalar(10);
|
|
|
|
SkScalar y0 = SkIntToScalar(10);
|
|
|
|
path->moveTo(x0, y0);
|
|
|
|
path->cubicTo(x0, y0 + 400 * SK_Scalar1,
|
|
|
|
x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1,
|
|
|
|
x0 + 600 * SK_Scalar1, y0);
|
|
|
|
}
|
|
|
|
|
|
|
|
class MakeDashBench : public SkBenchmark {
|
|
|
|
SkString fName;
|
|
|
|
SkPath fPath;
|
|
|
|
SkAutoTUnref<SkPathEffect> fPE;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
N = SkBENCHLOOP(400)
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
MakeDashBench(void* param, void (*proc)(SkPath*), const char name[]) : INHERITED(param) {
|
|
|
|
fName.printf("makedash_%s", name);
|
|
|
|
proc(&fPath);
|
|
|
|
|
|
|
|
SkScalar vals[] = { SkIntToScalar(4), SkIntToScalar(4) };
|
|
|
|
fPE.reset(new SkDashPathEffect(vals, 2, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() SK_OVERRIDE {
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
SkPath dst;
|
|
|
|
for (int i = 0; i < N; ++i) {
|
Change patheffect to take a (new) StrokeRec object, which encapsulates the fill
or stroke parameters for a path.
Today, the patheffect only sees if the caller was going to stroke or fill, and
if stroke, it just sees the width. With this change, the effect can see all of the
related parameters (e.g. cap/join/miter). No other change is intended at this
time.
After this change, I hope to use this additional data to allow SkDashPathEffect
to, at times, apply the stroke as part of its effect, which may be much more
efficient than first dashing, and then reading that and stroking it.
Most of these files changed just because of the new parameter to filterPath. The
key changes are in SkPathEffect.[h,cpp], SkPaint.cpp and SkScalerContext.cpp
Review URL: https://codereview.appspot.com/6250051
git-svn-id: http://skia.googlecode.com/svn/trunk@4048 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-05-25 01:04:12 +00:00
|
|
|
SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
|
2012-05-18 18:32:54 +00:00
|
|
|
|
Change patheffect to take a (new) StrokeRec object, which encapsulates the fill
or stroke parameters for a path.
Today, the patheffect only sees if the caller was going to stroke or fill, and
if stroke, it just sees the width. With this change, the effect can see all of the
related parameters (e.g. cap/join/miter). No other change is intended at this
time.
After this change, I hope to use this additional data to allow SkDashPathEffect
to, at times, apply the stroke as part of its effect, which may be much more
efficient than first dashing, and then reading that and stroking it.
Most of these files changed just because of the new parameter to filterPath. The
key changes are in SkPathEffect.[h,cpp], SkPaint.cpp and SkScalerContext.cpp
Review URL: https://codereview.appspot.com/6250051
git-svn-id: http://skia.googlecode.com/svn/trunk@4048 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-05-25 01:04:12 +00:00
|
|
|
fPE->filterPath(&dst, fPath, &rec);
|
2012-05-18 18:32:54 +00:00
|
|
|
dst.rewind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkBenchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
2012-05-04 16:37:45 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 };
|
|
|
|
|
|
|
|
#define PARAM(array) array, SK_ARRAY_COUNT(array)
|
|
|
|
|
2012-05-05 13:46:10 +00:00
|
|
|
static SkBenchmark* gF0(void* p) { return new DashBench(p, PARAM(gDots), 0); }
|
|
|
|
static SkBenchmark* gF1(void* p) { return new DashBench(p, PARAM(gDots), 1); }
|
2012-05-15 19:50:58 +00:00
|
|
|
static SkBenchmark* gF2(void* p) { return new DashBench(p, PARAM(gDots), 1, true); }
|
|
|
|
static SkBenchmark* gF3(void* p) { return new DashBench(p, PARAM(gDots), 4); }
|
2012-05-18 18:32:54 +00:00
|
|
|
static SkBenchmark* gF4(void* p) { return new MakeDashBench(p, make_poly, "poly"); }
|
|
|
|
static SkBenchmark* gF5(void* p) { return new MakeDashBench(p, make_quad, "quad"); }
|
|
|
|
static SkBenchmark* gF6(void* p) { return new MakeDashBench(p, make_cubic, "cubic"); }
|
2012-05-04 16:37:45 +00:00
|
|
|
|
|
|
|
static BenchRegistry gR0(gF0);
|
2012-05-05 13:46:10 +00:00
|
|
|
static BenchRegistry gR1(gF1);
|
|
|
|
static BenchRegistry gR2(gF2);
|
2012-05-10 15:40:57 +00:00
|
|
|
static BenchRegistry gR3(gF3);
|
2012-05-15 19:50:58 +00:00
|
|
|
static BenchRegistry gR4(gF4);
|
2012-05-18 18:32:54 +00:00
|
|
|
static BenchRegistry gR5(gF5);
|
|
|
|
static BenchRegistry gR6(gF6);
|