blob: 355d55c2a1ebfeba682ff796e18bf8f986687f44 [file] [log] [blame]
humper@google.com7c7292c2013-01-04 20:29:03 +00001
2/*
3 * Copyright 2013 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 "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkShader.h"
13#include "SkString.h"
14#include "SkBlurMask.h"
15
16#define SMALL SkIntToScalar(2)
17#define REAL SkFloatToScalar(1.5f)
18#define BIG SkIntToScalar(10)
19#define REALBIG SkFloatToScalar(30.5f)
20
21class BlurRectBench: public SkBenchmark {
22 SkScalar fRadius;
23 SkString fName;
24
25public:
26 BlurRectBench(void *param, SkScalar rad) : INHERITED(param) {
27 fRadius = rad;
28 }
29
30protected:
31 virtual const char* onGetName() {
32 return fName.c_str();
33 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000034
humper@google.com7c7292c2013-01-04 20:29:03 +000035 SkScalar radius() const {
36 return fRadius;
37 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000038
humper@google.com7c7292c2013-01-04 20:29:03 +000039 void setName( SkString name ) {
40 fName = name;
41 }
42
43 virtual void onDraw(SkCanvas* canvas) {
44 SkPaint paint;
45 this->setupPaint(&paint);
46
47 paint.setAntiAlias(true);
48
49 int pad = fRadius * 1.5 + 1;
50 SkRect r = SkRect::MakeWH(2 * pad + 1, 2 * pad + 1);
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000051
humper@google.com7c7292c2013-01-04 20:29:03 +000052 int loop_count;
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000053
humper@google.com7c7292c2013-01-04 20:29:03 +000054 if (fRadius > SkIntToScalar(50)) {
55 loop_count = 10;
56 } else if (fRadius > SkIntToScalar(5)) {
57 loop_count = 1000;
58 } else {
59 loop_count = 10000;
60 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000061
humper@google.com7c7292c2013-01-04 20:29:03 +000062 preBenchSetup( r );
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000063
humper@google.com7c7292c2013-01-04 20:29:03 +000064 for (int i = 0; i < SkBENCHLOOP(loop_count); i++) {
65 makeBlurryRect( r );
66 }
67 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000068
humper@google.com7c7292c2013-01-04 20:29:03 +000069 virtual void makeBlurryRect( SkRect &r ) = 0;
70 virtual void preBenchSetup( SkRect &r ) {}
71private:
72 typedef SkBenchmark INHERITED;
73};
74
75
76class BlurRectDirectBench: public BlurRectBench {
77 public:
78 BlurRectDirectBench( void *param, SkScalar rad ) : BlurRectBench( param, rad ) {
79 SkString name;
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000080
humper@google.com7c7292c2013-01-04 20:29:03 +000081 if (SkScalarFraction(rad) != 0) {
82 name.printf("blurrect_direct_%.2f", SkScalarToFloat(rad));
83 } else {
84 name.printf("blurrect_direct_%d", SkScalarRound(rad));
85 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000086
humper@google.com7c7292c2013-01-04 20:29:03 +000087 setName( name );
88 }
89protected:
90 virtual void makeBlurryRect( SkRect &r ) {
91 SkMask mask;
92 SkBlurMask::BlurRect( &mask, r, radius(), SkBlurMask::kNormal_Style, SkBlurMask::kHigh_Quality );
93 }
94};
95
96class BlurRectSeparableBench: public BlurRectBench {
97 SkMask fSrcMask;
98public:
99 BlurRectSeparableBench(void *param, SkScalar rad) : BlurRectBench( param, rad ) {
100 SkString name;
101 if (SkScalarFraction(rad) != 0) {
102 name.printf("blurrect_separable_%.2f", SkScalarToFloat(rad));
103 } else {
104 name.printf("blurrect_separable_%d", SkScalarRound(rad));
105 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000106
humper@google.com7c7292c2013-01-04 20:29:03 +0000107 setName( name );
108 }
109
110protected:
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000111 virtual void preBenchSetup( SkRect &r ) {
humper@google.com7c7292c2013-01-04 20:29:03 +0000112 fSrcMask.fFormat = SkMask::kA8_Format;
113 fSrcMask.fRowBytes = r.width();
114 fSrcMask.fBounds = SkIRect::MakeWH(r.width(), r.height());
115 fSrcMask.fImage = SkMask::AllocImage( fSrcMask.computeTotalImageSize() );
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000116
humper@google.com7c7292c2013-01-04 20:29:03 +0000117 memset( fSrcMask.fImage, 0xff, fSrcMask.computeTotalImageSize() );
118 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000119
humper@google.com7c7292c2013-01-04 20:29:03 +0000120 virtual void makeBlurryRect( SkRect &r ) {
121 SkMask mask;
122 SkBlurMask::BlurSeparable( &mask, fSrcMask, radius(), SkBlurMask::kNormal_Style, SkBlurMask::kHigh_Quality );
123 }
124};
125
126DEF_BENCH(return new BlurRectSeparableBench(p, SMALL);)
127DEF_BENCH(return new BlurRectSeparableBench(p, BIG);)
128DEF_BENCH(return new BlurRectSeparableBench(p, REALBIG);)
129DEF_BENCH(return new BlurRectSeparableBench(p, REAL);)
130DEF_BENCH(return new BlurRectDirectBench(p, SMALL);)
131DEF_BENCH(return new BlurRectDirectBench(p, BIG);)
132DEF_BENCH(return new BlurRectDirectBench(p, REALBIG);)
133DEF_BENCH(return new BlurRectDirectBench(p, REAL);)