blob: ad3d4c29ea807bb53bb66389ded96f576883d356 [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
9#include "SkBlurMaskFilter.h"
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000010#include "SkCanvas.h"
tfarinaf168b862014-06-19 12:32:29 -070011#include "SkLayerDrawLooper.h"
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000012#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPath.h"
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000014#include "SkRandom.h"
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000015
16// This bench replicates a problematic use case of a draw looper used
17// to create an inner blurred rect
tfarinaf168b862014-06-19 12:32:29 -070018class RectoriBench : public Benchmark {
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000019public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000020 RectoriBench() {}
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000021
22protected:
23
mtklein36352bf2015-03-25 18:17:31 -070024 const char* onGetName() override {
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000025 return "rectori";
26 }
27
mtkleina1ebeb22015-10-01 09:43:39 -070028 void onDraw(int loops, SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000029 SkRandom Random;
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000030
commit-bot@chromium.org33614712013-12-03 18:17:16 +000031 for (int i = 0; i < loops; i++) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000032 SkScalar blurSigma = Random.nextRangeScalar(1.5f, 25.0f);
33 SkScalar size = Random.nextRangeScalar(20*blurSigma, 50*blurSigma);
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000034
35 SkScalar x = Random.nextRangeScalar(0.0f, W - size);
36 SkScalar y = Random.nextRangeScalar(0.0f, H - size);
37
38 SkRect inner = { x, y, x + size, y + size };
39
40 SkRect outer(inner);
41 // outer is always outset either 2x or 4x the blur radius (we go with 2x)
robertphillips@google.comb7061172013-09-06 14:16:12 +000042 outer.outset(2*blurSigma, 2*blurSigma);
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000043
44 SkPath p;
45
46 p.addRect(outer);
47 p.addRect(inner);
48 p.setFillType(SkPath::kEvenOdd_FillType);
49
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;
53
54 SkPaint paint;
reed7b380d02016-03-21 13:25:16 -070055 paint.setLooper(this->createLooper(-translate, blurSigma));
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000056 paint.setColor(0xff000000 | Random.nextU());
57 paint.setAntiAlias(true);
58
59 canvas->save();
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);
64 canvas->restore();
65 }
66 }
67
68private:
69 enum {
70 W = 640,
71 H = 480,
72 };
73
reed7b380d02016-03-21 13:25:16 -070074 sk_sp<SkDrawLooper> createLooper(SkScalar xOff, SkScalar sigma) {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000075 SkLayerDrawLooper::Builder looperBuilder;
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000076
77 //-----------------------------------------------
78 SkLayerDrawLooper::LayerInfo info;
79
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000080 // TODO: add a color filter to better match what is seen in the wild
skia.committer@gmail.com0431b872013-05-14 07:01:11 +000081 info.fPaintBits = /* SkLayerDrawLooper::kColorFilter_Bit |*/
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000082 SkLayerDrawLooper::kMaskFilter_Bit;
83 info.fColorMode = SkXfermode::kDst_Mode;
84 info.fOffset.set(xOff, 0);
85 info.fPostTranslate = false;
86
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000087 SkPaint* paint = looperBuilder.addLayer(info);
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000088
reedefdfd512016-04-04 10:02:58 -070089 paint->setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma,
90 SkBlurMaskFilter::kHighQuality_BlurFlag));
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000091
92 //-----------------------------------------------
93 info.fPaintBits = 0;
94 info.fOffset.set(0, 0);
95
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000096 paint = looperBuilder.addLayer(info);
reed7b380d02016-03-21 13:25:16 -070097 return looperBuilder.detach();
robertphillips@google.com1d2f6312013-05-13 14:10:31 +000098 }
99
tfarinaf168b862014-06-19 12:32:29 -0700100 typedef Benchmark INHERITED;
robertphillips@google.com1d2f6312013-05-13 14:10:31 +0000101};
102
halcanary385fe4d2015-08-26 13:07:48 -0700103DEF_BENCH(return new RectoriBench();)