blob: 7f9f076d5562b3dc2a35e1a54211bff9f4c03d4f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#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
16extern bool SkSetPoly3To3(SkMatrix* matrix, const SkPoint src[3], const SkPoint dst[3]);
17
reed@google.com0faac1e2011-05-11 05:58:58 +000018class PolyToPolyView : public SampleView {
reed@google.com67981c52011-02-17 14:21:01 +000019public:
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 PolyToPolyView() {
21 // tests
22 {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000023 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.com8a1c16f2008-12-17 15:59:43 +000029 SkMatrix m1, m2;
30 bool success;
reed@google.com67981c52011-02-17 14:21:01 +000031
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 success = m1.setPolyToPoly(src, dst, 3);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
reed@google.com67981c52011-02-17 14:21:01 +000034 m2.reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 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.com67981c52011-02-17 14:21:01 +000041
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 m1.reset();
reed@google.com67981c52011-02-17 14:21:01 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 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.com67981c52011-02-17 14:21:01 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 success = m2.setPolyToPoly((const SkPoint*)src1, (SkPoint*)dst1, 4);
reed@google.com67981c52011-02-17 14:21:01 +000053
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 {
55 const SkPoint src[] = {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000056 { SkIntToScalar(1), SkIntToScalar(0) },
57 { SkIntToScalar(4), SkIntToScalar(7) },
58 { SkIntToScalar(10), SkIntToScalar(2) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 };
60 const SkPoint dst[] = {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000061 { SkIntToScalar(4), SkIntToScalar(2) },
62 { SkIntToScalar(45), SkIntToScalar(26) },
63 { SkIntToScalar(32), SkIntToScalar(17) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 };
reed@google.com67981c52011-02-17 14:21:01 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 SkMatrix m0, m1;
67 m0.setPolyToPoly(src, dst, 3);
reed@google.com67981c52011-02-17 14:21:01 +000068 // SkSetPoly3To3(&m1, src, dst);
reed@google.com2f3dc9d2011-05-02 17:33:45 +000069 // m0.dump();
70 // m1.dump();
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 }
72 }
73 }
reed@google.com67981c52011-02-17 14:21:01 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075protected:
76 // overrides from SkEventSink
77 virtual bool onQuery(SkEvent* evt) {
78 if (SampleCode::TitleQ(*evt)) {
79 SkString str("PolyToPolyView");
80 SampleCode::TitleR(evt, str.c_str());
81 return true;
82 }
83 return this->INHERITED::onQuery(evt);
84 }
reed@google.com67981c52011-02-17 14:21:01 +000085
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
87 const int idst[], int count) {
88 SkMatrix matrix;
89 SkPoint src[4], dst[4];
reed@google.com67981c52011-02-17 14:21:01 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 for (int i = 0; i < count; i++) {
92 src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
93 dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
94 }
reed@google.com67981c52011-02-17 14:21:01 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 canvas->save();
97 matrix.setPolyToPoly(src, dst, count);
98 canvas->concat(matrix);
reed@google.com67981c52011-02-17 14:21:01 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 paint->setColor(SK_ColorGRAY);
101 paint->setStyle(SkPaint::kStroke_Style);
102 const SkScalar D = SkIntToScalar(64);
103 canvas->drawRectCoords(0, 0, D, D, *paint);
104 canvas->drawLine(0, 0, D, D, *paint);
105 canvas->drawLine(0, D, D, 0, *paint);
reed@google.com67981c52011-02-17 14:21:01 +0000106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 SkPaint::FontMetrics fm;
108 paint->getFontMetrics(&fm);
109 paint->setColor(SK_ColorRED);
110 paint->setStyle(SkPaint::kFill_Style);
111 SkScalar x = D/2;
112 float y = D/2 - (fm.fAscent + fm.fDescent)/2;
113 SkString str;
114 str.appendS32(count);
115 canvas->drawText(str.c_str(), str.size(), x, y, *paint);
reed@google.com67981c52011-02-17 14:21:01 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 canvas->restore();
118 }
reed@google.com67981c52011-02-17 14:21:01 +0000119
reed@google.com0faac1e2011-05-11 05:58:58 +0000120 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 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.com67981c52011-02-17 14:21:01 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 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.com67981c52011-02-17 14:21:01 +0000142
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 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.com67981c52011-02-17 14:21:01 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 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.com67981c52011-02-17 14:21:01 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000161 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162};
163
164//////////////////////////////////////////////////////////////////////////////
165
166static SkView* MyFactory() { return new PolyToPolyView; }
167static SkViewRegister reg(MyFactory);
168