blob: ee79ca9964500135b187d8a11e9bbc3d0809ce8c [file] [log] [blame]
robertphillips@google.com1d2f6312013-05-13 14:10:31 +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
8#include "SkBenchmark.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkBlurMaskFilter.h"
13#include "SkLayerDrawLooper.h"
14
15// This bench replicates a problematic use case of a draw looper used
16// to create an inner blurred rect
17class RectoriBench : public SkBenchmark {
18public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000019 RectoriBench() {}
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000020
21protected:
22
23 virtual const char* onGetName() {
24 return "rectori";
25 }
26
commit-bot@chromium.org33614712013-12-03 18:17:16 +000027 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000028 SkRandom Random;
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000029
commit-bot@chromium.org33614712013-12-03 18:17:16 +000030 for (int i = 0; i < loops; i++) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000031 SkScalar blurSigma = Random.nextRangeScalar(1.5f, 25.0f);
32 SkScalar size = Random.nextRangeScalar(20*blurSigma, 50*blurSigma);
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000033
34 SkScalar x = Random.nextRangeScalar(0.0f, W - size);
35 SkScalar y = Random.nextRangeScalar(0.0f, H - size);
36
37 SkRect inner = { x, y, x + size, y + size };
38
39 SkRect outer(inner);
40 // outer is always outset either 2x or 4x the blur radius (we go with 2x)
robertphillips@google.comb7061172013-09-06 14:16:12 +000041 outer.outset(2*blurSigma, 2*blurSigma);
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000042
43 SkPath p;
44
45 p.addRect(outer);
46 p.addRect(inner);
47 p.setFillType(SkPath::kEvenOdd_FillType);
48
49 // This will be used to translate the normal draw outside the
50 // clip rect and translate the blurred version back inside
51 SkScalar translate = 2.0f * size;
52
53 SkPaint paint;
robertphillips@google.comb7061172013-09-06 14:16:12 +000054 paint.setLooper(this->createLooper(-translate, blurSigma))->unref();
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000055 paint.setColor(0xff000000 | Random.nextU());
56 paint.setAntiAlias(true);
57
58 canvas->save();
59 // clip always equals inner rect so we get the inside blur
60 canvas->clipRect(inner);
61 canvas->translate(translate, 0);
62 canvas->drawPath(p, paint);
63 canvas->restore();
64 }
65 }
66
67private:
68 enum {
69 W = 640,
70 H = 480,
71 };
72
robertphillips@google.comb7061172013-09-06 14:16:12 +000073 SkLayerDrawLooper* createLooper(SkScalar xOff, SkScalar sigma) {
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000074 SkLayerDrawLooper* looper = new SkLayerDrawLooper;
75
76 //-----------------------------------------------
77 SkLayerDrawLooper::LayerInfo info;
78
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000079 // TODO: add a color filter to better match what is seen in the wild
skia.committer@gmail.com0431b872013-05-14 07:01:11 +000080 info.fPaintBits = /* SkLayerDrawLooper::kColorFilter_Bit |*/
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000081 SkLayerDrawLooper::kMaskFilter_Bit;
82 info.fColorMode = SkXfermode::kDst_Mode;
83 info.fOffset.set(xOff, 0);
84 info.fPostTranslate = false;
85
86 SkPaint* paint = looper->addLayer(info);
87
robertphillips@google.comb7061172013-09-06 14:16:12 +000088 SkMaskFilter* mf = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
89 sigma,
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000090 SkBlurMaskFilter::kHighQuality_BlurFlag);
91 paint->setMaskFilter(mf)->unref();
92
93 //-----------------------------------------------
94 info.fPaintBits = 0;
95 info.fOffset.set(0, 0);
96
97 paint = looper->addLayer(info);
98 return looper;
99 }
100
101 typedef SkBenchmark INHERITED;
102};
103
mtklein@google.com410e6e82013-09-13 19:52:27 +0000104DEF_BENCH( return SkNEW_ARGS(RectoriBench, ()); )