blob: 9b6bcd590682d68d7a651e1d749889c7694d47f2 [file] [log] [blame]
reed@android.com88983632009-03-23 16:05:19 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "Sk64.h"
5#include "SkCornerPathEffect.h"
6#include "SkGradientShader.h"
7#include "SkGraphics.h"
8#include "SkImageDecoder.h"
9#include "SkKernel33MaskFilter.h"
10#include "SkPath.h"
11#include "SkRandom.h"
12#include "SkRegion.h"
13#include "SkShader.h"
14#include "SkUtils.h"
15#include "SkColorPriv.h"
16#include "SkColorFilter.h"
17#include "SkTime.h"
18#include "SkTypeface.h"
19#include "SkXfermode.h"
20
21#include "SkStream.h"
22#include "SkXMLParser.h"
23#include "SkColorPriv.h"
24#include "SkImageDecoder.h"
25
26static SkRandom gRand;
27
28static void test_chromium_9005() {
29 SkBitmap bm;
30 bm.setConfig(SkBitmap::kARGB_8888_Config, 800, 600);
31 bm.allocPixels();
32
33 SkCanvas canvas(bm);
34
35 SkPoint pt0 = { SkFloatToScalar(799.33374f), SkFloatToScalar(1.2360189f) };
36 SkPoint pt1 = { SkFloatToScalar(808.49969f), SkFloatToScalar(-7.4338055f) };
37
38 SkPaint paint;
39 paint.setAntiAlias(true);
40 canvas.drawLine(pt0.fX, pt0.fY, pt1.fX, pt1.fY, paint);
41}
42
43static void generate_pts(SkPoint pts[], int count, int w, int h) {
44 for (int i = 0; i < count; i++) {
45 pts[i].set(gRand.nextUScalar1() * 3 * w - SkIntToScalar(w),
46 gRand.nextUScalar1() * 3 * h - SkIntToScalar(h));
47 }
48}
49
50static bool check_zeros(const SkPMColor pixels[], int count, int skip) {
51 for (int i = 0; i < count; i++) {
52 if (*pixels) {
53 return false;
54 }
55 pixels += skip;
56 }
57 return true;
58}
59
60static bool check_bitmap_margin(const SkBitmap& bm, int margin) {
61 size_t rb = bm.rowBytes();
62 for (int i = 0; i < margin; i++) {
63 if (!check_zeros(bm.getAddr32(0, i), bm.width(), 1)) {
64 return false;
65 }
66 int bottom = bm.height() - i - 1;
67 if (!check_zeros(bm.getAddr32(0, bottom), bm.width(), 1)) {
68 return false;
69 }
70 // left column
71 if (!check_zeros(bm.getAddr32(i, 0), bm.height(), rb >> 2)) {
72 return false;
73 }
74 int right = bm.width() - margin + i;
75 if (!check_zeros(bm.getAddr32(right, 0), bm.height(), rb >> 2)) {
76 return false;
77 }
78 }
79 return true;
80}
81
reed@android.com28937282009-08-28 15:34:46 +000082#define WIDTH 620
83#define HEIGHT 460
84#define MARGIN 10
85
86static void line_proc(SkCanvas* canvas, const SkPaint& paint,
87 const SkBitmap& bm) {
88 const int N = 2;
89 SkPoint pts[N];
90 for (int i = 0; i < 400; i++) {
91 generate_pts(pts, N, WIDTH, HEIGHT);
92
93 canvas->drawLine(pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, paint);
94 if (!check_bitmap_margin(bm, MARGIN)) {
95 SkDebugf("---- hairline failure (%g %g) (%g %g)\n",
96 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY);
97 break;
98 }
99 }
100}
101
102static void poly_proc(SkCanvas* canvas, const SkPaint& paint,
103 const SkBitmap& bm) {
104 const int N = 8;
105 SkPoint pts[N];
106 for (int i = 0; i < 50; i++) {
107 generate_pts(pts, N, WIDTH, HEIGHT);
108
109 SkPath path;
110 path.moveTo(pts[0]);
111 for (int j = 1; j < N; j++) {
112 path.lineTo(pts[j]);
113 }
114 canvas->drawPath(path, paint);
115 }
116}
117
118static SkPoint ave(const SkPoint& a, const SkPoint& b) {
119 SkPoint c = a + b;
120 c.fX = SkScalarHalf(c.fX);
121 c.fY = SkScalarHalf(c.fY);
122 return c;
123}
124
125static void quad_proc(SkCanvas* canvas, const SkPaint& paint,
126 const SkBitmap& bm) {
127 const int N = 30;
128 SkPoint pts[N];
129 for (int i = 0; i < 10; i++) {
130 generate_pts(pts, N, WIDTH, HEIGHT);
131
132 SkPath path;
133 path.moveTo(pts[0]);
134 for (int j = 1; j < N - 2; j++) {
135 path.quadTo(pts[j], ave(pts[j], pts[j+1]));
136 }
137 path.quadTo(pts[N - 2], pts[N - 1]);
138
139 canvas->drawPath(path, paint);
140 }
141}
142
143static void add_cubic(SkPath* path, const SkPoint& mid, const SkPoint& end) {
144 SkPoint start;
145 path->getLastPt(&start);
146 path->cubicTo(ave(start, mid), ave(mid, end), end);
147}
148
149static void cube_proc(SkCanvas* canvas, const SkPaint& paint,
150 const SkBitmap& bm) {
151 const int N = 30;
152 SkPoint pts[N];
153 for (int i = 0; i < 10; i++) {
154 generate_pts(pts, N, WIDTH, HEIGHT);
155
156 SkPath path;
157 path.moveTo(pts[0]);
158 for (int j = 1; j < N - 2; j++) {
159 add_cubic(&path, pts[j], ave(pts[j], pts[j+1]));
160 }
161 add_cubic(&path, pts[N - 2], pts[N - 1]);
162
163 canvas->drawPath(path, paint);
164 }
165}
166
167typedef void (*HairProc)(SkCanvas*, const SkPaint&, const SkBitmap&);
168
169static const struct {
170 const char* fName;
171 HairProc fProc;
172} gProcs[] = {
173 { "line", line_proc },
174 { "poly", poly_proc },
175 { "quad", quad_proc },
176 { "cube", cube_proc },
177};
178
179static int cycle_hairproc_index(int index) {
180 return (index + 1) % SK_ARRAY_COUNT(gProcs);
181}
reed@android.com88983632009-03-23 16:05:19 +0000182
183class HairlineView : public SkView {
reed@android.com28937282009-08-28 15:34:46 +0000184 int fProcIndex;
185 bool fDoAA;
reed@android.com88983632009-03-23 16:05:19 +0000186public:
reed@android.com28937282009-08-28 15:34:46 +0000187 HairlineView() {
188 fProcIndex = 0;
189 fDoAA = true;
190 }
reed@android.com88983632009-03-23 16:05:19 +0000191
192protected:
193 // overrides from SkEventSink
194 virtual bool onQuery(SkEvent* evt) {
195 if (SampleCode::TitleQ(*evt)) {
reed@android.com28937282009-08-28 15:34:46 +0000196 SkString str;
197 str.printf("Hair-%s", gProcs[fProcIndex].fName);
198 SampleCode::TitleR(evt, str.c_str());
reed@android.com88983632009-03-23 16:05:19 +0000199 return true;
200 }
201 return this->INHERITED::onQuery(evt);
202 }
203
204 void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1,
205 const SkIRect& inset) {
206 canvas->drawBitmap(b0, 0, 0, NULL);
207 canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, NULL);
208 }
209
210 void drawBG(SkCanvas* canvas) {
211// canvas->drawColor(0xFFDDDDDD);
212 canvas->drawColor(SK_ColorWHITE);
213 // canvas->drawColor(SK_ColorBLACK);
214 }
215
reed@android.com28937282009-08-28 15:34:46 +0000216 int fCounter;
217
reed@android.com88983632009-03-23 16:05:19 +0000218 virtual void onDraw(SkCanvas* canvas) {
219 this->drawBG(canvas);
220
221 if (true) {
222 test_chromium_9005();
223 }
224
225 SkBitmap bm, bm2;
226 bm.setConfig(SkBitmap::kARGB_8888_Config,
227 WIDTH + MARGIN*2,
228 HEIGHT + MARGIN*2);
229 bm.allocPixels();
230 // this will erase our margin, which we want to always stay 0
231 bm.eraseColor(0);
232
233 bm2.setConfig(SkBitmap::kARGB_8888_Config, WIDTH, HEIGHT,
234 bm.rowBytes());
235 bm2.setPixels(bm.getAddr32(MARGIN, MARGIN));
236
237 SkCanvas c2(bm2);
238 SkPaint paint;
reed@android.com28937282009-08-28 15:34:46 +0000239 paint.setAntiAlias(fDoAA);
240 paint.setStyle(SkPaint::kStroke_Style);
241
242 bm2.eraseColor(0);
243 gProcs[fProcIndex].fProc(&c2, paint, bm);
244 canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), NULL);
reed@android.com88983632009-03-23 16:05:19 +0000245
reed@android.com28937282009-08-28 15:34:46 +0000246 fCounter += 1;
247 fDoAA = !fDoAA;
248 if (fCounter > 50) {
249 fProcIndex = cycle_hairproc_index(fProcIndex);
250 // todo: signal that we want to rebuild our TITLE
251 fCounter = 0;
252 }
reed@android.com88983632009-03-23 16:05:19 +0000253 this->inval(NULL);
254 }
255
reed@android.com28937282009-08-28 15:34:46 +0000256 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
257 fDoAA = !fDoAA;
258 this->inval(NULL);
259 return this->INHERITED::onFindClickHandler(x, y);
260 }
261
262
reed@android.com88983632009-03-23 16:05:19 +0000263private:
264 typedef SkView INHERITED;
265};
266
267//////////////////////////////////////////////////////////////////////////////
268
269static SkView* MyFactory() { return new HairlineView; }
270static SkViewRegister reg(MyFactory);
271