blob: a8985c851e8d33d27008bcc074b81fa338bc7df2 [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"
reed@android.com88983632009-03-23 16:05:19 +000010#include "SkView.h"
11#include "SkCanvas.h"
reed@android.com88983632009-03-23 16:05:19 +000012#include "SkCornerPathEffect.h"
13#include "SkGradientShader.h"
14#include "SkGraphics.h"
15#include "SkImageDecoder.h"
reed@android.com88983632009-03-23 16:05:19 +000016#include "SkPath.h"
17#include "SkRandom.h"
18#include "SkRegion.h"
19#include "SkShader.h"
20#include "SkUtils.h"
21#include "SkColorPriv.h"
22#include "SkColorFilter.h"
23#include "SkTime.h"
24#include "SkTypeface.h"
25#include "SkXfermode.h"
26
27#include "SkStream.h"
28#include "SkXMLParser.h"
29#include "SkColorPriv.h"
30#include "SkImageDecoder.h"
31
32static SkRandom gRand;
33
reed@android.com88983632009-03-23 16:05:19 +000034static void generate_pts(SkPoint pts[], int count, int w, int h) {
35 for (int i = 0; i < count; i++) {
36 pts[i].set(gRand.nextUScalar1() * 3 * w - SkIntToScalar(w),
37 gRand.nextUScalar1() * 3 * h - SkIntToScalar(h));
38 }
39}
40
41static bool check_zeros(const SkPMColor pixels[], int count, int skip) {
42 for (int i = 0; i < count; i++) {
43 if (*pixels) {
44 return false;
45 }
46 pixels += skip;
47 }
48 return true;
49}
50
51static bool check_bitmap_margin(const SkBitmap& bm, int margin) {
52 size_t rb = bm.rowBytes();
53 for (int i = 0; i < margin; i++) {
54 if (!check_zeros(bm.getAddr32(0, i), bm.width(), 1)) {
55 return false;
56 }
57 int bottom = bm.height() - i - 1;
58 if (!check_zeros(bm.getAddr32(0, bottom), bm.width(), 1)) {
59 return false;
60 }
61 // left column
reed@google.com7fa2a652014-01-27 13:42:58 +000062 if (!check_zeros(bm.getAddr32(i, 0), bm.height(), SkToInt(rb >> 2))) {
reed@android.com88983632009-03-23 16:05:19 +000063 return false;
64 }
65 int right = bm.width() - margin + i;
reed@google.com7fa2a652014-01-27 13:42:58 +000066 if (!check_zeros(bm.getAddr32(right, 0), bm.height(),
67 SkToInt(rb >> 2))) {
reed@android.com88983632009-03-23 16:05:19 +000068 return false;
69 }
70 }
71 return true;
72}
73
reed@android.com28937282009-08-28 15:34:46 +000074#define WIDTH 620
75#define HEIGHT 460
76#define MARGIN 10
77
78static void line_proc(SkCanvas* canvas, const SkPaint& paint,
79 const SkBitmap& bm) {
80 const int N = 2;
81 SkPoint pts[N];
82 for (int i = 0; i < 400; i++) {
83 generate_pts(pts, N, WIDTH, HEIGHT);
84
85 canvas->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint);
86 if (!check_bitmap_margin(bm, MARGIN)) {
87 SkDebugf("---- hairline failure (%g %g) (%g %g)\n",
88 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY);
89 break;
90 }
91 }
92}
93
94static void poly_proc(SkCanvas* canvas, const SkPaint& paint,
95 const SkBitmap& bm) {
96 const int N = 8;
97 SkPoint pts[N];
98 for (int i = 0; i < 50; i++) {
99 generate_pts(pts, N, WIDTH, HEIGHT);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@android.com28937282009-08-28 15:34:46 +0000101 SkPath path;
102 path.moveTo(pts[0]);
103 for (int j = 1; j < N; j++) {
104 path.lineTo(pts[j]);
105 }
106 canvas->drawPath(path, paint);
107 }
108}
109
110static SkPoint ave(const SkPoint& a, const SkPoint& b) {
111 SkPoint c = a + b;
112 c.fX = SkScalarHalf(c.fX);
113 c.fY = SkScalarHalf(c.fY);
114 return c;
115}
116
117static void quad_proc(SkCanvas* canvas, const SkPaint& paint,
118 const SkBitmap& bm) {
119 const int N = 30;
120 SkPoint pts[N];
121 for (int i = 0; i < 10; i++) {
122 generate_pts(pts, N, WIDTH, HEIGHT);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
reed@android.com28937282009-08-28 15:34:46 +0000124 SkPath path;
125 path.moveTo(pts[0]);
126 for (int j = 1; j < N - 2; j++) {
127 path.quadTo(pts[j], ave(pts[j], pts[j+1]));
128 }
129 path.quadTo(pts[N - 2], pts[N - 1]);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com28937282009-08-28 15:34:46 +0000131 canvas->drawPath(path, paint);
132 }
133}
134
135static void add_cubic(SkPath* path, const SkPoint& mid, const SkPoint& end) {
136 SkPoint start;
137 path->getLastPt(&start);
138 path->cubicTo(ave(start, mid), ave(mid, end), end);
139}
140
141static void cube_proc(SkCanvas* canvas, const SkPaint& paint,
142 const SkBitmap& bm) {
143 const int N = 30;
144 SkPoint pts[N];
145 for (int i = 0; i < 10; i++) {
146 generate_pts(pts, N, WIDTH, HEIGHT);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@android.com28937282009-08-28 15:34:46 +0000148 SkPath path;
149 path.moveTo(pts[0]);
150 for (int j = 1; j < N - 2; j++) {
151 add_cubic(&path, pts[j], ave(pts[j], pts[j+1]));
152 }
153 add_cubic(&path, pts[N - 2], pts[N - 1]);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
reed@android.com28937282009-08-28 15:34:46 +0000155 canvas->drawPath(path, paint);
156 }
157}
158
159typedef void (*HairProc)(SkCanvas*, const SkPaint&, const SkBitmap&);
160
161static const struct {
162 const char* fName;
163 HairProc fProc;
164} gProcs[] = {
165 { "line", line_proc },
166 { "poly", poly_proc },
167 { "quad", quad_proc },
168 { "cube", cube_proc },
169};
170
171static int cycle_hairproc_index(int index) {
172 return (index + 1) % SK_ARRAY_COUNT(gProcs);
173}
reed@android.com88983632009-03-23 16:05:19 +0000174
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000175class HairlineView : public SampleView {
reed@android.coma3d90102009-11-30 12:48:33 +0000176 SkMSec fNow;
reed@android.com28937282009-08-28 15:34:46 +0000177 int fProcIndex;
178 bool fDoAA;
reed@android.com88983632009-03-23 16:05:19 +0000179public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180 HairlineView() {
reed@android.com28937282009-08-28 15:34:46 +0000181 fProcIndex = 0;
182 fDoAA = true;
reed@android.coma3d90102009-11-30 12:48:33 +0000183 fNow = 0;
reed@android.com28937282009-08-28 15:34:46 +0000184 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185
reed@android.com88983632009-03-23 16:05:19 +0000186protected:
187 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700188 bool onQuery(SkEvent* evt) override {
reed@android.com88983632009-03-23 16:05:19 +0000189 if (SampleCode::TitleQ(*evt)) {
reed@android.com28937282009-08-28 15:34:46 +0000190 SkString str;
191 str.printf("Hair-%s", gProcs[fProcIndex].fName);
192 SampleCode::TitleR(evt, str.c_str());
reed@android.com88983632009-03-23 16:05:19 +0000193 return true;
194 }
195 return this->INHERITED::onQuery(evt);
196 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000197
reed@android.com88983632009-03-23 16:05:19 +0000198 void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1,
199 const SkIRect& inset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700200 canvas->drawBitmap(b0, 0, 0, nullptr);
201 canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, nullptr);
reed@android.com88983632009-03-23 16:05:19 +0000202 }
203
mtklein36352bf2015-03-25 18:17:31 -0700204 void onDrawContent(SkCanvas* canvas) override {
reed@android.coma3d90102009-11-30 12:48:33 +0000205 gRand.setSeed(fNow);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000206
reed@android.com88983632009-03-23 16:05:19 +0000207 SkBitmap bm, bm2;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000208 bm.allocN32Pixels(WIDTH + MARGIN*2, HEIGHT + MARGIN*2);
reed@android.com88983632009-03-23 16:05:19 +0000209 // this will erase our margin, which we want to always stay 0
junov@google.comdbfac8a2012-12-06 21:47:40 +0000210 bm.eraseColor(SK_ColorTRANSPARENT);
reed@android.com88983632009-03-23 16:05:19 +0000211
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000212 bm2.installPixels(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT),
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000213 bm.getAddr32(MARGIN, MARGIN), bm.rowBytes());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000214
reed@android.com88983632009-03-23 16:05:19 +0000215 SkCanvas c2(bm2);
216 SkPaint paint;
reed@android.com28937282009-08-28 15:34:46 +0000217 paint.setAntiAlias(fDoAA);
218 paint.setStyle(SkPaint::kStroke_Style);
219
junov@google.comdbfac8a2012-12-06 21:47:40 +0000220 bm2.eraseColor(SK_ColorTRANSPARENT);
reed@android.com28937282009-08-28 15:34:46 +0000221 gProcs[fProcIndex].fProc(&c2, paint, bm);
halcanary96fcdcc2015-08-27 07:41:13 -0700222 canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), nullptr);
reed@android.com88983632009-03-23 16:05:19 +0000223 }
224
mtklein36352bf2015-03-25 18:17:31 -0700225 bool onAnimate(const SkAnimTimer&) override {
reedd9adfe62015-02-01 19:01:04 -0800226 if (fDoAA) {
227 fProcIndex = cycle_hairproc_index(fProcIndex);
228 // todo: signal that we want to rebuild our TITLE
229 }
230 fDoAA = !fDoAA;
231 return true;
232 }
233
mtklein36352bf2015-03-25 18:17:31 -0700234 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com28937282009-08-28 15:34:46 +0000235 fDoAA = !fDoAA;
halcanary96fcdcc2015-08-27 07:41:13 -0700236 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000237 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com28937282009-08-28 15:34:46 +0000238 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000239
reed@android.com28937282009-08-28 15:34:46 +0000240
reed@android.com88983632009-03-23 16:05:19 +0000241private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000242 typedef SampleView INHERITED;
reed@android.com88983632009-03-23 16:05:19 +0000243};
244
245//////////////////////////////////////////////////////////////////////////////
246
247static SkView* MyFactory() { return new HairlineView; }
248static SkViewRegister reg(MyFactory);