blob: e606d87731397ce0e90c6448a0fe6f0e12f0b7dd [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)) {
reed@google.com4b0f0b22011-03-18 14:47:36 +000091 SkPoint pts[4];
reed@android.com77f0ef72009-11-17 18:47:52 +000092 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
mike@reedtribe.org90bb4972011-06-18 01:35:18 +0000149class LineClipperView : public SampleView {
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() {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000158 for (size_t i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
reed@android.com77f0ef72009-11-17 18:47:52 +0000159 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;
senorblanco@chromium.org50108cd2011-05-24 20:25:32 +0000169 fNow = 0;
reed@android.com3a0cd7f2009-11-17 19:39:51 +0000170
reed@android.com70149062009-11-16 20:39:43 +0000171 int x = (640 - W)/2;
172 int y = (480 - H)/2;
reed@google.com4b0f0b22011-03-18 14:47:36 +0000173 fClip.set(SkIntToScalar(x), SkIntToScalar(y),
174 SkIntToScalar(x + W), SkIntToScalar(y + H));
reed@android.com70149062009-11-16 20:39:43 +0000175 this->randPts();
176 }
177
178protected:
179 // overrides from SkEventSink
180 virtual bool onQuery(SkEvent* evt) {
181 if (SampleCode::TitleQ(*evt)) {
182 SampleCode::TitleR(evt, "LineClipper");
183 return true;
184 }
185 return this->INHERITED::onQuery(evt);
186 }
187
reed@android.com70149062009-11-16 20:39:43 +0000188 static void drawVLine(SkCanvas* canvas, SkScalar x, const SkPaint& paint) {
189 canvas->drawLine(x, -999, x, 999, paint);
190 }
191
192 static void drawHLine(SkCanvas* canvas, SkScalar y, const SkPaint& paint) {
193 canvas->drawLine(-999, y, 999, y, paint);
194 }
195
mike@reedtribe.org90bb4972011-06-18 01:35:18 +0000196 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com44177402009-11-23 21:07:51 +0000197 SkMSec now = SampleCode::GetAnimTime();
198 if (fNow != now) {
199 fNow = now;
200 this->randPts();
201 this->inval(NULL);
202 }
203
reed@android.come28ff552009-11-19 20:46:39 +0000204 // fProcIndex = test0(fPts, &fClip);
reed@android.com70149062009-11-16 20:39:43 +0000205
reed@android.com77f0ef72009-11-17 18:47:52 +0000206 SkPaint paint, paint1;
reed@android.com70149062009-11-16 20:39:43 +0000207
208 drawVLine(canvas, fClip.fLeft + SK_ScalarHalf, paint);
209 drawVLine(canvas, fClip.fRight - SK_ScalarHalf, paint);
210 drawHLine(canvas, fClip.fTop + SK_ScalarHalf, paint);
211 drawHLine(canvas, fClip.fBottom - SK_ScalarHalf, paint);
212
213 paint.setColor(SK_ColorLTGRAY);
214 canvas->drawRect(fClip, paint);
215
216 paint.setAntiAlias(true);
217 paint.setColor(SK_ColorBLUE);
reed@android.com77f0ef72009-11-17 18:47:52 +0000218 paint.setStyle(SkPaint::kStroke_Style);
219 // paint.setStrokeWidth(SkIntToScalar(3));
reed@android.com70149062009-11-16 20:39:43 +0000220 paint.setStrokeCap(SkPaint::kRound_Cap);
reed@android.com77f0ef72009-11-17 18:47:52 +0000221
222 paint1.setAntiAlias(true);
223 paint1.setColor(SK_ColorRED);
224 paint1.setStyle(SkPaint::kStroke_Style);
225 gProcs[fProcIndex](fPts, fClip, canvas, paint, paint1);
mike@reedtribe.org90bb4972011-06-18 01:35:18 +0000226 this->inval(NULL);
reed@android.com70149062009-11-16 20:39:43 +0000227 }
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:
mike@reedtribe.org90bb4972011-06-18 01:35:18 +0000243 typedef SampleView INHERITED;
reed@android.com70149062009-11-16 20:39:43 +0000244};
245
246//////////////////////////////////////////////////////////////////////////////
247
248static SkView* MyFactory() { return new LineClipperView; }
249static SkViewRegister reg(MyFactory);
250