Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(595)

Side by Side Diff: gm/mixedxfermodes.cpp

Issue 666043003: Preventing division by 0 in non-separable blend mode shaders. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: adding myself to AUTHORS Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « AUTHORS ('k') | src/core/SkXfermode.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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) {
39 switch (shape) { 48 case kShapeTypeCircle:
40 case 0: 49 canvas->drawCircle(0, 0, 50, paint);
41 canvas->drawCircle(0, 0, 50, paint); 50 break;
42 break; 51 case kShapeTypeRoundRect:
43 case 1: 52 canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20 ), paint);
44 canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), p aint); 53 break;
45 break; 54 case kShapeTypeRect:
46 case 2: 55 canvas->drawRect(kRect, paint);
47 canvas->drawRect(kRect, paint); 56 break;
48 break; 57 case kShapeTypeConvexPath:
49 case 3: 58 if (fConvexPath.isEmpty()) {
50 if (fConvexPath.isEmpty()) { 59 SkPoint points[4];
51 SkPoint points[4]; 60 kRect.toQuad(points);
52 kRect.toQuad(points); 61 fConvexPath.moveTo(points[0]);
53 fConvexPath.moveTo(points[0]); 62 fConvexPath.quadTo(points[1], points[2]);
54 fConvexPath.quadTo(points[1], points[2]); 63 fConvexPath.quadTo(points[3], points[0]);
55 fConvexPath.quadTo(points[3], points[0]); 64 SkASSERT(fConvexPath.isConvex());
56 SkASSERT(fConvexPath.isConvex());
57 }
58 canvas->drawPath(fConvexPath, paint);
59 break;
60 case 4:
61 if (fConcavePath.isEmpty()) {
62 SkPoint points[5] = {{0, SkIntToScalar(-50)} };
63 SkMatrix rot;
64 rot.setRotate(SkIntToScalar(360) / 5);
65 for (int i = 1; i < 5; ++i) {
66 rot.mapPoints(points + i, points + i - 1, 1);
67 } 65 }
68 fConcavePath.moveTo(points[0]); 66 canvas->drawPath(fConvexPath, paint);
69 for (int i = 0; i < 5; ++i) { 67 break;
70 fConcavePath.lineTo(points[(2 * i) % 5]); 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());
71 } 82 }
72 fConcavePath.setFillType(SkPath::kEvenOdd_FillType); 83 canvas->drawPath(fConcavePath, paint);
73 SkASSERT(!fConcavePath.isConvex()); 84 break;
74 } 85 default:
75 canvas->drawPath(fConcavePath, paint); 86 break;
76 break;
77 } 87 }
78 } 88 }
79 89
80 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 90 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
81 if (NULL == fBG.get()) { 91 if (NULL == fBG.get()) {
82 static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF, 92 static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF,
83 0xFFCCCCCC, 93 0xFFCCCCCC,
84 0xFFCCCCCC, 94 0xFFCCCCCC,
85 0xFFFFFFFF }; 95 0xFFFFFFFF };
86 SkBitmap bitmap; 96 SkBitmap bitmap;
(...skipping 14 matching lines...) Expand all
101 SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeig ht))) / 300; 111 SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeig ht))) / 300;
102 SkRandom random; 112 SkRandom random;
103 for (int i = 0; i < kNumShapes; ++i) { 113 for (int i = 0; i < kNumShapes; ++i) {
104 SkScalar s = random.nextRangeScalar(SK_Scalar1 / 8, SK_Scalar1) * ma xScale; 114 SkScalar s = random.nextRangeScalar(SK_Scalar1 / 8, SK_Scalar1) * ma xScale;
105 SkScalar r = random.nextRangeScalar(0, SkIntToScalar(360)); 115 SkScalar r = random.nextRangeScalar(0, SkIntToScalar(360));
106 SkScalar dx = random.nextRangeScalar(0, SkIntToScalar(size.fWidth)); 116 SkScalar dx = random.nextRangeScalar(0, SkIntToScalar(size.fWidth));
107 SkScalar dy = random.nextRangeScalar(0, SkIntToScalar(size.fHeight)) ; 117 SkScalar dy = random.nextRangeScalar(0, SkIntToScalar(size.fHeight)) ;
108 SkColor color = random.nextU(); 118 SkColor color = random.nextU();
109 SkXfermode::Mode mode = 119 SkXfermode::Mode mode =
110 static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::k LastMode + 1)); 120 static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::k LastMode + 1));
121 ShapeType shapeType = static_cast<ShapeType>(random.nextULessThan(kN umShapeTypes));
111 122
112 SkPaint p; 123 SkPaint p;
113 p.setAntiAlias(true); 124 p.setAntiAlias(true);
114 p.setColor(color); 125 p.setColor(color);
115 p.setXfermodeMode(mode); 126 p.setXfermodeMode(mode);
116 canvas->save(); 127 canvas->save();
117 canvas->translate(dx, dy); 128 canvas->translate(dx, dy);
118 canvas->scale(s, s); 129 canvas->scale(s, s);
119 canvas->rotate(r); 130 canvas->rotate(r);
120 this->drawShape(canvas, p, &random); 131 this->drawShape(canvas, p, shapeType);
121 canvas->restore(); 132 canvas->restore();
122 } 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();
123 } 165 }
124 166
125 virtual uint32_t onGetFlags() const { 167 virtual uint32_t onGetFlags() const {
126 // Skip PDF rasterization since rendering this PDF takes forever. 168 // Skip PDF rasterization since rendering this PDF takes forever.
127 return kSkipPDFRasterization_Flag | kSkipTiled_Flag; 169 return kSkipPDFRasterization_Flag | kSkipTiled_Flag;
128 } 170 }
129 171
130 private: 172 private:
131 enum { 173 enum {
132 kNumShapes = 100, 174 kNumShapes = 100,
133 }; 175 };
134 SkAutoTUnref<SkShader> fBG; 176 SkAutoTUnref<SkShader> fBG;
135 SkPath fConcavePath; 177 SkPath fConcavePath;
136 SkPath fConvexPath; 178 SkPath fConvexPath;
137 typedef GM INHERITED; 179 typedef GM INHERITED;
138 }; 180 };
139 181
140 ////////////////////////////////////////////////////////////////////////////// 182 //////////////////////////////////////////////////////////////////////////////
141 183
142 static GM* MyFactory(void*) { return new MixedXfermodesGM; } 184 static GM* MyFactory(void*) { return new MixedXfermodesGM; }
143 static GMRegistry reg(MyFactory); 185 static GMRegistry reg(MyFactory);
144 186
145 } 187 }
OLDNEW
« no previous file with comments | « AUTHORS ('k') | src/core/SkXfermode.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698