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 static void draw_shape(SkCanvas* canvas, Shape shape, const SkColor color, size_ t xfermodeIdx); | |
44 | |
45 /** | |
46 * Verifies AA works properly on all Xfermodes, including arithmetic, with vario us color invariants. | |
47 */ | |
48 class AAXfermodesGM : public GM { | |
49 public: | |
50 AAXfermodesGM() {} | |
51 | |
52 protected: | |
53 SkString onShortName() override { | |
54 return SkString("aaxfermodes"); | |
55 } | |
56 | |
57 SkISize onISize() override { | |
58 return SkISize::Make(3*kPaintWidth + 2*kPaintPadding + 2*kPadding, | |
59 (2 + SkXfermode::kLastCoeffMode) * kShapeSpacing + 2*kPadding); | |
60 } | |
61 | |
62 void onDraw(SkCanvas* canvas) override { | |
63 SkPaint checkerPaint; | |
64 checkerPaint.setAntiAlias(false); | |
65 checkerPaint.setStyle(SkPaint::kFill_Style); | |
66 checkerPaint.setShader(fCheckerShader); | |
67 canvas->drawPaint(checkerPaint); | |
68 | |
69 canvas->saveLayer(NULL, NULL); | |
70 canvas->drawColor(kBGColor, SkXfermode::kSrc_Mode); | |
71 | |
72 canvas->translate(kPadding + kShapeSize/2, kPadding + kShapeSpacing + kS hapeSize/2); | |
73 | |
74 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); color Idx++) { | |
75 SkColor color = kShapeColors[colorIdx]; | |
76 | |
77 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) { | |
78 Shape shape = static_cast<Shape>(shapeIdx); | |
79 canvas->save(); | |
80 | |
81 for (size_t xfermodeIdx = 0; xfermodeIdx < kXfermodeCount; xferm odeIdx++) { | |
82 draw_shape(canvas, shape, color, xfermodeIdx); | |
83 | |
84 if (xfermodeIdx == SkXfermode::kLastCoeffMode) { | |
85 // New column. | |
86 canvas->restore(); | |
87 canvas->translate(kShapeSpacing, 0); | |
88 canvas->save(); | |
89 } else { | |
90 canvas->translate(0, kShapeSpacing); | |
91 } | |
92 } | |
93 | |
94 canvas->restore(); | |
95 canvas->translate(shape != kLast_Shape ? kShapeTypeSpacing : kPa intSpacing, 0); | |
96 } | |
97 } | |
98 | |
99 canvas->restore(); | |
100 | |
101 SkPaint textPaint; | |
102 textPaint.setAntiAlias(true); | |
103 sk_tool_utils::set_portable_typeface(&textPaint); | |
104 textPaint.setTextAlign(SkPaint::kCenter_Align); | |
105 textPaint.setFakeBoldText(true); | |
106 textPaint.setTextSize(21 * kShapeSize/32); | |
107 | |
108 canvas->translate(kPadding + kPaintWidth/2, | |
109 kPadding + kShapeSize/2 + textPaint.getTextSize()/4); | |
110 canvas->drawText("input color unknown", sizeof("input color unknown") - 1, 0, 0, textPaint); | |
111 | |
112 canvas->translate(kPaintWidth + kPaintPadding, 0); | |
113 canvas->drawText("input color opaque", sizeof("input color opaque") - 1, 0, 0, textPaint); | |
114 | |
115 canvas->translate(kPaintWidth + kPaintPadding, 0); | |
116 canvas->drawText("input solid white", sizeof("input solid white") - 1, 0 , 0, textPaint); | |
117 } | |
118 | |
119 private: | |
120 void onOnceBeforeDraw() override { | |
121 static const uint32_t kCheckerData[] = { | |
122 SkPackARGB32(0xff, 0xc0, 0xc0, 0xc0), | |
egdaniel
2015/05/20 18:59:42
we have a helper function in sk_tool_util that wil
Chris Dalton
2015/05/21 23:14:49
Done.
| |
123 SkPackARGB32(0xff, 0xff, 0xff, 0xff), | |
124 SkPackARGB32(0xff, 0xff, 0xff, 0xff), | |
125 SkPackARGB32(0xff, 0xc0, 0xc0, 0xc0) | |
126 }; | |
127 SkBitmap checker; | |
128 checker.allocN32Pixels(2, 2, true); | |
129 memcpy(checker.getPixels(), kCheckerData, sizeof(kCheckerData)); | |
130 | |
131 SkMatrix lm; | |
132 lm.setScale(SkIntToScalar(10), SkIntToScalar(10)); | |
133 fCheckerShader.reset(SkShader::CreateBitmapShader(checker, | |
134 SkShader::kRepeat_Tile Mode, | |
135 SkShader::kRepeat_Tile Mode, | |
136 &lm)); | |
137 } | |
138 | |
139 SkAutoTUnref<SkShader> fCheckerShader; | |
140 | |
141 typedef GM INHERITED; | |
142 }; | |
143 | |
144 static void draw_shape(SkCanvas* canvas, Shape shape, const SkColor color, size_ t xfermodeIdx) { | |
145 SkPaint shapePaint; | |
146 shapePaint.setAntiAlias(kSquare_Shape != shape); | |
147 shapePaint.setColor(color); | |
148 | |
149 SkAutoTUnref<SkXfermode> xfermode; | |
150 if (xfermodeIdx <= SkXfermode::kLastMode) { | |
151 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(xfermodeIdx); | |
152 xfermode.reset(SkXfermode::Create(mode)); | |
153 } else { | |
154 xfermode.reset(SkArithmeticMode::Create(+1.0f, +0.25f, -0.5f, +0.1f)); | |
155 } | |
156 shapePaint.setXfermode(xfermode); | |
157 | |
158 if (xfermodeIdx == SkXfermode::kPlus_Mode) { | |
159 // Check for overflow and dim the src and dst colors if we need to, othe rwise we might get | |
160 // confusing AA artifacts. | |
161 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color), | |
162 SkColorGetR(kBGColor) + SkColorGetR(color)), | |
163 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color), | |
164 SkColorGetB(kBGColor) + SkColorGetB(color))); | |
165 | |
166 if (maxSum > 255) { | |
167 SkPaint dimPaint; | |
168 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0); | |
169 dimPaint.setAntiAlias(false); | |
170 dimPaint.setXfermode(SkXfermode::Create(SkXfermode::kDstIn_Mode)); | |
171 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2, | |
172 kShapeSpacing/2, kShapeSpacing/2, dimPaint); | |
173 | |
174 shapePaint.setAlpha(255 * shapePaint.getAlpha() / maxSum); | |
175 } | |
176 } | |
177 | |
178 switch (shape) { | |
179 case kSquare_Shape: | |
180 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, k ShapeSize/2, | |
181 shapePaint); | |
182 break; | |
183 | |
184 case kDiamond_Shape: | |
185 canvas->save(); | |
186 canvas->rotate(45); | |
187 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, k ShapeSize/2, | |
188 shapePaint); | |
189 canvas->restore(); | |
190 break; | |
191 | |
192 case kOval_Shape: | |
193 canvas->save(); | |
194 canvas->rotate((511 * xfermodeIdx + 257) % 360); | |
195 canvas->drawArc(SkRect::MakeLTRB(-kShapeSize/2, -1.4f * kShapeSize/2 , | |
196 kShapeSize/2, 1.4f * kShapeSize/2), | |
197 0, 360, true, shapePaint); | |
198 canvas->restore(); | |
199 break; | |
200 | |
201 default: | |
202 SkFAIL("Invalid shape."); | |
203 } | |
204 } | |
205 | |
206 ////////////////////////////////////////////////////////////////////////////// | |
207 | |
208 static GM* MyFactory(void*) { return new AAXfermodesGM; } | |
209 static GMRegistry reg(MyFactory); | |
210 | |
211 } | |
OLD | NEW |