blob: c373841df27f6c533be454ccd2d32f0bb6f5fab2 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkPaint.h"
Mike Reede9d783c2020-08-17 14:14:13 -040014#include "include/core/SkPathBuilder.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040015#include "include/core/SkPoint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
20#include "include/core/SkTypeface.h"
21#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/utils/SkTextUtils.h"
23#include "tools/ToolUtils.h"
cdalton6fd158e2015-05-27 15:08:33 -070024
25enum {
Mike Reedd4706732016-11-15 16:44:34 -050026 kXfermodeCount = (int)SkBlendMode::kLastMode + 1 + 1, // extra for arith
cdalton6fd158e2015-05-27 15:08:33 -070027 kShapeSize = 22,
28 kShapeSpacing = 36,
29 kShapeTypeSpacing = 4 * kShapeSpacing / 3,
ethannicholas3819d2d2015-12-17 10:58:28 -080030 kPaintSpacing = 4 * kShapeTypeSpacing,
cdalton6fd158e2015-05-27 15:08:33 -070031 kLabelSpacing = 3 * kShapeSize,
32 kMargin = kShapeSpacing / 2,
33 kXfermodeTypeSpacing = kLabelSpacing + 2 * kPaintSpacing + kShapeTypeSpacing,
34 kTitleSpacing = 3 * kShapeSpacing / 4,
35 kSubtitleSpacing = 5 * kShapeSpacing / 8
36};
37
mtkleindbfd7ab2016-09-01 11:24:54 -070038constexpr SkColor kBGColor = 0xc8d2b887;
cdalton6fd158e2015-05-27 15:08:33 -070039
mtkleindbfd7ab2016-09-01 11:24:54 -070040constexpr SkColor kShapeColors[2] = {
41 0x82ff0080, // input color unknown
42 0xff00ffff, // input color opaque
cdalton6fd158e2015-05-27 15:08:33 -070043};
44
45enum Shape {
46 kSquare_Shape,
47 kDiamond_Shape,
48 kOval_Shape,
ethannicholas3819d2d2015-12-17 10:58:28 -080049 kConcave_Shape,
cdalton6fd158e2015-05-27 15:08:33 -070050
ethannicholas3819d2d2015-12-17 10:58:28 -080051 kLast_Shape = kConcave_Shape
cdalton6fd158e2015-05-27 15:08:33 -070052};
53
cdalton6fd158e2015-05-27 15:08:33 -070054/**
55 * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown
56 * src colors.
57 */
Mike Reed403d1542016-09-26 18:51:24 -040058class AAXfermodesGM : public skiagm::GM {
cdalton6fd158e2015-05-27 15:08:33 -070059public:
60 AAXfermodesGM() {}
61
62protected:
caryclark83ca6282015-06-10 09:31:09 -070063 enum DrawingPass {
64 kCheckerboard_Pass,
65 kBackground_Pass,
66 kShape_Pass
67 };
68
cdalton6fd158e2015-05-27 15:08:33 -070069 SkString onShortName() override {
70 return SkString("aaxfermodes");
71 }
72
73 SkISize onISize() override {
74 return SkISize::Make(2 * kMargin + 2 * kXfermodeTypeSpacing -
75 (kXfermodeTypeSpacing - (kLabelSpacing + 2 * kPaintSpacing)),
76 2 * kMargin + kTitleSpacing + kSubtitleSpacing +
Mike Reedd4706732016-11-15 16:44:34 -050077 (1 + (int)SkBlendMode::kLastCoeffMode) * kShapeSpacing);
cdalton6fd158e2015-05-27 15:08:33 -070078 }
79
80 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050081 fLabelFont.setTypeface(ToolUtils::create_portable_typeface());
Mike Reedb579f072019-01-03 15:45:53 -050082 fLabelFont.setSize(5 * kShapeSize/8);
83 fLabelFont.setSubpixel(true);
cdalton6fd158e2015-05-27 15:08:33 -070084
mtkleindbfd7ab2016-09-01 11:24:54 -070085 constexpr SkScalar radius = -1.4f * kShapeSize/2;
cdalton6fd158e2015-05-27 15:08:33 -070086 SkPoint pts[4] = {
87 {-radius, 0},
88 {0, -1.33f * radius},
89 {radius, 0},
90 {0, 1.33f * radius}
91 };
Mike Reede9d783c2020-08-17 14:14:13 -040092 fOval = SkPathBuilder().moveTo(pts[0])
93 .quadTo(pts[1], pts[2])
94 .quadTo(pts[3], pts[0])
95 .detach();
ethannicholas3819d2d2015-12-17 10:58:28 -080096
Mike Reede9d783c2020-08-17 14:14:13 -040097 fConcave = SkPathBuilder().moveTo(-radius, 0)
98 .quadTo(0, 0, 0, -radius)
99 .quadTo(0, 0, radius, 0)
100 .quadTo(0, 0, 0, radius)
101 .quadTo(0, 0, -radius, 0)
102 .close()
103 .detach();
cdalton6fd158e2015-05-27 15:08:33 -0700104 }
105
caryclark83ca6282015-06-10 09:31:09 -0700106 void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) {
107 SkRect clipRect =
108 { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 };
cdalton6fd158e2015-05-27 15:08:33 -0700109
caryclark83ca6282015-06-10 09:31:09 -0700110 canvas->save();
111 if (kCheckerboard_Pass == drawingPass) {
112 canvas->translate(kMargin, kMargin);
113 }
cdalton6fd158e2015-05-27 15:08:33 -0700114 canvas->translate(0, kTitleSpacing);
115
116 for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) {
Mike Reedd4706732016-11-15 16:44:34 -0500117 size_t firstMode = ((size_t)SkBlendMode::kLastCoeffMode + 1) * xfermodeSet;
cdalton6fd158e2015-05-27 15:08:33 -0700118 canvas->save();
119
caryclark83ca6282015-06-10 09:31:09 -0700120 if (kShape_Pass == drawingPass) {
Mike Reed331ccfd2018-10-25 12:36:06 -0400121 SkTextUtils::DrawString(canvas, "Src Unknown",
ethannicholas3819d2d2015-12-17 10:58:28 -0800122 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
Mike Reedb579f072019-01-03 15:45:53 -0500123 kSubtitleSpacing / 2 + fLabelFont.getSize() / 3, fLabelFont, SkPaint(),
Mike Reed3a42ec02018-10-30 12:53:21 -0400124 SkTextUtils::kCenter_Align);
Mike Reed331ccfd2018-10-25 12:36:06 -0400125 SkTextUtils::DrawString(canvas, "Src Opaque",
halcanary9d524f22016-03-29 09:03:52 -0700126 kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
Mike Reedb579f072019-01-03 15:45:53 -0500127 kPaintSpacing, kSubtitleSpacing / 2 + fLabelFont.getSize() / 3,
128 fLabelFont, SkPaint(), SkTextUtils::kCenter_Align);
caryclark83ca6282015-06-10 09:31:09 -0700129 }
cdalton6fd158e2015-05-27 15:08:33 -0700130
131 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2);
132
Mike Reedd4706732016-11-15 16:44:34 -0500133 for (size_t m = 0; m <= (size_t)SkBlendMode::kLastCoeffMode; m++) {
134 if (firstMode + m > (size_t)SkBlendMode::kLastMode) {
Mike Reed403d1542016-09-26 18:51:24 -0400135 break;
136 }
reed374772b2016-10-05 17:33:02 -0700137 SkBlendMode mode = static_cast<SkBlendMode>(firstMode + m);
cdalton6fd158e2015-05-27 15:08:33 -0700138 canvas->save();
139
caryclark83ca6282015-06-10 09:31:09 -0700140 if (kShape_Pass == drawingPass) {
141 this->drawModeName(canvas, mode);
142 }
cdalton6fd158e2015-05-27 15:08:33 -0700143 canvas->translate(kLabelSpacing + kShapeSpacing/2, 0);
144
145 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); colorIdx++) {
146 SkPaint paint;
147 this->setupShapePaint(canvas, kShapeColors[colorIdx], mode, &paint);
148 SkASSERT(colorIdx == 0 || 255 == paint.getAlpha());
149 canvas->save();
150
151 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) {
caryclark83ca6282015-06-10 09:31:09 -0700152 if (kShape_Pass != drawingPass) {
153 canvas->save();
154 canvas->clipRect(clipRect);
155 if (kCheckerboard_Pass == drawingPass) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500156 ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 10);
caryclark83ca6282015-06-10 09:31:09 -0700157 } else {
158 SkASSERT(kBackground_Pass == drawingPass);
reed374772b2016-10-05 17:33:02 -0700159 canvas->drawColor(kBGColor, SkBlendMode::kSrc);
caryclark83ca6282015-06-10 09:31:09 -0700160 }
161 canvas->restore();
162 } else {
163 this->drawShape(canvas, static_cast<Shape>(shapeIdx), paint, mode);
164 }
cdalton6fd158e2015-05-27 15:08:33 -0700165 canvas->translate(kShapeTypeSpacing, 0);
166 }
167
168 canvas->restore();
169 canvas->translate(kPaintSpacing, 0);
170 }
171
172 canvas->restore();
173 canvas->translate(0, kShapeSpacing);
174 }
175
176 canvas->restore();
177 canvas->translate(kXfermodeTypeSpacing, 0);
178 }
179
180 canvas->restore();
181 }
182
caryclark83ca6282015-06-10 09:31:09 -0700183 void onDraw(SkCanvas* canvas) override {
184 draw_pass(canvas, kCheckerboard_Pass);
halcanary96fcdcc2015-08-27 07:41:13 -0700185 canvas->saveLayer(nullptr, nullptr);
caryclark83ca6282015-06-10 09:31:09 -0700186
187 canvas->translate(kMargin, kMargin);
188 draw_pass(canvas, kBackground_Pass);
189
Mike Reedb579f072019-01-03 15:45:53 -0500190 SkFont titleFont(fLabelFont);
191 titleFont.setSize(9 * titleFont.getSize() / 8);
192 titleFont.setEmbolden(true);
Mike Reed331ccfd2018-10-25 12:36:06 -0400193 SkTextUtils::DrawString(canvas, "Porter Duff",
194 kLabelSpacing + 4 * kShapeTypeSpacing,
Mike Reedb579f072019-01-03 15:45:53 -0500195 kTitleSpacing / 2 + titleFont.getSize() / 3, titleFont, SkPaint(),
Mike Reed3a42ec02018-10-30 12:53:21 -0400196 SkTextUtils::kCenter_Align);
Mike Reed331ccfd2018-10-25 12:36:06 -0400197 SkTextUtils::DrawString(canvas, "Advanced",
198 kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing,
Mike Reedb579f072019-01-03 15:45:53 -0500199 kTitleSpacing / 2 + titleFont.getSize() / 3, titleFont, SkPaint(),
Mike Reed3a42ec02018-10-30 12:53:21 -0400200 SkTextUtils::kCenter_Align);
caryclark83ca6282015-06-10 09:31:09 -0700201
202 draw_pass(canvas, kShape_Pass);
203 canvas->restore();
204 }
205
reed374772b2016-10-05 17:33:02 -0700206 void drawModeName(SkCanvas* canvas, SkBlendMode mode) {
Mike Reedd4706732016-11-15 16:44:34 -0500207 const char* modeName = SkBlendMode_Name(mode);
Mike Reed331ccfd2018-10-25 12:36:06 -0400208 SkTextUtils::DrawString(canvas, modeName, kLabelSpacing - kShapeSize / 4,
Mike Reedb579f072019-01-03 15:45:53 -0500209 fLabelFont.getSize() / 4, fLabelFont, SkPaint(),
Mike Reed3a42ec02018-10-30 12:53:21 -0400210 SkTextUtils::kRight_Align);
cdalton6fd158e2015-05-27 15:08:33 -0700211 }
212
Brian Osman93690312018-09-19 15:45:23 -0400213 void setupShapePaint(SkCanvas* canvas, SkColor color, SkBlendMode mode, SkPaint* paint) {
cdalton6fd158e2015-05-27 15:08:33 -0700214 paint->setColor(color);
215
reed374772b2016-10-05 17:33:02 -0700216 if (mode == SkBlendMode::kPlus) {
cdalton6fd158e2015-05-27 15:08:33 -0700217 // Check for overflow, otherwise we might get confusing AA artifacts.
Brian Osman788b9162020-02-07 10:36:46 -0500218 int maxSum = std::max(std::max(SkColorGetA(kBGColor) + SkColorGetA(color),
cdalton6fd158e2015-05-27 15:08:33 -0700219 SkColorGetR(kBGColor) + SkColorGetR(color)),
Brian Osman788b9162020-02-07 10:36:46 -0500220 std::max(SkColorGetG(kBGColor) + SkColorGetG(color),
cdalton6fd158e2015-05-27 15:08:33 -0700221 SkColorGetB(kBGColor) + SkColorGetB(color)));
222
223 if (maxSum > 255) {
224 SkPaint dimPaint;
225 dimPaint.setAntiAlias(false);
reed374772b2016-10-05 17:33:02 -0700226 dimPaint.setBlendMode(SkBlendMode::kDstIn);
cdalton6fd158e2015-05-27 15:08:33 -0700227 if (255 != paint->getAlpha()) {
228 // Dim the src and dst colors.
229 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0);
230 paint->setAlpha(255 * paint->getAlpha() / maxSum);
231 } else {
232 // Just clear the dst, we need to preserve the paint's opacity.
233 dimPaint.setARGB(0, 0, 0, 0);
234 }
Mike Reed3661bc92017-02-22 13:21:42 -0500235 canvas->drawRect({ -kShapeSpacing/2, -kShapeSpacing/2,
236 kShapeSpacing/2 + 3 * kShapeTypeSpacing, kShapeSpacing/2 },
237 dimPaint);
cdalton6fd158e2015-05-27 15:08:33 -0700238 }
239 }
240 }
241
reed374772b2016-10-05 17:33:02 -0700242 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkBlendMode mode) {
243 SkASSERT(mode <= SkBlendMode::kLastMode);
cdalton6fd158e2015-05-27 15:08:33 -0700244 SkPaint shapePaint(paint);
245 shapePaint.setAntiAlias(kSquare_Shape != shape);
reed374772b2016-10-05 17:33:02 -0700246 shapePaint.setBlendMode(mode);
cdalton6fd158e2015-05-27 15:08:33 -0700247
248 switch (shape) {
249 case kSquare_Shape:
Mike Reed3661bc92017-02-22 13:21:42 -0500250 canvas->drawRect({ -kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2 },
251 shapePaint);
cdalton6fd158e2015-05-27 15:08:33 -0700252 break;
253
254 case kDiamond_Shape:
255 canvas->save();
256 canvas->rotate(45);
Mike Reed3661bc92017-02-22 13:21:42 -0500257 canvas->drawRect({ -kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2 },
258 shapePaint);
cdalton6fd158e2015-05-27 15:08:33 -0700259 canvas->restore();
260 break;
261
262 case kOval_Shape:
263 canvas->save();
reed374772b2016-10-05 17:33:02 -0700264 canvas->rotate(static_cast<SkScalar>((511 * (int)mode + 257) % 360));
ethannicholas3819d2d2015-12-17 10:58:28 -0800265 canvas->drawPath(fOval, shapePaint);
cdalton6fd158e2015-05-27 15:08:33 -0700266 canvas->restore();
267 break;
268
ethannicholas3819d2d2015-12-17 10:58:28 -0800269 case kConcave_Shape:
270 canvas->drawPath(fConcave, shapePaint);
271 break;
272
cdalton6fd158e2015-05-27 15:08:33 -0700273 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400274 SK_ABORT("Invalid shape.");
cdalton6fd158e2015-05-27 15:08:33 -0700275 }
276 }
277
278private:
Mike Reedb579f072019-01-03 15:45:53 -0500279 SkFont fLabelFont;
ethannicholas3819d2d2015-12-17 10:58:28 -0800280 SkPath fOval;
281 SkPath fConcave;
cdalton6fd158e2015-05-27 15:08:33 -0700282
John Stiles7571f9e2020-09-02 22:42:33 -0400283 using INHERITED = skiagm::GM;
cdalton6fd158e2015-05-27 15:08:33 -0700284};
Mike Reed403d1542016-09-26 18:51:24 -0400285DEF_GM( return new AAXfermodesGM; )