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 | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkPaint.h" |
| 10 | #include "SkView.h" |
| 11 | #include "SkLayer.h" |
| 12 | |
reed@android.com | c8c49c5 | 2010-02-24 15:36:57 +0000 | [diff] [blame] | 13 | #include "SkMatrix44.h" |
| 14 | static 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.com | b5d0f7b | 2010-02-24 22:08:58 +0000 | [diff] [blame] | 38 | static 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.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 50 | |
| 51 | static 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.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 58 | dst.toString(&str); |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 59 | 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.com | c8c49c5 | 2010-02-24 15:36:57 +0000 | [diff] [blame] | 64 | static 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 81 | |
reed@android.com | c8c49c5 | 2010-02-24 15:36:57 +0000 | [diff] [blame] | 82 | m0.setScale(2, 4, 6); |
| 83 | m0.postScale(SkDoubleToMScalar(0.5)); |
| 84 | test_inv("scale/postscale to 1,2,3", m0); |
reed@android.com | b5d0f7b | 2010-02-24 22:08:58 +0000 | [diff] [blame] | 85 | |
| 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.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 100 | |
| 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.com | c8c49c5 | 2010-02-24 15:36:57 +0000 | [diff] [blame] | 109 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 110 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 111 | /////////////////////////////////////////////////////////////////////////////// |
| 112 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 113 | static void dump_layers(const SkLayer* layer, int tab = 0) { |
| 114 | SkMatrix matrix; |
| 115 | SkString matrixStr; |
| 116 | |
| 117 | layer->getLocalTransform(&matrix); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 118 | matrix.toString(&matrixStr); |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 119 | |
| 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.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 131 | class TestLayer : public SkLayer { |
| 132 | public: |
| 133 | TestLayer(SkColor c) : fColor(c) {} |
| 134 | |
| 135 | protected: |
| 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.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 142 | paint.setAlpha(SkScalarRoundToInt(opacity * 255)); |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 143 | |
| 144 | canvas->drawRect(r, paint); |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | SkColor fColor; |
| 149 | }; |
| 150 | |
| 151 | class SkLayerView : public SkView { |
| 152 | private: |
| 153 | SkLayer* fRootLayer; |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 154 | SkLayer* fLastChild; |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 155 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 156 | SkLayerView() { |
reed@android.com | c8c49c5 | 2010-02-24 15:36:57 +0000 | [diff] [blame] | 157 | test44(); |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 158 | static const int W = 600; |
| 159 | static const int H = 440; |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 160 | 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.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 173 | fRootLayer = new TestLayer(0xFFDDDDDD); |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 174 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 181 | |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 182 | 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.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 192 | fLastChild = child; |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 193 | fRootLayer->addChild(child)->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 194 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 195 | if (false) { |
| 196 | SkMatrix matrix; |
| 197 | matrix.setScale(0.5, 0.5); |
| 198 | fRootLayer->setMatrix(matrix); |
| 199 | } |
| 200 | |
reed@android.com | b5d0f7b | 2010-02-24 22:08:58 +0000 | [diff] [blame] | 201 | // dump_layers(fRootLayer); |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 202 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 203 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 204 | virtual ~SkLayerView() { |
| 205 | SkSafeUnref(fRootLayer); |
| 206 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 207 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 208 | protected: |
| 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 217 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 218 | virtual void onDraw(SkCanvas* canvas) { |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 219 | canvas->drawColor(SK_ColorWHITE); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 220 | |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 221 | canvas->translate(20, 20); |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 222 | fRootLayer->draw(canvas); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 223 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 224 | // 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.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 235 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 236 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 237 | private: |
| 238 | typedef SkView INHERITED; |
| 239 | }; |
| 240 | |
| 241 | /////////////////////////////////////////////////////////////////////////////// |
| 242 | |
| 243 | static SkView* MyFactory() { return new SkLayerView; } |
| 244 | static SkViewRegister reg(MyFactory); |