blob: a55d0733e073de417927ddddba3e9e1ed67f783c [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) {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000027 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
rmistry@google.comae933ce2012-08-23 18:19:56 +000028
robertphillips@google.com2470b252012-05-15 17:03:16 +000029 // offset the rects a bit so we get antialiasing even in the rect case
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000030 SkScalar xA = 0.65f;
31 SkScalar xB = 10.65f;
32 SkScalar xC = 20.65f;
33 SkScalar xD = 30.65f;
34 SkScalar xE = 40.65f;
35 SkScalar xF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000036
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000037 SkScalar yA = 0.65f;
38 SkScalar yB = 10.65f;
39 SkScalar yC = 20.65f;
40 SkScalar yD = 30.65f;
41 SkScalar yE = 40.65f;
42 SkScalar yF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000043
44 fWidth = xF - xA;
45 fHeight = yF - yA;
46
bsalomon@google.com574f29d2011-08-29 18:05:05 +000047 fRects[0].set(xB, yB, xE, yE);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000048 fRRects[0].setRectXY(fRects[0], 7, 7);
49 fPaths[0].addRoundRect(fRects[0], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000050 fRectColors[0] = SK_ColorRED;
51
bsalomon@google.com574f29d2011-08-29 18:05:05 +000052 fRects[1].set(xA, yA, xD, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000053 fRRects[1].setRectXY(fRects[1], 7, 7);
54 fPaths[1].addRoundRect(fRects[1], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000055 fRectColors[1] = SK_ColorGREEN;
56
bsalomon@google.com574f29d2011-08-29 18:05:05 +000057 fRects[2].set(xC, yA, xF, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000058 fRRects[2].setRectXY(fRects[2], 7, 7);
59 fPaths[2].addRoundRect(fRects[2], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000060 fRectColors[2] = SK_ColorBLUE;
61
bsalomon@google.com574f29d2011-08-29 18:05:05 +000062 fRects[3].set(xA, yC, xD, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000063 fRRects[3].setRectXY(fRects[3], 7, 7);
64 fPaths[3].addRoundRect(fRects[3], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000065 fRectColors[3] = SK_ColorYELLOW;
66
bsalomon@google.com574f29d2011-08-29 18:05:05 +000067 fRects[4].set(xC, yC, xF, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000068 fRRects[4].setRectXY(fRects[4], 7, 7);
69 fPaths[4].addRoundRect(fRects[4], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000070 fRectColors[4] = SK_ColorCYAN;
71
72 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
73 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
bsalomon@google.com574f29d2011-08-29 18:05:05 +000074
75 SkRegion::Op ops[] = {
76 SkRegion::kDifference_Op,
77 SkRegion::kIntersect_Op,
78 SkRegion::kUnion_Op,
79 SkRegion::kXOR_Op,
80 SkRegion::kReverseDifference_Op,
81 SkRegion::kReplace_Op,
82 };
83
scroggof9d61012014-12-15 12:54:51 -080084 SkRandom r;
bsalomon@google.com574f29d2011-08-29 18:05:05 +000085 for (int i = 0; i < kRows; ++i) {
86 for (int j = 0; j < kCols; ++j) {
87 for (int k = 0; k < 5; ++k) {
88 fOps[j*kRows+i][k] = ops[r.nextU() % SK_ARRAY_COUNT(ops)];
89 }
90 }
91 }
reed@google.com30db5992011-08-29 17:41:02 +000092 }
93
reed@google.com30db5992011-08-29 17:41:02 +000094protected:
95
96 static const int kRows = 5;
97 static const int kCols = 5;
98 static const int kPadX = 20;
99 static const int kPadY = 20;
100
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000101 static const char* ClipStr(Clip clip) {
102 switch (clip) {
103 case kRect_Clip:
104 return "rect";
105 case kRRect_Clip:
106 return "rrect";
107 case kPath_Clip:
108 return "path";
109 }
110 SkDEBUGFAIL("Unknown clip type.");
111 return "";
112 }
113
reed@google.com45482d12011-08-29 19:02:39 +0000114 virtual SkString onShortName() {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000115 if (kRect_Clip == fClip && !fAntiAlias) {
robertphillips@google.com2470b252012-05-15 17:03:16 +0000116 return SkString("complexclip2");
117 }
118
119 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120 str.printf("complexclip2_%s_%s",
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000121 ClipStr(fClip),
robertphillips@google.com2470b252012-05-15 17:03:16 +0000122 fAntiAlias ? "aa" : "bw");
123 return str;
reed@google.com30db5992011-08-29 17:41:02 +0000124 }
125
reed@google.com45482d12011-08-29 19:02:39 +0000126 virtual SkISize onISize() {
tfarinaf5393182014-06-09 23:59:03 -0700127 return SkISize::Make(SkScalarRoundToInt(fTotalWidth),
128 SkScalarRoundToInt(fTotalHeight));
reed@google.com45482d12011-08-29 19:02:39 +0000129 }
reed@google.com30db5992011-08-29 17:41:02 +0000130
reed@google.com30db5992011-08-29 17:41:02 +0000131 virtual void onDraw(SkCanvas* canvas) {
reed@google.com30db5992011-08-29 17:41:02 +0000132 SkPaint rectPaint;
133 rectPaint.setStyle(SkPaint::kStroke_Style);
134 rectPaint.setStrokeWidth(-1);
135
136 SkPaint fillPaint;
137 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
138
139 for (int i = 0; i < kRows; ++i) {
140 for (int j = 0; j < kCols; ++j) {
141 canvas->save();
robertphillips@google.com2470b252012-05-15 17:03:16 +0000142
reed@google.com30db5992011-08-29 17:41:02 +0000143 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
144 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
robertphillips@google.com2470b252012-05-15 17:03:16 +0000145
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146 // draw the original shapes first so we can see the
robertphillips@google.com2470b252012-05-15 17:03:16 +0000147 // antialiasing on the clipped draw
reed@google.com30db5992011-08-29 17:41:02 +0000148 for (int k = 0; k < 5; ++k) {
149 rectPaint.setColor(fRectColors[k]);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000150 switch (fClip) {
151 case kRect_Clip:
152 canvas->drawRect(fRects[k], rectPaint);
153 break;
154 case kRRect_Clip:
155 canvas->drawRRect(fRRects[k], rectPaint);
156 break;
157 case kPath_Clip:
158 canvas->drawPath(fPaths[k], rectPaint);
159 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000160 }
reed@google.com30db5992011-08-29 17:41:02 +0000161 }
robertphillips@google.com2470b252012-05-15 17:03:16 +0000162
163 for (int k = 0; k < 5; ++k) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000164 switch (fClip) {
165 case kRect_Clip:
166 canvas->clipRect(fRects[k],
167 fOps[j*kRows+i][k],
168 fAntiAlias);
169 break;
170 case kRRect_Clip:
171 canvas->clipRRect(fRRects[k],
172 fOps[j*kRows+i][k],
173 fAntiAlias);
174 break;
175 case kPath_Clip:
176 canvas->clipPath(fPaths[k],
177 fOps[j*kRows+i][k],
178 fAntiAlias);
179 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000180 }
181 }
182 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
reed@google.com30db5992011-08-29 17:41:02 +0000183 canvas->restore();
184 }
185 }
186 }
187private:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000188 Clip fClip;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000189 bool fAntiAlias;
reed@google.com30db5992011-08-29 17:41:02 +0000190 SkRect fRects[5];
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000191 SkRRect fRRects[5];
robertphillips@google.com2470b252012-05-15 17:03:16 +0000192 SkPath fPaths[5];
reed@google.com30db5992011-08-29 17:41:02 +0000193 SkColor fRectColors[5];
194 SkRegion::Op fOps[kRows * kCols][5];
195 SkScalar fWidth;
196 SkScalar fHeight;
197 SkScalar fTotalWidth;
198 SkScalar fTotalHeight;
199
200 typedef GM INHERITED;
201};
202
203//////////////////////////////////////////////////////////////////////////////
204
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000205// bw
206DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, false); )
207DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, false); )
208DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, false); )
reed@google.com30db5992011-08-29 17:41:02 +0000209
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000210// aa
211DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, true); )
212DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, true); )
213DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, true); )
robertphillips@google.com2470b252012-05-15 17:03:16 +0000214
reed@google.com30db5992011-08-29 17:41:02 +0000215}