3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
12 #include "SkXfermode.h"
17 * Renders overlapping shapes with random SkXfermode::Modes against a checkerboard.
19 class MixedXfermodesGM : public GM {
30 kShapeTypeConcavePath,
34 SkString onShortName() SK_OVERRIDE {
35 return SkString("mixed_xfermodes");
38 SkISize onISize() SK_OVERRIDE {
39 return SkISize::Make(790, 640);
42 void drawShape(SkCanvas* canvas,
45 static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50),
46 SkIntToScalar(75), SkIntToScalar(105));
48 case kShapeTypeCircle:
49 canvas->drawCircle(0, 0, 50, paint);
51 case kShapeTypeRoundRect:
52 canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), paint);
55 canvas->drawRect(kRect, paint);
57 case kShapeTypeConvexPath:
58 if (fConvexPath.isEmpty()) {
61 fConvexPath.moveTo(points[0]);
62 fConvexPath.quadTo(points[1], points[2]);
63 fConvexPath.quadTo(points[3], points[0]);
64 SkASSERT(fConvexPath.isConvex());
66 canvas->drawPath(fConvexPath, paint);
68 case kShapeTypeConcavePath:
69 if (fConcavePath.isEmpty()) {
70 SkPoint points[5] = {{0, SkIntToScalar(-50)} };
72 rot.setRotate(SkIntToScalar(360) / 5);
73 for (int i = 1; i < 5; ++i) {
74 rot.mapPoints(points + i, points + i - 1, 1);
76 fConcavePath.moveTo(points[0]);
77 for (int i = 0; i < 5; ++i) {
78 fConcavePath.lineTo(points[(2 * i) % 5]);
80 fConcavePath.setFillType(SkPath::kEvenOdd_FillType);
81 SkASSERT(!fConcavePath.isConvex());
83 canvas->drawPath(fConcavePath, paint);
90 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
91 if (NULL == fBG.get()) {
92 static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF,
97 bitmap.allocN32Pixels(2, 2);
98 memcpy(bitmap.getPixels(), kCheckerPixelData, sizeof(kCheckerPixelData));
100 lm.setScale(SkIntToScalar(20), SkIntToScalar(20));
101 fBG.reset(SkShader::CreateBitmapShader(bitmap,
102 SkShader::kRepeat_TileMode,
103 SkShader::kRepeat_TileMode,
108 bgPaint.setShader(fBG.get());
109 canvas->drawPaint(bgPaint);
110 SkISize size = canvas->getDeviceSize();
111 SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeight))) / 300;
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));
124 p.setAntiAlias(true);
126 p.setXfermodeMode(mode);
128 canvas->translate(dx, dy);
131 this->drawShape(canvas, p, shapeType);
135 // This draw should not affect the test's result.
136 drawWithHueOnWhite(canvas);
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.
145 void drawWithHueOnWhite(SkCanvas* canvas) {
146 SkColor color = SkColorSetARGBMacro(225, 255, 255, 255);
147 SkXfermode::Mode mode = SkXfermode::kHue_Mode;
148 ShapeType shapeType = kShapeTypeConvexPath;
150 // Make it fit into a square.
152 // Look for a clean white square.
157 p.setAntiAlias(true);
159 p.setXfermodeMode(mode);
161 canvas->translate(dx, dy);
163 this->drawShape(canvas, p, shapeType);
171 SkAutoTUnref<SkShader> fBG;
174 typedef GM INHERITED;
177 //////////////////////////////////////////////////////////////////////////////
179 static GM* MyFactory(void*) { return new MixedXfermodesGM; }
180 static GMRegistry reg(MyFactory);