Index: cc/resources/resource_util.cc |
diff --git a/cc/resources/resource_util.cc b/cc/resources/resource_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..72c10b6a5982aa00458c0f38ba1a20738cf41b49 |
--- /dev/null |
+++ b/cc/resources/resource_util.cc |
@@ -0,0 +1,58 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "cc/resources/resource_util.h" |
+ |
+#include "base/numerics/safe_math.h" |
+#include "cc/base/math_util.h" |
+ |
+namespace cc { |
+ |
+// static |
+bool ResourceUtil::VerifySizeInBytes(const gfx::Size& size, |
+ ResourceFormat format) { |
+ base::CheckedNumeric<size_t> checked_value = BitsPerPixel(format); |
+ checked_value *= size.width(); |
+ checked_value *= size.height(); |
+ if (!checked_value.IsValid()) |
+ return false; |
+ size_t value = checked_value.ValueOrDie(); |
+ if ((value % 8) != 0) |
+ return false; |
+ return true; |
+} |
+ |
+// static |
+size_t ResourceUtil::CheckedMemorySizeBytes(const gfx::Size& size, |
+ ResourceFormat format) { |
+ DCHECK(VerifySizeInBytes(size, format)); |
+ base::CheckedNumeric<size_t> checked_value = BitsPerPixel(format); |
+ checked_value *= size.width(); |
+ checked_value *= size.height(); |
+ checked_value /= 8; |
+ return checked_value.ValueOrDie(); |
+} |
+ |
+// static |
+size_t ResourceUtil::UncheckedMemorySizeBytes(const gfx::Size& size, |
+ ResourceFormat format) { |
+ DCHECK(VerifySizeInBytes(size, format)); |
+ size_t bytes_per_row = |
+ size.width() * static_cast<size_t>(BitsPerPixel(format)) / 8; |
+ return size.height() * bytes_per_row; |
+} |
+ |
+// static |
+size_t ResourceUtil::UncheckedMemorySizeBytesAligned(const gfx::Size& size, |
+ ResourceFormat format) { |
+ DCHECK(VerifySizeInBytes(size, format)); |
+ size_t bytes_per_row = |
+ size.width() * static_cast<size_t>(BitsPerPixel(format)) / 8; |
+ // Use 4-byte row alignment (OpenGL default) for upload performance. |
+ // Assuming that GL_UNPACK_ALIGNMENT has not changed from default. |
+ bytes_per_row = MathUtil::RoundUp<size_t>(bytes_per_row, 4u); |
+ return size.height() * bytes_per_row; |
+} |
+ |
+} // namespace cc |