blob: 77fbc49b1176c24b53969323086077fa9102c087 [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"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
reed@google.com25df8882011-07-14 19:03:58 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkShader.h"
14#include "SkString.h"
15#include "SkBlurMaskFilter.h"
16
17#define SMALL SkIntToScalar(2)
tomhudson@google.com8caac642011-11-22 15:58:06 +000018#define REAL SkFloatToScalar(1.5f)
reed@google.com25df8882011-07-14 19:03:58 +000019#define BIG SkIntToScalar(10)
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000020#define REALBIG SkFloatToScalar(100.5f)
reed@google.com25df8882011-07-14 19:03:58 +000021
22static const char* gStyleName[] = {
23 "normal",
24 "solid",
25 "outer",
26 "inner"
27};
28
29class BlurBench : public SkBenchmark {
30 SkScalar fRadius;
31 SkBlurMaskFilter::BlurStyle fStyle;
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000032 uint32_t fFlags;
reed@google.com25df8882011-07-14 19:03:58 +000033 SkString fName;
34
35public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000036 BlurBench(SkScalar rad, SkBlurMaskFilter::BlurStyle bs, uint32_t flags = 0) {
reed@google.com25df8882011-07-14 19:03:58 +000037 fRadius = rad;
38 fStyle = bs;
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000039 fFlags = flags;
reed@google.com25df8882011-07-14 19:03:58 +000040 const char* name = rad > 0 ? gStyleName[bs] : "none";
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000041 const char* quality = flags & SkBlurMaskFilter::kHighQuality_BlurFlag ? "high_quality"
robertphillips@google.comb7061172013-09-06 14:16:12 +000042 : "low_quality";
tomhudson@google.com8caac642011-11-22 15:58:06 +000043 if (SkScalarFraction(rad) != 0) {
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000044 fName.printf("blur_%.2f_%s_%s", SkScalarToFloat(rad), name, quality);
tomhudson@google.com8caac642011-11-22 15:58:06 +000045 } else {
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000046 fName.printf("blur_%d_%s_%s", SkScalarRound(rad), name, quality);
tomhudson@google.com8caac642011-11-22 15:58:06 +000047 }
reed@google.com25df8882011-07-14 19:03:58 +000048 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000049
reed@google.com25df8882011-07-14 19:03:58 +000050protected:
51 virtual const char* onGetName() {
52 return fName.c_str();
53 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000054
reed@google.com25df8882011-07-14 19:03:58 +000055 virtual void onDraw(SkCanvas* canvas) {
56 SkPaint paint;
57 this->setupPaint(&paint);
58
59 paint.setAntiAlias(true);
60
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000061 SkRandom rand;
mtklein@google.comc2897432013-09-10 19:23:38 +000062 for (int i = 0; i < this->getLoops(); i++) {
reed@google.com25df8882011-07-14 19:03:58 +000063 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
64 rand.nextUScalar1() * 400);
65 r.offset(fRadius, fRadius);
66
67 if (fRadius > 0) {
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000068 SkMaskFilter* mf = SkBlurMaskFilter::Create(fStyle,
69 SkBlurMask::ConvertRadiusToSigma(fRadius),
robertphillips@google.comb7061172013-09-06 14:16:12 +000070 fFlags);
reed@google.com25df8882011-07-14 19:03:58 +000071 paint.setMaskFilter(mf)->unref();
72 }
73 canvas->drawOval(r, paint);
74 }
75 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000076
reed@google.com25df8882011-07-14 19:03:58 +000077private:
78 typedef SkBenchmark INHERITED;
79};
80
mtklein@google.com410e6e82013-09-13 19:52:27 +000081DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kNormal_BlurStyle);)
82DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kSolid_BlurStyle);)
83DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kOuter_BlurStyle);)
84DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kInner_BlurStyle);)
reed@google.com25df8882011-07-14 19:03:58 +000085
mtklein@google.com410e6e82013-09-13 19:52:27 +000086DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kNormal_BlurStyle);)
87DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kSolid_BlurStyle);)
88DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kOuter_BlurStyle);)
89DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kInner_BlurStyle);)
reed@google.com25df8882011-07-14 19:03:58 +000090
mtklein@google.com410e6e82013-09-13 19:52:27 +000091DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kNormal_BlurStyle);)
92DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kSolid_BlurStyle);)
93DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kOuter_BlurStyle);)
94DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kInner_BlurStyle);)
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +000095
mtklein@google.com410e6e82013-09-13 19:52:27 +000096DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kNormal_BlurStyle);)
97DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kSolid_BlurStyle);)
98DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kOuter_BlurStyle);)
99DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kInner_BlurStyle);)
tomhudson@google.com8caac642011-11-22 15:58:06 +0000100
mtklein@google.com410e6e82013-09-13 19:52:27 +0000101DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +0000102
mtklein@google.com410e6e82013-09-13 19:52:27 +0000103DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +0000104
mtklein@google.com410e6e82013-09-13 19:52:27 +0000105DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +0000106
mtklein@google.com410e6e82013-09-13 19:52:27 +0000107DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
senorblanco@chromium.org91f489a2012-11-29 17:09:27 +0000108
mtklein@google.com410e6e82013-09-13 19:52:27 +0000109DEF_BENCH(return new BlurBench(0, SkBlurMaskFilter::kNormal_BlurStyle);)