Preventing division by 0 in non-separable blend mode shaders.

In the software path, the same issue has been fixed some time ago:
https://codereview.chromium.org/114173002

BUG=skia:

Review URL: https://codereview.chromium.org/666043003
This commit is contained in:
rosca 2014-11-20 07:24:32 -08:00 committed by Commit bot
parent e097be50bd
commit ace7f42769
3 changed files with 84 additions and 41 deletions

View File

@ -31,3 +31,4 @@ Steve Singer <steve@ssinger.info>
The Chromium Authors <*@chromium.org> The Chromium Authors <*@chromium.org>
Thiago Fransosi Farina <thiago.farina@gmail.com> Thiago Fransosi Farina <thiago.farina@gmail.com>
Pavel Krajcevski <pavel@cs.unc.edu> Pavel Krajcevski <pavel@cs.unc.edu>
Ion Rosca <rosca@adobe.com>

View File

@ -22,6 +22,15 @@ public:
} }
protected: protected:
enum ShapeType {
kShapeTypeCircle,
kShapeTypeRoundRect,
kShapeTypeRect,
kShapeTypeConvexPath,
kShapeTypeConcavePath,
kNumShapeTypes
};
virtual SkString onShortName() SK_OVERRIDE { virtual SkString onShortName() SK_OVERRIDE {
return SkString("mixed_xfermodes"); return SkString("mixed_xfermodes");
} }
@ -32,48 +41,49 @@ protected:
void drawShape(SkCanvas* canvas, void drawShape(SkCanvas* canvas,
const SkPaint& paint, const SkPaint& paint,
SkRandom* random) { ShapeType type) {
static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50), static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50),
SkIntToScalar(75), SkIntToScalar(105)); SkIntToScalar(75), SkIntToScalar(105));
int shape = random->nextULessThan(5); switch (type) {
switch (shape) { case kShapeTypeCircle:
case 0: canvas->drawCircle(0, 0, 50, paint);
canvas->drawCircle(0, 0, 50, paint); break;
break; case kShapeTypeRoundRect:
case 1: canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), paint);
canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), paint); break;
break; case kShapeTypeRect:
case 2: canvas->drawRect(kRect, paint);
canvas->drawRect(kRect, paint); break;
break; case kShapeTypeConvexPath:
case 3: if (fConvexPath.isEmpty()) {
if (fConvexPath.isEmpty()) { SkPoint points[4];
SkPoint points[4]; kRect.toQuad(points);
kRect.toQuad(points); fConvexPath.moveTo(points[0]);
fConvexPath.moveTo(points[0]); fConvexPath.quadTo(points[1], points[2]);
fConvexPath.quadTo(points[1], points[2]); fConvexPath.quadTo(points[3], points[0]);
fConvexPath.quadTo(points[3], points[0]); SkASSERT(fConvexPath.isConvex());
SkASSERT(fConvexPath.isConvex());
}
canvas->drawPath(fConvexPath, paint);
break;
case 4:
if (fConcavePath.isEmpty()) {
SkPoint points[5] = {{0, SkIntToScalar(-50)} };
SkMatrix rot;
rot.setRotate(SkIntToScalar(360) / 5);
for (int i = 1; i < 5; ++i) {
rot.mapPoints(points + i, points + i - 1, 1);
} }
fConcavePath.moveTo(points[0]); canvas->drawPath(fConvexPath, paint);
for (int i = 0; i < 5; ++i) { break;
fConcavePath.lineTo(points[(2 * i) % 5]); case kShapeTypeConcavePath:
if (fConcavePath.isEmpty()) {
SkPoint points[5] = {{0, SkIntToScalar(-50)} };
SkMatrix rot;
rot.setRotate(SkIntToScalar(360) / 5);
for (int i = 1; i < 5; ++i) {
rot.mapPoints(points + i, points + i - 1, 1);
}
fConcavePath.moveTo(points[0]);
for (int i = 0; i < 5; ++i) {
fConcavePath.lineTo(points[(2 * i) % 5]);
}
fConcavePath.setFillType(SkPath::kEvenOdd_FillType);
SkASSERT(!fConcavePath.isConvex());
} }
fConcavePath.setFillType(SkPath::kEvenOdd_FillType); canvas->drawPath(fConcavePath, paint);
SkASSERT(!fConcavePath.isConvex()); break;
} default:
canvas->drawPath(fConcavePath, paint); break;
break;
} }
} }
@ -108,6 +118,7 @@ protected:
SkColor color = random.nextU(); SkColor color = random.nextU();
SkXfermode::Mode mode = SkXfermode::Mode mode =
static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::kLastMode + 1)); static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::kLastMode + 1));
ShapeType shapeType = static_cast<ShapeType>(random.nextULessThan(kNumShapeTypes));
SkPaint p; SkPaint p;
p.setAntiAlias(true); p.setAntiAlias(true);
@ -117,9 +128,40 @@ protected:
canvas->translate(dx, dy); canvas->translate(dx, dy);
canvas->scale(s, s); canvas->scale(s, s);
canvas->rotate(r); canvas->rotate(r);
this->drawShape(canvas, p, &random); this->drawShape(canvas, p, shapeType);
canvas->restore(); canvas->restore();
} }
// This draw should not affect the test's result.
drawWithHueOnWhite(canvas);
}
/**
* Draws white color into a white square using the hue blend mode.
* The result color should be white, so it doesn't change the expectations.
* This will test a divide by 0 bug in shaders' setLum function,
* which used to output black pixels.
*/
void drawWithHueOnWhite(SkCanvas* canvas) {
SkColor color = SkColorSetARGBMacro(225, 255, 255, 255);
SkXfermode::Mode mode = SkXfermode::kHue_Mode;
ShapeType shapeType = kShapeTypeConvexPath;
// Make it fit into a square.
SkScalar s = 0.15f;
// Look for a clean white square.
SkScalar dx = 30.f;
SkScalar dy = 350.f;
SkPaint p;
p.setAntiAlias(true);
p.setColor(color);
p.setXfermodeMode(mode);
canvas->save();
canvas->translate(dx, dy);
canvas->scale(s, s);
this->drawShape(canvas, p, shapeType);
canvas->restore();
} }
virtual uint32_t onGetFlags() const { virtual uint32_t onGetFlags() const {

View File

@ -1101,10 +1101,10 @@ public:
setLumBody.appendf("\tfloat outLum = %s(outColor);\n", getFunction.c_str()); setLumBody.appendf("\tfloat outLum = %s(outColor);\n", getFunction.c_str());
setLumBody.append("\tfloat minComp = min(min(outColor.r, outColor.g), outColor.b);\n" setLumBody.append("\tfloat minComp = min(min(outColor.r, outColor.g), outColor.b);\n"
"\tfloat maxComp = max(max(outColor.r, outColor.g), outColor.b);\n" "\tfloat maxComp = max(max(outColor.r, outColor.g), outColor.b);\n"
"\tif (minComp < 0.0) {\n" "\tif (minComp < 0.0 && outLum != minComp) {\n"
"\t\toutColor = outLum + ((outColor - vec3(outLum, outLum, outLum)) * outLum) / (outLum - minComp);\n" "\t\toutColor = outLum + ((outColor - vec3(outLum, outLum, outLum)) * outLum) / (outLum - minComp);\n"
"\t}\n" "\t}\n"
"\tif (maxComp > alpha) {\n" "\tif (maxComp > alpha && maxComp != outLum) {\n"
"\t\toutColor = outLum + ((outColor - vec3(outLum, outLum, outLum)) * (alpha - outLum)) / (maxComp - outLum);\n" "\t\toutColor = outLum + ((outColor - vec3(outLum, outLum, outLum)) * (alpha - outLum)) / (maxComp - outLum);\n"
"\t}\n" "\t}\n"
"\treturn outColor;\n"); "\treturn outColor;\n");