Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CC_RESOURCES_RESOURCE_UTIL_H_ | |
| 6 #define CC_RESOURCES_RESOURCE_UTIL_H_ | |
| 7 | |
| 8 #include <limits> | |
| 9 | |
| 10 #include "base/numerics/safe_math.h" | |
| 11 #include "cc/base/cc_export.h" | |
| 12 #include "cc/resources/resource_format.h" | |
| 13 #include "ui/gfx/geometry/size.h" | |
| 14 | |
| 15 namespace cc { | |
| 16 | |
| 17 class CC_EXPORT ResourceUtil { | |
| 18 public: | |
| 19 // Returns true if the size is valid and fits in bytes, false otherwise. | |
| 20 template <typename T> | |
| 21 static bool VerifySizeInBytes(const gfx::Size& size, ResourceFormat format) { | |
| 22 static_assert(std::numeric_limits<T>::is_integer, | |
| 23 "T must be an integer type"); | |
| 24 base::CheckedNumeric<T> checked_value = BitsPerPixel(format); | |
| 25 checked_value *= size.width(); | |
| 26 checked_value *= size.height(); | |
| 27 if (!checked_value.IsValid()) | |
| 28 return false; | |
| 29 size_t value = checked_value.ValueOrDie(); | |
| 30 if ((value % 8) != 0) | |
| 31 return false; | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 35 // Dies with a CRASH() if the size can not be represented as a positive | |
| 36 // number of bytes. | |
| 37 template <typename T> | |
| 38 static T CheckedSizeInBytes(const gfx::Size& size, ResourceFormat format) { | |
| 39 static_assert(std::numeric_limits<T>::is_integer, | |
| 40 "T must be an integer type"); | |
| 41 DCHECK(VerifySizeInBytes<T>(size, format)); | |
| 42 base::CheckedNumeric<T> checked_value = BitsPerPixel(format); | |
| 43 checked_value *= size.width(); | |
| 44 checked_value *= size.height(); | |
| 45 checked_value /= 8; | |
| 46 return checked_value.ValueOrDie(); | |
| 47 } | |
| 48 | |
| 49 // Returns the width in bytes but may overflow or return 0. Only do this for | |
| 50 // computing widths for sizes that have already been checked. | |
| 51 static size_t UncheckedWidthInBytes(size_t width, ResourceFormat format); | |
|
ericrk
2015/07/24 20:03:52
I like the templatization - can you templatize the
prashant.n
2015/07/25 07:15:43
Hmm, I had thought of this earlier, but then we sh
| |
| 52 // Returns the size in bytes but may overflow or return 0. Only do this for | |
| 53 // sizes that have already been checked. | |
| 54 static size_t UncheckedSizeInBytes(const gfx::Size& size, | |
| 55 ResourceFormat format); | |
| 56 // Same as above, but bytes are aligned on 4-bytes boundaries for upload | |
| 57 // performance, assuming that GL_PACK_ALIGNMENT or GL_UNPACK_ALIGNMENT have | |
| 58 // not changed from default. | |
| 59 static size_t UncheckedWidthInBytesAligned(size_t width, | |
| 60 ResourceFormat format); | |
| 61 static size_t UncheckedSizeInBytesAligned(const gfx::Size& size, | |
| 62 ResourceFormat format); | |
| 63 | |
| 64 private: | |
| 65 DISALLOW_COPY_AND_ASSIGN(ResourceUtil); | |
| 66 }; | |
| 67 | |
| 68 } // namespace cc | |
| 69 | |
| 70 #endif // CC_RESOURCES_RESOURCE_UTIL_H_ | |
| OLD | NEW |