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