blob: b03942199b0f2d5946278fd38641aef89e606869 [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"
Florin Malitaab244f02017-05-03 19:16:58 +00009#include "SkBitmap.h"
reed@android.com311c82d2009-05-05 23:13:23 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkPath.h"
reed@android.com311c82d2009-05-05 23:13:23 +000013#include "SkView.h"
14
15///////////////////////////////////////////////////////////////////////////////
16
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000017class LayerMaskView : public SampleView {
reed@android.com311c82d2009-05-05 23:13:23 +000018public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000019 LayerMaskView() {
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000020 this->setBGColor(0xFFDDDDDD);
21 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000022
reed@android.com311c82d2009-05-05 23:13:23 +000023protected:
24 // overrides from SkEventSink
25 virtual bool onQuery(SkEvent* evt) {
26 if (SampleCode::TitleQ(*evt)) {
27 SampleCode::TitleR(evt, "LayerMask");
28 return true;
29 }
30 return this->INHERITED::onQuery(evt);
31 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000032
reed@android.com311c82d2009-05-05 23:13:23 +000033 void drawMask(SkCanvas* canvas, const SkRect& r) {
34 SkPaint paint;
35 paint.setAntiAlias(true);
36
37 if (true) {
38 SkBitmap mask;
reed@google.come1ca7052013-12-17 19:22:07 +000039 int w = SkScalarRoundToInt(r.width());
40 int h = SkScalarRoundToInt(r.height());
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000041 mask.allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000042 mask.eraseColor(SK_ColorTRANSPARENT);
reed@android.com311c82d2009-05-05 23:13:23 +000043 SkCanvas c(mask);
44 SkRect bounds = r;
45 bounds.offset(-bounds.fLeft, -bounds.fTop);
46 c.drawOval(bounds, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed374772b2016-10-05 17:33:02 -070048 paint.setBlendMode(SkBlendMode::kDstIn);
reed@android.com311c82d2009-05-05 23:13:23 +000049 canvas->drawBitmap(mask, r.fLeft, r.fTop, &paint);
50 } else {
51 SkPath p;
52 p.addOval(r);
53 p.setFillType(SkPath::kInverseWinding_FillType);
reed374772b2016-10-05 17:33:02 -070054 paint.setBlendMode(SkBlendMode::kDstOut);
reed@android.com311c82d2009-05-05 23:13:23 +000055 canvas->drawPath(p, paint);
56 }
57 }
58
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000059 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com311c82d2009-05-05 23:13:23 +000060 SkRect r;
61 r.set(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(120), SkIntToScalar(120));
halcanary96fcdcc2015-08-27 07:41:13 -070062 canvas->saveLayer(&r, nullptr);
reed@android.com311c82d2009-05-05 23:13:23 +000063 canvas->drawColor(SK_ColorRED);
64 drawMask(canvas, r);
65 canvas->restore();
66 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@android.com311c82d2009-05-05 23:13:23 +000068private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000069 typedef SampleView INHERITED;
reed@android.com311c82d2009-05-05 23:13:23 +000070};
71
72///////////////////////////////////////////////////////////////////////////////
73
74static SkView* MyFactory() { return new LayerMaskView; }
75static SkViewRegister reg(MyFactory);