blob: 9e07f8d7518acb4f77bae854fd820e19c5a92e11 [file] [log] [blame]
cdalton6fd158e2015-05-27 15:08:33 -07001
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"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkPath.h"
cdalton6fd158e2015-05-27 15:08:33 -070011#include "SkShader.h"
12#include "SkXfermode.h"
13
14enum {
15 kXfermodeCount = SkXfermode::kLastMode + 2, // All xfermodes plus arithmetic mode.
16 kShapeSize = 22,
17 kShapeSpacing = 36,
18 kShapeTypeSpacing = 4 * kShapeSpacing / 3,
19 kPaintSpacing = 3 * kShapeTypeSpacing,
20 kLabelSpacing = 3 * kShapeSize,
21 kMargin = kShapeSpacing / 2,
22 kXfermodeTypeSpacing = kLabelSpacing + 2 * kPaintSpacing + kShapeTypeSpacing,
23 kTitleSpacing = 3 * kShapeSpacing / 4,
24 kSubtitleSpacing = 5 * kShapeSpacing / 8
25};
26
27static const SkColor kBGColor = SkColorSetARGB(200, 210, 184, 135);
28
29static const SkColor kShapeColors[2] = {
30 SkColorSetARGB(130, 255, 0, 128), // input color unknown
31 SkColorSetARGB(255, 0, 255, 255) // input color opaque
32};
33
34enum Shape {
35 kSquare_Shape,
36 kDiamond_Shape,
37 kOval_Shape,
38
39 kLast_Shape = kOval_Shape
40};
41
42namespace skiagm {
43
44/**
45 * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown
46 * src colors.
47 */
48class AAXfermodesGM : public GM {
49public:
50 AAXfermodesGM() {}
51
52protected:
caryclark83ca6282015-06-10 09:31:09 -070053 enum DrawingPass {
54 kCheckerboard_Pass,
55 kBackground_Pass,
56 kShape_Pass
57 };
58
cdalton6fd158e2015-05-27 15:08:33 -070059 SkString onShortName() override {
60 return SkString("aaxfermodes");
61 }
62
63 SkISize onISize() override {
64 return SkISize::Make(2 * kMargin + 2 * kXfermodeTypeSpacing -
65 (kXfermodeTypeSpacing - (kLabelSpacing + 2 * kPaintSpacing)),
66 2 * kMargin + kTitleSpacing + kSubtitleSpacing +
67 (1 + SkXfermode::kLastCoeffMode) * kShapeSpacing);
68 }
69
70 void onOnceBeforeDraw() override {
71 fLabelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070072 sk_tool_utils::set_portable_typeface(&fLabelPaint);
cdalton6fd158e2015-05-27 15:08:33 -070073 fLabelPaint.setTextSize(5 * kShapeSize/8);
74 fLabelPaint.setSubpixelText(true);
75
76 static const SkScalar radius = -1.4f * kShapeSize/2;
77 SkPoint pts[4] = {
78 {-radius, 0},
79 {0, -1.33f * radius},
80 {radius, 0},
81 {0, 1.33f * radius}
82 };
83 fPath.moveTo(pts[0]);
84 fPath.quadTo(pts[1], pts[2]);
85 fPath.quadTo(pts[3], pts[0]);
86 }
87
caryclark83ca6282015-06-10 09:31:09 -070088 void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) {
89 SkRect clipRect =
90 { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 };
cdalton6fd158e2015-05-27 15:08:33 -070091
caryclark83ca6282015-06-10 09:31:09 -070092 canvas->save();
93 if (kCheckerboard_Pass == drawingPass) {
94 canvas->translate(kMargin, kMargin);
95 }
cdalton6fd158e2015-05-27 15:08:33 -070096 canvas->translate(0, kTitleSpacing);
97
98 for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) {
99 size_t firstMode = (SkXfermode::kLastCoeffMode + 1) * xfermodeSet;
100 canvas->save();
101
caryclark83ca6282015-06-10 09:31:09 -0700102 if (kShape_Pass == drawingPass) {
103 fLabelPaint.setTextAlign(SkPaint::kCenter_Align);
104 canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1,
105 kLabelSpacing + kShapeSpacing / 2 + kShapeTypeSpacing,
106 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
107 canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1,
108 kLabelSpacing + kShapeSpacing / 2 + kShapeTypeSpacing + kPaintSpacing,
109 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
110 }
cdalton6fd158e2015-05-27 15:08:33 -0700111
112 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2);
113
114 for (size_t m = 0; m <= SkXfermode::kLastCoeffMode; m++) {
115 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(firstMode + m);
116 canvas->save();
117
caryclark83ca6282015-06-10 09:31:09 -0700118 if (kShape_Pass == drawingPass) {
119 this->drawModeName(canvas, mode);
120 }
cdalton6fd158e2015-05-27 15:08:33 -0700121 canvas->translate(kLabelSpacing + kShapeSpacing/2, 0);
122
123 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); colorIdx++) {
124 SkPaint paint;
125 this->setupShapePaint(canvas, kShapeColors[colorIdx], mode, &paint);
126 SkASSERT(colorIdx == 0 || 255 == paint.getAlpha());
127 canvas->save();
128
129 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) {
caryclark83ca6282015-06-10 09:31:09 -0700130 if (kShape_Pass != drawingPass) {
131 canvas->save();
132 canvas->clipRect(clipRect);
133 if (kCheckerboard_Pass == drawingPass) {
134 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6,
135 10);
136 } else {
137 SkASSERT(kBackground_Pass == drawingPass);
138 canvas->drawColor(kBGColor, SkXfermode::kSrc_Mode);
139 }
140 canvas->restore();
141 } else {
142 this->drawShape(canvas, static_cast<Shape>(shapeIdx), paint, mode);
143 }
cdalton6fd158e2015-05-27 15:08:33 -0700144 canvas->translate(kShapeTypeSpacing, 0);
145 }
146
147 canvas->restore();
148 canvas->translate(kPaintSpacing, 0);
149 }
150
151 canvas->restore();
152 canvas->translate(0, kShapeSpacing);
153 }
154
155 canvas->restore();
156 canvas->translate(kXfermodeTypeSpacing, 0);
157 }
158
159 canvas->restore();
160 }
161
caryclark83ca6282015-06-10 09:31:09 -0700162 void onDraw(SkCanvas* canvas) override {
163 draw_pass(canvas, kCheckerboard_Pass);
halcanary96fcdcc2015-08-27 07:41:13 -0700164 canvas->saveLayer(nullptr, nullptr);
caryclark83ca6282015-06-10 09:31:09 -0700165
166 canvas->translate(kMargin, kMargin);
167 draw_pass(canvas, kBackground_Pass);
168
169 SkPaint titlePaint(fLabelPaint);
170 titlePaint.setTextSize(9 * titlePaint.getTextSize() / 8);
171 titlePaint.setFakeBoldText(true);
172 titlePaint.setTextAlign(SkPaint::kCenter_Align);
173 canvas->drawText("Porter Duff", sizeof("Porter Duff") - 1,
174 kLabelSpacing + 3 * kShapeTypeSpacing,
175 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
176 canvas->drawText("Advanced", sizeof("Advanced") - 1,
177 kXfermodeTypeSpacing + kLabelSpacing + 3 * kShapeTypeSpacing,
178 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
179
180 draw_pass(canvas, kShape_Pass);
181 canvas->restore();
182 }
183
cdalton6fd158e2015-05-27 15:08:33 -0700184 void drawModeName(SkCanvas* canvas, SkXfermode::Mode mode) {
185 const char* modeName = mode <= SkXfermode::kLastMode ? SkXfermode::ModeName(mode)
186 : "Arithmetic";
187 fLabelPaint.setTextAlign(SkPaint::kRight_Align);
188 canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize / 4,
189 fLabelPaint.getTextSize() / 3, fLabelPaint);
190 }
191
192 void setupShapePaint(SkCanvas* canvas, GrColor color, SkXfermode::Mode mode, SkPaint* paint) {
193 paint->setColor(color);
194
195 if (mode == SkXfermode::kPlus_Mode) {
196 // Check for overflow, otherwise we might get confusing AA artifacts.
197 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color),
198 SkColorGetR(kBGColor) + SkColorGetR(color)),
199 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color),
200 SkColorGetB(kBGColor) + SkColorGetB(color)));
201
202 if (maxSum > 255) {
203 SkPaint dimPaint;
204 dimPaint.setAntiAlias(false);
205 dimPaint.setXfermode(SkXfermode::Create(SkXfermode::kDstIn_Mode));
206 if (255 != paint->getAlpha()) {
207 // Dim the src and dst colors.
208 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0);
209 paint->setAlpha(255 * paint->getAlpha() / maxSum);
210 } else {
211 // Just clear the dst, we need to preserve the paint's opacity.
212 dimPaint.setARGB(0, 0, 0, 0);
213 }
214 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2,
215 kShapeSpacing/2 + 2 * kShapeTypeSpacing,
216 kShapeSpacing/2, dimPaint);
217 }
218 }
219 }
220
221 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkXfermode::Mode mode) {
222 SkPaint shapePaint(paint);
223 shapePaint.setAntiAlias(kSquare_Shape != shape);
224
225 SkAutoTUnref<SkXfermode> xfermode;
226 if (mode <= SkXfermode::kLastMode) {
227 xfermode.reset(SkXfermode::Create(mode));
228 } else {
229 xfermode.reset(SkArithmeticMode::Create(+1.0f, +0.25f, -0.5f, +0.1f));
230 }
231 shapePaint.setXfermode(xfermode);
232
233 switch (shape) {
234 case kSquare_Shape:
235 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
236 shapePaint);
237 break;
238
239 case kDiamond_Shape:
240 canvas->save();
241 canvas->rotate(45);
242 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
243 shapePaint);
244 canvas->restore();
245 break;
246
247 case kOval_Shape:
248 canvas->save();
249 canvas->rotate(static_cast<SkScalar>((511 * mode + 257) % 360));
250 canvas->drawPath(fPath, shapePaint);
251 canvas->restore();
252 break;
253
254 default:
255 SkFAIL("Invalid shape.");
256 }
257 }
258
259private:
260 SkPaint fLabelPaint;
261 SkPath fPath;
262
263 typedef GM INHERITED;
264};
265
266//////////////////////////////////////////////////////////////////////////////
267
268static GM* MyFactory(void*) { return new AAXfermodesGM; }
269static GMRegistry reg(MyFactory);
270
271}