epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
reed@google.com | bf0001d | 2014-01-13 14:53:55 +0000 | [diff] [blame] | 7 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 11 | #include "SkCornerPathEffect.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkGraphics.h" |
| 14 | #include "SkImageDecoder.h" |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 15 | #include "SkPath.h" |
| 16 | #include "SkRandom.h" |
| 17 | #include "SkRegion.h" |
| 18 | #include "SkShader.h" |
| 19 | #include "SkUtils.h" |
| 20 | #include "SkColorPriv.h" |
| 21 | #include "SkColorFilter.h" |
| 22 | #include "SkTime.h" |
| 23 | #include "SkTypeface.h" |
| 24 | #include "SkXfermode.h" |
| 25 | |
| 26 | #include "SkStream.h" |
| 27 | #include "SkXMLParser.h" |
| 28 | #include "SkColorPriv.h" |
| 29 | #include "SkImageDecoder.h" |
| 30 | |
| 31 | static SkRandom gRand; |
| 32 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 33 | static void generate_pts(SkPoint pts[], int count, int w, int h) { |
| 34 | for (int i = 0; i < count; i++) { |
| 35 | pts[i].set(gRand.nextUScalar1() * 3 * w - SkIntToScalar(w), |
| 36 | gRand.nextUScalar1() * 3 * h - SkIntToScalar(h)); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static bool check_zeros(const SkPMColor pixels[], int count, int skip) { |
| 41 | for (int i = 0; i < count; i++) { |
| 42 | if (*pixels) { |
| 43 | return false; |
| 44 | } |
| 45 | pixels += skip; |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | static bool check_bitmap_margin(const SkBitmap& bm, int margin) { |
| 51 | size_t rb = bm.rowBytes(); |
| 52 | for (int i = 0; i < margin; i++) { |
| 53 | if (!check_zeros(bm.getAddr32(0, i), bm.width(), 1)) { |
| 54 | return false; |
| 55 | } |
| 56 | int bottom = bm.height() - i - 1; |
| 57 | if (!check_zeros(bm.getAddr32(0, bottom), bm.width(), 1)) { |
| 58 | return false; |
| 59 | } |
| 60 | // left column |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 61 | if (!check_zeros(bm.getAddr32(i, 0), bm.height(), SkToInt(rb >> 2))) { |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | int right = bm.width() - margin + i; |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 65 | if (!check_zeros(bm.getAddr32(right, 0), bm.height(), |
| 66 | SkToInt(rb >> 2))) { |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 73 | #define WIDTH 620 |
| 74 | #define HEIGHT 460 |
| 75 | #define MARGIN 10 |
| 76 | |
| 77 | static void line_proc(SkCanvas* canvas, const SkPaint& paint, |
| 78 | const SkBitmap& bm) { |
| 79 | const int N = 2; |
| 80 | SkPoint pts[N]; |
| 81 | for (int i = 0; i < 400; i++) { |
| 82 | generate_pts(pts, N, WIDTH, HEIGHT); |
| 83 | |
| 84 | canvas->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint); |
| 85 | if (!check_bitmap_margin(bm, MARGIN)) { |
| 86 | SkDebugf("---- hairline failure (%g %g) (%g %g)\n", |
| 87 | pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY); |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static void poly_proc(SkCanvas* canvas, const SkPaint& paint, |
| 94 | const SkBitmap& bm) { |
| 95 | const int N = 8; |
| 96 | SkPoint pts[N]; |
| 97 | for (int i = 0; i < 50; i++) { |
| 98 | generate_pts(pts, N, WIDTH, HEIGHT); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 99 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 100 | SkPath path; |
| 101 | path.moveTo(pts[0]); |
| 102 | for (int j = 1; j < N; j++) { |
| 103 | path.lineTo(pts[j]); |
| 104 | } |
| 105 | canvas->drawPath(path, paint); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static SkPoint ave(const SkPoint& a, const SkPoint& b) { |
| 110 | SkPoint c = a + b; |
| 111 | c.fX = SkScalarHalf(c.fX); |
| 112 | c.fY = SkScalarHalf(c.fY); |
| 113 | return c; |
| 114 | } |
| 115 | |
| 116 | static void quad_proc(SkCanvas* canvas, const SkPaint& paint, |
| 117 | const SkBitmap& bm) { |
| 118 | const int N = 30; |
| 119 | SkPoint pts[N]; |
| 120 | for (int i = 0; i < 10; i++) { |
| 121 | generate_pts(pts, N, WIDTH, HEIGHT); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 123 | SkPath path; |
| 124 | path.moveTo(pts[0]); |
| 125 | for (int j = 1; j < N - 2; j++) { |
| 126 | path.quadTo(pts[j], ave(pts[j], pts[j+1])); |
| 127 | } |
| 128 | path.quadTo(pts[N - 2], pts[N - 1]); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 129 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 130 | canvas->drawPath(path, paint); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static void add_cubic(SkPath* path, const SkPoint& mid, const SkPoint& end) { |
| 135 | SkPoint start; |
| 136 | path->getLastPt(&start); |
| 137 | path->cubicTo(ave(start, mid), ave(mid, end), end); |
| 138 | } |
| 139 | |
| 140 | static void cube_proc(SkCanvas* canvas, const SkPaint& paint, |
| 141 | const SkBitmap& bm) { |
| 142 | const int N = 30; |
| 143 | SkPoint pts[N]; |
| 144 | for (int i = 0; i < 10; i++) { |
| 145 | generate_pts(pts, N, WIDTH, HEIGHT); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 146 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 147 | SkPath path; |
| 148 | path.moveTo(pts[0]); |
| 149 | for (int j = 1; j < N - 2; j++) { |
| 150 | add_cubic(&path, pts[j], ave(pts[j], pts[j+1])); |
| 151 | } |
| 152 | add_cubic(&path, pts[N - 2], pts[N - 1]); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 153 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 154 | canvas->drawPath(path, paint); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | typedef void (*HairProc)(SkCanvas*, const SkPaint&, const SkBitmap&); |
| 159 | |
| 160 | static const struct { |
| 161 | const char* fName; |
| 162 | HairProc fProc; |
| 163 | } gProcs[] = { |
| 164 | { "line", line_proc }, |
| 165 | { "poly", poly_proc }, |
| 166 | { "quad", quad_proc }, |
| 167 | { "cube", cube_proc }, |
| 168 | }; |
| 169 | |
| 170 | static int cycle_hairproc_index(int index) { |
| 171 | return (index + 1) % SK_ARRAY_COUNT(gProcs); |
| 172 | } |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 173 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 174 | class HairlineView : public SampleView { |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 175 | SkMSec fNow; |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 176 | int fProcIndex; |
| 177 | bool fDoAA; |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 178 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 179 | HairlineView() { |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 180 | fProcIndex = 0; |
| 181 | fDoAA = true; |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 182 | fNow = 0; |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 183 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 184 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 185 | protected: |
| 186 | // overrides from SkEventSink |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame^] | 187 | bool onQuery(SkEvent* evt) SK_OVERRIDE { |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 188 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 189 | SkString str; |
| 190 | str.printf("Hair-%s", gProcs[fProcIndex].fName); |
| 191 | SampleCode::TitleR(evt, str.c_str()); |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 192 | return true; |
| 193 | } |
| 194 | return this->INHERITED::onQuery(evt); |
| 195 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 196 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 197 | void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1, |
| 198 | const SkIRect& inset) { |
| 199 | canvas->drawBitmap(b0, 0, 0, NULL); |
| 200 | canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, NULL); |
| 201 | } |
| 202 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame^] | 203 | void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
reed@android.com | a3d9010 | 2009-11-30 12:48:33 +0000 | [diff] [blame] | 204 | gRand.setSeed(fNow); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 205 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 206 | SkBitmap bm, bm2; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 207 | bm.allocN32Pixels(WIDTH + MARGIN*2, HEIGHT + MARGIN*2); |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 208 | // this will erase our margin, which we want to always stay 0 |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 209 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 210 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 211 | bm2.installPixels(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT), |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 212 | bm.getAddr32(MARGIN, MARGIN), bm.rowBytes()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 213 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 214 | SkCanvas c2(bm2); |
| 215 | SkPaint paint; |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 216 | paint.setAntiAlias(fDoAA); |
| 217 | paint.setStyle(SkPaint::kStroke_Style); |
| 218 | |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 219 | bm2.eraseColor(SK_ColorTRANSPARENT); |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 220 | gProcs[fProcIndex].fProc(&c2, paint, bm); |
| 221 | canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), NULL); |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 222 | } |
| 223 | |
reed | d9adfe6 | 2015-02-01 19:01:04 -0800 | [diff] [blame^] | 224 | bool onAnimatePulse(SkMSec curr, SkMSec prev) SK_OVERRIDE { |
| 225 | if (fDoAA) { |
| 226 | fProcIndex = cycle_hairproc_index(fProcIndex); |
| 227 | // todo: signal that we want to rebuild our TITLE |
| 228 | } |
| 229 | fDoAA = !fDoAA; |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE { |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 234 | fDoAA = !fDoAA; |
| 235 | this->inval(NULL); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 236 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 237 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 238 | |
reed@android.com | 2893728 | 2009-08-28 15:34:46 +0000 | [diff] [blame] | 239 | |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 240 | private: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 241 | typedef SampleView INHERITED; |
reed@android.com | 8898363 | 2009-03-23 16:05:19 +0000 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | ////////////////////////////////////////////////////////////////////////////// |
| 245 | |
| 246 | static SkView* MyFactory() { return new HairlineView; } |
| 247 | static SkViewRegister reg(MyFactory); |