blob: 6a9dedbb436f8e4d9148603da92eac35a5b80e4b [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"
18#include "src/core/SkClipOpPriv.h"
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000019
reedbbf3e892014-06-20 11:33:59 -070020class CircularClipsGM : public skiagm::GM {
21 SkScalar fX1, fX2, fY, fR;
22 SkPath fCircle1, fCircle2;
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000023
caryclark63c684a2015-02-25 09:04:04 -080024protected:
mtklein36352bf2015-03-25 18:17:31 -070025 void onOnceBeforeDraw() override {
reedbbf3e892014-06-20 11:33:59 -070026 fX1 = 80;
27 fX2 = 120;
28 fY = 50;
29 fR = 40;
30
Mike Reed30bc5272019-11-22 18:34:02 +000031 fCircle1.addCircle(fX1, fY, fR, SkPathDirection::kCW);
32 fCircle2.addCircle(fX2, fY, fR, SkPathDirection::kCW);
reedbbf3e892014-06-20 11:33:59 -070033 }
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000034
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000035
mtklein36352bf2015-03-25 18:17:31 -070036 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -080037
mtklein36352bf2015-03-25 18:17:31 -070038 SkString onShortName() override {
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000039 return SkString("circular-clips");
40 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 SkISize onISize() override {
Michael Ludwig9689e392020-05-05 10:56:42 -040043 return SkISize::Make(800, 200);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000044 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 void onDraw(SkCanvas* canvas) override {
Mike Reedc1f77742016-12-09 09:00:50 -050047 const SkClipOp ops[] = {
48 kDifference_SkClipOp,
Michael Ludwig9689e392020-05-05 10:56:42 -040049 kIntersect_SkClipOp
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000050 };
51
reedbbf3e892014-06-20 11:33:59 -070052 SkRect rect = SkRect::MakeLTRB(fX1 - fR, fY - fR, fX2 + fR, fY + fR);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000053
54 SkPaint fillPaint;
55
bsalomonfcabe422015-04-28 12:43:01 -070056 // Giant background circular clips (AA, non-inverted, replace/isect)
57 fillPaint.setColor(0x80808080);
58 canvas->save();
59 canvas->scale(10, 10);
60 canvas->translate(-((fX1 + fX2)/2 - fR), -(fY - 2*fR/3));
reed66998382016-09-21 11:15:07 -070061 canvas->clipPath(fCircle1, true);
62 canvas->clipPath(fCircle2, true);
bsalomonfcabe422015-04-28 12:43:01 -070063
64 canvas->drawRect(rect, fillPaint);
65
66 canvas->restore();
halcanary9d524f22016-03-29 09:03:52 -070067
bsalomonfcabe422015-04-28 12:43:01 -070068 fillPaint.setColor(0xFF000000);
69
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000070 for (size_t i = 0; i < 4; i++) {
reedbbf3e892014-06-20 11:33:59 -070071 fCircle1.toggleInverseFillType();
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000072 if (i % 2 == 0) {
reedbbf3e892014-06-20 11:33:59 -070073 fCircle2.toggleInverseFillType();
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000074 }
75
76 canvas->save();
77 for (size_t op = 0; op < SK_ARRAY_COUNT(ops); op++) {
78 canvas->save();
79
reed66998382016-09-21 11:15:07 -070080 canvas->clipPath(fCircle1);
reedbbf3e892014-06-20 11:33:59 -070081 canvas->clipPath(fCircle2, ops[op]);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000082
83 canvas->drawRect(rect, fillPaint);
84
85 canvas->restore();
reedbbf3e892014-06-20 11:33:59 -070086 canvas->translate(0, 2 * fY);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000087 }
88 canvas->restore();
reedbbf3e892014-06-20 11:33:59 -070089 canvas->translate(fX1 + fX2, 0);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000090 }
91 }
92
93private:
reedbbf3e892014-06-20 11:33:59 -070094 typedef skiagm::GM INHERITED;
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +000095};
96
97//////////////////////////////////////////////////////////////////////////////
98
99DEF_GM( return new CircularClipsGM; )