blob: b90e99e719162e774811763323cccdf27f594b60 [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
reed@android.com88983632009-03-23 16:05:19 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
Mike Reed75ae4212018-01-23 11:24:08 -050010#include "SkBitmap.h"
reed@android.com88983632009-03-23 16:05:19 +000011#include "SkCanvas.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040012#include "SkColorFilter.h"
13#include "SkColorPriv.h"
reed@android.com88983632009-03-23 16:05:19 +000014#include "SkCornerPathEffect.h"
15#include "SkGradientShader.h"
16#include "SkGraphics.h"
reed@android.com88983632009-03-23 16:05:19 +000017#include "SkPath.h"
18#include "SkRandom.h"
19#include "SkRegion.h"
20#include "SkShader.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040021#include "SkStream.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040022#include "SkTime.h"
23#include "SkTo.h"
24#include "SkTypeface.h"
25#include "SkUtils.h"
26#include "SkView.h"
reed@android.com88983632009-03-23 16:05:19 +000027
28static SkRandom gRand;
29
reed@android.com88983632009-03-23 16:05:19 +000030static 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
37static 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
47static 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.com7fa2a652014-01-27 13:42:58 +000058 if (!check_zeros(bm.getAddr32(i, 0), bm.height(), SkToInt(rb >> 2))) {
reed@android.com88983632009-03-23 16:05:19 +000059 return false;
60 }
61 int right = bm.width() - margin + i;
reed@google.com7fa2a652014-01-27 13:42:58 +000062 if (!check_zeros(bm.getAddr32(right, 0), bm.height(),
63 SkToInt(rb >> 2))) {
reed@android.com88983632009-03-23 16:05:19 +000064 return false;
65 }
66 }
67 return true;
68}
69
reed@android.com28937282009-08-28 15:34:46 +000070#define WIDTH 620
71#define HEIGHT 460
72#define MARGIN 10
73
74static 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 Canary23e474c2017-05-15 13:35:35 -040081 canvas->drawLine(pts[0], pts[1], paint);
reed@android.com28937282009-08-28 15:34:46 +000082 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
90static 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.comae933ce2012-08-23 18:19:56 +000096
reed@android.com28937282009-08-28 15:34:46 +000097 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
106static 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
113static 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.comae933ce2012-08-23 18:19:56 +0000119
reed@android.com28937282009-08-28 15:34:46 +0000120 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.comae933ce2012-08-23 18:19:56 +0000126
reed@android.com28937282009-08-28 15:34:46 +0000127 canvas->drawPath(path, paint);
128 }
129}
130
131static 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
137static 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.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com28937282009-08-28 15:34:46 +0000144 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.comae933ce2012-08-23 18:19:56 +0000150
reed@android.com28937282009-08-28 15:34:46 +0000151 canvas->drawPath(path, paint);
152 }
153}
154
155typedef void (*HairProc)(SkCanvas*, const SkPaint&, const SkBitmap&);
156
157static 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
167static int cycle_hairproc_index(int index) {
168 return (index + 1) % SK_ARRAY_COUNT(gProcs);
169}
reed@android.com88983632009-03-23 16:05:19 +0000170
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000171class HairlineView : public SampleView {
reed@android.coma3d90102009-11-30 12:48:33 +0000172 SkMSec fNow;
reed@android.com28937282009-08-28 15:34:46 +0000173 int fProcIndex;
174 bool fDoAA;
reed@android.com88983632009-03-23 16:05:19 +0000175public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000176 HairlineView() {
reed@android.com28937282009-08-28 15:34:46 +0000177 fProcIndex = 0;
178 fDoAA = true;
reed@android.coma3d90102009-11-30 12:48:33 +0000179 fNow = 0;
reed@android.com28937282009-08-28 15:34:46 +0000180 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
reed@android.com88983632009-03-23 16:05:19 +0000182protected:
183 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700184 bool onQuery(SkEvent* evt) override {
reed@android.com88983632009-03-23 16:05:19 +0000185 if (SampleCode::TitleQ(*evt)) {
reed@android.com28937282009-08-28 15:34:46 +0000186 SkString str;
187 str.printf("Hair-%s", gProcs[fProcIndex].fName);
188 SampleCode::TitleR(evt, str.c_str());
reed@android.com88983632009-03-23 16:05:19 +0000189 return true;
190 }
191 return this->INHERITED::onQuery(evt);
192 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000193
reed@android.com88983632009-03-23 16:05:19 +0000194 void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1,
195 const SkIRect& inset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700196 canvas->drawBitmap(b0, 0, 0, nullptr);
197 canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, nullptr);
reed@android.com88983632009-03-23 16:05:19 +0000198 }
199
mtklein36352bf2015-03-25 18:17:31 -0700200 void onDrawContent(SkCanvas* canvas) override {
reed@android.coma3d90102009-11-30 12:48:33 +0000201 gRand.setSeed(fNow);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000202
reed@android.com88983632009-03-23 16:05:19 +0000203 SkBitmap bm, bm2;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000204 bm.allocN32Pixels(WIDTH + MARGIN*2, HEIGHT + MARGIN*2);
reed@android.com88983632009-03-23 16:05:19 +0000205 // this will erase our margin, which we want to always stay 0
junov@google.comdbfac8a2012-12-06 21:47:40 +0000206 bm.eraseColor(SK_ColorTRANSPARENT);
reed@android.com88983632009-03-23 16:05:19 +0000207
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000208 bm2.installPixels(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT),
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000209 bm.getAddr32(MARGIN, MARGIN), bm.rowBytes());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000210
reed@android.com88983632009-03-23 16:05:19 +0000211 SkCanvas c2(bm2);
212 SkPaint paint;
reed@android.com28937282009-08-28 15:34:46 +0000213 paint.setAntiAlias(fDoAA);
214 paint.setStyle(SkPaint::kStroke_Style);
215
junov@google.comdbfac8a2012-12-06 21:47:40 +0000216 bm2.eraseColor(SK_ColorTRANSPARENT);
reed@android.com28937282009-08-28 15:34:46 +0000217 gProcs[fProcIndex].fProc(&c2, paint, bm);
halcanary96fcdcc2015-08-27 07:41:13 -0700218 canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), nullptr);
reed@android.com88983632009-03-23 16:05:19 +0000219 }
220
mtklein36352bf2015-03-25 18:17:31 -0700221 bool onAnimate(const SkAnimTimer&) override {
reedd9adfe62015-02-01 19:01:04 -0800222 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
mtklein36352bf2015-03-25 18:17:31 -0700230 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com28937282009-08-28 15:34:46 +0000231 fDoAA = !fDoAA;
reed@google.com4d5c26d2013-01-08 16:17:50 +0000232 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com28937282009-08-28 15:34:46 +0000233 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000234
reed@android.com28937282009-08-28 15:34:46 +0000235
reed@android.com88983632009-03-23 16:05:19 +0000236private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000237 typedef SampleView INHERITED;
reed@android.com88983632009-03-23 16:05:19 +0000238};
239
240//////////////////////////////////////////////////////////////////////////////
241
242static SkView* MyFactory() { return new HairlineView; }
243static SkViewRegister reg(MyFactory);