blob: 7106ce817409afe06e21d7e8a9aa97b8517932a1 [file] [log] [blame]
cdalton6fd158e2015-05-27 15:08:33 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "gm.h"
8#include "SkArithmeticMode.h"
bungemand3ebb482015-08-05 13:57:49 -07009#include "SkPath.h"
cdalton6fd158e2015-05-27 15:08:33 -070010#include "SkShader.h"
11#include "SkXfermode.h"
12
13enum {
14 kXfermodeCount = SkXfermode::kLastMode + 2, // All xfermodes plus arithmetic mode.
15 kShapeSize = 22,
16 kShapeSpacing = 36,
17 kShapeTypeSpacing = 4 * kShapeSpacing / 3,
ethannicholas3819d2d2015-12-17 10:58:28 -080018 kPaintSpacing = 4 * kShapeTypeSpacing,
cdalton6fd158e2015-05-27 15:08:33 -070019 kLabelSpacing = 3 * kShapeSize,
20 kMargin = kShapeSpacing / 2,
21 kXfermodeTypeSpacing = kLabelSpacing + 2 * kPaintSpacing + kShapeTypeSpacing,
22 kTitleSpacing = 3 * kShapeSpacing / 4,
23 kSubtitleSpacing = 5 * kShapeSpacing / 8
24};
25
mtkleindbfd7ab2016-09-01 11:24:54 -070026constexpr SkColor kBGColor = 0xc8d2b887;
cdalton6fd158e2015-05-27 15:08:33 -070027
mtkleindbfd7ab2016-09-01 11:24:54 -070028constexpr SkColor kShapeColors[2] = {
29 0x82ff0080, // input color unknown
30 0xff00ffff, // input color opaque
cdalton6fd158e2015-05-27 15:08:33 -070031};
32
33enum Shape {
34 kSquare_Shape,
35 kDiamond_Shape,
36 kOval_Shape,
ethannicholas3819d2d2015-12-17 10:58:28 -080037 kConcave_Shape,
cdalton6fd158e2015-05-27 15:08:33 -070038
ethannicholas3819d2d2015-12-17 10:58:28 -080039 kLast_Shape = kConcave_Shape
cdalton6fd158e2015-05-27 15:08:33 -070040};
41
cdalton6fd158e2015-05-27 15:08:33 -070042/**
43 * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown
44 * src colors.
45 */
Mike Reed403d1542016-09-26 18:51:24 -040046class AAXfermodesGM : public skiagm::GM {
cdalton6fd158e2015-05-27 15:08:33 -070047public:
48 AAXfermodesGM() {}
49
50protected:
caryclark83ca6282015-06-10 09:31:09 -070051 enum DrawingPass {
52 kCheckerboard_Pass,
53 kBackground_Pass,
54 kShape_Pass
55 };
56
cdalton6fd158e2015-05-27 15:08:33 -070057 SkString onShortName() override {
58 return SkString("aaxfermodes");
59 }
60
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);
66 }
67
68 void onOnceBeforeDraw() override {
69 fLabelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070070 sk_tool_utils::set_portable_typeface(&fLabelPaint);
cdalton6fd158e2015-05-27 15:08:33 -070071 fLabelPaint.setTextSize(5 * kShapeSize/8);
72 fLabelPaint.setSubpixelText(true);
73
mtkleindbfd7ab2016-09-01 11:24:54 -070074 constexpr SkScalar radius = -1.4f * kShapeSize/2;
cdalton6fd158e2015-05-27 15:08:33 -070075 SkPoint pts[4] = {
76 {-radius, 0},
77 {0, -1.33f * radius},
78 {radius, 0},
79 {0, 1.33f * radius}
80 };
ethannicholas3819d2d2015-12-17 10:58:28 -080081 fOval.moveTo(pts[0]);
82 fOval.quadTo(pts[1], pts[2]);
83 fOval.quadTo(pts[3], pts[0]);
84
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);
90 fConcave.close();
cdalton6fd158e2015-05-27 15:08:33 -070091 }
92
caryclark83ca6282015-06-10 09:31:09 -070093 void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) {
94 SkRect clipRect =
95 { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 };
cdalton6fd158e2015-05-27 15:08:33 -070096
caryclark83ca6282015-06-10 09:31:09 -070097 canvas->save();
98 if (kCheckerboard_Pass == drawingPass) {
99 canvas->translate(kMargin, kMargin);
100 }
cdalton6fd158e2015-05-27 15:08:33 -0700101 canvas->translate(0, kTitleSpacing);
102
103 for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) {
104 size_t firstMode = (SkXfermode::kLastCoeffMode + 1) * xfermodeSet;
105 canvas->save();
106
caryclark83ca6282015-06-10 09:31:09 -0700107 if (kShape_Pass == drawingPass) {
108 fLabelPaint.setTextAlign(SkPaint::kCenter_Align);
109 canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1,
ethannicholas3819d2d2015-12-17 10:58:28 -0800110 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
caryclark83ca6282015-06-10 09:31:09 -0700111 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
112 canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1,
halcanary9d524f22016-03-29 09:03:52 -0700113 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
114 kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3,
ethannicholas3819d2d2015-12-17 10:58:28 -0800115 fLabelPaint);
caryclark83ca6282015-06-10 09:31:09 -0700116 }
cdalton6fd158e2015-05-27 15:08:33 -0700117
118 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2);
119
120 for (size_t m = 0; m <= SkXfermode::kLastCoeffMode; m++) {
Mike Reed403d1542016-09-26 18:51:24 -0400121 if (firstMode + m > SkXfermode::kLastMode) {
122 break;
123 }
reed374772b2016-10-05 17:33:02 -0700124 SkBlendMode mode = static_cast<SkBlendMode>(firstMode + m);
cdalton6fd158e2015-05-27 15:08:33 -0700125 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);
reed374772b2016-10-05 17:33:02 -0700147 canvas->drawColor(kBGColor, SkBlendMode::kSrc);
caryclark83ca6282015-06-10 09:31:09 -0700148 }
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
reed374772b2016-10-05 17:33:02 -0700193 void drawModeName(SkCanvas* canvas, SkBlendMode mode) {
194 const char* modeName = SkXfermode::ModeName(mode);
cdalton6fd158e2015-05-27 15:08:33 -0700195 fLabelPaint.setTextAlign(SkPaint::kRight_Align);
196 canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize / 4,
ethannicholas3819d2d2015-12-17 10:58:28 -0800197 fLabelPaint.getTextSize() / 4, fLabelPaint);
cdalton6fd158e2015-05-27 15:08:33 -0700198 }
199
reed374772b2016-10-05 17:33:02 -0700200 void setupShapePaint(SkCanvas* canvas, GrColor color, SkBlendMode mode, SkPaint* paint) {
cdalton6fd158e2015-05-27 15:08:33 -0700201 paint->setColor(color);
202
reed374772b2016-10-05 17:33:02 -0700203 if (mode == SkBlendMode::kPlus) {
cdalton6fd158e2015-05-27 15:08:33 -0700204 // 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)));
209
210 if (maxSum > 255) {
211 SkPaint dimPaint;
212 dimPaint.setAntiAlias(false);
reed374772b2016-10-05 17:33:02 -0700213 dimPaint.setBlendMode(SkBlendMode::kDstIn);
cdalton6fd158e2015-05-27 15:08:33 -0700214 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);
218 } else {
219 // Just clear the dst, we need to preserve the paint's opacity.
220 dimPaint.setARGB(0, 0, 0, 0);
221 }
222 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2,
ethannicholas3819d2d2015-12-17 10:58:28 -0800223 kShapeSpacing/2 + 3 * kShapeTypeSpacing,
cdalton6fd158e2015-05-27 15:08:33 -0700224 kShapeSpacing/2, dimPaint);
225 }
226 }
227 }
228
reed374772b2016-10-05 17:33:02 -0700229 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkBlendMode mode) {
230 SkASSERT(mode <= SkBlendMode::kLastMode);
cdalton6fd158e2015-05-27 15:08:33 -0700231 SkPaint shapePaint(paint);
232 shapePaint.setAntiAlias(kSquare_Shape != shape);
reed374772b2016-10-05 17:33:02 -0700233 shapePaint.setBlendMode(mode);
cdalton6fd158e2015-05-27 15:08:33 -0700234
235 switch (shape) {
236 case kSquare_Shape:
237 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
238 shapePaint);
239 break;
240
241 case kDiamond_Shape:
242 canvas->save();
243 canvas->rotate(45);
244 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2,
245 shapePaint);
246 canvas->restore();
247 break;
248
249 case kOval_Shape:
250 canvas->save();
reed374772b2016-10-05 17:33:02 -0700251 canvas->rotate(static_cast<SkScalar>((511 * (int)mode + 257) % 360));
ethannicholas3819d2d2015-12-17 10:58:28 -0800252 canvas->drawPath(fOval, shapePaint);
cdalton6fd158e2015-05-27 15:08:33 -0700253 canvas->restore();
254 break;
255
ethannicholas3819d2d2015-12-17 10:58:28 -0800256 case kConcave_Shape:
257 canvas->drawPath(fConcave, shapePaint);
258 break;
259
cdalton6fd158e2015-05-27 15:08:33 -0700260 default:
261 SkFAIL("Invalid shape.");
262 }
263 }
264
265private:
266 SkPaint fLabelPaint;
ethannicholas3819d2d2015-12-17 10:58:28 -0800267 SkPath fOval;
268 SkPath fConcave;
cdalton6fd158e2015-05-27 15:08:33 -0700269
Mike Reed403d1542016-09-26 18:51:24 -0400270 typedef skiagm::GM INHERITED;
cdalton6fd158e2015-05-27 15:08:33 -0700271};
Mike Reed403d1542016-09-26 18:51:24 -0400272DEF_GM( return new AAXfermodesGM; )