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/android/gvr/gvr_device.h" | 5 #include "device/vr/android/gvr/gvr_device.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 | 9 |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "device/vr/android/gvr/gvr_delegate.h" | 12 #include "device/vr/android/gvr/gvr_delegate.h" |
13 #include "device/vr/android/gvr/gvr_device_provider.h" | 13 #include "device/vr/android/gvr/gvr_device_provider.h" |
14 #include "device/vr/vr_device_manager.h" | 14 #include "device/vr/vr_device_manager.h" |
15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" | 15 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" |
16 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr_types.h" | 16 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr_types.h" |
17 #include "ui/gfx/transform.h" | 17 #include "ui/gfx/transform.h" |
18 #include "ui/gfx/transform_util.h" | 18 #include "ui/gfx/transform_util.h" |
19 | 19 |
20 namespace device { | 20 namespace device { |
21 | 21 |
22 GvrDevice::GvrDevice(GvrDeviceProvider* provider, GvrDelegate* delegate) | 22 GvrDevice::GvrDevice(GvrDeviceProvider* provider) |
23 : VRDevice(), delegate_(delegate), gvr_provider_(provider) {} | 23 : VRDevice(), gvr_provider_(provider) {} |
24 | 24 |
25 GvrDevice::~GvrDevice() {} | 25 GvrDevice::~GvrDevice() {} |
26 | 26 |
27 mojom::VRDisplayInfoPtr GvrDevice::GetVRDevice() { | 27 void GvrDevice::GetVRDevice( |
28 TRACE_EVENT0("input", "GvrDevice::GetVRDevice"); | 28 const base::Callback<void(mojom::VRDisplayInfoPtr)>& callback) { |
29 | 29 GvrDelegate* delegate = GetGvrDelegate(); |
30 mojom::VRDisplayInfoPtr device = mojom::VRDisplayInfo::New(); | 30 if (delegate) { |
31 | 31 delegate->CreateVRDisplayInfo(callback, id()); |
32 device->index = id(); | 32 } else { |
33 | 33 callback.Run(nullptr); |
34 device->capabilities = mojom::VRDisplayCapabilities::New(); | |
35 device->capabilities->hasOrientation = true; | |
36 device->capabilities->hasPosition = false; | |
37 device->capabilities->hasExternalDisplay = false; | |
38 device->capabilities->canPresent = true; | |
39 | |
40 device->leftEye = mojom::VREyeParameters::New(); | |
41 device->rightEye = mojom::VREyeParameters::New(); | |
42 mojom::VREyeParametersPtr& left_eye = device->leftEye; | |
43 mojom::VREyeParametersPtr& right_eye = device->rightEye; | |
44 | |
45 left_eye->fieldOfView = mojom::VRFieldOfView::New(); | |
46 right_eye->fieldOfView = mojom::VRFieldOfView::New(); | |
47 | |
48 left_eye->offset.resize(3); | |
49 right_eye->offset.resize(3); | |
50 | |
51 // Set the render target size to "invalid" to indicate that | |
52 // we can't render into it yet. Other code uses this to check | |
53 // for valid state. | |
54 gvr::Sizei render_target_size = kInvalidRenderTargetSize; | |
55 left_eye->renderWidth = render_target_size.width / 2; | |
56 left_eye->renderHeight = render_target_size.height; | |
57 | |
58 right_eye->renderWidth = left_eye->renderWidth; | |
59 right_eye->renderHeight = left_eye->renderHeight; | |
60 | |
61 gvr::GvrApi* gvr_api = GetGvrApi(); | |
62 if (!gvr_api) { | |
63 // We may not be able to get an instance of GvrApi right away, so | |
64 // stub in some data till we have one. | |
65 device->displayName = "Unknown"; | |
66 | |
67 left_eye->fieldOfView->upDegrees = 45; | |
68 left_eye->fieldOfView->downDegrees = 45; | |
69 left_eye->fieldOfView->leftDegrees = 45; | |
70 left_eye->fieldOfView->rightDegrees = 45; | |
71 | |
72 right_eye->fieldOfView->upDegrees = 45; | |
73 right_eye->fieldOfView->downDegrees = 45; | |
74 right_eye->fieldOfView->leftDegrees = 45; | |
75 right_eye->fieldOfView->rightDegrees = 45; | |
76 | |
77 left_eye->offset[0] = -0.0; | |
78 left_eye->offset[1] = -0.0; | |
79 left_eye->offset[2] = -0.03; | |
80 | |
81 right_eye->offset[0] = 0.0; | |
82 right_eye->offset[1] = 0.0; | |
83 right_eye->offset[2] = 0.03; | |
84 | |
85 // Tell the delegate not to draw yet, to avoid a race condition | |
86 // (and visible wobble) on entering VR. | |
87 if (delegate_) { | |
88 delegate_->SetWebVRRenderSurfaceSize(kInvalidRenderTargetSize.width, | |
89 kInvalidRenderTargetSize.height); | |
90 } | |
91 | |
92 return device; | |
93 } | 34 } |
94 | |
95 // In compositor mode, we have to use the current compositor window's | |
96 // surface size. Would be nice to change it, but that needs more browser | |
97 // internals to be modified. TODO(klausw,crbug.com/655722): remove this once | |
98 // we can pick our own surface size. | |
99 gvr::Sizei compositor_size = delegate_->GetWebVRCompositorSurfaceSize(); | |
100 left_eye->renderWidth = compositor_size.width / 2; | |
101 left_eye->renderHeight = compositor_size.height; | |
102 right_eye->renderWidth = left_eye->renderWidth; | |
103 right_eye->renderHeight = left_eye->renderHeight; | |
104 | |
105 std::string vendor = gvr_api->GetViewerVendor(); | |
106 std::string model = gvr_api->GetViewerModel(); | |
107 device->displayName = vendor + " " + model; | |
108 | |
109 gvr::BufferViewportList gvr_buffer_viewports = | |
110 gvr_api->CreateEmptyBufferViewportList(); | |
111 gvr_buffer_viewports.SetToRecommendedBufferViewports(); | |
112 | |
113 gvr::BufferViewport eye_viewport = gvr_api->CreateBufferViewport(); | |
114 gvr_buffer_viewports.GetBufferViewport(GVR_LEFT_EYE, &eye_viewport); | |
115 gvr::Rectf eye_fov = eye_viewport.GetSourceFov(); | |
116 left_eye->fieldOfView->upDegrees = eye_fov.top; | |
117 left_eye->fieldOfView->downDegrees = eye_fov.bottom; | |
118 left_eye->fieldOfView->leftDegrees = eye_fov.left; | |
119 left_eye->fieldOfView->rightDegrees = eye_fov.right; | |
120 | |
121 eye_viewport = gvr_api->CreateBufferViewport(); | |
122 gvr_buffer_viewports.GetBufferViewport(GVR_RIGHT_EYE, &eye_viewport); | |
123 eye_fov = eye_viewport.GetSourceFov(); | |
124 right_eye->fieldOfView->upDegrees = eye_fov.top; | |
125 right_eye->fieldOfView->downDegrees = eye_fov.bottom; | |
126 right_eye->fieldOfView->leftDegrees = eye_fov.left; | |
127 right_eye->fieldOfView->rightDegrees = eye_fov.right; | |
128 | |
129 gvr::Mat4f left_eye_mat = gvr_api->GetEyeFromHeadMatrix(GVR_LEFT_EYE); | |
130 left_eye->offset[0] = -left_eye_mat.m[0][3]; | |
131 left_eye->offset[1] = -left_eye_mat.m[1][3]; | |
132 left_eye->offset[2] = -left_eye_mat.m[2][3]; | |
133 | |
134 gvr::Mat4f right_eye_mat = gvr_api->GetEyeFromHeadMatrix(GVR_RIGHT_EYE); | |
135 right_eye->offset[0] = -right_eye_mat.m[0][3]; | |
136 right_eye->offset[1] = -right_eye_mat.m[1][3]; | |
137 right_eye->offset[2] = -right_eye_mat.m[2][3]; | |
138 | |
139 if (delegate_) { | |
140 delegate_->SetWebVRRenderSurfaceSize(2 * left_eye->renderWidth, | |
141 left_eye->renderHeight); | |
142 } | |
143 | |
144 return device; | |
145 } | 35 } |
146 | 36 |
147 void GvrDevice::ResetPose() { | 37 void GvrDevice::ResetPose() { |
148 gvr::GvrApi* gvr_api = GetGvrApi(); | 38 GvrDelegate* delegate = GetGvrDelegate(); |
149 | 39 if (delegate) |
150 // Should never call RecenterTracking when using with Daydream viewers. On | 40 delegate->ResetPose(); |
151 // those devices recentering should only be done via the controller. | |
152 if (gvr_api && gvr_api->GetViewerType() == GVR_VIEWER_TYPE_CARDBOARD) | |
153 gvr_api->RecenterTracking(); | |
154 } | 41 } |
155 | 42 |
156 void GvrDevice::RequestPresent(const base::Callback<void(bool)>& callback) { | 43 void GvrDevice::RequestPresent(const base::Callback<void(bool)>& callback) { |
157 gvr_provider_->RequestPresent(callback); | 44 gvr_provider_->RequestPresent(callback); |
158 } | 45 } |
159 | 46 |
160 void GvrDevice::SetSecureOrigin(bool secure_origin) { | 47 void GvrDevice::SetSecureOrigin(bool secure_origin) { |
161 secure_origin_ = secure_origin; | 48 secure_origin_ = secure_origin; |
162 if (delegate_) | 49 GvrDelegate* delegate = GetGvrDelegate(); |
163 delegate_->SetWebVRSecureOrigin(secure_origin_); | 50 if (delegate) |
| 51 delegate->SetWebVRSecureOrigin(secure_origin_); |
164 } | 52 } |
165 | 53 |
166 void GvrDevice::ExitPresent() { | 54 void GvrDevice::ExitPresent() { |
167 gvr_provider_->ExitPresent(); | 55 gvr_provider_->ExitPresent(); |
168 OnExitPresent(); | 56 OnExitPresent(); |
169 } | 57 } |
170 | 58 |
171 void GvrDevice::SubmitFrame(mojom::VRPosePtr pose) { | 59 void GvrDevice::SubmitFrame(mojom::VRPosePtr pose) { |
172 if (delegate_) | 60 GvrDelegate* delegate = GetGvrDelegate(); |
173 delegate_->SubmitWebVRFrame(); | 61 if (delegate) |
| 62 delegate->SubmitWebVRFrame(); |
174 } | 63 } |
175 | 64 |
176 void GvrDevice::UpdateLayerBounds(int16_t frame_index, | 65 void GvrDevice::UpdateLayerBounds(int16_t frame_index, |
177 mojom::VRLayerBoundsPtr left_bounds, | 66 mojom::VRLayerBoundsPtr left_bounds, |
178 mojom::VRLayerBoundsPtr right_bounds) { | 67 mojom::VRLayerBoundsPtr right_bounds) { |
179 if (!delegate_) | 68 GvrDelegate* delegate = GetGvrDelegate(); |
| 69 if (!delegate) |
180 return; | 70 return; |
181 | 71 |
182 gvr::Rectf left_gvr_bounds; | 72 gvr::Rectf left_gvr_bounds; |
183 left_gvr_bounds.left = left_bounds->left; | 73 left_gvr_bounds.left = left_bounds->left; |
184 left_gvr_bounds.top = 1.0f - left_bounds->top; | 74 left_gvr_bounds.top = 1.0f - left_bounds->top; |
185 left_gvr_bounds.right = left_bounds->left + left_bounds->width; | 75 left_gvr_bounds.right = left_bounds->left + left_bounds->width; |
186 left_gvr_bounds.bottom = 1.0f - (left_bounds->top + left_bounds->height); | 76 left_gvr_bounds.bottom = 1.0f - (left_bounds->top + left_bounds->height); |
187 | 77 |
188 gvr::Rectf right_gvr_bounds; | 78 gvr::Rectf right_gvr_bounds; |
189 right_gvr_bounds.left = right_bounds->left; | 79 right_gvr_bounds.left = right_bounds->left; |
190 right_gvr_bounds.top = 1.0f - right_bounds->top; | 80 right_gvr_bounds.top = 1.0f - right_bounds->top; |
191 right_gvr_bounds.right = right_bounds->left + right_bounds->width; | 81 right_gvr_bounds.right = right_bounds->left + right_bounds->width; |
192 right_gvr_bounds.bottom = 1.0f - (right_bounds->top + right_bounds->height); | 82 right_gvr_bounds.bottom = 1.0f - (right_bounds->top + right_bounds->height); |
193 | 83 |
194 delegate_->UpdateWebVRTextureBounds(frame_index, left_gvr_bounds, | 84 delegate->UpdateWebVRTextureBounds(frame_index, left_gvr_bounds, |
195 right_gvr_bounds); | 85 right_gvr_bounds); |
196 } | 86 } |
197 | 87 |
198 void GvrDevice::GetVRVSyncProvider(mojom::VRVSyncProviderRequest request) { | 88 void GvrDevice::GetVRVSyncProvider(mojom::VRVSyncProviderRequest request) { |
199 if (delegate_) | 89 GvrDelegate* delegate = GetGvrDelegate(); |
200 delegate_->OnVRVsyncProviderRequest(std::move(request)); | 90 if (delegate) |
| 91 delegate->OnVRVsyncProviderRequest(std::move(request)); |
201 } | 92 } |
202 | 93 |
203 void GvrDevice::SetDelegate(GvrDelegate* delegate) { | 94 void GvrDevice::OnDelegateChanged() { |
204 delegate_ = delegate; | 95 GvrDelegate* delegate = GetGvrDelegate(); |
| 96 if (!delegate || !delegate->SupportsPresentation()) |
| 97 OnExitPresent(); |
| 98 // Notify the clients that this device has changed |
| 99 if (delegate) |
| 100 delegate->SetWebVRSecureOrigin(secure_origin_); |
205 | 101 |
206 // Notify the clients that this device has changed | 102 OnChanged(); |
207 if (delegate_) { | |
208 delegate_->SetWebVRSecureOrigin(secure_origin_); | |
209 OnChanged(); | |
210 } | |
211 } | 103 } |
212 | 104 |
213 gvr::GvrApi* GvrDevice::GetGvrApi() { | 105 GvrDelegate* GvrDevice::GetGvrDelegate() { |
214 if (!delegate_) | 106 GvrDelegateProvider* delegate_provider = GvrDelegateProvider::GetInstance(); |
215 return nullptr; | 107 if (delegate_provider) |
216 | 108 return delegate_provider->GetDelegate(); |
217 return delegate_->gvr_api(); | 109 return nullptr; |
218 } | 110 } |
219 | 111 |
220 } // namespace device | 112 } // namespace device |
OLD | NEW |