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"
16 // This bench replicates a problematic use case of a draw looper used
17 // to create an inner blurred rect
18 class RectoriBench : public Benchmark {
24 const char* onGetName() override {
28 void onDraw(int loops, SkCanvas* canvas) override {
31 for (int i = 0; i < loops; i++) {
32 SkScalar blurSigma = Random.nextRangeScalar(1.5f, 25.0f);
33 SkScalar size = Random.nextRangeScalar(20*blurSigma, 50*blurSigma);
35 SkScalar x = Random.nextRangeScalar(0.0f, W - size);
36 SkScalar y = Random.nextRangeScalar(0.0f, H - size);
38 SkRect inner = { x, y, x + size, y + size };
41 // outer is always outset either 2x or 4x the blur radius (we go with 2x)
42 outer.outset(2*blurSigma, 2*blurSigma);
48 p.setFillType(SkPath::kEvenOdd_FillType);
50 // This will be used to translate the normal draw outside the
51 // clip rect and translate the blurred version back inside
52 SkScalar translate = 2.0f * size;
55 paint.setLooper(this->createLooper(-translate, blurSigma))->unref();
56 paint.setColor(0xff000000 | Random.nextU());
57 paint.setAntiAlias(true);
60 // clip always equals inner rect so we get the inside blur
61 canvas->clipRect(inner);
62 canvas->translate(translate, 0);
63 canvas->drawPath(p, paint);
74 SkLayerDrawLooper* createLooper(SkScalar xOff, SkScalar sigma) {
75 SkLayerDrawLooper::Builder looperBuilder;
77 //-----------------------------------------------
78 SkLayerDrawLooper::LayerInfo info;
80 // TODO: add a color filter to better match what is seen in the wild
81 info.fPaintBits = /* SkLayerDrawLooper::kColorFilter_Bit |*/
82 SkLayerDrawLooper::kMaskFilter_Bit;
83 info.fColorMode = SkXfermode::kDst_Mode;
84 info.fOffset.set(xOff, 0);
85 info.fPostTranslate = false;
87 SkPaint* paint = looperBuilder.addLayer(info);
89 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
91 SkBlurMaskFilter::kHighQuality_BlurFlag);
92 paint->setMaskFilter(mf)->unref();
94 //-----------------------------------------------
96 info.fOffset.set(0, 0);
98 paint = looperBuilder.addLayer(info);
99 return looperBuilder.detachLooper();
102 typedef Benchmark INHERITED;
105 DEF_BENCH(return new RectoriBench();)