bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | /** |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 17 | * Renders overlapping shapes with random SkXfermode::Modes against a checkerboard. |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 18 | */ |
| 19 | class MixedXfermodesGM : public GM { |
| 20 | public: |
| 21 | MixedXfermodesGM() { |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | protected: |
rosca | ace7f42 | 2014-11-20 07:24:32 -0800 | [diff] [blame] | 25 | enum ShapeType { |
| 26 | kShapeTypeCircle, |
| 27 | kShapeTypeRoundRect, |
| 28 | kShapeTypeRect, |
| 29 | kShapeTypeConvexPath, |
| 30 | kShapeTypeConcavePath, |
| 31 | kNumShapeTypes |
| 32 | }; |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkString onShortName() override { |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 35 | return SkString("mixed_xfermodes"); |
| 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 39 | return SkISize::Make(790, 640); |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 40 | } |
| 41 | |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 42 | void drawShape(SkCanvas* canvas, |
| 43 | const SkPaint& paint, |
rosca | ace7f42 | 2014-11-20 07:24:32 -0800 | [diff] [blame] | 44 | ShapeType type) { |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 45 | static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50), |
| 46 | SkIntToScalar(75), SkIntToScalar(105)); |
rosca | ace7f42 | 2014-11-20 07:24:32 -0800 | [diff] [blame] | 47 | 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.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 65 | } |
rosca | ace7f42 | 2014-11-20 07:24:32 -0800 | [diff] [blame] | 66 | 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.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 82 | } |
rosca | ace7f42 | 2014-11-20 07:24:32 -0800 | [diff] [blame] | 83 | canvas->drawPath(fConcavePath, paint); |
| 84 | break; |
| 85 | default: |
| 86 | break; |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 90 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 91 | if (NULL == fBG.get()) { |
| 92 | static uint32_t kCheckerPixelData[] = { 0xFFFFFFFF, |
| 93 | 0xFFCCCCCC, |
| 94 | 0xFFCCCCCC, |
| 95 | 0xFFFFFFFF }; |
| 96 | SkBitmap bitmap; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 97 | bitmap.allocN32Pixels(2, 2); |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 98 | memcpy(bitmap.getPixels(), kCheckerPixelData, sizeof(kCheckerPixelData)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 99 | SkMatrix lm; |
| 100 | lm.setScale(SkIntToScalar(20), SkIntToScalar(20)); |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 101 | fBG.reset(SkShader::CreateBitmapShader(bitmap, |
| 102 | SkShader::kRepeat_TileMode, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 103 | SkShader::kRepeat_TileMode, |
| 104 | &lm)); |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 105 | } |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 106 | |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 107 | SkPaint bgPaint; |
| 108 | bgPaint.setShader(fBG.get()); |
| 109 | canvas->drawPaint(bgPaint); |
| 110 | SkISize size = canvas->getDeviceSize(); |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 111 | SkScalar maxScale = SkScalarSqrt((SkIntToScalar(size.fWidth * size.fHeight))) / 300; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 112 | SkRandom random; |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 113 | 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.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 118 | SkColor color = random.nextU(); |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 119 | SkXfermode::Mode mode = |
| 120 | static_cast<SkXfermode::Mode>(random.nextULessThan(SkXfermode::kLastMode + 1)); |
rosca | ace7f42 | 2014-11-20 07:24:32 -0800 | [diff] [blame] | 121 | ShapeType shapeType = static_cast<ShapeType>(random.nextULessThan(kNumShapeTypes)); |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 122 | |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 123 | SkPaint p; |
| 124 | p.setAntiAlias(true); |
| 125 | p.setColor(color); |
| 126 | p.setXfermodeMode(mode); |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 127 | canvas->save(); |
| 128 | canvas->translate(dx, dy); |
| 129 | canvas->scale(s, s); |
| 130 | canvas->rotate(r); |
rosca | ace7f42 | 2014-11-20 07:24:32 -0800 | [diff] [blame] | 131 | this->drawShape(canvas, p, shapeType); |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 132 | canvas->restore(); |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 133 | } |
rosca | ace7f42 | 2014-11-20 07:24:32 -0800 | [diff] [blame] | 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(); |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 165 | } |
| 166 | |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 167 | private: |
| 168 | enum { |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 169 | kNumShapes = 100, |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 170 | }; |
| 171 | SkAutoTUnref<SkShader> fBG; |
bsalomon@google.com | 6db9626 | 2013-05-15 13:10:18 +0000 | [diff] [blame] | 172 | SkPath fConcavePath; |
| 173 | SkPath fConvexPath; |
bsalomon@google.com | 20edf38 | 2013-04-01 18:02:55 +0000 | [diff] [blame] | 174 | typedef GM INHERITED; |
| 175 | }; |
| 176 | |
| 177 | ////////////////////////////////////////////////////////////////////////////// |
| 178 | |
| 179 | static GM* MyFactory(void*) { return new MixedXfermodesGM; } |
| 180 | static GMRegistry reg(MyFactory); |
| 181 | |
| 182 | } |