blob: 8e2d620584a94e36e2434ba2b97bbec6ce609a11 [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.com44177402009-11-23 21:07:51 +0000150 SkMSec fNow;
reed@android.com3a0cd7f2009-11-17 19:39:51 +0000151 int fCounter;
reed@android.com77f0ef72009-11-17 18:47:52 +0000152 int fProcIndex;
reed@android.com70149062009-11-16 20:39:43 +0000153 SkRect fClip;
154 SkRandom fRand;
reed@android.com77f0ef72009-11-17 18:47:52 +0000155 SkPoint fPts[4];
reed@android.com70149062009-11-16 20:39:43 +0000156
157 void randPts() {
reed@android.com77f0ef72009-11-17 18:47:52 +0000158 for (int i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
159 fPts[i].set(fRand.nextUScalar1() * 640,
160 fRand.nextUScalar1() * 480);
161 }
reed@android.com3a0cd7f2009-11-17 19:39:51 +0000162 fCounter += 1;
reed@android.com70149062009-11-16 20:39:43 +0000163 }
164
165public:
166 LineClipperView() {
reed@android.come28ff552009-11-19 20:46:39 +0000167 fProcIndex = 0;
reed@android.com3a0cd7f2009-11-17 19:39:51 +0000168 fCounter = 0;
169
reed@android.com70149062009-11-16 20:39:43 +0000170 int x = (640 - W)/2;
171 int y = (480 - H)/2;
172 fClip.set(x, y, x + W, y + H);
173 this->randPts();
174 }
175
176protected:
177 // overrides from SkEventSink
178 virtual bool onQuery(SkEvent* evt) {
179 if (SampleCode::TitleQ(*evt)) {
180 SampleCode::TitleR(evt, "LineClipper");
181 return true;
182 }
183 return this->INHERITED::onQuery(evt);
184 }
185
186 void drawBG(SkCanvas* canvas) {
187 canvas->drawColor(SK_ColorWHITE);
188 }
189
190 static void drawVLine(SkCanvas* canvas, SkScalar x, const SkPaint& paint) {
191 canvas->drawLine(x, -999, x, 999, paint);
192 }
193
194 static void drawHLine(SkCanvas* canvas, SkScalar y, const SkPaint& paint) {
195 canvas->drawLine(-999, y, 999, y, paint);
196 }
197
reed@android.com70149062009-11-16 20:39:43 +0000198 virtual void onDraw(SkCanvas* canvas) {
199 this->drawBG(canvas);
reed@android.com44177402009-11-23 21:07:51 +0000200
201 SkMSec now = SampleCode::GetAnimTime();
202 if (fNow != now) {
203 fNow = now;
204 this->randPts();
205 this->inval(NULL);
206 }
207
reed@android.come28ff552009-11-19 20:46:39 +0000208 // fProcIndex = test0(fPts, &fClip);
reed@android.com70149062009-11-16 20:39:43 +0000209
reed@android.com77f0ef72009-11-17 18:47:52 +0000210 SkPaint paint, paint1;
reed@android.com70149062009-11-16 20:39:43 +0000211
212 drawVLine(canvas, fClip.fLeft + SK_ScalarHalf, paint);
213 drawVLine(canvas, fClip.fRight - SK_ScalarHalf, paint);
214 drawHLine(canvas, fClip.fTop + SK_ScalarHalf, paint);
215 drawHLine(canvas, fClip.fBottom - SK_ScalarHalf, paint);
216
217 paint.setColor(SK_ColorLTGRAY);
218 canvas->drawRect(fClip, paint);
219
220 paint.setAntiAlias(true);
221 paint.setColor(SK_ColorBLUE);
reed@android.com77f0ef72009-11-17 18:47:52 +0000222 paint.setStyle(SkPaint::kStroke_Style);
223 // paint.setStrokeWidth(SkIntToScalar(3));
reed@android.com70149062009-11-16 20:39:43 +0000224 paint.setStrokeCap(SkPaint::kRound_Cap);
reed@android.com77f0ef72009-11-17 18:47:52 +0000225
226 paint1.setAntiAlias(true);
227 paint1.setColor(SK_ColorRED);
228 paint1.setStyle(SkPaint::kStroke_Style);
229 gProcs[fProcIndex](fPts, fClip, canvas, paint, paint1);
reed@android.com70149062009-11-16 20:39:43 +0000230 }
231
232 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com77f0ef72009-11-17 18:47:52 +0000233 // fProcIndex = (fProcIndex + 1) % SK_ARRAY_COUNT(gProcs);
234 if (x < 50 && y < 50) {
235 this->randPts();
236 }
reed@android.com70149062009-11-16 20:39:43 +0000237 this->inval(NULL);
238 return NULL;
239 }
240
241 virtual bool onClick(Click* click) {
242 return false;
243 }
244
245private:
246 typedef SkView INHERITED;
247};
248
249//////////////////////////////////////////////////////////////////////////////
250
251static SkView* MyFactory() { return new LineClipperView; }
252static SkViewRegister reg(MyFactory);
253