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