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