• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkPath.h"
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 = 4 * 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     kConcave_Shape,
39 
40     kLast_Shape = kConcave_Shape
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:
AAXfermodesGM()51     AAXfermodesGM() {}
52 
53 protected:
54     enum DrawingPass {
55         kCheckerboard_Pass,
56         kBackground_Pass,
57         kShape_Pass
58     };
59 
onShortName()60     SkString onShortName() override {
61         return SkString("aaxfermodes");
62     }
63 
onISize()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 
onOnceBeforeDraw()71     void onOnceBeforeDraw() override {
72         fLabelPaint.setAntiAlias(true);
73         sk_tool_utils::set_portable_typeface(&fLabelPaint);
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         };
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();
94     }
95 
draw_pass(SkCanvas * canvas,DrawingPass drawingPass)96     void draw_pass(SkCanvas* canvas, DrawingPass drawingPass) {
97         SkRect clipRect =
98                 { -kShapeSize*11/16, -kShapeSize*11/16, kShapeSize*11/16, kShapeSize*11/16 };
99 
100         canvas->save();
101         if (kCheckerboard_Pass == drawingPass) {
102             canvas->translate(kMargin, kMargin);
103         }
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 
110             if (kShape_Pass == drawingPass) {
111                 fLabelPaint.setTextAlign(SkPaint::kCenter_Align);
112                 canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1,
113                         kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
114                         kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
115                 canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1,
116                         kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
117                         kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3,
118                         fLabelPaint);
119             }
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 
127                 if (kShape_Pass == drawingPass) {
128                     this->drawModeName(canvas, mode);
129                 }
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++) {
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                         }
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 
onDraw(SkCanvas * canvas)171     void onDraw(SkCanvas* canvas) override {
172         draw_pass(canvas, kCheckerboard_Pass);
173         canvas->saveLayer(nullptr, nullptr);
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,
183                          kLabelSpacing + 4 * kShapeTypeSpacing,
184                          kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
185         canvas->drawText("Advanced", sizeof("Advanced") - 1,
186                          kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing,
187                          kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
188 
189         draw_pass(canvas, kShape_Pass);
190         canvas->restore();
191     }
192 
drawModeName(SkCanvas * canvas,SkXfermode::Mode mode)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,
198                          fLabelPaint.getTextSize() / 4, fLabelPaint);
199     }
200 
setupShapePaint(SkCanvas * canvas,GrColor color,SkXfermode::Mode mode,SkPaint * paint)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,
224                                        kShapeSpacing/2 + 3 * kShapeTypeSpacing,
225                                        kShapeSpacing/2, dimPaint);
226             }
227         }
228     }
229 
drawShape(SkCanvas * canvas,Shape shape,const SkPaint & paint,SkXfermode::Mode mode)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));
259                 canvas->drawPath(fOval, shapePaint);
260                 canvas->restore();
261                 break;
262 
263             case kConcave_Shape:
264                 canvas->drawPath(fConcave, shapePaint);
265                 break;
266 
267             default:
268                 SkFAIL("Invalid shape.");
269         }
270     }
271 
272 private:
273     SkPaint   fLabelPaint;
274     SkPath    fOval;
275     SkPath    fConcave;
276 
277     typedef GM INHERITED;
278 };
279 
280 //////////////////////////////////////////////////////////////////////////////
281 
MyFactory(void *)282 static GM* MyFactory(void*) { return new AAXfermodesGM; }
283 static GMRegistry reg(MyFactory);
284 
285 }
286