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