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() { |
| 189 | fProcIndex = 0; |
| 190 | fDoAA = true; |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 191 | fNow = 0; |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 192 | } |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 193 | |
| 194 | protected: |
| 195 | // overrides from SkEventSink |
| 196 | virtual bool onQuery(SkEvent* evt) { |
| 197 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 198 | SkString str; |
| 199 | str.printf("Hair-%s", gProcs[fProcIndex].fName); |
| 200 | SampleCode::TitleR(evt, str.c_str()); |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | return this->INHERITED::onQuery(evt); |
| 204 | } |
| 205 | |
| 206 | void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1, |
| 207 | const SkIRect& inset) { |
| 208 | canvas->drawBitmap(b0, 0, 0, NULL); |
| 209 | canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, NULL); |
| 210 | } |
| 211 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 212 | int fCounter; |
| 213 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 214 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 215 | gRand.setSeed(fNow); |
| 216 | |
| 217 | if (false) { |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 218 | test_chromium_9005(); |
| 219 | } |
| 220 | |
| 221 | SkBitmap bm, bm2; |
| 222 | bm.setConfig(SkBitmap::kARGB_8888_Config, |
| 223 | WIDTH + MARGIN*2, |
| 224 | HEIGHT + MARGIN*2); |
| 225 | bm.allocPixels(); |
| 226 | // this will erase our margin, which we want to always stay 0 |
| 227 | bm.eraseColor(0); |
| 228 | |
| 229 | bm2.setConfig(SkBitmap::kARGB_8888_Config, WIDTH, HEIGHT, |
| 230 | bm.rowBytes()); |
| 231 | bm2.setPixels(bm.getAddr32(MARGIN, MARGIN)); |
| 232 | |
| 233 | SkCanvas c2(bm2); |
| 234 | SkPaint paint; |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 235 | paint.setAntiAlias(fDoAA); |
| 236 | paint.setStyle(SkPaint::kStroke_Style); |
| 237 | |
| 238 | bm2.eraseColor(0); |
| 239 | gProcs[fProcIndex].fProc(&c2, paint, bm); |
| 240 | canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), NULL); |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 241 | |
| 242 | SkMSec now = SampleCode::GetAnimTime(); |
| 243 | if (fNow != now) { |
| 244 | fNow = now; |
| 245 | fCounter += 1; |
| 246 | fDoAA = !fDoAA; |
| 247 | if (fCounter > 50) { |
| 248 | fProcIndex = cycle_hairproc_index(fProcIndex); |
| 249 | // todo: signal that we want to rebuild our TITLE |
| 250 | fCounter = 0; |
| 251 | } |
| 252 | this->inval(NULL); |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 253 | } |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 254 | } |
| 255 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 256 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 257 | fDoAA = !fDoAA; |
| 258 | this->inval(NULL); |
| 259 | return this->INHERITED::onFindClickHandler(x, y); |
| 260 | } |
| 261 | |
| 262 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 263 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 264 | typedef SampleView INHERITED; |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | ////////////////////////////////////////////////////////////////////////////// |
| 268 | |
| 269 | static SkView* MyFactory() { return new HairlineView; } |
| 270 | static SkViewRegister reg(MyFactory); |
| 271 | |