epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 7 | |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 10 | #include "SkBlurMaskFilter.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
| 13 | #include "SkGradientShader.h" |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 14 | #include "SkUtils.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 15 | #include "SkView.h" |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 16 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 17 | class BlurView : public SampleView { |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 18 | SkBitmap fBM; |
| 19 | public: |
Mike Reed | a920d36 | 2017-07-03 13:36:17 -0400 | [diff] [blame] | 20 | BlurView() {} |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 21 | |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 22 | protected: |
| 23 | // overrides from SkEventSink |
| 24 | virtual bool onQuery(SkEvent* evt) { |
| 25 | if (SampleCode::TitleQ(*evt)) { |
| 26 | SampleCode::TitleR(evt, "Blur"); |
| 27 | return true; |
| 28 | } |
| 29 | return this->INHERITED::onQuery(evt); |
| 30 | } |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 31 | |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 32 | void drawBG(SkCanvas* canvas) { |
| 33 | canvas->drawColor(0xFFDDDDDD); |
| 34 | } |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 35 | |
senorblanco@chromium.org | e36ddf0 | 2011-07-15 14:28:16 +0000 | [diff] [blame] | 36 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 37 | drawBG(canvas); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 38 | |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 39 | SkBlurStyle NONE = SkBlurStyle(-999); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 40 | static const struct { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 41 | SkBlurStyle fStyle; |
| 42 | int fCx, fCy; |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 43 | } gRecs[] = { |
| 44 | { NONE, 0, 0 }, |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 45 | { kInner_SkBlurStyle, -1, 0 }, |
| 46 | { kNormal_SkBlurStyle, 0, 1 }, |
| 47 | { kSolid_SkBlurStyle, 0, -1 }, |
| 48 | { kOuter_SkBlurStyle, 1, 0 }, |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 49 | }; |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 50 | |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 51 | SkPaint paint; |
| 52 | paint.setAntiAlias(true); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 53 | paint.setTextSize(25); |
| 54 | canvas->translate(-40, 0); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 55 | |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 56 | SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag; |
| 57 | for (int j = 0; j < 2; j++) { |
| 58 | canvas->save(); |
| 59 | paint.setColor(SK_ColorBLUE); |
| 60 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) { |
| 61 | if (gRecs[i].fStyle != NONE) { |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 62 | paint.setMaskFilter(SkBlurMaskFilter::Make(gRecs[i].fStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 63 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)), |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 64 | flags)); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 65 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | paint.setMaskFilter(nullptr); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 67 | } |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 68 | canvas->drawCircle(200 + gRecs[i].fCx*100.f, |
| 69 | 200 + gRecs[i].fCy*100.f, 50, paint); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 70 | } |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 71 | // draw text |
| 72 | { |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 73 | paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| 74 | SkBlurMask::ConvertRadiusToSigma(4), |
| 75 | flags)); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 76 | SkScalar x = SkIntToScalar(70); |
| 77 | SkScalar y = SkIntToScalar(400); |
| 78 | paint.setColor(SK_ColorBLACK); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 79 | canvas->drawString("Hamburgefons Style", x, y, paint); |
| 80 | canvas->drawString("Hamburgefons Style", x, y + SkIntToScalar(50), paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | paint.setMaskFilter(nullptr); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 82 | paint.setColor(SK_ColorWHITE); |
| 83 | x -= SkIntToScalar(2); |
| 84 | y -= SkIntToScalar(2); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 85 | canvas->drawString("Hamburgefons Style", x, y, paint); |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 86 | } |
| 87 | canvas->restore(); |
reed@google.com | 11a5ff3 | 2011-02-18 20:20:51 +0000 | [diff] [blame] | 88 | flags = SkBlurMaskFilter::kHighQuality_BlurFlag; |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 89 | canvas->translate(350, 0); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 90 | } |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 91 | } |
reed@google.com | 0ecc2b79 | 2011-02-16 13:06:15 +0000 | [diff] [blame] | 92 | |
reed@android.com | 47ca036 | 2009-09-02 02:07:45 +0000 | [diff] [blame] | 93 | private: |
| 94 | typedef SkView INHERITED; |
| 95 | }; |
| 96 | |
| 97 | ////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | static SkView* MyFactory() { return new BlurView; } |
| 100 | static SkViewRegister reg(MyFactory); |