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 */ 7 8 #include "SampleCode.h" 9 #include "SkBitmap.h" 10 #include "SkCanvas.h" 11 #include "SkPaint.h" 12 #include "SkPath.h" 13 #include "SkView.h" 14 15 /////////////////////////////////////////////////////////////////////////////// 16 17 class LayerMaskView : public SampleView { 18 public: 19 LayerMaskView() { 20 this->setBGColor(0xFFDDDDDD); 21 } 22 23 protected: 24 // overrides from SkEventSink 25 virtual bool onQuery(SkEvent* evt) { 26 if (SampleCode::TitleQ(*evt)) { 27 SampleCode::TitleR(evt, "LayerMask"); 28 return true; 29 } 30 return this->INHERITED::onQuery(evt); 31 } 32 33 void drawMask(SkCanvas* canvas, const SkRect& r) { 34 SkPaint paint; 35 paint.setAntiAlias(true); 36 37 if (true) { 38 SkBitmap mask; 39 int w = SkScalarRoundToInt(r.width()); 40 int h = SkScalarRoundToInt(r.height()); 41 mask.allocN32Pixels(w, h); 42 mask.eraseColor(SK_ColorTRANSPARENT); 43 SkCanvas c(mask); 44 SkRect bounds = r; 45 bounds.offset(-bounds.fLeft, -bounds.fTop); 46 c.drawOval(bounds, paint); 47 48 paint.setBlendMode(SkBlendMode::kDstIn); 49 canvas->drawBitmap(mask, r.fLeft, r.fTop, &paint); 50 } else { 51 SkPath p; 52 p.addOval(r); 53 p.setFillType(SkPath::kInverseWinding_FillType); 54 paint.setBlendMode(SkBlendMode::kDstOut); 55 canvas->drawPath(p, paint); 56 } 57 } 58 59 virtual void onDrawContent(SkCanvas* canvas) { 60 SkRect r; 61 r.set(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(120), SkIntToScalar(120)); 62 canvas->saveLayer(&r, nullptr); 63 canvas->drawColor(SK_ColorRED); 64 drawMask(canvas, r); 65 canvas->restore(); 66 } 67 68 private: 69 typedef SampleView INHERITED; 70 }; 71 72 /////////////////////////////////////////////////////////////////////////////// 73 74 static SkView* MyFactory() { return new LayerMaskView; } 75 static SkViewRegister reg(MyFactory); 76