diff options
author | vollick@chromium.org <vollick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-26 20:13:08 +0000 |
---|---|---|
committer | vollick@chromium.org <vollick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-26 20:13:08 +0000 |
commit | f7c321eb19e1dd8a2f8b154cacb7e13788f1fc28 (patch) | |
tree | e1f1c285d359f728fd9d270267b881a836dbea0e /ui/gfx/transform.cc | |
parent | eb052c93fa488df29314bcb70bb2458982fc94c8 (diff) | |
download | chromium_src-f7c321eb19e1dd8a2f8b154cacb7e13788f1fc28.zip chromium_src-f7c321eb19e1dd8a2f8b154cacb7e13788f1fc28.tar.gz chromium_src-f7c321eb19e1dd8a2f8b154cacb7e13788f1fc28.tar.bz2 |
gfx::Transform API clean-up
We have too many ways to do the same thing in gfx::Transform, and their names
can lead to confusion. We have the notion of Concat-ing and Preconcat-ing.
We've borrowed this verbage from skia. a.preConcat(b) means a = a * b. This may
seem counter-intuitive, but is the correct definition if we are multiplying our
points/vectors on the right.
That said, we almost always want to pre-concat. This what is done throughout
WebKit. To simplify matters, rather than having ConcatFoo and PreconcatFoo, we
will now only have Foo which does what PreconcatFoo used to.
Furthermore, we also have SetFoo which is almost always used immediately after
a transform is created, so Foo would do fine (with the optimization mentioned
below).
Another bit of redundant code eliminated by this CL is
InterpolatedTransform::FactorTRS. This function was brittle and naive, and now
that gfx::Transform::Blend exists, it needs to go away.
Other minor changes rolled into this cleanup:
- RotateAbout now takes the newly minted Vector3dF
- The Foo functions mentioned above also check for identity to avoid
needless matrix multiplications.
BUG=159972
Review URL: https://chromiumcodereview.appspot.com/11418040
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@169476 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/transform.cc')
-rw-r--r-- | ui/gfx/transform.cc | 300 |
1 files changed, 107 insertions, 193 deletions
diff --git a/ui/gfx/transform.cc b/ui/gfx/transform.cc index 7815e8d..19b59da 100644 --- a/ui/gfx/transform.cc +++ b/ui/gfx/transform.cc @@ -45,225 +45,128 @@ bool Transform::operator!=(const Transform& rhs) const { return !(*this == rhs); } -void Transform::SetRotate(double degree) { - matrix_.setRotateDegreesAbout(SkDoubleToMScalar(0), - SkDoubleToMScalar(0), - SkDoubleToMScalar(1), - SkDoubleToMScalar(degree)); +void Transform::MakeIdentity() { + matrix_.setIdentity(); } -void Transform::SetRotateAbout(const Point3F& axis, double degree) { - matrix_.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), - SkDoubleToMScalar(axis.y()), - SkDoubleToMScalar(axis.z()), - SkDoubleToMScalar(degree)); -} - -void Transform::SetScaleX(double x) { - matrix_.setDouble(0, 0, x); -} - -void Transform::SetScaleY(double y) { - matrix_.setDouble(1, 1, y); +void Transform::Rotate(double degree) { + if (matrix_.isIdentity()) { + matrix_.setRotateDegreesAbout(SkDoubleToMScalar(0), + SkDoubleToMScalar(0), + SkDoubleToMScalar(1), + SkDoubleToMScalar(degree)); + } else { + SkMatrix44 rot; + rot.setRotateDegreesAbout(SkDoubleToMScalar(0), + SkDoubleToMScalar(0), + SkDoubleToMScalar(1), + SkDoubleToMScalar(degree)); + matrix_.preConcat(rot); + } } -void Transform::SetScaleZ(double z) { - matrix_.setDouble(2, 2, z); +void Transform::RotateAbout(const Vector3dF& axis, double degree) { + if (matrix_.isIdentity()) { + matrix_.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), + SkDoubleToMScalar(axis.y()), + SkDoubleToMScalar(axis.z()), + SkDoubleToMScalar(degree)); + } else { + SkMatrix44 rot; + rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), + SkDoubleToMScalar(axis.y()), + SkDoubleToMScalar(axis.z()), + SkDoubleToMScalar(degree)); + matrix_.preConcat(rot); + } } -void Transform::SetScale(double x, double y) { - matrix_.setScale(SkDoubleToMScalar(x), +void Transform::Scale(double x, double y) { + if (matrix_.isIdentity()) { + matrix_.setScale(SkDoubleToMScalar(x), + SkDoubleToMScalar(y), + SkDoubleToMScalar(1)); + } else { + SkMatrix44 scale; + scale.setScale(SkDoubleToMScalar(x), SkDoubleToMScalar(y), - matrix_.get(2, 2)); + SkDoubleToMScalar(1)); + matrix_.preConcat(scale); + } } -void Transform::SetScale3d(double x, double y, double z) { - matrix_.setScale(SkDoubleToMScalar(x), +void Transform::Scale3d(double x, double y, double z) { + if (matrix_.isIdentity()) { + matrix_.setScale(SkDoubleToMScalar(x), + SkDoubleToMScalar(y), + SkDoubleToMScalar(z)); + } else { + SkMatrix44 scale; + scale.setScale(SkDoubleToMScalar(x), SkDoubleToMScalar(y), SkDoubleToMScalar(z)); + matrix_.preConcat(scale); + } } -void Transform::SetTranslateX(double x) { - matrix_.setDouble(0, 3, x); -} - -void Transform::SetTranslateY(double y) { - matrix_.setDouble(1, 3, y); -} - -void Transform::SetTranslateZ(double z) { - matrix_.setDouble(2, 3, z); -} - -void Transform::SetTranslate(double x, double y) { - matrix_.setTranslate(SkDoubleToMScalar(x), - SkDoubleToMScalar(y), - matrix_.get(2, 3)); -} - -void Transform::SetTranslate3d(double x, double y, double z) { - matrix_.setTranslate(SkDoubleToMScalar(x), - SkDoubleToMScalar(y), - SkDoubleToMScalar(z)); -} - -void Transform::SetSkewX(double angle) { - matrix_.setDouble(0, 1, TanDegrees(angle)); -} - -void Transform::SetSkewY(double angle) { - matrix_.setDouble(1, 0, TanDegrees(angle)); -} - -void Transform::SetPerspectiveDepth(double depth) { - if (depth == 0) - return; - - SkMatrix44 m; - m.setDouble(3, 2, -1.0 / depth); - matrix_ = m; -} - -void Transform::ConcatRotate(double degree) { - SkMatrix44 rot; - rot.setRotateDegreesAbout(SkDoubleToMScalar(0), - SkDoubleToMScalar(0), - SkDoubleToMScalar(1), - SkDoubleToMScalar(degree)); - matrix_.postConcat(rot); -} - -void Transform::ConcatRotateAbout(const Point3F& axis, double degree) { - SkMatrix44 rot; - rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), - SkDoubleToMScalar(axis.y()), - SkDoubleToMScalar(axis.z()), - SkDoubleToMScalar(degree)); - matrix_.postConcat(rot); -} - -void Transform::ConcatScale(double x, double y) { - SkMatrix44 scale; - scale.setScale(SkDoubleToMScalar(x), - SkDoubleToMScalar(y), - SkDoubleToMScalar(1)); - matrix_.postConcat(scale); -} - -void Transform::ConcatScale3d(double x, double y, double z) { - SkMatrix44 scale; - scale.setScale(SkDoubleToMScalar(x), - SkDoubleToMScalar(y), - SkDoubleToMScalar(z)); - matrix_.postConcat(scale); -} - -void Transform::ConcatTranslate(double x, double y) { - SkMatrix44 translate; - translate.setTranslate(SkDoubleToMScalar(x), - SkDoubleToMScalar(y), - SkDoubleToMScalar(0)); - matrix_.postConcat(translate); -} - -void Transform::ConcatTranslate3d(double x, double y, double z) { - SkMatrix44 translate; - translate.setTranslate(SkDoubleToMScalar(x), - SkDoubleToMScalar(y), - SkDoubleToMScalar(z)); - matrix_.postConcat(translate); -} - -void Transform::ConcatSkewX(double angle_x) { - Transform t; - t.SetSkewX(angle_x); - matrix_.postConcat(t.matrix_); -} - -void Transform::ConcatSkewY(double angle_y) { - Transform t; - t.SetSkewY(angle_y); - matrix_.postConcat(t.matrix_); -} - -void Transform::ConcatPerspectiveDepth(double depth) { - if (depth == 0) - return; - - SkMatrix44 m; - m.setDouble(3, 2, -1.0 / depth); - matrix_.postConcat(m); -} - -void Transform::PreconcatRotate(double degree) { - SkMatrix44 rot; - rot.setRotateDegreesAbout(SkDoubleToMScalar(0), - SkDoubleToMScalar(0), - SkDoubleToMScalar(1), - SkDoubleToMScalar(degree)); - matrix_.preConcat(rot); -} - -void Transform::PreconcatRotateAbout(const Point3F& axis, double degree) { - SkMatrix44 rot; - rot.setRotateDegreesAbout(SkDoubleToMScalar(axis.x()), - SkDoubleToMScalar(axis.y()), - SkDoubleToMScalar(axis.z()), - SkDoubleToMScalar(degree)); - matrix_.preConcat(rot); -} - -void Transform::PreconcatScale(double x, double y) { - SkMatrix44 scale; - scale.setScale(SkDoubleToMScalar(x), - SkDoubleToMScalar(y), - SkDoubleToMScalar(1)); - matrix_.preConcat(scale); -} - -void Transform::PreconcatScale3d(double x, double y, double z) { - SkMatrix44 scale; - scale.setScale(SkDoubleToMScalar(x), - SkDoubleToMScalar(y), - SkDoubleToMScalar(z)); - matrix_.preConcat(scale); -} - -void Transform::PreconcatTranslate(double x, double y) { - SkMatrix44 translate; - translate.setTranslate(SkDoubleToMScalar(x), +void Transform::Translate(double x, double y) { + if (matrix_.isIdentity()) { + matrix_.setTranslate(SkDoubleToMScalar(x), SkDoubleToMScalar(y), SkDoubleToMScalar(0)); - matrix_.preConcat(translate); + } else { + SkMatrix44 translate; + translate.setTranslate(SkDoubleToMScalar(x), + SkDoubleToMScalar(y), + SkDoubleToMScalar(0)); + matrix_.preConcat(translate); + } } -void Transform::PreconcatTranslate3d(double x, double y, double z) { - SkMatrix44 translate; - translate.setTranslate(SkDoubleToMScalar(x), +void Transform::Translate3d(double x, double y, double z) { + if (matrix_.isIdentity()) { + matrix_.setTranslate(SkDoubleToMScalar(x), SkDoubleToMScalar(y), SkDoubleToMScalar(z)); - matrix_.preConcat(translate); + } else { + SkMatrix44 translate; + translate.setTranslate(SkDoubleToMScalar(x), + SkDoubleToMScalar(y), + SkDoubleToMScalar(z)); + matrix_.preConcat(translate); + } } -void Transform::PreconcatSkewX(double angle_x) { - Transform t; - t.SetSkewX(angle_x); - matrix_.preConcat(t.matrix_); +void Transform::SkewX(double angle_x) { + if (matrix_.isIdentity()) + matrix_.setDouble(0, 1, TanDegrees(angle_x)); + else { + SkMatrix44 skew; + skew.setDouble(0, 1, TanDegrees(angle_x)); + matrix_.preConcat(skew); + } } -void Transform::PreconcatSkewY(double angle_y) { - Transform t; - t.SetSkewY(angle_y); - matrix_.preConcat(t.matrix_); +void Transform::SkewY(double angle_y) { + if (matrix_.isIdentity()) + matrix_.setDouble(1, 0, TanDegrees(angle_y)); + else { + SkMatrix44 skew; + skew.setDouble(1, 0, TanDegrees(angle_y)); + matrix_.preConcat(skew); + } } -void Transform::PreconcatPerspectiveDepth(double depth) { +void Transform::ApplyPerspectiveDepth(double depth) { if (depth == 0) - return; - - SkMatrix44 m; - m.setDouble(3, 2, -1.0 / depth); - matrix_.preConcat(m); + return; + if (matrix_.isIdentity()) + matrix_.setDouble(3, 2, -1.0 / depth); + else { + SkMatrix44 m; + m.setDouble(3, 2, -1.0 / depth); + matrix_.preConcat(m); + } } void Transform::PreconcatTransform(const Transform& transform) { @@ -362,6 +265,17 @@ bool Transform::Blend(const Transform& from, double progress) { return true; } +Transform Transform::operator*(const Transform& other) const { + Transform to_return; + to_return.matrix_.setConcat(matrix_, other.matrix_); + return to_return; +} + +Transform& Transform::operator*=(const Transform& other) { + matrix_.preConcat(other.matrix_); + return *this; +} + void Transform::TransformPointInternal(const SkMatrix44& xform, Point3F& point) const { SkMScalar p[4] = { |