blob: 1283a23bc577f8289c8b98047c53240244ec49e3 [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:
rmistry@google.comae933ce2012-08-23 18:19:56 +000020 PolyToPolyView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 // 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;
reed@google.com67981c52011-02-17 14:21:01 +000030
djsollen@google.comefbe8e92013-02-07 18:58:35 +000031 (void) m1.setPolyToPoly(src, dst, 3);
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
reed@google.com67981c52011-02-17 14:21:01 +000033 m2.reset();
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 m2.set(SkMatrix::kMScaleX, dst[1].fX - dst[0].fX);
35 m2.set(SkMatrix::kMSkewX, dst[2].fX - dst[0].fX);
36 m2.set(SkMatrix::kMTransX, dst[0].fX);
37 m2.set(SkMatrix::kMSkewY, dst[1].fY - dst[0].fY);
38 m2.set(SkMatrix::kMScaleY, dst[2].fY - dst[0].fY);
39 m2.set(SkMatrix::kMTransY, dst[0].fY);
reed@google.com67981c52011-02-17 14:21:01 +000040
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 m1.reset();
reed@google.com67981c52011-02-17 14:21:01 +000042
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 const SkScalar src1[] = {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000044 0, 0, 0, 427, 316, 427, 316, 0
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 };
46 const SkScalar dst1[] = {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000047 158, 177.5f, 158, 249.5f,
48 158, 604.5f, 158, -177.5f
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 };
reed@google.com67981c52011-02-17 14:21:01 +000050
djsollen@google.comefbe8e92013-02-07 18:58:35 +000051 (void) m2.setPolyToPoly((const SkPoint*)src1, (SkPoint*)dst1, 4);
reed@google.com67981c52011-02-17 14:21:01 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 {
54 const SkPoint src[] = {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000055 { SkIntToScalar(1), SkIntToScalar(0) },
56 { SkIntToScalar(4), SkIntToScalar(7) },
57 { SkIntToScalar(10), SkIntToScalar(2) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 };
59 const SkPoint dst[] = {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000060 { SkIntToScalar(4), SkIntToScalar(2) },
61 { SkIntToScalar(45), SkIntToScalar(26) },
62 { SkIntToScalar(32), SkIntToScalar(17) }
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 };
reed@google.com67981c52011-02-17 14:21:01 +000064
bsalomon@google.com820e80a2011-10-24 21:09:40 +000065 SkMatrix m0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 m0.setPolyToPoly(src, dst, 3);
bsalomon@google.com820e80a2011-10-24 21:09:40 +000067 // SkMatrix m1;
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)) {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000079 SampleCode::TitleR(evt, "PolyToPolyView");
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 return true;
81 }
82 return this->INHERITED::onQuery(evt);
83 }
reed@google.com67981c52011-02-17 14:21:01 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
86 const int idst[], int count) {
87 SkMatrix matrix;
88 SkPoint src[4], dst[4];
reed@google.com67981c52011-02-17 14:21:01 +000089
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 for (int i = 0; i < count; i++) {
91 src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
92 dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
93 }
reed@google.com67981c52011-02-17 14:21:01 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 canvas->save();
96 matrix.setPolyToPoly(src, dst, count);
97 canvas->concat(matrix);
reed@google.com67981c52011-02-17 14:21:01 +000098
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 paint->setColor(SK_ColorGRAY);
100 paint->setStyle(SkPaint::kStroke_Style);
101 const SkScalar D = SkIntToScalar(64);
102 canvas->drawRectCoords(0, 0, D, D, *paint);
103 canvas->drawLine(0, 0, D, D, *paint);
104 canvas->drawLine(0, D, D, 0, *paint);
reed@google.com67981c52011-02-17 14:21:01 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 SkPaint::FontMetrics fm;
107 paint->getFontMetrics(&fm);
108 paint->setColor(SK_ColorRED);
109 paint->setStyle(SkPaint::kFill_Style);
110 SkScalar x = D/2;
111 float y = D/2 - (fm.fAscent + fm.fDescent)/2;
112 SkString str;
113 str.appendS32(count);
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000114 canvas->drawText(str.c_str(), str.size(),
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000115 x, y,
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000116 *paint);
reed@google.com67981c52011-02-17 14:21:01 +0000117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 canvas->restore();
119 }
reed@google.com67981c52011-02-17 14:21:01 +0000120
reed@google.com0faac1e2011-05-11 05:58:58 +0000121 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 SkPaint paint;
123 paint.setAntiAlias(true);
124 paint.setStrokeWidth(SkIntToScalar(4));
125 paint.setTextSize(SkIntToScalar(40));
126 paint.setTextAlign(SkPaint::kCenter_Align);
127
128 canvas->save();
129 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
130 // translate (1 point)
131 const int src1[] = { 0, 0 };
132 const int dst1[] = { 5, 5 };
133 doDraw(canvas, &paint, src1, dst1, 1);
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(160), SkIntToScalar(10));
138 // rotate/uniform-scale (2 points)
139 const int src2[] = { 32, 32, 64, 32 };
140 const int dst2[] = { 32, 32, 64, 48 };
141 doDraw(canvas, &paint, src2, dst2, 2);
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(10), SkIntToScalar(110));
146 // rotate/skew (3 points)
147 const int src3[] = { 0, 0, 64, 0, 0, 64 };
148 const int dst3[] = { 0, 0, 96, 0, 24, 64 };
149 doDraw(canvas, &paint, src3, dst3, 3);
150 canvas->restore();
reed@google.com67981c52011-02-17 14:21:01 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 canvas->save();
153 canvas->translate(SkIntToScalar(160), SkIntToScalar(110));
154 // perspective (4 points)
155 const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
156 const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
157 doDraw(canvas, &paint, src4, dst4, 4);
158 canvas->restore();
159 }
reed@google.com67981c52011-02-17 14:21:01 +0000160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161private:
reed@google.com0faac1e2011-05-11 05:58:58 +0000162 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163};
164
165//////////////////////////////////////////////////////////////////////////////
166
167static SkView* MyFactory() { return new PolyToPolyView; }
168static SkViewRegister reg(MyFactory);