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