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