blob: 042256161773f1b28042864dc5bd8c5649e0a17f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.comda6fb322010-02-19 21:41:30 +00007#include "SampleCode.h"
8#include "SkCanvas.h"
9#include "SkPaint.h"
10#include "SkView.h"
11#include "SkLayer.h"
12
reed@android.comc8c49c52010-02-24 15:36:57 +000013#include "SkMatrix44.h"
14static void test_inv(const char label[], const SkMatrix44& mat) {
15 SkDebugf("%s\n", label);
16 mat.dump();
17
18 SkMatrix44 inv;
19 if (mat.invert(&inv)) {
20 inv.dump();
21 } else {
22 SkDebugf("--- invert failed\n");
23 }
24
25 SkMatrix44 a, b;
26 a.setConcat(mat, inv);
27 b.setConcat(inv, mat);
28 SkDebugf("concat mat with inverse pre=%d post=%d\n", a.isIdentity(), b.isIdentity());
29 if (!a.isIdentity()) {
30 a.dump();
31 }
32 if (!b.isIdentity()) {
33 b.dump();
34 }
35 SkDebugf("\n");
36}
37
reed@android.comb5d0f7b2010-02-24 22:08:58 +000038static void test_map(SkScalar x0, SkScalar y0, SkScalar z0,
39 const SkMatrix44& mat,
40 SkScalar x1, SkScalar y1, SkScalar z1) {
41 SkVector4 src, dst;
42 src.set(x0, y0, z0);
43 dst = mat * src;
44 SkDebugf("map: src: %g %g %g dst: %g %g %g (%g) expected: %g %g %g match: %d\n",
45 x0, y0, z0,
46 dst.fData[0], dst.fData[1], dst.fData[2], dst.fData[3],
47 x1, y1, z1,
48 dst.fData[0] == x1 && dst.fData[1] == y1 && dst.fData[2] == z1);
49}
reed@android.com7d970c72010-04-22 16:07:49 +000050
51static void test_33(const SkMatrix44& mat,
52 SkScalar x0, SkScalar x1, SkScalar x2,
53 SkScalar y0, SkScalar y1, SkScalar y2) {
54 SkMatrix dst = mat;
55 if (dst[0] != x0 || dst[1] != x1 || dst[2] != x2 ||
56 dst[3] != y0 || dst[4] != y1 || dst[5] != y2) {
57 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +000058 dst.toString(&str);
reed@android.com7d970c72010-04-22 16:07:49 +000059 SkDebugf("3x3: expected 3x3 [%g %g %g] [%g %g %g] bug got %s\n",
60 x0, x1, x2, y0, y1, y2, str.c_str());
61 }
62}
63
reed@android.comc8c49c52010-02-24 15:36:57 +000064static void test44() {
65 SkMatrix44 m0, m1, m2;
66
67 test_inv("identity", m0);
68 m0.setTranslate(2,3,4);
69 test_inv("translate", m0);
70 m0.setScale(2,3,4);
71 test_inv("scale", m0);
72 m0.postTranslate(5, 6, 7);
73 test_inv("postTranslate", m0);
74 m0.setScale(2,3,4);
75 m1.setTranslate(5, 6, 7);
76 m0.setConcat(m0, m1);
77 test_inv("postTranslate2", m0);
78 m0.setScale(2,3,4);
79 m0.preTranslate(5, 6, 7);
80 test_inv("preTranslate", m0);
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reed@android.comc8c49c52010-02-24 15:36:57 +000082 m0.setScale(2, 4, 6);
83 m0.postScale(SkDoubleToMScalar(0.5));
84 test_inv("scale/postscale to 1,2,3", m0);
reed@android.comb5d0f7b2010-02-24 22:08:58 +000085
86 m0.reset();
87 test_map(1, 0, 0, m0, 1, 0, 0);
88 test_map(0, 1, 0, m0, 0, 1, 0);
89 test_map(0, 0, 1, m0, 0, 0, 1);
90 m0.setScale(2, 3, 4);
91 test_map(1, 0, 0, m0, 2, 0, 0);
92 test_map(0, 1, 0, m0, 0, 3, 0);
93 test_map(0, 0, 1, m0, 0, 0, 4);
94 m0.setTranslate(2, 3, 4);
95 test_map(0, 0, 0, m0, 2, 3, 4);
96 m0.preScale(5, 6, 7);
97 test_map(1, 0, 0, m0, 7, 3, 4);
98 test_map(0, 1, 0, m0, 2, 9, 4);
99 test_map(0, 0, 1, m0, 2, 3, 11);
reed@android.com7d970c72010-04-22 16:07:49 +0000100
101 SkMScalar deg = 45;
102 m0.setRotateDegreesAbout(0, 0, 1, deg);
103 test_map(1, 0, 0, m0, 0.707106769, -0.707106769, 0);
104
105 m0.reset();
106 test_33(m0, 1, 0, 0, 0, 1, 0);
107 m0.setTranslate(3, 4, 5);
108 test_33(m0, 1, 0, 3, 0, 1, 4);
reed@android.comc8c49c52010-02-24 15:36:57 +0000109}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed@android.comda6fb322010-02-19 21:41:30 +0000111///////////////////////////////////////////////////////////////////////////////
112
reed@android.com745bfbd2010-02-24 17:16:35 +0000113static void dump_layers(const SkLayer* layer, int tab = 0) {
114 SkMatrix matrix;
115 SkString matrixStr;
116
117 layer->getLocalTransform(&matrix);
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000118 matrix.toString(&matrixStr);
reed@android.com745bfbd2010-02-24 17:16:35 +0000119
120 for (int j = 0; j < tab; j++) {
121 SkDebugf(" ");
122 }
123 SkDebugf("layer=%p parent=%p size=[%g %g] transform=%s\n",
124 layer, layer->getParent(), layer->getWidth(), layer->getHeight(),
125 matrixStr.c_str());
126 for (int i = 0; i < layer->countChildren(); i++) {
127 dump_layers(layer->getChild(i), tab + 4);
128 }
129}
130
reed@android.comda6fb322010-02-19 21:41:30 +0000131class TestLayer : public SkLayer {
132public:
133 TestLayer(SkColor c) : fColor(c) {}
134
135protected:
136 virtual void onDraw(SkCanvas* canvas, SkScalar opacity) {
137 SkRect r;
138 r.set(0, 0, this->getWidth(), this->getHeight());
139
140 SkPaint paint;
141 paint.setColor(fColor);
reed@google.come1ca7052013-12-17 19:22:07 +0000142 paint.setAlpha(SkScalarRoundToInt(opacity * 255));
reed@android.comda6fb322010-02-19 21:41:30 +0000143
144 canvas->drawRect(r, paint);
145 }
146
147private:
148 SkColor fColor;
149};
150
151class SkLayerView : public SkView {
152private:
153 SkLayer* fRootLayer;
reed@android.com745bfbd2010-02-24 17:16:35 +0000154 SkLayer* fLastChild;
reed@android.comda6fb322010-02-19 21:41:30 +0000155public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000156 SkLayerView() {
reed@android.comc8c49c52010-02-24 15:36:57 +0000157 test44();
reed@android.com2bd703b2010-02-24 02:01:23 +0000158 static const int W = 600;
159 static const int H = 440;
reed@android.comda6fb322010-02-19 21:41:30 +0000160 static const struct {
161 int fWidth;
162 int fHeight;
163 SkColor fColor;
164 int fPosX;
165 int fPosY;
166 } gData[] = {
167 { 120, 80, SK_ColorRED, 0, 0 },
168 { 120, 80, SK_ColorGREEN, W - 120, 0 },
169 { 120, 80, SK_ColorBLUE, 0, H - 80 },
170 { 120, 80, SK_ColorMAGENTA, W - 120, H - 80 },
171 };
172
reed@android.com2bd703b2010-02-24 02:01:23 +0000173 fRootLayer = new TestLayer(0xFFDDDDDD);
reed@android.comda6fb322010-02-19 21:41:30 +0000174 fRootLayer->setSize(W, H);
175 for (size_t i = 0; i < SK_ARRAY_COUNT(gData); i++) {
176 SkLayer* child = new TestLayer(gData[i].fColor);
177 child->setSize(gData[i].fWidth, gData[i].fHeight);
178 child->setPosition(gData[i].fPosX, gData[i].fPosY);
179 fRootLayer->addChild(child)->unref();
180 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
reed@android.com2bd703b2010-02-24 02:01:23 +0000182 SkLayer* child = new TestLayer(0xFFDD8844);
183 child->setSize(120, 80);
184 child->setPosition(fRootLayer->getWidth()/2 - child->getWidth()/2,
185 fRootLayer->getHeight()/2 - child->getHeight()/2);
186 child->setAnchorPoint(SK_ScalarHalf, SK_ScalarHalf);
187 {
188 SkMatrix m;
189 m.setRotate(SkIntToScalar(30));
190 child->setMatrix(m);
191 }
reed@android.com745bfbd2010-02-24 17:16:35 +0000192 fLastChild = child;
reed@android.com2bd703b2010-02-24 02:01:23 +0000193 fRootLayer->addChild(child)->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194
reed@android.com745bfbd2010-02-24 17:16:35 +0000195 if (false) {
196 SkMatrix matrix;
197 matrix.setScale(0.5, 0.5);
198 fRootLayer->setMatrix(matrix);
199 }
200
reed@android.comb5d0f7b2010-02-24 22:08:58 +0000201// dump_layers(fRootLayer);
reed@android.comda6fb322010-02-19 21:41:30 +0000202 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203
reed@android.comda6fb322010-02-19 21:41:30 +0000204 virtual ~SkLayerView() {
205 SkSafeUnref(fRootLayer);
206 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000207
reed@android.comda6fb322010-02-19 21:41:30 +0000208protected:
209 // overrides from SkEventSink
210 virtual bool onQuery(SkEvent* evt) {
211 if (SampleCode::TitleQ(*evt)) {
212 SampleCode::TitleR(evt, "SkLayer");
213 return true;
214 }
215 return this->INHERITED::onQuery(evt);
216 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000217
reed@android.com745bfbd2010-02-24 17:16:35 +0000218 virtual void onDraw(SkCanvas* canvas) {
reed@android.com2bd703b2010-02-24 02:01:23 +0000219 canvas->drawColor(SK_ColorWHITE);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000220
reed@android.com2bd703b2010-02-24 02:01:23 +0000221 canvas->translate(20, 20);
reed@android.comda6fb322010-02-19 21:41:30 +0000222 fRootLayer->draw(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000223
reed@android.com745bfbd2010-02-24 17:16:35 +0000224 // visual test of getLocalTransform
225 if (true) {
226 SkMatrix matrix;
227 fLastChild->localToGlobal(&matrix);
228 SkPaint paint;
229 paint.setStyle(SkPaint::kStroke_Style);
230 paint.setStrokeWidth(5);
231 paint.setColor(0x88FF0000);
232 canvas->concat(matrix);
233 canvas->drawRect(SkRect::MakeSize(fLastChild->getSize()), paint);
234 }
reed@android.comda6fb322010-02-19 21:41:30 +0000235 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000236
reed@android.comda6fb322010-02-19 21:41:30 +0000237private:
238 typedef SkView INHERITED;
239};
240
241///////////////////////////////////////////////////////////////////////////////
242
243static SkView* MyFactory() { return new SkLayerView; }
244static SkViewRegister reg(MyFactory);