blob: 86a092cd06ecce8969227d28e3ce4bd4d5a18838 [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:
rmistry@google.comae933ce2012-08-23 18:19:56 +000017 LayerMaskView() {
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000018 this->setBGColor(0xFFDDDDDD);
19 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000020
reed@android.com311c82d2009-05-05 23:13:23 +000021protected:
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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000030
reed@android.com311c82d2009-05-05 23:13:23 +000031 void drawMask(SkCanvas* canvas, const SkRect& r) {
32 SkPaint paint;
33 paint.setAntiAlias(true);
34
35 if (true) {
36 SkBitmap mask;
reed@google.come1ca7052013-12-17 19:22:07 +000037 int w = SkScalarRoundToInt(r.width());
38 int h = SkScalarRoundToInt(r.height());
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000039 mask.allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000040 mask.eraseColor(SK_ColorTRANSPARENT);
reed@android.com311c82d2009-05-05 23:13:23 +000041 SkCanvas c(mask);
42 SkRect bounds = r;
43 bounds.offset(-bounds.fLeft, -bounds.fTop);
44 c.drawOval(bounds, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000045
reed@android.com0baf1932009-06-24 12:41:42 +000046 paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
reed@android.com311c82d2009-05-05 23:13:23 +000047 canvas->drawBitmap(mask, r.fLeft, r.fTop, &paint);
48 } else {
49 SkPath p;
50 p.addOval(r);
51 p.setFillType(SkPath::kInverseWinding_FillType);
reed@android.com0baf1932009-06-24 12:41:42 +000052 paint.setXfermodeMode(SkXfermode::kDstOut_Mode);
reed@android.com311c82d2009-05-05 23:13:23 +000053 canvas->drawPath(p, paint);
54 }
55 }
56
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000057 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com311c82d2009-05-05 23:13:23 +000058 SkRect r;
59 r.set(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(120), SkIntToScalar(120));
commit-bot@chromium.org091a5942014-04-18 14:19:31 +000060 canvas->saveLayer(&r, NULL);
reed@android.com311c82d2009-05-05 23:13:23 +000061 canvas->drawColor(SK_ColorRED);
62 drawMask(canvas, r);
63 canvas->restore();
64 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000065
reed@android.com311c82d2009-05-05 23:13:23 +000066private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000067 typedef SampleView INHERITED;
reed@android.com311c82d2009-05-05 23:13:23 +000068};
69
70///////////////////////////////////////////////////////////////////////////////
71
72static SkView* MyFactory() { return new LayerMaskView; }
73static SkViewRegister reg(MyFactory);