OLD | NEW |
---|---|
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 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "ui/gfx/point.h" | 9 #include "ui/gfx/point.h" |
10 #include "ui/gfx/rect.h" | 10 #include "ui/gfx/rect.h" |
11 #include "ui/gfx/skia_util.h" | 11 #include "ui/gfx/skia_util.h" |
12 | 12 |
13 namespace { | |
14 // Should be in a standard header | |
15 static inline int SymmetricRound(float x) { | |
sky
2011/06/09 17:20:29
don't indent this.
| |
16 return static_cast<int>( | |
17 x > 0.0f | |
18 ? std::floor(x + 0.5f) | |
19 : std::ceil(x - 0.5f)); | |
20 } | |
21 } | |
22 | |
13 namespace ui { | 23 namespace ui { |
14 | 24 |
15 Transform::Transform() { | 25 Transform::Transform() |
26 : hasInverse_(false) { | |
16 matrix_.reset(); | 27 matrix_.reset(); |
28 inverse_.reset(); | |
17 } | 29 } |
18 | 30 |
19 Transform::~Transform() {} | 31 Transform::~Transform() {} |
20 | 32 |
21 void Transform::SetRotate(float degree) { | 33 void Transform::SetRotate(float degree) { |
22 matrix_.setRotate(SkFloatToScalar(degree)); | 34 matrix_.setRotateDegreesAbout( |
reed1
2011/06/09 19:22:47
I think this can just be
matrix.setRotateDegreesA
| |
35 static_cast<SkMScalar>(0.0), | |
36 static_cast<SkMScalar>(0.0), | |
37 static_cast<SkMScalar>(1.0), | |
38 SkFloatToScalar(degree)); | |
39 hasInverse_ = false; | |
23 } | 40 } |
24 | 41 |
25 void Transform::SetScaleX(float x) { | 42 void Transform::SetScaleX(float x) { |
26 matrix_.setScaleX(SkFloatToScalar(x)); | 43 matrix_.setScale( |
reed1
2011/06/09 19:22:47
For this and SetScaleY,Z, see my no-need-to-cast c
| |
44 SkFloatToScalar(x), | |
45 static_cast<SkMScalar>(1.0), | |
46 static_cast<SkMScalar>(1.0)); | |
wjmaclean
2011/06/09 17:20:58
Should this be altering other scales?
| |
47 hasInverse_ = false; | |
27 } | 48 } |
28 | 49 |
29 void Transform::SetScaleY(float y) { | 50 void Transform::SetScaleY(float y) { |
30 matrix_.setScaleY(SkFloatToScalar(y)); | 51 matrix_.setScale( |
52 static_cast<SkMScalar>(1.0), | |
53 SkFloatToScalar(y), | |
54 static_cast<SkMScalar>(1.0)); | |
55 hasInverse_ = false; | |
31 } | 56 } |
32 | 57 |
33 void Transform::SetScale(float x, float y) { | 58 void Transform::SetScale(float x, float y) { |
34 matrix_.setScale(SkFloatToScalar(x), SkFloatToScalar(y)); | 59 matrix_.setScale( |
60 SkFloatToScalar(x), | |
61 SkFloatToScalar(y), | |
62 static_cast<SkMScalar>(1.0)); | |
63 hasInverse_ = false; | |
35 } | 64 } |
36 | 65 |
37 void Transform::SetTranslateX(float x) { | 66 void Transform::SetTranslateX(float x) { |
38 matrix_.setTranslateX(SkFloatToScalar(x)); | 67 matrix_.setTranslate( |
68 SkFloatToScalar(x), | |
69 static_cast<SkMScalar>(0.0), | |
70 static_cast<SkMScalar>(0.0)); | |
wjmaclean
2011/06/09 17:20:58
Again, should this alter existing Ty components?
| |
71 hasInverse_ = false; | |
39 } | 72 } |
40 | 73 |
41 void Transform::SetTranslateY(float y) { | 74 void Transform::SetTranslateY(float y) { |
42 matrix_.setTranslateY(SkFloatToScalar(y)); | 75 matrix_.setTranslate( |
76 static_cast<SkMScalar>(0.0), | |
77 SkFloatToScalar(y), | |
78 static_cast<SkMScalar>(0.0)); | |
79 hasInverse_ = false; | |
43 } | 80 } |
44 | 81 |
45 void Transform::SetTranslate(float x, float y) { | 82 void Transform::SetTranslate(float x, float y) { |
46 matrix_.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); | 83 matrix_.setTranslate( |
84 SkFloatToScalar(x), | |
85 SkFloatToScalar(y), | |
86 static_cast<SkMScalar>(0.0)); | |
87 hasInverse_ = false; | |
47 } | 88 } |
48 | 89 |
49 void Transform::ConcatRotate(float degree) { | 90 void Transform::ConcatRotate(float degree) { |
50 matrix_.postRotate(SkFloatToScalar(degree)); | 91 SkMatrix44 rot; |
92 rot.reset(); | |
93 rot.setRotateDegreesAbout( | |
94 static_cast<SkMScalar>(0.0), | |
95 static_cast<SkMScalar>(0.0), | |
96 static_cast<SkMScalar>(1.0), | |
97 SkFloatToScalar(degree)); | |
98 matrix_.postConcat(rot); | |
99 hasInverse_ = false; | |
51 } | 100 } |
52 | 101 |
53 void Transform::ConcatScale(float x, float y) { | 102 void Transform::ConcatScale(float x, float y) { |
54 matrix_.postScale(SkFloatToScalar(x), SkFloatToScalar(y)); | 103 SkMatrix44 scale; |
104 scale.reset(); | |
105 scale.setScale( | |
106 SkFloatToScalar(x), | |
107 SkFloatToScalar(y), | |
108 static_cast<SkMScalar>(1.0)); | |
109 matrix_.postConcat(scale); | |
110 hasInverse_ = false; | |
55 } | 111 } |
56 | 112 |
57 void Transform::ConcatTranslate(float x, float y) { | 113 void Transform::ConcatTranslate(float x, float y) { |
58 matrix_.postTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); | 114 SkMatrix44 translate; |
reed1
2011/06/09 19:22:47
setTranslate() by definition always overwrites the
| |
115 translate.reset(); | |
116 translate.setTranslate( | |
117 SkFloatToScalar(x), | |
118 SkFloatToScalar(y), | |
119 static_cast<SkMScalar>(0.0)); | |
120 matrix_.postConcat(translate); | |
121 hasInverse_ = false; | |
59 } | 122 } |
60 | 123 |
61 bool Transform::PreconcatTransform(const Transform& transform) { | 124 void Transform::PreconcatTransform(const Transform& transform) { |
62 return matrix_.setConcat(matrix_, transform.matrix_); | 125 if (!transform.matrix_.isIdentity()) { |
126 matrix_.preConcat(transform.matrix_); | |
127 hasInverse_ = false; | |
128 } | |
63 } | 129 } |
64 | 130 |
65 bool Transform::ConcatTransform(const Transform& transform) { | 131 void Transform::ConcatTransform(const Transform& transform) { |
66 return matrix_.setConcat(transform.matrix_, matrix_); | 132 if (!transform.matrix_.isIdentity()) { |
133 matrix_.postConcat(transform.matrix_); | |
134 hasInverse_ = false; | |
135 } | |
67 } | 136 } |
68 | 137 |
69 bool Transform::HasChange() const { | 138 bool Transform::HasChange() const { |
70 return !matrix_.isIdentity(); | 139 return !matrix_.isIdentity(); |
71 } | 140 } |
72 | 141 |
73 bool Transform::TransformPoint(gfx::Point* point) { | 142 void Transform::TransformPoint(gfx::Point* point) { |
74 SkPoint skp; | 143 TransformPointInternal(matrix_, point); |
75 matrix_.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); | |
76 point->SetPoint(static_cast<int>(std::floor(skp.fX)), | |
77 static_cast<int>(std::floor(skp.fY))); | |
78 return true; | |
79 } | 144 } |
80 | 145 |
81 bool Transform::TransformPointReverse(gfx::Point* point) { | 146 bool Transform::TransformPointReverse(gfx::Point* point) { |
82 SkMatrix inverse; | |
83 // TODO(sad): Try to avoid trying to invert the matrix. | 147 // TODO(sad): Try to avoid trying to invert the matrix. |
84 if (matrix_.invert(&inverse)) { | 148 if (!ComputeInverse()) |
85 SkPoint skp; | 149 return false; |
86 inverse.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); | 150 |
87 point->SetPoint(static_cast<int>(std::floor(skp.fX)), | 151 TransformPointInternal(inverse_, point); |
88 static_cast<int>(std::floor(skp.fY))); | 152 return true; |
89 return true; | |
90 } | |
91 return false; | |
92 } | 153 } |
93 | 154 |
94 bool Transform::TransformRect(gfx::Rect* rect) { | 155 bool Transform::TransformRect(gfx::Rect* rect) { |
95 SkRect src = gfx::RectToSkRect(*rect); | 156 SkRect src = gfx::RectToSkRect(*rect); |
96 if (!matrix_.mapRect(&src)) | 157 SkMatrix matrix = matrix_; |
158 if (!matrix.mapRect(&src)) | |
97 return false; | 159 return false; |
98 *rect = gfx::SkRectToRect(src); | 160 *rect = gfx::SkRectToRect(src); |
99 return true; | 161 return true; |
100 } | 162 } |
101 | 163 |
102 bool Transform::TransformRectReverse(gfx::Rect* rect) { | 164 bool Transform::TransformRectReverse(gfx::Rect* rect) { |
103 SkMatrix inverse; | 165 if (!ComputeInverse()) |
104 if (!matrix_.invert(&inverse)) | |
105 return false; | 166 return false; |
106 | 167 SkMatrix matrix = inverse_; |
107 SkRect src = gfx::RectToSkRect(*rect); | 168 SkRect src = gfx::RectToSkRect(*rect); |
108 if (!inverse.mapRect(&src)) | 169 if (!matrix.mapRect(&src)) |
109 return false; | 170 return false; |
110 *rect = gfx::SkRectToRect(src); | 171 *rect = gfx::SkRectToRect(src); |
111 return true; | 172 return true; |
112 } | 173 } |
113 | 174 |
175 void Transform::TransformPointInternal(const SkMatrix44& xform, | |
176 gfx::Point* point) { | |
177 SkScalar p[4] = { | |
178 SkIntToScalar(point->x()), | |
179 SkIntToScalar(point->y()), | |
180 static_cast<SkScalar>(0), | |
181 static_cast<SkScalar>(1) | |
182 }; | |
183 | |
184 xform.map(p); | |
185 point->SetPoint(SymmetricRound(p[0]), | |
sky
2011/06/09 17:20:29
Why do we want SymmetricRound and not floor?
| |
186 SymmetricRound(p[1])); | |
187 } | |
188 | |
189 bool Transform::ComputeInverse() { | |
190 if (!hasInverse_) { | |
sky
2011/06/09 17:20:29
Why do we bother storing the inverse?
| |
191 hasInverse_ = matrix_.invert(&inverse_); | |
192 } | |
193 return hasInverse_; | |
194 } | |
195 | |
114 } // namespace ui | 196 } // namespace ui |
OLD | NEW |