Index: gpu/command_buffer/service/client_service_map.h |
diff --git a/gpu/command_buffer/service/client_service_map.h b/gpu/command_buffer/service/client_service_map.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..14a914cdeef67d6d5c841b837e0006d4d96bdb13 |
--- /dev/null |
+++ b/gpu/command_buffer/service/client_service_map.h |
@@ -0,0 +1,72 @@ |
+// Copyright (c) 2016 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 GPU_COMMAND_BUFFER_SERVICE_CLIENT_SERVICE_MAP_H_ |
+#define GPU_COMMAND_BUFFER_SERVICE_CLIENT_SERVICE_MAP_H_ |
+ |
+#include <limits> |
+#include <unordered_map> |
+ |
+namespace gpu { |
+ |
+namespace gles2 { |
+ |
+template <typename ClientType, typename ServiceType> |
+class ClientServiceMap { |
+ public: |
+ ClientServiceMap() |
+ : client_to_service_(), |
+ next_service_reserved_index_(std::numeric_limits<ServiceType>::max()) {} |
+ |
+ void SetIDMapping(ClientType client_id, ServiceType service_id) { |
+ DCHECK(client_to_service_.find(client_id) == client_to_service_.end()); |
+ client_to_service_[client_id] = service_id; |
+ } |
+ |
+ void RemoveClientID(ClientType client_id) { |
+ client_to_service_.erase(client_id); |
+ } |
+ |
+ bool GetServiceID(ClientType client_id, ServiceType* service_id) const { |
+ if (client_id == 0) { |
+ if (service_id) { |
+ *service_id = 0; |
+ } |
+ return true; |
+ } |
+ auto iter = client_to_service_.find(client_id); |
+ if (iter != client_to_service_.end()) { |
+ if (service_id) { |
+ *service_id = iter->second; |
+ } |
+ return true; |
+ } else { |
Zhenyao Mo
2016/09/09 21:22:10
nit: chrome style asks no else here.
Geoff Lang
2016/09/12 14:21:29
Done.
|
+ return false; |
+ } |
+ } |
+ |
+ ServiceType ReserveClientID(ClientType client_id) { |
+ ServiceType service_id = next_service_reserved_index_--; |
+ SetIDMapping(client_id, service_id); |
+ return service_id; |
+ } |
+ |
+ ServiceType GetServiceIDOrReserve(ClientType client_id) { |
+ ServiceType service_id; |
+ if (GetServiceID(client_id, &service_id)) { |
+ return service_id; |
+ } else { |
Zhenyao Mo
2016/09/09 21:22:10
Same here, no else
Geoff Lang
2016/09/12 14:21:29
Done.
|
+ return ReserveClientID(client_id); |
+ } |
+ } |
+ |
+ private: |
+ std::unordered_map<ClientType, ServiceType> client_to_service_; |
+ ServiceType next_service_reserved_index_; |
+}; |
+ |
+} // namespace gles2 |
+} // namespace gpu |
+ |
+#endif // GPU_COMMAND_BUFFER_SERVICE_CLIENT_SERVICE_MAP_H_ |