Chromium Code Reviews| Index: ui/gfx/transform.cc |
| diff --git a/ui/gfx/transform.cc b/ui/gfx/transform.cc |
| index 03e2873d27c5ca1e795c3d4c5049854bf87c0c42..e5a3df4d4be2221c59ad4e2dbdefc04462968019 100644 |
| --- a/ui/gfx/transform.cc |
| +++ b/ui/gfx/transform.cc |
| @@ -3,13 +3,21 @@ |
| // found in the LICENSE file. |
| #include "ui/gfx/transform.h" |
| - |
| -#include <cmath> |
| - |
| -#include "ui/gfx/point.h" |
| +#include "ui/gfx/point3.h" |
| #include "ui/gfx/rect.h" |
| #include "ui/gfx/skia_util.h" |
| +namespace { |
| + |
| +static int SymmetricRound(float x) { |
| + return static_cast<int>( |
| + x > 0 |
| + ? std::floor(x + 0.5f) |
| + : std::ceil(x - 0.5f)); |
| +} |
| + |
| +} // anonymous namespace |
| + |
| namespace ui { |
| Transform::Transform() { |
| @@ -19,96 +27,165 @@ Transform::Transform() { |
| Transform::~Transform() {} |
| void Transform::SetRotate(float degree) { |
| - matrix_.setRotate(SkFloatToScalar(degree)); |
| + matrix_.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); |
| } |
| void Transform::SetScaleX(float x) { |
| - matrix_.setScaleX(SkFloatToScalar(x)); |
| + matrix_.setScale( |
| + SkFloatToScalar(x), |
| + matrix_.get(1,1), |
| + matrix_.get(2,2)); |
| } |
| void Transform::SetScaleY(float y) { |
| - matrix_.setScaleY(SkFloatToScalar(y)); |
| + matrix_.setScale( |
| + matrix_.get(0,0), |
| + SkFloatToScalar(y), |
| + matrix_.get(2,2)); |
| } |
| void Transform::SetScale(float x, float y) { |
| - matrix_.setScale(SkFloatToScalar(x), SkFloatToScalar(y)); |
| + matrix_.setScale( |
| + SkFloatToScalar(x), |
| + SkFloatToScalar(y), |
| + matrix_.get(2, 2)); |
| } |
| void Transform::SetTranslateX(float x) { |
| - matrix_.setTranslateX(SkFloatToScalar(x)); |
| + matrix_.setTranslate( |
| + SkFloatToScalar(x), |
| + matrix_.get(1,3), |
| + matrix_.get(2,3)); |
| } |
| void Transform::SetTranslateY(float y) { |
| - matrix_.setTranslateY(SkFloatToScalar(y)); |
| + matrix_.setTranslate( |
| + matrix_.get(0,3), |
| + SkFloatToScalar(y), |
| + matrix_.get(2,3)); |
| } |
| void Transform::SetTranslate(float x, float y) { |
| - matrix_.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); |
| + matrix_.setTranslate( |
| + SkFloatToScalar(x), |
| + SkFloatToScalar(y), |
| + matrix_.get(2, 3)); |
| } |
| void Transform::ConcatRotate(float degree) { |
| - matrix_.postRotate(SkFloatToScalar(degree)); |
| + SkMatrix44 rot; |
| + rot.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); |
| + matrix_.postConcat(rot); |
| } |
| void Transform::ConcatScale(float x, float y) { |
| - matrix_.postScale(SkFloatToScalar(x), SkFloatToScalar(y)); |
| + SkMatrix44 scale; |
| + scale.setScale(SkFloatToScalar(x), SkFloatToScalar(y), 1); |
| + matrix_.postConcat(scale); |
| } |
| void Transform::ConcatTranslate(float x, float y) { |
| - matrix_.postTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); |
| + SkMatrix44 translate; |
| + translate.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y), 0); |
| + matrix_.postConcat(translate); |
| } |
| -bool Transform::PreconcatTransform(const Transform& transform) { |
| - return matrix_.setConcat(matrix_, transform.matrix_); |
| +void Transform::PreconcatTransform(const Transform& transform) { |
| + if (!transform.matrix_.isIdentity()) { |
| + matrix_.preConcat(transform.matrix_); |
| + } |
| } |
| -bool Transform::ConcatTransform(const Transform& transform) { |
| - return matrix_.setConcat(transform.matrix_, matrix_); |
| +void Transform::ConcatTransform(const Transform& transform) { |
| + if (!transform.matrix_.isIdentity()) { |
| + matrix_.postConcat(transform.matrix_); |
| + } |
| } |
| bool Transform::HasChange() const { |
| return !matrix_.isIdentity(); |
| } |
| -bool Transform::TransformPoint(gfx::Point* point) { |
| - SkPoint skp; |
| - matrix_.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); |
| - point->SetPoint(static_cast<int>(std::floor(skp.fX)), |
| - static_cast<int>(std::floor(skp.fY))); |
| +bool Transform::TransformRect(gfx::Rect* rect) const { |
|
sky
2011/06/17 18:43:41
position of functions in .cc should match that of
|
| + SkRect src = gfx::RectToSkRect(*rect); |
| + const SkMatrix& matrix = matrix_; |
| + if (!matrix.mapRect(&src)) |
| + return false; |
| + *rect = gfx::SkRectToRect(src); |
| return true; |
| } |
| -bool Transform::TransformPointReverse(gfx::Point* point) { |
| - SkMatrix inverse; |
| - // TODO(sad): Try to avoid trying to invert the matrix. |
| - if (matrix_.invert(&inverse)) { |
| - SkPoint skp; |
| - inverse.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); |
| - point->SetPoint(static_cast<int>(std::floor(skp.fX)), |
| - static_cast<int>(std::floor(skp.fY))); |
| - return true; |
| - } |
| - return false; |
| -} |
| - |
| -bool Transform::TransformRect(gfx::Rect* rect) { |
| +bool Transform::TransformRectReverse(gfx::Rect* rect) const { |
| + SkMatrix44 inverse; |
| + if (!matrix_.invert(&inverse)) |
| + return false; |
| + const SkMatrix& matrix = inverse; |
| SkRect src = gfx::RectToSkRect(*rect); |
| - if (!matrix_.mapRect(&src)) |
| + if (!matrix.mapRect(&src)) |
| return false; |
| *rect = gfx::SkRectToRect(src); |
| return true; |
| } |
| -bool Transform::TransformRectReverse(gfx::Rect* rect) { |
| - SkMatrix inverse; |
| +bool Transform::TransformPoint(gfx::Point& point) const { |
| + TransformPointInternal(matrix_, point); |
| + return true; |
|
sky
2011/06/17 18:43:41
For the ones that always return true, make them vo
|
| +} |
| + |
| +bool Transform::TransformPoint(gfx::Point3f& point) const { |
| + TransformPointInternal(matrix_, point); |
| + return true; |
| +} |
| + |
| +bool Transform::TransformPointReverse(gfx::Point& point) const { |
| + // TODO(sad): Try to avoid trying to invert the matrix. |
| + SkMatrix44 inverse; |
| if (!matrix_.invert(&inverse)) |
| return false; |
| - SkRect src = gfx::RectToSkRect(*rect); |
| - if (!inverse.mapRect(&src)) |
| + TransformPointInternal(inverse, point); |
| + return true; |
| +} |
| + |
| +bool Transform::TransformPointReverse(gfx::Point3f& point) const { |
| + // TODO(sad): Try to avoid trying to invert the matrix. |
| + SkMatrix44 inverse; |
| + if (!matrix_.invert(&inverse)) |
| return false; |
| - *rect = gfx::SkRectToRect(src); |
| + |
| + TransformPointInternal(inverse, point); |
| return true; |
| } |
| +void Transform::TransformPointInternal(const SkMatrix44& xform, |
| + gfx::Point3f& point) const { |
| + SkScalar p[4] = { |
| + point.x(), |
| + point.y(), |
| + point.z(), |
| + 1 }; |
| + |
| + xform.map(p); |
| + |
| + if (p[3] != 1 && abs(p[3]) > 0) { |
| + point.SetPoint(p[0]/p[3], p[1]/p[3], p[2]/p[3]); |
| + } else { |
| + point.SetPoint(p[0], p[1], p[2]); |
| + } |
| +} |
| + |
| +void Transform::TransformPointInternal(const SkMatrix44& xform, |
| + gfx::Point& point) const { |
| + SkScalar p[4] = { |
| + SkIntToScalar(point.x()), |
| + SkIntToScalar(point.y()), |
| + 0, |
| + 1 }; |
| + |
| + xform.map(p); |
| + |
| + point.SetPoint(SymmetricRound(p[0]), |
| + SymmetricRound(p[1])); |
| +} |
| + |
| } // namespace ui |