blob: 14e77b9077ded5605c3400efe2fbd48ddb2b4549 [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 {
reed@google.coma8f60f22011-12-08 16:18:29 +000021 bool fDoAAClip;
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +000022 bool fDoSaveLayer;
bsalomon@google.com807cec42011-03-10 19:20:15 +000023public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000024 ComplexClipGM(bool aaclip, bool saveLayer)
robertphillips@google.com50a69a02012-07-12 13:48:46 +000025 : fDoAAClip(aaclip)
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +000026 , fDoSaveLayer(saveLayer) {
reed@google.coma8f60f22011-12-08 16:18:29 +000027 this->setBGColor(0xFFDDDDDD);
28// this->setBGColor(SkColorSetRGB(0xB0,0xDD,0xB0));
bsalomon@google.com807cec42011-03-10 19:20:15 +000029 }
30
31protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000032 virtual uint32_t onGetFlags() const SK_OVERRIDE {
33 return kSkipTiled_Flag;
34 }
35
bsalomon@google.com807cec42011-03-10 19:20:15 +000036
37 SkString onShortName() {
reed@google.coma8f60f22011-12-08 16:18:29 +000038 SkString str;
rmistry@google.comd6176b02012-08-23 18:14:13 +000039 str.printf("complexclip_%s%s",
robertphillips@google.com50a69a02012-07-12 13:48:46 +000040 fDoAAClip ? "aa" : "bw",
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +000041 fDoSaveLayer ? "_layer" : "");
reed@google.coma8f60f22011-12-08 16:18:29 +000042 return str;
bsalomon@google.com807cec42011-03-10 19:20:15 +000043 }
44
tfarinaf5393182014-06-09 23:59:03 -070045 SkISize onISize() { return SkISize::Make(970, 780); }
bsalomon@google.com807cec42011-03-10 19:20:15 +000046
bsalomon@google.com807cec42011-03-10 19:20:15 +000047 virtual void onDraw(SkCanvas* canvas) {
48 SkPath path;
49 path.moveTo(SkIntToScalar(0), SkIntToScalar(50));
50 path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0));
51 path.lineTo(SkIntToScalar(175), SkIntToScalar(0));
52 path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25));
53 path.lineTo(SkIntToScalar(200), SkIntToScalar(150));
54 path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200));
55 path.lineTo(SkIntToScalar(0), SkIntToScalar(200));
56 path.close();
57 path.moveTo(SkIntToScalar(50), SkIntToScalar(50));
58 path.lineTo(SkIntToScalar(150), SkIntToScalar(50));
59 path.lineTo(SkIntToScalar(150), SkIntToScalar(125));
60 path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150));
61 path.lineTo(SkIntToScalar(50), SkIntToScalar(150));
62 path.close();
63 path.setFillType(SkPath::kEvenOdd_FillType);
bsalomon@google.com807cec42011-03-10 19:20:15 +000064 SkPaint pathPaint;
65 pathPaint.setAntiAlias(true);
reed@google.coma8f60f22011-12-08 16:18:29 +000066 pathPaint.setColor(gPathColor);
bsalomon@google.com807cec42011-03-10 19:20:15 +000067
68 SkPath clipA;
69 clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20));
70 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22));
71 clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105));
72 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177));
73 clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180));
74 clipA.close();
bsalomon@google.com807cec42011-03-10 19:20:15 +000075
76 SkPath clipB;
77 clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10));
78 clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15));
79 clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190));
80 clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185));
81 clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100));
82 clipB.close();
bsalomon@google.com807cec42011-03-10 19:20:15 +000083
bsalomon@google.com807cec42011-03-10 19:20:15 +000084 SkPaint paint;
85 paint.setAntiAlias(true);
reed@google.coma8f60f22011-12-08 16:18:29 +000086 paint.setTextSize(SkIntToScalar(20));
bsalomon@google.com807cec42011-03-10 19:20:15 +000087
88 static const struct {
89 SkRegion::Op fOp;
90 const char* fName;
91 } gOps[] = { //extra spaces in names for measureText
92 {SkRegion::kIntersect_Op, "Isect "},
93 {SkRegion::kDifference_Op, "Diff " },
94 {SkRegion::kUnion_Op, "Union "},
95 {SkRegion::kXOR_Op, "Xor " },
96 {SkRegion::kReverseDifference_Op, "RDiff "}
97 };
98
reed@google.coma8f60f22011-12-08 16:18:29 +000099 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
bsalomon@google.com807cec42011-03-10 19:20:15 +0000100 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000101
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000102 if (fDoSaveLayer) {
103 // We want the layer to appear symmetric relative to actual
104 // device boundaries so we need to "undo" the effect of the
105 // scale and translate
106 SkRect bounds = SkRect::MakeLTRB(
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000107 4.0f/3.0f * -20,
108 4.0f/3.0f * -20,
109 4.0f/3.0f * (this->getISize().fWidth - 20),
110 4.0f/3.0f * (this->getISize().fHeight - 20));
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000111
112 bounds.inset(SkIntToScalar(100), SkIntToScalar(100));
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000113 SkPaint boundPaint;
114 boundPaint.setColor(SK_ColorRED);
115 boundPaint.setStyle(SkPaint::kStroke_Style);
116 canvas->drawRect(bounds, boundPaint);
117 canvas->saveLayer(&bounds, NULL);
118 }
119
reed@google.coma8f60f22011-12-08 16:18:29 +0000120 for (int invBits = 0; invBits < 4; ++invBits) {
121 canvas->save();
bsalomon@google.com807cec42011-03-10 19:20:15 +0000122 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
reed@google.coma8f60f22011-12-08 16:18:29 +0000123 this->drawHairlines(canvas, path, clipA, clipB);
124
125 bool doInvA = SkToBool(invBits & 1);
126 bool doInvB = SkToBool(invBits & 2);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000127 canvas->save();
128 // set clip
reed@google.coma8f60f22011-12-08 16:18:29 +0000129 clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
130 SkPath::kEvenOdd_FillType);
131 clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
132 SkPath::kEvenOdd_FillType);
133 canvas->clipPath(clipA, SkRegion::kIntersect_Op, fDoAAClip);
134 canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000135
136 // draw path clipped
137 canvas->drawPath(path, pathPaint);
138 canvas->restore();
139
bsalomon@google.com807cec42011-03-10 19:20:15 +0000140
reed@google.coma8f60f22011-12-08 16:18:29 +0000141 SkScalar txtX = SkIntToScalar(45);
142 paint.setColor(gClipAColor);
143 const char* aTxt = doInvA ? "InvA " : "A ";
bsalomon@google.com807cec42011-03-10 19:20:15 +0000144 canvas->drawText(aTxt, strlen(aTxt), txtX, SkIntToScalar(220), paint);
145 txtX += paint.measureText(aTxt, strlen(aTxt));
146 paint.setColor(SK_ColorBLACK);
147 canvas->drawText(gOps[op].fName, strlen(gOps[op].fName),
148 txtX, SkIntToScalar(220), paint);
149 txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName));
reed@google.coma8f60f22011-12-08 16:18:29 +0000150 paint.setColor(gClipBColor);
151 const char* bTxt = doInvB ? "InvB " : "B ";
152 canvas->drawText(bTxt, strlen(bTxt), txtX, SkIntToScalar(220), paint);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000153
154 canvas->translate(SkIntToScalar(250),0);
155 }
reed@google.coma8f60f22011-12-08 16:18:29 +0000156 canvas->restore();
157 canvas->translate(0, SkIntToScalar(250));
bsalomon@google.com807cec42011-03-10 19:20:15 +0000158 }
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000159
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000160 if (fDoSaveLayer) {
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000161 canvas->restore();
162 }
bsalomon@google.com807cec42011-03-10 19:20:15 +0000163 }
164private:
reed@google.coma8f60f22011-12-08 16:18:29 +0000165 void drawHairlines(SkCanvas* canvas, const SkPath& path,
166 const SkPath& clipA, const SkPath& clipB) {
167 SkPaint paint;
168 paint.setAntiAlias(true);
169 paint.setStyle(SkPaint::kStroke_Style);
170 const SkAlpha fade = 0x33;
171
172 // draw path in hairline
173 paint.setColor(gPathColor); paint.setAlpha(fade);
174 canvas->drawPath(path, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000175
reed@google.coma8f60f22011-12-08 16:18:29 +0000176 // draw clips in hair line
177 paint.setColor(gClipAColor); paint.setAlpha(fade);
178 canvas->drawPath(clipA, paint);
179 paint.setColor(gClipBColor); paint.setAlpha(fade);
180 canvas->drawPath(clipB, paint);
181 }
182
bsalomon@google.com807cec42011-03-10 19:20:15 +0000183 typedef GM INHERITED;
184};
185
186//////////////////////////////////////////////////////////////////////////////
187
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000188// aliased and anti-aliased w/o a layer
189static GM* gFact0(void*) { return new ComplexClipGM(false, false); }
190static GM* gFact1(void*) { return new ComplexClipGM(true, false); }
191
192// aliased and anti-aliased w/ a layer
193static GM* gFact2(void*) { return new ComplexClipGM(false, true); }
194static GM* gFact3(void*) { return new ComplexClipGM(true, true); }
reed@google.coma8f60f22011-12-08 16:18:29 +0000195
196static GMRegistry gReg0(gFact0);
197static GMRegistry gReg1(gFact1);
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000198static GMRegistry gReg2(gFact2);
199static GMRegistry gReg3(gFact3);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000200
201}