blob: f551b29e4aaa4145f135817856ce4e2ef2379717 [file] [log] [blame]
egdanieldf795032014-12-17 11:22:37 -08001/*
2 * Copyright 2014 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 */
Ben Wagner7fde8e12019-05-01 17:28:53 -04007
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/SkColor.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkPaint.h"
Mike Reedcfb130c2020-08-03 11:02:20 -040014#include "include/core/SkPathBuilder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTypeface.h"
20#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/ToolUtils.h"
egdanieldf795032014-12-17 11:22:37 -080022
Ben Wagnerf08d1d02018-06-18 15:11:00 -040023#include <utility>
24
egdanieldf795032014-12-17 11:22:37 -080025namespace skiagm {
26
mtkleindbfd7ab2016-09-01 11:24:54 -070027constexpr SkColor gPathColor = SK_ColorYELLOW;
egdanieldf795032014-12-17 11:22:37 -080028
29class ComplexClip3GM : public GM {
30public:
31 ComplexClip3GM(bool doSimpleClipFirst)
32 : fDoSimpleClipFirst(doSimpleClipFirst) {
Mike Kleind46dce32018-08-16 10:17:03 -040033 this->setBGColor(0xFFDDDDDD);
egdanieldf795032014-12-17 11:22:37 -080034 }
35
36protected:
egdanieldf795032014-12-17 11:22:37 -080037
Brian Salomond0072812020-07-21 17:03:56 -040038 SkString onShortName() override {
egdanieldf795032014-12-17 11:22:37 -080039 SkString str;
40 str.printf("complexclip3_%s", fDoSimpleClipFirst ? "simple" : "complex");
41 return str;
halcanary9d524f22016-03-29 09:03:52 -070042 }
egdanieldf795032014-12-17 11:22:37 -080043
Brian Salomond0072812020-07-21 17:03:56 -040044 SkISize onISize() override { return SkISize::Make(400, 950); }
egdanieldf795032014-12-17 11:22:37 -080045
Brian Salomond0072812020-07-21 17:03:56 -040046 void onDraw(SkCanvas* canvas) override {
Mike Reedcfb130c2020-08-03 11:02:20 -040047 SkPath clipSimple = SkPath::Circle(70, 50, 20);
egdanieldf795032014-12-17 11:22:37 -080048
49 SkRect r1 = { 10, 20, 70, 80 };
Mike Reedcfb130c2020-08-03 11:02:20 -040050 SkPath clipComplex = SkPathBuilder().moveTo(40, 50)
51 .arcTo(r1, 30, 300, false)
52 .close()
53 .detach();
egdanieldf795032014-12-17 11:22:37 -080054
55 SkPath* firstClip = &clipSimple;
56 SkPath* secondClip = &clipComplex;
egdanieldf795032014-12-17 11:22:37 -080057 if (!fDoSimpleClipFirst) {
Mike Reedcfb130c2020-08-03 11:02:20 -040058 std::swap(firstClip, secondClip);
egdanieldf795032014-12-17 11:22:37 -080059 }
60
61 SkPaint paint;
62 paint.setAntiAlias(true);
Mike Reed4de2f1f2019-01-05 16:35:13 -050063
Mike Kleinea3f0142019-03-20 11:12:10 -050064 SkFont font(ToolUtils::create_portable_typeface(), 20);
egdanieldf795032014-12-17 11:22:37 -080065
mtkleindbfd7ab2016-09-01 11:24:54 -070066 constexpr struct {
Mike Reedc1f77742016-12-09 09:00:50 -050067 SkClipOp fOp;
68 const char* fName;
egdanieldf795032014-12-17 11:22:37 -080069 } gOps[] = {
Michael Ludwig2f6e2f82021-08-03 13:08:50 -040070 {SkClipOp::kIntersect, "I"},
71 {SkClipOp::kDifference, "D" },
egdanieldf795032014-12-17 11:22:37 -080072 };
73
74 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
75 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
76
77 SkPaint pathPaint;
78 pathPaint.setAntiAlias(true);
79 pathPaint.setColor(gPathColor);
80
81 for (int invA = 0; invA < 2; ++invA) {
82 for (int aaBits = 0; aaBits < 4; ++aaBits) {
83 canvas->save();
84 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
85 for (int invB = 0; invB < 2; ++invB) {
86 bool doAAA = SkToBool(aaBits & 1);
87 bool doAAB = SkToBool(aaBits & 2);
88 bool doInvA = SkToBool(invA);
89 bool doInvB = SkToBool(invB);
90 canvas->save();
91 // set clip
Mike Reed7d34dc72019-11-26 12:17:17 -050092 firstClip->setFillType(doInvA ? SkPathFillType::kInverseEvenOdd :
93 SkPathFillType::kEvenOdd);
94 secondClip->setFillType(doInvB ? SkPathFillType::kInverseEvenOdd :
95 SkPathFillType::kEvenOdd);
reed66998382016-09-21 11:15:07 -070096 canvas->clipPath(*firstClip, doAAA);
egdanieldf795032014-12-17 11:22:37 -080097 canvas->clipPath(*secondClip, gOps[op].fOp, doAAB);
98
99 // draw rect clipped
100 SkRect r = { 0, 0, 100, 100 };
101 canvas->drawRect(r, pathPaint);
102 canvas->restore();
103
104
105 SkScalar txtX = SkIntToScalar(10);
106 paint.setColor(SK_ColorBLACK);
107 SkString str;
108 str.printf("%s%s %s %s%s", doAAA ? "A" : "B",
109 doInvA ? "I" : "N",
110 gOps[op].fName,
111 doAAB ? "A" : "B",
112 doInvB ? "I" : "N");
113
Mike Reed4de2f1f2019-01-05 16:35:13 -0500114 canvas->drawString(str.c_str(), txtX, SkIntToScalar(130), font, paint);
egdanieldf795032014-12-17 11:22:37 -0800115 if (doInvB) {
116 canvas->translate(SkIntToScalar(150),0);
117 } else {
118 canvas->translate(SkIntToScalar(120),0);
119 }
120 }
121 }
122 canvas->restore();
123 canvas->translate(0, SkIntToScalar(150));
124 }
125 }
126 }
127
128private:
129 bool fDoSimpleClipFirst;
130
John Stiles7571f9e2020-09-02 22:42:33 -0400131 using INHERITED = GM;
egdanieldf795032014-12-17 11:22:37 -0800132};
133
134//////////////////////////////////////////////////////////////////////////////
135
136// Simple clip first
137DEF_GM( return new ComplexClip3GM(true); )
138// Complex clip first
139DEF_GM( return new ComplexClip3GM(false); )
John Stilesa6841be2020-08-06 14:11:56 -0400140} // namespace skiagm