blob: 38a16d20ebcfd45de3f907bdeaa4976ce57f3078 [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
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000037 SkString getName() const override { return SkString("circular-clips"); }
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000038
Leandro Lovisolo8f023882023-08-15 21:13:52 +000039 SkISize getISize() override { return SkISize::Make(800, 200); }
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 void onDraw(SkCanvas* canvas) override {
Mike Reedc1f77742016-12-09 09:00:50 -050042 const SkClipOp ops[] = {
Michael Ludwig2f6e2f82021-08-03 13:08:50 -040043 SkClipOp::kDifference,
44 SkClipOp::kIntersect
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000045 };
46
reedbbf3e892014-06-20 11:33:59 -070047 SkRect rect = SkRect::MakeLTRB(fX1 - fR, fY - fR, fX2 + fR, fY + fR);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000048
49 SkPaint fillPaint;
50
bsalomonfcabe422015-04-28 12:43:01 -070051 // Giant background circular clips (AA, non-inverted, replace/isect)
52 fillPaint.setColor(0x80808080);
53 canvas->save();
54 canvas->scale(10, 10);
55 canvas->translate(-((fX1 + fX2)/2 - fR), -(fY - 2*fR/3));
reed66998382016-09-21 11:15:07 -070056 canvas->clipPath(fCircle1, true);
57 canvas->clipPath(fCircle2, true);
bsalomonfcabe422015-04-28 12:43:01 -070058
59 canvas->drawRect(rect, fillPaint);
60
61 canvas->restore();
halcanary9d524f22016-03-29 09:03:52 -070062
bsalomonfcabe422015-04-28 12:43:01 -070063 fillPaint.setColor(0xFF000000);
64
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000065 for (size_t i = 0; i < 4; i++) {
reedbbf3e892014-06-20 11:33:59 -070066 fCircle1.toggleInverseFillType();
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000067 if (i % 2 == 0) {
reedbbf3e892014-06-20 11:33:59 -070068 fCircle2.toggleInverseFillType();
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000069 }
70
71 canvas->save();
Herb Derbyc37b3862022-06-21 09:49:17 -040072 for (size_t op = 0; op < std::size(ops); op++) {
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000073 canvas->save();
74
reed66998382016-09-21 11:15:07 -070075 canvas->clipPath(fCircle1);
reedbbf3e892014-06-20 11:33:59 -070076 canvas->clipPath(fCircle2, ops[op]);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000077
78 canvas->drawRect(rect, fillPaint);
79
80 canvas->restore();
reedbbf3e892014-06-20 11:33:59 -070081 canvas->translate(0, 2 * fY);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000082 }
83 canvas->restore();
reedbbf3e892014-06-20 11:33:59 -070084 canvas->translate(fX1 + fX2, 0);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000085 }
86 }
87
88private:
John Stiles7571f9e2020-09-02 22:42:33 -040089 using INHERITED = skiagm::GM;
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000090};
91
92//////////////////////////////////////////////////////////////////////////////
93
94DEF_GM( return new CircularClipsGM; )