2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkArithmeticMode.h"
11 #include "SkXfermode.h"
14 kXfermodeCount = SkXfermode::kLastMode + 2, // All xfermodes plus arithmetic mode.
17 kShapeTypeSpacing = 4 * kShapeSpacing / 3,
18 kPaintSpacing = 4 * kShapeTypeSpacing,
19 kLabelSpacing = 3 * kShapeSize,
20 kMargin = kShapeSpacing / 2,
21 kXfermodeTypeSpacing = kLabelSpacing + 2 * kPaintSpacing + kShapeTypeSpacing,
22 kTitleSpacing = 3 * kShapeSpacing / 4,
23 kSubtitleSpacing = 5 * kShapeSpacing / 8
26 constexpr SkColor kBGColor = 0xc8d2b887;
28 constexpr SkColor kShapeColors[2] = {
29 0x82ff0080, // input color unknown
30 0xff00ffff, // input color opaque
39 kLast_Shape = kConcave_Shape
43 * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown
46 class AAXfermodesGM : public skiagm::GM {
57 SkString onShortName() override {
58 return SkString("aaxfermodes");
61 SkISize onISize() override {
62 return SkISize::Make(2 * kMargin + 2 * kXfermodeTypeSpacing -
63 (kXfermodeTypeSpacing - (kLabelSpacing + 2 * kPaintSpacing)),
64 2 * kMargin + kTitleSpacing + kSubtitleSpacing +
65 (1 + SkXfermode::kLastCoeffMode) * kShapeSpacing);
68 void onOnceBeforeDraw() override {
69 fLabelPaint.setAntiAlias(true);
70 sk_tool_utils::set_portable_typeface(&fLabelPaint);
71 fLabelPaint.setTextSize(5 * kShapeSize/8);
72 fLabelPaint.setSubpixelText(true);
74 constexpr SkScalar radius = -1.4f * kShapeSize/2;
82 fOval.quadTo(pts[1], pts[2]);
83 fOval.quadTo(pts[3], pts[0]);
85 fConcave.moveTo(-radius, 0);
86 fConcave.quadTo(0, 0, 0, -radius);
87 fConcave.quadTo(0, 0, radius, 0);
88 fConcave.quadTo(0, 0, 0, radius);
89 fConcave.quadTo(0, 0, -radius, 0);
93 void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) {
95 { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 };
98 if (kCheckerboard_Pass == drawingPass) {
99 canvas->translate(kMargin, kMargin);
101 canvas->translate(0, kTitleSpacing);
103 for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) {
104 size_t firstMode = (SkXfermode::kLastCoeffMode + 1) * xfermodeSet;
107 if (kShape_Pass == drawingPass) {
108 fLabelPaint.setTextAlign(SkPaint::kCenter_Align);
109 canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1,
110 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
111 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
112 canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1,
113 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
114 kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3,
118 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2);
120 for (size_t m = 0; m <= SkXfermode::kLastCoeffMode; m++) {
121 if (firstMode + m > SkXfermode::kLastMode) {
124 SkBlendMode mode = static_cast<SkBlendMode>(firstMode + m);
127 if (kShape_Pass == drawingPass) {
128 this->drawModeName(canvas, mode);
130 canvas->translate(kLabelSpacing + kShapeSpacing/2, 0);
132 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); colorIdx++) {
134 this->setupShapePaint(canvas, kShapeColors[colorIdx], mode, &paint);
135 SkASSERT(colorIdx == 0 || 255 == paint.getAlpha());
138 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) {
139 if (kShape_Pass != drawingPass) {
141 canvas->clipRect(clipRect);
142 if (kCheckerboard_Pass == drawingPass) {
143 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6,
146 SkASSERT(kBackground_Pass == drawingPass);
147 canvas->drawColor(kBGColor, SkBlendMode::kSrc);
151 this->drawShape(canvas, static_cast<Shape>(shapeIdx), paint, mode);
153 canvas->translate(kShapeTypeSpacing, 0);
157 canvas->translate(kPaintSpacing, 0);
161 canvas->translate(0, kShapeSpacing);
165 canvas->translate(kXfermodeTypeSpacing, 0);
171 void onDraw(SkCanvas* canvas) override {
172 draw_pass(canvas, kCheckerboard_Pass);
173 canvas->saveLayer(nullptr, nullptr);
175 canvas->translate(kMargin, kMargin);
176 draw_pass(canvas, kBackground_Pass);
178 SkPaint titlePaint(fLabelPaint);
179 titlePaint.setTextSize(9 * titlePaint.getTextSize() / 8);
180 titlePaint.setFakeBoldText(true);
181 titlePaint.setTextAlign(SkPaint::kCenter_Align);
182 canvas->drawText("Porter Duff", sizeof("Porter Duff") - 1,
183 kLabelSpacing + 4 * kShapeTypeSpacing,
184 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
185 canvas->drawText("Advanced", sizeof("Advanced") - 1,
186 kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing,
187 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
189 draw_pass(canvas, kShape_Pass);
193 void drawModeName(SkCanvas* canvas, SkBlendMode mode) {
194 const char* modeName = SkXfermode::ModeName(mode);
195 fLabelPaint.setTextAlign(SkPaint::kRight_Align);
196 canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize / 4,
197 fLabelPaint.getTextSize() / 4, fLabelPaint);
200 void setupShapePaint(SkCanvas* canvas, GrColor color, SkBlendMode mode, SkPaint* paint) {
201 paint->setColor(color);
203 if (mode == SkBlendMode::kPlus) {
204 // Check for overflow, otherwise we might get confusing AA artifacts.
205 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color),
206 SkColorGetR(kBGColor) + SkColorGetR(color)),
207 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color),
208 SkColorGetB(kBGColor) + SkColorGetB(color)));
212 dimPaint.setAntiAlias(false);
213 dimPaint.setBlendMode(SkBlendMode::kDstIn);
214 if (255 != paint->getAlpha()) {
215 // Dim the src and dst colors.
216 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0);
217 paint->setAlpha(255 * paint->getAlpha() / maxSum);
219 // Just clear the dst, we need to preserve the paint's opacity.
220 dimPaint.setARGB(0, 0, 0, 0);
222 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2,
223 kShapeSpacing/2 + 3 * kShapeTypeSpacing,
224 kShapeSpacing/2, dimPaint);
229 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkBlendMode mode) {
230 SkASSERT(mode <= SkBlendMode::kLastMode);
231 SkPaint shapePaint(paint);
232 shapePaint.setAntiAlias(kSquare_Shape != shape);
233 shapePaint.setBlendMode(mode);
237 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
244 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
251 canvas->rotate(static_cast<SkScalar>((511 * (int)mode + 257) % 360));
252 canvas->drawPath(fOval, shapePaint);
257 canvas->drawPath(fConcave, shapePaint);
261 SkFAIL("Invalid shape.");
270 typedef skiagm::GM INHERITED;
272 DEF_GM( return new AAXfermodesGM; )