blob: 77021cf78c79405e363743d35e399e020e69a251 [file] [log] [blame]
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +00001/*
2 * Copyright 2013 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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkClipOp.h"
11#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkRect.h"
14#include "include/core/SkScalar.h"
15#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
17#include "include/core/SkTypes.h"
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000018
reedbbf3e892014-06-20 11:33:59 -070019class CircularClipsGM : public skiagm::GM {
20 SkScalar fX1, fX2, fY, fR;
21 SkPath fCircle1, fCircle2;
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000022
caryclark63c684a2015-02-25 09:04:04 -080023protected:
mtklein36352bf2015-03-25 18:17:31 -070024 void onOnceBeforeDraw() override {
reedbbf3e892014-06-20 11:33:59 -070025 fX1 = 80;
26 fX2 = 120;
27 fY = 50;
28 fR = 40;
29
Mike Reed58cc97a2020-08-24 15:15:01 -040030 fCircle1 = SkPath::Circle(fX1, fY, fR, SkPathDirection::kCW);
31 fCircle2 = SkPath::Circle(fX2, fY, fR, SkPathDirection::kCW);
reedbbf3e892014-06-20 11:33:59 -070032 }
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000033
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000034
mtklein36352bf2015-03-25 18:17:31 -070035 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -080036
mtklein36352bf2015-03-25 18:17:31 -070037 SkString onShortName() override {
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000038 return SkString("circular-clips");
39 }
40
mtklein36352bf2015-03-25 18:17:31 -070041 SkISize onISize() override {
Michael Ludwig9689e392020-05-05 10:56:42 -040042 return SkISize::Make(800, 200);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000043 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 void onDraw(SkCanvas* canvas) override {
Mike Reedc1f77742016-12-09 09:00:50 -050046 const SkClipOp ops[] = {
Michael Ludwig2f6e2f82021-08-03 13:08:50 -040047 SkClipOp::kDifference,
48 SkClipOp::kIntersect
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000049 };
50
reedbbf3e892014-06-20 11:33:59 -070051 SkRect rect = SkRect::MakeLTRB(fX1 - fR, fY - fR, fX2 + fR, fY + fR);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000052
53 SkPaint fillPaint;
54
bsalomonfcabe422015-04-28 12:43:01 -070055 // Giant background circular clips (AA, non-inverted, replace/isect)
56 fillPaint.setColor(0x80808080);
57 canvas->save();
58 canvas->scale(10, 10);
59 canvas->translate(-((fX1 + fX2)/2 - fR), -(fY - 2*fR/3));
reed66998382016-09-21 11:15:07 -070060 canvas->clipPath(fCircle1, true);
61 canvas->clipPath(fCircle2, true);
bsalomonfcabe422015-04-28 12:43:01 -070062
63 canvas->drawRect(rect, fillPaint);
64
65 canvas->restore();
halcanary9d524f22016-03-29 09:03:52 -070066
bsalomonfcabe422015-04-28 12:43:01 -070067 fillPaint.setColor(0xFF000000);
68
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000069 for (size_t i = 0; i < 4; i++) {
reedbbf3e892014-06-20 11:33:59 -070070 fCircle1.toggleInverseFillType();
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000071 if (i % 2 == 0) {
reedbbf3e892014-06-20 11:33:59 -070072 fCircle2.toggleInverseFillType();
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000073 }
74
75 canvas->save();
76 for (size_t op = 0; op < SK_ARRAY_COUNT(ops); op++) {
77 canvas->save();
78
reed66998382016-09-21 11:15:07 -070079 canvas->clipPath(fCircle1);
reedbbf3e892014-06-20 11:33:59 -070080 canvas->clipPath(fCircle2, ops[op]);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000081
82 canvas->drawRect(rect, fillPaint);
83
84 canvas->restore();
reedbbf3e892014-06-20 11:33:59 -070085 canvas->translate(0, 2 * fY);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000086 }
87 canvas->restore();
reedbbf3e892014-06-20 11:33:59 -070088 canvas->translate(fX1 + fX2, 0);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000089 }
90 }
91
92private:
John Stiles7571f9e2020-09-02 22:42:33 -040093 using INHERITED = skiagm::GM;
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000094};
95
96//////////////////////////////////////////////////////////////////////////////
97
98DEF_GM( return new CircularClipsGM; )