OLD | NEW |
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 // This file is here so other GLES2 related files can have a common set of | 5 // This file is here so other GLES2 related files can have a common set of |
6 // includes where appropriate. | 6 // includes where appropriate. |
7 | 7 |
8 #ifndef GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ | 8 #ifndef GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ |
9 #define GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ | 9 #define GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ |
10 | 10 |
11 #include <stdint.h> | 11 #include <stdint.h> |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
| 17 #include "base/numerics/safe_math.h" |
17 #include "gpu/command_buffer/common/gles2_utils_export.h" | 18 #include "gpu/command_buffer/common/gles2_utils_export.h" |
18 | 19 |
19 namespace gpu { | 20 namespace gpu { |
20 namespace gles2 { | 21 namespace gles2 { |
21 | 22 |
22 // Does a multiply and checks for overflow. If the multiply did not overflow | 23 // Does a multiply and checks for overflow. If the multiply did not overflow |
23 // returns true. | 24 // returns true. |
24 | 25 |
25 // Multiplies 2 32 bit unsigned numbers checking for overflow. | 26 // Multiplies 2 32 bit unsigned numbers checking for overflow. |
26 // If there was no overflow returns true. | 27 // If there was no overflow returns true. |
27 inline bool SafeMultiplyUint32(uint32_t a, uint32_t b, uint32_t* dst) { | 28 inline bool SafeMultiplyUint32(uint32_t a, uint32_t b, uint32_t* dst) { |
28 if (b == 0) { | 29 DCHECK(dst); |
29 *dst = 0; | 30 base::CheckedNumeric<uint32_t> checked = a; |
30 return true; | 31 checked *= b; |
31 } | 32 *dst = checked.ValueOrDefault(0); |
32 uint32_t v = a * b; | 33 return checked.IsValid(); |
33 if (v / b != a) { | |
34 *dst = 0; | |
35 return false; | |
36 } | |
37 *dst = v; | |
38 return true; | |
39 } | 34 } |
40 | 35 |
41 // Does an add checking for overflow. If there was no overflow returns true. | 36 // Does an add checking for overflow. If there was no overflow returns true. |
42 inline bool SafeAddUint32(uint32_t a, uint32_t b, uint32_t* dst) { | 37 inline bool SafeAddUint32(uint32_t a, uint32_t b, uint32_t* dst) { |
43 if (a + b < a) { | 38 DCHECK(dst); |
44 *dst = 0; | 39 base::CheckedNumeric<uint32_t> checked = a; |
45 return false; | 40 checked += b; |
46 } | 41 *dst = checked.ValueOrDefault(0); |
47 *dst = a + b; | 42 return checked.IsValid(); |
48 return true; | |
49 } | 43 } |
50 | 44 |
51 // Does an add checking for overflow. If there was no overflow returns true. | 45 // Does an add checking for overflow. If there was no overflow returns true. |
52 inline bool SafeAddInt32(int32_t a, int32_t b, int32_t* dst) { | 46 inline bool SafeAddInt32(int32_t a, int32_t b, int32_t* dst) { |
53 int64_t sum64 = static_cast<int64_t>(a) + b; | 47 DCHECK(dst); |
54 int32_t sum32 = static_cast<int32_t>(sum64); | 48 base::CheckedNumeric<int32_t> checked = a; |
55 bool safe = sum64 == static_cast<int64_t>(sum32); | 49 checked += b; |
56 *dst = safe ? sum32 : 0; | 50 *dst = checked.ValueOrDefault(0); |
57 return safe; | 51 return checked.IsValid(); |
58 } | |
59 | |
60 // Return false if |value| is more than a 32 bit integer can represent. | |
61 template<typename T> | |
62 inline bool FitInt32NonNegative(T value) { | |
63 const int32_t max = std::numeric_limits<int32_t>::max(); | |
64 return (std::numeric_limits<T>::max() <= max || | |
65 value <= static_cast<T>(max)); | |
66 } | 52 } |
67 | 53 |
68 // Utilties for GLES2 support. | 54 // Utilties for GLES2 support. |
69 class GLES2_UTILS_EXPORT GLES2Util { | 55 class GLES2_UTILS_EXPORT GLES2Util { |
70 public: | 56 public: |
71 static const int kNumFaces = 6; | 57 static const int kNumFaces = 6; |
72 | 58 |
73 // Bits returned by GetChannelsForFormat | 59 // Bits returned by GetChannelsForFormat |
74 enum ChannelBits { | 60 enum ChannelBits { |
75 kRed = 0x1, | 61 kRed = 0x1, |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 bool fail_if_major_perf_caveat; | 212 bool fail_if_major_perf_caveat; |
227 bool lose_context_when_out_of_memory; | 213 bool lose_context_when_out_of_memory; |
228 bool es3_context_required; | 214 bool es3_context_required; |
229 }; | 215 }; |
230 | 216 |
231 } // namespace gles2 | 217 } // namespace gles2 |
232 } // namespace gpu | 218 } // namespace gpu |
233 | 219 |
234 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ | 220 #endif // GPU_COMMAND_BUFFER_COMMON_GLES2_CMD_UTILS_H_ |
235 | 221 |
OLD | NEW |