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