blob: ebb7707c9cfeec36dbabfe5b269bc36d1c57693e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
bsalomon@google.com807cec42011-03-10 19:20:15 +00008#include "gm.h"
9#include "SkCanvas.h"
10//#include "SkParsePath.h"
11#include "SkPath.h"
12//#include "SkRandom.h"
13
14namespace skiagm {
15
reed@google.coma8f60f22011-12-08 16:18:29 +000016static const SkColor gPathColor = SK_ColorBLACK;
17static const SkColor gClipAColor = SK_ColorBLUE;
18static const SkColor gClipBColor = SK_ColorRED;
19
bsalomon@google.com807cec42011-03-10 19:20:15 +000020class ComplexClipGM : public GM {
21public:
bsalomon6ae83cf2014-12-17 14:38:49 -080022 ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw)
robertphillips@google.com50a69a02012-07-12 13:48:46 +000023 : fDoAAClip(aaclip)
bsalomon6ae83cf2014-12-17 14:38:49 -080024 , fDoSaveLayer(saveLayer)
25 , fInvertDraw(invertDraw) {
reed@google.coma8f60f22011-12-08 16:18:29 +000026 this->setBGColor(0xFFDDDDDD);
bsalomon@google.com807cec42011-03-10 19:20:15 +000027 }
28
29protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000030 virtual uint32_t onGetFlags() const SK_OVERRIDE {
31 return kSkipTiled_Flag;
32 }
33
bsalomon@google.com807cec42011-03-10 19:20:15 +000034
35 SkString onShortName() {
reed@google.coma8f60f22011-12-08 16:18:29 +000036 SkString str;
bsalomon6ae83cf2014-12-17 14:38:49 -080037 str.printf("complexclip_%s%s%s",
robertphillips@google.com50a69a02012-07-12 13:48:46 +000038 fDoAAClip ? "aa" : "bw",
bsalomon6ae83cf2014-12-17 14:38:49 -080039 fDoSaveLayer ? "_layer" : "",
40 fInvertDraw ? "_invert" : "");
reed@google.coma8f60f22011-12-08 16:18:29 +000041 return str;
bsalomon@google.com807cec42011-03-10 19:20:15 +000042 }
43
tfarinaf5393182014-06-09 23:59:03 -070044 SkISize onISize() { return SkISize::Make(970, 780); }
bsalomon@google.com807cec42011-03-10 19:20:15 +000045
bsalomon@google.com807cec42011-03-10 19:20:15 +000046 virtual void onDraw(SkCanvas* canvas) {
47 SkPath path;
48 path.moveTo(SkIntToScalar(0), SkIntToScalar(50));
49 path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0));
50 path.lineTo(SkIntToScalar(175), SkIntToScalar(0));
51 path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25));
52 path.lineTo(SkIntToScalar(200), SkIntToScalar(150));
53 path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200));
54 path.lineTo(SkIntToScalar(0), SkIntToScalar(200));
55 path.close();
56 path.moveTo(SkIntToScalar(50), SkIntToScalar(50));
57 path.lineTo(SkIntToScalar(150), SkIntToScalar(50));
58 path.lineTo(SkIntToScalar(150), SkIntToScalar(125));
59 path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150));
60 path.lineTo(SkIntToScalar(50), SkIntToScalar(150));
61 path.close();
bsalomon6ae83cf2014-12-17 14:38:49 -080062 if (fInvertDraw) {
63 path.setFillType(SkPath::kInverseEvenOdd_FillType);
64 } else {
65 path.setFillType(SkPath::kEvenOdd_FillType);
66 }
bsalomon@google.com807cec42011-03-10 19:20:15 +000067 SkPaint pathPaint;
68 pathPaint.setAntiAlias(true);
reed@google.coma8f60f22011-12-08 16:18:29 +000069 pathPaint.setColor(gPathColor);
bsalomon@google.com807cec42011-03-10 19:20:15 +000070
71 SkPath clipA;
72 clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20));
73 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22));
74 clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105));
75 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177));
76 clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180));
77 clipA.close();
bsalomon@google.com807cec42011-03-10 19:20:15 +000078
79 SkPath clipB;
80 clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10));
81 clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15));
82 clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190));
83 clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185));
84 clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100));
85 clipB.close();
bsalomon@google.com807cec42011-03-10 19:20:15 +000086
bsalomon@google.com807cec42011-03-10 19:20:15 +000087 SkPaint paint;
88 paint.setAntiAlias(true);
caryclark5fb6bd42014-06-23 11:25:00 -070089 sk_tool_utils::set_portable_typeface(&paint);
reed@google.coma8f60f22011-12-08 16:18:29 +000090 paint.setTextSize(SkIntToScalar(20));
bsalomon@google.com807cec42011-03-10 19:20:15 +000091
92 static const struct {
93 SkRegion::Op fOp;
94 const char* fName;
95 } gOps[] = { //extra spaces in names for measureText
96 {SkRegion::kIntersect_Op, "Isect "},
97 {SkRegion::kDifference_Op, "Diff " },
98 {SkRegion::kUnion_Op, "Union "},
99 {SkRegion::kXOR_Op, "Xor " },
100 {SkRegion::kReverseDifference_Op, "RDiff "}
101 };
102
reed@google.coma8f60f22011-12-08 16:18:29 +0000103 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
bsalomon@google.com807cec42011-03-10 19:20:15 +0000104 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000105
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000106 if (fDoSaveLayer) {
107 // We want the layer to appear symmetric relative to actual
108 // device boundaries so we need to "undo" the effect of the
109 // scale and translate
110 SkRect bounds = SkRect::MakeLTRB(
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000111 4.0f/3.0f * -20,
112 4.0f/3.0f * -20,
113 4.0f/3.0f * (this->getISize().fWidth - 20),
114 4.0f/3.0f * (this->getISize().fHeight - 20));
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000115
116 bounds.inset(SkIntToScalar(100), SkIntToScalar(100));
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000117 SkPaint boundPaint;
118 boundPaint.setColor(SK_ColorRED);
119 boundPaint.setStyle(SkPaint::kStroke_Style);
120 canvas->drawRect(bounds, boundPaint);
121 canvas->saveLayer(&bounds, NULL);
122 }
123
reed@google.coma8f60f22011-12-08 16:18:29 +0000124 for (int invBits = 0; invBits < 4; ++invBits) {
125 canvas->save();
bsalomon@google.com807cec42011-03-10 19:20:15 +0000126 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
reed@google.coma8f60f22011-12-08 16:18:29 +0000127 this->drawHairlines(canvas, path, clipA, clipB);
128
129 bool doInvA = SkToBool(invBits & 1);
130 bool doInvB = SkToBool(invBits & 2);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000131 canvas->save();
132 // set clip
reed@google.coma8f60f22011-12-08 16:18:29 +0000133 clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
134 SkPath::kEvenOdd_FillType);
135 clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
136 SkPath::kEvenOdd_FillType);
137 canvas->clipPath(clipA, SkRegion::kIntersect_Op, fDoAAClip);
138 canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000139
bsalomon6ae83cf2014-12-17 14:38:49 -0800140 // In the inverse case we need to prevent the draw from covering the whole
141 // canvas.
142 if (fInvertDraw) {
143 SkRect rectClip = clipA.getBounds();
144 rectClip.join(path.getBounds());
145 rectClip.join(path.getBounds());
146 rectClip.outset(5, 5);
147 canvas->clipRect(rectClip);
148 }
149
bsalomon@google.com807cec42011-03-10 19:20:15 +0000150 // draw path clipped
151 canvas->drawPath(path, pathPaint);
152 canvas->restore();
153
bsalomon@google.com807cec42011-03-10 19:20:15 +0000154
reed@google.coma8f60f22011-12-08 16:18:29 +0000155 SkScalar txtX = SkIntToScalar(45);
156 paint.setColor(gClipAColor);
157 const char* aTxt = doInvA ? "InvA " : "A ";
bsalomon@google.com807cec42011-03-10 19:20:15 +0000158 canvas->drawText(aTxt, strlen(aTxt), txtX, SkIntToScalar(220), paint);
159 txtX += paint.measureText(aTxt, strlen(aTxt));
160 paint.setColor(SK_ColorBLACK);
161 canvas->drawText(gOps[op].fName, strlen(gOps[op].fName),
162 txtX, SkIntToScalar(220), paint);
163 txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName));
reed@google.coma8f60f22011-12-08 16:18:29 +0000164 paint.setColor(gClipBColor);
165 const char* bTxt = doInvB ? "InvB " : "B ";
166 canvas->drawText(bTxt, strlen(bTxt), txtX, SkIntToScalar(220), paint);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000167
168 canvas->translate(SkIntToScalar(250),0);
169 }
reed@google.coma8f60f22011-12-08 16:18:29 +0000170 canvas->restore();
171 canvas->translate(0, SkIntToScalar(250));
bsalomon@google.com807cec42011-03-10 19:20:15 +0000172 }
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000173
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000174 if (fDoSaveLayer) {
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000175 canvas->restore();
176 }
bsalomon@google.com807cec42011-03-10 19:20:15 +0000177 }
178private:
reed@google.coma8f60f22011-12-08 16:18:29 +0000179 void drawHairlines(SkCanvas* canvas, const SkPath& path,
180 const SkPath& clipA, const SkPath& clipB) {
181 SkPaint paint;
182 paint.setAntiAlias(true);
183 paint.setStyle(SkPaint::kStroke_Style);
184 const SkAlpha fade = 0x33;
185
186 // draw path in hairline
187 paint.setColor(gPathColor); paint.setAlpha(fade);
188 canvas->drawPath(path, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189
reed@google.coma8f60f22011-12-08 16:18:29 +0000190 // draw clips in hair line
191 paint.setColor(gClipAColor); paint.setAlpha(fade);
192 canvas->drawPath(clipA, paint);
193 paint.setColor(gClipBColor); paint.setAlpha(fade);
194 canvas->drawPath(clipB, paint);
195 }
196
bsalomon6ae83cf2014-12-17 14:38:49 -0800197 bool fDoAAClip;
198 bool fDoSaveLayer;
199 bool fInvertDraw;
200
bsalomon@google.com807cec42011-03-10 19:20:15 +0000201 typedef GM INHERITED;
202};
203
204//////////////////////////////////////////////////////////////////////////////
205
bsalomon6ae83cf2014-12-17 14:38:49 -0800206DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, false, false)); )
207DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, false, true)); )
208DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, true, false)); )
209DEF_GM( return SkNEW_ARGS(ComplexClipGM, (false, true, true)); )
210DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, false, false)); )
211DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, false, true)); )
212DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, true, false)); )
213DEF_GM( return SkNEW_ARGS(ComplexClipGM, (true, true, true)); )
bsalomon@google.com807cec42011-03-10 19:20:15 +0000214
215}