reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | /* libs/graphics/animator/SkMatrixParts.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "SkMatrixParts.h" |
| 19 | #include "SkAnimateMaker.h" |
| 20 | #include "SkDrawMatrix.h" |
| 21 | #include "SkDrawRectangle.h" |
| 22 | #include "SkDrawPath.h" |
| 23 | |
| 24 | SkMatrixPart::SkMatrixPart() : fMatrix(NULL) { |
| 25 | } |
| 26 | |
| 27 | void SkMatrixPart::dirty() { |
| 28 | fMatrix->dirty(); |
| 29 | } |
| 30 | |
| 31 | SkDisplayable* SkMatrixPart::getParent() const { |
| 32 | return fMatrix; |
| 33 | } |
| 34 | |
| 35 | bool SkMatrixPart::setParent(SkDisplayable* parent) { |
| 36 | SkASSERT(parent != NULL); |
| 37 | if (parent->isMatrix() == false) |
| 38 | return true; |
| 39 | fMatrix = (SkDrawMatrix*) parent; |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | #if SK_USE_CONDENSED_INFO == 0 |
| 45 | |
| 46 | const SkMemberInfo SkRotate::fInfo[] = { |
| 47 | SK_MEMBER(center, Point), |
| 48 | SK_MEMBER(degrees, Float) |
| 49 | }; |
| 50 | |
| 51 | #endif |
| 52 | |
| 53 | DEFINE_GET_MEMBER(SkRotate); |
| 54 | |
| 55 | SkRotate::SkRotate() : degrees(0) { |
| 56 | center.fX = center.fY = 0; |
| 57 | } |
| 58 | |
| 59 | bool SkRotate::add() { |
| 60 | fMatrix->rotate(degrees, center); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | #if SK_USE_CONDENSED_INFO == 0 |
| 66 | |
| 67 | const SkMemberInfo SkScale::fInfo[] = { |
| 68 | SK_MEMBER(center, Point), |
| 69 | SK_MEMBER(x, Float), |
| 70 | SK_MEMBER(y, Float) |
| 71 | }; |
| 72 | |
| 73 | #endif |
| 74 | |
| 75 | DEFINE_GET_MEMBER(SkScale); |
| 76 | |
| 77 | SkScale::SkScale() : x(SK_Scalar1), y(SK_Scalar1) { |
| 78 | center.fX = center.fY = 0; |
| 79 | } |
| 80 | |
| 81 | bool SkScale::add() { |
| 82 | fMatrix->scale(x, y, center); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | #if SK_USE_CONDENSED_INFO == 0 |
| 88 | |
| 89 | const SkMemberInfo SkSkew::fInfo[] = { |
| 90 | SK_MEMBER(center, Point), |
| 91 | SK_MEMBER(x, Float), |
| 92 | SK_MEMBER(y, Float) |
| 93 | }; |
| 94 | |
| 95 | #endif |
| 96 | |
| 97 | DEFINE_GET_MEMBER(SkSkew); |
| 98 | |
| 99 | SkSkew::SkSkew() : x(0), y(0) { |
| 100 | center.fX = center.fY = 0; |
| 101 | } |
| 102 | |
| 103 | bool SkSkew::add() { |
| 104 | fMatrix->skew(x, y, center); |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | #if SK_USE_CONDENSED_INFO == 0 |
| 110 | |
| 111 | const SkMemberInfo SkTranslate::fInfo[] = { |
| 112 | SK_MEMBER(x, Float), |
| 113 | SK_MEMBER(y, Float) |
| 114 | }; |
| 115 | |
| 116 | #endif |
| 117 | |
| 118 | DEFINE_GET_MEMBER(SkTranslate); |
| 119 | |
| 120 | SkTranslate::SkTranslate() : x(0), y(0) { |
| 121 | } |
| 122 | |
| 123 | bool SkTranslate::add() { |
| 124 | fMatrix->translate(x, y); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | #if SK_USE_CONDENSED_INFO == 0 |
| 130 | |
| 131 | const SkMemberInfo SkFromPath::fInfo[] = { |
| 132 | SK_MEMBER(mode, FromPathMode), |
| 133 | SK_MEMBER(offset, Float), |
| 134 | SK_MEMBER(path, Path) |
| 135 | }; |
| 136 | |
| 137 | #endif |
| 138 | |
| 139 | DEFINE_GET_MEMBER(SkFromPath); |
| 140 | |
| 141 | SkFromPath::SkFromPath() : |
| 142 | mode(0), offset(0), path(NULL) { |
| 143 | } |
| 144 | |
| 145 | SkFromPath::~SkFromPath() { |
| 146 | } |
| 147 | |
| 148 | bool SkFromPath::add() { |
| 149 | if (path == NULL) |
| 150 | return true; |
| 151 | static const uint8_t gFlags[] = { |
| 152 | SkPathMeasure::kGetPosAndTan_MatrixFlag, // normal |
| 153 | SkPathMeasure::kGetTangent_MatrixFlag, // angle |
| 154 | SkPathMeasure::kGetPosition_MatrixFlag // position |
| 155 | }; |
| 156 | if ((unsigned)mode >= SK_ARRAY_COUNT(gFlags)) |
| 157 | return true; |
| 158 | SkMatrix result; |
| 159 | fPathMeasure.setPath(&path->getPath(), false); |
| 160 | if (fPathMeasure.getMatrix(offset, &result, (SkPathMeasure::MatrixFlags)gFlags[mode])) |
| 161 | fMatrix->set(result); |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | #if SK_USE_CONDENSED_INFO == 0 |
| 167 | |
| 168 | const SkMemberInfo SkRectToRect::fInfo[] = { |
| 169 | SK_MEMBER(destination, Rect), |
| 170 | SK_MEMBER(source, Rect) |
| 171 | }; |
| 172 | |
| 173 | #endif |
| 174 | |
| 175 | DEFINE_GET_MEMBER(SkRectToRect); |
| 176 | |
| 177 | SkRectToRect::SkRectToRect() : |
| 178 | source(NULL), destination(NULL) { |
| 179 | } |
| 180 | |
| 181 | SkRectToRect::~SkRectToRect() { |
| 182 | } |
| 183 | |
| 184 | bool SkRectToRect::add() { |
| 185 | if (source == NULL || destination == NULL) |
| 186 | return true; |
| 187 | SkMatrix temp; |
| 188 | temp.setRectToRect(source->fRect, destination->fRect, |
| 189 | SkMatrix::kFill_ScaleToFit); |
| 190 | fMatrix->set(temp); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | #ifdef SK_DUMP_ENABLED |
| 195 | void SkRectToRect::dump(SkAnimateMaker* maker) { |
| 196 | dumpBase(maker); |
| 197 | SkDebugf("/>\n"); |
| 198 | SkDisplayList::fIndent += 4; |
| 199 | if (source) { |
| 200 | SkDebugf("%*s<source>\n", SkDisplayList::fIndent, ""); |
| 201 | SkDisplayList::fIndent += 4; |
| 202 | source->dump(maker); |
| 203 | SkDisplayList::fIndent -= 4; |
| 204 | SkDebugf("%*s</source>\n", SkDisplayList::fIndent, ""); |
| 205 | } |
| 206 | if (destination) { |
| 207 | SkDebugf("%*s<destination>\n", SkDisplayList::fIndent, ""); |
| 208 | SkDisplayList::fIndent += 4; |
| 209 | destination->dump(maker); |
| 210 | SkDisplayList::fIndent -= 4; |
| 211 | SkDebugf("%*s</destination>\n", SkDisplayList::fIndent, ""); |
| 212 | } |
| 213 | SkDisplayList::fIndent -= 4; |
| 214 | dumpEnd(maker); |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | const SkMemberInfo* SkRectToRect::preferredChild(SkDisplayTypes ) { |
| 219 | if (source == NULL) |
| 220 | return getMember("source"); // !!! cwap! need to refer to member through enum like kScope instead |
| 221 | else { |
| 222 | SkASSERT(destination == NULL); |
| 223 | return getMember("destination"); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | |
| 228 | #if SK_USE_CONDENSED_INFO == 0 |
| 229 | |
| 230 | const SkMemberInfo SkPolyToPoly::fInfo[] = { |
| 231 | SK_MEMBER(destination, Polygon), |
| 232 | SK_MEMBER(source, Polygon) |
| 233 | }; |
| 234 | |
| 235 | #endif |
| 236 | |
| 237 | DEFINE_GET_MEMBER(SkPolyToPoly); |
| 238 | |
| 239 | SkPolyToPoly::SkPolyToPoly() : source(NULL), destination(NULL) { |
| 240 | } |
| 241 | |
| 242 | SkPolyToPoly::~SkPolyToPoly() { |
| 243 | } |
| 244 | |
| 245 | bool SkPolyToPoly::add() { |
| 246 | SkASSERT(source); |
| 247 | SkASSERT(destination); |
| 248 | SkPoint src[4]; |
| 249 | SkPoint dst[4]; |
| 250 | SkPath& sourcePath = source->getPath(); |
| 251 | int srcPts = sourcePath.getPoints(src, 4); |
| 252 | SkPath& destPath = destination->getPath(); |
| 253 | int dstPts = destPath.getPoints(dst, 4); |
| 254 | if (srcPts != dstPts) |
| 255 | return true; |
| 256 | SkMatrix temp; |
| 257 | temp.setPolyToPoly(src, dst, srcPts); |
| 258 | fMatrix->set(temp); |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | #ifdef SK_DUMP_ENABLED |
| 263 | void SkPolyToPoly::dump(SkAnimateMaker* maker) { |
| 264 | dumpBase(maker); |
| 265 | SkDebugf("/>\n"); |
| 266 | SkDisplayList::fIndent += 4; |
| 267 | if (source) { |
| 268 | SkDebugf("%*s<source>\n", SkDisplayList::fIndent, ""); |
| 269 | SkDisplayList::fIndent += 4; |
| 270 | source->dump(maker); |
| 271 | SkDisplayList::fIndent -= 4; |
| 272 | SkDebugf("%*s</source>\n", SkDisplayList::fIndent, ""); |
| 273 | } |
| 274 | if (destination) { |
| 275 | SkDebugf("%*s<destination>\n", SkDisplayList::fIndent, ""); |
| 276 | SkDisplayList::fIndent += 4; |
| 277 | destination->dump(maker); |
| 278 | SkDisplayList::fIndent -= 4; |
| 279 | SkDebugf("%*s</destination>\n", SkDisplayList::fIndent, ""); |
| 280 | } |
| 281 | SkDisplayList::fIndent -= 4; |
| 282 | dumpEnd(maker); |
| 283 | } |
| 284 | #endif |
| 285 | |
| 286 | void SkPolyToPoly::onEndElement(SkAnimateMaker& ) { |
| 287 | SkASSERT(source); |
| 288 | SkASSERT(destination); |
| 289 | if (source->childHasID() || destination->childHasID()) |
| 290 | fMatrix->setChildHasID(); |
| 291 | } |
| 292 | |
| 293 | const SkMemberInfo* SkPolyToPoly::preferredChild(SkDisplayTypes ) { |
| 294 | if (source == NULL) |
| 295 | return getMember("source"); // !!! cwap! need to refer to member through enum like kScope instead |
| 296 | else { |
| 297 | SkASSERT(destination == NULL); |
| 298 | return getMember("destination"); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | |