OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/gfx/geometry/scale.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/numerics/safe_math.h" |
| 10 #include "ui/gfx/switches.h" |
| 11 |
| 12 namespace { |
| 13 const int kScalePrecision = 4; |
| 14 |
| 15 bool VerifyFixedPrecisionValue(float value) { |
| 16 const base::CheckedNumeric<float> checked_factor = pow(10, kScalePrecision); |
| 17 if (!checked_factor.IsValid()) |
| 18 return false; |
| 19 |
| 20 base::CheckedNumeric<float> checked_value = value; |
| 21 checked_value *= checked_factor.ValueOrDie(); |
| 22 if (!checked_value.IsValid()) |
| 23 return false; |
| 24 |
| 25 checked_value = floor(checked_value.ValueOrDie()); |
| 26 checked_value /= checked_factor.ValueOrDie(); |
| 27 return checked_value.IsValid(); |
| 28 } |
| 29 |
| 30 float ComputeFixedPrecisionValue(float value) { |
| 31 // Limiting precision 1 <= kScalePrecision <= 8, as floating point precision |
| 32 // of >= 12 gives different values, 8 on a safer side. |
| 33 DCHECK(kScalePrecision >= 1 && kScalePrecision <= 8); |
| 34 DCHECK(VerifyFixedPrecisionValue(value)); |
| 35 |
| 36 const float factor = pow(10, kScalePrecision); |
| 37 value *= factor; |
| 38 value = floor(value); |
| 39 value /= factor; |
| 40 return value; |
| 41 } |
| 42 |
| 43 bool IsFixedPrecisionScaleEnabled() { |
| 44 static bool fixed_precision_enabled = |
| 45 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 46 switches::kEnableFixedPrecisionScale); |
| 47 return fixed_precision_enabled; |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 namespace gfx { |
| 53 |
| 54 Scale::Scale(float value) : absolute_value_(value) { |
| 55 // Scale < 0.0f is undefined. |
| 56 DCHECK(absolute_value_ >= 0); |
| 57 DCHECK_LT(absolute_value_, std::numeric_limits<float>::max()); |
| 58 |
| 59 if (IsFixedPrecisionScaleEnabled()) |
| 60 value_ = ComputeFixedPrecisionValue(absolute_value_); |
| 61 else |
| 62 value_ = absolute_value_; |
| 63 } |
| 64 |
| 65 Scale::Scale(const Scale& other) { |
| 66 absolute_value_ = other.absolute_value_; |
| 67 value_ = other.value_; |
| 68 } |
| 69 |
| 70 Scale& Scale::operator=(const Scale& other) { |
| 71 absolute_value_ = other.absolute_value_; |
| 72 value_ = other.value_; |
| 73 return *this; |
| 74 } |
| 75 |
| 76 bool Scale::operator<(const Scale& other) const { |
| 77 return this->Value() < other.Value(); |
| 78 } |
| 79 |
| 80 bool Scale::operator>(const Scale& other) const { |
| 81 return this->Value() > other.Value(); |
| 82 } |
| 83 |
| 84 } // namespace gfx |
OLD | NEW |