2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkClipOp.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkRRect.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypes.h"
20 #include "include/utils/SkRandom.h"
24 class ComplexClip2GM : public GM {
32 ComplexClip2GM(Clip clip, bool antiAlias)
34 , fAntiAlias(antiAlias) {
44 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
45 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
49 void onOnceBeforeDraw() override {
50 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
52 // offset the rects a bit so we get antialiasing even in the rect case
67 fRects[0].setLTRB(xB, yB, xE, yE);
68 fRRects[0].setRectXY(fRects[0], 7, 7);
69 fPaths[0] = SkPath::RRect(fRects[0], 5, 5);
70 fRectColors[0] = SK_ColorRED;
72 fRects[1].setLTRB(xA, yA, xD, yD);
73 fRRects[1].setRectXY(fRects[1], 7, 7);
74 fPaths[1] = SkPath::RRect(fRects[1], 5, 5);
75 fRectColors[1] = SK_ColorGREEN;
77 fRects[2].setLTRB(xC, yA, xF, yD);
78 fRRects[2].setRectXY(fRects[2], 7, 7);
79 fPaths[2] = SkPath::RRect(fRects[2], 5, 5);
80 fRectColors[2] = SK_ColorBLUE;
82 fRects[3].setLTRB(xA, yC, xD, yF);
83 fRRects[3].setRectXY(fRects[3], 7, 7);
84 fPaths[3] = SkPath::RRect(fRects[3], 5, 5);
85 fRectColors[3] = SK_ColorYELLOW;
87 fRects[4].setLTRB(xC, yC, xF, yF);
88 fRRects[4].setRectXY(fRects[4], 7, 7);
89 fPaths[4] = SkPath::RRect(fRects[4], 5, 5);
90 fRectColors[4] = SK_ColorCYAN;
92 const SkClipOp ops[] = {
93 SkClipOp::kDifference,
98 for (int i = 0; i < kRows; ++i) {
99 for (int j = 0; j < kCols; ++j) {
100 for (int k = 0; k < 5; ++k) {
101 fOps[j*kRows+i][k] = ops[r.nextU() % SK_ARRAY_COUNT(ops)];
107 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;
112 static const char* ClipStr(Clip clip) {
121 SkDEBUGFAIL("Unknown clip type.");
125 SkString onShortName() override {
126 if (kRect_Clip == fClip && !fAntiAlias) {
127 return SkString("complexclip2");
131 str.printf("complexclip2_%s_%s",
133 fAntiAlias ? "aa" : "bw");
137 SkISize onISize() override {
138 return SkISize::Make(SkScalarRoundToInt(fTotalWidth),
139 SkScalarRoundToInt(fTotalHeight));
142 void onDraw(SkCanvas* canvas) override {
144 rectPaint.setStyle(SkPaint::kStroke_Style);
145 rectPaint.setStrokeWidth(-1);
148 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
150 for (int i = 0; i < kRows; ++i) {
151 for (int j = 0; j < kCols; ++j) {
154 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
155 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
157 // draw the original shapes first so we can see the
158 // antialiasing on the clipped draw
159 for (int k = 0; k < 5; ++k) {
160 rectPaint.setColor(fRectColors[k]);
163 canvas->drawRect(fRects[k], rectPaint);
166 canvas->drawRRect(fRRects[k], rectPaint);
169 canvas->drawPath(fPaths[k], rectPaint);
174 for (int k = 0; k < 5; ++k) {
177 canvas->clipRect(fRects[k],
182 canvas->clipRRect(fRRects[k],
187 canvas->clipPath(fPaths[k],
193 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
204 SkColor fRectColors[5];
205 SkClipOp fOps[kRows * kCols][5];
208 SkScalar fTotalWidth;
209 SkScalar fTotalHeight;
211 using INHERITED = GM;
214 //////////////////////////////////////////////////////////////////////////////
217 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, false); )
218 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, false); )
219 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, false); )
222 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, true); )
223 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, true); )
224 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, true); )
226 } // namespace skiagm