Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: ui/gfx/transform.cc

Issue 7044062: Use SkMatrix44 for the underlying implementation of ui::Transform (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Layer animator updates Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/transform.h" 5 #include "ui/gfx/transform.h"
6 6 #include "ui/gfx/point3.h"
7 #include <cmath>
8
9 #include "ui/gfx/point.h"
10 #include "ui/gfx/rect.h" 7 #include "ui/gfx/rect.h"
11 #include "ui/gfx/skia_util.h" 8 #include "ui/gfx/skia_util.h"
12 9
10 namespace {
11
12 static int SymmetricRound(float x) {
13 return static_cast<int>(
14 x > 0
15 ? std::floor(x + 0.5f)
16 : std::ceil(x - 0.5f));
17 }
18
19 } // anonymous namespace
20
13 namespace ui { 21 namespace ui {
14 22
15 Transform::Transform() { 23 Transform::Transform() {
16 matrix_.reset(); 24 matrix_.reset();
17 } 25 }
18 26
19 Transform::~Transform() {} 27 Transform::~Transform() {}
20 28
29 bool Transform::operator==(const Transform& rhs) const {
30 return matrix_ == rhs.matrix_;
31 }
32
33 bool Transform::operator!=(const Transform& rhs) const {
34 return !(*this == rhs);
35 }
36
21 void Transform::SetRotate(float degree) { 37 void Transform::SetRotate(float degree) {
22 matrix_.setRotate(SkFloatToScalar(degree)); 38 matrix_.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree));
23 } 39 }
24 40
25 void Transform::SetScaleX(float x) { 41 void Transform::SetScaleX(float x) {
26 matrix_.setScaleX(SkFloatToScalar(x)); 42 matrix_.set(0, 0, SkFloatToScalar(x));
27 } 43 }
28 44
29 void Transform::SetScaleY(float y) { 45 void Transform::SetScaleY(float y) {
30 matrix_.setScaleY(SkFloatToScalar(y)); 46 matrix_.set(1, 1, SkFloatToScalar(y));
31 } 47 }
32 48
33 void Transform::SetScale(float x, float y) { 49 void Transform::SetScale(float x, float y) {
34 matrix_.setScale(SkFloatToScalar(x), SkFloatToScalar(y)); 50 matrix_.setScale(
51 SkFloatToScalar(x),
52 SkFloatToScalar(y),
53 matrix_.get(2, 2));
35 } 54 }
36 55
37 void Transform::SetTranslateX(float x) { 56 void Transform::SetTranslateX(float x) {
38 matrix_.setTranslateX(SkFloatToScalar(x)); 57 matrix_.set(0, 3, SkFloatToScalar(x));
39 } 58 }
40 59
41 void Transform::SetTranslateY(float y) { 60 void Transform::SetTranslateY(float y) {
42 matrix_.setTranslateY(SkFloatToScalar(y)); 61 matrix_.set(1, 3, SkFloatToScalar(y));
43 } 62 }
44 63
45 void Transform::SetTranslate(float x, float y) { 64 void Transform::SetTranslate(float x, float y) {
46 matrix_.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); 65 matrix_.setTranslate(
66 SkFloatToScalar(x),
67 SkFloatToScalar(y),
68 matrix_.get(2, 3));
47 } 69 }
48 70
49 void Transform::ConcatRotate(float degree) { 71 void Transform::ConcatRotate(float degree) {
50 matrix_.postRotate(SkFloatToScalar(degree)); 72 SkMatrix44 rot;
73 rot.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree));
74 matrix_.postConcat(rot);
51 } 75 }
52 76
53 void Transform::ConcatScale(float x, float y) { 77 void Transform::ConcatScale(float x, float y) {
54 matrix_.postScale(SkFloatToScalar(x), SkFloatToScalar(y)); 78 SkMatrix44 scale;
79 scale.setScale(SkFloatToScalar(x), SkFloatToScalar(y), 1);
80 matrix_.postConcat(scale);
55 } 81 }
56 82
57 void Transform::ConcatTranslate(float x, float y) { 83 void Transform::ConcatTranslate(float x, float y) {
58 matrix_.postTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); 84 SkMatrix44 translate;
85 translate.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y), 0);
86 matrix_.postConcat(translate);
59 } 87 }
60 88
61 bool Transform::PreconcatTransform(const Transform& transform) { 89 void Transform::PreconcatTransform(const Transform& transform) {
62 return matrix_.setConcat(matrix_, transform.matrix_); 90 if (!transform.matrix_.isIdentity()) {
91 matrix_.preConcat(transform.matrix_);
92 }
63 } 93 }
64 94
65 bool Transform::ConcatTransform(const Transform& transform) { 95 void Transform::ConcatTransform(const Transform& transform) {
66 return matrix_.setConcat(transform.matrix_, matrix_); 96 if (!transform.matrix_.isIdentity()) {
97 matrix_.postConcat(transform.matrix_);
98 }
67 } 99 }
68 100
69 bool Transform::HasChange() const { 101 bool Transform::HasChange() const {
70 return !matrix_.isIdentity(); 102 return !matrix_.isIdentity();
71 } 103 }
72 104
73 bool Transform::TransformPoint(gfx::Point* point) const { 105 void Transform::TransformPoint(gfx::Point& point) const {
74 SkPoint skp; 106 TransformPointInternal(matrix_, point);
75 matrix_.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); 107 }
76 point->SetPoint(static_cast<int>(std::floor(skp.fX)), 108
77 static_cast<int>(std::floor(skp.fY))); 109 void Transform::TransformPoint(gfx::Point3f& point) const {
110 TransformPointInternal(matrix_, point);
111 }
112
113 bool Transform::TransformPointReverse(gfx::Point& point) const {
114 // TODO(sad): Try to avoid trying to invert the matrix.
115 SkMatrix44 inverse;
116 if (!matrix_.invert(&inverse))
117 return false;
118
119 TransformPointInternal(inverse, point);
78 return true; 120 return true;
79 } 121 }
80 122
81 bool Transform::TransformPointReverse(gfx::Point* point) const { 123 bool Transform::TransformPointReverse(gfx::Point3f& point) const {
82 SkMatrix inverse;
83 // TODO(sad): Try to avoid trying to invert the matrix. 124 // TODO(sad): Try to avoid trying to invert the matrix.
84 if (matrix_.invert(&inverse)) { 125 SkMatrix44 inverse;
85 SkPoint skp; 126 if (!matrix_.invert(&inverse))
86 inverse.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); 127 return false;
87 point->SetPoint(static_cast<int>(std::floor(skp.fX)), 128
88 static_cast<int>(std::floor(skp.fY))); 129 TransformPointInternal(inverse, point);
89 return true; 130 return true;
90 }
91 return false;
92 } 131 }
93 132
94 bool Transform::TransformRect(gfx::Rect* rect) const { 133 void Transform::TransformRect(gfx::Rect* rect) const {
95 SkRect src = gfx::RectToSkRect(*rect); 134 SkRect src = gfx::RectToSkRect(*rect);
96 if (!matrix_.mapRect(&src)) 135 const SkMatrix& matrix = matrix_;
136 matrix.mapRect(&src);
137 *rect = gfx::SkRectToRect(src);
138 }
139
140 bool Transform::TransformRectReverse(gfx::Rect* rect) const {
141 SkMatrix44 inverse;
142 if (!matrix_.invert(&inverse))
97 return false; 143 return false;
144 const SkMatrix& matrix = inverse;
145 SkRect src = gfx::RectToSkRect(*rect);
146 matrix.mapRect(&src);
98 *rect = gfx::SkRectToRect(src); 147 *rect = gfx::SkRectToRect(src);
99 return true; 148 return true;
100 } 149 }
101 150
102 bool Transform::TransformRectReverse(gfx::Rect* rect) const { 151 void Transform::TransformPointInternal(const SkMatrix44& xform,
103 SkMatrix inverse; 152 gfx::Point3f& point) const {
104 if (!matrix_.invert(&inverse)) 153 SkScalar p[4] = {
105 return false; 154 point.x(),
sky 2011/06/24 20:58:19 Do these need to be wrapped in SkFloatToScalar()?
155 point.y(),
156 point.z(),
157 1 };
106 158
107 SkRect src = gfx::RectToSkRect(*rect); 159 xform.map(p);
108 if (!inverse.mapRect(&src)) 160
109 return false; 161 if (p[3] != 1 && abs(p[3]) > 0) {
110 *rect = gfx::SkRectToRect(src); 162 point.SetPoint(p[0]/p[3], p[1]/p[3], p[2]/p[3]);
sky 2011/06/24 20:58:19 nit: add whitespace before/after the '/'
111 return true; 163 } else {
164 point.SetPoint(p[0], p[1], p[2]);
165 }
166 }
167
168 void Transform::TransformPointInternal(const SkMatrix44& xform,
169 gfx::Point& point) const {
170 SkScalar p[4] = {
171 SkIntToScalar(point.x()),
172 SkIntToScalar(point.y()),
173 0,
174 1 };
175
176 xform.map(p);
177
178 point.SetPoint(SymmetricRound(p[0]),
179 SymmetricRound(p[1]));
112 } 180 }
113 181
114 } // namespace ui 182 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698