OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_CLIENT_SERVICE_MAP_H_ |
| 6 #define GPU_COMMAND_BUFFER_SERVICE_CLIENT_SERVICE_MAP_H_ |
| 7 |
| 8 #include <limits> |
| 9 #include <unordered_map> |
| 10 |
| 11 namespace gpu { |
| 12 |
| 13 namespace gles2 { |
| 14 |
| 15 template <typename ClientType, typename ServiceType> |
| 16 class ClientServiceMap { |
| 17 public: |
| 18 ClientServiceMap() |
| 19 : client_to_service_(), |
| 20 next_service_reserved_index_(std::numeric_limits<ServiceType>::max()) {} |
| 21 |
| 22 void SetIDMapping(ClientType client_id, ServiceType service_id) { |
| 23 DCHECK(client_to_service_.find(client_id) == client_to_service_.end()); |
| 24 client_to_service_[client_id] = service_id; |
| 25 } |
| 26 |
| 27 void RemoveClientID(ClientType client_id) { |
| 28 client_to_service_.erase(client_id); |
| 29 } |
| 30 |
| 31 bool GetServiceID(ClientType client_id, ServiceType* service_id) const { |
| 32 if (client_id == 0) { |
| 33 if (service_id) { |
| 34 *service_id = 0; |
| 35 } |
| 36 return true; |
| 37 } |
| 38 auto iter = client_to_service_.find(client_id); |
| 39 if (iter != client_to_service_.end()) { |
| 40 if (service_id) { |
| 41 *service_id = iter->second; |
| 42 } |
| 43 return true; |
| 44 } |
| 45 return false; |
| 46 } |
| 47 |
| 48 ServiceType ReserveClientID(ClientType client_id) { |
| 49 ServiceType service_id = next_service_reserved_index_--; |
| 50 SetIDMapping(client_id, service_id); |
| 51 return service_id; |
| 52 } |
| 53 |
| 54 ServiceType GetServiceIDOrReserve(ClientType client_id) { |
| 55 ServiceType service_id; |
| 56 if (GetServiceID(client_id, &service_id)) { |
| 57 return service_id; |
| 58 } |
| 59 return ReserveClientID(client_id); |
| 60 } |
| 61 |
| 62 private: |
| 63 std::unordered_map<ClientType, ServiceType> client_to_service_; |
| 64 ServiceType next_service_reserved_index_; |
| 65 }; |
| 66 |
| 67 } // namespace gles2 |
| 68 } // namespace gpu |
| 69 |
| 70 #endif // GPU_COMMAND_BUFFER_SERVICE_CLIENT_SERVICE_MAP_H_ |
OLD | NEW |