blob: 845e4a14b45b78c196437039c0b21bcecb1f04a2 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkPaint.h"
11#include "include/core/SkPath.h"
12#include "samplecode/Sample.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:
Brian Salomond0072812020-07-21 17:03:56 -040023 SkString name() override { return SkString("LayerMask"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000024
reed@android.com311c82d2009-05-05 23:13:23 +000025 void drawMask(SkCanvas* canvas, const SkRect& r) {
26 SkPaint paint;
27 paint.setAntiAlias(true);
28
29 if (true) {
30 SkBitmap mask;
reed@google.come1ca7052013-12-17 19:22:07 +000031 int w = SkScalarRoundToInt(r.width());
32 int h = SkScalarRoundToInt(r.height());
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000033 mask.allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000034 mask.eraseColor(SK_ColorTRANSPARENT);
reed@android.com311c82d2009-05-05 23:13:23 +000035 SkCanvas c(mask);
36 SkRect bounds = r;
37 bounds.offset(-bounds.fLeft, -bounds.fTop);
38 c.drawOval(bounds, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000039
reed374772b2016-10-05 17:33:02 -070040 paint.setBlendMode(SkBlendMode::kDstIn);
reed@android.com311c82d2009-05-05 23:13:23 +000041 canvas->drawBitmap(mask, r.fLeft, r.fTop, &paint);
42 } else {
43 SkPath p;
44 p.addOval(r);
Mike Reed7d34dc72019-11-26 12:17:17 -050045 p.setFillType(SkPathFillType::kInverseWinding);
reed374772b2016-10-05 17:33:02 -070046 paint.setBlendMode(SkBlendMode::kDstOut);
reed@android.com311c82d2009-05-05 23:13:23 +000047 canvas->drawPath(p, paint);
48 }
49 }
50
Brian Salomond0072812020-07-21 17:03:56 -040051 void onDrawContent(SkCanvas* canvas) override {
reed@android.com311c82d2009-05-05 23:13:23 +000052 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -040053 r.setLTRB(20, 20, 120, 120);
halcanary96fcdcc2015-08-27 07:41:13 -070054 canvas->saveLayer(&r, nullptr);
reed@android.com311c82d2009-05-05 23:13:23 +000055 canvas->drawColor(SK_ColorRED);
56 drawMask(canvas, r);
57 canvas->restore();
58 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000059
reed@android.com311c82d2009-05-05 23:13:23 +000060private:
John Stiles7571f9e2020-09-02 22:42:33 -040061 using INHERITED = Sample;
reed@android.com311c82d2009-05-05 23:13:23 +000062};
63
64///////////////////////////////////////////////////////////////////////////////
65
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040066DEF_SAMPLE( return new LayerMaskView(); )