| 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 |  | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 82 | #define WIDTH   620 | 
 | 83 | #define HEIGHT  460 | 
 | 84 | #define MARGIN  10 | 
 | 85 |  | 
 | 86 | static void line_proc(SkCanvas* canvas, const SkPaint& paint, | 
 | 87 |                       const SkBitmap& bm) { | 
 | 88 |     const int N = 2; | 
 | 89 |     SkPoint pts[N]; | 
 | 90 |     for (int i = 0; i < 400; i++) { | 
 | 91 |         generate_pts(pts, N, WIDTH, HEIGHT); | 
 | 92 |  | 
 | 93 |         canvas->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint); | 
 | 94 |         if (!check_bitmap_margin(bm, MARGIN)) { | 
 | 95 |             SkDebugf("---- hairline failure (%g %g) (%g %g)\n", | 
 | 96 |                      pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY); | 
 | 97 |             break; | 
 | 98 |         } | 
 | 99 |     } | 
 | 100 | } | 
 | 101 |  | 
 | 102 | static void poly_proc(SkCanvas* canvas, const SkPaint& paint, | 
 | 103 |                       const SkBitmap& bm) { | 
 | 104 |     const int N = 8; | 
 | 105 |     SkPoint pts[N]; | 
 | 106 |     for (int i = 0; i < 50; i++) { | 
 | 107 |         generate_pts(pts, N, WIDTH, HEIGHT); | 
 | 108 |          | 
 | 109 |         SkPath path; | 
 | 110 |         path.moveTo(pts[0]); | 
 | 111 |         for (int j = 1; j < N; j++) { | 
 | 112 |             path.lineTo(pts[j]); | 
 | 113 |         } | 
 | 114 |         canvas->drawPath(path, paint); | 
 | 115 |     } | 
 | 116 | } | 
 | 117 |  | 
 | 118 | static SkPoint ave(const SkPoint& a, const SkPoint& b) { | 
 | 119 |     SkPoint c = a + b; | 
 | 120 |     c.fX = SkScalarHalf(c.fX); | 
 | 121 |     c.fY = SkScalarHalf(c.fY); | 
 | 122 |     return c; | 
 | 123 | } | 
 | 124 |  | 
 | 125 | static void quad_proc(SkCanvas* canvas, const SkPaint& paint, | 
 | 126 |                       const SkBitmap& bm) { | 
 | 127 |     const int N = 30; | 
 | 128 |     SkPoint pts[N]; | 
 | 129 |     for (int i = 0; i < 10; i++) { | 
 | 130 |         generate_pts(pts, N, WIDTH, HEIGHT); | 
 | 131 |          | 
 | 132 |         SkPath path; | 
 | 133 |         path.moveTo(pts[0]); | 
 | 134 |         for (int j = 1; j < N - 2; j++) { | 
 | 135 |             path.quadTo(pts[j], ave(pts[j], pts[j+1])); | 
 | 136 |         } | 
 | 137 |         path.quadTo(pts[N - 2], pts[N - 1]); | 
 | 138 |          | 
 | 139 |         canvas->drawPath(path, paint); | 
 | 140 |     } | 
 | 141 | } | 
 | 142 |  | 
 | 143 | static void add_cubic(SkPath* path, const SkPoint& mid, const SkPoint& end) { | 
 | 144 |     SkPoint start; | 
 | 145 |     path->getLastPt(&start); | 
 | 146 |     path->cubicTo(ave(start, mid), ave(mid, end), end); | 
 | 147 | } | 
 | 148 |  | 
 | 149 | static void cube_proc(SkCanvas* canvas, const SkPaint& paint, | 
 | 150 |                       const SkBitmap& bm) { | 
 | 151 |     const int N = 30; | 
 | 152 |     SkPoint pts[N]; | 
 | 153 |     for (int i = 0; i < 10; i++) { | 
 | 154 |         generate_pts(pts, N, WIDTH, HEIGHT); | 
 | 155 |          | 
 | 156 |         SkPath path; | 
 | 157 |         path.moveTo(pts[0]); | 
 | 158 |         for (int j = 1; j < N - 2; j++) { | 
 | 159 |             add_cubic(&path, pts[j], ave(pts[j], pts[j+1])); | 
 | 160 |         } | 
 | 161 |         add_cubic(&path, pts[N - 2], pts[N - 1]); | 
 | 162 |          | 
 | 163 |         canvas->drawPath(path, paint); | 
 | 164 |     } | 
 | 165 | } | 
 | 166 |  | 
 | 167 | typedef void (*HairProc)(SkCanvas*, const SkPaint&, const SkBitmap&); | 
 | 168 |  | 
 | 169 | static const struct { | 
 | 170 |     const char* fName; | 
 | 171 |     HairProc    fProc; | 
 | 172 | } gProcs[] = { | 
 | 173 |     { "line",   line_proc }, | 
 | 174 |     { "poly",   poly_proc }, | 
 | 175 |     { "quad",   quad_proc }, | 
 | 176 |     { "cube",   cube_proc }, | 
 | 177 | }; | 
 | 178 |  | 
 | 179 | static int cycle_hairproc_index(int index) { | 
 | 180 |     return (index + 1) % SK_ARRAY_COUNT(gProcs); | 
 | 181 | } | 
| reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 182 |  | 
| mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 183 | class HairlineView : public SampleView { | 
| reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 184 |     SkMSec fNow; | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 185 |     int fProcIndex; | 
 | 186 |     bool fDoAA; | 
| reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 187 | public: | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 188 | 	HairlineView() { | 
| senorblanco@chromium.org | 50108cd | 2011-05-24 20:25:32 +0000 | [diff] [blame] | 189 |         fCounter = 0; | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 190 |         fProcIndex = 0; | 
 | 191 |         fDoAA = true; | 
| reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 192 |         fNow = 0; | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 193 |     } | 
| reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 194 |      | 
 | 195 | protected: | 
 | 196 |     // overrides from SkEventSink | 
 | 197 |     virtual bool onQuery(SkEvent* evt) { | 
 | 198 |         if (SampleCode::TitleQ(*evt)) { | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 199 |             SkString str; | 
 | 200 |             str.printf("Hair-%s", gProcs[fProcIndex].fName); | 
 | 201 |             SampleCode::TitleR(evt, str.c_str()); | 
| reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 202 |             return true; | 
 | 203 |         } | 
 | 204 |         return this->INHERITED::onQuery(evt); | 
 | 205 |     } | 
 | 206 |      | 
 | 207 |     void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1, | 
 | 208 |                       const SkIRect& inset) { | 
 | 209 |         canvas->drawBitmap(b0, 0, 0, NULL); | 
 | 210 |         canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, NULL); | 
 | 211 |     } | 
 | 212 |  | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 213 |     int fCounter; | 
 | 214 |  | 
| mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 215 |     virtual void onDrawContent(SkCanvas* canvas) { | 
| reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 216 |         gRand.setSeed(fNow); | 
 | 217 |          | 
 | 218 |         if (false) { | 
| reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 219 |             test_chromium_9005(); | 
 | 220 |         } | 
 | 221 |          | 
 | 222 |         SkBitmap bm, bm2; | 
 | 223 |         bm.setConfig(SkBitmap::kARGB_8888_Config, | 
 | 224 |                      WIDTH + MARGIN*2, | 
 | 225 |                      HEIGHT + MARGIN*2); | 
 | 226 |         bm.allocPixels(); | 
 | 227 |         // this will erase our margin, which we want to always stay 0 | 
 | 228 |         bm.eraseColor(0); | 
 | 229 |  | 
 | 230 |         bm2.setConfig(SkBitmap::kARGB_8888_Config, WIDTH, HEIGHT, | 
 | 231 |                       bm.rowBytes()); | 
 | 232 |         bm2.setPixels(bm.getAddr32(MARGIN, MARGIN)); | 
 | 233 |          | 
 | 234 |         SkCanvas c2(bm2); | 
 | 235 |         SkPaint paint; | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 236 |         paint.setAntiAlias(fDoAA); | 
 | 237 |         paint.setStyle(SkPaint::kStroke_Style); | 
 | 238 |  | 
 | 239 |         bm2.eraseColor(0); | 
 | 240 |         gProcs[fProcIndex].fProc(&c2, paint, bm); | 
 | 241 |         canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), NULL); | 
| reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 242 |  | 
 | 243 |         SkMSec now = SampleCode::GetAnimTime(); | 
 | 244 |         if (fNow != now) { | 
 | 245 |             fNow = now; | 
 | 246 |             fCounter += 1; | 
 | 247 |             fDoAA = !fDoAA; | 
 | 248 |             if (fCounter > 50) { | 
 | 249 |                 fProcIndex = cycle_hairproc_index(fProcIndex); | 
 | 250 |                 // todo: signal that we want to rebuild our TITLE | 
 | 251 |                 fCounter = 0; | 
 | 252 |             } | 
 | 253 |             this->inval(NULL); | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 254 |         } | 
| reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 255 |     } | 
 | 256 |  | 
| reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 257 |     virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { | 
 | 258 |         fDoAA = !fDoAA; | 
 | 259 |         this->inval(NULL); | 
 | 260 |         return this->INHERITED::onFindClickHandler(x, y); | 
 | 261 |     } | 
 | 262 |      | 
 | 263 |  | 
| reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 264 | private: | 
| mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 265 |     typedef SampleView INHERITED; | 
| reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 266 | }; | 
 | 267 |  | 
 | 268 | ////////////////////////////////////////////////////////////////////////////// | 
 | 269 |  | 
 | 270 | static SkView* MyFactory() { return new HairlineView; } | 
 | 271 | static SkViewRegister reg(MyFactory); | 
 | 272 |  |