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

Side by Side Diff: cc/surfaces/surface_id.h

Issue 1996783002: Make cc::SurfaceId unguessable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Tom's comments Created 4 years, 6 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 unified diff | Download patch
« no previous file with comments | « cc/surfaces/surface_hittest_unittest.cc ('k') | cc/surfaces/surface_id_allocator.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #ifndef CC_SURFACES_SURFACE_ID_H_ 5 #ifndef CC_SURFACES_SURFACE_ID_H_
6 #define CC_SURFACES_SURFACE_ID_H_ 6 #define CC_SURFACES_SURFACE_ID_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
11 #include <functional> 11 #include <functional>
12 12
13 #include "base/format_macros.h"
14 #include "base/hash.h"
15 #include "base/strings/stringprintf.h"
16
13 namespace cc { 17 namespace cc {
14 18
15 struct SurfaceId { 19 class SurfaceId {
16 SurfaceId() : id(0) {} 20 public:
17 explicit SurfaceId(uint64_t id) : id(id) {} 21 SurfaceId() : id_namespace_(0), local_id_(0), nonce_(0) {}
18 22
19 bool is_null() const { return id == 0; } 23 SurfaceId(const SurfaceId& other)
24 : id_namespace_(other.id_namespace_),
25 local_id_(other.local_id_),
26 nonce_(other.nonce_) {}
20 27
28 SurfaceId(uint32_t id_namespace, uint32_t local_id, uint64_t nonce)
29 : id_namespace_(id_namespace),
30 local_id_(local_id),
31 nonce_(nonce)
32
33 {}
34
35 bool is_null() const {
36 return id_namespace_ == 0 && nonce_ == 0 && local_id_ == 0;
37 }
38
39 size_t hash() const {
40 size_t interim = base::HashInts(id_namespace_, local_id_);
41 return base::HashInts(interim, nonce_);
42 }
43
44 uint32_t id_namespace() const { return id_namespace_; }
45
46 uint32_t local_id() const { return local_id_; }
47
48 uint64_t nonce() const { return nonce_; }
49
50 std::string ToString() const {
51 return base::StringPrintf("%d:%" PRIuS ":%d", id_namespace_, nonce_,
52 local_id_);
53 }
54
55 bool operator==(const SurfaceId& other) const {
56 return (id_namespace_ == other.id_namespace_) &&
Tom Sepez 2016/05/25 00:09:53 nit: overparenthesized.
Fady Samuel 2016/05/25 00:57:45 Done.
57 (local_id_ == other.local_id_) && (nonce_ == other.nonce_);
58 }
59
60 bool operator!=(const SurfaceId& other) const { return !(*this == other); }
61
62 bool operator<(const SurfaceId& other) const {
63 if (id_namespace_ < other.id_namespace_)
Tom Sepez 2016/05/25 00:09:53 nit: this is fine, but still reads a little weird.
Fady Samuel 2016/05/25 00:57:45 Nice! Much nicer! Done.
danakj 2016/05/25 00:58:55 The "modern" way to do that is with std::tie(), an
Fady Samuel 2016/05/25 01:07:41 I totally forgot about std::tie. Thanks! Let's go
64 return true;
65
66 if (local_id_ < other.local_id_)
67 return id_namespace_ == other.id_namespace_;
68
69 if (nonce_ < other.nonce_) {
70 return id_namespace_ == other.id_namespace_ &&
71 local_id_ == other.local_id_;
72 }
73
74 return false;
75 }
76
77 private:
21 // See SurfaceIdAllocator::GenerateId. 78 // See SurfaceIdAllocator::GenerateId.
22 uint32_t id_namespace() const { return id >> 32; } 79 uint32_t id_namespace_;
23 80 uint32_t local_id_;
24 uint64_t id; 81 uint64_t nonce_;
25 }; 82 };
26 83
27 inline bool operator==(const SurfaceId& a, const SurfaceId& b) {
28 return a.id == b.id;
29 }
30
31 inline bool operator!=(const SurfaceId& a, const SurfaceId& b) {
32 return !(a == b);
33 }
34
35 inline bool operator<(const SurfaceId& a, const SurfaceId& b) {
36 return a.id < b.id;
37 }
38
39 struct SurfaceIdHash { 84 struct SurfaceIdHash {
40 size_t operator()(const SurfaceId& key) const { 85 size_t operator()(const SurfaceId& key) const { return key.hash(); }
41 return std::hash<uint64_t>()(key.id);
42 }
43 }; 86 };
44 87
45 } // namespace cc 88 } // namespace cc
46 89
47 #endif // CC_SURFACES_SURFACE_ID_H_ 90 #endif // CC_SURFACES_SURFACE_ID_H_
OLDNEW
« no previous file with comments | « cc/surfaces/surface_hittest_unittest.cc ('k') | cc/surfaces/surface_id_allocator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698