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