Chromium Code Reviews| 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 | |
| 15 // Should be in a standard header | |
| 16 static inline int SymmetricRound(float x) { | |
| 17 return static_cast<int>( | |
| 18 x > 0.0f | |
| 19 ? std::floor(x + 0.5f) | |
| 20 : std::ceil(x - 0.5f)); | |
| 21 } | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 13 namespace ui { | 25 namespace ui { |
| 14 | 26 |
| 15 Transform::Transform() { | 27 Transform::Transform() |
| 28 : hasInverse_(false) { | |
| 16 matrix_.reset(); | 29 matrix_.reset(); |
| 30 inverse_.reset(); | |
| 17 } | 31 } |
| 18 | 32 |
| 19 Transform::~Transform() {} | 33 Transform::~Transform() {} |
| 20 | 34 |
| 21 void Transform::SetRotate(float degree) { | 35 void Transform::SetRotate(float degree) { |
| 22 matrix_.setRotate(SkFloatToScalar(degree)); | 36 matrix_.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); |
| 37 hasInverse_ = false; | |
| 23 } | 38 } |
| 24 | 39 |
| 25 void Transform::SetScaleX(float x) { | 40 void Transform::SetScaleX(float x) { |
| 26 matrix_.setScaleX(SkFloatToScalar(x)); | 41 matrix_.setScale( |
| 42 SkFloatToScalar(x), | |
| 43 matrix_.get(1,1), | |
| 44 matrix_.get(2,2)); | |
| 45 hasInverse_ = false; | |
| 27 } | 46 } |
| 28 | 47 |
| 29 void Transform::SetScaleY(float y) { | 48 void Transform::SetScaleY(float y) { |
| 30 matrix_.setScaleY(SkFloatToScalar(y)); | 49 matrix_.setScale( |
| 50 matrix_.get(0,0), | |
| 51 SkFloatToScalar(y), | |
| 52 matrix_.get(2,2)); | |
| 53 hasInverse_ = false; | |
| 31 } | 54 } |
| 32 | 55 |
| 33 void Transform::SetScale(float x, float y) { | 56 void Transform::SetScale(float x, float y) { |
| 34 matrix_.setScale(SkFloatToScalar(x), SkFloatToScalar(y)); | 57 matrix_.setScale( |
| 58 SkFloatToScalar(x), | |
| 59 SkFloatToScalar(y), | |
| 60 matrix_.get(2, 2)); | |
| 61 hasInverse_ = false; | |
| 35 } | 62 } |
| 36 | 63 |
| 37 void Transform::SetTranslateX(float x) { | 64 void Transform::SetTranslateX(float x) { |
| 38 matrix_.setTranslateX(SkFloatToScalar(x)); | 65 matrix_.setTranslate( |
| 66 SkFloatToScalar(x), | |
| 67 matrix_.get(1,3), | |
| 68 matrix_.get(2,3)); | |
| 69 hasInverse_ = false; | |
| 39 } | 70 } |
| 40 | 71 |
| 41 void Transform::SetTranslateY(float y) { | 72 void Transform::SetTranslateY(float y) { |
| 42 matrix_.setTranslateY(SkFloatToScalar(y)); | 73 matrix_.setTranslate( |
| 74 matrix_.get(0,3), | |
| 75 SkFloatToScalar(y), | |
| 76 matrix_.get(2,3)); | |
| 77 hasInverse_ = false; | |
| 43 } | 78 } |
| 44 | 79 |
| 45 void Transform::SetTranslate(float x, float y) { | 80 void Transform::SetTranslate(float x, float y) { |
| 46 matrix_.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); | 81 matrix_.setTranslate( |
| 82 SkFloatToScalar(x), | |
| 83 SkFloatToScalar(y), | |
| 84 matrix_.get(2, 3)); | |
| 85 hasInverse_ = false; | |
| 47 } | 86 } |
| 48 | 87 |
| 49 void Transform::ConcatRotate(float degree) { | 88 void Transform::ConcatRotate(float degree) { |
| 50 matrix_.postRotate(SkFloatToScalar(degree)); | 89 SkMatrix44 rot; |
|
rjkroege
2011/06/10 17:52:14
rot()? Make it clear you're invoking the default c
| |
| 90 rot.reset(); | |
| 91 rot.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); | |
| 92 matrix_.postConcat(rot); | |
| 93 hasInverse_ = false; | |
| 51 } | 94 } |
| 52 | 95 |
| 53 void Transform::ConcatScale(float x, float y) { | 96 void Transform::ConcatScale(float x, float y) { |
| 54 matrix_.postScale(SkFloatToScalar(x), SkFloatToScalar(y)); | 97 SkMatrix44 scale; |
| 98 scale.reset(); | |
| 99 scale.setScale(SkFloatToScalar(x), SkFloatToScalar(y), 1); | |
| 100 matrix_.postConcat(scale); | |
| 101 hasInverse_ = false; | |
| 55 } | 102 } |
| 56 | 103 |
| 57 void Transform::ConcatTranslate(float x, float y) { | 104 void Transform::ConcatTranslate(float x, float y) { |
| 58 matrix_.postTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); | 105 SkMatrix44 translate; |
| 106 translate.reset(); | |
| 107 translate.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y), 0); | |
| 108 matrix_.postConcat(translate); | |
| 109 hasInverse_ = false; | |
| 59 } | 110 } |
| 60 | 111 |
| 61 bool Transform::PreconcatTransform(const Transform& transform) { | 112 void Transform::PreconcatTransform(const Transform& transform) { |
| 62 return matrix_.setConcat(matrix_, transform.matrix_); | 113 if (!transform.matrix_.isIdentity()) { |
| 114 matrix_.preConcat(transform.matrix_); | |
| 115 hasInverse_ = false; | |
| 116 } | |
| 63 } | 117 } |
| 64 | 118 |
| 65 bool Transform::ConcatTransform(const Transform& transform) { | 119 void Transform::ConcatTransform(const Transform& transform) { |
| 66 return matrix_.setConcat(transform.matrix_, matrix_); | 120 if (!transform.matrix_.isIdentity()) { |
| 121 matrix_.postConcat(transform.matrix_); | |
| 122 hasInverse_ = false; | |
| 123 } | |
| 67 } | 124 } |
| 68 | 125 |
| 69 bool Transform::HasChange() const { | 126 bool Transform::HasChange() const { |
| 70 return !matrix_.isIdentity(); | 127 return !matrix_.isIdentity(); |
| 71 } | 128 } |
| 72 | 129 |
| 73 bool Transform::TransformPoint(gfx::Point* point) { | 130 void Transform::TransformPoint(gfx::Point* point) { |
| 74 SkPoint skp; | 131 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 } | 132 } |
| 80 | 133 |
| 81 bool Transform::TransformPointReverse(gfx::Point* point) { | 134 bool Transform::TransformPointReverse(gfx::Point* point) { |
| 82 SkMatrix inverse; | |
| 83 // TODO(sad): Try to avoid trying to invert the matrix. | 135 // TODO(sad): Try to avoid trying to invert the matrix. |
| 84 if (matrix_.invert(&inverse)) { | 136 if (!ComputeInverse()) |
| 85 SkPoint skp; | 137 return false; |
| 86 inverse.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); | 138 |
| 87 point->SetPoint(static_cast<int>(std::floor(skp.fX)), | 139 TransformPointInternal(inverse_, point); |
| 88 static_cast<int>(std::floor(skp.fY))); | 140 return true; |
| 89 return true; | |
| 90 } | |
| 91 return false; | |
| 92 } | 141 } |
| 93 | 142 |
| 94 bool Transform::TransformRect(gfx::Rect* rect) { | 143 bool Transform::TransformRect(gfx::Rect* rect) { |
| 95 SkRect src = gfx::RectToSkRect(*rect); | 144 SkRect src = gfx::RectToSkRect(*rect); |
| 96 if (!matrix_.mapRect(&src)) | 145 SkMatrix matrix = matrix_; |
|
rjkroege
2011/06/10 17:52:14
does this invoke the copy constructor? does it nee
| |
| 146 if (!matrix.mapRect(&src)) | |
| 97 return false; | 147 return false; |
| 98 *rect = gfx::SkRectToRect(src); | 148 *rect = gfx::SkRectToRect(src); |
| 99 return true; | 149 return true; |
| 100 } | 150 } |
| 101 | 151 |
| 102 bool Transform::TransformRectReverse(gfx::Rect* rect) { | 152 bool Transform::TransformRectReverse(gfx::Rect* rect) { |
| 103 SkMatrix inverse; | 153 if (!ComputeInverse()) |
| 104 if (!matrix_.invert(&inverse)) | |
| 105 return false; | 154 return false; |
| 106 | 155 SkMatrix matrix = inverse_; |
| 107 SkRect src = gfx::RectToSkRect(*rect); | 156 SkRect src = gfx::RectToSkRect(*rect); |
| 108 if (!inverse.mapRect(&src)) | 157 if (!matrix.mapRect(&src)) |
| 109 return false; | 158 return false; |
| 110 *rect = gfx::SkRectToRect(src); | 159 *rect = gfx::SkRectToRect(src); |
| 111 return true; | 160 return true; |
| 112 } | 161 } |
| 113 | 162 |
| 163 void Transform::TransformPointInternal(const SkMatrix44& xform, | |
| 164 gfx::Point* point) { | |
| 165 SkScalar p[4] = { | |
| 166 SkIntToScalar(point->x()), | |
| 167 SkIntToScalar(point->y()), | |
| 168 0, | |
| 169 1 }; | |
| 170 | |
| 171 xform.map(p); | |
| 172 point->SetPoint(SymmetricRound(p[0]), | |
| 173 SymmetricRound(p[1])); | |
| 174 } | |
| 175 | |
| 176 bool Transform::ComputeInverse() { | |
| 177 if (!hasInverse_) { | |
| 178 hasInverse_ = matrix_.invert(&inverse_); | |
| 179 } | |
| 180 return hasInverse_; | |
| 181 } | |
| 182 | |
| 114 } // namespace ui | 183 } // namespace ui |
| OLD | NEW |