blob: cf956109746a290cc67257379f0b5c1d0856cdca [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)
19
20static const char* gStyleName[] = {
21 "normal",
22 "solid",
23 "outer",
24 "inner"
25};
26
27class BlurBench : public SkBenchmark {
28 SkScalar fRadius;
29 SkBlurMaskFilter::BlurStyle fStyle;
30 SkString fName;
31
32public:
33 BlurBench(void* param, SkScalar rad, SkBlurMaskFilter::BlurStyle bs) : INHERITED(param) {
34 fRadius = rad;
35 fStyle = bs;
36 const char* name = rad > 0 ? gStyleName[bs] : "none";
tomhudson@google.com8caac642011-11-22 15:58:06 +000037 if (SkScalarFraction(rad) != 0) {
38 fName.printf("blur_%.2f_%s", SkScalarToFloat(rad), name);
39 } else {
40 fName.printf("blur_%d_%s", SkScalarRound(rad), name);
41 }
reed@google.com25df8882011-07-14 19:03:58 +000042 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
reed@google.com25df8882011-07-14 19:03:58 +000044protected:
45 virtual const char* onGetName() {
46 return fName.c_str();
47 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048
reed@google.com25df8882011-07-14 19:03:58 +000049 virtual void onDraw(SkCanvas* canvas) {
50 SkPaint paint;
51 this->setupPaint(&paint);
52
53 paint.setAntiAlias(true);
54
55 SkRandom rand;
tomhudson@google.comca529d32011-10-28 15:34:49 +000056 for (int i = 0; i < SkBENCHLOOP(10); i++) {
reed@google.com25df8882011-07-14 19:03:58 +000057 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
58 rand.nextUScalar1() * 400);
59 r.offset(fRadius, fRadius);
60
61 if (fRadius > 0) {
62 SkMaskFilter* mf = SkBlurMaskFilter::Create(fRadius, fStyle, 0);
63 paint.setMaskFilter(mf)->unref();
64 }
65 canvas->drawOval(r, paint);
66 }
67 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
reed@google.com25df8882011-07-14 19:03:58 +000069private:
70 typedef SkBenchmark INHERITED;
71};
72
reed@google.comfc5100a2012-11-08 20:51:57 +000073DEF_BENCH(return new BlurBench(p, SMALL, SkBlurMaskFilter::kNormal_BlurStyle);)
74DEF_BENCH(return new BlurBench(p, SMALL, SkBlurMaskFilter::kSolid_BlurStyle);)
75DEF_BENCH(return new BlurBench(p, SMALL, SkBlurMaskFilter::kOuter_BlurStyle);)
76DEF_BENCH(return new BlurBench(p, SMALL, SkBlurMaskFilter::kInner_BlurStyle);)
reed@google.com25df8882011-07-14 19:03:58 +000077
reed@google.comfc5100a2012-11-08 20:51:57 +000078DEF_BENCH(return new BlurBench(p, BIG, SkBlurMaskFilter::kNormal_BlurStyle);)
79DEF_BENCH(return new BlurBench(p, BIG, SkBlurMaskFilter::kSolid_BlurStyle);)
80DEF_BENCH(return new BlurBench(p, BIG, SkBlurMaskFilter::kOuter_BlurStyle);)
81DEF_BENCH(return new BlurBench(p, BIG, SkBlurMaskFilter::kInner_BlurStyle);)
reed@google.com25df8882011-07-14 19:03:58 +000082
reed@google.comfc5100a2012-11-08 20:51:57 +000083DEF_BENCH(return new BlurBench(p, REAL, SkBlurMaskFilter::kNormal_BlurStyle);)
84DEF_BENCH(return new BlurBench(p, REAL, SkBlurMaskFilter::kSolid_BlurStyle);)
85DEF_BENCH(return new BlurBench(p, REAL, SkBlurMaskFilter::kOuter_BlurStyle);)
86DEF_BENCH(return new BlurBench(p, REAL, SkBlurMaskFilter::kInner_BlurStyle);)
tomhudson@google.com8caac642011-11-22 15:58:06 +000087
reed@google.comfc5100a2012-11-08 20:51:57 +000088DEF_BENCH(return new BlurBench(p, 0, SkBlurMaskFilter::kNormal_BlurStyle);)
reed@google.com25df8882011-07-14 19:03:58 +000089