blob: dfec4e73e1e6ad6f571c8b1e66a1997dbad75752 [file] [log] [blame]
reed@google.com30db5992011-08-29 17:41:02 +00001/*
2 * Copyright 2011 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 */
bsalomon@google.com574f29d2011-08-29 18:05:05 +00007
8
reed@google.com30db5992011-08-29 17:41:02 +00009#include "gm.h"
10#include "SkCanvas.h"
11#include "SkPath.h"
12#include "SkRandom.h"
13
14namespace skiagm {
15
16class ComplexClip2GM : public GM {
17public:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000018 enum Clip {
19 kRect_Clip,
20 kRRect_Clip,
21 kPath_Clip
22 };
23
24 ComplexClip2GM(Clip clip, bool antiAlias)
25 : fClip(clip)
robertphillips@google.com2470b252012-05-15 17:03:16 +000026 , fAntiAlias(antiAlias) {
caryclark63c684a2015-02-25 09:04:04 -080027 SkScalar xA = 0.65f;
28 SkScalar xF = 50.65f;
29
30 SkScalar yA = 0.65f;
31 SkScalar yF = 50.65f;
32
33 fWidth = xF - xA;
34 fHeight = yF - yA;
35
36 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
37 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
38 }
39
40protected:
mtklein36352bf2015-03-25 18:17:31 -070041 void onOnceBeforeDraw() override {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000042 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
robertphillips@google.com2470b252012-05-15 17:03:16 +000044 // offset the rects a bit so we get antialiasing even in the rect case
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000045 SkScalar xA = 0.65f;
46 SkScalar xB = 10.65f;
47 SkScalar xC = 20.65f;
48 SkScalar xD = 30.65f;
49 SkScalar xE = 40.65f;
50 SkScalar xF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000051
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000052 SkScalar yA = 0.65f;
53 SkScalar yB = 10.65f;
54 SkScalar yC = 20.65f;
55 SkScalar yD = 30.65f;
56 SkScalar yE = 40.65f;
57 SkScalar yF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000058
bsalomon@google.com574f29d2011-08-29 18:05:05 +000059 fRects[0].set(xB, yB, xE, yE);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000060 fRRects[0].setRectXY(fRects[0], 7, 7);
61 fPaths[0].addRoundRect(fRects[0], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000062 fRectColors[0] = SK_ColorRED;
63
bsalomon@google.com574f29d2011-08-29 18:05:05 +000064 fRects[1].set(xA, yA, xD, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000065 fRRects[1].setRectXY(fRects[1], 7, 7);
66 fPaths[1].addRoundRect(fRects[1], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000067 fRectColors[1] = SK_ColorGREEN;
68
bsalomon@google.com574f29d2011-08-29 18:05:05 +000069 fRects[2].set(xC, yA, xF, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000070 fRRects[2].setRectXY(fRects[2], 7, 7);
71 fPaths[2].addRoundRect(fRects[2], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000072 fRectColors[2] = SK_ColorBLUE;
73
bsalomon@google.com574f29d2011-08-29 18:05:05 +000074 fRects[3].set(xA, yC, xD, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000075 fRRects[3].setRectXY(fRects[3], 7, 7);
76 fPaths[3].addRoundRect(fRects[3], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000077 fRectColors[3] = SK_ColorYELLOW;
78
bsalomon@google.com574f29d2011-08-29 18:05:05 +000079 fRects[4].set(xC, yC, xF, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000080 fRRects[4].setRectXY(fRects[4], 7, 7);
81 fPaths[4].addRoundRect(fRects[4], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000082 fRectColors[4] = SK_ColorCYAN;
83
bsalomon@google.com574f29d2011-08-29 18:05:05 +000084 SkRegion::Op ops[] = {
85 SkRegion::kDifference_Op,
86 SkRegion::kIntersect_Op,
87 SkRegion::kUnion_Op,
88 SkRegion::kXOR_Op,
89 SkRegion::kReverseDifference_Op,
90 SkRegion::kReplace_Op,
91 };
92
scroggof9d61012014-12-15 12:54:51 -080093 SkRandom r;
bsalomon@google.com574f29d2011-08-29 18:05:05 +000094 for (int i = 0; i < kRows; ++i) {
95 for (int j = 0; j < kCols; ++j) {
96 for (int k = 0; k < 5; ++k) {
97 fOps[j*kRows+i][k] = ops[r.nextU() % SK_ARRAY_COUNT(ops)];
98 }
99 }
100 }
reed@google.com30db5992011-08-29 17:41:02 +0000101 }
102
reed@google.com30db5992011-08-29 17:41:02 +0000103 static const int kRows = 5;
104 static const int kCols = 5;
105 static const int kPadX = 20;
106 static const int kPadY = 20;
107
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000108 static const char* ClipStr(Clip clip) {
109 switch (clip) {
110 case kRect_Clip:
111 return "rect";
112 case kRRect_Clip:
113 return "rrect";
114 case kPath_Clip:
115 return "path";
116 }
117 SkDEBUGFAIL("Unknown clip type.");
118 return "";
119 }
120
reed@google.com45482d12011-08-29 19:02:39 +0000121 virtual SkString onShortName() {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000122 if (kRect_Clip == fClip && !fAntiAlias) {
robertphillips@google.com2470b252012-05-15 17:03:16 +0000123 return SkString("complexclip2");
124 }
125
126 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127 str.printf("complexclip2_%s_%s",
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000128 ClipStr(fClip),
robertphillips@google.com2470b252012-05-15 17:03:16 +0000129 fAntiAlias ? "aa" : "bw");
130 return str;
reed@google.com30db5992011-08-29 17:41:02 +0000131 }
132
reed@google.com45482d12011-08-29 19:02:39 +0000133 virtual SkISize onISize() {
tfarinaf5393182014-06-09 23:59:03 -0700134 return SkISize::Make(SkScalarRoundToInt(fTotalWidth),
135 SkScalarRoundToInt(fTotalHeight));
reed@google.com45482d12011-08-29 19:02:39 +0000136 }
reed@google.com30db5992011-08-29 17:41:02 +0000137
reed@google.com30db5992011-08-29 17:41:02 +0000138 virtual void onDraw(SkCanvas* canvas) {
reed@google.com30db5992011-08-29 17:41:02 +0000139 SkPaint rectPaint;
140 rectPaint.setStyle(SkPaint::kStroke_Style);
141 rectPaint.setStrokeWidth(-1);
142
143 SkPaint fillPaint;
144 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
145
146 for (int i = 0; i < kRows; ++i) {
147 for (int j = 0; j < kCols; ++j) {
148 canvas->save();
robertphillips@google.com2470b252012-05-15 17:03:16 +0000149
reed@google.com30db5992011-08-29 17:41:02 +0000150 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
151 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
robertphillips@google.com2470b252012-05-15 17:03:16 +0000152
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153 // draw the original shapes first so we can see the
robertphillips@google.com2470b252012-05-15 17:03:16 +0000154 // antialiasing on the clipped draw
reed@google.com30db5992011-08-29 17:41:02 +0000155 for (int k = 0; k < 5; ++k) {
156 rectPaint.setColor(fRectColors[k]);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000157 switch (fClip) {
158 case kRect_Clip:
159 canvas->drawRect(fRects[k], rectPaint);
160 break;
161 case kRRect_Clip:
162 canvas->drawRRect(fRRects[k], rectPaint);
163 break;
164 case kPath_Clip:
165 canvas->drawPath(fPaths[k], rectPaint);
166 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000167 }
reed@google.com30db5992011-08-29 17:41:02 +0000168 }
robertphillips@google.com2470b252012-05-15 17:03:16 +0000169
170 for (int k = 0; k < 5; ++k) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000171 switch (fClip) {
172 case kRect_Clip:
173 canvas->clipRect(fRects[k],
174 fOps[j*kRows+i][k],
175 fAntiAlias);
176 break;
177 case kRRect_Clip:
178 canvas->clipRRect(fRRects[k],
179 fOps[j*kRows+i][k],
180 fAntiAlias);
181 break;
182 case kPath_Clip:
183 canvas->clipPath(fPaths[k],
184 fOps[j*kRows+i][k],
185 fAntiAlias);
186 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000187 }
188 }
189 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
reed@google.com30db5992011-08-29 17:41:02 +0000190 canvas->restore();
191 }
192 }
193 }
194private:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000195 Clip fClip;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000196 bool fAntiAlias;
reed@google.com30db5992011-08-29 17:41:02 +0000197 SkRect fRects[5];
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000198 SkRRect fRRects[5];
robertphillips@google.com2470b252012-05-15 17:03:16 +0000199 SkPath fPaths[5];
reed@google.com30db5992011-08-29 17:41:02 +0000200 SkColor fRectColors[5];
201 SkRegion::Op fOps[kRows * kCols][5];
202 SkScalar fWidth;
203 SkScalar fHeight;
204 SkScalar fTotalWidth;
205 SkScalar fTotalHeight;
206
207 typedef GM INHERITED;
208};
209
210//////////////////////////////////////////////////////////////////////////////
211
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000212// bw
213DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, false); )
214DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, false); )
215DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, false); )
reed@google.com30db5992011-08-29 17:41:02 +0000216
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000217// aa
218DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, true); )
219DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, true); )
220DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, true); )
robertphillips@google.com2470b252012-05-15 17:03:16 +0000221
reed@google.com30db5992011-08-29 17:41:02 +0000222}