3 * Copyright 2015 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SkArithmeticMode.h"
11 #include "SkXfermode.h"
14 kXfermodeCount = SkXfermode::kLastMode + 2, // All xfermodes plus arithmetic mode.
17 kShapeTypeSpacing = 4 * kShapeSpacing / 3,
18 kPaintSpacing = 3 * 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 static const SkColor kBGColor = SkColorSetARGB(200, 210, 184, 135);
28 static const SkColor kShapeColors[2] = {
29 SkColorSetARGB(130, 255, 0, 128), // input color unknown
30 SkColorSetARGB(255, 0, 255, 255) // input color opaque
38 kLast_Shape = kOval_Shape
44 * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown
47 class AAXfermodesGM : public GM {
58 SkString onShortName() override {
59 return SkString("aaxfermodes");
62 SkISize onISize() override {
63 return SkISize::Make(2 * kMargin + 2 * kXfermodeTypeSpacing -
64 (kXfermodeTypeSpacing - (kLabelSpacing + 2 * kPaintSpacing)),
65 2 * kMargin + kTitleSpacing + kSubtitleSpacing +
66 (1 + SkXfermode::kLastCoeffMode) * kShapeSpacing);
69 void onOnceBeforeDraw() override {
70 fLabelPaint.setAntiAlias(true);
71 sk_tool_utils::set_portable_typeface(&fLabelPaint);
72 fLabelPaint.setTextSize(5 * kShapeSize/8);
73 fLabelPaint.setSubpixelText(true);
75 static const SkScalar radius = -1.4f * kShapeSize/2;
83 fPath.quadTo(pts[1], pts[2]);
84 fPath.quadTo(pts[3], pts[0]);
87 void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) {
89 { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 };
92 if (kCheckerboard_Pass == drawingPass) {
93 canvas->translate(kMargin, kMargin);
95 canvas->translate(0, kTitleSpacing);
97 for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) {
98 size_t firstMode = (SkXfermode::kLastCoeffMode + 1) * xfermodeSet;
101 if (kShape_Pass == drawingPass) {
102 fLabelPaint.setTextAlign(SkPaint::kCenter_Align);
103 canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1,
104 kLabelSpacing + kShapeSpacing / 2 + kShapeTypeSpacing,
105 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
106 canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1,
107 kLabelSpacing + kShapeSpacing / 2 + kShapeTypeSpacing + kPaintSpacing,
108 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
111 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2);
113 for (size_t m = 0; m <= SkXfermode::kLastCoeffMode; m++) {
114 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(firstMode + m);
117 if (kShape_Pass == drawingPass) {
118 this->drawModeName(canvas, mode);
120 canvas->translate(kLabelSpacing + kShapeSpacing/2, 0);
122 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); colorIdx++) {
124 this->setupShapePaint(canvas, kShapeColors[colorIdx], mode, &paint);
125 SkASSERT(colorIdx == 0 || 255 == paint.getAlpha());
128 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) {
129 if (kShape_Pass != drawingPass) {
131 canvas->clipRect(clipRect);
132 if (kCheckerboard_Pass == drawingPass) {
133 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6,
136 SkASSERT(kBackground_Pass == drawingPass);
137 canvas->drawColor(kBGColor, SkXfermode::kSrc_Mode);
141 this->drawShape(canvas, static_cast<Shape>(shapeIdx), paint, mode);
143 canvas->translate(kShapeTypeSpacing, 0);
147 canvas->translate(kPaintSpacing, 0);
151 canvas->translate(0, kShapeSpacing);
155 canvas->translate(kXfermodeTypeSpacing, 0);
161 void onDraw(SkCanvas* canvas) override {
162 draw_pass(canvas, kCheckerboard_Pass);
163 canvas->saveLayer(NULL, NULL);
165 canvas->translate(kMargin, kMargin);
166 draw_pass(canvas, kBackground_Pass);
168 SkPaint titlePaint(fLabelPaint);
169 titlePaint.setTextSize(9 * titlePaint.getTextSize() / 8);
170 titlePaint.setFakeBoldText(true);
171 titlePaint.setTextAlign(SkPaint::kCenter_Align);
172 canvas->drawText("Porter Duff", sizeof("Porter Duff") - 1,
173 kLabelSpacing + 3 * kShapeTypeSpacing,
174 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
175 canvas->drawText("Advanced", sizeof("Advanced") - 1,
176 kXfermodeTypeSpacing + kLabelSpacing + 3 * kShapeTypeSpacing,
177 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
179 draw_pass(canvas, kShape_Pass);
183 void drawModeName(SkCanvas* canvas, SkXfermode::Mode mode) {
184 const char* modeName = mode <= SkXfermode::kLastMode ? SkXfermode::ModeName(mode)
186 fLabelPaint.setTextAlign(SkPaint::kRight_Align);
187 canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize / 4,
188 fLabelPaint.getTextSize() / 3, fLabelPaint);
191 void setupShapePaint(SkCanvas* canvas, GrColor color, SkXfermode::Mode mode, SkPaint* paint) {
192 paint->setColor(color);
194 if (mode == SkXfermode::kPlus_Mode) {
195 // Check for overflow, otherwise we might get confusing AA artifacts.
196 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color),
197 SkColorGetR(kBGColor) + SkColorGetR(color)),
198 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color),
199 SkColorGetB(kBGColor) + SkColorGetB(color)));
203 dimPaint.setAntiAlias(false);
204 dimPaint.setXfermode(SkXfermode::Create(SkXfermode::kDstIn_Mode));
205 if (255 != paint->getAlpha()) {
206 // Dim the src and dst colors.
207 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0);
208 paint->setAlpha(255 * paint->getAlpha() / maxSum);
210 // Just clear the dst, we need to preserve the paint's opacity.
211 dimPaint.setARGB(0, 0, 0, 0);
213 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2,
214 kShapeSpacing/2 + 2 * kShapeTypeSpacing,
215 kShapeSpacing/2, dimPaint);
220 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkXfermode::Mode mode) {
221 SkPaint shapePaint(paint);
222 shapePaint.setAntiAlias(kSquare_Shape != shape);
224 SkAutoTUnref<SkXfermode> xfermode;
225 if (mode <= SkXfermode::kLastMode) {
226 xfermode.reset(SkXfermode::Create(mode));
228 xfermode.reset(SkArithmeticMode::Create(+1.0f, +0.25f, -0.5f, +0.1f));
230 shapePaint.setXfermode(xfermode);
234 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
241 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
248 canvas->rotate(static_cast<SkScalar>((511 * mode + 257) % 360));
249 canvas->drawPath(fPath, shapePaint);
254 SkFAIL("Invalid shape.");
262 typedef GM INHERITED;
265 //////////////////////////////////////////////////////////////////////////////
267 static GM* MyFactory(void*) { return new AAXfermodesGM; }
268 static GMRegistry reg(MyFactory);