OLD | NEW |
---|---|
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/hash.h" | |
14 #include "base/strings/stringprintf.h" | |
15 | |
13 namespace cc { | 16 namespace cc { |
14 | 17 |
15 struct SurfaceId { | 18 class SurfaceId { |
16 SurfaceId() : id(0) {} | 19 public: |
17 explicit SurfaceId(uint64_t id) : id(id) {} | 20 SurfaceId() : id_namespace_(0), nonce_(0), local_id_(0) {} |
18 | 21 |
19 bool is_null() const { return id == 0; } | 22 SurfaceId(const SurfaceId& other) |
23 : id_namespace_(other.id_namespace_), | |
24 nonce_(other.nonce_), | |
25 local_id_(other.local_id_) {} | |
20 | 26 |
27 SurfaceId(uint32_t id_namespace, uint64_t nonce, uint32_t local_id) | |
danakj
2016/05/25 00:19:08
Can you please document what is the namespace, non
Fady Samuel
2016/05/25 00:57:45
Done.
| |
28 : id_namespace_(id_namespace), | |
29 nonce_(nonce), | |
30 local_id_(local_id) | |
31 | |
32 {} | |
33 | |
34 bool is_null() const { | |
35 return id_namespace_ == 0 && nonce_ == 0 && local_id_ == 0; | |
36 } | |
37 | |
38 size_t hash() const { | |
39 size_t interim = base::HashInts(id_namespace_, nonce_); | |
40 return base::HashInts(interim, local_id_); | |
41 } | |
42 | |
43 uint32_t id_namespace() const { return id_namespace_; } | |
44 | |
45 uint64_t nonce() const { return nonce_; } | |
46 | |
47 uint32_t local_id() const { return local_id_; } | |
48 | |
49 std::string ToString() const { | |
50 return base::StringPrintf("(%d, %lu, %d)", id_namespace_, nonce_, | |
Tom Sepez
2016/05/24 22:35:45
might we ever have a FromString()? If so, we migh
Fady Samuel
2016/05/25 00:04:29
Done. Went with namespace:local:nonce
| |
51 local_id_); | |
52 } | |
53 | |
54 bool operator==(const SurfaceId& b) const { | |
55 if (id_namespace_ != b.id_namespace_) | |
Tom Sepez
2016/05/24 22:35:45
nit or just:
return id_namespace == b.id_namespa
Fady Samuel
2016/05/25 00:04:29
Done.
| |
56 return false; | |
57 | |
58 if (nonce_ != b.nonce_) | |
59 return false; | |
60 | |
61 return local_id_ == b.local_id_; | |
62 } | |
63 | |
64 bool operator!=(const SurfaceId& b) const { return !(*this == b); } | |
65 | |
66 bool operator<(const SurfaceId& b) const { | |
67 if (id_namespace_ < b.id_namespace_) | |
Tom Sepez
2016/05/24 22:35:45
I'd sort these by namespace, then local, then nonc
Fady Samuel
2016/05/25 00:04:29
Done.
| |
68 return true; | |
69 | |
70 if (nonce_ < b.nonce_) | |
71 return id_namespace_ == b.id_namespace_; | |
72 | |
73 if (local_id_ < b.local_id_) { | |
74 return id_namespace_ == b.id_namespace_ && nonce_ == b.nonce_; | |
75 } | |
76 | |
77 return false; | |
78 } | |
79 | |
80 private: | |
21 // See SurfaceIdAllocator::GenerateId. | 81 // See SurfaceIdAllocator::GenerateId. |
22 uint32_t id_namespace() const { return id >> 32; } | 82 uint32_t id_namespace_; |
Tom Sepez
2016/05/24 22:35:45
I'd order these as namespace_id_, local_id, nonce_
Fady Samuel
2016/05/25 00:04:29
Done.
| |
23 | 83 uint64_t nonce_; |
24 uint64_t id; | 84 uint32_t local_id_; |
25 }; | 85 }; |
26 | 86 |
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 { | 87 struct SurfaceIdHash { |
40 size_t operator()(const SurfaceId& key) const { | 88 size_t operator()(const SurfaceId& key) const { return key.hash(); } |
41 return std::hash<uint64_t>()(key.id); | |
42 } | |
43 }; | 89 }; |
44 | 90 |
45 } // namespace cc | 91 } // namespace cc |
46 | 92 |
47 #endif // CC_SURFACES_SURFACE_ID_H_ | 93 #endif // CC_SURFACES_SURFACE_ID_H_ |
OLD | NEW |