blob: 09606f6b89a27d3d918ac5db2faf5625ad617d23 [file] [log] [blame]
reed@android.com70149062009-11-16 20:39:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGradientShader.h"
5#include "SkGraphics.h"
6#include "SkImageDecoder.h"
7#include "SkPath.h"
8#include "SkRegion.h"
9#include "SkShader.h"
10#include "SkUtils.h"
11#include "SkXfermode.h"
12#include "SkColorPriv.h"
13#include "SkColorFilter.h"
14#include "SkTime.h"
15#include "SkRandom.h"
reed@android.com77f0ef72009-11-17 18:47:52 +000016
reed@android.com70149062009-11-16 20:39:43 +000017#include "SkLineClipper.h"
reed@android.com909994f2009-11-18 16:09:51 +000018#include "SkEdgeClipper.h"
reed@android.com77f0ef72009-11-17 18:47:52 +000019
reed@android.come28ff552009-11-19 20:46:39 +000020#define AUTO_ANIMATE true
21
22static int test0(SkPoint pts[], SkRect* clip) {
23 pts[0].set(200000, 140);
24 pts[1].set(-740000, 483);
25 pts[2].set(1.00000102e-06f, 9.10000017e-05f);
26 clip->set(0, 0, 640, 480);
27 return 2;
28}
29
30///////////////////////////////////////////////////////////////////////////////
31
reed@android.com77f0ef72009-11-17 18:47:52 +000032static void drawQuad(SkCanvas* canvas, const SkPoint pts[3], const SkPaint& p) {
33 SkPath path;
34 path.moveTo(pts[0]);
35 path.quadTo(pts[1], pts[2]);
36 canvas->drawPath(path, p);
37}
38
reed@android.combb135862009-11-18 13:47:40 +000039static void drawCubic(SkCanvas* canvas, const SkPoint pts[4], const SkPaint& p) {
40 SkPath path;
41 path.moveTo(pts[0]);
42 path.cubicTo(pts[1], pts[2], pts[3]);
43 canvas->drawPath(path, p);
44}
45
reed@android.com77f0ef72009-11-17 18:47:52 +000046typedef void (*clipper_proc)(const SkPoint src[], const SkRect& clip,
47 SkCanvas*, const SkPaint&, const SkPaint&);
48
49static void check_clipper(int count, const SkPoint pts[], const SkRect& clip) {
50 for (int i = 0; i < count; i++) {
51 SkASSERT(pts[i].fX >= clip.fLeft);
52 SkASSERT(pts[i].fX <= clip.fRight);
53 SkASSERT(pts[i].fY >= clip.fTop);
54 SkASSERT(pts[i].fY <= clip.fBottom);
55 }
reed@android.com3a0cd7f2009-11-17 19:39:51 +000056
57 if (count > 1) {
reed@android.combb135862009-11-18 13:47:40 +000058 sk_assert_monotonic_y(pts, count);
reed@android.com3a0cd7f2009-11-17 19:39:51 +000059 }
reed@android.com77f0ef72009-11-17 18:47:52 +000060}
61
reed@android.come28ff552009-11-19 20:46:39 +000062static void line_intersector(const SkPoint src[], const SkRect& clip,
63 SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
reed@android.com77f0ef72009-11-17 18:47:52 +000064 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, src, p1);
reed@android.come28ff552009-11-19 20:46:39 +000065
66 SkPoint dst[2];
67 if (SkLineClipper::IntersectLine(src, clip, dst)) {
68 check_clipper(2, dst, clip);
69 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, dst, p0);
70 }
71}
reed@android.com77f0ef72009-11-17 18:47:52 +000072
reed@android.come28ff552009-11-19 20:46:39 +000073static void line_clipper(const SkPoint src[], const SkRect& clip,
74 SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
75 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, src, p1);
76
reed@android.com77f0ef72009-11-17 18:47:52 +000077 SkPoint dst[SkLineClipper::kMaxPoints];
78 int count = SkLineClipper::ClipLine(src, clip, dst);
79 for (int i = 0; i < count; i++) {
80 check_clipper(2, &dst[i], clip);
81 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, &dst[i], p0);
82 }
83}
84
85static void quad_clipper(const SkPoint src[], const SkRect& clip,
reed@android.combb135862009-11-18 13:47:40 +000086 SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
reed@android.com77f0ef72009-11-17 18:47:52 +000087 drawQuad(canvas, src, p1);
reed@android.combb135862009-11-18 13:47:40 +000088
reed@android.com909994f2009-11-18 16:09:51 +000089 SkEdgeClipper clipper;
reed@android.com77f0ef72009-11-17 18:47:52 +000090 if (clipper.clipQuad(src, clip)) {
91 SkPoint pts[3];
92 SkPath::Verb verb;
93 while ((verb = clipper.next(pts)) != SkPath::kDone_Verb) {
94 switch (verb) {
95 case SkPath::kLine_Verb:
reed@android.com3a0cd7f2009-11-17 19:39:51 +000096 check_clipper(2, pts, clip);
reed@android.com77f0ef72009-11-17 18:47:52 +000097 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p0);
98 break;
99 case SkPath::kQuad_Verb:
reed@android.com3a0cd7f2009-11-17 19:39:51 +0000100 check_clipper(3, pts, clip);
reed@android.com77f0ef72009-11-17 18:47:52 +0000101 drawQuad(canvas, pts, p0);
102 break;
103 default:
104 SkASSERT(!"unexpected verb");
105 }
106 }
107 }
108}
109
reed@android.combb135862009-11-18 13:47:40 +0000110static void cubic_clipper(const SkPoint src[], const SkRect& clip,
111 SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) {
112 drawCubic(canvas, src, p1);
113
reed@android.com909994f2009-11-18 16:09:51 +0000114 SkEdgeClipper clipper;
reed@android.combb135862009-11-18 13:47:40 +0000115 if (clipper.clipCubic(src, clip)) {
116 SkPoint pts[4];
117 SkPath::Verb verb;
118 while ((verb = clipper.next(pts)) != SkPath::kDone_Verb) {
119 switch (verb) {
120 case SkPath::kLine_Verb:
121 check_clipper(2, pts, clip);
122 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p0);
123 break;
124 case SkPath::kCubic_Verb:
125 // check_clipper(4, pts, clip);
126 drawCubic(canvas, pts, p0);
127 break;
128 default:
129 SkASSERT(!"unexpected verb");
130 }
131 }
132 }
133}
134
reed@android.com77f0ef72009-11-17 18:47:52 +0000135static const clipper_proc gProcs[] = {
reed@android.come28ff552009-11-19 20:46:39 +0000136 line_intersector,
reed@android.com77f0ef72009-11-17 18:47:52 +0000137 line_clipper,
reed@android.combb135862009-11-18 13:47:40 +0000138 quad_clipper,
139 cubic_clipper
reed@android.com77f0ef72009-11-17 18:47:52 +0000140};
141
142///////////////////////////////////////////////////////////////////////////////
reed@android.com70149062009-11-16 20:39:43 +0000143
144enum {
reed@android.com77f0ef72009-11-17 18:47:52 +0000145 W = 640/3,
146 H = 480/3
reed@android.com70149062009-11-16 20:39:43 +0000147};
148
149class LineClipperView : public SkView {
reed@android.com3a0cd7f2009-11-17 19:39:51 +0000150 int fCounter;
reed@android.com77f0ef72009-11-17 18:47:52 +0000151 int fProcIndex;
reed@android.com70149062009-11-16 20:39:43 +0000152 SkRect fClip;
153 SkRandom fRand;
reed@android.com77f0ef72009-11-17 18:47:52 +0000154 SkPoint fPts[4];
reed@android.com70149062009-11-16 20:39:43 +0000155
156 void randPts() {
reed@android.com77f0ef72009-11-17 18:47:52 +0000157 for (int i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
158 fPts[i].set(fRand.nextUScalar1() * 640,
159 fRand.nextUScalar1() * 480);
160 }
reed@android.com3a0cd7f2009-11-17 19:39:51 +0000161 fCounter += 1;
reed@android.com70149062009-11-16 20:39:43 +0000162 }
163
164public:
165 LineClipperView() {
reed@android.come28ff552009-11-19 20:46:39 +0000166 fProcIndex = 0;
reed@android.com3a0cd7f2009-11-17 19:39:51 +0000167 fCounter = 0;
168
reed@android.com70149062009-11-16 20:39:43 +0000169 int x = (640 - W)/2;
170 int y = (480 - H)/2;
171 fClip.set(x, y, x + W, y + H);
172 this->randPts();
173 }
174
175protected:
176 // overrides from SkEventSink
177 virtual bool onQuery(SkEvent* evt) {
178 if (SampleCode::TitleQ(*evt)) {
179 SampleCode::TitleR(evt, "LineClipper");
180 return true;
181 }
182 return this->INHERITED::onQuery(evt);
183 }
184
185 void drawBG(SkCanvas* canvas) {
186 canvas->drawColor(SK_ColorWHITE);
187 }
188
189 static void drawVLine(SkCanvas* canvas, SkScalar x, const SkPaint& paint) {
190 canvas->drawLine(x, -999, x, 999, paint);
191 }
192
193 static void drawHLine(SkCanvas* canvas, SkScalar y, const SkPaint& paint) {
194 canvas->drawLine(-999, y, 999, y, paint);
195 }
196
reed@android.com70149062009-11-16 20:39:43 +0000197 virtual void onDraw(SkCanvas* canvas) {
198 this->drawBG(canvas);
reed@android.come28ff552009-11-19 20:46:39 +0000199
200 // fProcIndex = test0(fPts, &fClip);
reed@android.com70149062009-11-16 20:39:43 +0000201
reed@android.com77f0ef72009-11-17 18:47:52 +0000202 SkPaint paint, paint1;
reed@android.com70149062009-11-16 20:39:43 +0000203
204 drawVLine(canvas, fClip.fLeft + SK_ScalarHalf, paint);
205 drawVLine(canvas, fClip.fRight - SK_ScalarHalf, paint);
206 drawHLine(canvas, fClip.fTop + SK_ScalarHalf, paint);
207 drawHLine(canvas, fClip.fBottom - SK_ScalarHalf, paint);
208
209 paint.setColor(SK_ColorLTGRAY);
210 canvas->drawRect(fClip, paint);
211
212 paint.setAntiAlias(true);
213 paint.setColor(SK_ColorBLUE);
reed@android.com77f0ef72009-11-17 18:47:52 +0000214 paint.setStyle(SkPaint::kStroke_Style);
215 // paint.setStrokeWidth(SkIntToScalar(3));
reed@android.com70149062009-11-16 20:39:43 +0000216 paint.setStrokeCap(SkPaint::kRound_Cap);
reed@android.com77f0ef72009-11-17 18:47:52 +0000217
218 paint1.setAntiAlias(true);
219 paint1.setColor(SK_ColorRED);
220 paint1.setStyle(SkPaint::kStroke_Style);
221 gProcs[fProcIndex](fPts, fClip, canvas, paint, paint1);
reed@android.com70149062009-11-16 20:39:43 +0000222
reed@android.come28ff552009-11-19 20:46:39 +0000223 if (AUTO_ANIMATE) {
reed@android.com70149062009-11-16 20:39:43 +0000224 this->randPts();
225 this->inval(NULL);
226 }
227 }
228
229 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com77f0ef72009-11-17 18:47:52 +0000230 // fProcIndex = (fProcIndex + 1) % SK_ARRAY_COUNT(gProcs);
231 if (x < 50 && y < 50) {
232 this->randPts();
233 }
reed@android.com70149062009-11-16 20:39:43 +0000234 this->inval(NULL);
235 return NULL;
236 }
237
238 virtual bool onClick(Click* click) {
239 return false;
240 }
241
242private:
243 typedef SkView INHERITED;
244};
245
246//////////////////////////////////////////////////////////////////////////////
247
248static SkView* MyFactory() { return new LineClipperView; }
249static SkViewRegister reg(MyFactory);
250