blob: 5fce85c4b7d7675181ff41aa101870ea269cc148 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
bungemand3ebb482015-08-05 13:57:49 -07007
reed@android.com311c82d2009-05-05 23:13:23 +00008#include "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
reed@android.com311c82d2009-05-05 23:13:23 +000012#include "SkView.h"
13
14///////////////////////////////////////////////////////////////////////////////
15
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000016class LayerMaskView : public SampleView {
reed@android.com311c82d2009-05-05 23:13:23 +000017public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000018 LayerMaskView() {
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000019 this->setBGColor(0xFFDDDDDD);
20 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000021
reed@android.com311c82d2009-05-05 23:13:23 +000022protected:
23 // overrides from SkEventSink
24 virtual bool onQuery(SkEvent* evt) {
25 if (SampleCode::TitleQ(*evt)) {
26 SampleCode::TitleR(evt, "LayerMask");
27 return true;
28 }
29 return this->INHERITED::onQuery(evt);
30 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
reed@android.com311c82d2009-05-05 23:13:23 +000032 void drawMask(SkCanvas* canvas, const SkRect& r) {
33 SkPaint paint;
34 paint.setAntiAlias(true);
35
36 if (true) {
37 SkBitmap mask;
reed@google.come1ca7052013-12-17 19:22:07 +000038 int w = SkScalarRoundToInt(r.width());
39 int h = SkScalarRoundToInt(r.height());
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000040 mask.allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000041 mask.eraseColor(SK_ColorTRANSPARENT);
reed@android.com311c82d2009-05-05 23:13:23 +000042 SkCanvas c(mask);
43 SkRect bounds = r;
44 bounds.offset(-bounds.fLeft, -bounds.fTop);
45 c.drawOval(bounds, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
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));
halcanary96fcdcc2015-08-27 07:41:13 -070061 canvas->saveLayer(&r, nullptr);
reed@android.com311c82d2009-05-05 23:13:23 +000062 canvas->drawColor(SK_ColorRED);
63 drawMask(canvas, r);
64 canvas->restore();
65 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000066
reed@android.com311c82d2009-05-05 23:13:23 +000067private:
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);