• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Sample.h"
9 #include "SkBitmap.h"
10 #include "SkCanvas.h"
11 #include "SkPaint.h"
12 #include "SkPath.h"
13 
14 ///////////////////////////////////////////////////////////////////////////////
15 
16 class LayerMaskView : public Sample {
17 public:
LayerMaskView()18     LayerMaskView() {
19         this->setBGColor(0xFFDDDDDD);
20     }
21 
22 protected:
onQuery(Sample::Event * evt)23     virtual bool onQuery(Sample::Event* evt) {
24         if (Sample::TitleQ(*evt)) {
25             Sample::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.setBlendMode(SkBlendMode::kDstIn);
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.setBlendMode(SkBlendMode::kDstOut);
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, nullptr);
61         canvas->drawColor(SK_ColorRED);
62         drawMask(canvas, r);
63         canvas->restore();
64     }
65 
66 private:
67     typedef Sample INHERITED;
68 };
69 
70 ///////////////////////////////////////////////////////////////////////////////
71 
72 DEF_SAMPLE( return new LayerMaskView(); )
73