blob: f08146e672cb1e13e562bbd5ba651b0453aa5fa1 [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"
9#include "SkView.h"
10#include "SkCanvas.h"
reed@android.com88983632009-03-23 16:05:19 +000011#include "SkCornerPathEffect.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkImageDecoder.h"
15#include "SkKernel33MaskFilter.h"
16#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
34static void test_chromium_9005() {
35 SkBitmap bm;
36 bm.setConfig(SkBitmap::kARGB_8888_Config, 800, 600);
37 bm.allocPixels();
38
39 SkCanvas canvas(bm);
40
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000041 SkPoint pt0 = { 799.33374f, 1.2360189f };
42 SkPoint pt1 = { 808.49969f, -7.4338055f };
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
reed@android.com88983632009-03-23 16:05:19 +000044 SkPaint paint;
45 paint.setAntiAlias(true);
46 canvas.drawLine(pt0.fX, pt0.fY, pt1.fX, pt1.fY, paint);
47}
48
49static void generate_pts(SkPoint pts[], int count, int w, int h) {
50 for (int i = 0; i < count; i++) {
51 pts[i].set(gRand.nextUScalar1() * 3 * w - SkIntToScalar(w),
52 gRand.nextUScalar1() * 3 * h - SkIntToScalar(h));
53 }
54}
55
56static bool check_zeros(const SkPMColor pixels[], int count, int skip) {
57 for (int i = 0; i < count; i++) {
58 if (*pixels) {
59 return false;
60 }
61 pixels += skip;
62 }
63 return true;
64}
65
66static bool check_bitmap_margin(const SkBitmap& bm, int margin) {
67 size_t rb = bm.rowBytes();
68 for (int i = 0; i < margin; i++) {
69 if (!check_zeros(bm.getAddr32(0, i), bm.width(), 1)) {
70 return false;
71 }
72 int bottom = bm.height() - i - 1;
73 if (!check_zeros(bm.getAddr32(0, bottom), bm.width(), 1)) {
74 return false;
75 }
76 // left column
reed@google.com7fa2a652014-01-27 13:42:58 +000077 if (!check_zeros(bm.getAddr32(i, 0), bm.height(), SkToInt(rb >> 2))) {
reed@android.com88983632009-03-23 16:05:19 +000078 return false;
79 }
80 int right = bm.width() - margin + i;
reed@google.com7fa2a652014-01-27 13:42:58 +000081 if (!check_zeros(bm.getAddr32(right, 0), bm.height(),
82 SkToInt(rb >> 2))) {
reed@android.com88983632009-03-23 16:05:19 +000083 return false;
84 }
85 }
86 return true;
87}
88
reed@android.com28937282009-08-28 15:34:46 +000089#define WIDTH 620
90#define HEIGHT 460
91#define MARGIN 10
92
93static void line_proc(SkCanvas* canvas, const SkPaint& paint,
94 const SkBitmap& bm) {
95 const int N = 2;
96 SkPoint pts[N];
97 for (int i = 0; i < 400; i++) {
98 generate_pts(pts, N, WIDTH, HEIGHT);
99
100 canvas->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint);
101 if (!check_bitmap_margin(bm, MARGIN)) {
102 SkDebugf("---- hairline failure (%g %g) (%g %g)\n",
103 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY);
104 break;
105 }
106 }
107}
108
109static void poly_proc(SkCanvas* canvas, const SkPaint& paint,
110 const SkBitmap& bm) {
111 const int N = 8;
112 SkPoint pts[N];
113 for (int i = 0; i < 50; i++) {
114 generate_pts(pts, N, WIDTH, HEIGHT);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed@android.com28937282009-08-28 15:34:46 +0000116 SkPath path;
117 path.moveTo(pts[0]);
118 for (int j = 1; j < N; j++) {
119 path.lineTo(pts[j]);
120 }
121 canvas->drawPath(path, paint);
122 }
123}
124
125static SkPoint ave(const SkPoint& a, const SkPoint& b) {
126 SkPoint c = a + b;
127 c.fX = SkScalarHalf(c.fX);
128 c.fY = SkScalarHalf(c.fY);
129 return c;
130}
131
132static void quad_proc(SkCanvas* canvas, const SkPaint& paint,
133 const SkBitmap& bm) {
134 const int N = 30;
135 SkPoint pts[N];
136 for (int i = 0; i < 10; i++) {
137 generate_pts(pts, N, WIDTH, HEIGHT);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
reed@android.com28937282009-08-28 15:34:46 +0000139 SkPath path;
140 path.moveTo(pts[0]);
141 for (int j = 1; j < N - 2; j++) {
142 path.quadTo(pts[j], ave(pts[j], pts[j+1]));
143 }
144 path.quadTo(pts[N - 2], pts[N - 1]);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
reed@android.com28937282009-08-28 15:34:46 +0000146 canvas->drawPath(path, paint);
147 }
148}
149
150static void add_cubic(SkPath* path, const SkPoint& mid, const SkPoint& end) {
151 SkPoint start;
152 path->getLastPt(&start);
153 path->cubicTo(ave(start, mid), ave(mid, end), end);
154}
155
156static void cube_proc(SkCanvas* canvas, const SkPaint& paint,
157 const SkBitmap& bm) {
158 const int N = 30;
159 SkPoint pts[N];
160 for (int i = 0; i < 10; i++) {
161 generate_pts(pts, N, WIDTH, HEIGHT);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000162
reed@android.com28937282009-08-28 15:34:46 +0000163 SkPath path;
164 path.moveTo(pts[0]);
165 for (int j = 1; j < N - 2; j++) {
166 add_cubic(&path, pts[j], ave(pts[j], pts[j+1]));
167 }
168 add_cubic(&path, pts[N - 2], pts[N - 1]);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000169
reed@android.com28937282009-08-28 15:34:46 +0000170 canvas->drawPath(path, paint);
171 }
172}
173
174typedef void (*HairProc)(SkCanvas*, const SkPaint&, const SkBitmap&);
175
176static const struct {
177 const char* fName;
178 HairProc fProc;
179} gProcs[] = {
180 { "line", line_proc },
181 { "poly", poly_proc },
182 { "quad", quad_proc },
183 { "cube", cube_proc },
184};
185
186static int cycle_hairproc_index(int index) {
187 return (index + 1) % SK_ARRAY_COUNT(gProcs);
188}
reed@android.com88983632009-03-23 16:05:19 +0000189
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000190class HairlineView : public SampleView {
reed@android.coma3d90102009-11-30 12:48:33 +0000191 SkMSec fNow;
reed@android.com28937282009-08-28 15:34:46 +0000192 int fProcIndex;
193 bool fDoAA;
reed@android.com88983632009-03-23 16:05:19 +0000194public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195 HairlineView() {
senorblanco@chromium.org50108cd2011-05-24 20:25:32 +0000196 fCounter = 0;
reed@android.com28937282009-08-28 15:34:46 +0000197 fProcIndex = 0;
198 fDoAA = true;
reed@android.coma3d90102009-11-30 12:48:33 +0000199 fNow = 0;
reed@android.com28937282009-08-28 15:34:46 +0000200 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000201
reed@android.com88983632009-03-23 16:05:19 +0000202protected:
203 // overrides from SkEventSink
204 virtual bool onQuery(SkEvent* evt) {
205 if (SampleCode::TitleQ(*evt)) {
reed@android.com28937282009-08-28 15:34:46 +0000206 SkString str;
207 str.printf("Hair-%s", gProcs[fProcIndex].fName);
208 SampleCode::TitleR(evt, str.c_str());
reed@android.com88983632009-03-23 16:05:19 +0000209 return true;
210 }
211 return this->INHERITED::onQuery(evt);
212 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000213
reed@android.com88983632009-03-23 16:05:19 +0000214 void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1,
215 const SkIRect& inset) {
216 canvas->drawBitmap(b0, 0, 0, NULL);
217 canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, NULL);
218 }
219
reed@android.com28937282009-08-28 15:34:46 +0000220 int fCounter;
221
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000222 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.coma3d90102009-11-30 12:48:33 +0000223 gRand.setSeed(fNow);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000224
caryclark@google.come3510502012-06-06 12:13:40 +0000225 if (false) { // avoid bit rot, suppress warning
reed@android.com88983632009-03-23 16:05:19 +0000226 test_chromium_9005();
227 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000228
reed@android.com88983632009-03-23 16:05:19 +0000229 SkBitmap bm, bm2;
230 bm.setConfig(SkBitmap::kARGB_8888_Config,
231 WIDTH + MARGIN*2,
232 HEIGHT + MARGIN*2);
233 bm.allocPixels();
234 // this will erase our margin, which we want to always stay 0
junov@google.comdbfac8a2012-12-06 21:47:40 +0000235 bm.eraseColor(SK_ColorTRANSPARENT);
reed@android.com88983632009-03-23 16:05:19 +0000236
237 bm2.setConfig(SkBitmap::kARGB_8888_Config, WIDTH, HEIGHT,
238 bm.rowBytes());
239 bm2.setPixels(bm.getAddr32(MARGIN, MARGIN));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000240
reed@android.com88983632009-03-23 16:05:19 +0000241 SkCanvas c2(bm2);
242 SkPaint paint;
reed@android.com28937282009-08-28 15:34:46 +0000243 paint.setAntiAlias(fDoAA);
244 paint.setStyle(SkPaint::kStroke_Style);
245
junov@google.comdbfac8a2012-12-06 21:47:40 +0000246 bm2.eraseColor(SK_ColorTRANSPARENT);
reed@android.com28937282009-08-28 15:34:46 +0000247 gProcs[fProcIndex].fProc(&c2, paint, bm);
248 canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), NULL);
reed@android.coma3d90102009-11-30 12:48:33 +0000249
250 SkMSec now = SampleCode::GetAnimTime();
251 if (fNow != now) {
252 fNow = now;
253 fCounter += 1;
254 fDoAA = !fDoAA;
255 if (fCounter > 50) {
256 fProcIndex = cycle_hairproc_index(fProcIndex);
257 // todo: signal that we want to rebuild our TITLE
258 fCounter = 0;
259 }
260 this->inval(NULL);
reed@android.com28937282009-08-28 15:34:46 +0000261 }
reed@android.com88983632009-03-23 16:05:19 +0000262 }
263
reed@google.com4d5c26d2013-01-08 16:17:50 +0000264 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
265 unsigned modi) {
reed@android.com28937282009-08-28 15:34:46 +0000266 fDoAA = !fDoAA;
267 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000268 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com28937282009-08-28 15:34:46 +0000269 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000270
reed@android.com28937282009-08-28 15:34:46 +0000271
reed@android.com88983632009-03-23 16:05:19 +0000272private:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000273 typedef SampleView INHERITED;
reed@android.com88983632009-03-23 16:05:19 +0000274};
275
276//////////////////////////////////////////////////////////////////////////////
277
278static SkView* MyFactory() { return new HairlineView; }
279static SkViewRegister reg(MyFactory);