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

Unified Diff: cc/resources/resource_util.h

Issue 1202843008: cc: Fix BytesPerPixel issue and refactor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed review 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.h
diff --git a/cc/resources/resource_util.h b/cc/resources/resource_util.h
new file mode 100644
index 0000000000000000000000000000000000000000..ad50ff613f18224c661d280e405b78d8f32c7dcc
--- /dev/null
+++ b/cc/resources/resource_util.h
@@ -0,0 +1,70 @@
+// 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.
+
+#ifndef CC_RESOURCES_RESOURCE_UTIL_H_
+#define CC_RESOURCES_RESOURCE_UTIL_H_
+
+#include <limits>
+
+#include "base/numerics/safe_math.h"
+#include "cc/base/cc_export.h"
+#include "cc/resources/resource_format.h"
+#include "ui/gfx/geometry/size.h"
+
+namespace cc {
+
+class CC_EXPORT ResourceUtil {
+ public:
+ // Returns true if the size is valid and fits in bytes, false otherwise.
+ template <typename T>
+ static bool VerifySizeInBytes(const gfx::Size& size, ResourceFormat format) {
+ static_assert(std::numeric_limits<T>::is_integer,
+ "T must be an integer type");
+ base::CheckedNumeric<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;
+ }
+
+ // Dies with a CRASH() if the size can not be represented as a positive
+ // number of bytes.
+ template <typename T>
+ static T CheckedSizeInBytes(const gfx::Size& size, ResourceFormat format) {
+ static_assert(std::numeric_limits<T>::is_integer,
+ "T must be an integer type");
+ DCHECK(VerifySizeInBytes<T>(size, format));
+ base::CheckedNumeric<T> checked_value = BitsPerPixel(format);
+ checked_value *= size.width();
+ checked_value *= size.height();
+ checked_value /= 8;
+ return checked_value.ValueOrDie();
+ }
+
+ // Returns the width in bytes but may overflow or return 0. Only do this for
+ // computing widths for sizes that have already been checked.
+ 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
+ // Returns the size in bytes but may overflow or return 0. Only do this for
+ // sizes that have already been checked.
+ static size_t UncheckedSizeInBytes(const gfx::Size& size,
+ ResourceFormat format);
+ // Same as above, but bytes are aligned on 4-bytes boundaries for upload
+ // performance, assuming that GL_PACK_ALIGNMENT or GL_UNPACK_ALIGNMENT have
+ // not changed from default.
+ static size_t UncheckedWidthInBytesAligned(size_t width,
+ ResourceFormat format);
+ static size_t UncheckedSizeInBytesAligned(const gfx::Size& size,
+ ResourceFormat format);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ResourceUtil);
+};
+
+} // namespace cc
+
+#endif // CC_RESOURCES_RESOURCE_UTIL_H_

Powered by Google App Engine
This is Rietveld 408576698