2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBlurMaskFilter.h"
11 #include "SkLayerDrawLooper.h"
15 // This bench replicates a problematic use case of a draw looper used
16 // to create an inner blurred rect
17 class RectoriBench : public Benchmark {
23 const char* onGetName() override {
27 void onDraw(const int loops, SkCanvas* canvas) override {
30 for (int i = 0; i < loops; i++) {
31 SkScalar blurSigma = Random.nextRangeScalar(1.5f, 25.0f);
32 SkScalar size = Random.nextRangeScalar(20*blurSigma, 50*blurSigma);
34 SkScalar x = Random.nextRangeScalar(0.0f, W - size);
35 SkScalar y = Random.nextRangeScalar(0.0f, H - size);
37 SkRect inner = { x, y, x + size, y + size };
40 // outer is always outset either 2x or 4x the blur radius (we go with 2x)
41 outer.outset(2*blurSigma, 2*blurSigma);
47 p.setFillType(SkPath::kEvenOdd_FillType);
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;
54 paint.setLooper(this->createLooper(-translate, blurSigma))->unref();
55 paint.setColor(0xff000000 | Random.nextU());
56 paint.setAntiAlias(true);
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);
73 SkLayerDrawLooper* createLooper(SkScalar xOff, SkScalar sigma) {
74 SkLayerDrawLooper::Builder looperBuilder;
76 //-----------------------------------------------
77 SkLayerDrawLooper::LayerInfo info;
79 // TODO: add a color filter to better match what is seen in the wild
80 info.fPaintBits = /* SkLayerDrawLooper::kColorFilter_Bit |*/
81 SkLayerDrawLooper::kMaskFilter_Bit;
82 info.fColorMode = SkXfermode::kDst_Mode;
83 info.fOffset.set(xOff, 0);
84 info.fPostTranslate = false;
86 SkPaint* paint = looperBuilder.addLayer(info);
88 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
90 SkBlurMaskFilter::kHighQuality_BlurFlag);
91 paint->setMaskFilter(mf)->unref();
93 //-----------------------------------------------
95 info.fOffset.set(0, 0);
97 paint = looperBuilder.addLayer(info);
98 return looperBuilder.detachLooper();
101 typedef Benchmark INHERITED;
104 DEF_BENCH( return SkNEW_ARGS(RectoriBench, ()); )