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