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/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 // A SurfaceId consists of three components: namespace, local Id, and nonce. |
| 29 // An |id_namespace| is a display compositor service allocated ID that |
| 30 // uniquely identifies a client. |
| 31 // A |local_id| is a sequentially allocated ID generated by the display |
| 32 // compositor client. |
| 33 // A |nonce| is a cryptographically secure random int that makes a SurfaceId |
| 34 // unguessable by other clients. |
| 35 SurfaceId(uint32_t id_namespace, uint32_t local_id, uint64_t nonce) |
| 36 : id_namespace_(id_namespace), local_id_(local_id), nonce_(nonce) {} |
| 37 |
| 38 bool is_null() const { |
| 39 return id_namespace_ == 0 && nonce_ == 0 && local_id_ == 0; |
| 40 } |
| 41 |
| 42 size_t hash() const { |
| 43 size_t interim = base::HashInts(id_namespace_, local_id_); |
| 44 return base::HashInts(static_cast<uint64_t>(interim), nonce_); |
| 45 } |
| 46 |
| 47 uint32_t id_namespace() const { return id_namespace_; } |
| 48 |
| 49 uint32_t local_id() const { return local_id_; } |
| 50 |
| 51 uint64_t nonce() const { return nonce_; } |
| 52 |
| 53 std::string ToString() const { |
| 54 return base::StringPrintf("%d:%d:%" PRIu64, id_namespace_, local_id_, |
| 55 nonce_); |
| 56 } |
| 57 |
| 58 bool operator==(const SurfaceId& other) const { |
| 59 return id_namespace_ == other.id_namespace_ && |
| 60 local_id_ == other.local_id_ && nonce_ == other.nonce_; |
| 61 } |
| 62 |
| 63 bool operator!=(const SurfaceId& other) const { return !(*this == other); } |
| 64 |
| 65 bool operator<(const SurfaceId& other) const { |
| 66 return std::tie(id_namespace_, local_id_, nonce_) < |
| 67 std::tie(other.id_namespace_, other.local_id_, other.nonce_); |
| 68 } |
| 69 |
| 70 private: |
21 // See SurfaceIdAllocator::GenerateId. | 71 // See SurfaceIdAllocator::GenerateId. |
22 uint32_t id_namespace() const { return id >> 32; } | 72 uint32_t id_namespace_; |
23 | 73 uint32_t local_id_; |
24 uint64_t id; | 74 uint64_t nonce_; |
25 }; | 75 }; |
26 | 76 |
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 { | 77 struct SurfaceIdHash { |
40 size_t operator()(const SurfaceId& key) const { | 78 size_t operator()(const SurfaceId& key) const { return key.hash(); } |
41 return std::hash<uint64_t>()(key.id); | |
42 } | |
43 }; | 79 }; |
44 | 80 |
45 } // namespace cc | 81 } // namespace cc |
46 | 82 |
47 #endif // CC_SURFACES_SURFACE_ID_H_ | 83 #endif // CC_SURFACES_SURFACE_ID_H_ |
OLD | NEW |