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