| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "device/vr/vr_service_impl.h" | 5 #include "device/vr/vr_service_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "device/vr/vr_device.h" | 10 #include "device/vr/vr_device.h" |
| 11 #include "device/vr/vr_device_manager.h" | 11 #include "device/vr/vr_device_manager.h" |
| 12 | 12 |
| 13 namespace device { | 13 namespace device { |
| 14 | 14 |
| 15 VRServiceImpl::VRServiceImpl() {} | 15 VRServiceImpl::VRServiceImpl() {} |
| 16 | 16 |
| 17 VRServiceImpl::~VRServiceImpl() { | 17 VRServiceImpl::~VRServiceImpl() { |
| 18 RemoveFromDeviceManager(); | 18 RemoveFromDeviceManager(); |
| 19 } | 19 } |
| 20 | 20 |
| 21 void VRServiceImpl::BindRequest( | 21 void VRServiceImpl::BindRequest(mojo::InterfaceRequest<VRService> request) { |
| 22 mojo::InterfaceRequest<mojom::VRService> request) { | |
| 23 VRServiceImpl* service = new VRServiceImpl(); | 22 VRServiceImpl* service = new VRServiceImpl(); |
| 24 service->Bind(std::move(request)); | 23 service->Bind(std::move(request)); |
| 25 } | 24 } |
| 26 | 25 |
| 27 // Gets a VRDisplayPtr unique to this service so that the associated page can | 26 void VRServiceImpl::Bind(mojo::InterfaceRequest<VRService> request) { |
| 28 // communicate with the VRDevice. | 27 binding_.reset(new mojo::Binding<VRService>(this, std::move(request))); |
| 29 VRDisplayImpl* VRServiceImpl::GetVRDisplayImpl(VRDevice* device) { | |
| 30 DisplayImplMap::iterator it = displays_.find(device); | |
| 31 if (it != displays_.end()) | |
| 32 return it->second.get(); | |
| 33 | |
| 34 VRDisplayImpl* display_impl = new VRDisplayImpl(device, this); | |
| 35 displays_[device] = base::WrapUnique(display_impl); | |
| 36 return display_impl; | |
| 37 } | |
| 38 | |
| 39 void VRServiceImpl::Bind(mojo::InterfaceRequest<mojom::VRService> request) { | |
| 40 // TODO(shaobo.yan@intel.com) : Keep one binding_ and use close and rebind. | |
| 41 binding_.reset(new mojo::Binding<mojom::VRService>(this, std::move(request))); | |
| 42 binding_->set_connection_error_handler(base::Bind( | 28 binding_->set_connection_error_handler(base::Bind( |
| 43 &VRServiceImpl::RemoveFromDeviceManager, base::Unretained(this))); | 29 &VRServiceImpl::RemoveFromDeviceManager, base::Unretained(this))); |
| 44 } | 30 } |
| 45 | 31 |
| 46 void VRServiceImpl::RemoveFromDeviceManager() { | 32 void VRServiceImpl::RemoveFromDeviceManager() { |
| 47 displays_.clear(); | |
| 48 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 33 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 49 device_manager->RemoveService(this); | 34 device_manager->RemoveService(this); |
| 50 } | 35 } |
| 51 | 36 |
| 52 void VRServiceImpl::RemoveDevice(VRDevice* device) { | 37 void VRServiceImpl::SetClient(VRServiceClientPtr client) { |
| 53 displays_.erase(device); | 38 DCHECK(!client_.get()); |
| 39 |
| 40 client_ = std::move(client); |
| 41 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 42 device_manager->AddService(this); |
| 54 } | 43 } |
| 55 | 44 |
| 56 void VRServiceImpl::SetClient(mojom::VRServiceClientPtr service_client, | 45 void VRServiceImpl::GetDisplays(const GetDisplaysCallback& callback) { |
| 57 const SetClientCallback& callback) { | 46 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 58 DCHECK(!client_.get()); | 47 callback.Run(device_manager->GetVRDevices()); |
| 48 } |
| 59 | 49 |
| 60 client_ = std::move(service_client); | 50 void VRServiceImpl::GetPose(uint32_t index, const GetPoseCallback& callback) { |
| 51 VRDevice* device = VRDeviceManager::GetAllowedDevice(this, index); |
| 52 |
| 53 if (device) { |
| 54 callback.Run(device->GetPose()); |
| 55 } else { |
| 56 callback.Run(nullptr); |
| 57 } |
| 58 } |
| 59 |
| 60 void VRServiceImpl::ResetPose(uint32_t index) { |
| 61 VRDevice* device = VRDeviceManager::GetAllowedDevice(this, index); |
| 62 if (device) |
| 63 device->ResetPose(); |
| 64 } |
| 65 |
| 66 void VRServiceImpl::RequestPresent(uint32_t index, |
| 67 bool secureOrigin, |
| 68 const RequestPresentCallback& callback) { |
| 61 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); | 69 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 62 // Once a client has been connected AddService will force any VRDisplays to | 70 callback.Run(device_manager->RequestPresent(this, index, secureOrigin)); |
| 63 // send OnConnected to it so that it's populated with the currently active | 71 } |
| 64 // displays. Thereafer it will stay up to date by virtue of listening for new | 72 |
| 65 // connected events. | 73 void VRServiceImpl::ExitPresent(uint32_t index) { |
| 66 device_manager->AddService(this); | 74 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 67 callback.Run(device_manager->GetNumberOfConnectedDevices()); | 75 device_manager->ExitPresent(this, index); |
| 76 } |
| 77 |
| 78 void VRServiceImpl::SubmitFrame(uint32_t index, VRPosePtr pose) { |
| 79 VRDeviceManager* device_manager = VRDeviceManager::GetInstance(); |
| 80 device_manager->SubmitFrame(this, index, std::move(pose)); |
| 81 } |
| 82 |
| 83 void VRServiceImpl::UpdateLayerBounds(uint32_t index, |
| 84 VRLayerBoundsPtr leftBounds, |
| 85 VRLayerBoundsPtr rightBounds) { |
| 86 VRDevice* device = VRDeviceManager::GetAllowedDevice(this, index); |
| 87 if (device) |
| 88 device->UpdateLayerBounds(std::move(leftBounds), std::move(rightBounds)); |
| 68 } | 89 } |
| 69 | 90 |
| 70 } // namespace device | 91 } // namespace device |
| OLD | NEW |