epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 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" |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 11 | #include "SkView.h" |
| 12 | |
| 13 | /////////////////////////////////////////////////////////////////////////////// |
| 14 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 15 | class LayerMaskView : public SampleView { |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 16 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 17 | LayerMaskView() { |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 18 | this->setBGColor(0xFFDDDDDD); |
| 19 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 20 | |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 21 | protected: |
| 22 | // overrides from SkEventSink |
| 23 | virtual bool onQuery(SkEvent* evt) { |
| 24 | if (SampleCode::TitleQ(*evt)) { |
| 25 | SampleCode::TitleR(evt, "LayerMask"); |
| 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@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 46 | paint.setXfermodeMode(SkXfermode::kDstIn_Mode); |
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@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 52 | paint.setXfermodeMode(SkXfermode::kDstOut_Mode); |
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)); |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 60 | canvas->saveLayer(&r, NULL); |
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: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 67 | typedef SampleView INHERITED; |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | static SkView* MyFactory() { return new LayerMaskView; } |
| 73 | static SkViewRegister reg(MyFactory); |