reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkPaint.h" |
| 4 | #include "SkView.h" |
| 5 | #include "SkLayer.h" |
| 6 | |
reed@android.com | c8c49c5 | 2010-02-24 15:36:57 +0000 | [diff] [blame] | 7 | #include "SkMatrix44.h" |
| 8 | static void test_inv(const char label[], const SkMatrix44& mat) { |
| 9 | SkDebugf("%s\n", label); |
| 10 | mat.dump(); |
| 11 | |
| 12 | SkMatrix44 inv; |
| 13 | if (mat.invert(&inv)) { |
| 14 | inv.dump(); |
| 15 | } else { |
| 16 | SkDebugf("--- invert failed\n"); |
| 17 | } |
| 18 | |
| 19 | SkMatrix44 a, b; |
| 20 | a.setConcat(mat, inv); |
| 21 | b.setConcat(inv, mat); |
| 22 | SkDebugf("concat mat with inverse pre=%d post=%d\n", a.isIdentity(), b.isIdentity()); |
| 23 | if (!a.isIdentity()) { |
| 24 | a.dump(); |
| 25 | } |
| 26 | if (!b.isIdentity()) { |
| 27 | b.dump(); |
| 28 | } |
| 29 | SkDebugf("\n"); |
| 30 | } |
| 31 | |
reed@android.com | b5d0f7b | 2010-02-24 22:08:58 +0000 | [diff] [blame] | 32 | static void test_map(SkScalar x0, SkScalar y0, SkScalar z0, |
| 33 | const SkMatrix44& mat, |
| 34 | SkScalar x1, SkScalar y1, SkScalar z1) { |
| 35 | SkVector4 src, dst; |
| 36 | src.set(x0, y0, z0); |
| 37 | dst = mat * src; |
| 38 | SkDebugf("map: src: %g %g %g dst: %g %g %g (%g) expected: %g %g %g match: %d\n", |
| 39 | x0, y0, z0, |
| 40 | dst.fData[0], dst.fData[1], dst.fData[2], dst.fData[3], |
| 41 | x1, y1, z1, |
| 42 | dst.fData[0] == x1 && dst.fData[1] == y1 && dst.fData[2] == z1); |
| 43 | } |
| 44 | |
reed@android.com | c8c49c5 | 2010-02-24 15:36:57 +0000 | [diff] [blame] | 45 | static void test44() { |
| 46 | SkMatrix44 m0, m1, m2; |
| 47 | |
| 48 | test_inv("identity", m0); |
| 49 | m0.setTranslate(2,3,4); |
| 50 | test_inv("translate", m0); |
| 51 | m0.setScale(2,3,4); |
| 52 | test_inv("scale", m0); |
| 53 | m0.postTranslate(5, 6, 7); |
| 54 | test_inv("postTranslate", m0); |
| 55 | m0.setScale(2,3,4); |
| 56 | m1.setTranslate(5, 6, 7); |
| 57 | m0.setConcat(m0, m1); |
| 58 | test_inv("postTranslate2", m0); |
| 59 | m0.setScale(2,3,4); |
| 60 | m0.preTranslate(5, 6, 7); |
| 61 | test_inv("preTranslate", m0); |
| 62 | |
| 63 | m0.setScale(2, 4, 6); |
| 64 | m0.postScale(SkDoubleToMScalar(0.5)); |
| 65 | test_inv("scale/postscale to 1,2,3", m0); |
reed@android.com | b5d0f7b | 2010-02-24 22:08:58 +0000 | [diff] [blame] | 66 | |
| 67 | m0.reset(); |
| 68 | test_map(1, 0, 0, m0, 1, 0, 0); |
| 69 | test_map(0, 1, 0, m0, 0, 1, 0); |
| 70 | test_map(0, 0, 1, m0, 0, 0, 1); |
| 71 | m0.setScale(2, 3, 4); |
| 72 | test_map(1, 0, 0, m0, 2, 0, 0); |
| 73 | test_map(0, 1, 0, m0, 0, 3, 0); |
| 74 | test_map(0, 0, 1, m0, 0, 0, 4); |
| 75 | m0.setTranslate(2, 3, 4); |
| 76 | test_map(0, 0, 0, m0, 2, 3, 4); |
| 77 | m0.preScale(5, 6, 7); |
| 78 | test_map(1, 0, 0, m0, 7, 3, 4); |
| 79 | test_map(0, 1, 0, m0, 2, 9, 4); |
| 80 | test_map(0, 0, 1, m0, 2, 3, 11); |
reed@android.com | c8c49c5 | 2010-02-24 15:36:57 +0000 | [diff] [blame] | 81 | } |
| 82 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 85 | static void dump_layers(const SkLayer* layer, int tab = 0) { |
| 86 | SkMatrix matrix; |
| 87 | SkString matrixStr; |
| 88 | |
| 89 | layer->getLocalTransform(&matrix); |
| 90 | matrix.toDumpString(&matrixStr); |
| 91 | |
| 92 | for (int j = 0; j < tab; j++) { |
| 93 | SkDebugf(" "); |
| 94 | } |
| 95 | SkDebugf("layer=%p parent=%p size=[%g %g] transform=%s\n", |
| 96 | layer, layer->getParent(), layer->getWidth(), layer->getHeight(), |
| 97 | matrixStr.c_str()); |
| 98 | for (int i = 0; i < layer->countChildren(); i++) { |
| 99 | dump_layers(layer->getChild(i), tab + 4); |
| 100 | } |
| 101 | } |
| 102 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 103 | class TestLayer : public SkLayer { |
| 104 | public: |
| 105 | TestLayer(SkColor c) : fColor(c) {} |
| 106 | |
| 107 | protected: |
| 108 | virtual void onDraw(SkCanvas* canvas, SkScalar opacity) { |
| 109 | SkRect r; |
| 110 | r.set(0, 0, this->getWidth(), this->getHeight()); |
| 111 | |
| 112 | SkPaint paint; |
| 113 | paint.setColor(fColor); |
| 114 | paint.setAlpha(SkScalarRound(opacity * 255)); |
| 115 | |
| 116 | canvas->drawRect(r, paint); |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | SkColor fColor; |
| 121 | }; |
| 122 | |
| 123 | class SkLayerView : public SkView { |
| 124 | private: |
| 125 | SkLayer* fRootLayer; |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 126 | SkLayer* fLastChild; |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 127 | public: |
| 128 | SkLayerView() { |
reed@android.com | c8c49c5 | 2010-02-24 15:36:57 +0000 | [diff] [blame] | 129 | test44(); |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 130 | static const int W = 600; |
| 131 | static const int H = 440; |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 132 | static const struct { |
| 133 | int fWidth; |
| 134 | int fHeight; |
| 135 | SkColor fColor; |
| 136 | int fPosX; |
| 137 | int fPosY; |
| 138 | } gData[] = { |
| 139 | { 120, 80, SK_ColorRED, 0, 0 }, |
| 140 | { 120, 80, SK_ColorGREEN, W - 120, 0 }, |
| 141 | { 120, 80, SK_ColorBLUE, 0, H - 80 }, |
| 142 | { 120, 80, SK_ColorMAGENTA, W - 120, H - 80 }, |
| 143 | }; |
| 144 | |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 145 | fRootLayer = new TestLayer(0xFFDDDDDD); |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 146 | fRootLayer->setSize(W, H); |
| 147 | for (size_t i = 0; i < SK_ARRAY_COUNT(gData); i++) { |
| 148 | SkLayer* child = new TestLayer(gData[i].fColor); |
| 149 | child->setSize(gData[i].fWidth, gData[i].fHeight); |
| 150 | child->setPosition(gData[i].fPosX, gData[i].fPosY); |
| 151 | fRootLayer->addChild(child)->unref(); |
| 152 | } |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 153 | |
| 154 | SkLayer* child = new TestLayer(0xFFDD8844); |
| 155 | child->setSize(120, 80); |
| 156 | child->setPosition(fRootLayer->getWidth()/2 - child->getWidth()/2, |
| 157 | fRootLayer->getHeight()/2 - child->getHeight()/2); |
| 158 | child->setAnchorPoint(SK_ScalarHalf, SK_ScalarHalf); |
| 159 | { |
| 160 | SkMatrix m; |
| 161 | m.setRotate(SkIntToScalar(30)); |
| 162 | child->setMatrix(m); |
| 163 | } |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 164 | fLastChild = child; |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 165 | fRootLayer->addChild(child)->unref(); |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 166 | |
| 167 | if (false) { |
| 168 | SkMatrix matrix; |
| 169 | matrix.setScale(0.5, 0.5); |
| 170 | fRootLayer->setMatrix(matrix); |
| 171 | } |
| 172 | |
reed@android.com | b5d0f7b | 2010-02-24 22:08:58 +0000 | [diff] [blame] | 173 | // dump_layers(fRootLayer); |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | virtual ~SkLayerView() { |
| 177 | SkSafeUnref(fRootLayer); |
| 178 | } |
| 179 | |
| 180 | protected: |
| 181 | // overrides from SkEventSink |
| 182 | virtual bool onQuery(SkEvent* evt) { |
| 183 | if (SampleCode::TitleQ(*evt)) { |
| 184 | SampleCode::TitleR(evt, "SkLayer"); |
| 185 | return true; |
| 186 | } |
| 187 | return this->INHERITED::onQuery(evt); |
| 188 | } |
| 189 | |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 190 | virtual void onDraw(SkCanvas* canvas) { |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 191 | canvas->drawColor(SK_ColorWHITE); |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 192 | |
reed@android.com | 2bd703b | 2010-02-24 02:01:23 +0000 | [diff] [blame] | 193 | canvas->translate(20, 20); |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 194 | fRootLayer->draw(canvas); |
reed@android.com | 745bfbd | 2010-02-24 17:16:35 +0000 | [diff] [blame] | 195 | |
| 196 | // visual test of getLocalTransform |
| 197 | if (true) { |
| 198 | SkMatrix matrix; |
| 199 | fLastChild->localToGlobal(&matrix); |
| 200 | SkPaint paint; |
| 201 | paint.setStyle(SkPaint::kStroke_Style); |
| 202 | paint.setStrokeWidth(5); |
| 203 | paint.setColor(0x88FF0000); |
| 204 | canvas->concat(matrix); |
| 205 | canvas->drawRect(SkRect::MakeSize(fLastChild->getSize()), paint); |
| 206 | } |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 207 | } |
| 208 | |
reed@android.com | da6fb32 | 2010-02-19 21:41:30 +0000 | [diff] [blame] | 209 | private: |
| 210 | typedef SkView INHERITED; |
| 211 | }; |
| 212 | |
| 213 | /////////////////////////////////////////////////////////////////////////////// |
| 214 | |
| 215 | static SkView* MyFactory() { return new SkLayerView; } |
| 216 | static SkViewRegister reg(MyFactory); |
| 217 | |