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