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 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 15 | class PolyToPolyView : public SampleView { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 16 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 17 | PolyToPolyView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | // tests |
| 19 | { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 20 | SkPoint src[] = { { 0, 0 }, |
| 21 | { SK_Scalar1, 0 }, |
| 22 | { 0, SK_Scalar1 } }; |
| 23 | SkPoint dst[] = { { 0, 0 }, |
| 24 | { 2*SK_Scalar1, 0 }, |
| 25 | { 0, 2*SK_Scalar1 } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | SkMatrix m1, m2; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 27 | |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 28 | (void) m1.setPolyToPoly(src, dst, 3); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 30 | m2.reset(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | m2.set(SkMatrix::kMScaleX, dst[1].fX - dst[0].fX); |
| 32 | m2.set(SkMatrix::kMSkewX, dst[2].fX - dst[0].fX); |
| 33 | m2.set(SkMatrix::kMTransX, dst[0].fX); |
| 34 | m2.set(SkMatrix::kMSkewY, dst[1].fY - dst[0].fY); |
| 35 | m2.set(SkMatrix::kMScaleY, dst[2].fY - dst[0].fY); |
| 36 | m2.set(SkMatrix::kMTransY, dst[0].fY); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 37 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | m1.reset(); |
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 | const SkScalar src1[] = { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 41 | 0, 0, 0, 427, 316, 427, 316, 0 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | }; |
| 43 | const SkScalar dst1[] = { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 44 | 158, 177.5f, 158, 249.5f, |
| 45 | 158, 604.5f, 158, -177.5f |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | }; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 47 | |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 48 | (void) m2.setPolyToPoly((const SkPoint*)src1, (SkPoint*)dst1, 4); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 49 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | { |
| 51 | const SkPoint src[] = { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 52 | { SkIntToScalar(1), SkIntToScalar(0) }, |
| 53 | { SkIntToScalar(4), SkIntToScalar(7) }, |
| 54 | { SkIntToScalar(10), SkIntToScalar(2) } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | }; |
| 56 | const SkPoint dst[] = { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 57 | { SkIntToScalar(4), SkIntToScalar(2) }, |
| 58 | { SkIntToScalar(45), SkIntToScalar(26) }, |
| 59 | { SkIntToScalar(32), SkIntToScalar(17) } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | }; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 62 | SkMatrix m0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | m0.setPolyToPoly(src, dst, 3); |
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)) { |
tfarina@chromium.org | 45369a3 | 2012-09-30 11:30:01 +0000 | [diff] [blame] | 72 | SampleCode::TitleR(evt, "PolyToPolyView"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | return this->INHERITED::onQuery(evt); |
| 76 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[], |
| 79 | const int idst[], int count) { |
| 80 | SkMatrix matrix; |
| 81 | SkPoint src[4], dst[4]; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 82 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | for (int i = 0; i < count; i++) { |
| 84 | src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1])); |
| 85 | dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1])); |
| 86 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 87 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | canvas->save(); |
| 89 | matrix.setPolyToPoly(src, dst, count); |
| 90 | canvas->concat(matrix); |
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 | paint->setColor(SK_ColorGRAY); |
| 93 | paint->setStyle(SkPaint::kStroke_Style); |
| 94 | const SkScalar D = SkIntToScalar(64); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 95 | canvas->drawRect(SkRect::MakeWH(D, D), *paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | canvas->drawLine(0, 0, D, D, *paint); |
| 97 | canvas->drawLine(0, D, D, 0, *paint); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | SkPaint::FontMetrics fm; |
| 100 | paint->getFontMetrics(&fm); |
| 101 | paint->setColor(SK_ColorRED); |
| 102 | paint->setStyle(SkPaint::kFill_Style); |
| 103 | SkScalar x = D/2; |
| 104 | float y = D/2 - (fm.fAscent + fm.fDescent)/2; |
| 105 | SkString str; |
| 106 | str.appendS32(count); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 107 | canvas->drawString(str, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 108 | x, y, |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 109 | *paint); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 110 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | canvas->restore(); |
| 112 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 113 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 114 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | SkPaint paint; |
| 116 | paint.setAntiAlias(true); |
| 117 | paint.setStrokeWidth(SkIntToScalar(4)); |
| 118 | paint.setTextSize(SkIntToScalar(40)); |
| 119 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 120 | |
| 121 | canvas->save(); |
| 122 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 123 | // translate (1 point) |
| 124 | const int src1[] = { 0, 0 }; |
| 125 | const int dst1[] = { 5, 5 }; |
| 126 | doDraw(canvas, &paint, src1, dst1, 1); |
| 127 | canvas->restore(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | canvas->save(); |
| 130 | canvas->translate(SkIntToScalar(160), SkIntToScalar(10)); |
| 131 | // rotate/uniform-scale (2 points) |
| 132 | const int src2[] = { 32, 32, 64, 32 }; |
| 133 | const int dst2[] = { 32, 32, 64, 48 }; |
| 134 | doDraw(canvas, &paint, src2, dst2, 2); |
| 135 | canvas->restore(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 136 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | canvas->save(); |
| 138 | canvas->translate(SkIntToScalar(10), SkIntToScalar(110)); |
| 139 | // rotate/skew (3 points) |
| 140 | const int src3[] = { 0, 0, 64, 0, 0, 64 }; |
| 141 | const int dst3[] = { 0, 0, 96, 0, 24, 64 }; |
| 142 | doDraw(canvas, &paint, src3, dst3, 3); |
| 143 | canvas->restore(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 144 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | canvas->save(); |
| 146 | canvas->translate(SkIntToScalar(160), SkIntToScalar(110)); |
| 147 | // perspective (4 points) |
| 148 | const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 }; |
| 149 | const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 }; |
| 150 | doDraw(canvas, &paint, src4, dst4, 4); |
| 151 | canvas->restore(); |
| 152 | } |
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 | private: |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 155 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | ////////////////////////////////////////////////////////////////////////////// |
| 159 | |
| 160 | static SkView* MyFactory() { return new PolyToPolyView; } |
| 161 | static SkViewRegister reg(MyFactory); |