f818719bcdf5f4f719a295e2966bdd634b35811d
[platform/upstream/libSkiaSharp.git] / gm / mixedxfermodes.cpp
1
2 /*
3  * Copyright 2013 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 "SkBitmap.h"
10 #include "SkRandom.h"
11 #include "SkShader.h"
12 #include "SkXfermode.h"
13
14 namespace skiagm {
15
16 /**
17  * Renders overlapping shapes with random SkXfermode::Modes against a checkerboard.
18  */
19 class MixedXfermodesGM : public GM {
20 public:
21     MixedXfermodesGM() {
22     }
23
24 protected:
25     enum ShapeType {
26         kShapeTypeCircle,
27         kShapeTypeRoundRect,
28         kShapeTypeRect,
29         kShapeTypeConvexPath,
30         kShapeTypeConcavePath,
31         kNumShapeTypes
32     };
33
34     SkString onShortName() SK_OVERRIDE {
35         return SkString("mixed_xfermodes");
36     }
37
38     SkISize onISize() SK_OVERRIDE {
39         return SkISize::Make(790, 640);
40     }
41
42     void drawShape(SkCanvas* canvas,
43                    const SkPaint& paint,
44                    ShapeType type) {
45         static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50),
46                                                      SkIntToScalar(75), SkIntToScalar(105));
47         switch (type) {
48             case kShapeTypeCircle:
49                 canvas->drawCircle(0, 0, 50, paint);
50                 break;
51             case kShapeTypeRoundRect:
52                 canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), paint);
53                 break;
54             case kShapeTypeRect:
55                 canvas->drawRect(kRect, paint);
56                 break;
57             case kShapeTypeConvexPath:
58                 if (fConvexPath.isEmpty()) {
59                     SkPoint points[4];
60                     kRect.toQuad(points);
61                     fConvexPath.moveTo(points[0]);
62                     fConvexPath.quadTo(points[1], points[2]);
63                     fConvexPath.quadTo(points[3], points[0]);
64                     SkASSERT(fConvexPath.isConvex());
65                 }
66                 canvas->drawPath(fConvexPath, paint);
67                 break;
68             case kShapeTypeConcavePath:
69                 if (fConcavePath.isEmpty()) {
70                     SkPoint points[5] = {{0, SkIntToScalar(-50)} };
71                     SkMatrix rot;
72                     rot.setRotate(SkIntToScalar(360) / 5);
73                     for (int i = 1; i < 5; ++i) {
74                         rot.mapPoints(points + i, points + i - 1, 1);
75                     }
76                     fConcavePath.moveTo(points[0]);
77                     for (int i = 0; i < 5; ++i) {
78                         fConcavePath.lineTo(points[(2 * i) % 5]);
79                     }
80                     fConcavePath.setFillType(SkPath::kEvenOdd_FillType);
81                     SkASSERT(!fConcavePath.isConvex());
82                 }
83                 canvas->drawPath(fConcavePath, paint);
84                 break;
85             default:
86                 break;
87         }
88     }
89
90     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
91         if (NULL == fBG.get()) {
92             static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF,
93                                                     0xFFCCCCCC,
94                                                     0xFFCCCCCC,
95                                                     0xFFFFFFFF };
96             SkBitmap bitmap;
97             bitmap.allocN32Pixels(2, 2);
98             memcpy(bitmap.getPixels(), kCheckerPixelData, sizeof(kCheckerPixelData));
99             SkMatrix lm;
100             lm.setScale(SkIntToScalar(20), SkIntToScalar(20));
101             fBG.reset(SkShader::CreateBitmapShader(bitmap,
102                                                    SkShader::kRepeat_TileMode,
103                                                    SkShader::kRepeat_TileMode,
104                                                    &lm));
105         }
106
107         SkPaint bgPaint;
108         bgPaint.setShader(fBG.get());
109         canvas->drawPaint(bgPaint);
110         SkISize size = canvas->getDeviceSize();
111         SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeight))) / 300;
112         SkRandom random;
113         for (int i = 0; i < kNumShapes; ++i) {
114             SkScalar s = random.nextRangeScalar(SK_Scalar1 / 8, SK_Scalar1) * maxScale;
115             SkScalar r = random.nextRangeScalar(0, SkIntToScalar(360));
116             SkScalar dx = random.nextRangeScalar(0, SkIntToScalar(size.fWidth));
117             SkScalar dy = random.nextRangeScalar(0, SkIntToScalar(size.fHeight));
118             SkColor color = random.nextU();
119             SkXfermode::Mode mode =
120                 static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::kLastMode + 1));
121             ShapeType shapeType = static_cast<ShapeType>(random.nextULessThan(kNumShapeTypes));
122
123             SkPaint p;
124             p.setAntiAlias(true);
125             p.setColor(color);
126             p.setXfermodeMode(mode);
127             canvas->save();
128             canvas->translate(dx, dy);
129             canvas->scale(s, s);
130             canvas->rotate(r);
131             this->drawShape(canvas, p, shapeType);
132             canvas->restore();
133         }
134
135         // This draw should not affect the test's result.
136         drawWithHueOnWhite(canvas);
137     }
138
139     /**
140      * Draws white color into a white square using the hue blend mode.
141      * The result color should be white, so it doesn't change the expectations.
142      * This will test a divide by 0 bug in shaders' setLum function,
143      * which used to output black pixels.
144      */
145     void drawWithHueOnWhite(SkCanvas* canvas) {
146         SkColor color = SkColorSetARGBMacro(225, 255, 255, 255);
147         SkXfermode::Mode mode = SkXfermode::kHue_Mode;
148         ShapeType shapeType = kShapeTypeConvexPath;
149
150         // Make it fit into a square.
151         SkScalar s = 0.15f;
152         // Look for a clean white square.
153         SkScalar dx = 30.f;
154         SkScalar dy = 350.f;
155
156         SkPaint p;
157         p.setAntiAlias(true);
158         p.setColor(color);
159         p.setXfermodeMode(mode);
160         canvas->save();
161         canvas->translate(dx, dy);
162         canvas->scale(s, s);
163         this->drawShape(canvas, p, shapeType);
164         canvas->restore();
165     }
166
167 private:
168     enum {
169         kNumShapes = 100,
170     };
171     SkAutoTUnref<SkShader> fBG;
172     SkPath                 fConcavePath;
173     SkPath                 fConvexPath;
174     typedef GM INHERITED;
175 };
176
177 //////////////////////////////////////////////////////////////////////////////
178
179 static GM* MyFactory(void*) { return new MixedXfermodesGM; }
180 static GMRegistry reg(MyFactory);
181
182 }