OLD | NEW |
---|---|
(Empty) | |
1 | |
2 /* | |
3 * Copyright 2015 Google Inc. | |
4 * | |
5 * Use of this source code is governed by a BSD-style license that can be | |
6 * found in the LICENSE file. | |
7 */ | |
8 #include "gm.h" | |
9 #include "SkArithmeticMode.h" | |
10 #include "SkShader.h" | |
11 #include "SkXfermode.h" | |
12 | |
13 enum { | |
14 kXfermodeCount = SkXfermode::kLastMode + 2, // All xfermodes plus arithmetic mode. | |
15 kShapeSize = 22, | |
16 kShapeSpacing = 36, | |
17 kShapeTypeSpacing = 4 * kShapeSpacing/3, | |
18 kPaintSpacing = 2 * kShapeSpacing, | |
19 kPadding = (kPaintSpacing - kShapeSpacing) / 2, | |
20 | |
21 kPaintWidth = 3*kShapeSpacing + 2*kShapeTypeSpacing + kShapeSize, | |
22 kPaintPadding = kPaintSpacing - kShapeSize, | |
23 }; | |
24 | |
25 static const SkColor kBGColor = SkColorSetARGB(200, 210, 184, 135); | |
26 | |
27 static const SkColor kShapeColors[3] = { | |
28 SkColorSetARGB(130, 255, 0, 128), // input color unknown | |
29 SkColorSetARGB(255, 0, 255, 255), // input color opaque | |
30 SkColorSetARGB(255, 255, 255, 255) // input solid white | |
31 }; | |
32 | |
33 enum Shape { | |
34 kSquare_Shape, | |
35 kDiamond_Shape, | |
36 kOval_Shape, | |
37 | |
38 kLast_Shape = kOval_Shape | |
39 }; | |
40 | |
41 namespace skiagm { | |
42 | |
43 /** | |
44 * Verifies AA works properly on all Xfermodes, including arithmetic, with vario us color invariants. | |
45 */ | |
46 class AAXfermodesGM : public GM { | |
egdaniel
2015/05/26 14:15:22
So for this GM I'm think we can take off the solid
Chris Dalton
2015/05/26 22:10:28
Done.
| |
47 public: | |
48 AAXfermodesGM() {} | |
49 | |
50 protected: | |
51 SkString onShortName() override { | |
52 return SkString("aaxfermodes"); | |
53 } | |
54 | |
55 SkISize onISize() override { | |
56 return SkISize::Make(3*kPaintWidth + 2*kPaintPadding + 2*kPadding, | |
57 (2 + SkXfermode::kLastCoeffMode) * kShapeSpacing + 2*kPadding); | |
58 } | |
59 | |
60 void onOnceBeforeDraw() override { | |
61 static const SkScalar radius = -1.4f * kShapeSize/2; | |
62 SkPoint pts[4] = { | |
63 {-radius, 0}, | |
64 {0, -1.33f * radius}, | |
65 {radius, 0}, | |
66 {0, 1.33f * radius} | |
67 }; | |
68 fPath.moveTo(pts[0]); | |
69 fPath.quadTo(pts[1], pts[2]); | |
70 fPath.quadTo(pts[3], pts[0]); | |
71 } | |
72 | |
73 void onDraw(SkCanvas* canvas) override { | |
74 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc0c0c0, 10); | |
75 | |
76 canvas->saveLayer(NULL, NULL); | |
77 canvas->drawColor(kBGColor, SkXfermode::kSrc_Mode); | |
78 | |
79 canvas->translate(kPadding + kShapeSize/2, kPadding + kShapeSpacing + kS hapeSize/2); | |
80 | |
81 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); color Idx++) { | |
82 SkColor color = kShapeColors[colorIdx]; | |
83 | |
84 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) { | |
85 Shape shape = static_cast<Shape>(shapeIdx); | |
86 canvas->save(); | |
87 | |
88 for (size_t xfermodeIdx = 0; xfermodeIdx < kXfermodeCount; xferm odeIdx++) { | |
89 this->drawShape(canvas, shape, color, xfermodeIdx); | |
90 | |
91 if (xfermodeIdx == SkXfermode::kLastCoeffMode) { | |
92 // New column. | |
93 canvas->restore(); | |
94 canvas->translate(kShapeSpacing, 0); | |
95 canvas->save(); | |
96 } else { | |
97 canvas->translate(0, kShapeSpacing); | |
98 } | |
99 } | |
100 | |
101 canvas->restore(); | |
102 | |
103 if (shape != kLast_Shape) { | |
104 canvas->translate(kShapeTypeSpacing, 0); | |
105 } else { | |
106 canvas->translate(kPaintSpacing, 0); | |
107 } | |
108 } | |
109 } | |
110 | |
111 canvas->restore(); | |
112 | |
113 SkPaint textPaint; | |
114 textPaint.setAntiAlias(true); | |
115 sk_tool_utils::set_portable_typeface(&textPaint); | |
116 textPaint.setTextAlign(SkPaint::kCenter_Align); | |
117 textPaint.setFakeBoldText(true); | |
118 textPaint.setTextSize(21 * kShapeSize/32); | |
119 | |
120 canvas->translate(kPadding + kPaintWidth/2, | |
121 kPadding + kShapeSize/2 + textPaint.getTextSize()/4); | |
122 canvas->drawText("input color unknown", sizeof("input color unknown") - 1, 0, 0, textPaint); | |
123 | |
124 canvas->translate(kPaintWidth + kPaintPadding, 0); | |
125 canvas->drawText("input color opaque", sizeof("input color opaque") - 1, 0, 0, textPaint); | |
126 | |
127 canvas->translate(kPaintWidth + kPaintPadding, 0); | |
128 canvas->drawText("input solid white", sizeof("input solid white") - 1, 0 , 0, textPaint); | |
129 } | |
130 | |
131 void drawShape(SkCanvas* canvas, Shape shape, const SkColor color, size_t xf ermodeIdx) { | |
132 SkPaint shapePaint; | |
133 shapePaint.setAntiAlias(kSquare_Shape != shape); | |
134 shapePaint.setColor(color); | |
135 | |
136 SkAutoTUnref<SkXfermode> xfermode; | |
137 if (xfermodeIdx <= SkXfermode::kLastMode) { | |
138 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(xfermodeIdx); | |
139 xfermode.reset(SkXfermode::Create(mode)); | |
140 } else { | |
141 xfermode.reset(SkArithmeticMode::Create(+1.0f, +0.25f, -0.5f, +0.1f) ); | |
142 } | |
143 shapePaint.setXfermode(xfermode); | |
144 | |
145 if (xfermodeIdx == SkXfermode::kPlus_Mode) { | |
146 // Check for overflow and dim the src and dst colors if we need to, otherwise we might get | |
egdaniel
2015/05/26 14:15:22
100 chars
Chris Dalton
2015/05/26 22:10:28
Done.
| |
147 // confusing AA artifacts. | |
148 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color ), | |
149 SkColorGetR(kBGColor) + SkColorGetR(color )), | |
150 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color ), | |
151 SkColorGetB(kBGColor) + SkColorGetB(color ))); | |
152 | |
153 if (maxSum > 255) { | |
154 SkPaint dimPaint; | |
155 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0); | |
156 dimPaint.setAntiAlias(false); | |
157 dimPaint.setXfermode(SkXfermode::Create(SkXfermode::kDstIn_Mode) ); | |
158 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2, | |
159 kShapeSpacing/2, kShapeSpacing/2, dimPain t); | |
160 | |
161 shapePaint.setAlpha(255 * shapePaint.getAlpha() / maxSum); | |
162 } | |
163 } | |
164 | |
165 switch (shape) { | |
166 case kSquare_Shape: | |
167 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/ 2, kShapeSize/2, | |
168 shapePaint); | |
169 break; | |
170 | |
171 case kDiamond_Shape: | |
172 canvas->save(); | |
173 canvas->rotate(45); | |
174 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/ 2, kShapeSize/2, | |
175 shapePaint); | |
176 canvas->restore(); | |
177 break; | |
178 | |
179 case kOval_Shape: | |
180 canvas->save(); | |
181 canvas->rotate(static_cast<SkScalar>((511 * xfermodeIdx + 257) % 360)); | |
182 canvas->drawPath(fPath, shapePaint); | |
183 canvas->restore(); | |
184 break; | |
185 | |
186 default: | |
187 SkFAIL("Invalid shape."); | |
188 } | |
189 } | |
190 | |
191 private: | |
192 SkPath fPath; | |
193 | |
194 typedef GM INHERITED; | |
195 }; | |
196 | |
197 ////////////////////////////////////////////////////////////////////////////// | |
198 | |
199 static GM* MyFactory(void*) { return new AAXfermodesGM; } | |
200 static GMRegistry reg(MyFactory); | |
201 | |
202 } | |
OLD | NEW |