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

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: No more templates 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
21 void Transform::SetRotate(float degree) { 29 void Transform::SetRotate(float degree) {
22 matrix_.setRotate(SkFloatToScalar(degree)); 30 matrix_.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree));
23 } 31 }
24 32
25 void Transform::SetScaleX(float x) { 33 void Transform::SetScaleX(float x) {
26 matrix_.setScaleX(SkFloatToScalar(x)); 34 matrix_.setScale(
35 SkFloatToScalar(x),
36 matrix_.get(1,1),
37 matrix_.get(2,2));
27 } 38 }
28 39
29 void Transform::SetScaleY(float y) { 40 void Transform::SetScaleY(float y) {
30 matrix_.setScaleY(SkFloatToScalar(y)); 41 matrix_.setScale(
42 matrix_.get(0,0),
43 SkFloatToScalar(y),
44 matrix_.get(2,2));
31 } 45 }
32 46
33 void Transform::SetScale(float x, float y) { 47 void Transform::SetScale(float x, float y) {
34 matrix_.setScale(SkFloatToScalar(x), SkFloatToScalar(y)); 48 matrix_.setScale(
49 SkFloatToScalar(x),
50 SkFloatToScalar(y),
51 matrix_.get(2, 2));
35 } 52 }
36 53
37 void Transform::SetTranslateX(float x) { 54 void Transform::SetTranslateX(float x) {
38 matrix_.setTranslateX(SkFloatToScalar(x)); 55 matrix_.setTranslate(
56 SkFloatToScalar(x),
57 matrix_.get(1,3),
58 matrix_.get(2,3));
39 } 59 }
40 60
41 void Transform::SetTranslateY(float y) { 61 void Transform::SetTranslateY(float y) {
42 matrix_.setTranslateY(SkFloatToScalar(y)); 62 matrix_.setTranslate(
63 matrix_.get(0,3),
64 SkFloatToScalar(y),
65 matrix_.get(2,3));
43 } 66 }
44 67
45 void Transform::SetTranslate(float x, float y) { 68 void Transform::SetTranslate(float x, float y) {
46 matrix_.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); 69 matrix_.setTranslate(
70 SkFloatToScalar(x),
71 SkFloatToScalar(y),
72 matrix_.get(2, 3));
47 } 73 }
48 74
49 void Transform::ConcatRotate(float degree) { 75 void Transform::ConcatRotate(float degree) {
50 matrix_.postRotate(SkFloatToScalar(degree)); 76 SkMatrix44 rot;
77 rot.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree));
78 matrix_.postConcat(rot);
51 } 79 }
52 80
53 void Transform::ConcatScale(float x, float y) { 81 void Transform::ConcatScale(float x, float y) {
54 matrix_.postScale(SkFloatToScalar(x), SkFloatToScalar(y)); 82 SkMatrix44 scale;
83 scale.setScale(SkFloatToScalar(x), SkFloatToScalar(y), 1);
84 matrix_.postConcat(scale);
55 } 85 }
56 86
57 void Transform::ConcatTranslate(float x, float y) { 87 void Transform::ConcatTranslate(float x, float y) {
58 matrix_.postTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); 88 SkMatrix44 translate;
89 translate.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y), 0);
90 matrix_.postConcat(translate);
59 } 91 }
60 92
61 bool Transform::PreconcatTransform(const Transform& transform) { 93 void Transform::PreconcatTransform(const Transform& transform) {
62 return matrix_.setConcat(matrix_, transform.matrix_); 94 if (!transform.matrix_.isIdentity()) {
95 matrix_.preConcat(transform.matrix_);
96 }
63 } 97 }
64 98
65 bool Transform::ConcatTransform(const Transform& transform) { 99 void Transform::ConcatTransform(const Transform& transform) {
66 return matrix_.setConcat(transform.matrix_, matrix_); 100 if (!transform.matrix_.isIdentity()) {
101 matrix_.postConcat(transform.matrix_);
102 }
67 } 103 }
68 104
69 bool Transform::HasChange() const { 105 bool Transform::HasChange() const {
70 return !matrix_.isIdentity(); 106 return !matrix_.isIdentity();
71 } 107 }
72 108
73 bool Transform::TransformPoint(gfx::Point* point) { 109 bool Transform::TransformRect(gfx::Rect* rect) const {
sky 2011/06/17 18:43:41 position of functions in .cc should match that of
74 SkPoint skp;
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 }
80
81 bool Transform::TransformPointReverse(gfx::Point* point) {
82 SkMatrix inverse;
83 // TODO(sad): Try to avoid trying to invert the matrix.
84 if (matrix_.invert(&inverse)) {
85 SkPoint skp;
86 inverse.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp);
87 point->SetPoint(static_cast<int>(std::floor(skp.fX)),
88 static_cast<int>(std::floor(skp.fY)));
89 return true;
90 }
91 return false;
92 }
93
94 bool Transform::TransformRect(gfx::Rect* rect) {
95 SkRect src = gfx::RectToSkRect(*rect); 110 SkRect src = gfx::RectToSkRect(*rect);
96 if (!matrix_.mapRect(&src)) 111 const SkMatrix& matrix = matrix_;
112 if (!matrix.mapRect(&src))
97 return false; 113 return false;
98 *rect = gfx::SkRectToRect(src); 114 *rect = gfx::SkRectToRect(src);
99 return true; 115 return true;
100 } 116 }
101 117
102 bool Transform::TransformRectReverse(gfx::Rect* rect) { 118 bool Transform::TransformRectReverse(gfx::Rect* rect) const {
103 SkMatrix inverse; 119 SkMatrix44 inverse;
104 if (!matrix_.invert(&inverse)) 120 if (!matrix_.invert(&inverse))
105 return false; 121 return false;
106 122 const SkMatrix& matrix = inverse;
107 SkRect src = gfx::RectToSkRect(*rect); 123 SkRect src = gfx::RectToSkRect(*rect);
108 if (!inverse.mapRect(&src)) 124 if (!matrix.mapRect(&src))
109 return false; 125 return false;
110 *rect = gfx::SkRectToRect(src); 126 *rect = gfx::SkRectToRect(src);
111 return true; 127 return true;
112 } 128 }
113 129
130 bool Transform::TransformPoint(gfx::Point& point) const {
131 TransformPointInternal(matrix_, point);
132 return true;
sky 2011/06/17 18:43:41 For the ones that always return true, make them vo
133 }
134
135 bool Transform::TransformPoint(gfx::Point3f& point) const {
136 TransformPointInternal(matrix_, point);
137 return true;
138 }
139
140 bool Transform::TransformPointReverse(gfx::Point& point) const {
141 // TODO(sad): Try to avoid trying to invert the matrix.
142 SkMatrix44 inverse;
143 if (!matrix_.invert(&inverse))
144 return false;
145
146 TransformPointInternal(inverse, point);
147 return true;
148 }
149
150 bool Transform::TransformPointReverse(gfx::Point3f& point) const {
151 // TODO(sad): Try to avoid trying to invert the matrix.
152 SkMatrix44 inverse;
153 if (!matrix_.invert(&inverse))
154 return false;
155
156 TransformPointInternal(inverse, point);
157 return true;
158 }
159
160 void Transform::TransformPointInternal(const SkMatrix44& xform,
161 gfx::Point3f& point) const {
162 SkScalar p[4] = {
163 point.x(),
164 point.y(),
165 point.z(),
166 1 };
167
168 xform.map(p);
169
170 if (p[3] != 1 && abs(p[3]) > 0) {
171 point.SetPoint(p[0]/p[3], p[1]/p[3], p[2]/p[3]);
172 } else {
173 point.SetPoint(p[0], p[1], p[2]);
174 }
175 }
176
177 void Transform::TransformPointInternal(const SkMatrix44& xform,
178 gfx::Point& point) const {
179 SkScalar p[4] = {
180 SkIntToScalar(point.x()),
181 SkIntToScalar(point.y()),
182 0,
183 1 };
184
185 xform.map(p);
186
187 point.SetPoint(SymmetricRound(p[0]),
188 SymmetricRound(p[1]));
189 }
190
114 } // namespace ui 191 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698