OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "gm.h" | 7 #include "gm.h" |
8 #include "SkArithmeticMode.h" | 8 #include "SkArithmeticMode.h" |
9 #include "SkPath.h" | 9 #include "SkPath.h" |
10 #include "SkShader.h" | 10 #include "SkShader.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTex
tSize() / 3, | 114 kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTex
tSize() / 3, |
115 fLabelPaint); | 115 fLabelPaint); |
116 } | 116 } |
117 | 117 |
118 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2); | 118 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2); |
119 | 119 |
120 for (size_t m = 0; m <= SkXfermode::kLastCoeffMode; m++) { | 120 for (size_t m = 0; m <= SkXfermode::kLastCoeffMode; m++) { |
121 if (firstMode + m > SkXfermode::kLastMode) { | 121 if (firstMode + m > SkXfermode::kLastMode) { |
122 break; | 122 break; |
123 } | 123 } |
124 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(firstMode
+ m); | 124 SkBlendMode mode = static_cast<SkBlendMode>(firstMode + m); |
125 canvas->save(); | 125 canvas->save(); |
126 | 126 |
127 if (kShape_Pass == drawingPass) { | 127 if (kShape_Pass == drawingPass) { |
128 this->drawModeName(canvas, mode); | 128 this->drawModeName(canvas, mode); |
129 } | 129 } |
130 canvas->translate(kLabelSpacing + kShapeSpacing/2, 0); | 130 canvas->translate(kLabelSpacing + kShapeSpacing/2, 0); |
131 | 131 |
132 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors
); colorIdx++) { | 132 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors
); colorIdx++) { |
133 SkPaint paint; | 133 SkPaint paint; |
134 this->setupShapePaint(canvas, kShapeColors[colorIdx], mode,
&paint); | 134 this->setupShapePaint(canvas, kShapeColors[colorIdx], mode,
&paint); |
135 SkASSERT(colorIdx == 0 || 255 == paint.getAlpha()); | 135 SkASSERT(colorIdx == 0 || 255 == paint.getAlpha()); |
136 canvas->save(); | 136 canvas->save(); |
137 | 137 |
138 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx+
+) { | 138 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx+
+) { |
139 if (kShape_Pass != drawingPass) { | 139 if (kShape_Pass != drawingPass) { |
140 canvas->save(); | 140 canvas->save(); |
141 canvas->clipRect(clipRect); | 141 canvas->clipRect(clipRect); |
142 if (kCheckerboard_Pass == drawingPass) { | 142 if (kCheckerboard_Pass == drawingPass) { |
143 sk_tool_utils::draw_checkerboard(canvas, 0xfffff
fff, 0xffc6c3c6, | 143 sk_tool_utils::draw_checkerboard(canvas, 0xfffff
fff, 0xffc6c3c6, |
144 10); | 144 10); |
145 } else { | 145 } else { |
146 SkASSERT(kBackground_Pass == drawingPass); | 146 SkASSERT(kBackground_Pass == drawingPass); |
147 canvas->drawColor(kBGColor, SkXfermode::kSrc_Mod
e); | 147 canvas->drawColor(kBGColor, SkBlendMode::kSrc); |
148 } | 148 } |
149 canvas->restore(); | 149 canvas->restore(); |
150 } else { | 150 } else { |
151 this->drawShape(canvas, static_cast<Shape>(shapeIdx)
, paint, mode); | 151 this->drawShape(canvas, static_cast<Shape>(shapeIdx)
, paint, mode); |
152 } | 152 } |
153 canvas->translate(kShapeTypeSpacing, 0); | 153 canvas->translate(kShapeTypeSpacing, 0); |
154 } | 154 } |
155 | 155 |
156 canvas->restore(); | 156 canvas->restore(); |
157 canvas->translate(kPaintSpacing, 0); | 157 canvas->translate(kPaintSpacing, 0); |
(...skipping 25 matching lines...) Expand all Loading... |
183 kLabelSpacing + 4 * kShapeTypeSpacing, | 183 kLabelSpacing + 4 * kShapeTypeSpacing, |
184 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, title
Paint); | 184 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, title
Paint); |
185 canvas->drawText("Advanced", sizeof("Advanced") - 1, | 185 canvas->drawText("Advanced", sizeof("Advanced") - 1, |
186 kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSp
acing, | 186 kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSp
acing, |
187 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, title
Paint); | 187 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, title
Paint); |
188 | 188 |
189 draw_pass(canvas, kShape_Pass); | 189 draw_pass(canvas, kShape_Pass); |
190 canvas->restore(); | 190 canvas->restore(); |
191 } | 191 } |
192 | 192 |
193 void drawModeName(SkCanvas* canvas, SkXfermode::Mode mode) { | 193 void drawModeName(SkCanvas* canvas, SkBlendMode mode) { |
194 const char* modeName = mode <= SkXfermode::kLastMode ? SkXfermode::ModeN
ame(mode) | 194 const char* modeName = SkXfermode::ModeName(mode); |
195 : "Arithmetic"; | |
196 fLabelPaint.setTextAlign(SkPaint::kRight_Align); | 195 fLabelPaint.setTextAlign(SkPaint::kRight_Align); |
197 canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize
/ 4, | 196 canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize
/ 4, |
198 fLabelPaint.getTextSize() / 4, fLabelPaint); | 197 fLabelPaint.getTextSize() / 4, fLabelPaint); |
199 } | 198 } |
200 | 199 |
201 void setupShapePaint(SkCanvas* canvas, GrColor color, SkXfermode::Mode mode,
SkPaint* paint) { | 200 void setupShapePaint(SkCanvas* canvas, GrColor color, SkBlendMode mode, SkPa
int* paint) { |
202 paint->setColor(color); | 201 paint->setColor(color); |
203 | 202 |
204 if (mode == SkXfermode::kPlus_Mode) { | 203 if (mode == SkBlendMode::kPlus) { |
205 // Check for overflow, otherwise we might get confusing AA artifacts
. | 204 // Check for overflow, otherwise we might get confusing AA artifacts
. |
206 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color
), | 205 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color
), |
207 SkColorGetR(kBGColor) + SkColorGetR(color
)), | 206 SkColorGetR(kBGColor) + SkColorGetR(color
)), |
208 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color
), | 207 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color
), |
209 SkColorGetB(kBGColor) + SkColorGetB(color
))); | 208 SkColorGetB(kBGColor) + SkColorGetB(color
))); |
210 | 209 |
211 if (maxSum > 255) { | 210 if (maxSum > 255) { |
212 SkPaint dimPaint; | 211 SkPaint dimPaint; |
213 dimPaint.setAntiAlias(false); | 212 dimPaint.setAntiAlias(false); |
214 dimPaint.setXfermodeMode(SkXfermode::kDstIn_Mode); | 213 dimPaint.setBlendMode(SkBlendMode::kDstIn); |
215 if (255 != paint->getAlpha()) { | 214 if (255 != paint->getAlpha()) { |
216 // Dim the src and dst colors. | 215 // Dim the src and dst colors. |
217 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0); | 216 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0); |
218 paint->setAlpha(255 * paint->getAlpha() / maxSum); | 217 paint->setAlpha(255 * paint->getAlpha() / maxSum); |
219 } else { | 218 } else { |
220 // Just clear the dst, we need to preserve the paint's opaci
ty. | 219 // Just clear the dst, we need to preserve the paint's opaci
ty. |
221 dimPaint.setARGB(0, 0, 0, 0); | 220 dimPaint.setARGB(0, 0, 0, 0); |
222 } | 221 } |
223 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2, | 222 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2, |
224 kShapeSpacing/2 + 3 * kShapeTypeSpacing, | 223 kShapeSpacing/2 + 3 * kShapeTypeSpacing, |
225 kShapeSpacing/2, dimPaint); | 224 kShapeSpacing/2, dimPaint); |
226 } | 225 } |
227 } | 226 } |
228 } | 227 } |
229 | 228 |
230 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkXfermo
de::Mode mode) { | 229 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkBlendM
ode mode) { |
231 SkASSERT(mode <= SkXfermode::kLastMode); | 230 SkASSERT(mode <= SkBlendMode::kLastMode); |
232 SkPaint shapePaint(paint); | 231 SkPaint shapePaint(paint); |
233 shapePaint.setAntiAlias(kSquare_Shape != shape); | 232 shapePaint.setAntiAlias(kSquare_Shape != shape); |
234 shapePaint.setXfermodeMode(mode); | 233 shapePaint.setBlendMode(mode); |
235 | 234 |
236 switch (shape) { | 235 switch (shape) { |
237 case kSquare_Shape: | 236 case kSquare_Shape: |
238 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/
2, kShapeSize/2, | 237 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/
2, kShapeSize/2, |
239 shapePaint); | 238 shapePaint); |
240 break; | 239 break; |
241 | 240 |
242 case kDiamond_Shape: | 241 case kDiamond_Shape: |
243 canvas->save(); | 242 canvas->save(); |
244 canvas->rotate(45); | 243 canvas->rotate(45); |
245 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/
2, kShapeSize/2, | 244 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/
2, kShapeSize/2, |
246 shapePaint); | 245 shapePaint); |
247 canvas->restore(); | 246 canvas->restore(); |
248 break; | 247 break; |
249 | 248 |
250 case kOval_Shape: | 249 case kOval_Shape: |
251 canvas->save(); | 250 canvas->save(); |
252 canvas->rotate(static_cast<SkScalar>((511 * mode + 257) % 360)); | 251 canvas->rotate(static_cast<SkScalar>((511 * (int)mode + 257) % 3
60)); |
253 canvas->drawPath(fOval, shapePaint); | 252 canvas->drawPath(fOval, shapePaint); |
254 canvas->restore(); | 253 canvas->restore(); |
255 break; | 254 break; |
256 | 255 |
257 case kConcave_Shape: | 256 case kConcave_Shape: |
258 canvas->drawPath(fConcave, shapePaint); | 257 canvas->drawPath(fConcave, shapePaint); |
259 break; | 258 break; |
260 | 259 |
261 default: | 260 default: |
262 SkFAIL("Invalid shape."); | 261 SkFAIL("Invalid shape."); |
263 } | 262 } |
264 } | 263 } |
265 | 264 |
266 private: | 265 private: |
267 SkPaint fLabelPaint; | 266 SkPaint fLabelPaint; |
268 SkPath fOval; | 267 SkPath fOval; |
269 SkPath fConcave; | 268 SkPath fConcave; |
270 | 269 |
271 typedef skiagm::GM INHERITED; | 270 typedef skiagm::GM INHERITED; |
272 }; | 271 }; |
273 DEF_GM( return new AAXfermodesGM; ) | 272 DEF_GM( return new AAXfermodesGM; ) |
OLD | NEW |