blob: 883642ccf1216dc23976fc0132aff91fe19656c5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com311c82d2009-05-05 23:13:23 +00008#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
reed@android.com311c82d2009-05-05 23:13:23 +000011#include "SkView.h"
12
13///////////////////////////////////////////////////////////////////////////////
14
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000015class LayerMaskView : public SampleView {
reed@android.com311c82d2009-05-05 23:13:23 +000016public:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000017 LayerMaskView() {
18 this->setBGColor(0xFFDDDDDD);
19 }
reed@android.com311c82d2009-05-05 23:13:23 +000020
21protected:
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.com0baf1932009-06-24 12:41:42 +000047 paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
reed@android.com311c82d2009-05-05 23:13:23 +000048 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.com0baf1932009-06-24 12:41:42 +000053 paint.setXfermodeMode(SkXfermode::kDstOut_Mode);
reed@android.com311c82d2009-05-05 23:13:23 +000054 canvas->drawPath(p, paint);
55 }
56 }
57
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000058 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com311c82d2009-05-05 23:13:23 +000059 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
67private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000068 typedef SampleView INHERITED;
reed@android.com311c82d2009-05-05 23:13:23 +000069};
70
71///////////////////////////////////////////////////////////////////////////////
72
73static SkView* MyFactory() { return new LayerMaskView; }
74static SkViewRegister reg(MyFactory);
75