blob: 9f1e1438620fff62bec6ce5842564c6c4e3841eb [file] [log] [blame]
scroggo@google.com7b056592013-11-05 15:57:21 +00001/*
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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
scroggo@google.com7b056592013-11-05 15:57:21 +00009#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"
scroggo@google.com7b056592013-11-05 15:57:21 +000017#include "SkRRect.h"
tfarinaf168b862014-06-19 12:32:29 -070018#include "SkRect.h"
scroggo@google.com7b056592013-11-05 15:57:21 +000019#include "SkString.h"
20#include "SkXfermode.h"
21
scroggo@google.com8610d2c2013-11-05 21:10:58 +000022// Large blurred RR appear frequently on web pages. This benchmark measures our
23// performance in this case.
tfarinaf168b862014-06-19 12:32:29 -070024class BlurRoundRectBench : public Benchmark {
scroggo@google.com7b056592013-11-05 15:57:21 +000025public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000026 BlurRoundRectBench(int width, int height, int cornerRadius)
27 : fName("blurroundrect") {
28 fName.appendf("_WH[%ix%i]_cr[%i]", width, height, cornerRadius);
29 SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
30 fRRect.setRectXY(r, SkIntToScalar(cornerRadius), SkIntToScalar(cornerRadius));
scroggo@google.com7b056592013-11-05 15:57:21 +000031 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 const char* onGetName() override {
scroggo@google.com7b056592013-11-05 15:57:21 +000034 return fName.c_str();
35 }
36
mtklein36352bf2015-03-25 18:17:31 -070037 SkIPoint onGetSize() override {
scroggo@google.com8610d2c2013-11-05 21:10:58 +000038 return SkIPoint::Make(SkScalarCeilToInt(fRRect.rect().width()),
39 SkScalarCeilToInt(fRRect.rect().height()));
scroggo@google.com7b056592013-11-05 15:57:21 +000040 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 void onDraw(const int loops, SkCanvas* canvas) override {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000043 SkLayerDrawLooper::Builder looperBuilder;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000044 {
45 SkLayerDrawLooper::LayerInfo info;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000046 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
47 | SkLayerDrawLooper::kColorFilter_Bit;
48 info.fColorMode = SkXfermode::kSrc_Mode;
49 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
50 info.fPostTranslate = false;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000051 SkPaint* paint = looperBuilder.addLayerOnTop(info);
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000052 SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000053 kNormal_SkBlurStyle,
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000054 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf),
scroggo@google.com8610d2c2013-11-05 21:10:58 +000055 SkBlurMaskFilter::kHighQuality_BlurFlag);
56 paint->setMaskFilter(maskFilter)->unref();
57 SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(SK_ColorLTGRAY,
58 SkXfermode::kSrcIn_Mode);
59 paint->setColorFilter(colorFilter)->unref();
60 paint->setColor(SK_ColorGRAY);
61 }
62 {
63 SkLayerDrawLooper::LayerInfo info;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000064 looperBuilder.addLayerOnTop(info);
scroggo@google.com8610d2c2013-11-05 21:10:58 +000065 }
66 SkPaint dullPaint;
67 dullPaint.setAntiAlias(true);
68
69 SkPaint loopedPaint;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000070 loopedPaint.setLooper(looperBuilder.detachLooper())->unref();
scroggo@google.com8610d2c2013-11-05 21:10:58 +000071 loopedPaint.setAntiAlias(true);
72 loopedPaint.setColor(SK_ColorCYAN);
73
commit-bot@chromium.org33614712013-12-03 18:17:16 +000074 for (int i = 0; i < loops; i++) {
scroggo@google.com8610d2c2013-11-05 21:10:58 +000075 canvas->drawRect(fRRect.rect(), dullPaint);
76 canvas->drawRRect(fRRect, loopedPaint);
scroggo@google.com7b056592013-11-05 15:57:21 +000077 }
78 }
79
80private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000081 SkString fName;
82 SkRRect fRRect;
83
tfarinaf168b862014-06-19 12:32:29 -070084 typedef Benchmark INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +000085};
86
87// Create one with dimensions/rounded corners based on the skp
scroggo@google.com8610d2c2013-11-05 21:10:58 +000088DEF_BENCH(return new BlurRoundRectBench(600, 5514, 6);)
scroggo@google.com7b056592013-11-05 15:57:21 +000089// Same radii, much smaller rectangle
scroggo@google.com8610d2c2013-11-05 21:10:58 +000090DEF_BENCH(return new BlurRoundRectBench(100, 100, 6);)
91// Other radii options
92DEF_BENCH(return new BlurRoundRectBench(100, 100, 30);)
93DEF_BENCH(return new BlurRoundRectBench(100, 100, 90);)