blob: 4465cd1d01739fae7b49d267bd372f1f1fe885c1 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/utils/SkRandom.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "src/core/SkClipOpPriv.h"
reed@google.com30db5992011-08-29 17:41:02 +000022
23namespace skiagm {
24
25class ComplexClip2GM : public GM {
26public:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000027 enum Clip {
28 kRect_Clip,
29 kRRect_Clip,
30 kPath_Clip
31 };
32
33 ComplexClip2GM(Clip clip, bool antiAlias)
34 : fClip(clip)
robertphillips@google.com2470b252012-05-15 17:03:16 +000035 , fAntiAlias(antiAlias) {
caryclark63c684a2015-02-25 09:04:04 -080036 SkScalar xA = 0.65f;
37 SkScalar xF = 50.65f;
38
39 SkScalar yA = 0.65f;
40 SkScalar yF = 50.65f;
41
42 fWidth = xF - xA;
43 fHeight = yF - yA;
44
45 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
46 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
47 }
48
49protected:
mtklein36352bf2015-03-25 18:17:31 -070050 void onOnceBeforeDraw() override {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000051 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
robertphillips@google.com2470b252012-05-15 17:03:16 +000053 // offset the rects a bit so we get antialiasing even in the rect case
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000054 SkScalar xA = 0.65f;
55 SkScalar xB = 10.65f;
56 SkScalar xC = 20.65f;
57 SkScalar xD = 30.65f;
58 SkScalar xE = 40.65f;
59 SkScalar xF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000060
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000061 SkScalar yA = 0.65f;
62 SkScalar yB = 10.65f;
63 SkScalar yC = 20.65f;
64 SkScalar yD = 30.65f;
65 SkScalar yE = 40.65f;
66 SkScalar yF = 50.65f;
reed@google.com30db5992011-08-29 17:41:02 +000067
Mike Reed92b33352019-08-24 19:39:13 -040068 fRects[0].setLTRB(xB, yB, xE, yE);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000069 fRRects[0].setRectXY(fRects[0], 7, 7);
Mike Reed4241f5e2019-09-14 19:13:23 +000070 fPaths[0].addRoundRect(fRects[0], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000071 fRectColors[0] = SK_ColorRED;
72
Mike Reed92b33352019-08-24 19:39:13 -040073 fRects[1].setLTRB(xA, yA, xD, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000074 fRRects[1].setRectXY(fRects[1], 7, 7);
Mike Reed4241f5e2019-09-14 19:13:23 +000075 fPaths[1].addRoundRect(fRects[1], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000076 fRectColors[1] = SK_ColorGREEN;
77
Mike Reed92b33352019-08-24 19:39:13 -040078 fRects[2].setLTRB(xC, yA, xF, yD);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000079 fRRects[2].setRectXY(fRects[2], 7, 7);
Mike Reed4241f5e2019-09-14 19:13:23 +000080 fPaths[2].addRoundRect(fRects[2], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000081 fRectColors[2] = SK_ColorBLUE;
82
Mike Reed92b33352019-08-24 19:39:13 -040083 fRects[3].setLTRB(xA, yC, xD, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000084 fRRects[3].setRectXY(fRects[3], 7, 7);
Mike Reed4241f5e2019-09-14 19:13:23 +000085 fPaths[3].addRoundRect(fRects[3], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000086 fRectColors[3] = SK_ColorYELLOW;
87
Mike Reed92b33352019-08-24 19:39:13 -040088 fRects[4].setLTRB(xC, yC, xF, yF);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +000089 fRRects[4].setRectXY(fRects[4], 7, 7);
Mike Reed4241f5e2019-09-14 19:13:23 +000090 fPaths[4].addRoundRect(fRects[4], 5, 5);
reed@google.com30db5992011-08-29 17:41:02 +000091 fRectColors[4] = SK_ColorCYAN;
92
Mike Reedc1f77742016-12-09 09:00:50 -050093 const SkClipOp ops[] = {
94 kDifference_SkClipOp,
95 kIntersect_SkClipOp,
bsalomon@google.com574f29d2011-08-29 18:05:05 +000096 };
97
scroggof9d61012014-12-15 12:54:51 -080098 SkRandom r;
bsalomon@google.com574f29d2011-08-29 18:05:05 +000099 for (int i = 0; i < kRows; ++i) {
100 for (int j = 0; j < kCols; ++j) {
101 for (int k = 0; k < 5; ++k) {
102 fOps[j*kRows+i][k] = ops[r.nextU() % SK_ARRAY_COUNT(ops)];
103 }
104 }
105 }
reed@google.com30db5992011-08-29 17:41:02 +0000106 }
107
mtkleindbfd7ab2016-09-01 11:24:54 -0700108 static constexpr int kRows = 5;
109 static constexpr int kCols = 5;
110 static constexpr int kPadX = 20;
111 static constexpr int kPadY = 20;
reed@google.com30db5992011-08-29 17:41:02 +0000112
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000113 static const char* ClipStr(Clip clip) {
114 switch (clip) {
115 case kRect_Clip:
116 return "rect";
117 case kRRect_Clip:
118 return "rrect";
119 case kPath_Clip:
120 return "path";
121 }
122 SkDEBUGFAIL("Unknown clip type.");
123 return "";
124 }
125
mtkleinf0599002015-07-13 06:18:39 -0700126 SkString onShortName() override {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000127 if (kRect_Clip == fClip && !fAntiAlias) {
robertphillips@google.com2470b252012-05-15 17:03:16 +0000128 return SkString("complexclip2");
129 }
130
131 SkString str;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132 str.printf("complexclip2_%s_%s",
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000133 ClipStr(fClip),
robertphillips@google.com2470b252012-05-15 17:03:16 +0000134 fAntiAlias ? "aa" : "bw");
135 return str;
reed@google.com30db5992011-08-29 17:41:02 +0000136 }
137
mtkleinf0599002015-07-13 06:18:39 -0700138 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700139 return SkISize::Make(SkScalarRoundToInt(fTotalWidth),
140 SkScalarRoundToInt(fTotalHeight));
reed@google.com45482d12011-08-29 19:02:39 +0000141 }
reed@google.com30db5992011-08-29 17:41:02 +0000142
mtkleinf0599002015-07-13 06:18:39 -0700143 void onDraw(SkCanvas* canvas) override {
reed@google.com30db5992011-08-29 17:41:02 +0000144 SkPaint rectPaint;
145 rectPaint.setStyle(SkPaint::kStroke_Style);
146 rectPaint.setStrokeWidth(-1);
147
148 SkPaint fillPaint;
149 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
150
151 for (int i = 0; i < kRows; ++i) {
152 for (int j = 0; j < kCols; ++j) {
153 canvas->save();
robertphillips@google.com2470b252012-05-15 17:03:16 +0000154
reed@google.com30db5992011-08-29 17:41:02 +0000155 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
156 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
robertphillips@google.com2470b252012-05-15 17:03:16 +0000157
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158 // draw the original shapes first so we can see the
robertphillips@google.com2470b252012-05-15 17:03:16 +0000159 // antialiasing on the clipped draw
reed@google.com30db5992011-08-29 17:41:02 +0000160 for (int k = 0; k < 5; ++k) {
161 rectPaint.setColor(fRectColors[k]);
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000162 switch (fClip) {
163 case kRect_Clip:
164 canvas->drawRect(fRects[k], rectPaint);
165 break;
166 case kRRect_Clip:
167 canvas->drawRRect(fRRects[k], rectPaint);
168 break;
169 case kPath_Clip:
170 canvas->drawPath(fPaths[k], rectPaint);
171 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000172 }
reed@google.com30db5992011-08-29 17:41:02 +0000173 }
robertphillips@google.com2470b252012-05-15 17:03:16 +0000174
175 for (int k = 0; k < 5; ++k) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000176 switch (fClip) {
177 case kRect_Clip:
178 canvas->clipRect(fRects[k],
179 fOps[j*kRows+i][k],
180 fAntiAlias);
181 break;
182 case kRRect_Clip:
183 canvas->clipRRect(fRRects[k],
184 fOps[j*kRows+i][k],
185 fAntiAlias);
186 break;
187 case kPath_Clip:
188 canvas->clipPath(fPaths[k],
189 fOps[j*kRows+i][k],
190 fAntiAlias);
191 break;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000192 }
193 }
194 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
reed@google.com30db5992011-08-29 17:41:02 +0000195 canvas->restore();
196 }
197 }
198 }
199private:
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000200 Clip fClip;
robertphillips@google.com2470b252012-05-15 17:03:16 +0000201 bool fAntiAlias;
reed@google.com30db5992011-08-29 17:41:02 +0000202 SkRect fRects[5];
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000203 SkRRect fRRects[5];
robertphillips@google.com2470b252012-05-15 17:03:16 +0000204 SkPath fPaths[5];
reed@google.com30db5992011-08-29 17:41:02 +0000205 SkColor fRectColors[5];
Mike Reedc1f77742016-12-09 09:00:50 -0500206 SkClipOp fOps[kRows * kCols][5];
reed@google.com30db5992011-08-29 17:41:02 +0000207 SkScalar fWidth;
208 SkScalar fHeight;
209 SkScalar fTotalWidth;
210 SkScalar fTotalHeight;
211
212 typedef GM INHERITED;
213};
214
215//////////////////////////////////////////////////////////////////////////////
216
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000217// bw
218DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, false); )
219DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, false); )
220DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, false); )
reed@google.com30db5992011-08-29 17:41:02 +0000221
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000222// aa
223DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, true); )
224DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, true); )
225DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, true); )
robertphillips@google.com2470b252012-05-15 17:03:16 +0000226
reed@google.com30db5992011-08-29 17:41:02 +0000227}