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 | */ |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 7 | |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkPaint.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkPath.h" |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 13 | #include "SkView.h" |
| 14 | |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 17 | class LayerMaskView : public SampleView { |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 18 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 19 | LayerMaskView() { |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 20 | this->setBGColor(0xFFDDDDDD); |
| 21 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 22 | |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 23 | protected: |
| 24 | // overrides from SkEventSink |
| 25 | virtual bool onQuery(SkEvent* evt) { |
| 26 | if (SampleCode::TitleQ(*evt)) { |
| 27 | SampleCode::TitleR(evt, "LayerMask"); |
| 28 | return true; |
| 29 | } |
| 30 | return this->INHERITED::onQuery(evt); |
| 31 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 32 | |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 33 | void drawMask(SkCanvas* canvas, const SkRect& r) { |
| 34 | SkPaint paint; |
| 35 | paint.setAntiAlias(true); |
| 36 | |
| 37 | if (true) { |
| 38 | SkBitmap mask; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 39 | int w = SkScalarRoundToInt(r.width()); |
| 40 | int h = SkScalarRoundToInt(r.height()); |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 41 | mask.allocN32Pixels(w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 42 | mask.eraseColor(SK_ColorTRANSPARENT); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 43 | SkCanvas c(mask); |
| 44 | SkRect bounds = r; |
| 45 | bounds.offset(-bounds.fLeft, -bounds.fTop); |
| 46 | c.drawOval(bounds, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 47 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 48 | paint.setBlendMode(SkBlendMode::kDstIn); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 49 | canvas->drawBitmap(mask, r.fLeft, r.fTop, &paint); |
| 50 | } else { |
| 51 | SkPath p; |
| 52 | p.addOval(r); |
| 53 | p.setFillType(SkPath::kInverseWinding_FillType); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 54 | paint.setBlendMode(SkBlendMode::kDstOut); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 55 | canvas->drawPath(p, paint); |
| 56 | } |
| 57 | } |
| 58 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 59 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 60 | SkRect r; |
| 61 | r.set(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(120), SkIntToScalar(120)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | canvas->saveLayer(&r, nullptr); |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 63 | canvas->drawColor(SK_ColorRED); |
| 64 | drawMask(canvas, r); |
| 65 | canvas->restore(); |
| 66 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 68 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 69 | typedef SampleView INHERITED; |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | static SkView* MyFactory() { return new LayerMaskView; } |
| 75 | static SkViewRegister reg(MyFactory); |