blob: eab68b4e81d8f0a14e963a225f5dba5afaddfe9e [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 */
Mike Reedd4706732016-11-15 16:44:34 -05007
cdalton6fd158e2015-05-27 15:08:33 -07008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Mike Reedd4706732016-11-15 16:44:34 -050010#include "SkBlendModePriv.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
cdalton6fd158e2015-05-27 15:08:33 -070012#include "SkShader.h"
cdalton6fd158e2015-05-27 15:08:33 -070013
14enum {
Mike Reedd4706732016-11-15 16:44:34 -050015 kXfermodeCount = (int)SkBlendMode::kLastMode + 1 + 1, // extra for arith
cdalton6fd158e2015-05-27 15:08:33 -070016 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
mtkleindbfd7ab2016-09-01 11:24:54 -070027constexpr SkColor kBGColor = 0xc8d2b887;
cdalton6fd158e2015-05-27 15:08:33 -070028
mtkleindbfd7ab2016-09-01 11:24:54 -070029constexpr SkColor kShapeColors[2] = {
30 0x82ff0080, // input color unknown
31 0xff00ffff, // input color opaque
cdalton6fd158e2015-05-27 15:08:33 -070032};
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
cdalton6fd158e2015-05-27 15:08:33 -070043/**
44 * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown
45 * src colors.
46 */
Mike Reed403d1542016-09-26 18:51:24 -040047class AAXfermodesGM : public skiagm::GM {
cdalton6fd158e2015-05-27 15:08:33 -070048public:
49 AAXfermodesGM() {}
50
51protected:
caryclark83ca6282015-06-10 09:31:09 -070052 enum DrawingPass {
53 kCheckerboard_Pass,
54 kBackground_Pass,
55 kShape_Pass
56 };
57
cdalton6fd158e2015-05-27 15:08:33 -070058 SkString onShortName() override {
59 return SkString("aaxfermodes");
60 }
61
62 SkISize onISize() override {
63 return SkISize::Make(2 * kMargin + 2 * kXfermodeTypeSpacing -
64 (kXfermodeTypeSpacing - (kLabelSpacing + 2 * kPaintSpacing)),
65 2 * kMargin + kTitleSpacing + kSubtitleSpacing +
Mike Reedd4706732016-11-15 16:44:34 -050066 (1 + (int)SkBlendMode::kLastCoeffMode) * kShapeSpacing);
cdalton6fd158e2015-05-27 15:08:33 -070067 }
68
69 void onOnceBeforeDraw() override {
70 fLabelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070071 sk_tool_utils::set_portable_typeface(&fLabelPaint);
cdalton6fd158e2015-05-27 15:08:33 -070072 fLabelPaint.setTextSize(5 * kShapeSize/8);
73 fLabelPaint.setSubpixelText(true);
74
mtkleindbfd7ab2016-09-01 11:24:54 -070075 constexpr SkScalar radius = -1.4f * kShapeSize/2;
cdalton6fd158e2015-05-27 15:08:33 -070076 SkPoint pts[4] = {
77 {-radius, 0},
78 {0, -1.33f * radius},
79 {radius, 0},
80 {0, 1.33f * radius}
81 };
ethannicholas3819d2d2015-12-17 10:58:28 -080082 fOval.moveTo(pts[0]);
83 fOval.quadTo(pts[1], pts[2]);
84 fOval.quadTo(pts[3], pts[0]);
85
86 fConcave.moveTo(-radius, 0);
87 fConcave.quadTo(0, 0, 0, -radius);
88 fConcave.quadTo(0, 0, radius, 0);
89 fConcave.quadTo(0, 0, 0, radius);
90 fConcave.quadTo(0, 0, -radius, 0);
91 fConcave.close();
cdalton6fd158e2015-05-27 15:08:33 -070092 }
93
caryclark83ca6282015-06-10 09:31:09 -070094 void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) {
95 SkRect clipRect =
96 { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 };
cdalton6fd158e2015-05-27 15:08:33 -070097
caryclark83ca6282015-06-10 09:31:09 -070098 canvas->save();
99 if (kCheckerboard_Pass == drawingPass) {
100 canvas->translate(kMargin, kMargin);
101 }
cdalton6fd158e2015-05-27 15:08:33 -0700102 canvas->translate(0, kTitleSpacing);
103
104 for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) {
Mike Reedd4706732016-11-15 16:44:34 -0500105 size_t firstMode = ((size_t)SkBlendMode::kLastCoeffMode + 1) * xfermodeSet;
cdalton6fd158e2015-05-27 15:08:33 -0700106 canvas->save();
107
caryclark83ca6282015-06-10 09:31:09 -0700108 if (kShape_Pass == drawingPass) {
109 fLabelPaint.setTextAlign(SkPaint::kCenter_Align);
Cary Clark2a475ea2017-04-28 15:35:12 -0400110 canvas->drawString("Src Unknown",
ethannicholas3819d2d2015-12-17 10:58:28 -0800111 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
caryclark83ca6282015-06-10 09:31:09 -0700112 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
Cary Clark2a475ea2017-04-28 15:35:12 -0400113 canvas->drawString("Src Opaque",
halcanary9d524f22016-03-29 09:03:52 -0700114 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
115 kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3,
ethannicholas3819d2d2015-12-17 10:58:28 -0800116 fLabelPaint);
caryclark83ca6282015-06-10 09:31:09 -0700117 }
cdalton6fd158e2015-05-27 15:08:33 -0700118
119 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2);
120
Mike Reedd4706732016-11-15 16:44:34 -0500121 for (size_t m = 0; m <= (size_t)SkBlendMode::kLastCoeffMode; m++) {
122 if (firstMode + m > (size_t)SkBlendMode::kLastMode) {
Mike Reed403d1542016-09-26 18:51:24 -0400123 break;
124 }
reed374772b2016-10-05 17:33:02 -0700125 SkBlendMode mode = static_cast<SkBlendMode>(firstMode + m);
cdalton6fd158e2015-05-27 15:08:33 -0700126 canvas->save();
127
caryclark83ca6282015-06-10 09:31:09 -0700128 if (kShape_Pass == drawingPass) {
129 this->drawModeName(canvas, mode);
130 }
cdalton6fd158e2015-05-27 15:08:33 -0700131 canvas->translate(kLabelSpacing + kShapeSpacing/2, 0);
132
133 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); colorIdx++) {
134 SkPaint paint;
135 this->setupShapePaint(canvas, kShapeColors[colorIdx], mode, &paint);
136 SkASSERT(colorIdx == 0 || 255 == paint.getAlpha());
137 canvas->save();
138
139 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) {
caryclark83ca6282015-06-10 09:31:09 -0700140 if (kShape_Pass != drawingPass) {
141 canvas->save();
142 canvas->clipRect(clipRect);
143 if (kCheckerboard_Pass == drawingPass) {
144 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6,
145 10);
146 } else {
147 SkASSERT(kBackground_Pass == drawingPass);
reed374772b2016-10-05 17:33:02 -0700148 canvas->drawColor(kBGColor, SkBlendMode::kSrc);
caryclark83ca6282015-06-10 09:31:09 -0700149 }
150 canvas->restore();
151 } else {
152 this->drawShape(canvas, static_cast<Shape>(shapeIdx), paint, mode);
153 }
cdalton6fd158e2015-05-27 15:08:33 -0700154 canvas->translate(kShapeTypeSpacing, 0);
155 }
156
157 canvas->restore();
158 canvas->translate(kPaintSpacing, 0);
159 }
160
161 canvas->restore();
162 canvas->translate(0, kShapeSpacing);
163 }
164
165 canvas->restore();
166 canvas->translate(kXfermodeTypeSpacing, 0);
167 }
168
169 canvas->restore();
170 }
171
caryclark83ca6282015-06-10 09:31:09 -0700172 void onDraw(SkCanvas* canvas) override {
173 draw_pass(canvas, kCheckerboard_Pass);
halcanary96fcdcc2015-08-27 07:41:13 -0700174 canvas->saveLayer(nullptr, nullptr);
caryclark83ca6282015-06-10 09:31:09 -0700175
176 canvas->translate(kMargin, kMargin);
177 draw_pass(canvas, kBackground_Pass);
178
179 SkPaint titlePaint(fLabelPaint);
180 titlePaint.setTextSize(9 * titlePaint.getTextSize() / 8);
181 titlePaint.setFakeBoldText(true);
182 titlePaint.setTextAlign(SkPaint::kCenter_Align);
Cary Clark2a475ea2017-04-28 15:35:12 -0400183 canvas->drawString("Porter Duff",
ethannicholas3819d2d2015-12-17 10:58:28 -0800184 kLabelSpacing + 4 * kShapeTypeSpacing,
caryclark83ca6282015-06-10 09:31:09 -0700185 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
Cary Clark2a475ea2017-04-28 15:35:12 -0400186 canvas->drawString("Advanced",
ethannicholas3819d2d2015-12-17 10:58:28 -0800187 kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing,
caryclark83ca6282015-06-10 09:31:09 -0700188 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
189
190 draw_pass(canvas, kShape_Pass);
191 canvas->restore();
192 }
193
reed374772b2016-10-05 17:33:02 -0700194 void drawModeName(SkCanvas* canvas, SkBlendMode mode) {
Mike Reedd4706732016-11-15 16:44:34 -0500195 const char* modeName = SkBlendMode_Name(mode);
cdalton6fd158e2015-05-27 15:08:33 -0700196 fLabelPaint.setTextAlign(SkPaint::kRight_Align);
Cary Clark2a475ea2017-04-28 15:35:12 -0400197 canvas->drawString(modeName, kLabelSpacing - kShapeSize / 4,
ethannicholas3819d2d2015-12-17 10:58:28 -0800198 fLabelPaint.getTextSize() / 4, fLabelPaint);
cdalton6fd158e2015-05-27 15:08:33 -0700199 }
200
reed374772b2016-10-05 17:33:02 -0700201 void setupShapePaint(SkCanvas* canvas, GrColor color, SkBlendMode mode, SkPaint* paint) {
cdalton6fd158e2015-05-27 15:08:33 -0700202 paint->setColor(color);
203
reed374772b2016-10-05 17:33:02 -0700204 if (mode == SkBlendMode::kPlus) {
cdalton6fd158e2015-05-27 15:08:33 -0700205 // 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);
reed374772b2016-10-05 17:33:02 -0700214 dimPaint.setBlendMode(SkBlendMode::kDstIn);
cdalton6fd158e2015-05-27 15:08:33 -0700215 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 }
Mike Reed3661bc92017-02-22 13:21:42 -0500223 canvas->drawRect({ -kShapeSpacing/2, -kShapeSpacing/2,
224 kShapeSpacing/2 + 3 * kShapeTypeSpacing, kShapeSpacing/2 },
225 dimPaint);
cdalton6fd158e2015-05-27 15:08:33 -0700226 }
227 }
228 }
229
reed374772b2016-10-05 17:33:02 -0700230 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkBlendMode mode) {
231 SkASSERT(mode <= SkBlendMode::kLastMode);
cdalton6fd158e2015-05-27 15:08:33 -0700232 SkPaint shapePaint(paint);
233 shapePaint.setAntiAlias(kSquare_Shape != shape);
reed374772b2016-10-05 17:33:02 -0700234 shapePaint.setBlendMode(mode);
cdalton6fd158e2015-05-27 15:08:33 -0700235
236 switch (shape) {
237 case kSquare_Shape:
Mike Reed3661bc92017-02-22 13:21:42 -0500238 canvas->drawRect({ -kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2 },
239 shapePaint);
cdalton6fd158e2015-05-27 15:08:33 -0700240 break;
241
242 case kDiamond_Shape:
243 canvas->save();
244 canvas->rotate(45);
Mike Reed3661bc92017-02-22 13:21:42 -0500245 canvas->drawRect({ -kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2 },
246 shapePaint);
cdalton6fd158e2015-05-27 15:08:33 -0700247 canvas->restore();
248 break;
249
250 case kOval_Shape:
251 canvas->save();
reed374772b2016-10-05 17:33:02 -0700252 canvas->rotate(static_cast<SkScalar>((511 * (int)mode + 257) % 360));
ethannicholas3819d2d2015-12-17 10:58:28 -0800253 canvas->drawPath(fOval, shapePaint);
cdalton6fd158e2015-05-27 15:08:33 -0700254 canvas->restore();
255 break;
256
ethannicholas3819d2d2015-12-17 10:58:28 -0800257 case kConcave_Shape:
258 canvas->drawPath(fConcave, shapePaint);
259 break;
260
cdalton6fd158e2015-05-27 15:08:33 -0700261 default:
262 SkFAIL("Invalid shape.");
263 }
264 }
265
266private:
267 SkPaint fLabelPaint;
ethannicholas3819d2d2015-12-17 10:58:28 -0800268 SkPath fOval;
269 SkPath fConcave;
cdalton6fd158e2015-05-27 15:08:33 -0700270
Mike Reed403d1542016-09-26 18:51:24 -0400271 typedef skiagm::GM INHERITED;
cdalton6fd158e2015-05-27 15:08:33 -0700272};
Mike Reed403d1542016-09-26 18:51:24 -0400273DEF_GM( return new AAXfermodesGM; )