OLD | NEW |
(Empty) | |
| 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" |
| 10 #include "SkShader.h" |
| 11 #include "SkXfermode.h" |
| 12 |
| 13 enum { |
| 14 kXfermodeCount = SkXfermode::kLastMode + 2, // All xfermodes plus arithmetic
mode. |
| 15 kShapeSize = 22, |
| 16 kShapeSpacing = 36, |
| 17 kShapeTypeSpacing = 4 * kShapeSpacing / 3, |
| 18 kPaintSpacing = 3 * kShapeTypeSpacing, |
| 19 kLabelSpacing = 3 * kShapeSize, |
| 20 kMargin = kShapeSpacing / 2, |
| 21 kXfermodeTypeSpacing = kLabelSpacing + 2 * kPaintSpacing + kShapeTypeSpacing
, |
| 22 kTitleSpacing = 3 * kShapeSpacing / 4, |
| 23 kSubtitleSpacing = 5 * kShapeSpacing / 8 |
| 24 }; |
| 25 |
| 26 static const SkColor kBGColor = SkColorSetARGB(200, 210, 184, 135); |
| 27 |
| 28 static const SkColor kShapeColors[2] = { |
| 29 SkColorSetARGB(130, 255, 0, 128), // input color unknown |
| 30 SkColorSetARGB(255, 0, 255, 255) // input color opaque |
| 31 }; |
| 32 |
| 33 enum Shape { |
| 34 kSquare_Shape, |
| 35 kDiamond_Shape, |
| 36 kOval_Shape, |
| 37 |
| 38 kLast_Shape = kOval_Shape |
| 39 }; |
| 40 |
| 41 namespace skiagm { |
| 42 |
| 43 /** |
| 44 * Verifies AA works properly on all Xfermodes, including arithmetic, with both
opaque and unknown |
| 45 * src colors. |
| 46 */ |
| 47 class AAXfermodesGM : public GM { |
| 48 public: |
| 49 AAXfermodesGM() {} |
| 50 |
| 51 protected: |
| 52 SkString onShortName() override { |
| 53 return SkString("aaxfermodes"); |
| 54 } |
| 55 |
| 56 SkISize onISize() override { |
| 57 return SkISize::Make(2 * kMargin + 2 * kXfermodeTypeSpacing - |
| 58 (kXfermodeTypeSpacing - (kLabelSpacing + 2 * kPaint
Spacing)), |
| 59 2 * kMargin + kTitleSpacing + kSubtitleSpacing + |
| 60 (1 + SkXfermode::kLastCoeffMode) * kShapeSpacing); |
| 61 } |
| 62 |
| 63 void onOnceBeforeDraw() override { |
| 64 fLabelPaint.setAntiAlias(true); |
| 65 sk_tool_utils::set_portable_typeface(&fLabelPaint); |
| 66 fLabelPaint.setTextSize(5 * kShapeSize/8); |
| 67 fLabelPaint.setSubpixelText(true); |
| 68 |
| 69 static const SkScalar radius = -1.4f * kShapeSize/2; |
| 70 SkPoint pts[4] = { |
| 71 {-radius, 0}, |
| 72 {0, -1.33f * radius}, |
| 73 {radius, 0}, |
| 74 {0, 1.33f * radius} |
| 75 }; |
| 76 fPath.moveTo(pts[0]); |
| 77 fPath.quadTo(pts[1], pts[2]); |
| 78 fPath.quadTo(pts[3], pts[0]); |
| 79 } |
| 80 |
| 81 void onDraw(SkCanvas* canvas) override { |
| 82 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc0c0c0, 10); |
| 83 |
| 84 canvas->saveLayer(NULL, NULL); |
| 85 canvas->drawColor(kBGColor, SkXfermode::kSrc_Mode); |
| 86 |
| 87 canvas->translate(kMargin, kMargin); |
| 88 |
| 89 SkPaint titlePaint(fLabelPaint); |
| 90 titlePaint.setTextSize(9 * titlePaint.getTextSize() / 8); |
| 91 titlePaint.setFakeBoldText(true); |
| 92 titlePaint.setTextAlign(SkPaint::kCenter_Align); |
| 93 canvas->drawText("Porter Duff", sizeof("Porter Duff") - 1, |
| 94 kLabelSpacing + 3 * kShapeTypeSpacing, |
| 95 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, title
Paint); |
| 96 canvas->drawText("Advanced", sizeof("Advanced") - 1, |
| 97 kXfermodeTypeSpacing + kLabelSpacing + 3 * kShapeTypeSp
acing, |
| 98 kTitleSpacing / 2 + titlePaint.getTextSize() / 3, title
Paint); |
| 99 |
| 100 canvas->translate(0, kTitleSpacing); |
| 101 |
| 102 for (size_t xfermodeSet = 0; xfermodeSet < 2; xfermodeSet++) { |
| 103 size_t firstMode = (SkXfermode::kLastCoeffMode + 1) * xfermodeSet; |
| 104 canvas->save(); |
| 105 |
| 106 fLabelPaint.setTextAlign(SkPaint::kCenter_Align); |
| 107 canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1, |
| 108 kLabelSpacing + kShapeSpacing / 2 + kShapeTypeSpaci
ng, |
| 109 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() /
3, fLabelPaint); |
| 110 canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1, |
| 111 kLabelSpacing + kShapeSpacing / 2 + kShapeTypeSpaci
ng + kPaintSpacing, |
| 112 kSubtitleSpacing / 2 + fLabelPaint.getTextSize() /
3, fLabelPaint); |
| 113 |
| 114 canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2); |
| 115 |
| 116 for (size_t m = 0; m <= SkXfermode::kLastCoeffMode; m++) { |
| 117 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(firstMode
+ m); |
| 118 canvas->save(); |
| 119 |
| 120 this->drawModeName(canvas, mode); |
| 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+
+) { |
| 130 this->drawShape(canvas, static_cast<Shape>(shapeIdx), pa
int, mode); |
| 131 canvas->translate(kShapeTypeSpacing, 0); |
| 132 } |
| 133 |
| 134 canvas->restore(); |
| 135 canvas->translate(kPaintSpacing, 0); |
| 136 } |
| 137 |
| 138 canvas->restore(); |
| 139 canvas->translate(0, kShapeSpacing); |
| 140 } |
| 141 |
| 142 canvas->restore(); |
| 143 canvas->translate(kXfermodeTypeSpacing, 0); |
| 144 } |
| 145 |
| 146 canvas->restore(); |
| 147 } |
| 148 |
| 149 void drawModeName(SkCanvas* canvas, SkXfermode::Mode mode) { |
| 150 const char* modeName = mode <= SkXfermode::kLastMode ? SkXfermode::ModeN
ame(mode) |
| 151 : "Arithmetic"; |
| 152 fLabelPaint.setTextAlign(SkPaint::kRight_Align); |
| 153 canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize
/ 4, |
| 154 fLabelPaint.getTextSize() / 3, fLabelPaint); |
| 155 } |
| 156 |
| 157 void setupShapePaint(SkCanvas* canvas, GrColor color, SkXfermode::Mode mode,
SkPaint* paint) { |
| 158 paint->setColor(color); |
| 159 |
| 160 if (mode == SkXfermode::kPlus_Mode) { |
| 161 // Check for overflow, otherwise we might get confusing AA artifacts
. |
| 162 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color
), |
| 163 SkColorGetR(kBGColor) + SkColorGetR(color
)), |
| 164 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color
), |
| 165 SkColorGetB(kBGColor) + SkColorGetB(color
))); |
| 166 |
| 167 if (maxSum > 255) { |
| 168 SkPaint dimPaint; |
| 169 dimPaint.setAntiAlias(false); |
| 170 dimPaint.setXfermode(SkXfermode::Create(SkXfermode::kDstIn_Mode)
); |
| 171 if (255 != paint->getAlpha()) { |
| 172 // Dim the src and dst colors. |
| 173 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0); |
| 174 paint->setAlpha(255 * paint->getAlpha() / maxSum); |
| 175 } else { |
| 176 // Just clear the dst, we need to preserve the paint's opaci
ty. |
| 177 dimPaint.setARGB(0, 0, 0, 0); |
| 178 } |
| 179 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2, |
| 180 kShapeSpacing/2 + 2 * kShapeTypeSpacing, |
| 181 kShapeSpacing/2, dimPaint); |
| 182 } |
| 183 } |
| 184 } |
| 185 |
| 186 void drawShape(SkCanvas* canvas, Shape shape, const SkPaint& paint, SkXfermo
de::Mode mode) { |
| 187 SkPaint shapePaint(paint); |
| 188 shapePaint.setAntiAlias(kSquare_Shape != shape); |
| 189 |
| 190 SkAutoTUnref<SkXfermode> xfermode; |
| 191 if (mode <= SkXfermode::kLastMode) { |
| 192 xfermode.reset(SkXfermode::Create(mode)); |
| 193 } else { |
| 194 xfermode.reset(SkArithmeticMode::Create(+1.0f, +0.25f, -0.5f, +0.1f)
); |
| 195 } |
| 196 shapePaint.setXfermode(xfermode); |
| 197 |
| 198 switch (shape) { |
| 199 case kSquare_Shape: |
| 200 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/
2, kShapeSize/2, |
| 201 shapePaint); |
| 202 break; |
| 203 |
| 204 case kDiamond_Shape: |
| 205 canvas->save(); |
| 206 canvas->rotate(45); |
| 207 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/
2, kShapeSize/2, |
| 208 shapePaint); |
| 209 canvas->restore(); |
| 210 break; |
| 211 |
| 212 case kOval_Shape: |
| 213 canvas->save(); |
| 214 canvas->rotate(static_cast<SkScalar>((511 * mode + 257) % 360)); |
| 215 canvas->drawPath(fPath, shapePaint); |
| 216 canvas->restore(); |
| 217 break; |
| 218 |
| 219 default: |
| 220 SkFAIL("Invalid shape."); |
| 221 } |
| 222 } |
| 223 |
| 224 private: |
| 225 SkPaint fLabelPaint; |
| 226 SkPath fPath; |
| 227 |
| 228 typedef GM INHERITED; |
| 229 }; |
| 230 |
| 231 ////////////////////////////////////////////////////////////////////////////// |
| 232 |
| 233 static GM* MyFactory(void*) { return new AAXfermodesGM; } |
| 234 static GMRegistry reg(MyFactory); |
| 235 |
| 236 } |
OLD | NEW |