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