blob: 14073df53820be4790de50b87b55c433e5ec6a01 [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,
ethannicholas3819d2d2015-12-17 10:58:28 -080019 kPaintSpacing = 4 * kShapeTypeSpacing,
cdalton6fd158e2015-05-27 15:08:33 -070020 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,
ethannicholas3819d2d2015-12-17 10:58:28 -080038 kConcave_Shape,
cdalton6fd158e2015-05-27 15:08:33 -070039
ethannicholas3819d2d2015-12-17 10:58:28 -080040 kLast_Shape = kConcave_Shape
cdalton6fd158e2015-05-27 15:08:33 -070041};
42
43namespace skiagm {
44
45/**
46 * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown
47 * src colors.
48 */
49class AAXfermodesGM : public GM {
50public:
51 AAXfermodesGM() {}
52
53protected:
caryclark83ca6282015-06-10 09:31:09 -070054 enum DrawingPass {
55 kCheckerboard_Pass,
56 kBackground_Pass,
57 kShape_Pass
58 };
59
cdalton6fd158e2015-05-27 15:08:33 -070060 SkString onShortName() override {
61 return SkString("aaxfermodes");
62 }
63
64 SkISize onISize() override {
65 return SkISize::Make(2 * kMargin + 2 * kXfermodeTypeSpacing -
66 (kXfermodeTypeSpacing - (kLabelSpacing + 2 * kPaintSpacing)),
67 2 * kMargin + kTitleSpacing + kSubtitleSpacing +
68 (1 + SkXfermode::kLastCoeffMode) * kShapeSpacing);
69 }
70
71 void onOnceBeforeDraw() override {
72 fLabelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070073 sk_tool_utils::set_portable_typeface(&fLabelPaint);
cdalton6fd158e2015-05-27 15:08:33 -070074 fLabelPaint.setTextSize(5 * kShapeSize/8);
75 fLabelPaint.setSubpixelText(true);
76
77 static const SkScalar radius = -1.4f * kShapeSize/2;
78 SkPoint pts[4] = {
79 {-radius, 0},
80 {0, -1.33f * radius},
81 {radius, 0},
82 {0, 1.33f * radius}
83 };
ethannicholas3819d2d2015-12-17 10:58:28 -080084 fOval.moveTo(pts[0]);
85 fOval.quadTo(pts[1], pts[2]);
86 fOval.quadTo(pts[3], pts[0]);
87
88 fConcave.moveTo(-radius, 0);
89 fConcave.quadTo(0, 0, 0, -radius);
90 fConcave.quadTo(0, 0, radius, 0);
91 fConcave.quadTo(0, 0, 0, radius);
92 fConcave.quadTo(0, 0, -radius, 0);
93 fConcave.close();
cdalton6fd158e2015-05-27 15:08:33 -070094 }
95
caryclark83ca6282015-06-10 09:31:09 -070096 void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) {
97 SkRect clipRect =
98 { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 };
cdalton6fd158e2015-05-27 15:08:33 -070099
caryclark83ca6282015-06-10 09:31:09 -0700100 canvas->save();
101 if (kCheckerboard_Pass == drawingPass) {
102 canvas->translate(kMargin, kMargin);
103 }
cdalton6fd158e2015-05-27 15:08:33 -0700104 canvas->translate(0, kTitleSpacing);
105
106 for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) {
107 size_t firstMode = (SkXfermode::kLastCoeffMode + 1) * xfermodeSet;
108 canvas->save();
109
caryclark83ca6282015-06-10 09:31:09 -0700110 if (kShape_Pass == drawingPass) {
111 fLabelPaint.setTextAlign(SkPaint::kCenter_Align);
112 canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1,
ethannicholas3819d2d2015-12-17 10:58:28 -0800113 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
caryclark83ca6282015-06-10 09:31:09 -0700114 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
115 canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1,
ethannicholas3819d2d2015-12-17 10:58:28 -0800116 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
117 kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3,
118 fLabelPaint);
caryclark83ca6282015-06-10 09:31:09 -0700119 }
cdalton6fd158e2015-05-27 15:08:33 -0700120
121 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2);
122
123 for (size_t m = 0; m <= SkXfermode::kLastCoeffMode; m++) {
124 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(firstMode + m);
125 canvas->save();
126
caryclark83ca6282015-06-10 09:31:09 -0700127 if (kShape_Pass == drawingPass) {
128 this->drawModeName(canvas, mode);
129 }
cdalton6fd158e2015-05-27 15:08:33 -0700130 canvas->translate(kLabelSpacing + kShapeSpacing/2, 0);
131
132 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); colorIdx++) {
133 SkPaint paint;
134 this->setupShapePaint(canvas, kShapeColors[colorIdx], mode, &paint);
135 SkASSERT(colorIdx == 0 || 255 == paint.getAlpha());
136 canvas->save();
137
138 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) {
caryclark83ca6282015-06-10 09:31:09 -0700139 if (kShape_Pass != drawingPass) {
140 canvas->save();
141 canvas->clipRect(clipRect);
142 if (kCheckerboard_Pass == drawingPass) {
143 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6,
144 10);
145 } else {
146 SkASSERT(kBackground_Pass == drawingPass);
147 canvas->drawColor(kBGColor, SkXfermode::kSrc_Mode);
148 }
149 canvas->restore();
150 } else {
151 this->drawShape(canvas, static_cast<Shape>(shapeIdx), paint, mode);
152 }
cdalton6fd158e2015-05-27 15:08:33 -0700153 canvas->translate(kShapeTypeSpacing, 0);
154 }
155
156 canvas->restore();
157 canvas->translate(kPaintSpacing, 0);
158 }
159
160 canvas->restore();
161 canvas->translate(0, kShapeSpacing);
162 }
163
164 canvas->restore();
165 canvas->translate(kXfermodeTypeSpacing, 0);
166 }
167
168 canvas->restore();
169 }
170
caryclark83ca6282015-06-10 09:31:09 -0700171 void onDraw(SkCanvas* canvas) override {
172 draw_pass(canvas, kCheckerboard_Pass);
halcanary96fcdcc2015-08-27 07:41:13 -0700173 canvas->saveLayer(nullptr, nullptr);
caryclark83ca6282015-06-10 09:31:09 -0700174
175 canvas->translate(kMargin, kMargin);
176 draw_pass(canvas, kBackground_Pass);
177
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,
ethannicholas3819d2d2015-12-17 10:58:28 -0800183 kLabelSpacing + 4 * kShapeTypeSpacing,
caryclark83ca6282015-06-10 09:31:09 -0700184 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
185 canvas->drawText("Advanced", sizeof("Advanced") - 1,
ethannicholas3819d2d2015-12-17 10:58:28 -0800186 kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing,
caryclark83ca6282015-06-10 09:31:09 -0700187 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
188
189 draw_pass(canvas, kShape_Pass);
190 canvas->restore();
191 }
192
cdalton6fd158e2015-05-27 15:08:33 -0700193 void drawModeName(SkCanvas* canvas, SkXfermode::Mode mode) {
194 const char* modeName = mode <= SkXfermode::kLastMode ? SkXfermode::ModeName(mode)
195 : "Arithmetic";
196 fLabelPaint.setTextAlign(SkPaint::kRight_Align);
197 canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize / 4,
ethannicholas3819d2d2015-12-17 10:58:28 -0800198 fLabelPaint.getTextSize() / 4, fLabelPaint);
cdalton6fd158e2015-05-27 15:08:33 -0700199 }
200
201 void setupShapePaint(SkCanvas* canvas, GrColor color, SkXfermode::Mode mode, SkPaint* paint) {
202 paint->setColor(color);
203
204 if (mode == SkXfermode::kPlus_Mode) {
205 // Check for overflow, otherwise we might get confusing AA artifacts.
206 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color),
207 SkColorGetR(kBGColor) + SkColorGetR(color)),
208 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color),
209 SkColorGetB(kBGColor) + SkColorGetB(color)));
210
211 if (maxSum > 255) {
212 SkPaint dimPaint;
213 dimPaint.setAntiAlias(false);
214 dimPaint.setXfermode(SkXfermode::Create(SkXfermode::kDstIn_Mode));
215 if (255 != paint->getAlpha()) {
216 // Dim the src and dst colors.
217 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0);
218 paint->setAlpha(255 * paint->getAlpha() / maxSum);
219 } else {
220 // Just clear the dst, we need to preserve the paint's opacity.
221 dimPaint.setARGB(0, 0, 0, 0);
222 }
223 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2,
ethannicholas3819d2d2015-12-17 10:58:28 -0800224 kShapeSpacing/2 + 3 * kShapeTypeSpacing,
cdalton6fd158e2015-05-27 15:08:33 -0700225 kShapeSpacing/2, dimPaint);
226 }
227 }
228 }
229
230 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkXfermode::Mode mode) {
231 SkPaint shapePaint(paint);
232 shapePaint.setAntiAlias(kSquare_Shape != shape);
233
234 SkAutoTUnref<SkXfermode> xfermode;
235 if (mode <= SkXfermode::kLastMode) {
236 xfermode.reset(SkXfermode::Create(mode));
237 } else {
238 xfermode.reset(SkArithmeticMode::Create(+1.0f, +0.25f, -0.5f, +0.1f));
239 }
240 shapePaint.setXfermode(xfermode);
241
242 switch (shape) {
243 case kSquare_Shape:
244 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
245 shapePaint);
246 break;
247
248 case kDiamond_Shape:
249 canvas->save();
250 canvas->rotate(45);
251 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
252 shapePaint);
253 canvas->restore();
254 break;
255
256 case kOval_Shape:
257 canvas->save();
258 canvas->rotate(static_cast<SkScalar>((511 * mode + 257) % 360));
ethannicholas3819d2d2015-12-17 10:58:28 -0800259 canvas->drawPath(fOval, shapePaint);
cdalton6fd158e2015-05-27 15:08:33 -0700260 canvas->restore();
261 break;
262
ethannicholas3819d2d2015-12-17 10:58:28 -0800263 case kConcave_Shape:
264 canvas->drawPath(fConcave, shapePaint);
265 break;
266
cdalton6fd158e2015-05-27 15:08:33 -0700267 default:
268 SkFAIL("Invalid shape.");
269 }
270 }
271
272private:
273 SkPaint fLabelPaint;
ethannicholas3819d2d2015-12-17 10:58:28 -0800274 SkPath fOval;
275 SkPath fConcave;
cdalton6fd158e2015-05-27 15:08:33 -0700276
277 typedef GM INHERITED;
278};
279
280//////////////////////////////////////////////////////////////////////////////
281
282static GM* MyFactory(void*) { return new AAXfermodesGM; }
283static GMRegistry reg(MyFactory);
284
285}