blob: 2bc4145dca4786a3dc061aaeaf5ce4ae38444944 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkClipOp.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkPath.h"
14#include "include/core/SkRRect.h"
15#include "include/core/SkRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTypes.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050020#include "src/base/SkRandom.h"
reed@google.com30db5992011-08-29 17:41:02 +000021
22namespace skiagm {
23
24class ComplexClip2GM : public GM {
25public:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000026 enum Clip {
27 kRect_Clip,
28 kRRect_Clip,
29 kPath_Clip
30 };
31
32 ComplexClip2GM(Clip clip, bool antiAlias)
33 : fClip(clip)
robertphillips@google.com2470b252012-05-15 17:03:16 +000034 , fAntiAlias(antiAlias) {
caryclark63c684a2015-02-25 09:04:04 -080035 SkScalar xA = 0.65f;
36 SkScalar xF = 50.65f;
37
38 SkScalar yA = 0.65f;
39 SkScalar yF = 50.65f;
40
41 fWidth = xF - xA;
42 fHeight = yF - yA;
43
44 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
45 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
46 }
47
48protected:
mtklein36352bf2015-03-25 18:17:31 -070049 void onOnceBeforeDraw() override {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000050 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
robertphillips@google.com2470b252012-05-15 17:03:16 +000052 // offset the rects a bit so we get antialiasing even in the rect case
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000053 SkScalar xA = 0.65f;
54 SkScalar xB = 10.65f;
55 SkScalar xC = 20.65f;
56 SkScalar xD = 30.65f;
57 SkScalar xE = 40.65f;
58 SkScalar xF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000059
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000060 SkScalar yA = 0.65f;
61 SkScalar yB = 10.65f;
62 SkScalar yC = 20.65f;
63 SkScalar yD = 30.65f;
64 SkScalar yE = 40.65f;
65 SkScalar yF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000066
Mike Reed92b33352019-08-24 19:39:13 -040067 fRects[0].setLTRB(xB, yB, xE, yE);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000068 fRRects[0].setRectXY(fRects[0], 7, 7);
Mike Reed58cc97a2020-08-24 15:15:01 -040069 fPaths[0] = SkPath::RRect(fRects[0], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000070 fRectColors[0] = SK_ColorRED;
71
Mike Reed92b33352019-08-24 19:39:13 -040072 fRects[1].setLTRB(xA, yA, xD, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000073 fRRects[1].setRectXY(fRects[1], 7, 7);
Mike Reed58cc97a2020-08-24 15:15:01 -040074 fPaths[1] = SkPath::RRect(fRects[1], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000075 fRectColors[1] = SK_ColorGREEN;
76
Mike Reed92b33352019-08-24 19:39:13 -040077 fRects[2].setLTRB(xC, yA, xF, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000078 fRRects[2].setRectXY(fRects[2], 7, 7);
Mike Reed58cc97a2020-08-24 15:15:01 -040079 fPaths[2] = SkPath::RRect(fRects[2], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000080 fRectColors[2] = SK_ColorBLUE;
81
Mike Reed92b33352019-08-24 19:39:13 -040082 fRects[3].setLTRB(xA, yC, xD, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000083 fRRects[3].setRectXY(fRects[3], 7, 7);
Mike Reed58cc97a2020-08-24 15:15:01 -040084 fPaths[3] = SkPath::RRect(fRects[3], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000085 fRectColors[3] = SK_ColorYELLOW;
86
Mike Reed92b33352019-08-24 19:39:13 -040087 fRects[4].setLTRB(xC, yC, xF, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000088 fRRects[4].setRectXY(fRects[4], 7, 7);
Mike Reed58cc97a2020-08-24 15:15:01 -040089 fPaths[4] = SkPath::RRect(fRects[4], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000090 fRectColors[4] = SK_ColorCYAN;
91
Mike Reedc1f77742016-12-09 09:00:50 -050092 const SkClipOp ops[] = {
Michael Ludwig2f6e2f82021-08-03 13:08:50 -040093 SkClipOp::kDifference,
94 SkClipOp::kIntersect,
bsalomon@google.com574f29d2011-08-29 18:05:05 +000095 };
96
scroggof9d61012014-12-15 12:54:51 -080097 SkRandom r;
bsalomon@google.com574f29d2011-08-29 18:05:05 +000098 for (int i = 0; i < kRows; ++i) {
99 for (int j = 0; j < kCols; ++j) {
100 for (int k = 0; k < 5; ++k) {
Herb Derbyc37b3862022-06-21 09:49:17 -0400101 fOps[j*kRows+i][k] = ops[r.nextU() % std::size(ops)];
bsalomon@google.com574f29d2011-08-29 18:05:05 +0000102 }
103 }
104 }
reed@google.com30db5992011-08-29 17:41:02 +0000105 }
106
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400107 inline static constexpr int kRows = 5;
108 inline static constexpr int kCols = 5;
109 inline static constexpr int kPadX = 20;
110 inline static constexpr int kPadY = 20;
reed@google.com30db5992011-08-29 17:41:02 +0000111
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000112 static const char* ClipStr(Clip clip) {
113 switch (clip) {
114 case kRect_Clip:
115 return "rect";
116 case kRRect_Clip:
117 return "rrect";
118 case kPath_Clip:
119 return "path";
120 }
121 SkDEBUGFAIL("Unknown clip type.");
122 return "";
123 }
124
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000125 SkString getName() const override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000126 if (kRect_Clip == fClip && !fAntiAlias) {
robertphillips@google.com2470b252012-05-15 17:03:16 +0000127 return SkString("complexclip2");
128 }
129
130 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131 str.printf("complexclip2_%s_%s",
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000132 ClipStr(fClip),
robertphillips@google.com2470b252012-05-15 17:03:16 +0000133 fAntiAlias ? "aa" : "bw");
134 return str;
reed@google.com30db5992011-08-29 17:41:02 +0000135 }
136
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000137 SkISize getISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700138 return SkISize::Make(SkScalarRoundToInt(fTotalWidth),
139 SkScalarRoundToInt(fTotalHeight));
reed@google.com45482d12011-08-29 19:02:39 +0000140 }
reed@google.com30db5992011-08-29 17:41:02 +0000141
mtkleinf0599002015-07-13 06:18:39 -0700142 void onDraw(SkCanvas* canvas) override {
reed@google.com30db5992011-08-29 17:41:02 +0000143 SkPaint rectPaint;
144 rectPaint.setStyle(SkPaint::kStroke_Style);
145 rectPaint.setStrokeWidth(-1);
146
147 SkPaint fillPaint;
148 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
149
150 for (int i = 0; i < kRows; ++i) {
151 for (int j = 0; j < kCols; ++j) {
152 canvas->save();
robertphillips@google.com2470b252012-05-15 17:03:16 +0000153
reed@google.com30db5992011-08-29 17:41:02 +0000154 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
155 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
robertphillips@google.com2470b252012-05-15 17:03:16 +0000156
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157 // draw the original shapes first so we can see the
robertphillips@google.com2470b252012-05-15 17:03:16 +0000158 // antialiasing on the clipped draw
reed@google.com30db5992011-08-29 17:41:02 +0000159 for (int k = 0; k < 5; ++k) {
160 rectPaint.setColor(fRectColors[k]);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000161 switch (fClip) {
162 case kRect_Clip:
163 canvas->drawRect(fRects[k], rectPaint);
164 break;
165 case kRRect_Clip:
166 canvas->drawRRect(fRRects[k], rectPaint);
167 break;
168 case kPath_Clip:
169 canvas->drawPath(fPaths[k], rectPaint);
170 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000171 }
reed@google.com30db5992011-08-29 17:41:02 +0000172 }
robertphillips@google.com2470b252012-05-15 17:03:16 +0000173
174 for (int k = 0; k < 5; ++k) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000175 switch (fClip) {
176 case kRect_Clip:
177 canvas->clipRect(fRects[k],
178 fOps[j*kRows+i][k],
179 fAntiAlias);
180 break;
181 case kRRect_Clip:
182 canvas->clipRRect(fRRects[k],
183 fOps[j*kRows+i][k],
184 fAntiAlias);
185 break;
186 case kPath_Clip:
187 canvas->clipPath(fPaths[k],
188 fOps[j*kRows+i][k],
189 fAntiAlias);
190 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000191 }
192 }
193 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
reed@google.com30db5992011-08-29 17:41:02 +0000194 canvas->restore();
195 }
196 }
197 }
198private:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000199 Clip fClip;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000200 bool fAntiAlias;
reed@google.com30db5992011-08-29 17:41:02 +0000201 SkRect fRects[5];
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000202 SkRRect fRRects[5];
robertphillips@google.com2470b252012-05-15 17:03:16 +0000203 SkPath fPaths[5];
reed@google.com30db5992011-08-29 17:41:02 +0000204 SkColor fRectColors[5];
Mike Reedc1f77742016-12-09 09:00:50 -0500205 SkClipOp fOps[kRows * kCols][5];
reed@google.com30db5992011-08-29 17:41:02 +0000206 SkScalar fWidth;
207 SkScalar fHeight;
208 SkScalar fTotalWidth;
209 SkScalar fTotalHeight;
210
John Stiles7571f9e2020-09-02 22:42:33 -0400211 using INHERITED = GM;
reed@google.com30db5992011-08-29 17:41:02 +0000212};
213
214//////////////////////////////////////////////////////////////////////////////
215
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000216// bw
217DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, false); )
218DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, false); )
219DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, false); )
reed@google.com30db5992011-08-29 17:41:02 +0000220
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000221// aa
222DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, true); )
223DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, true); )
224DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, true); )
robertphillips@google.com2470b252012-05-15 17:03:16 +0000225
John Stilesa6841be2020-08-06 14:11:56 -0400226} // namespace skiagm