blob: 71e07c92210624d364b986301d75ae376a50078c [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"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkRect.h"
14#include "SkRRect.h"
reed@google.com30db5992011-08-29 17:41:02 +000015
16namespace skiagm {
17
18class ComplexClip2GM : public GM {
19public:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000020 enum Clip {
21 kRect_Clip,
22 kRRect_Clip,
23 kPath_Clip
24 };
25
26 ComplexClip2GM(Clip clip, bool antiAlias)
27 : fClip(clip)
robertphillips@google.com2470b252012-05-15 17:03:16 +000028 , fAntiAlias(antiAlias) {
caryclark63c684a2015-02-25 09:04:04 -080029 SkScalar xA = 0.65f;
30 SkScalar xF = 50.65f;
31
32 SkScalar yA = 0.65f;
33 SkScalar yF = 50.65f;
34
35 fWidth = xF - xA;
36 fHeight = yF - yA;
37
38 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
39 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
40 }
41
42protected:
mtklein36352bf2015-03-25 18:17:31 -070043 void onOnceBeforeDraw() override {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000044 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
rmistry@google.comae933ce2012-08-23 18:19:56 +000045
robertphillips@google.com2470b252012-05-15 17:03:16 +000046 // offset the rects a bit so we get antialiasing even in the rect case
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000047 SkScalar xA = 0.65f;
48 SkScalar xB = 10.65f;
49 SkScalar xC = 20.65f;
50 SkScalar xD = 30.65f;
51 SkScalar xE = 40.65f;
52 SkScalar xF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000053
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000054 SkScalar yA = 0.65f;
55 SkScalar yB = 10.65f;
56 SkScalar yC = 20.65f;
57 SkScalar yD = 30.65f;
58 SkScalar yE = 40.65f;
59 SkScalar yF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000060
bsalomon@google.com574f29d2011-08-29 18:05:05 +000061 fRects[0].set(xB, yB, xE, yE);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000062 fRRects[0].setRectXY(fRects[0], 7, 7);
63 fPaths[0].addRoundRect(fRects[0], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000064 fRectColors[0] = SK_ColorRED;
65
bsalomon@google.com574f29d2011-08-29 18:05:05 +000066 fRects[1].set(xA, yA, xD, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000067 fRRects[1].setRectXY(fRects[1], 7, 7);
68 fPaths[1].addRoundRect(fRects[1], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000069 fRectColors[1] = SK_ColorGREEN;
70
bsalomon@google.com574f29d2011-08-29 18:05:05 +000071 fRects[2].set(xC, yA, xF, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000072 fRRects[2].setRectXY(fRects[2], 7, 7);
73 fPaths[2].addRoundRect(fRects[2], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000074 fRectColors[2] = SK_ColorBLUE;
75
bsalomon@google.com574f29d2011-08-29 18:05:05 +000076 fRects[3].set(xA, yC, xD, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000077 fRRects[3].setRectXY(fRects[3], 7, 7);
78 fPaths[3].addRoundRect(fRects[3], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000079 fRectColors[3] = SK_ColorYELLOW;
80
bsalomon@google.com574f29d2011-08-29 18:05:05 +000081 fRects[4].set(xC, yC, xF, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000082 fRRects[4].setRectXY(fRects[4], 7, 7);
83 fPaths[4].addRoundRect(fRects[4], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000084 fRectColors[4] = SK_ColorCYAN;
85
Mike Reedc1f77742016-12-09 09:00:50 -050086 const SkClipOp ops[] = {
87 kDifference_SkClipOp,
88 kIntersect_SkClipOp,
89 kUnion_SkClipOp,
90 kXOR_SkClipOp,
91 kReverseDifference_SkClipOp,
92 kReplace_SkClipOp,
bsalomon@google.com574f29d2011-08-29 18:05:05 +000093 };
94
scroggof9d61012014-12-15 12:54:51 -080095 SkRandom r;
bsalomon@google.com574f29d2011-08-29 18:05:05 +000096 for (int i = 0; i < kRows; ++i) {
97 for (int j = 0; j < kCols; ++j) {
98 for (int k = 0; k < 5; ++k) {
99 fOps[j*kRows+i][k] = ops[r.nextU() % SK_ARRAY_COUNT(ops)];
100 }
101 }
102 }
reed@google.com30db5992011-08-29 17:41:02 +0000103 }
104
mtkleindbfd7ab2016-09-01 11:24:54 -0700105 static constexpr int kRows = 5;
106 static constexpr int kCols = 5;
107 static constexpr int kPadX = 20;
108 static constexpr int kPadY = 20;
reed@google.com30db5992011-08-29 17:41:02 +0000109
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000110 static const char* ClipStr(Clip clip) {
111 switch (clip) {
112 case kRect_Clip:
113 return "rect";
114 case kRRect_Clip:
115 return "rrect";
116 case kPath_Clip:
117 return "path";
118 }
119 SkDEBUGFAIL("Unknown clip type.");
120 return "";
121 }
122
mtkleinf0599002015-07-13 06:18:39 -0700123 SkString onShortName() override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000124 if (kRect_Clip == fClip && !fAntiAlias) {
robertphillips@google.com2470b252012-05-15 17:03:16 +0000125 return SkString("complexclip2");
126 }
127
128 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129 str.printf("complexclip2_%s_%s",
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000130 ClipStr(fClip),
robertphillips@google.com2470b252012-05-15 17:03:16 +0000131 fAntiAlias ? "aa" : "bw");
132 return str;
reed@google.com30db5992011-08-29 17:41:02 +0000133 }
134
mtkleinf0599002015-07-13 06:18:39 -0700135 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700136 return SkISize::Make(SkScalarRoundToInt(fTotalWidth),
137 SkScalarRoundToInt(fTotalHeight));
reed@google.com45482d12011-08-29 19:02:39 +0000138 }
reed@google.com30db5992011-08-29 17:41:02 +0000139
mtkleinf0599002015-07-13 06:18:39 -0700140 void onDraw(SkCanvas* canvas) override {
reed@google.com30db5992011-08-29 17:41:02 +0000141 SkPaint rectPaint;
142 rectPaint.setStyle(SkPaint::kStroke_Style);
143 rectPaint.setStrokeWidth(-1);
144
145 SkPaint fillPaint;
146 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
147
148 for (int i = 0; i < kRows; ++i) {
149 for (int j = 0; j < kCols; ++j) {
150 canvas->save();
robertphillips@google.com2470b252012-05-15 17:03:16 +0000151
reed@google.com30db5992011-08-29 17:41:02 +0000152 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
153 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
robertphillips@google.com2470b252012-05-15 17:03:16 +0000154
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155 // draw the original shapes first so we can see the
robertphillips@google.com2470b252012-05-15 17:03:16 +0000156 // antialiasing on the clipped draw
reed@google.com30db5992011-08-29 17:41:02 +0000157 for (int k = 0; k < 5; ++k) {
158 rectPaint.setColor(fRectColors[k]);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000159 switch (fClip) {
160 case kRect_Clip:
161 canvas->drawRect(fRects[k], rectPaint);
162 break;
163 case kRRect_Clip:
164 canvas->drawRRect(fRRects[k], rectPaint);
165 break;
166 case kPath_Clip:
167 canvas->drawPath(fPaths[k], rectPaint);
168 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000169 }
reed@google.com30db5992011-08-29 17:41:02 +0000170 }
robertphillips@google.com2470b252012-05-15 17:03:16 +0000171
172 for (int k = 0; k < 5; ++k) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000173 switch (fClip) {
174 case kRect_Clip:
175 canvas->clipRect(fRects[k],
176 fOps[j*kRows+i][k],
177 fAntiAlias);
178 break;
179 case kRRect_Clip:
180 canvas->clipRRect(fRRects[k],
181 fOps[j*kRows+i][k],
182 fAntiAlias);
183 break;
184 case kPath_Clip:
185 canvas->clipPath(fPaths[k],
186 fOps[j*kRows+i][k],
187 fAntiAlias);
188 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000189 }
190 }
191 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
reed@google.com30db5992011-08-29 17:41:02 +0000192 canvas->restore();
193 }
194 }
195 }
196private:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000197 Clip fClip;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000198 bool fAntiAlias;
reed@google.com30db5992011-08-29 17:41:02 +0000199 SkRect fRects[5];
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000200 SkRRect fRRects[5];
robertphillips@google.com2470b252012-05-15 17:03:16 +0000201 SkPath fPaths[5];
reed@google.com30db5992011-08-29 17:41:02 +0000202 SkColor fRectColors[5];
Mike Reedc1f77742016-12-09 09:00:50 -0500203 SkClipOp fOps[kRows * kCols][5];
reed@google.com30db5992011-08-29 17:41:02 +0000204 SkScalar fWidth;
205 SkScalar fHeight;
206 SkScalar fTotalWidth;
207 SkScalar fTotalHeight;
208
209 typedef GM INHERITED;
210};
211
212//////////////////////////////////////////////////////////////////////////////
213
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000214// bw
215DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, false); )
216DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, false); )
217DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, false); )
reed@google.com30db5992011-08-29 17:41:02 +0000218
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000219// aa
220DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, true); )
221DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, true); )
222DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, true); )
robertphillips@google.com2470b252012-05-15 17:03:16 +0000223
reed@google.com30db5992011-08-29 17:41:02 +0000224}