OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef CC_RESOURCES_RESOURCE_UTIL_H_ | 5 #ifndef CC_RESOURCES_RESOURCE_UTIL_H_ |
6 #define CC_RESOURCES_RESOURCE_UTIL_H_ | 6 #define CC_RESOURCES_RESOURCE_UTIL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // this for computing widths for sizes that have already been checked. | 48 // this for computing widths for sizes that have already been checked. |
49 template <typename T> | 49 template <typename T> |
50 static T UncheckedWidthInBytesAligned(int width, ResourceFormat format); | 50 static T UncheckedWidthInBytesAligned(int width, ResourceFormat format); |
51 // Returns the size in bytes aligned but may overflow or return 0. Only do | 51 // Returns the size in bytes aligned but may overflow or return 0. Only do |
52 // this for sizes that have already been checked. | 52 // this for sizes that have already been checked. |
53 template <typename T> | 53 template <typename T> |
54 static T UncheckedSizeInBytesAligned(const gfx::Size& size, | 54 static T UncheckedSizeInBytesAligned(const gfx::Size& size, |
55 ResourceFormat format); | 55 ResourceFormat format); |
56 | 56 |
57 private: | 57 private: |
58 // TODO(prashant.n): Replace IsSameType with std::is_same once C++11 is used | |
59 // on all platforms. | |
60 template <typename T, typename U> | |
61 struct IsSameType { | |
62 static const bool value = false; | |
63 }; | |
64 | |
65 template <typename T> | |
66 struct IsSameType<T, T> { | |
67 static const bool value = true; | |
68 }; | |
69 | |
70 template <typename T> | 58 template <typename T> |
71 static inline void VerifyType(); | 59 static inline void VerifyType(); |
72 | 60 |
73 template <typename T> | 61 template <typename T> |
74 static bool VerifyFitsInBytesInternal(int width, | 62 static bool VerifyFitsInBytesInternal(int width, |
75 int height, | 63 int height, |
76 ResourceFormat format, | 64 ResourceFormat format, |
77 bool verify_size, | 65 bool verify_size, |
78 bool aligned); | 66 bool aligned); |
79 | 67 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 ResourceFormat format) { | 142 ResourceFormat format) { |
155 VerifyType<T>(); | 143 VerifyType<T>(); |
156 DCHECK(VerifyFitsInBytesInternal<T>(size.width(), size.height(), format, true, | 144 DCHECK(VerifyFitsInBytesInternal<T>(size.width(), size.height(), format, true, |
157 true)); | 145 true)); |
158 return BytesInternal<T>(size.width(), size.height(), format, true, true); | 146 return BytesInternal<T>(size.width(), size.height(), format, true, true); |
159 } | 147 } |
160 | 148 |
161 template <typename T> | 149 template <typename T> |
162 void ResourceUtil::VerifyType() { | 150 void ResourceUtil::VerifyType() { |
163 static_assert( | 151 static_assert( |
164 std::numeric_limits<T>::is_integer && !IsSameType<T, bool>::value, | 152 std::numeric_limits<T>::is_integer && !std::is_same<T, bool>::value, |
165 "T must be non-bool integer type. Preferred type is size_t."); | 153 "T must be non-bool integer type. Preferred type is size_t."); |
166 } | 154 } |
167 | 155 |
168 template <typename T> | 156 template <typename T> |
169 bool ResourceUtil::VerifyFitsInBytesInternal(int width, | 157 bool ResourceUtil::VerifyFitsInBytesInternal(int width, |
170 int height, | 158 int height, |
171 ResourceFormat format, | 159 ResourceFormat format, |
172 bool verify_size, | 160 bool verify_size, |
173 bool aligned) { | 161 bool aligned) { |
174 base::CheckedNumeric<T> checked_value = BitsPerPixel(format); | 162 base::CheckedNumeric<T> checked_value = BitsPerPixel(format); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 bytes = MathUtil::UncheckedRoundUp<T>(bytes, 4); | 204 bytes = MathUtil::UncheckedRoundUp<T>(bytes, 4); |
217 if (verify_size) | 205 if (verify_size) |
218 bytes *= height; | 206 bytes *= height; |
219 | 207 |
220 return bytes; | 208 return bytes; |
221 } | 209 } |
222 | 210 |
223 } // namespace cc | 211 } // namespace cc |
224 | 212 |
225 #endif // CC_RESOURCES_RESOURCE_UTIL_H_ | 213 #endif // CC_RESOURCES_RESOURCE_UTIL_H_ |
OLD | NEW |