2013-04-01 18:02:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
/**
|
2013-05-15 13:10:18 +00:00
|
|
|
* Renders overlapping shapes with random SkXfermode::Modes against a checkerboard.
|
2013-04-01 18:02:55 +00:00
|
|
|
*/
|
|
|
|
class MixedXfermodesGM : public GM {
|
|
|
|
public:
|
|
|
|
MixedXfermodesGM() {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-11-20 15:24:32 +00:00
|
|
|
enum ShapeType {
|
|
|
|
kShapeTypeCircle,
|
|
|
|
kShapeTypeRoundRect,
|
|
|
|
kShapeTypeRect,
|
|
|
|
kShapeTypeConvexPath,
|
|
|
|
kShapeTypeConcavePath,
|
|
|
|
kNumShapeTypes
|
|
|
|
};
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
SkString onShortName() SK_OVERRIDE {
|
2013-04-01 18:02:55 +00:00
|
|
|
return SkString("mixed_xfermodes");
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
SkISize onISize() SK_OVERRIDE {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(790, 640);
|
2013-04-01 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2013-05-15 13:10:18 +00:00
|
|
|
void drawShape(SkCanvas* canvas,
|
|
|
|
const SkPaint& paint,
|
2014-11-20 15:24:32 +00:00
|
|
|
ShapeType type) {
|
2013-05-15 13:10:18 +00:00
|
|
|
static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50),
|
|
|
|
SkIntToScalar(75), SkIntToScalar(105));
|
2014-11-20 15:24:32 +00:00
|
|
|
switch (type) {
|
|
|
|
case kShapeTypeCircle:
|
|
|
|
canvas->drawCircle(0, 0, 50, paint);
|
|
|
|
break;
|
|
|
|
case kShapeTypeRoundRect:
|
|
|
|
canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), paint);
|
|
|
|
break;
|
|
|
|
case kShapeTypeRect:
|
|
|
|
canvas->drawRect(kRect, paint);
|
|
|
|
break;
|
|
|
|
case kShapeTypeConvexPath:
|
|
|
|
if (fConvexPath.isEmpty()) {
|
|
|
|
SkPoint points[4];
|
|
|
|
kRect.toQuad(points);
|
|
|
|
fConvexPath.moveTo(points[0]);
|
|
|
|
fConvexPath.quadTo(points[1], points[2]);
|
|
|
|
fConvexPath.quadTo(points[3], points[0]);
|
|
|
|
SkASSERT(fConvexPath.isConvex());
|
2013-05-15 13:10:18 +00:00
|
|
|
}
|
2014-11-20 15:24:32 +00:00
|
|
|
canvas->drawPath(fConvexPath, paint);
|
|
|
|
break;
|
|
|
|
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());
|
2013-05-15 13:10:18 +00:00
|
|
|
}
|
2014-11-20 15:24:32 +00:00
|
|
|
canvas->drawPath(fConcavePath, paint);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-05-15 13:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2013-05-15 13:10:18 +00:00
|
|
|
if (NULL == fBG.get()) {
|
|
|
|
static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF,
|
|
|
|
0xFFCCCCCC,
|
|
|
|
0xFFCCCCCC,
|
|
|
|
0xFFFFFFFF };
|
|
|
|
SkBitmap bitmap;
|
2014-01-25 16:46:20 +00:00
|
|
|
bitmap.allocN32Pixels(2, 2);
|
2013-05-15 13:10:18 +00:00
|
|
|
memcpy(bitmap.getPixels(), kCheckerPixelData, sizeof(kCheckerPixelData));
|
2014-04-28 14:55:39 +00:00
|
|
|
SkMatrix lm;
|
|
|
|
lm.setScale(SkIntToScalar(20), SkIntToScalar(20));
|
2013-05-15 13:10:18 +00:00
|
|
|
fBG.reset(SkShader::CreateBitmapShader(bitmap,
|
|
|
|
SkShader::kRepeat_TileMode,
|
2014-04-28 14:55:39 +00:00
|
|
|
SkShader::kRepeat_TileMode,
|
|
|
|
&lm));
|
2013-05-15 13:10:18 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 18:02:55 +00:00
|
|
|
SkPaint bgPaint;
|
|
|
|
bgPaint.setShader(fBG.get());
|
|
|
|
canvas->drawPaint(bgPaint);
|
|
|
|
SkISize size = canvas->getDeviceSize();
|
2013-05-15 13:10:18 +00:00
|
|
|
SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeight))) / 300;
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom random;
|
2013-05-15 13:10:18 +00:00
|
|
|
for (int i = 0; i < kNumShapes; ++i) {
|
|
|
|
SkScalar s = random.nextRangeScalar(SK_Scalar1 / 8, SK_Scalar1) * maxScale;
|
|
|
|
SkScalar r = random.nextRangeScalar(0, SkIntToScalar(360));
|
|
|
|
SkScalar dx = random.nextRangeScalar(0, SkIntToScalar(size.fWidth));
|
|
|
|
SkScalar dy = random.nextRangeScalar(0, SkIntToScalar(size.fHeight));
|
2013-04-01 18:02:55 +00:00
|
|
|
SkColor color = random.nextU();
|
|
|
|
SkXfermode::Mode mode =
|
|
|
|
static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::kLastMode + 1));
|
2014-11-20 15:24:32 +00:00
|
|
|
ShapeType shapeType = static_cast<ShapeType>(random.nextULessThan(kNumShapeTypes));
|
2013-04-02 07:01:34 +00:00
|
|
|
|
2013-04-01 18:02:55 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setColor(color);
|
|
|
|
p.setXfermodeMode(mode);
|
2013-05-15 13:10:18 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(dx, dy);
|
|
|
|
canvas->scale(s, s);
|
|
|
|
canvas->rotate(r);
|
2014-11-20 15:24:32 +00:00
|
|
|
this->drawShape(canvas, p, shapeType);
|
2013-05-15 13:10:18 +00:00
|
|
|
canvas->restore();
|
2013-04-01 18:02:55 +00:00
|
|
|
}
|
2014-11-20 15:24:32 +00:00
|
|
|
|
|
|
|
// 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();
|
2013-04-01 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum {
|
2013-05-15 13:10:18 +00:00
|
|
|
kNumShapes = 100,
|
2013-04-01 18:02:55 +00:00
|
|
|
};
|
|
|
|
SkAutoTUnref<SkShader> fBG;
|
2013-05-15 13:10:18 +00:00
|
|
|
SkPath fConcavePath;
|
|
|
|
SkPath fConvexPath;
|
2013-04-01 18:02:55 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new MixedXfermodesGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|