diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-17 15:29:51 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-17 15:29:51 +0000 |
commit | b9b1e7a4fa49c108c40536cee59ce0b2b0a09d86 (patch) | |
tree | b8d8f83119b439d3656d540baa91baae12464f40 /ui/gfx | |
parent | 2180ba95f998a4a414d2aed0b644079739242aaa (diff) | |
download | chromium_src-b9b1e7a4fa49c108c40536cee59ce0b2b0a09d86.zip chromium_src-b9b1e7a4fa49c108c40536cee59ce0b2b0a09d86.tar.gz chromium_src-b9b1e7a4fa49c108c40536cee59ce0b2b0a09d86.tar.bz2 |
Makes Transform concrete. Fixes bug in coordinate conversion and makes all conversion routines calculate the transform in the same way. Lastly fixes bug in touch_factory.cc that was causing crashes on my machine when running views_unittests. Oh, and adds some tests of conversion methods.
BUG=none
TEST=none
R=ben@chromium.org,sadrul@chromium.org
Review URL: http://codereview.chromium.org/7033002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85635 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r-- | ui/gfx/canvas_skia.cc | 4 | ||||
-rw-r--r-- | ui/gfx/transform.cc | 103 | ||||
-rw-r--r-- | ui/gfx/transform.h | 61 | ||||
-rw-r--r-- | ui/gfx/transform_skia.cc | 108 | ||||
-rw-r--r-- | ui/gfx/transform_skia.h | 55 |
5 files changed, 141 insertions, 190 deletions
diff --git a/ui/gfx/canvas_skia.cc b/ui/gfx/canvas_skia.cc index 5177116..1cec581 100644 --- a/ui/gfx/canvas_skia.cc +++ b/ui/gfx/canvas_skia.cc @@ -12,7 +12,7 @@ #include "ui/gfx/brush.h" #include "ui/gfx/font.h" #include "ui/gfx/rect.h" -#include "ui/gfx/transform_skia.h" +#include "ui/gfx/transform.h" #if defined(OS_WIN) #include "ui/gfx/canvas_skia_paint.h" @@ -332,7 +332,7 @@ void CanvasSkia::EndPlatformPaint() { } void CanvasSkia::Transform(const ui::Transform& transform) { - concat(*reinterpret_cast<const ui::TransformSkia&>(transform).matrix_.get()); + concat(transform.matrix()); } CanvasSkia* CanvasSkia::AsCanvasSkia() { diff --git a/ui/gfx/transform.cc b/ui/gfx/transform.cc new file mode 100644 index 0000000..c84d199 --- /dev/null +++ b/ui/gfx/transform.cc @@ -0,0 +1,103 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/gfx/transform.h" + +#include "ui/gfx/point.h" +#include "ui/gfx/rect.h" +#include "ui/gfx/skia_util.h" + +namespace ui { + +Transform::Transform() { + matrix_.reset(); +} + +Transform::~Transform() {} + +void Transform::SetRotate(float degree) { + matrix_.setRotate(SkFloatToScalar(degree)); +} + +void Transform::SetScaleX(float x) { + matrix_.setScaleX(SkFloatToScalar(x)); +} + +void Transform::SetScaleY(float y) { + matrix_.setScaleY(SkFloatToScalar(y)); +} + +void Transform::SetScale(float x, float y) { + matrix_.setScale(SkFloatToScalar(x), SkFloatToScalar(y)); +} + +void Transform::SetTranslateX(float x) { + matrix_.setTranslateX(SkFloatToScalar(x)); +} + +void Transform::SetTranslateY(float y) { + matrix_.setTranslateY(SkFloatToScalar(y)); +} + +void Transform::SetTranslate(float x, float y) { + matrix_.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); +} + +void Transform::ConcatRotate(float degree) { + matrix_.postRotate(SkFloatToScalar(degree)); +} + +void Transform::ConcatScale(float x, float y) { + matrix_.postScale(SkFloatToScalar(x), SkFloatToScalar(y)); +} + +void Transform::ConcatTranslate(float x, float y) { + // Temporary workaround for bug in Skia. + ui::Transform t; + t.SetTranslate(x, y); + ConcatTransform(t); + // matrix_.postTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); +} + +bool Transform::PreconcatTransform(const Transform& transform) { + return matrix_.setConcat(transform.matrix_, matrix_); +} + +bool Transform::ConcatTransform(const Transform& transform) { + return matrix_.setConcat(matrix_, 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>(skp.fX), static_cast<int>(skp.fY)); + 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>(skp.fX), static_cast<int>(skp.fY)); + return true; + } + return false; +} + +bool Transform::TransformRect(gfx::Rect* rect) { + SkRect src = gfx::RectToSkRect(*rect); + if (!matrix_.mapRect(&src)) + return false; + gfx::Rect xrect = gfx::SkRectToRect(src); + rect->SetRect(xrect.x(), xrect.y(), xrect.width(), xrect.height()); + return true; +} + +} // namespace ui diff --git a/ui/gfx/transform.h b/ui/gfx/transform.h index ce3fb67e..1002e71 100644 --- a/ui/gfx/transform.h +++ b/ui/gfx/transform.h @@ -6,6 +6,10 @@ #define UI_GFX_TRANSFORM_H_ #pragma once +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "third_party/skia/include/core/SkMatrix.h" + namespace gfx { class Point; class Rect; @@ -13,16 +17,13 @@ class Rect; namespace ui { -// Transformation interface. -// Classes implement this interface to apply transformations (e.g. rotation, -// scaling etc.) on UI components. +// 3x3 transformation matrix. Transform is cheap and explicitly allows +// copy/assign. +// TODO: make this a 4x4. class Transform { public: - virtual ~Transform() {} - - // Creates an object that implements this interface (e.g. using skia - // transformation matrices). - static Transform* Create(); + Transform(); + ~Transform(); // NOTE: The 'Set' functions overwrite the previously set transformation // parameters. The 'Concat' functions apply a transformation (e.g. rotation, @@ -34,49 +35,59 @@ class Transform { // with the 'Set' functions. // Sets the rotation of the transformation. - virtual void SetRotate(float degree) = 0; + void SetRotate(float degree); // Sets the scaling parameters. - virtual void SetScaleX(float x) = 0; - virtual void SetScaleY(float y) = 0; - virtual void SetScale(float x, float y) = 0; + void SetScaleX(float x); + void SetScaleY(float y); + void SetScale(float x, float y); // Sets the translation parameters. - virtual void SetTranslateX(float x) = 0; - virtual void SetTranslateY(float y) = 0; - virtual void SetTranslate(float x, float y) = 0; + void SetTranslateX(float x); + void SetTranslateY(float y); + void SetTranslate(float x, float y); // Applies rotation on the current transformation. - virtual void ConcatRotate(float degree) = 0; + void ConcatRotate(float degree); // Applies scaling on current transform. - virtual void ConcatScale(float x, float y) = 0; + void ConcatScale(float x, float y); // Applies translation on current transform. - virtual void ConcatTranslate(float x, float y) = 0; + void ConcatTranslate(float x, float y); + + // Applies a transformation on the current transformation + // (i.e. 'this = transform * this;'). Returns true if the result can be + // represented. + bool PreconcatTransform(const Transform& transform); // Applies a transformation on the current transformation // (i.e. 'this = this * transform;'). Returns true if the result can be // represented. - virtual bool ConcatTransform(const Transform& transform) = 0; + bool ConcatTransform(const Transform& transform); // Does the transformation change anything? - virtual bool HasChange() const = 0; + bool HasChange() const; // Applies the transformation on the point. Returns true if the point is // transformed successfully. - virtual bool TransformPoint(gfx::Point* point) = 0; + bool TransformPoint(gfx::Point* point); // Applies the reverse transformation on the point. Returns true if the point // is transformed successfully. - virtual bool TransformPointReverse(gfx::Point* point) = 0; + bool TransformPointReverse(gfx::Point* point); // Applies transformation on the rectangle. Returns true of the rectangle is // transformed successfully. - virtual bool TransformRect(gfx::Rect* rect) = 0; + bool TransformRect(gfx::Rect* rect); + + // Returns the underlying matrix. + const SkMatrix& matrix() const { return matrix_; } + + private: + SkMatrix matrix_; - // operator=. - virtual void Copy(const Transform& transform) = 0; + // copy/assign are allowed. }; } // namespace ui diff --git a/ui/gfx/transform_skia.cc b/ui/gfx/transform_skia.cc deleted file mode 100644 index 021ede6..0000000 --- a/ui/gfx/transform_skia.cc +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "ui/gfx/transform_skia.h" - -#include "third_party/skia/include/core/SkMatrix.h" -#include "ui/gfx/canvas_skia.h" -#include "ui/gfx/point.h" -#include "ui/gfx/rect.h" -#include "ui/gfx/skia_util.h" - -namespace ui { - -// static -Transform* Transform::Create() { - return new TransformSkia(); -} - -TransformSkia::TransformSkia() { - matrix_.reset(new SkMatrix); - matrix_->reset(); -} - -TransformSkia::~TransformSkia() {} - -void TransformSkia::SetRotate(float degree) { - matrix_->setRotate(SkFloatToScalar(degree)); -} - -void TransformSkia::SetScaleX(float x) { - matrix_->setScaleX(SkFloatToScalar(x)); -} - -void TransformSkia::SetScaleY(float y) { - matrix_->setScaleY(SkFloatToScalar(y)); -} - -void TransformSkia::SetScale(float x, float y) { - matrix_->setScale(SkFloatToScalar(x), SkFloatToScalar(y)); -} - -void TransformSkia::SetTranslateX(float x) { - matrix_->setTranslateX(SkFloatToScalar(x)); -} - -void TransformSkia::SetTranslateY(float y) { - matrix_->setTranslateY(SkFloatToScalar(y)); -} - -void TransformSkia::SetTranslate(float x, float y) { - matrix_->setTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); -} - -void TransformSkia::ConcatRotate(float degree) { - matrix_->postRotate(SkFloatToScalar(degree)); -} - -void TransformSkia::ConcatScale(float x, float y) { - matrix_->postScale(SkFloatToScalar(x), SkFloatToScalar(y)); -} - -void TransformSkia::ConcatTranslate(float x, float y) { - matrix_->postTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); -} - -bool TransformSkia::ConcatTransform(const Transform& transform) { - return matrix_->setConcat(*reinterpret_cast<const TransformSkia&> - (transform).matrix_.get(), *matrix_.get()); -} - -bool TransformSkia::HasChange() const { - return !matrix_->isIdentity(); -} - -bool TransformSkia::TransformPoint(gfx::Point* point) { - SkPoint skp; - matrix_->mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); - point->SetPoint(static_cast<int>(skp.fX), static_cast<int>(skp.fY)); - return true; -} - -bool TransformSkia::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>(skp.fX), static_cast<int>(skp.fY)); - return true; - } - return false; -} - -bool TransformSkia::TransformRect(gfx::Rect* rect) { - SkRect src = gfx::RectToSkRect(*rect); - if (!matrix_->mapRect(&src)) - return false; - gfx::Rect xrect = gfx::SkRectToRect(src); - rect->SetRect(xrect.x(), xrect.y(), xrect.width(), xrect.height()); - return true; -} - -void TransformSkia::Copy(const Transform& transform) { - *matrix_ = *(static_cast<const TransformSkia&>(transform).matrix_); -} - -} // namespace ui diff --git a/ui/gfx/transform_skia.h b/ui/gfx/transform_skia.h deleted file mode 100644 index 398d1c2..0000000 --- a/ui/gfx/transform_skia.h +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef UI_GFX_TRANSFORM_SKIA_H_ -#define UI_GFX_TRANSFORM_SKIA_H_ -#pragma once - -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "base/memory/scoped_ptr.h" -#include "ui/gfx/transform.h" - -class SkMatrix; - -namespace gfx { -class CanvasSkia; -} - -namespace ui { - -// Transformation using skia transformation matrices. -class TransformSkia : public Transform { - public: - TransformSkia(); - virtual ~TransformSkia(); - - // Overridden from ui::Transform - virtual void SetRotate(float degree) OVERRIDE; - virtual void SetScaleX(float x) OVERRIDE; - virtual void SetScaleY(float y) OVERRIDE; - virtual void SetScale(float x, float y) OVERRIDE; - virtual void SetTranslateX(float x) OVERRIDE; - virtual void SetTranslateY(float y) OVERRIDE; - virtual void SetTranslate(float x, float y) OVERRIDE; - virtual void ConcatRotate(float degree) OVERRIDE; - virtual void ConcatScale(float x, float y) OVERRIDE; - virtual void ConcatTranslate(float x, float y) OVERRIDE; - virtual bool ConcatTransform(const Transform& transform) OVERRIDE; - virtual bool HasChange() const OVERRIDE; - virtual bool TransformPoint(gfx::Point* point) OVERRIDE; - virtual bool TransformPointReverse(gfx::Point* point) OVERRIDE; - virtual bool TransformRect(gfx::Rect* rect) OVERRIDE; - virtual void Copy(const Transform& transform) OVERRIDE; - - private: - friend class gfx::CanvasSkia; - scoped_ptr<SkMatrix> matrix_; - - DISALLOW_COPY_AND_ASSIGN(TransformSkia); -}; - -} // namespace ui - -#endif // UI_GFX_TRANSFORM_SKIA_H_ |