blob: b13ef31f307a49821658d53ec3c4cc0508f6816b [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.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
14///////////////////////////////////////////////////////////////////////////////
15
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040016class LayerMaskView : public Sample {
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:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040023 virtual bool onQuery(Sample::Event* evt) {
24 if (Sample::TitleQ(*evt)) {
25 Sample::TitleR(evt, "LayerMask");
reed@android.com311c82d2009-05-05 23:13:23 +000026 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
reed374772b2016-10-05 17:33:02 -070046 paint.setBlendMode(SkBlendMode::kDstIn);
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);
reed374772b2016-10-05 17:33:02 -070052 paint.setBlendMode(SkBlendMode::kDstOut);
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));
halcanary96fcdcc2015-08-27 07:41:13 -070060 canvas->saveLayer(&r, nullptr);
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:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040067 typedef Sample INHERITED;
reed@android.com311c82d2009-05-05 23:13:23 +000068};
69
70///////////////////////////////////////////////////////////////////////////////
71
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040072DEF_SAMPLE( return new LayerMaskView(); )