OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkRandom.h" | 10 #include "SkRandom.h" |
11 #include "SkShader.h" | 11 #include "SkShader.h" |
12 #include "SkXfermode.h" | 12 #include "SkXfermode.h" |
13 | 13 |
14 namespace skiagm { | 14 namespace skiagm { |
15 | 15 |
16 /** | 16 /** |
17 * Renders overlapping shapes with random SkXfermode::Modes against a checkerboa rd. | 17 * Renders overlapping shapes with random SkXfermode::Modes against a checkerboa rd. |
18 */ | 18 */ |
19 class MixedXfermodesGM : public GM { | 19 class MixedXfermodesGM : public GM { |
20 public: | 20 public: |
21 MixedXfermodesGM() { | 21 MixedXfermodesGM() { |
22 } | 22 } |
23 | 23 |
24 protected: | 24 protected: |
25 enum ShapeType { | |
26 kShapeTypeCircle, | |
27 kShapeTypeRoundRect, | |
28 kShapeTypeRect, | |
29 kShapeTypeConvexPath, | |
30 kShapeTypeConcavePath, | |
31 kNumShapeTypes | |
32 }; | |
33 | |
25 virtual SkString onShortName() SK_OVERRIDE { | 34 virtual SkString onShortName() SK_OVERRIDE { |
26 return SkString("mixed_xfermodes"); | 35 return SkString("mixed_xfermodes"); |
27 } | 36 } |
28 | 37 |
29 virtual SkISize onISize() SK_OVERRIDE { | 38 virtual SkISize onISize() SK_OVERRIDE { |
30 return SkISize::Make(790, 640); | 39 return SkISize::Make(790, 640); |
31 } | 40 } |
32 | 41 |
33 void drawShape(SkCanvas* canvas, | 42 void drawShape(SkCanvas* canvas, |
34 const SkPaint& paint, | 43 const SkPaint& paint, |
35 SkRandom* random) { | 44 ShapeType type) { |
36 static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntTo Scalar(-50), | 45 static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntTo Scalar(-50), |
37 SkIntToScalar(75), SkIntToS calar(105)); | 46 SkIntToScalar(75), SkIntToS calar(105)); |
38 int shape = random->nextULessThan(5); | 47 switch (type) { |
egdaniel
2014/11/14 16:08:21
You need to have a default case or else there will
rosca
2014/11/17 16:09:31
Done.
| |
39 switch (shape) { | 48 case kShapeTypeCircle: |
40 case 0: | |
41 canvas->drawCircle(0, 0, 50, paint); | 49 canvas->drawCircle(0, 0, 50, paint); |
42 break; | 50 break; |
43 case 1: | 51 case kShapeTypeRoundRect: |
44 canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), p aint); | 52 canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), p aint); |
45 break; | 53 break; |
46 case 2: | 54 case kShapeTypeRect: |
47 canvas->drawRect(kRect, paint); | 55 canvas->drawRect(kRect, paint); |
48 break; | 56 break; |
49 case 3: | 57 case kShapeTypeConvexPath: |
50 if (fConvexPath.isEmpty()) { | 58 if (fConvexPath.isEmpty()) { |
51 SkPoint points[4]; | 59 SkPoint points[4]; |
52 kRect.toQuad(points); | 60 kRect.toQuad(points); |
53 fConvexPath.moveTo(points[0]); | 61 fConvexPath.moveTo(points[0]); |
54 fConvexPath.quadTo(points[1], points[2]); | 62 fConvexPath.quadTo(points[1], points[2]); |
55 fConvexPath.quadTo(points[3], points[0]); | 63 fConvexPath.quadTo(points[3], points[0]); |
56 SkASSERT(fConvexPath.isConvex()); | 64 SkASSERT(fConvexPath.isConvex()); |
57 } | 65 } |
58 canvas->drawPath(fConvexPath, paint); | 66 canvas->drawPath(fConvexPath, paint); |
59 break; | 67 break; |
60 case 4: | 68 case kShapeTypeConcavePath: |
61 if (fConcavePath.isEmpty()) { | 69 if (fConcavePath.isEmpty()) { |
62 SkPoint points[5] = {{0, SkIntToScalar(-50)} }; | 70 SkPoint points[5] = {{0, SkIntToScalar(-50)} }; |
63 SkMatrix rot; | 71 SkMatrix rot; |
64 rot.setRotate(SkIntToScalar(360) / 5); | 72 rot.setRotate(SkIntToScalar(360) / 5); |
65 for (int i = 1; i < 5; ++i) { | 73 for (int i = 1; i < 5; ++i) { |
66 rot.mapPoints(points + i, points + i - 1, 1); | 74 rot.mapPoints(points + i, points + i - 1, 1); |
67 } | 75 } |
68 fConcavePath.moveTo(points[0]); | 76 fConcavePath.moveTo(points[0]); |
69 for (int i = 0; i < 5; ++i) { | 77 for (int i = 0; i < 5; ++i) { |
70 fConcavePath.lineTo(points[(2 * i) % 5]); | 78 fConcavePath.lineTo(points[(2 * i) % 5]); |
(...skipping 30 matching lines...) Expand all Loading... | |
101 SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeig ht))) / 300; | 109 SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeig ht))) / 300; |
102 SkRandom random; | 110 SkRandom random; |
103 for (int i = 0; i < kNumShapes; ++i) { | 111 for (int i = 0; i < kNumShapes; ++i) { |
104 SkScalar s = random.nextRangeScalar(SK_Scalar1 / 8, SK_Scalar1) * ma xScale; | 112 SkScalar s = random.nextRangeScalar(SK_Scalar1 / 8, SK_Scalar1) * ma xScale; |
105 SkScalar r = random.nextRangeScalar(0, SkIntToScalar(360)); | 113 SkScalar r = random.nextRangeScalar(0, SkIntToScalar(360)); |
106 SkScalar dx = random.nextRangeScalar(0, SkIntToScalar(size.fWidth)); | 114 SkScalar dx = random.nextRangeScalar(0, SkIntToScalar(size.fWidth)); |
107 SkScalar dy = random.nextRangeScalar(0, SkIntToScalar(size.fHeight)) ; | 115 SkScalar dy = random.nextRangeScalar(0, SkIntToScalar(size.fHeight)) ; |
108 SkColor color = random.nextU(); | 116 SkColor color = random.nextU(); |
109 SkXfermode::Mode mode = | 117 SkXfermode::Mode mode = |
110 static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::k LastMode + 1)); | 118 static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::k LastMode + 1)); |
119 ShapeType shapeType = static_cast<ShapeType>(random.nextULessThan(kN umShapeTypes)); | |
120 | |
121 if (i == kNumShapes - 1) { | |
egdaniel
2014/11/14 16:08:21
Since what you want to test ends up drawing nothin
rosca
2014/11/17 16:09:31
Done.
| |
122 color = SkColorSetARGBMacro(225, 100, 100, 100); | |
123 mode = SkXfermode::kSaturation_Mode; | |
124 shapeType = kShapeTypeConvexPath; | |
125 } | |
111 | 126 |
112 SkPaint p; | 127 SkPaint p; |
113 p.setAntiAlias(true); | 128 p.setAntiAlias(true); |
114 p.setColor(color); | 129 p.setColor(color); |
115 p.setXfermodeMode(mode); | 130 p.setXfermodeMode(mode); |
116 canvas->save(); | 131 canvas->save(); |
117 canvas->translate(dx, dy); | 132 canvas->translate(dx, dy); |
118 canvas->scale(s, s); | 133 canvas->scale(s, s); |
119 canvas->rotate(r); | 134 canvas->rotate(r); |
120 this->drawShape(canvas, p, &random); | 135 this->drawShape(canvas, p, shapeType); |
121 canvas->restore(); | 136 canvas->restore(); |
122 } | 137 } |
123 } | 138 } |
124 | 139 |
125 virtual uint32_t onGetFlags() const { | 140 virtual uint32_t onGetFlags() const { |
126 // Skip PDF rasterization since rendering this PDF takes forever. | 141 // Skip PDF rasterization since rendering this PDF takes forever. |
127 return kSkipPDFRasterization_Flag | kSkipTiled_Flag; | 142 return kSkipPDFRasterization_Flag | kSkipTiled_Flag; |
128 } | 143 } |
129 | 144 |
130 private: | 145 private: |
131 enum { | 146 enum { |
132 kNumShapes = 100, | 147 kNumShapes = 100, |
133 }; | 148 }; |
134 SkAutoTUnref<SkShader> fBG; | 149 SkAutoTUnref<SkShader> fBG; |
135 SkPath fConcavePath; | 150 SkPath fConcavePath; |
136 SkPath fConvexPath; | 151 SkPath fConvexPath; |
137 typedef GM INHERITED; | 152 typedef GM INHERITED; |
138 }; | 153 }; |
139 | 154 |
140 ////////////////////////////////////////////////////////////////////////////// | 155 ////////////////////////////////////////////////////////////////////////////// |
141 | 156 |
142 static GM* MyFactory(void*) { return new MixedXfermodesGM; } | 157 static GM* MyFactory(void*) { return new MixedXfermodesGM; } |
143 static GMRegistry reg(MyFactory); | 158 static GMRegistry reg(MyFactory); |
144 | 159 |
145 } | 160 } |
OLD | NEW |