blob: aea0cb4457ee947350cfa637446646a4670aa737 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#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
9extern bool SkSetPoly3To3(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
10
reed@google.com0faac1e2011-05-11 05:58:58 +000011class PolyToPolyView : public SampleView {
reed@google.com67981c52011-02-17 14:21:01 +000012public:
reed@android.com8a1c16f2008-12-17 15:59:43 +000013 PolyToPolyView() {
14 // tests
15 {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000016 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.com8a1c16f2008-12-17 15:59:43 +000022 SkMatrix m1, m2;
23 bool success;
reed@google.com67981c52011-02-17 14:21:01 +000024
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 success = m1.setPolyToPoly(src, dst, 3);
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
reed@google.com67981c52011-02-17 14:21:01 +000027 m2.reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 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.com67981c52011-02-17 14:21:01 +000034
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 m1.reset();
reed@google.com67981c52011-02-17 14:21:01 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 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.com67981c52011-02-17 14:21:01 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 success = m2.setPolyToPoly((const SkPoint*)src1, (SkPoint*)dst1, 4);
reed@google.com67981c52011-02-17 14:21:01 +000046
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 {
48 const SkPoint src[] = {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000049 { SkIntToScalar(1), SkIntToScalar(0) },
50 { SkIntToScalar(4), SkIntToScalar(7) },
51 { SkIntToScalar(10), SkIntToScalar(2) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 };
53 const SkPoint dst[] = {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000054 { SkIntToScalar(4), SkIntToScalar(2) },
55 { SkIntToScalar(45), SkIntToScalar(26) },
56 { SkIntToScalar(32), SkIntToScalar(17) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 };
reed@google.com67981c52011-02-17 14:21:01 +000058
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 SkMatrix m0, m1;
60 m0.setPolyToPoly(src, dst, 3);
reed@google.com67981c52011-02-17 14:21:01 +000061 // SkSetPoly3To3(&m1, src, dst);
reed@google.com2f3dc9d2011-05-02 17:33:45 +000062 // m0.dump();
63 // m1.dump();
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 }
65 }
66 }
reed@google.com67981c52011-02-17 14:21:01 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068protected:
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.com67981c52011-02-17 14:21:01 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 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.com67981c52011-02-17 14:21:01 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 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.com67981c52011-02-17 14:21:01 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 canvas->save();
90 matrix.setPolyToPoly(src, dst, count);
91 canvas->concat(matrix);
reed@google.com67981c52011-02-17 14:21:01 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 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.com67981c52011-02-17 14:21:01 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 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.com67981c52011-02-17 14:21:01 +0000109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 canvas->restore();
111 }
reed@google.com67981c52011-02-17 14:21:01 +0000112
reed@google.com0faac1e2011-05-11 05:58:58 +0000113 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 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.com67981c52011-02-17 14:21:01 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 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.com67981c52011-02-17 14:21:01 +0000135
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 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.com67981c52011-02-17 14:21:01 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 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.com67981c52011-02-17 14:21:01 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000154 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155};
156
157//////////////////////////////////////////////////////////////////////////////
158
159static SkView* MyFactory() { return new PolyToPolyView; }
160static SkViewRegister reg(MyFactory);
161