blob: 5f2f425fbe870d834cf2c9eea61a76aa09f81fd5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
reed@google.com25df8882011-07-14 19:03:58 +00008#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 "SkBlurMaskFilter.h"
15
16#define SMALL SkIntToScalar(2)
tomhudson@google.com8caac642011-11-22 15:58:06 +000017#define REAL SkFloatToScalar(1.5f)
reed@google.com25df8882011-07-14 19:03:58 +000018#define BIG SkIntToScalar(10)
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000019#define REALBIG SkFloatToScalar(100.5f)
reed@google.com25df8882011-07-14 19:03:58 +000020
21static const char* gStyleName[] = {
22 "normal",
23 "solid",
24 "outer",
25 "inner"
26};
27
28class BlurBench : public SkBenchmark {
29 SkScalar fRadius;
30 SkBlurMaskFilter::BlurStyle fStyle;
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000031 uint32_t fFlags;
reed@google.com25df8882011-07-14 19:03:58 +000032 SkString fName;
33
34public:
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000035 BlurBench(void* param, SkScalar rad, SkBlurMaskFilter::BlurStyle bs, uint32_t flags = 0) : INHERITED(param) {
reed@google.com25df8882011-07-14 19:03:58 +000036 fRadius = rad;
37 fStyle = bs;
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000038 fFlags = flags;
reed@google.com25df8882011-07-14 19:03:58 +000039 const char* name = rad > 0 ? gStyleName[bs] : "none";
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000040 const char* quality = flags & SkBlurMaskFilter::kHighQuality_BlurFlag ? "high_quality" : "low_quality";
tomhudson@google.com8caac642011-11-22 15:58:06 +000041 if (SkScalarFraction(rad) != 0) {
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000042 fName.printf("blur_%.2f_%s_%s", SkScalarToFloat(rad), name, quality);
tomhudson@google.com8caac642011-11-22 15:58:06 +000043 } else {
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000044 fName.printf("blur_%d_%s_%s", SkScalarRound(rad), name, quality);
tomhudson@google.com8caac642011-11-22 15:58:06 +000045 }
reed@google.com25df8882011-07-14 19:03:58 +000046 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047
reed@google.com25df8882011-07-14 19:03:58 +000048protected:
49 virtual const char* onGetName() {
50 return fName.c_str();
51 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052
reed@google.com25df8882011-07-14 19:03:58 +000053 virtual void onDraw(SkCanvas* canvas) {
54 SkPaint paint;
55 this->setupPaint(&paint);
56
57 paint.setAntiAlias(true);
58
59 SkRandom rand;
tomhudson@google.comca529d32011-10-28 15:34:49 +000060 for (int i = 0; i < SkBENCHLOOP(10); i++) {
reed@google.com25df8882011-07-14 19:03:58 +000061 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
62 rand.nextUScalar1() * 400);
63 r.offset(fRadius, fRadius);
64
65 if (fRadius > 0) {
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000066 SkMaskFilter* mf = SkBlurMaskFilter::Create(fRadius, fStyle, fFlags);
reed@google.com25df8882011-07-14 19:03:58 +000067 paint.setMaskFilter(mf)->unref();
68 }
69 canvas->drawOval(r, paint);
70 }
71 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072
reed@google.com25df8882011-07-14 19:03:58 +000073private:
74 typedef SkBenchmark INHERITED;
75};
76
reed@google.comfc5100a2012-11-08 20:51:57 +000077DEF_BENCH(return new BlurBench(p, SMALL, SkBlurMaskFilter::kNormal_BlurStyle);)
78DEF_BENCH(return new BlurBench(p, SMALL, SkBlurMaskFilter::kSolid_BlurStyle);)
79DEF_BENCH(return new BlurBench(p, SMALL, SkBlurMaskFilter::kOuter_BlurStyle);)
80DEF_BENCH(return new BlurBench(p, SMALL, SkBlurMaskFilter::kInner_BlurStyle);)
reed@google.com25df8882011-07-14 19:03:58 +000081
reed@google.comfc5100a2012-11-08 20:51:57 +000082DEF_BENCH(return new BlurBench(p, BIG, SkBlurMaskFilter::kNormal_BlurStyle);)
83DEF_BENCH(return new BlurBench(p, BIG, SkBlurMaskFilter::kSolid_BlurStyle);)
84DEF_BENCH(return new BlurBench(p, BIG, SkBlurMaskFilter::kOuter_BlurStyle);)
85DEF_BENCH(return new BlurBench(p, BIG, SkBlurMaskFilter::kInner_BlurStyle);)
reed@google.com25df8882011-07-14 19:03:58 +000086
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000087DEF_BENCH(return new BlurBench(p, REALBIG, SkBlurMaskFilter::kNormal_BlurStyle);)
88DEF_BENCH(return new BlurBench(p, REALBIG, SkBlurMaskFilter::kSolid_BlurStyle);)
89DEF_BENCH(return new BlurBench(p, REALBIG, SkBlurMaskFilter::kOuter_BlurStyle);)
90DEF_BENCH(return new BlurBench(p, REALBIG, SkBlurMaskFilter::kInner_BlurStyle);)
91
reed@google.comfc5100a2012-11-08 20:51:57 +000092DEF_BENCH(return new BlurBench(p, REAL, SkBlurMaskFilter::kNormal_BlurStyle);)
93DEF_BENCH(return new BlurBench(p, REAL, SkBlurMaskFilter::kSolid_BlurStyle);)
94DEF_BENCH(return new BlurBench(p, REAL, SkBlurMaskFilter::kOuter_BlurStyle);)
95DEF_BENCH(return new BlurBench(p, REAL, SkBlurMaskFilter::kInner_BlurStyle);)
tomhudson@google.com8caac642011-11-22 15:58:06 +000096
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000097DEF_BENCH(return new BlurBench(p, SMALL, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
98
99DEF_BENCH(return new BlurBench(p, BIG, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
100
101DEF_BENCH(return new BlurBench(p, REALBIG, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
102
103DEF_BENCH(return new BlurBench(p, REAL, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
104
reed@google.comfc5100a2012-11-08 20:51:57 +0000105DEF_BENCH(return new BlurBench(p, 0, SkBlurMaskFilter::kNormal_BlurStyle);)