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, |
| 19 | kPaintSpacing = 3 * kShapeTypeSpacing, |
| 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, |
| 38 | |
| 39 | kLast_Shape = kOval_Shape |
| 40 | }; |
| 41 | |
| 42 | namespace skiagm { |
| 43 | |
| 44 | /** |
| 45 | * Verifies AA works properly on all Xfermodes, including arithmetic, with both opaque and unknown |
| 46 | * src colors. |
| 47 | */ |
| 48 | class AAXfermodesGM : public GM { |
| 49 | public: |
| 50 | AAXfermodesGM() {} |
| 51 | |
| 52 | protected: |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 53 | enum DrawingPass { |
| 54 | kCheckerboard_Pass, |
| 55 | kBackground_Pass, |
| 56 | kShape_Pass |
| 57 | }; |
| 58 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 59 | SkString onShortName() override { |
| 60 | return SkString("aaxfermodes"); |
| 61 | } |
| 62 | |
| 63 | SkISize onISize() override { |
| 64 | return SkISize::Make(2 * kMargin + 2 * kXfermodeTypeSpacing - |
| 65 | (kXfermodeTypeSpacing - (kLabelSpacing + 2 * kPaintSpacing)), |
| 66 | 2 * kMargin + kTitleSpacing + kSubtitleSpacing + |
| 67 | (1 + SkXfermode::kLastCoeffMode) * kShapeSpacing); |
| 68 | } |
| 69 | |
| 70 | void onOnceBeforeDraw() override { |
| 71 | fLabelPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 72 | sk_tool_utils::set_portable_typeface(&fLabelPaint); |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 73 | fLabelPaint.setTextSize(5 * kShapeSize/8); |
| 74 | fLabelPaint.setSubpixelText(true); |
| 75 | |
| 76 | static const SkScalar radius = -1.4f * kShapeSize/2; |
| 77 | SkPoint pts[4] = { |
| 78 | {-radius, 0}, |
| 79 | {0, -1.33f * radius}, |
| 80 | {radius, 0}, |
| 81 | {0, 1.33f * radius} |
| 82 | }; |
| 83 | fPath.moveTo(pts[0]); |
| 84 | fPath.quadTo(pts[1], pts[2]); |
| 85 | fPath.quadTo(pts[3], pts[0]); |
| 86 | } |
| 87 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 88 | void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) { |
| 89 | SkRect clipRect = |
| 90 | { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 }; |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 91 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 92 | canvas->save(); |
| 93 | if (kCheckerboard_Pass == drawingPass) { |
| 94 | canvas->translate(kMargin, kMargin); |
| 95 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 96 | canvas->translate(0, kTitleSpacing); |
| 97 | |
| 98 | for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) { |
| 99 | size_t firstMode = (SkXfermode::kLastCoeffMode + 1) * xfermodeSet; |
| 100 | canvas->save(); |
| 101 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 102 | if (kShape_Pass == drawingPass) { |
| 103 | fLabelPaint.setTextAlign(SkPaint::kCenter_Align); |
| 104 | canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1, |
| 105 | kLabelSpacing + kShapeSpacing / 2 + kShapeTypeSpacing, |
| 106 | kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint); |
| 107 | canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1, |
| 108 | kLabelSpacing + kShapeSpacing / 2 + kShapeTypeSpacing + kPaintSpacing, |
| 109 | kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint); |
| 110 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 111 | |
| 112 | canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2); |
| 113 | |
| 114 | for (size_t m = 0; m <= SkXfermode::kLastCoeffMode; m++) { |
| 115 | SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(firstMode + m); |
| 116 | canvas->save(); |
| 117 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 118 | if (kShape_Pass == drawingPass) { |
| 119 | this->drawModeName(canvas, mode); |
| 120 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 121 | canvas->translate(kLabelSpacing + kShapeSpacing/2, 0); |
| 122 | |
| 123 | for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); colorIdx++) { |
| 124 | SkPaint paint; |
| 125 | this->setupShapePaint(canvas, kShapeColors[colorIdx], mode, &paint); |
| 126 | SkASSERT(colorIdx == 0 || 255 == paint.getAlpha()); |
| 127 | canvas->save(); |
| 128 | |
| 129 | for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) { |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 130 | if (kShape_Pass != drawingPass) { |
| 131 | canvas->save(); |
| 132 | canvas->clipRect(clipRect); |
| 133 | if (kCheckerboard_Pass == drawingPass) { |
| 134 | sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, |
| 135 | 10); |
| 136 | } else { |
| 137 | SkASSERT(kBackground_Pass == drawingPass); |
| 138 | canvas->drawColor(kBGColor, SkXfermode::kSrc_Mode); |
| 139 | } |
| 140 | canvas->restore(); |
| 141 | } else { |
| 142 | this->drawShape(canvas, static_cast<Shape>(shapeIdx), paint, mode); |
| 143 | } |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 144 | canvas->translate(kShapeTypeSpacing, 0); |
| 145 | } |
| 146 | |
| 147 | canvas->restore(); |
| 148 | canvas->translate(kPaintSpacing, 0); |
| 149 | } |
| 150 | |
| 151 | canvas->restore(); |
| 152 | canvas->translate(0, kShapeSpacing); |
| 153 | } |
| 154 | |
| 155 | canvas->restore(); |
| 156 | canvas->translate(kXfermodeTypeSpacing, 0); |
| 157 | } |
| 158 | |
| 159 | canvas->restore(); |
| 160 | } |
| 161 | |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 162 | void onDraw(SkCanvas* canvas) override { |
| 163 | draw_pass(canvas, kCheckerboard_Pass); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 164 | canvas->saveLayer(nullptr, nullptr); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 165 | |
| 166 | canvas->translate(kMargin, kMargin); |
| 167 | draw_pass(canvas, kBackground_Pass); |
| 168 | |
| 169 | SkPaint titlePaint(fLabelPaint); |
| 170 | titlePaint.setTextSize(9 * titlePaint.getTextSize() / 8); |
| 171 | titlePaint.setFakeBoldText(true); |
| 172 | titlePaint.setTextAlign(SkPaint::kCenter_Align); |
| 173 | canvas->drawText("Porter Duff", sizeof("Porter Duff") - 1, |
| 174 | kLabelSpacing + 3 * kShapeTypeSpacing, |
| 175 | kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint); |
| 176 | canvas->drawText("Advanced", sizeof("Advanced") - 1, |
| 177 | kXfermodeTypeSpacing + kLabelSpacing + 3 * kShapeTypeSpacing, |
| 178 | kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint); |
| 179 | |
| 180 | draw_pass(canvas, kShape_Pass); |
| 181 | canvas->restore(); |
| 182 | } |
| 183 | |
cdalton | 6fd158e | 2015-05-27 15:08:33 -0700 | [diff] [blame] | 184 | void drawModeName(SkCanvas* canvas, SkXfermode::Mode mode) { |
| 185 | const char* modeName = mode <= SkXfermode::kLastMode ? SkXfermode::ModeName(mode) |
| 186 | : "Arithmetic"; |
| 187 | fLabelPaint.setTextAlign(SkPaint::kRight_Align); |
| 188 | canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize / 4, |
| 189 | fLabelPaint.getTextSize() / 3, fLabelPaint); |
| 190 | } |
| 191 | |
| 192 | void setupShapePaint(SkCanvas* canvas, GrColor color, SkXfermode::Mode mode, SkPaint* paint) { |
| 193 | paint->setColor(color); |
| 194 | |
| 195 | if (mode == SkXfermode::kPlus_Mode) { |
| 196 | // Check for overflow, otherwise we might get confusing AA artifacts. |
| 197 | int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color), |
| 198 | SkColorGetR(kBGColor) + SkColorGetR(color)), |
| 199 | SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color), |
| 200 | SkColorGetB(kBGColor) + SkColorGetB(color))); |
| 201 | |
| 202 | if (maxSum > 255) { |
| 203 | SkPaint dimPaint; |
| 204 | dimPaint.setAntiAlias(false); |
| 205 | dimPaint.setXfermode(SkXfermode::Create(SkXfermode::kDstIn_Mode)); |
| 206 | if (255 != paint->getAlpha()) { |
| 207 | // Dim the src and dst colors. |
| 208 | dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0); |
| 209 | paint->setAlpha(255 * paint->getAlpha() / maxSum); |
| 210 | } else { |
| 211 | // Just clear the dst, we need to preserve the paint's opacity. |
| 212 | dimPaint.setARGB(0, 0, 0, 0); |
| 213 | } |
| 214 | canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2, |
| 215 | kShapeSpacing/2 + 2 * kShapeTypeSpacing, |
| 216 | kShapeSpacing/2, dimPaint); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkXfermode::Mode mode) { |
| 222 | SkPaint shapePaint(paint); |
| 223 | shapePaint.setAntiAlias(kSquare_Shape != shape); |
| 224 | |
| 225 | SkAutoTUnref<SkXfermode> xfermode; |
| 226 | if (mode <= SkXfermode::kLastMode) { |
| 227 | xfermode.reset(SkXfermode::Create(mode)); |
| 228 | } else { |
| 229 | xfermode.reset(SkArithmeticMode::Create(+1.0f, +0.25f, -0.5f, +0.1f)); |
| 230 | } |
| 231 | shapePaint.setXfermode(xfermode); |
| 232 | |
| 233 | switch (shape) { |
| 234 | case kSquare_Shape: |
| 235 | canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2, |
| 236 | shapePaint); |
| 237 | break; |
| 238 | |
| 239 | case kDiamond_Shape: |
| 240 | canvas->save(); |
| 241 | canvas->rotate(45); |
| 242 | canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, kShapeSize/2, |
| 243 | shapePaint); |
| 244 | canvas->restore(); |
| 245 | break; |
| 246 | |
| 247 | case kOval_Shape: |
| 248 | canvas->save(); |
| 249 | canvas->rotate(static_cast<SkScalar>((511 * mode + 257) % 360)); |
| 250 | canvas->drawPath(fPath, shapePaint); |
| 251 | canvas->restore(); |
| 252 | break; |
| 253 | |
| 254 | default: |
| 255 | SkFAIL("Invalid shape."); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private: |
| 260 | SkPaint fLabelPaint; |
| 261 | SkPath fPath; |
| 262 | |
| 263 | typedef GM INHERITED; |
| 264 | }; |
| 265 | |
| 266 | ////////////////////////////////////////////////////////////////////////////// |
| 267 | |
| 268 | static GM* MyFactory(void*) { return new AAXfermodesGM; } |
| 269 | static GMRegistry reg(MyFactory); |
| 270 | |
| 271 | } |