summaryrefslogtreecommitdiffstats
path: root/ui
diff options
context:
space:
mode:
authorsadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-24 21:47:56 +0000
committersadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-24 21:47:56 +0000
commit36df22b48817bc7fe7159e498f65b5e8b00f1605 (patch)
tree15e7aa8f769102d90479c5c59330a5309f4dbc3c /ui
parentaf5ee7f3fe7c555578bd9c0cf1290bcf6af553e7 (diff)
downloadchromium_src-36df22b48817bc7fe7159e498f65b5e8b00f1605.zip
chromium_src-36df22b48817bc7fe7159e498f65b5e8b00f1605.tar.gz
chromium_src-36df22b48817bc7fe7159e498f65b5e8b00f1605.tar.bz2
Transformable views: Use the transformation for points and events.
Added and updated API for converting points between views' coordinate systems, taking transformations into consideration. This in turn gives us, for free, transformation for located events (mouse events, touch events). BUG=none TEST=ViewTest.TransformEvent Review URL: http://codereview.chromium.org/6534015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75960 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r--ui/gfx/canvas.h9
-rw-r--r--ui/gfx/canvas_direct2d.cc6
-rw-r--r--ui/gfx/canvas_direct2d.h3
-rw-r--r--ui/gfx/canvas_skia.cc7
-rw-r--r--ui/gfx/canvas_skia.h3
-rw-r--r--ui/gfx/gfx.gyp3
-rw-r--r--ui/gfx/transform.h66
-rw-r--r--ui/gfx/transform_skia.cc102
-rw-r--r--ui/gfx/transform_skia.h54
9 files changed, 248 insertions, 5 deletions
diff --git a/ui/gfx/canvas.h b/ui/gfx/canvas.h
index 8c89d59..0149486 100644
--- a/ui/gfx/canvas.h
+++ b/ui/gfx/canvas.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// 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.
@@ -13,6 +13,10 @@
#include "skia/ext/platform_canvas.h"
#include "ui/gfx/native_widget_types.h"
+namespace ui {
+class Transform;
+}
+
namespace gfx {
class Brush;
@@ -208,6 +212,9 @@ class Canvas {
// returned by BeginPlatformPaint().
virtual void EndPlatformPaint() = 0;
+ // Apply transformation on the canvas.
+ virtual void Transform(const ui::Transform& transform) = 0;
+
// TODO(beng): remove this once we don't need to use any skia-specific methods
// through this interface.
// A quick and dirty way to obtain the underlying SkCanvas.
diff --git a/ui/gfx/canvas_direct2d.cc b/ui/gfx/canvas_direct2d.cc
index 61d3403..63e9bb9 100644
--- a/ui/gfx/canvas_direct2d.cc
+++ b/ui/gfx/canvas_direct2d.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// 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.
@@ -341,6 +341,10 @@ void CanvasDirect2D::EndPlatformPaint() {
interop_rt_.release();
}
+void CanvasDirect2D::Transform(const ui::Transform& transform) {
+ NOTIMPLEMENTED();
+}
+
CanvasSkia* CanvasDirect2D::AsCanvasSkia() {
return NULL;
}
diff --git a/ui/gfx/canvas_direct2d.h b/ui/gfx/canvas_direct2d.h
index d87fdf3..45452f5 100644
--- a/ui/gfx/canvas_direct2d.h
+++ b/ui/gfx/canvas_direct2d.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// 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.
@@ -77,6 +77,7 @@ class CanvasDirect2D : public Canvas {
int dest_x, int dest_y, int w, int h);
virtual gfx::NativeDrawingContext BeginPlatformPaint();
virtual void EndPlatformPaint();
+ virtual void Transform(const ui::Transform& transform);
virtual CanvasSkia* AsCanvasSkia();
virtual const CanvasSkia* AsCanvasSkia() const;
diff --git a/ui/gfx/canvas_skia.cc b/ui/gfx/canvas_skia.cc
index 99174d1..e61f0f2 100644
--- a/ui/gfx/canvas_skia.cc
+++ b/ui/gfx/canvas_skia.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// 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.
@@ -12,6 +12,7 @@
#include "ui/gfx/brush.h"
#include "ui/gfx/font.h"
#include "ui/gfx/rect.h"
+#include "ui/gfx/transform_skia.h"
#if defined(OS_WIN)
#include "ui/gfx/canvas_skia_paint.h"
@@ -330,6 +331,10 @@ void CanvasSkia::EndPlatformPaint() {
endPlatformPaint();
}
+void CanvasSkia::Transform(const ui::Transform& transform) {
+ concat(*reinterpret_cast<const ui::TransformSkia&>(transform).matrix_.get());
+}
+
CanvasSkia* CanvasSkia::AsCanvasSkia() {
return this;
}
diff --git a/ui/gfx/canvas_skia.h b/ui/gfx/canvas_skia.h
index be44687..854ce84 100644
--- a/ui/gfx/canvas_skia.h
+++ b/ui/gfx/canvas_skia.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// 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.
@@ -139,6 +139,7 @@ class CanvasSkia : public skia::PlatformCanvas,
int dest_x, int dest_y, int w, int h);
virtual gfx::NativeDrawingContext BeginPlatformPaint();
virtual void EndPlatformPaint();
+ virtual void Transform(const ui::Transform& transform);
virtual CanvasSkia* AsCanvasSkia();
virtual const CanvasSkia* AsCanvasSkia() const;
diff --git a/ui/gfx/gfx.gyp b/ui/gfx/gfx.gyp
index 12d368d..d72e24b 100644
--- a/ui/gfx/gfx.gyp
+++ b/ui/gfx/gfx.gyp
@@ -147,6 +147,9 @@
'skia_util.h',
'skia_utils_gtk.cc',
'skia_utils_gtk.h',
+ 'transform.h',
+ 'transform_skia.cc',
+ 'transform_skia.h',
],
'conditions': [
['OS=="win"', {
diff --git a/ui/gfx/transform.h b/ui/gfx/transform.h
new file mode 100644
index 0000000..d8f6899
--- /dev/null
+++ b/ui/gfx/transform.h
@@ -0,0 +1,66 @@
+// 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_H_
+#define UI_GFX_TRANSFORM_H_
+#pragma once
+
+namespace gfx {
+class Point;
+class Rect;
+}
+
+namespace ui {
+
+// Transformation interface.
+// Classes implement this interface to apply transformations (e.g. rotation,
+// scaling etc.) on UI components.
+class Transform {
+ public:
+ // Create an object that implements this interface (e.g. using skia
+ // transformation matrices).
+ static Transform* Create();
+
+ // Set the rotation of the transformation.
+ virtual void SetRotate(float degree) = 0;
+
+ // Set the scaling parameters.
+ virtual void SetScaleX(float x) = 0;
+ virtual void SetScaleY(float y) = 0;
+ virtual void SetScale(float x, float y) = 0;
+
+ // Set the translation parameters.
+ virtual void SetTranslateX(float x) = 0;
+ virtual void SetTranslateY(float y) = 0;
+ virtual void SetTranslate(float x, float y) = 0;
+
+ // Apply rotation on the current transformation.
+ virtual void ConcatRotate(float degree) = 0;
+
+ // Apply scaling on current transform.
+ virtual void ConcatScale(float x, float y) = 0;
+
+ // Apply translation on current transform.
+ virtual void ConcatTranslate(float x, float y) = 0;
+
+ // Apply a transformation on the current transformation
+ // (i.e. 'this = this * transform;')
+ virtual bool ConcatTransform(const Transform& transform) = 0;
+
+ // Does the transformation change anything?
+ virtual bool HasChange() const = 0;
+
+ // Apply the transformation on the point.
+ virtual bool TransformPoint(gfx::Point* point) = 0;
+
+ // Apply the reverse transformation on the point.
+ virtual bool TransformPointReverse(gfx::Point* point) = 0;
+
+ // Apply transformatino on the rectangle.
+ virtual bool TransformRect(gfx::Rect* rect) = 0;
+};
+
+} // namespace ui
+
+#endif // UI_GFX_TRANSFORM_H_
diff --git a/ui/gfx/transform_skia.cc b/ui/gfx/transform_skia.cc
new file mode 100644
index 0000000..50a7dcd
--- /dev/null
+++ b/ui/gfx/transform_skia.cc
@@ -0,0 +1,102 @@
+// 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();
+}
+
+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;
+}
+
+} // namespace ui
diff --git a/ui/gfx/transform_skia.h b/ui/gfx/transform_skia.h
new file mode 100644
index 0000000..1bd8188
--- /dev/null
+++ b/ui/gfx/transform_skia.h
@@ -0,0 +1,54 @@
+// 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/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;
+
+ private:
+ friend class gfx::CanvasSkia;
+ scoped_ptr<SkMatrix> matrix_;
+
+ DISALLOW_COPY_AND_ASSIGN(TransformSkia);
+};
+
+} // namespace ui
+
+#endif // UI_GFX_TRANSFORM_SKIA_H_