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