1/*
2* Copyright 2013 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
8#include "Benchmark.h"
9#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
11#include "SkCanvas.h"
12#include "SkColorFilter.h"
13#include "SkLayerDrawLooper.h"
14#include "SkPaint.h"
15#include "SkPath.h"
16#include "SkPoint.h"
17#include "SkRRect.h"
18#include "SkRect.h"
19#include "SkString.h"
20
21// Large blurred RR appear frequently on web pages. This benchmark measures our
22// performance in this case.
23class BlurRoundRectBench : public Benchmark {
24public:
25    BlurRoundRectBench(int width, int height, int cornerRadius)
26        : fName("blurroundrect") {
27        fName.appendf("_WH_%ix%i_cr_%i", width, height, cornerRadius);
28        SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
29        fRRect.setRectXY(r, SkIntToScalar(cornerRadius), SkIntToScalar(cornerRadius));
30    }
31
32    const char* onGetName() override {
33        return fName.c_str();
34    }
35
36    SkIPoint onGetSize() override {
37        return SkIPoint::Make(SkScalarCeilToInt(fRRect.rect().width()),
38                              SkScalarCeilToInt(fRRect.rect().height()));
39    }
40
41    void onDraw(int loops, SkCanvas* canvas) override {
42        SkLayerDrawLooper::Builder looperBuilder;
43        {
44            SkLayerDrawLooper::LayerInfo info;
45            info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
46                              | SkLayerDrawLooper::kColorFilter_Bit;
47            info.fColorMode = SkBlendMode::kSrc;
48            info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
49            info.fPostTranslate = false;
50            SkPaint* paint = looperBuilder.addLayerOnTop(info);
51            paint->setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
52                                                        SkBlurMask::ConvertRadiusToSigma(0.5),
53                                                        SkBlurMaskFilter::kHighQuality_BlurFlag));
54            paint->setColorFilter(SkColorFilter::MakeModeFilter(SK_ColorLTGRAY,
55                                                                SkBlendMode::kSrcIn));
56            paint->setColor(SK_ColorGRAY);
57        }
58        {
59            SkLayerDrawLooper::LayerInfo info;
60            looperBuilder.addLayerOnTop(info);
61        }
62        SkPaint dullPaint;
63        dullPaint.setAntiAlias(true);
64
65        SkPaint loopedPaint;
66        loopedPaint.setLooper(looperBuilder.detach());
67        loopedPaint.setAntiAlias(true);
68        loopedPaint.setColor(SK_ColorCYAN);
69
70        for (int i = 0; i < loops; i++) {
71            canvas->drawRect(fRRect.rect(), dullPaint);
72            canvas->drawRRect(fRRect, loopedPaint);
73        }
74    }
75
76private:
77    SkString    fName;
78    SkRRect     fRRect;
79
80    typedef     Benchmark INHERITED;
81};
82
83// Create one with dimensions/rounded corners based on the skp
84DEF_BENCH(return new BlurRoundRectBench(600, 5514, 6);)
85// Same radii, much smaller rectangle
86DEF_BENCH(return new BlurRoundRectBench(100, 100, 6);)
87// Other radii options
88DEF_BENCH(return new BlurRoundRectBench(100, 100, 30);)
89DEF_BENCH(return new BlurRoundRectBench(100, 100, 90);)
90