robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
| 9 | #include "SkBlurMaskFilter.h" |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 11 | #include "SkLayerDrawLooper.h" |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 12 | #include "SkPaint.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 13 | #include "SkPath.h" |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 14 | #include "SkRandom.h" |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 15 | |
| 16 | // This bench replicates a problematic use case of a draw looper used |
| 17 | // to create an inner blurred rect |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 18 | class RectoriBench : public Benchmark { |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 19 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 20 | RectoriBench() {} |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 21 | |
| 22 | protected: |
| 23 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 24 | const char* onGetName() override { |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 25 | return "rectori"; |
| 26 | } |
| 27 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 28 | void onDraw(int loops, SkCanvas* canvas) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 29 | SkRandom Random; |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 30 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 31 | for (int i = 0; i < loops; i++) { |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 32 | SkScalar blurSigma = Random.nextRangeScalar(1.5f, 25.0f); |
| 33 | SkScalar size = Random.nextRangeScalar(20*blurSigma, 50*blurSigma); |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 34 | |
| 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.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 42 | outer.outset(2*blurSigma, 2*blurSigma); |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 43 | |
| 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; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 55 | paint.setLooper(this->createLooper(-translate, blurSigma)); |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 56 | 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 | |
| 68 | private: |
| 69 | enum { |
| 70 | W = 640, |
| 71 | H = 480, |
| 72 | }; |
| 73 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 74 | sk_sp<SkDrawLooper> createLooper(SkScalar xOff, SkScalar sigma) { |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 75 | SkLayerDrawLooper::Builder looperBuilder; |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 76 | |
| 77 | //----------------------------------------------- |
| 78 | SkLayerDrawLooper::LayerInfo info; |
| 79 | |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 80 | // TODO: add a color filter to better match what is seen in the wild |
skia.committer@gmail.com | 0431b87 | 2013-05-14 07:01:11 +0000 | [diff] [blame] | 81 | info.fPaintBits = /* SkLayerDrawLooper::kColorFilter_Bit |*/ |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 82 | SkLayerDrawLooper::kMaskFilter_Bit; |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 83 | info.fColorMode = SkBlendMode::kDst; |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 84 | info.fOffset.set(xOff, 0); |
| 85 | info.fPostTranslate = false; |
| 86 | |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 87 | SkPaint* paint = looperBuilder.addLayer(info); |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 88 | |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 89 | paint->setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma, |
| 90 | SkBlurMaskFilter::kHighQuality_BlurFlag)); |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 91 | |
| 92 | //----------------------------------------------- |
| 93 | info.fPaintBits = 0; |
| 94 | info.fOffset.set(0, 0); |
| 95 | |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 96 | paint = looperBuilder.addLayer(info); |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 97 | return looperBuilder.detach(); |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 98 | } |
| 99 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 100 | typedef Benchmark INHERITED; |
robertphillips@google.com | 1d2f631 | 2013-05-13 14:10:31 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 103 | DEF_BENCH(return new RectoriBench();) |