cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 1 | |
| 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" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 10 | #include "SkPath.h" |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 11 | #include "SkShader.h" |
| 12 | #include "SkXfermode.h" |
| 13 | |
| 14 | enum { |
| 15 | kXfermodeCount = SkXfermode::kLastMode + 2, // All xfermodes plus arithmetic mode. |
| 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 | |
| 27 | static const SkColor kBGColor = SkColorSetARGB(200, 210, 184, 135); |
| 28 | |
| 29 | static const SkColor kShapeColors[2] = { |
| 30 | SkColorSetARGB(130, 255, 0, 128), // input color unknown |
| 31 | SkColorSetARGB(255, 0, 255, 255) // input color opaque |
| 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 | |
| 43 | namespace skiagm { |
| 44 | |
| 45 | /** |
| 46 | * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown |
| 47 | * src colors. |
| 48 | */ |
| 49 | class AAXfermodesGM : public GM { |
| 50 | public: |
| 51 | AAXfermodesGM() {} |
| 52 | |
| 53 | protected: |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 54 | enum DrawingPass { |
| 55 | kCheckerboard_Pass, |
| 56 | kBackground_Pass, |
| 57 | kShape_Pass |
| 58 | }; |
| 59 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 60 | 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); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 73 | sk_tool_utils::set_portable_typeface(&fLabelPaint); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 74 | 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 | }; |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 84 | 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(); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 94 | } |
| 95 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 96 | void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) { |
| 97 | SkRect clipRect = |
| 98 | { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 }; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 99 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 100 | canvas->save(); |
| 101 | if (kCheckerboard_Pass == drawingPass) { |
| 102 | canvas->translate(kMargin, kMargin); |
| 103 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 104 | 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 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 110 | if (kShape_Pass == drawingPass) { |
| 111 | fLabelPaint.setTextAlign(SkPaint::kCenter_Align); |
| 112 | canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1, |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 113 | kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2, |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 114 | kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint); |
| 115 | canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1, |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 116 | kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 + |
| 117 | kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, |
| 118 | fLabelPaint); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 119 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 120 | |
| 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 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 127 | if (kShape_Pass == drawingPass) { |
| 128 | this->drawModeName(canvas, mode); |
| 129 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 130 | 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++) { |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 139 | 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 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 153 | 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 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 171 | void onDraw(SkCanvas* canvas) override { |
| 172 | draw_pass(canvas, kCheckerboard_Pass); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 173 | canvas->saveLayer(nullptr, nullptr); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 174 | |
| 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, |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 183 | kLabelSpacing + 4 * kShapeTypeSpacing, |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 184 | kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint); |
| 185 | canvas->drawText("Advanced", sizeof("Advanced") - 1, |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 186 | kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing, |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 187 | kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint); |
| 188 | |
| 189 | draw_pass(canvas, kShape_Pass); |
| 190 | canvas->restore(); |
| 191 | } |
| 192 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 193 | 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, |
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 | |
| 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, |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 224 | kShapeSpacing/2 + 3 * kShapeTypeSpacing, |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 225 | 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)); |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 259 | canvas->drawPath(fOval, shapePaint); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 260 | canvas->restore(); |
| 261 | break; |
| 262 | |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 263 | case kConcave_Shape: |
| 264 | canvas->drawPath(fConcave, shapePaint); |
| 265 | break; |
| 266 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 267 | default: |
| 268 | SkFAIL("Invalid shape."); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | private: |
| 273 | SkPaint fLabelPaint; |
ethannicholas | 3819d2d | 2015-12-17 10:58:28 -0800 | [diff] [blame] | 274 | SkPath fOval; |
| 275 | SkPath fConcave; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 276 | |
| 277 | typedef GM INHERITED; |
| 278 | }; |
| 279 | |
| 280 | ////////////////////////////////////////////////////////////////////////////// |
| 281 | |
| 282 | static GM* MyFactory(void*) { return new AAXfermodesGM; } |
| 283 | static GMRegistry reg(MyFactory); |
| 284 | |
| 285 | } |