Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
complexclip2.cpp
Go to the documentation of this file.
1/*
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 */
7
8#include "gm/gm.h"
13#include "include/core/SkPath.h"
15#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
20#include "src/base/SkRandom.h"
21
22namespace skiagm {
23
24class ComplexClip2GM : public GM {
25public:
31
32 ComplexClip2GM(Clip clip, bool antiAlias)
33 : fClip(clip)
34 , fAntiAlias(antiAlias) {
35 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:
49 void onOnceBeforeDraw() override {
50 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
51
52 // offset the rects a bit so we get antialiasing even in the rect case
53 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;
59
60 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;
66
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;
71
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;
76
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;
81
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;
86
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;
91
92 const SkClipOp ops[] = {
95 };
96
97 SkRandom r;
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() % std::size(ops)];
102 }
103 }
104 }
105 }
106
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;
111
112 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
125 SkString getName() const override {
126 if (kRect_Clip == fClip && !fAntiAlias) {
127 return SkString("complexclip2");
128 }
129
130 SkString str;
131 str.printf("complexclip2_%s_%s",
132 ClipStr(fClip),
133 fAntiAlias ? "aa" : "bw");
134 return str;
135 }
136
137 SkISize getISize() override {
138 return SkISize::Make(SkScalarRoundToInt(fTotalWidth),
139 SkScalarRoundToInt(fTotalHeight));
140 }
141
142 void onDraw(SkCanvas* canvas) override {
143 SkPaint rectPaint;
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();
153
154 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
155 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
156
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]);
161 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;
171 }
172 }
173
174 for (int k = 0; k < 5; ++k) {
175 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;
191 }
192 }
193 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
194 canvas->restore();
195 }
196 }
197 }
198private:
199 Clip fClip;
200 bool fAntiAlias;
201 SkRect fRects[5];
202 SkRRect fRRects[5];
203 SkPath fPaths[5];
204 SkColor fRectColors[5];
205 SkClipOp fOps[kRows * kCols][5];
206 SkScalar fWidth;
207 SkScalar fHeight;
208 SkScalar fTotalWidth;
209 SkScalar fTotalHeight;
210
211 using INHERITED = GM;
212};
213
214//////////////////////////////////////////////////////////////////////////////
215
216// 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); )
220
221// 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); )
225
226} // namespace skiagm
SkPathOp ops[]
#define SkDEBUGFAIL(message)
Definition SkAssert.h:118
SkClipOp
Definition SkClipOp.h:13
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
uint32_t SkColor
Definition SkColor.h:37
#define SkColorSetRGB(r, g, b)
Definition SkColor.h:57
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
#define SK_Scalar1
Definition SkScalar.h:18
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawRRect(const SkRRect &rrect, const SkPaint &paint)
void clipPath(const SkPath &path, SkClipOp op, bool doAntiAlias)
int save()
Definition SkCanvas.cpp:451
void drawPath(const SkPath &path, const SkPaint &paint)
void clipRRect(const SkRRect &rrect, SkClipOp op, bool doAntiAlias)
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
static SkPath RRect(const SkRRect &, SkPathDirection dir=SkPathDirection::kCW)
Definition SkPath.cpp:3534
void setRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition SkRRect.cpp:52
uint32_t nextU()
Definition SkRandom.h:42
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:534
static constexpr int kCols
static constexpr int kPadY
static constexpr int kRows
static const char * ClipStr(Clip clip)
static constexpr int kPadX
SkString getName() const override
void onOnceBeforeDraw() override
SkISize getISize() override
ComplexClip2GM(Clip clip, bool antiAlias)
void onDraw(SkCanvas *canvas) override
void setBGColor(SkColor)
Definition gm.cpp:159
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
void setLTRB(float left, float top, float right, float bottom)
Definition SkRect.h:865
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609