reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame^] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "Sk64.h" |
| 5 | #include "SkCornerPathEffect.h" |
| 6 | #include "SkGradientShader.h" |
| 7 | #include "SkGraphics.h" |
| 8 | #include "SkImageDecoder.h" |
| 9 | #include "SkKernel33MaskFilter.h" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkRandom.h" |
| 12 | #include "SkRegion.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkUtils.h" |
| 15 | #include "SkColorPriv.h" |
| 16 | #include "SkColorFilter.h" |
| 17 | #include "SkTime.h" |
| 18 | #include "SkTypeface.h" |
| 19 | #include "SkXfermode.h" |
| 20 | |
| 21 | #include "SkStream.h" |
| 22 | #include "SkXMLParser.h" |
| 23 | #include "SkColorPriv.h" |
| 24 | #include "SkImageDecoder.h" |
| 25 | |
| 26 | static SkRandom gRand; |
| 27 | |
| 28 | static void test_chromium_9005() { |
| 29 | SkBitmap bm; |
| 30 | bm.setConfig(SkBitmap::kARGB_8888_Config, 800, 600); |
| 31 | bm.allocPixels(); |
| 32 | |
| 33 | SkCanvas canvas(bm); |
| 34 | |
| 35 | SkPoint pt0 = { SkFloatToScalar(799.33374f), SkFloatToScalar(1.2360189f) }; |
| 36 | SkPoint pt1 = { SkFloatToScalar(808.49969f), SkFloatToScalar(-7.4338055f) }; |
| 37 | |
| 38 | SkPaint paint; |
| 39 | paint.setAntiAlias(true); |
| 40 | canvas.drawLine(pt0.fX, pt0.fY, pt1.fX, pt1.fY, paint); |
| 41 | } |
| 42 | |
| 43 | static void generate_pts(SkPoint pts[], int count, int w, int h) { |
| 44 | for (int i = 0; i < count; i++) { |
| 45 | pts[i].set(gRand.nextUScalar1() * 3 * w - SkIntToScalar(w), |
| 46 | gRand.nextUScalar1() * 3 * h - SkIntToScalar(h)); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static bool check_zeros(const SkPMColor pixels[], int count, int skip) { |
| 51 | for (int i = 0; i < count; i++) { |
| 52 | if (*pixels) { |
| 53 | return false; |
| 54 | } |
| 55 | pixels += skip; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | static bool check_bitmap_margin(const SkBitmap& bm, int margin) { |
| 61 | size_t rb = bm.rowBytes(); |
| 62 | for (int i = 0; i < margin; i++) { |
| 63 | if (!check_zeros(bm.getAddr32(0, i), bm.width(), 1)) { |
| 64 | return false; |
| 65 | } |
| 66 | int bottom = bm.height() - i - 1; |
| 67 | if (!check_zeros(bm.getAddr32(0, bottom), bm.width(), 1)) { |
| 68 | return false; |
| 69 | } |
| 70 | // left column |
| 71 | if (!check_zeros(bm.getAddr32(i, 0), bm.height(), rb >> 2)) { |
| 72 | return false; |
| 73 | } |
| 74 | int right = bm.width() - margin + i; |
| 75 | if (!check_zeros(bm.getAddr32(right, 0), bm.height(), rb >> 2)) { |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | #define WIDTH 80 |
| 83 | #define HEIGHT 60 |
| 84 | #define MARGIN 4 |
| 85 | |
| 86 | class HairlineView : public SkView { |
| 87 | public: |
| 88 | HairlineView() {} |
| 89 | |
| 90 | protected: |
| 91 | // overrides from SkEventSink |
| 92 | virtual bool onQuery(SkEvent* evt) { |
| 93 | if (SampleCode::TitleQ(*evt)) { |
| 94 | SampleCode::TitleR(evt, "Hairines"); |
| 95 | return true; |
| 96 | } |
| 97 | return this->INHERITED::onQuery(evt); |
| 98 | } |
| 99 | |
| 100 | void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1, |
| 101 | const SkIRect& inset) { |
| 102 | canvas->drawBitmap(b0, 0, 0, NULL); |
| 103 | canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, NULL); |
| 104 | } |
| 105 | |
| 106 | void drawBG(SkCanvas* canvas) { |
| 107 | // canvas->drawColor(0xFFDDDDDD); |
| 108 | canvas->drawColor(SK_ColorWHITE); |
| 109 | // canvas->drawColor(SK_ColorBLACK); |
| 110 | } |
| 111 | |
| 112 | virtual void onDraw(SkCanvas* canvas) { |
| 113 | this->drawBG(canvas); |
| 114 | |
| 115 | if (true) { |
| 116 | test_chromium_9005(); |
| 117 | } |
| 118 | |
| 119 | SkBitmap bm, bm2; |
| 120 | bm.setConfig(SkBitmap::kARGB_8888_Config, |
| 121 | WIDTH + MARGIN*2, |
| 122 | HEIGHT + MARGIN*2); |
| 123 | bm.allocPixels(); |
| 124 | // this will erase our margin, which we want to always stay 0 |
| 125 | bm.eraseColor(0); |
| 126 | |
| 127 | bm2.setConfig(SkBitmap::kARGB_8888_Config, WIDTH, HEIGHT, |
| 128 | bm.rowBytes()); |
| 129 | bm2.setPixels(bm.getAddr32(MARGIN, MARGIN)); |
| 130 | |
| 131 | SkCanvas c2(bm2); |
| 132 | SkPaint paint; |
| 133 | paint.setAntiAlias(true); |
| 134 | for (int i = 0; i < 10000; i++) { |
| 135 | SkPoint pts[2]; |
| 136 | generate_pts(pts, 2, WIDTH, HEIGHT); |
| 137 | bm2.eraseColor(0); |
| 138 | c2.drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint); |
| 139 | if (!check_bitmap_margin(bm, MARGIN)) { |
| 140 | SkDebugf("---- hairline failure (%g %g) (%g %g)\n", |
| 141 | pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY); |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | this->inval(NULL); |
| 147 | } |
| 148 | |
| 149 | private: |
| 150 | typedef SkView INHERITED; |
| 151 | }; |
| 152 | |
| 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | |
| 155 | static SkView* MyFactory() { return new HairlineView; } |
| 156 | static SkViewRegister reg(MyFactory); |
| 157 | |