blob: a43ac44471854f5a51803ab5b2bdd2cad9f25176 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.combf0001d2014-01-13 14:53:55 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
reed@android.com88983632009-03-23 16:05:19 +000025
26static SkRandom gRand;
27
reed@android.com88983632009-03-23 16:05:19 +000028static void generate_pts(SkPoint pts[], int count, int w, int h) {
29 for (int i = 0; i < count; i++) {
30 pts[i].set(gRand.nextUScalar1() * 3 * w - SkIntToScalar(w),
31 gRand.nextUScalar1() * 3 * h - SkIntToScalar(h));
32 }
33}
34
35static bool check_zeros(const SkPMColor pixels[], int count, int skip) {
36 for (int i = 0; i < count; i++) {
37 if (*pixels) {
38 return false;
39 }
40 pixels += skip;
41 }
42 return true;
43}
44
45static bool check_bitmap_margin(const SkBitmap& bm, int margin) {
46 size_t rb = bm.rowBytes();
47 for (int i = 0; i < margin; i++) {
48 if (!check_zeros(bm.getAddr32(0, i), bm.width(), 1)) {
49 return false;
50 }
51 int bottom = bm.height() - i - 1;
52 if (!check_zeros(bm.getAddr32(0, bottom), bm.width(), 1)) {
53 return false;
54 }
55 // left column
reed@google.com7fa2a652014-01-27 13:42:58 +000056 if (!check_zeros(bm.getAddr32(i, 0), bm.height(), SkToInt(rb >> 2))) {
reed@android.com88983632009-03-23 16:05:19 +000057 return false;
58 }
59 int right = bm.width() - margin + i;
reed@google.com7fa2a652014-01-27 13:42:58 +000060 if (!check_zeros(bm.getAddr32(right, 0), bm.height(),
61 SkToInt(rb >> 2))) {
reed@android.com88983632009-03-23 16:05:19 +000062 return false;
63 }
64 }
65 return true;
66}
67
reed@android.com28937282009-08-28 15:34:46 +000068#define WIDTH 620
69#define HEIGHT 460
70#define MARGIN 10
71
72static void line_proc(SkCanvas* canvas, const SkPaint& paint,
73 const SkBitmap& bm) {
74 const int N = 2;
75 SkPoint pts[N];
76 for (int i = 0; i < 400; i++) {
77 generate_pts(pts, N, WIDTH, HEIGHT);
78
Hal Canary23e474c2017-05-15 13:35:35 -040079 canvas->drawLine(pts[0], pts[1], paint);
reed@android.com28937282009-08-28 15:34:46 +000080 if (!check_bitmap_margin(bm, MARGIN)) {
81 SkDebugf("---- hairline failure (%g %g) (%g %g)\n",
82 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY);
83 break;
84 }
85 }
86}
87
88static void poly_proc(SkCanvas* canvas, const SkPaint& paint,
89 const SkBitmap& bm) {
90 const int N = 8;
91 SkPoint pts[N];
92 for (int i = 0; i < 50; i++) {
93 generate_pts(pts, N, WIDTH, HEIGHT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@android.com28937282009-08-28 15:34:46 +000095 SkPath path;
96 path.moveTo(pts[0]);
97 for (int j = 1; j < N; j++) {
98 path.lineTo(pts[j]);
99 }
100 canvas->drawPath(path, paint);
101 }
102}
103
104static SkPoint ave(const SkPoint& a, const SkPoint& b) {
105 SkPoint c = a + b;
106 c.fX = SkScalarHalf(c.fX);
107 c.fY = SkScalarHalf(c.fY);
108 return c;
109}
110
111static void quad_proc(SkCanvas* canvas, const SkPaint& paint,
112 const SkBitmap& bm) {
113 const int N = 30;
114 SkPoint pts[N];
115 for (int i = 0; i < 10; i++) {
116 generate_pts(pts, N, WIDTH, HEIGHT);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117
reed@android.com28937282009-08-28 15:34:46 +0000118 SkPath path;
119 path.moveTo(pts[0]);
120 for (int j = 1; j < N - 2; j++) {
121 path.quadTo(pts[j], ave(pts[j], pts[j+1]));
122 }
123 path.quadTo(pts[N - 2], pts[N - 1]);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com28937282009-08-28 15:34:46 +0000125 canvas->drawPath(path, paint);
126 }
127}
128
129static void add_cubic(SkPath* path, const SkPoint& mid, const SkPoint& end) {
130 SkPoint start;
131 path->getLastPt(&start);
132 path->cubicTo(ave(start, mid), ave(mid, end), end);
133}
134
135static void cube_proc(SkCanvas* canvas, const SkPaint& paint,
136 const SkBitmap& bm) {
137 const int N = 30;
138 SkPoint pts[N];
139 for (int i = 0; i < 10; i++) {
140 generate_pts(pts, N, WIDTH, HEIGHT);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@android.com28937282009-08-28 15:34:46 +0000142 SkPath path;
143 path.moveTo(pts[0]);
144 for (int j = 1; j < N - 2; j++) {
145 add_cubic(&path, pts[j], ave(pts[j], pts[j+1]));
146 }
147 add_cubic(&path, pts[N - 2], pts[N - 1]);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed@android.com28937282009-08-28 15:34:46 +0000149 canvas->drawPath(path, paint);
150 }
151}
152
153typedef void (*HairProc)(SkCanvas*, const SkPaint&, const SkBitmap&);
154
155static const struct {
156 const char* fName;
157 HairProc fProc;
158} gProcs[] = {
159 { "line", line_proc },
160 { "poly", poly_proc },
161 { "quad", quad_proc },
162 { "cube", cube_proc },
163};
164
165static int cycle_hairproc_index(int index) {
166 return (index + 1) % SK_ARRAY_COUNT(gProcs);
167}
reed@android.com88983632009-03-23 16:05:19 +0000168
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400169class HairlineView : public Sample {
reed@android.coma3d90102009-11-30 12:48:33 +0000170 SkMSec fNow;
reed@android.com28937282009-08-28 15:34:46 +0000171 int fProcIndex;
172 bool fDoAA;
reed@android.com88983632009-03-23 16:05:19 +0000173public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000174 HairlineView() {
reed@android.com28937282009-08-28 15:34:46 +0000175 fProcIndex = 0;
176 fDoAA = true;
reed@android.coma3d90102009-11-30 12:48:33 +0000177 fNow = 0;
reed@android.com28937282009-08-28 15:34:46 +0000178 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
reed@android.com88983632009-03-23 16:05:19 +0000180protected:
Hal Canary8a027312019-07-03 10:55:44 -0400181 SkString name() override { return SkStringPrintf("Hair-%s", gProcs[fProcIndex].fName); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182
reed@android.com88983632009-03-23 16:05:19 +0000183 void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1,
184 const SkIRect& inset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700185 canvas->drawBitmap(b0, 0, 0, nullptr);
186 canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, nullptr);
reed@android.com88983632009-03-23 16:05:19 +0000187 }
188
mtklein36352bf2015-03-25 18:17:31 -0700189 void onDrawContent(SkCanvas* canvas) override {
reed@android.coma3d90102009-11-30 12:48:33 +0000190 gRand.setSeed(fNow);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191
reed@android.com88983632009-03-23 16:05:19 +0000192 SkBitmap bm, bm2;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000193 bm.allocN32Pixels(WIDTH + MARGIN*2, HEIGHT + MARGIN*2);
reed@android.com88983632009-03-23 16:05:19 +0000194 // this will erase our margin, which we want to always stay 0
junov@google.comdbfac8a2012-12-06 21:47:40 +0000195 bm.eraseColor(SK_ColorTRANSPARENT);
reed@android.com88983632009-03-23 16:05:19 +0000196
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000197 bm2.installPixels(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT),
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000198 bm.getAddr32(MARGIN, MARGIN), bm.rowBytes());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199
reed@android.com88983632009-03-23 16:05:19 +0000200 SkCanvas c2(bm2);
201 SkPaint paint;
reed@android.com28937282009-08-28 15:34:46 +0000202 paint.setAntiAlias(fDoAA);
203 paint.setStyle(SkPaint::kStroke_Style);
204
junov@google.comdbfac8a2012-12-06 21:47:40 +0000205 bm2.eraseColor(SK_ColorTRANSPARENT);
reed@android.com28937282009-08-28 15:34:46 +0000206 gProcs[fProcIndex].fProc(&c2, paint, bm);
halcanary96fcdcc2015-08-27 07:41:13 -0700207 canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), nullptr);
reed@android.com88983632009-03-23 16:05:19 +0000208 }
209
Hal Canary41248072019-07-11 16:32:53 -0400210 bool onAnimate(double /*nanos*/) override {
reedd9adfe62015-02-01 19:01:04 -0800211 if (fDoAA) {
212 fProcIndex = cycle_hairproc_index(fProcIndex);
213 // todo: signal that we want to rebuild our TITLE
214 }
215 fDoAA = !fDoAA;
216 return true;
217 }
218
Hal Canaryb1f411a2019-08-29 10:39:22 -0400219 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override {
reed@android.com28937282009-08-28 15:34:46 +0000220 fDoAA = !fDoAA;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000221 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com28937282009-08-28 15:34:46 +0000222 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000223
reed@android.com28937282009-08-28 15:34:46 +0000224
reed@android.com88983632009-03-23 16:05:19 +0000225private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400226 typedef Sample INHERITED;
reed@android.com88983632009-03-23 16:05:19 +0000227};
228
229//////////////////////////////////////////////////////////////////////////////
230
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400231DEF_SAMPLE( return new HairlineView(); )