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: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 17 | LayerMaskView() { |
| 18 | this->setBGColor(0xFFDDDDDD); |
| 19 | } |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 20 | |
| 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 | } |
| 30 | |
| 31 | void drawMask(SkCanvas* canvas, const SkRect& r) { |
| 32 | SkPaint paint; |
| 33 | paint.setAntiAlias(true); |
| 34 | |
| 35 | if (true) { |
| 36 | SkBitmap mask; |
| 37 | int w = SkScalarRound(r.width()); |
| 38 | int h = SkScalarRound(r.height()); |
| 39 | mask.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 40 | mask.allocPixels(); |
| 41 | mask.eraseColor(0); |
| 42 | SkCanvas c(mask); |
| 43 | SkRect bounds = r; |
| 44 | bounds.offset(-bounds.fLeft, -bounds.fTop); |
| 45 | c.drawOval(bounds, paint); |
| 46 | |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 47 | paint.setXfermodeMode(SkXfermode::kDstIn_Mode); |
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@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 53 | paint.setXfermodeMode(SkXfermode::kDstOut_Mode); |
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)); |
| 61 | canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag); |
| 62 | canvas->drawColor(SK_ColorRED); |
| 63 | drawMask(canvas, r); |
| 64 | canvas->restore(); |
| 65 | } |
| 66 | |
| 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); |
| 75 | |