blob: 421b3d8d11c40fb914713d1dc24bce7f76e2ee5c [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 */
7#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04008#include "sk_tool_utils.h"
egdanieldf795032014-12-17 11:22:37 -08009#include "SkCanvas.h"
10#include "SkPath.h"
11
Ben Wagnerf08d1d02018-06-18 15:11:00 -040012#include <utility>
13
egdanieldf795032014-12-17 11:22:37 -080014namespace skiagm {
15
mtkleindbfd7ab2016-09-01 11:24:54 -070016constexpr SkColor gPathColor = SK_ColorYELLOW;
egdanieldf795032014-12-17 11:22:37 -080017
18class ComplexClip3GM : public GM {
19public:
20 ComplexClip3GM(bool doSimpleClipFirst)
21 : fDoSimpleClipFirst(doSimpleClipFirst) {
Mike Kleind46dce32018-08-16 10:17:03 -040022 this->setBGColor(0xFFDDDDDD);
egdanieldf795032014-12-17 11:22:37 -080023 }
24
25protected:
egdanieldf795032014-12-17 11:22:37 -080026
27 SkString onShortName() {
28 SkString str;
29 str.printf("complexclip3_%s", fDoSimpleClipFirst ? "simple" : "complex");
30 return str;
halcanary9d524f22016-03-29 09:03:52 -070031 }
egdanieldf795032014-12-17 11:22:37 -080032
33 SkISize onISize() { return SkISize::Make(1000, 950); }
34
35 virtual void onDraw(SkCanvas* canvas) {
36 SkPath clipSimple;
37 clipSimple.addCircle(SkIntToScalar(70), SkIntToScalar(50), SkIntToScalar(20));
38
39 SkRect r1 = { 10, 20, 70, 80 };
40 SkPath clipComplex;
41 clipComplex.moveTo(SkIntToScalar(40), SkIntToScalar(50));
42 clipComplex.arcTo(r1, SkIntToScalar(30), SkIntToScalar(300), false);
43 clipComplex.close();
44
45 SkPath* firstClip = &clipSimple;
46 SkPath* secondClip = &clipComplex;
47
48 if (!fDoSimpleClipFirst) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -040049 using std::swap;
50 swap(firstClip, secondClip);
egdanieldf795032014-12-17 11:22:37 -080051 }
52
53 SkPaint paint;
54 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070055 sk_tool_utils::set_portable_typeface(&paint);
egdanieldf795032014-12-17 11:22:37 -080056 paint.setTextSize(SkIntToScalar(20));
57
mtkleindbfd7ab2016-09-01 11:24:54 -070058 constexpr struct {
Mike Reedc1f77742016-12-09 09:00:50 -050059 SkClipOp fOp;
60 const char* fName;
egdanieldf795032014-12-17 11:22:37 -080061 } gOps[] = {
Mike Reedc1f77742016-12-09 09:00:50 -050062 {kIntersect_SkClipOp, "I"},
63 {kDifference_SkClipOp, "D" },
64 {kUnion_SkClipOp, "U"},
65 {kXOR_SkClipOp, "X" },
66 {kReverseDifference_SkClipOp, "R"}
egdanieldf795032014-12-17 11:22:37 -080067 };
68
69 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
70 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
71
72 SkPaint pathPaint;
73 pathPaint.setAntiAlias(true);
74 pathPaint.setColor(gPathColor);
75
76 for (int invA = 0; invA < 2; ++invA) {
77 for (int aaBits = 0; aaBits < 4; ++aaBits) {
78 canvas->save();
79 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
80 for (int invB = 0; invB < 2; ++invB) {
81 bool doAAA = SkToBool(aaBits & 1);
82 bool doAAB = SkToBool(aaBits & 2);
83 bool doInvA = SkToBool(invA);
84 bool doInvB = SkToBool(invB);
85 canvas->save();
86 // set clip
87 firstClip->setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
88 SkPath::kEvenOdd_FillType);
89 secondClip->setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
90 SkPath::kEvenOdd_FillType);
reed66998382016-09-21 11:15:07 -070091 canvas->clipPath(*firstClip, doAAA);
egdanieldf795032014-12-17 11:22:37 -080092 canvas->clipPath(*secondClip, gOps[op].fOp, doAAB);
93
94 // draw rect clipped
95 SkRect r = { 0, 0, 100, 100 };
96 canvas->drawRect(r, pathPaint);
97 canvas->restore();
98
99
100 SkScalar txtX = SkIntToScalar(10);
101 paint.setColor(SK_ColorBLACK);
102 SkString str;
103 str.printf("%s%s %s %s%s", doAAA ? "A" : "B",
104 doInvA ? "I" : "N",
105 gOps[op].fName,
106 doAAB ? "A" : "B",
107 doInvB ? "I" : "N");
108
Cary Clark2a475ea2017-04-28 15:35:12 -0400109 canvas->drawString(str.c_str(), txtX, SkIntToScalar(130), paint);
egdanieldf795032014-12-17 11:22:37 -0800110 if (doInvB) {
111 canvas->translate(SkIntToScalar(150),0);
112 } else {
113 canvas->translate(SkIntToScalar(120),0);
114 }
115 }
116 }
117 canvas->restore();
118 canvas->translate(0, SkIntToScalar(150));
119 }
120 }
121 }
122
123private:
124 bool fDoSimpleClipFirst;
125
126 typedef GM INHERITED;
127};
128
129//////////////////////////////////////////////////////////////////////////////
130
131// Simple clip first
132DEF_GM( return new ComplexClip3GM(true); )
133// Complex clip first
134DEF_GM( return new ComplexClip3GM(false); )
135}