Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3423)

Unified Diff: cc/resources/resource_util.cc

Issue 1202843008: cc: Fix BytesPerPixel issue and refactor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Corrected comments. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..08c0bf6066878f9e25639311b4c73311d236a289
--- /dev/null
+++ b/cc/resources/resource_util.cc
@@ -0,0 +1,66 @@
+// 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
+size_t ResourceUtil::CheckedSizeInBytes(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::UncheckedWidthInBytes(size_t width,
+ ResourceFormat format) {
+ size_t bytes_per_row = width * static_cast<size_t>(BitsPerPixel(format)) / 8;
+ return bytes_per_row;
+}
+
+// static
+size_t ResourceUtil::UncheckedWidthInBytesAligned(size_t width,
+ ResourceFormat format) {
+ // Use 4-byte row alignment (OpenGL default) for upload performance.
+ // Assuming that GL_UNPACK_ALIGNMENT has not changed from default.
+ return MathUtil::RoundUp<size_t>(UncheckedWidthInBytes(width, format), 4u);
+}
+
+// static
+size_t ResourceUtil::UncheckedSizeInBytes(const gfx::Size& size,
+ ResourceFormat format) {
+ DCHECK(VerifySizeInBytes(size, format));
+ return size.height() * UncheckedWidthInBytes(size.width(), format);
+}
+
+// static
+size_t ResourceUtil::UncheckedSizeInBytesAligned(const gfx::Size& size,
+ ResourceFormat format) {
+ DCHECK(VerifySizeInBytes(size, format));
+ return size.height() * UncheckedWidthInBytesAligned(size.width(), format);
+}
+
+// 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;
+}
+
+} // namespace cc

Powered by Google App Engine
This is Rietveld 408576698