Index: ui/gfx/transform.h |
diff --git a/ui/gfx/transform.h b/ui/gfx/transform.h |
index 3fdfc3235a36c79eab4508d7481e9c05e8a83278..10309939f84dc47f99b641c036a76ede38a761e9 100644 |
--- a/ui/gfx/transform.h |
+++ b/ui/gfx/transform.h |
@@ -6,20 +6,22 @@ |
#define UI_GFX_TRANSFORM_H_ |
#pragma once |
+#include "base/template_util.h" |
#include "base/basictypes.h" |
#include "base/compiler_specific.h" |
-#include "third_party/skia/include/core/SkMatrix.h" |
+#include "third_party/skia/include/utils/SkMatrix44.h" |
+#include "ui/gfx/point.h" |
+ |
+#include <cmath> |
namespace gfx { |
-class Point; |
class Rect; |
} |
namespace ui { |
-// 3x3 transformation matrix. Transform is cheap and explicitly allows |
+// 4x4 transformation matrix. Transform is cheap and explicitly allows |
// copy/assign. |
-// TODO: make this a 4x4. |
class Transform { |
public: |
Transform(); |
@@ -57,43 +59,112 @@ class Transform { |
void ConcatTranslate(float x, float y); |
// Applies a transformation on the current transformation |
- // (i.e. 'this = this * transform;'). Returns true if the result can be |
- // represented. |
- bool PreconcatTransform(const Transform& transform); |
+ // (i.e. 'this = this * transform;'). |
+ void PreconcatTransform(const Transform& transform); |
// Applies a transformation on the current transformation |
- // (i.e. 'this = transform * this;'). Returns true if the result can be |
- // represented. |
- bool ConcatTransform(const Transform& transform); |
+ // (i.e. 'this = transform * this;'). |
+ void ConcatTransform(const Transform& transform); |
// Does the transformation change anything? |
bool HasChange() const; |
// Applies the transformation on the point. Returns true if the point is |
// transformed successfully. |
- bool TransformPoint(gfx::Point* point); |
+ template <class T> |
+ bool TransformPoint(T& point) const; |
- // Applies the reverse transformation on the point. Returns true if the point |
- // is transformed successfully. |
- bool TransformPointReverse(gfx::Point* point); |
+ // Applies the reverse transformation on the point. Returns true if the |
+ // transformation can be inverted |
+ template <class T> |
+ bool TransformPointReverse(T& point) const; |
// Applies transformation on the rectangle. Returns true if the rectangle is |
// transformed successfully. |
- bool TransformRect(gfx::Rect* rect); |
+ bool TransformRect(gfx::Rect* rect) const; |
// Applies the reverse transformation on the rectangle. Returns true if the |
// rectangle is transformed successfully. |
- bool TransformRectReverse(gfx::Rect* rect); |
+ bool TransformRectReverse(gfx::Rect* rect) const; |
// Returns the underlying matrix. |
- const SkMatrix& matrix() const { return matrix_; } |
+ const SkMatrix44& matrix() const { return matrix_; } |
private: |
- SkMatrix matrix_; |
+ template <typename T> |
+ void TransformPointInternal(const SkMatrix44& xform, T& point) const; |
+ |
+ template <typename T> |
+ static int SymmetricRound(T value); |
+ |
+ SkMatrix44 matrix_; |
// copy/assign are allowed. |
}; |
-} // namespace ui |
+template <class T> |
+bool Transform::TransformPoint(T& point) const { |
+ TransformPointInternal(matrix_, point); |
+ return true; |
+} |
+ |
+template <class T> |
+bool Transform::TransformPointReverse(T& point) const { |
+ // TODO(sad): Try to avoid trying to invert the matrix. |
+ SkMatrix44 inverse; |
+ if (!matrix_.invert(&inverse)) |
+ return false; |
+ |
+ TransformPointInternal(inverse, point); |
+ return true; |
+} |
+ |
+template <typename T> |
+void Transform::TransformPointInternal(const SkMatrix44& xform, |
+ T& point) const { |
+ SkScalar p[4] = { |
+ point[0], |
+ point[1], |
+ point[2], |
+ 1 }; |
+ |
+ xform.map(p); |
+ |
+ if (p[3] != 1 && abs(p[3]) > 0) { |
+ point[0] = p[0] / p[3]; |
+ point[1] = p[1] / p[3]; |
+ point[2] = p[2] / p[3]; |
+ } else { |
+ point[0] = p[0]; |
+ point[1] = p[1]; |
+ point[2] = p[2]; |
+ } |
+} |
+ |
+template <> |
+inline void Transform::TransformPointInternal<gfx::Point>( |
+ 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])); |
+} |
+ |
+template <typename T> |
+int Transform::SymmetricRound(T x) { |
+ return static_cast<int>( |
+ x > 0.0f |
+ ? std::floor(x + 0.5f) |
+ : std::ceil(x - 0.5f)); |
+} |
+ |
+}// namespace ui |
#endif // UI_GFX_TRANSFORM_H_ |