tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPath.h" |
| 11 | |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 12 | #include "SkGradientShader.h" |
| 13 | static void test_shallow_gradient(SkCanvas* canvas, SkScalar width, SkScalar height) { |
| 14 | SkColor colors[] = { 0xFF7F7F7F, 0xFF7F7F7F, 0xFF000000 }; |
| 15 | SkScalar pos[] = { 0, 0.35f, SK_Scalar1 }; |
| 16 | SkPoint pts[] = { { 0, 0 }, { width, height } }; |
| 17 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos, |
| 18 | SK_ARRAY_COUNT(colors), |
| 19 | SkShader::kClamp_TileMode); |
| 20 | SkPaint paint; |
| 21 | paint.setShader(s)->unref(); |
| 22 | canvas->drawPaint(paint); |
| 23 | } |
| 24 | |
reed@google.com | b6399a7 | 2013-01-28 16:57:29 +0000 | [diff] [blame] | 25 | #include "SkDashPathEffect.h" |
| 26 | static void test_giant_dash(SkCanvas* canvas) { |
| 27 | SkPaint paint; |
| 28 | const SkScalar intervals[] = { SK_Scalar1, SK_Scalar1 }; |
| 29 | |
| 30 | paint.setStrokeWidth(2); |
| 31 | paint.setPathEffect(new SkDashPathEffect(intervals, 2, 0))->unref(); |
skia.committer@gmail.com | cdcb2ce | 2013-01-29 07:05:52 +0000 | [diff] [blame] | 32 | |
reed@google.com | b6399a7 | 2013-01-28 16:57:29 +0000 | [diff] [blame] | 33 | SkScalar big = 500 * 1000; |
| 34 | |
| 35 | canvas->drawLine(10, 10, big, 10, paint); |
| 36 | canvas->drawLine(-big, 20, 500, 20, paint); |
| 37 | canvas->drawLine(-big, 30, big, 30, paint); |
| 38 | |
| 39 | const SkScalar intervals2[] = { 20, 5, 10, 5 }; |
| 40 | paint.setPathEffect(new SkDashPathEffect(intervals2, 4, 17))->unref(); |
| 41 | |
| 42 | canvas->translate(0, 40); |
| 43 | SkScalar x = -500; |
| 44 | SkScalar width = 3173; |
| 45 | for (int i = 0; i < 40; ++i) { |
| 46 | if (i > 10) |
| 47 | canvas->drawLine(x, 0, x + width, 0, paint); |
| 48 | x += 1; |
| 49 | canvas->translate(0, 4); |
| 50 | } |
| 51 | } |
| 52 | |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 53 | |
| 54 | |
reed@google.com | e2f8c7c | 2012-11-27 21:11:05 +0000 | [diff] [blame] | 55 | // Reproduces bug found here: http://jsfiddle.net/R8Cu5/1/ |
| 56 | // |
| 57 | #include "SkGradientShader.h" |
| 58 | static void test_grad(SkCanvas* canvas) { |
| 59 | SkPoint pts[] = { |
| 60 | { 478.544067f, -84.2041016f }, |
| 61 | { 602.455933f, 625.204102f }, |
| 62 | }; |
| 63 | SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorRED, SK_ColorRED }; |
| 64 | SkScalar pos[] = { 0, 0.3f, 0.3f, 1.0f }; |
| 65 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos, 4, SkShader::kClamp_TileMode); |
| 66 | SkPaint p; |
| 67 | p.setShader(s)->unref(); |
| 68 | canvas->drawPaint(p); |
| 69 | } |
| 70 | |
reed@google.com | 5aff0ef | 2012-05-09 15:04:11 +0000 | [diff] [blame] | 71 | static SkCanvas* MakeCanvas(const SkIRect& bounds) { |
| 72 | SkBitmap bm; |
| 73 | bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height()); |
| 74 | bm.allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 75 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 5aff0ef | 2012-05-09 15:04:11 +0000 | [diff] [blame] | 76 | |
| 77 | SkCanvas* canvas = new SkCanvas(bm); |
| 78 | canvas->translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop)); |
| 79 | return canvas; |
| 80 | } |
| 81 | |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 82 | #ifdef SK_DEBUG |
reed@google.com | 5aff0ef | 2012-05-09 15:04:11 +0000 | [diff] [blame] | 83 | static void GetBitmap(const SkCanvas* canvas, SkBitmap* bm) { |
| 84 | *bm = canvas->getDevice()->accessBitmap(false); |
| 85 | } |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 86 | #endif |
reed@google.com | 5aff0ef | 2012-05-09 15:04:11 +0000 | [diff] [blame] | 87 | |
| 88 | static void compare_canvas(const SkCanvas* a, const SkCanvas* b) { |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 89 | #ifdef SK_DEBUG |
reed@google.com | 5aff0ef | 2012-05-09 15:04:11 +0000 | [diff] [blame] | 90 | SkBitmap bma, bmb; |
| 91 | GetBitmap(a, &bma); |
| 92 | GetBitmap(b, &bmb); |
| 93 | |
| 94 | SkASSERT(bma.width() == bmb.width()); |
| 95 | SkASSERT(bma.height() == bmb.height()); |
| 96 | |
| 97 | bma.lockPixels(); |
| 98 | bmb.lockPixels(); |
| 99 | for (int y = 0; y < bma.height(); ++y) { |
| 100 | const SkPMColor* rowa = bma.getAddr32(0, y); |
| 101 | const SkPMColor* rowb = bmb.getAddr32(0, y); |
| 102 | SkASSERT(!memcmp(rowa, rowb, bma.width() << 2)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 103 | |
reed@google.com | 5aff0ef | 2012-05-09 15:04:11 +0000 | [diff] [blame] | 104 | for (int x = 1; x < bma.width() - 1; ++x) { |
| 105 | SkASSERT(0xFF000000 == rowa[x]); |
| 106 | SkASSERT(0xFF000000 == rowb[x]); |
| 107 | } |
| 108 | } |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 109 | #endif |
reed@google.com | 5aff0ef | 2012-05-09 15:04:11 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void drawRectAsPath(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 113 | SkPath path; |
| 114 | path.addRect(r); |
| 115 | canvas->drawPath(path, p); |
| 116 | } |
| 117 | |
| 118 | static void test_maskFromPath(const SkPath& path) { |
| 119 | SkIRect bounds; |
| 120 | path.getBounds().roundOut(&bounds); |
| 121 | |
| 122 | SkPaint paint; |
| 123 | paint.setAntiAlias(true); |
| 124 | |
| 125 | SkAutoTUnref<SkCanvas> path_canvas(MakeCanvas(bounds)); |
| 126 | path_canvas->drawPath(path, paint); |
| 127 | |
| 128 | SkAutoTUnref<SkCanvas> rect_canvas(MakeCanvas(bounds)); |
| 129 | drawRectAsPath(rect_canvas, path.getBounds(), paint); |
| 130 | |
| 131 | compare_canvas(path_canvas, rect_canvas); |
| 132 | } |
| 133 | |
| 134 | static void test_mask() { |
| 135 | for (int i = 1; i <= 20; ++i) { |
| 136 | const SkScalar dx = SK_Scalar1 / i; |
| 137 | const SkRect constr = SkRect::MakeWH(dx, SkIntToScalar(2)); |
| 138 | for (int n = 2; n < 20; ++n) { |
| 139 | SkPath path; |
| 140 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 141 | SkRect r = constr; |
| 142 | while (r.fRight < SkIntToScalar(4)) { |
| 143 | path.addRect(r); |
| 144 | r.offset(dx, 0); |
| 145 | } |
| 146 | test_maskFromPath(path); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 151 | namespace skiagm { |
| 152 | |
| 153 | /** Draw a 2px border around the target, then red behind the target; |
| 154 | set the clip to match the target, then draw >> the target in blue. |
| 155 | */ |
| 156 | |
caryclark@google.com | 1313086 | 2012-06-06 12:10:45 +0000 | [diff] [blame] | 157 | static void draw (SkCanvas* canvas, SkRect& target, int x, int y) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 158 | SkPaint borderPaint; |
| 159 | borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0)); |
| 160 | borderPaint.setAntiAlias(true); |
| 161 | SkPaint backgroundPaint; |
| 162 | backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0)); |
| 163 | backgroundPaint.setAntiAlias(true); |
| 164 | SkPaint foregroundPaint; |
| 165 | foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD)); |
| 166 | foregroundPaint.setAntiAlias(true); |
| 167 | |
| 168 | canvas->save(); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 169 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 170 | target.inset(SkIntToScalar(-2), SkIntToScalar(-2)); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 171 | canvas->drawRect(target, borderPaint); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 172 | target.inset(SkIntToScalar(2), SkIntToScalar(2)); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 173 | canvas->drawRect(target, backgroundPaint); |
| 174 | canvas->clipRect(target, SkRegion::kIntersect_Op, true); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 175 | target.inset(SkIntToScalar(-4), SkIntToScalar(-4)); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 176 | canvas->drawRect(target, foregroundPaint); |
| 177 | canvas->restore(); |
| 178 | } |
| 179 | |
caryclark@google.com | 1313086 | 2012-06-06 12:10:45 +0000 | [diff] [blame] | 180 | static void draw_square (SkCanvas* canvas, int x, int y) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 181 | SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1)); |
| 182 | draw(canvas, target, x, y); |
| 183 | } |
| 184 | |
caryclark@google.com | 1313086 | 2012-06-06 12:10:45 +0000 | [diff] [blame] | 185 | static void draw_column (SkCanvas* canvas, int x, int y) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 186 | SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1)); |
| 187 | draw(canvas, target, x, y); |
| 188 | } |
| 189 | |
caryclark@google.com | 1313086 | 2012-06-06 12:10:45 +0000 | [diff] [blame] | 190 | static void draw_bar (SkCanvas* canvas, int x, int y) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 191 | SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1)); |
| 192 | draw(canvas, target, x, y); |
| 193 | } |
| 194 | |
caryclark@google.com | 1313086 | 2012-06-06 12:10:45 +0000 | [diff] [blame] | 195 | static void draw_rect_tests (SkCanvas* canvas) { |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 196 | draw_square(canvas, 10, 10); |
| 197 | draw_column(canvas, 30, 10); |
| 198 | draw_bar(canvas, 10, 30); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | Test a set of clipping problems discovered while writing blitAntiRect, |
| 203 | and test all the code paths through the clipping blitters. |
| 204 | Each region should show as a blue center surrounded by a 2px green |
| 205 | border, with no red. |
| 206 | */ |
| 207 | |
| 208 | class AAClipGM : public GM { |
| 209 | public: |
| 210 | AAClipGM() { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 211 | |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | protected: |
| 215 | virtual SkString onShortName() { |
| 216 | return SkString("aaclip"); |
| 217 | } |
| 218 | |
| 219 | virtual SkISize onISize() { |
| 220 | return make_isize(640, 480); |
| 221 | } |
| 222 | |
reed@google.com | b6399a7 | 2013-01-28 16:57:29 +0000 | [diff] [blame] | 223 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 224 | if (false) { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 225 | SkRect bounds; |
| 226 | canvas->getClipBounds(&bounds); |
| 227 | test_shallow_gradient(canvas, bounds.width(), bounds.height()); return; |
| 228 | } |
| 229 | if (false) { |
skia.committer@gmail.com | cdcb2ce | 2013-01-29 07:05:52 +0000 | [diff] [blame] | 230 | test_giant_dash(canvas); return; |
reed@google.com | b6399a7 | 2013-01-28 16:57:29 +0000 | [diff] [blame] | 231 | } |
reed@google.com | e2f8c7c | 2012-11-27 21:11:05 +0000 | [diff] [blame] | 232 | if (false) { |
| 233 | test_grad(canvas); return; |
| 234 | } |
caryclark@google.com | 1313086 | 2012-06-06 12:10:45 +0000 | [diff] [blame] | 235 | if (false) { // avoid bit rot, suppress warning |
| 236 | test_mask(); |
| 237 | } |
reed@google.com | 5aff0ef | 2012-05-09 15:04:11 +0000 | [diff] [blame] | 238 | |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 239 | // Initial pixel-boundary-aligned draw |
| 240 | draw_rect_tests(canvas); |
| 241 | |
| 242 | // Repeat 4x with .2, .4, .6, .8 px offsets |
| 243 | canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 244 | canvas->translate(SkIntToScalar(50), 0); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 245 | draw_rect_tests(canvas); |
| 246 | |
| 247 | canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 248 | canvas->translate(SkIntToScalar(50), 0); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 249 | draw_rect_tests(canvas); |
| 250 | |
| 251 | canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 252 | canvas->translate(SkIntToScalar(50), 0); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 253 | draw_rect_tests(canvas); |
| 254 | |
| 255 | canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5); |
tomhudson@google.com | abfa8d1 | 2011-12-28 19:40:48 +0000 | [diff] [blame] | 256 | canvas->translate(SkIntToScalar(50), 0); |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 257 | draw_rect_tests(canvas); |
| 258 | } |
| 259 | |
borenet@google.com | b4e7043 | 2012-07-20 18:45:10 +0000 | [diff] [blame] | 260 | virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; } |
| 261 | |
tomhudson@google.com | ef279d3 | 2011-12-21 14:27:14 +0000 | [diff] [blame] | 262 | private: |
| 263 | typedef GM INHERITED; |
| 264 | }; |
| 265 | |
| 266 | ////////////////////////////////////////////////////////////////////////////// |
| 267 | |
| 268 | static GM* MyFactory(void*) { return new AAClipGM; } |
| 269 | static GMRegistry reg(MyFactory); |
| 270 | |
| 271 | } |