cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 1 | /* |
| 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 Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 7 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 10 | #include "SkBlendModePriv.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkPath.h" |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 12 | #include "SkShader.h" |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 13 | |
| 14 | enum { |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 15 | kXfermodeCount = (int)SkBlendMode::kLastMode + 1 + 1, // extra for arith |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 16 | kShapeSize = 22, |
| 17 | kShapeSpacing = 36, |
| 18 | kShapeTypeSpacing = 4 * kShapeSpacing / 3, |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 19 | kPaintSpacing = 4 * kShapeTypeSpacing, |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 27 | constexpr SkColor kBGColor = 0xc8d2b887; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 28 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 29 | constexpr SkColor kShapeColors[2] = { |
| 30 | 0x82ff0080, // input color unknown |
| 31 | 0xff00ffff, // input color opaque |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | enum Shape { |
| 35 | kSquare_Shape, |
| 36 | kDiamond_Shape, |
| 37 | kOval_Shape, |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 38 | kConcave_Shape, |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 39 | |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 40 | kLast_Shape = kConcave_Shape |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown |
| 45 | * src colors. |
| 46 | */ |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 47 | class AAXfermodesGM : public skiagm::GM { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 48 | public: |
| 49 | AAXfermodesGM() {} |
| 50 | |
| 51 | protected: |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 52 | enum DrawingPass { |
| 53 | kCheckerboard_Pass, |
| 54 | kBackground_Pass, |
| 55 | kShape_Pass |
| 56 | }; |
| 57 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 58 | 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 Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 66 | (1 + (int)SkBlendMode::kLastCoeffMode) * kShapeSpacing); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void onOnceBeforeDraw() override { |
| 70 | fLabelPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 71 | sk_tool_utils::set_portable_typeface(&fLabelPaint); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 72 | fLabelPaint.setTextSize(5 * kShapeSize/8); |
| 73 | fLabelPaint.setSubpixelText(true); |
| 74 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 75 | constexpr SkScalar radius = -1.4f * kShapeSize/2; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 76 | SkPoint pts[4] = { |
| 77 | {-radius, 0}, |
| 78 | {0, -1.33f * radius}, |
| 79 | {radius, 0}, |
| 80 | {0, 1.33f * radius} |
| 81 | }; |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 82 | 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(); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 92 | } |
| 93 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 94 | void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) { |
| 95 | SkRect clipRect = |
| 96 | { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 }; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 97 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 98 | canvas->save(); |
| 99 | if (kCheckerboard_Pass == drawingPass) { |
| 100 | canvas->translate(kMargin, kMargin); |
| 101 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 102 | canvas->translate(0, kTitleSpacing); |
| 103 | |
| 104 | for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) { |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 105 | size_t firstMode = ((size_t)SkBlendMode::kLastCoeffMode + 1) * xfermodeSet; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 106 | canvas->save(); |
| 107 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 108 | if (kShape_Pass == drawingPass) { |
| 109 | fLabelPaint.setTextAlign(SkPaint::kCenter_Align); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 110 | canvas->drawString("Src Unknown", |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 111 | kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2, |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 112 | kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 113 | canvas->drawString("Src Opaque", |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 114 | kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 + |
| 115 | kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 116 | fLabelPaint); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 117 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 118 | |
| 119 | canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2); |
| 120 | |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 121 | for (size_t m = 0; m <= (size_t)SkBlendMode::kLastCoeffMode; m++) { |
| 122 | if (firstMode + m > (size_t)SkBlendMode::kLastMode) { |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 123 | break; |
| 124 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 125 | SkBlendMode mode = static_cast<SkBlendMode>(firstMode + m); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 126 | canvas->save(); |
| 127 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 128 | if (kShape_Pass == drawingPass) { |
| 129 | this->drawModeName(canvas, mode); |
| 130 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 131 | 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++) { |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 140 | 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); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 148 | canvas->drawColor(kBGColor, SkBlendMode::kSrc); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 149 | } |
| 150 | canvas->restore(); |
| 151 | } else { |
| 152 | this->drawShape(canvas, static_cast<Shape>(shapeIdx), paint, mode); |
| 153 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 154 | 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 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 172 | void onDraw(SkCanvas* canvas) override { |
| 173 | draw_pass(canvas, kCheckerboard_Pass); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | canvas->saveLayer(nullptr, nullptr); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 175 | |
| 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 Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 183 | canvas->drawString("Porter Duff", |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 184 | kLabelSpacing + 4 * kShapeTypeSpacing, |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 185 | kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 186 | canvas->drawString("Advanced", |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 187 | kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing, |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 188 | kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint); |
| 189 | |
| 190 | draw_pass(canvas, kShape_Pass); |
| 191 | canvas->restore(); |
| 192 | } |
| 193 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 194 | void drawModeName(SkCanvas* canvas, SkBlendMode mode) { |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 195 | const char* modeName = SkBlendMode_Name(mode); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 196 | fLabelPaint.setTextAlign(SkPaint::kRight_Align); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 197 | canvas->drawString(modeName, kLabelSpacing - kShapeSize / 4, |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 198 | fLabelPaint.getTextSize() / 4, fLabelPaint); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 199 | } |
| 200 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 201 | void setupShapePaint(SkCanvas* canvas, GrColor color, SkBlendMode mode, SkPaint* paint) { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 202 | paint->setColor(color); |
| 203 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 204 | if (mode == SkBlendMode::kPlus) { |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 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); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 214 | dimPaint.setBlendMode(SkBlendMode::kDstIn); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 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 | } |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 223 | canvas->drawRect({ -kShapeSpacing/2, -kShapeSpacing/2, |
| 224 | kShapeSpacing/2 + 3 * kShapeTypeSpacing, kShapeSpacing/2 }, |
| 225 | dimPaint); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 230 | void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkBlendMode mode) { |
| 231 | SkASSERT(mode <= SkBlendMode::kLastMode); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 232 | SkPaint shapePaint(paint); |
| 233 | shapePaint.setAntiAlias(kSquare_Shape != shape); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 234 | shapePaint.setBlendMode(mode); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 235 | |
| 236 | switch (shape) { |
| 237 | case kSquare_Shape: |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 238 | canvas->drawRect({ -kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2 }, |
| 239 | shapePaint); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 240 | break; |
| 241 | |
| 242 | case kDiamond_Shape: |
| 243 | canvas->save(); |
| 244 | canvas->rotate(45); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 245 | canvas->drawRect({ -kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2 }, |
| 246 | shapePaint); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 247 | canvas->restore(); |
| 248 | break; |
| 249 | |
| 250 | case kOval_Shape: |
| 251 | canvas->save(); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 252 | canvas->rotate(static_cast<SkScalar>((511 * (int)mode + 257) % 360)); |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 253 | canvas->drawPath(fOval, shapePaint); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 254 | canvas->restore(); |
| 255 | break; |
| 256 | |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 257 | case kConcave_Shape: |
| 258 | canvas->drawPath(fConcave, shapePaint); |
| 259 | break; |
| 260 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 261 | default: |
| 262 | SkFAIL("Invalid shape."); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | private: |
| 267 | SkPaint fLabelPaint; |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 268 | SkPath fOval; |
| 269 | SkPath fConcave; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 270 | |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 271 | typedef skiagm::GM INHERITED; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 272 | }; |
Mike Reed | 403d154 | 2016-09-26 18:51:24 -0400 | [diff] [blame] | 273 | DEF_GM( return new AAXfermodesGM; ) |