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