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