blob: 30bb50b988bb1d3c8c174179c87de6c6b8717910 [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;
bsalomon@google.com807cec42011-03-10 19:20:15 +000022public:
reed@google.coma8f60f22011-12-08 16:18:29 +000023 ComplexClipGM(bool aaclip) : fDoAAClip(aaclip) {
24 this->setBGColor(0xFFDDDDDD);
25// this->setBGColor(SkColorSetRGB(0xB0,0xDD,0xB0));
bsalomon@google.com807cec42011-03-10 19:20:15 +000026 }
27
28protected:
29
30 SkString onShortName() {
reed@google.coma8f60f22011-12-08 16:18:29 +000031 SkString str;
32 str.printf("complexclip_%s", fDoAAClip ? "aa" : "bw");
33 return str;
bsalomon@google.com807cec42011-03-10 19:20:15 +000034 }
35
reed@google.coma8f60f22011-12-08 16:18:29 +000036 SkISize onISize() { return make_isize(970, 780); }
bsalomon@google.com807cec42011-03-10 19:20:15 +000037
bsalomon@google.com807cec42011-03-10 19:20:15 +000038 virtual void onDraw(SkCanvas* canvas) {
39 SkPath path;
40 path.moveTo(SkIntToScalar(0), SkIntToScalar(50));
41 path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0));
42 path.lineTo(SkIntToScalar(175), SkIntToScalar(0));
43 path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25));
44 path.lineTo(SkIntToScalar(200), SkIntToScalar(150));
45 path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200));
46 path.lineTo(SkIntToScalar(0), SkIntToScalar(200));
47 path.close();
48 path.moveTo(SkIntToScalar(50), SkIntToScalar(50));
49 path.lineTo(SkIntToScalar(150), SkIntToScalar(50));
50 path.lineTo(SkIntToScalar(150), SkIntToScalar(125));
51 path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150));
52 path.lineTo(SkIntToScalar(50), SkIntToScalar(150));
53 path.close();
54 path.setFillType(SkPath::kEvenOdd_FillType);
bsalomon@google.com807cec42011-03-10 19:20:15 +000055 SkPaint pathPaint;
56 pathPaint.setAntiAlias(true);
reed@google.coma8f60f22011-12-08 16:18:29 +000057 pathPaint.setColor(gPathColor);
bsalomon@google.com807cec42011-03-10 19:20:15 +000058
59 SkPath clipA;
60 clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20));
61 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22));
62 clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105));
63 clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177));
64 clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180));
65 clipA.close();
bsalomon@google.com807cec42011-03-10 19:20:15 +000066
67 SkPath clipB;
68 clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10));
69 clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15));
70 clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190));
71 clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185));
72 clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100));
73 clipB.close();
bsalomon@google.com807cec42011-03-10 19:20:15 +000074
bsalomon@google.com807cec42011-03-10 19:20:15 +000075 SkPaint paint;
76 paint.setAntiAlias(true);
reed@google.coma8f60f22011-12-08 16:18:29 +000077 paint.setTextSize(SkIntToScalar(20));
bsalomon@google.com807cec42011-03-10 19:20:15 +000078
79 static const struct {
80 SkRegion::Op fOp;
81 const char* fName;
82 } gOps[] = { //extra spaces in names for measureText
83 {SkRegion::kIntersect_Op, "Isect "},
84 {SkRegion::kDifference_Op, "Diff " },
85 {SkRegion::kUnion_Op, "Union "},
86 {SkRegion::kXOR_Op, "Xor " },
87 {SkRegion::kReverseDifference_Op, "RDiff "}
88 };
89
reed@google.coma8f60f22011-12-08 16:18:29 +000090 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
bsalomon@google.com807cec42011-03-10 19:20:15 +000091 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
bsalomon@google.com807cec42011-03-10 19:20:15 +000092
reed@google.coma8f60f22011-12-08 16:18:29 +000093 for (int invBits = 0; invBits < 4; ++invBits) {
94 canvas->save();
bsalomon@google.com807cec42011-03-10 19:20:15 +000095 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
reed@google.coma8f60f22011-12-08 16:18:29 +000096 this->drawHairlines(canvas, path, clipA, clipB);
97
98 bool doInvA = SkToBool(invBits & 1);
99 bool doInvB = SkToBool(invBits & 2);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000100 canvas->save();
101 // set clip
reed@google.coma8f60f22011-12-08 16:18:29 +0000102 clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
103 SkPath::kEvenOdd_FillType);
104 clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
105 SkPath::kEvenOdd_FillType);
106 canvas->clipPath(clipA, SkRegion::kIntersect_Op, fDoAAClip);
107 canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000108
109 // draw path clipped
110 canvas->drawPath(path, pathPaint);
111 canvas->restore();
112
bsalomon@google.com807cec42011-03-10 19:20:15 +0000113
reed@google.coma8f60f22011-12-08 16:18:29 +0000114 SkScalar txtX = SkIntToScalar(45);
115 paint.setColor(gClipAColor);
116 const char* aTxt = doInvA ? "InvA " : "A ";
bsalomon@google.com807cec42011-03-10 19:20:15 +0000117 canvas->drawText(aTxt, strlen(aTxt), txtX, SkIntToScalar(220), paint);
118 txtX += paint.measureText(aTxt, strlen(aTxt));
119 paint.setColor(SK_ColorBLACK);
120 canvas->drawText(gOps[op].fName, strlen(gOps[op].fName),
121 txtX, SkIntToScalar(220), paint);
122 txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName));
reed@google.coma8f60f22011-12-08 16:18:29 +0000123 paint.setColor(gClipBColor);
124 const char* bTxt = doInvB ? "InvB " : "B ";
125 canvas->drawText(bTxt, strlen(bTxt), txtX, SkIntToScalar(220), paint);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000126
127 canvas->translate(SkIntToScalar(250),0);
128 }
reed@google.coma8f60f22011-12-08 16:18:29 +0000129 canvas->restore();
130 canvas->translate(0, SkIntToScalar(250));
bsalomon@google.com807cec42011-03-10 19:20:15 +0000131 }
bsalomon@google.com807cec42011-03-10 19:20:15 +0000132 }
133private:
reed@google.coma8f60f22011-12-08 16:18:29 +0000134 void drawHairlines(SkCanvas* canvas, const SkPath& path,
135 const SkPath& clipA, const SkPath& clipB) {
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 paint.setStyle(SkPaint::kStroke_Style);
139 const SkAlpha fade = 0x33;
140
141 // draw path in hairline
142 paint.setColor(gPathColor); paint.setAlpha(fade);
143 canvas->drawPath(path, paint);
144
145 // draw clips in hair line
146 paint.setColor(gClipAColor); paint.setAlpha(fade);
147 canvas->drawPath(clipA, paint);
148 paint.setColor(gClipBColor); paint.setAlpha(fade);
149 canvas->drawPath(clipB, paint);
150 }
151
bsalomon@google.com807cec42011-03-10 19:20:15 +0000152 typedef GM INHERITED;
153};
154
155//////////////////////////////////////////////////////////////////////////////
156
reed@google.coma8f60f22011-12-08 16:18:29 +0000157static GM* gFact0(void*) { return new ComplexClipGM(false); }
158static GM* gFact1(void*) { return new ComplexClipGM(true); }
159
160static GMRegistry gReg0(gFact0);
161static GMRegistry gReg1(gFact1);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000162
163}