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 | |
| 183 | class HairlineView : public SkView { |
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 | |
| 212 | void drawBG(SkCanvas* canvas) { |
| 213 | // canvas->drawColor(0xFFDDDDDD); |
| 214 | canvas->drawColor(SK_ColorWHITE); |
| 215 | // canvas->drawColor(SK_ColorBLACK); |
| 216 | } |
| 217 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 218 | int fCounter; |
| 219 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 220 | virtual void onDraw(SkCanvas* canvas) { |
| 221 | this->drawBG(canvas); |
| 222 | |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame^] | 223 | gRand.setSeed(fNow); |
| 224 | |
| 225 | if (false) { |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 226 | test_chromium_9005(); |
| 227 | } |
| 228 | |
| 229 | SkBitmap bm, bm2; |
| 230 | bm.setConfig(SkBitmap::kARGB_8888_Config, |
| 231 | WIDTH + MARGIN*2, |
| 232 | HEIGHT + MARGIN*2); |
| 233 | bm.allocPixels(); |
| 234 | // this will erase our margin, which we want to always stay 0 |
| 235 | bm.eraseColor(0); |
| 236 | |
| 237 | bm2.setConfig(SkBitmap::kARGB_8888_Config, WIDTH, HEIGHT, |
| 238 | bm.rowBytes()); |
| 239 | bm2.setPixels(bm.getAddr32(MARGIN, MARGIN)); |
| 240 | |
| 241 | SkCanvas c2(bm2); |
| 242 | SkPaint paint; |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 243 | paint.setAntiAlias(fDoAA); |
| 244 | paint.setStyle(SkPaint::kStroke_Style); |
| 245 | |
| 246 | bm2.eraseColor(0); |
| 247 | gProcs[fProcIndex].fProc(&c2, paint, bm); |
| 248 | canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), NULL); |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame^] | 249 | |
| 250 | SkMSec now = SampleCode::GetAnimTime(); |
| 251 | if (fNow != now) { |
| 252 | fNow = now; |
| 253 | fCounter += 1; |
| 254 | fDoAA = !fDoAA; |
| 255 | if (fCounter > 50) { |
| 256 | fProcIndex = cycle_hairproc_index(fProcIndex); |
| 257 | // todo: signal that we want to rebuild our TITLE |
| 258 | fCounter = 0; |
| 259 | } |
| 260 | this->inval(NULL); |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 261 | } |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 262 | } |
| 263 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 264 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 265 | fDoAA = !fDoAA; |
| 266 | this->inval(NULL); |
| 267 | return this->INHERITED::onFindClickHandler(x, y); |
| 268 | } |
| 269 | |
| 270 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 271 | private: |
| 272 | typedef SkView INHERITED; |
| 273 | }; |
| 274 | |
| 275 | ////////////////////////////////////////////////////////////////////////////// |
| 276 | |
| 277 | static SkView* MyFactory() { return new HairlineView; } |
| 278 | static SkViewRegister reg(MyFactory); |
| 279 | |