blob: f1ff544e6901278f58828bbce239fab73af642d4 [file] [log] [blame]
reed@google.com4aa1a702012-05-04 16:37:45 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SkBenchmark.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkDashPathEffect.h"
12#include "SkPaint.h"
13#include "SkPath.h"
14#include "SkRandom.h"
15#include "SkString.h"
16#include "SkTDArray.h"
17
18/*
19 * Cases to consider:
20 *
21 * 1. antialiasing on/off (esp. width <= 1)
22 * 2. strokewidth == 0, 1, 2
23 * 3. hline, vline, diagonal, rect, oval
24 * 4. dots [1,1] ([N,N] where N=strokeWidth?) or arbitrary (e.g. [2,1] or [1,2,3,2])
25 */
26static void path_hline(SkPath* path) {
27 path->moveTo(SkIntToScalar(10), SkIntToScalar(10));
28 path->lineTo(SkIntToScalar(600), SkIntToScalar(10));
29}
30
31class DashBench : public SkBenchmark {
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000032 SkString fName;
reed@google.com4aa1a702012-05-04 16:37:45 +000033 SkTDArray<SkScalar> fIntervals;
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000034 int fWidth;
reed@google.com4aa1a702012-05-04 16:37:45 +000035
36 enum {
37 N = SkBENCHLOOP(100)
38 };
39public:
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000040 DashBench(void* param, const SkScalar intervals[], int count, int width) : INHERITED(param) {
reed@google.com4aa1a702012-05-04 16:37:45 +000041 fIntervals.append(count, intervals);
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000042 for (int i = 0; i < count; ++i) {
43 fIntervals[i] *= width;
44 }
45 fWidth = width;
46 fName.printf("dash_%d", width);
reed@google.com4aa1a702012-05-04 16:37:45 +000047 }
48
49 virtual void makePath(SkPath* path) {
50 path_hline(path);
51 }
52
53protected:
54 virtual const char* onGetName() SK_OVERRIDE {
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000055 return fName.c_str();
reed@google.com4aa1a702012-05-04 16:37:45 +000056 }
57
58 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
59 SkPaint paint;
60 this->setupPaint(&paint);
61 paint.setStyle(SkPaint::kStroke_Style);
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000062 paint.setStrokeWidth(SkIntToScalar(fWidth));
reed@google.com4aa1a702012-05-04 16:37:45 +000063 paint.setAntiAlias(false);
64
65 SkPath path;
66 this->makePath(&path);
67
68 paint.setPathEffect(new SkDashPathEffect(fIntervals.begin(),
69 fIntervals.count(), 0))->unref();
70 for (int i = 0; i < N; ++i) {
71 canvas->drawPath(path, paint);
72 }
73 }
74
75private:
76 typedef SkBenchmark INHERITED;
77};
78
79///////////////////////////////////////////////////////////////////////////////
80
81static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 };
82
83#define PARAM(array) array, SK_ARRAY_COUNT(array)
84
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000085static SkBenchmark* gF0(void* p) { return new DashBench(p, PARAM(gDots), 0); }
86static SkBenchmark* gF1(void* p) { return new DashBench(p, PARAM(gDots), 1); }
87static SkBenchmark* gF2(void* p) { return new DashBench(p, PARAM(gDots), 4); }
reed@google.com4aa1a702012-05-04 16:37:45 +000088
89static BenchRegistry gR0(gF0);
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000090static BenchRegistry gR1(gF1);
91static BenchRegistry gR2(gF2);