reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkGraphics.h" |
| 5 | #include "SkPath.h" |
| 6 | #include "SkRandom.h" |
| 7 | #include "SkTime.h" |
| 8 | |
| 9 | extern bool SkSetPoly3To3(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]); |
| 10 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 11 | class PolyToPolyView : public SampleView { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 12 | public: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | PolyToPolyView() { |
| 14 | // tests |
| 15 | { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 16 | SkPoint src[] = { { 0, 0 }, |
| 17 | { SK_Scalar1, 0 }, |
| 18 | { 0, SK_Scalar1 } }; |
| 19 | SkPoint dst[] = { { 0, 0 }, |
| 20 | { 2*SK_Scalar1, 0 }, |
| 21 | { 0, 2*SK_Scalar1 } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | SkMatrix m1, m2; |
| 23 | bool success; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | success = m1.setPolyToPoly(src, dst, 3); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 27 | m2.reset(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | m2.set(SkMatrix::kMScaleX, dst[1].fX - dst[0].fX); |
| 29 | m2.set(SkMatrix::kMSkewX, dst[2].fX - dst[0].fX); |
| 30 | m2.set(SkMatrix::kMTransX, dst[0].fX); |
| 31 | m2.set(SkMatrix::kMSkewY, dst[1].fY - dst[0].fY); |
| 32 | m2.set(SkMatrix::kMScaleY, dst[2].fY - dst[0].fY); |
| 33 | m2.set(SkMatrix::kMTransY, dst[0].fY); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 34 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | m1.reset(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 36 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | const SkScalar src1[] = { |
| 38 | 0, 0, 0, SkFloatToScalar(427), SkFloatToScalar(316), SkFloatToScalar(427), SkFloatToScalar(316), 0 |
| 39 | }; |
| 40 | const SkScalar dst1[] = { |
| 41 | SkFloatToScalar(158), SkFloatToScalar(177.5f), SkFloatToScalar(158), SkFloatToScalar(249.5f), |
| 42 | SkFloatToScalar(158), SkFloatToScalar(604.5f), SkFloatToScalar(158), SkFloatToScalar(-177.5f) |
| 43 | }; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 44 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | success = m2.setPolyToPoly((const SkPoint*)src1, (SkPoint*)dst1, 4); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 46 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | { |
| 48 | const SkPoint src[] = { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 49 | { SkIntToScalar(1), SkIntToScalar(0) }, |
| 50 | { SkIntToScalar(4), SkIntToScalar(7) }, |
| 51 | { SkIntToScalar(10), SkIntToScalar(2) } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | }; |
| 53 | const SkPoint dst[] = { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 54 | { SkIntToScalar(4), SkIntToScalar(2) }, |
| 55 | { SkIntToScalar(45), SkIntToScalar(26) }, |
| 56 | { SkIntToScalar(32), SkIntToScalar(17) } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | }; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 58 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | SkMatrix m0, m1; |
| 60 | m0.setPolyToPoly(src, dst, 3); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 61 | // SkSetPoly3To3(&m1, src, dst); |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 62 | // m0.dump(); |
| 63 | // m1.dump(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | protected: |
| 69 | // overrides from SkEventSink |
| 70 | virtual bool onQuery(SkEvent* evt) { |
| 71 | if (SampleCode::TitleQ(*evt)) { |
| 72 | SkString str("PolyToPolyView"); |
| 73 | SampleCode::TitleR(evt, str.c_str()); |
| 74 | return true; |
| 75 | } |
| 76 | return this->INHERITED::onQuery(evt); |
| 77 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[], |
| 80 | const int idst[], int count) { |
| 81 | SkMatrix matrix; |
| 82 | SkPoint src[4], dst[4]; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 83 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | for (int i = 0; i < count; i++) { |
| 85 | src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1])); |
| 86 | dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1])); |
| 87 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | canvas->save(); |
| 90 | matrix.setPolyToPoly(src, dst, count); |
| 91 | canvas->concat(matrix); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 92 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | paint->setColor(SK_ColorGRAY); |
| 94 | paint->setStyle(SkPaint::kStroke_Style); |
| 95 | const SkScalar D = SkIntToScalar(64); |
| 96 | canvas->drawRectCoords(0, 0, D, D, *paint); |
| 97 | canvas->drawLine(0, 0, D, D, *paint); |
| 98 | canvas->drawLine(0, D, D, 0, *paint); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 99 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | SkPaint::FontMetrics fm; |
| 101 | paint->getFontMetrics(&fm); |
| 102 | paint->setColor(SK_ColorRED); |
| 103 | paint->setStyle(SkPaint::kFill_Style); |
| 104 | SkScalar x = D/2; |
| 105 | float y = D/2 - (fm.fAscent + fm.fDescent)/2; |
| 106 | SkString str; |
| 107 | str.appendS32(count); |
| 108 | canvas->drawText(str.c_str(), str.size(), x, y, *paint); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 109 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | canvas->restore(); |
| 111 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 112 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 113 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | SkPaint paint; |
| 115 | paint.setAntiAlias(true); |
| 116 | paint.setStrokeWidth(SkIntToScalar(4)); |
| 117 | paint.setTextSize(SkIntToScalar(40)); |
| 118 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 119 | |
| 120 | canvas->save(); |
| 121 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 122 | // translate (1 point) |
| 123 | const int src1[] = { 0, 0 }; |
| 124 | const int dst1[] = { 5, 5 }; |
| 125 | doDraw(canvas, &paint, src1, dst1, 1); |
| 126 | canvas->restore(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 127 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | canvas->save(); |
| 129 | canvas->translate(SkIntToScalar(160), SkIntToScalar(10)); |
| 130 | // rotate/uniform-scale (2 points) |
| 131 | const int src2[] = { 32, 32, 64, 32 }; |
| 132 | const int dst2[] = { 32, 32, 64, 48 }; |
| 133 | doDraw(canvas, &paint, src2, dst2, 2); |
| 134 | canvas->restore(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 135 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | canvas->save(); |
| 137 | canvas->translate(SkIntToScalar(10), SkIntToScalar(110)); |
| 138 | // rotate/skew (3 points) |
| 139 | const int src3[] = { 0, 0, 64, 0, 0, 64 }; |
| 140 | const int dst3[] = { 0, 0, 96, 0, 24, 64 }; |
| 141 | doDraw(canvas, &paint, src3, dst3, 3); |
| 142 | canvas->restore(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 143 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | canvas->save(); |
| 145 | canvas->translate(SkIntToScalar(160), SkIntToScalar(110)); |
| 146 | // perspective (4 points) |
| 147 | const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 }; |
| 148 | const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 }; |
| 149 | doDraw(canvas, &paint, src4, dst4, 4); |
| 150 | canvas->restore(); |
| 151 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 152 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 154 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | ////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | static SkView* MyFactory() { return new PolyToPolyView; } |
| 160 | static SkViewRegister reg(MyFactory); |
| 161 | |