reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 1 | #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.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 16 | |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 17 | #include "SkLineClipper.h" |
reed@android.com | 909994f | 2009-11-18 16:09:51 +0000 | [diff] [blame] | 18 | #include "SkEdgeClipper.h" |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 19 | |
reed@android.com | e28ff55 | 2009-11-19 20:46:39 +0000 | [diff] [blame] | 20 | #define AUTO_ANIMATE true |
| 21 | |
| 22 | static 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.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 32 | static 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.com | bb13586 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 39 | static 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.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 46 | typedef void (*clipper_proc)(const SkPoint src[], const SkRect& clip, |
| 47 | SkCanvas*, const SkPaint&, const SkPaint&); |
| 48 | |
| 49 | static 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.com | 3a0cd7f | 2009-11-17 19:39:51 +0000 | [diff] [blame] | 56 | |
| 57 | if (count > 1) { |
reed@android.com | bb13586 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 58 | sk_assert_monotonic_y(pts, count); |
reed@android.com | 3a0cd7f | 2009-11-17 19:39:51 +0000 | [diff] [blame] | 59 | } |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 60 | } |
| 61 | |
reed@android.com | e28ff55 | 2009-11-19 20:46:39 +0000 | [diff] [blame] | 62 | static void line_intersector(const SkPoint src[], const SkRect& clip, |
| 63 | SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) { |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 64 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, src, p1); |
reed@android.com | e28ff55 | 2009-11-19 20:46:39 +0000 | [diff] [blame] | 65 | |
| 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.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 72 | |
reed@android.com | e28ff55 | 2009-11-19 20:46:39 +0000 | [diff] [blame] | 73 | static 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.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 77 | 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 | |
| 85 | static void quad_clipper(const SkPoint src[], const SkRect& clip, |
reed@android.com | bb13586 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 86 | SkCanvas* canvas, const SkPaint& p0, const SkPaint& p1) { |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 87 | drawQuad(canvas, src, p1); |
reed@android.com | bb13586 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 88 | |
reed@android.com | 909994f | 2009-11-18 16:09:51 +0000 | [diff] [blame] | 89 | SkEdgeClipper clipper; |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 90 | 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.com | 3a0cd7f | 2009-11-17 19:39:51 +0000 | [diff] [blame] | 96 | check_clipper(2, pts, clip); |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 97 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p0); |
| 98 | break; |
| 99 | case SkPath::kQuad_Verb: |
reed@android.com | 3a0cd7f | 2009-11-17 19:39:51 +0000 | [diff] [blame] | 100 | check_clipper(3, pts, clip); |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 101 | drawQuad(canvas, pts, p0); |
| 102 | break; |
| 103 | default: |
| 104 | SkASSERT(!"unexpected verb"); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
reed@android.com | bb13586 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 110 | static 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.com | 909994f | 2009-11-18 16:09:51 +0000 | [diff] [blame] | 114 | SkEdgeClipper clipper; |
reed@android.com | bb13586 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 115 | 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.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 135 | static const clipper_proc gProcs[] = { |
reed@android.com | e28ff55 | 2009-11-19 20:46:39 +0000 | [diff] [blame] | 136 | line_intersector, |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 137 | line_clipper, |
reed@android.com | bb13586 | 2009-11-18 13:47:40 +0000 | [diff] [blame] | 138 | quad_clipper, |
| 139 | cubic_clipper |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 143 | |
| 144 | enum { |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 145 | W = 640/3, |
| 146 | H = 480/3 |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | class LineClipperView : public SkView { |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 150 | SkMSec fNow; |
reed@android.com | 3a0cd7f | 2009-11-17 19:39:51 +0000 | [diff] [blame] | 151 | int fCounter; |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 152 | int fProcIndex; |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 153 | SkRect fClip; |
| 154 | SkRandom fRand; |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 155 | SkPoint fPts[4]; |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 156 | |
| 157 | void randPts() { |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 158 | for (int i = 0; i < SK_ARRAY_COUNT(fPts); i++) { |
| 159 | fPts[i].set(fRand.nextUScalar1() * 640, |
| 160 | fRand.nextUScalar1() * 480); |
| 161 | } |
reed@android.com | 3a0cd7f | 2009-11-17 19:39:51 +0000 | [diff] [blame] | 162 | fCounter += 1; |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | public: |
| 166 | LineClipperView() { |
reed@android.com | e28ff55 | 2009-11-19 20:46:39 +0000 | [diff] [blame] | 167 | fProcIndex = 0; |
reed@android.com | 3a0cd7f | 2009-11-17 19:39:51 +0000 | [diff] [blame] | 168 | fCounter = 0; |
| 169 | |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 170 | 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 | |
| 176 | protected: |
| 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.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 198 | virtual void onDraw(SkCanvas* canvas) { |
| 199 | this->drawBG(canvas); |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 200 | |
| 201 | SkMSec now = SampleCode::GetAnimTime(); |
| 202 | if (fNow != now) { |
| 203 | fNow = now; |
| 204 | this->randPts(); |
| 205 | this->inval(NULL); |
| 206 | } |
| 207 | |
reed@android.com | e28ff55 | 2009-11-19 20:46:39 +0000 | [diff] [blame] | 208 | // fProcIndex = test0(fPts, &fClip); |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 209 | |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 210 | SkPaint paint, paint1; |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 211 | |
| 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.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 222 | paint.setStyle(SkPaint::kStroke_Style); |
| 223 | // paint.setStrokeWidth(SkIntToScalar(3)); |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 224 | paint.setStrokeCap(SkPaint::kRound_Cap); |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 225 | |
| 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.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
reed@android.com | 77f0ef7 | 2009-11-17 18:47:52 +0000 | [diff] [blame] | 233 | // fProcIndex = (fProcIndex + 1) % SK_ARRAY_COUNT(gProcs); |
| 234 | if (x < 50 && y < 50) { |
| 235 | this->randPts(); |
| 236 | } |
reed@android.com | 7014906 | 2009-11-16 20:39:43 +0000 | [diff] [blame] | 237 | this->inval(NULL); |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | virtual bool onClick(Click* click) { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | private: |
| 246 | typedef SkView INHERITED; |
| 247 | }; |
| 248 | |
| 249 | ////////////////////////////////////////////////////////////////////////////// |
| 250 | |
| 251 | static SkView* MyFactory() { return new LineClipperView; } |
| 252 | static SkViewRegister reg(MyFactory); |
| 253 | |