blob: dc858cb88b0cd0a3ce482a6be2438e60b558dc40 [file] [log] [blame]
bsalomon@google.com20edf382013-04-01 18:02:55 +00001
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
14namespace skiagm {
15
16/**
bsalomon@google.com6db96262013-05-15 13:10:18 +000017 * Renders overlapping shapes with random SkXfermode::Modes against a checkerboard.
bsalomon@google.com20edf382013-04-01 18:02:55 +000018 */
19class MixedXfermodesGM : public GM {
20public:
21 MixedXfermodesGM() {
bsalomon@google.com20edf382013-04-01 18:02:55 +000022 }
23
24protected:
roscaace7f422014-11-20 07:24:32 -080025 enum ShapeType {
26 kShapeTypeCircle,
27 kShapeTypeRoundRect,
28 kShapeTypeRect,
29 kShapeTypeConvexPath,
30 kShapeTypeConcavePath,
31 kNumShapeTypes
32 };
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkString onShortName() override {
bsalomon@google.com20edf382013-04-01 18:02:55 +000035 return SkString("mixed_xfermodes");
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070039 return SkISize::Make(790, 640);
bsalomon@google.com20edf382013-04-01 18:02:55 +000040 }
41
bsalomon@google.com6db96262013-05-15 13:10:18 +000042 void drawShape(SkCanvas* canvas,
43 const SkPaint& paint,
roscaace7f422014-11-20 07:24:32 -080044 ShapeType type) {
bsalomon@google.com6db96262013-05-15 13:10:18 +000045 static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50),
46 SkIntToScalar(75), SkIntToScalar(105));
roscaace7f422014-11-20 07:24:32 -080047 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());
bsalomon@google.com6db96262013-05-15 13:10:18 +000065 }
roscaace7f422014-11-20 07:24:32 -080066 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());
bsalomon@google.com6db96262013-05-15 13:10:18 +000082 }
roscaace7f422014-11-20 07:24:32 -080083 canvas->drawPath(fConcavePath, paint);
84 break;
85 default:
86 break;
bsalomon@google.com6db96262013-05-15 13:10:18 +000087 }
88 }
89
mtklein36352bf2015-03-25 18:17:31 -070090 void onDraw(SkCanvas* canvas) override {
bsalomon@google.com6db96262013-05-15 13:10:18 +000091 if (NULL == fBG.get()) {
92 static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF,
93 0xFFCCCCCC,
94 0xFFCCCCCC,
95 0xFFFFFFFF };
96 SkBitmap bitmap;
reed@google.comeb9a46c2014-01-25 16:46:20 +000097 bitmap.allocN32Pixels(2, 2);
bsalomon@google.com6db96262013-05-15 13:10:18 +000098 memcpy(bitmap.getPixels(), kCheckerPixelData, sizeof(kCheckerPixelData));
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000099 SkMatrix lm;
100 lm.setScale(SkIntToScalar(20), SkIntToScalar(20));
bsalomon@google.com6db96262013-05-15 13:10:18 +0000101 fBG.reset(SkShader::CreateBitmapShader(bitmap,
102 SkShader::kRepeat_TileMode,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000103 SkShader::kRepeat_TileMode,
104 &lm));
bsalomon@google.com6db96262013-05-15 13:10:18 +0000105 }
bsalomon@google.com6db96262013-05-15 13:10:18 +0000106
bsalomon@google.com20edf382013-04-01 18:02:55 +0000107 SkPaint bgPaint;
108 bgPaint.setShader(fBG.get());
109 canvas->drawPaint(bgPaint);
110 SkISize size = canvas->getDeviceSize();
bsalomon@google.com6db96262013-05-15 13:10:18 +0000111 SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeight))) / 300;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000112 SkRandom random;
bsalomon@google.com6db96262013-05-15 13:10:18 +0000113 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));
bsalomon@google.com20edf382013-04-01 18:02:55 +0000118 SkColor color = random.nextU();
bsalomon@google.com20edf382013-04-01 18:02:55 +0000119 SkXfermode::Mode mode =
120 static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::kLastMode + 1));
roscaace7f422014-11-20 07:24:32 -0800121 ShapeType shapeType = static_cast<ShapeType>(random.nextULessThan(kNumShapeTypes));
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000122
bsalomon@google.com20edf382013-04-01 18:02:55 +0000123 SkPaint p;
124 p.setAntiAlias(true);
125 p.setColor(color);
126 p.setXfermodeMode(mode);
bsalomon@google.com6db96262013-05-15 13:10:18 +0000127 canvas->save();
128 canvas->translate(dx, dy);
129 canvas->scale(s, s);
130 canvas->rotate(r);
roscaace7f422014-11-20 07:24:32 -0800131 this->drawShape(canvas, p, shapeType);
bsalomon@google.com6db96262013-05-15 13:10:18 +0000132 canvas->restore();
bsalomon@google.com20edf382013-04-01 18:02:55 +0000133 }
roscaace7f422014-11-20 07:24:32 -0800134
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();
bsalomon@google.com20edf382013-04-01 18:02:55 +0000165 }
166
bsalomon@google.com20edf382013-04-01 18:02:55 +0000167private:
168 enum {
bsalomon@google.com6db96262013-05-15 13:10:18 +0000169 kNumShapes = 100,
bsalomon@google.com20edf382013-04-01 18:02:55 +0000170 };
171 SkAutoTUnref<SkShader> fBG;
bsalomon@google.com6db96262013-05-15 13:10:18 +0000172 SkPath fConcavePath;
173 SkPath fConvexPath;
bsalomon@google.com20edf382013-04-01 18:02:55 +0000174 typedef GM INHERITED;
175};
176
177//////////////////////////////////////////////////////////////////////////////
178
179static GM* MyFactory(void*) { return new MixedXfermodesGM; }
180static GMRegistry reg(MyFactory);
181
182}