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

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

Issue 11434027: Check for identity before doing matrix math in more places in gfx::Transform (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // MSVC++ requires this to be set before any other includes to get M_PI. 5 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES 6 #define _USE_MATH_DEFINES
7 7
8 #include "ui/gfx/transform.h" 8 #include "ui/gfx/transform.h"
9 9
10 #include <cmath> 10 #include <cmath>
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 if (matrix_.isIdentity()) 200 if (matrix_.isIdentity())
201 matrix_.setDouble(3, 2, -1.0 / depth); 201 matrix_.setDouble(3, 2, -1.0 / depth);
202 else { 202 else {
203 SkMatrix44 m; 203 SkMatrix44 m;
204 m.setDouble(3, 2, -1.0 / depth); 204 m.setDouble(3, 2, -1.0 / depth);
205 matrix_.preConcat(m); 205 matrix_.preConcat(m);
206 } 206 }
207 } 207 }
208 208
209 void Transform::PreconcatTransform(const Transform& transform) { 209 void Transform::PreconcatTransform(const Transform& transform) {
210 if (!transform.matrix_.isIdentity()) { 210 if (matrix_.isIdentity()) {
211 *this = transform;
danakj 2012/11/30 01:29:26 matrix_ = transform.matrix_; like below?
212 } else if (!transform.matrix_.isIdentity()) {
211 matrix_.preConcat(transform.matrix_); 213 matrix_.preConcat(transform.matrix_);
212 } 214 }
213 } 215 }
214 216
215 void Transform::ConcatTransform(const Transform& transform) { 217 void Transform::ConcatTransform(const Transform& transform) {
216 if (!transform.matrix_.isIdentity()) { 218 if (matrix_.isIdentity()) {
219 matrix_ = transform.matrix_;
220 } else if (!transform.matrix_.isIdentity()) {
217 matrix_.postConcat(transform.matrix_); 221 matrix_.postConcat(transform.matrix_);
218 } 222 }
219 } 223 }
220 224
221 bool Transform::IsIdentity() const { 225 bool Transform::IsIdentity() const {
222 return matrix_.isIdentity(); 226 return matrix_.isIdentity();
223 } 227 }
224 228
225 bool Transform::IsIdentityOrTranslation() const { 229 bool Transform::IsIdentityOrTranslation() const {
230 if (matrix_.isIdentity())
231 return true;
232
226 bool has_no_perspective = !matrix_.getDouble(3, 0) && 233 bool has_no_perspective = !matrix_.getDouble(3, 0) &&
227 !matrix_.getDouble(3, 1) && 234 !matrix_.getDouble(3, 1) &&
228 !matrix_.getDouble(3, 2) && 235 !matrix_.getDouble(3, 2) &&
229 (matrix_.getDouble(3, 3) == 1); 236 (matrix_.getDouble(3, 3) == 1);
230 237
231 bool has_no_rotation_or_skew = !matrix_.getDouble(0, 1) && 238 bool has_no_rotation_or_skew = !matrix_.getDouble(0, 1) &&
232 !matrix_.getDouble(0, 2) && 239 !matrix_.getDouble(0, 2) &&
233 !matrix_.getDouble(1, 0) && 240 !matrix_.getDouble(1, 0) &&
234 !matrix_.getDouble(1, 2) && 241 !matrix_.getDouble(1, 2) &&
235 !matrix_.getDouble(2, 0) && 242 !matrix_.getDouble(2, 0) &&
236 !matrix_.getDouble(2, 1); 243 !matrix_.getDouble(2, 1);
237 244
238 bool has_no_scale = matrix_.getDouble(0, 0) == 1 && 245 bool has_no_scale = matrix_.getDouble(0, 0) == 1 &&
239 matrix_.getDouble(1, 1) == 1 && 246 matrix_.getDouble(1, 1) == 1 &&
240 matrix_.getDouble(2, 2) == 1; 247 matrix_.getDouble(2, 2) == 1;
241 248
242 return has_no_perspective && has_no_rotation_or_skew && has_no_scale; 249 return has_no_perspective && has_no_rotation_or_skew && has_no_scale;
243 } 250 }
244 251
245 bool Transform::IsScaleOrTranslation() const { 252 bool Transform::IsScaleOrTranslation() const {
253 if (matrix_.isIdentity())
254 return true;
255
246 bool has_no_perspective = !matrix_.getDouble(3, 0) && 256 bool has_no_perspective = !matrix_.getDouble(3, 0) &&
247 !matrix_.getDouble(3, 1) && 257 !matrix_.getDouble(3, 1) &&
248 !matrix_.getDouble(3, 2) && 258 !matrix_.getDouble(3, 2) &&
249 (matrix_.getDouble(3, 3) == 1); 259 (matrix_.getDouble(3, 3) == 1);
250 260
251 bool has_no_rotation_or_skew = !matrix_.getDouble(0, 1) && 261 bool has_no_rotation_or_skew = !matrix_.getDouble(0, 1) &&
252 !matrix_.getDouble(0, 2) && 262 !matrix_.getDouble(0, 2) &&
253 !matrix_.getDouble(1, 0) && 263 !matrix_.getDouble(1, 0) &&
254 !matrix_.getDouble(1, 2) && 264 !matrix_.getDouble(1, 2) &&
255 !matrix_.getDouble(2, 0) && 265 !matrix_.getDouble(2, 0) &&
256 !matrix_.getDouble(2, 1); 266 !matrix_.getDouble(2, 1);
257 267
258 return has_no_perspective && has_no_rotation_or_skew; 268 return has_no_perspective && has_no_rotation_or_skew;
259 } 269 }
260 270
261 bool Transform::HasPerspective() const { 271 bool Transform::HasPerspective() const {
262 return matrix_.getDouble(3, 0) || 272 return matrix_.getDouble(3, 0) ||
263 matrix_.getDouble(3, 1) || 273 matrix_.getDouble(3, 1) ||
264 matrix_.getDouble(3, 2) || 274 matrix_.getDouble(3, 2) ||
265 (matrix_.getDouble(3, 3) != 1); 275 (matrix_.getDouble(3, 3) != 1);
266 } 276 }
267 277
268 bool Transform::IsInvertible() const { 278 bool Transform::IsInvertible() const {
269 return std::abs(matrix_.determinant()) > kTooSmallForDeterminant; 279 return std::abs(matrix_.determinant()) > kTooSmallForDeterminant;
270 } 280 }
271 281
272 bool Transform::IsBackFaceVisible() const { 282 bool Transform::IsBackFaceVisible() const {
283 if (matrix_.isIdentity())
284 return false;
285
273 // Compute whether a layer with a forward-facing normal of (0, 0, 1) would 286 // Compute whether a layer with a forward-facing normal of (0, 0, 1) would
274 // have its back face visible after applying the transform. 287 // have its back face visible after applying the transform.
275 // 288 //
276 // This is done by transforming the normal and seeing if the resulting z 289 // This is done by transforming the normal and seeing if the resulting z
277 // value is positive or negative. However, note that transforming a normal 290 // value is positive or negative. However, note that transforming a normal
278 // actually requires using the inverse-transpose of the original transform. 291 // actually requires using the inverse-transpose of the original transform.
279 292
280 // TODO (shawnsingh) make this perform more efficiently - we do not 293 // TODO (shawnsingh) make this perform more efficiently - we do not
281 // actually need to instantiate/invert/transpose any matrices, exploiting the 294 // actually need to instantiate/invert/transpose any matrices, exploiting the
282 // fact that we only need to transform (0, 0, 1, 0). 295 // fact that we only need to transform (0, 0, 1, 0).
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 return false; 374 return false;
362 375
363 if (!BlendDecomposedTransforms(&to_decomp, to_decomp, from_decomp, progress)) 376 if (!BlendDecomposedTransforms(&to_decomp, to_decomp, from_decomp, progress))
364 return false; 377 return false;
365 378
366 matrix_ = ComposeTransform(to_decomp).matrix(); 379 matrix_ = ComposeTransform(to_decomp).matrix();
367 return true; 380 return true;
368 } 381 }
369 382
370 Transform Transform::operator*(const Transform& other) const { 383 Transform Transform::operator*(const Transform& other) const {
384 if (matrix_.isIdentity())
385 return other;
386 if (other.matrix_.isIdentity())
387 return *this;
371 Transform to_return; 388 Transform to_return;
372 to_return.matrix_.setConcat(matrix_, other.matrix_); 389 to_return.matrix_.setConcat(matrix_, other.matrix_);
373 return to_return; 390 return to_return;
374 } 391 }
375 392
376 Transform& Transform::operator*=(const Transform& other) { 393 Transform& Transform::operator*=(const Transform& other) {
377 matrix_.preConcat(other.matrix_); 394 PreconcatTransform(other);
378 return *this; 395 return *this;
379 } 396 }
380 397
381 void Transform::TransformPointInternal(const SkMatrix44& xform, 398 void Transform::TransformPointInternal(const SkMatrix44& xform,
382 Point3F& point) const { 399 Point3F& point) const {
383 SkMScalar p[4] = { 400 SkMScalar p[4] = {
384 SkDoubleToMScalar(point.x()), 401 SkDoubleToMScalar(point.x()),
385 SkDoubleToMScalar(point.y()), 402 SkDoubleToMScalar(point.y()),
386 SkDoubleToMScalar(point.z()), 403 SkDoubleToMScalar(point.z()),
387 SkDoubleToMScalar(1) 404 SkDoubleToMScalar(1)
(...skipping 16 matching lines...) Expand all
404 SkDoubleToMScalar(0), 421 SkDoubleToMScalar(0),
405 SkDoubleToMScalar(1) 422 SkDoubleToMScalar(1)
406 }; 423 };
407 424
408 xform.mapMScalars(p); 425 xform.mapMScalars(p);
409 426
410 point.SetPoint(ToRoundedInt(p[0]), ToRoundedInt(p[1])); 427 point.SetPoint(ToRoundedInt(p[0]), ToRoundedInt(p[1]));
411 } 428 }
412 429
413 } // namespace gfx 430 } // namespace gfx
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698