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 #ifndef CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_GL_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_GL_H_ |
6 #define CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_GL_H_ | 6 #define CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_GL_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/cancelable_callback.h" | 10 #include "base/cancelable_callback.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "device/vr/android/gvr/gvr_delegate.h" | 15 #include "device/vr/android/gvr/gvr_delegate.h" |
| 16 #include "device/vr/vr_service.mojom.h" |
| 17 #include "mojo/public/cpp/bindings/binding.h" |
16 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" | 18 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr.h" |
17 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr_types.h" | 19 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/
gvr_types.h" |
18 #include "ui/gfx/native_widget_types.h" | 20 #include "ui/gfx/native_widget_types.h" |
19 | 21 |
20 namespace base { | 22 namespace base { |
21 class ListValue; | 23 class ListValue; |
22 } | 24 } |
23 | 25 |
24 namespace blink { | 26 namespace blink { |
25 class WebInputEvent; | 27 class WebInputEvent; |
26 } | 28 } |
27 | 29 |
28 namespace gl { | 30 namespace gl { |
29 class GLContext; | 31 class GLContext; |
30 class GLSurface; | 32 class GLSurface; |
31 class ScopedJavaSurface; | 33 class ScopedJavaSurface; |
32 class SurfaceTexture; | 34 class SurfaceTexture; |
33 } | 35 } |
34 | 36 |
35 namespace vr_shell { | 37 namespace vr_shell { |
36 | 38 |
37 class UiScene; | 39 class UiScene; |
38 class VrController; | 40 class VrController; |
39 class VrInputManager; | 41 class VrInputManager; |
40 class VrShell; | 42 class VrShell; |
| 43 class VrShellDelegate; |
41 class VrShellRenderer; | 44 class VrShellRenderer; |
42 struct ContentRectangle; | 45 struct ContentRectangle; |
43 | 46 |
44 // This class manages all GLThread owned objects and GL rendering for VrShell. | 47 // This class manages all GLThread owned objects and GL rendering for VrShell. |
45 // It is not threadsafe and must only be used on the GL thread. | 48 // It is not threadsafe and must only be used on the GL thread. |
46 class VrShellGl { | 49 class VrShellGl : public device::mojom::VRVSyncProvider { |
47 public: | 50 public: |
48 enum class InputTarget { | 51 enum class InputTarget { |
49 NONE = 0, | 52 NONE = 0, |
50 CONTENT, | 53 CONTENT, |
51 UI | 54 UI |
52 }; | 55 }; |
53 | 56 |
54 VrShellGl( | 57 VrShellGl( |
55 const base::WeakPtr<VrShell>& weak_vr_shell, | 58 const base::WeakPtr<VrShell>& weak_vr_shell, |
56 const base::WeakPtr<VrInputManager>& content_input_manager, | 59 const base::WeakPtr<VrInputManager>& content_input_manager, |
57 const base::WeakPtr<VrInputManager>& ui_input_manager, | 60 const base::WeakPtr<VrInputManager>& ui_input_manager, |
| 61 const base::WeakPtr<VrShellDelegate>& delegate_provider, |
58 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, | 62 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, |
59 gvr_context* gvr_api, | 63 gvr_context* gvr_api, |
60 bool initially_web_vr, | 64 bool initially_web_vr, |
61 bool reprojected_rendering); | 65 bool reprojected_rendering); |
62 ~VrShellGl(); | 66 ~VrShellGl(); |
63 | 67 |
64 void Initialize(); | 68 void Initialize(); |
65 void InitializeGl(gfx::AcceleratedWidget window); | 69 void InitializeGl(gfx::AcceleratedWidget window); |
66 | 70 |
67 void DrawFrame(); | |
68 | |
69 void OnTriggerEvent(); | 71 void OnTriggerEvent(); |
70 void OnPause(); | 72 void OnPause(); |
71 void OnResume(); | 73 void OnResume(); |
72 | 74 |
73 void SetWebVrMode(bool enabled); | 75 void SetWebVrMode(bool enabled); |
74 void ContentBoundsChanged(int width, int height); | 76 void ContentBoundsChanged(int width, int height); |
75 void ContentPhysicalBoundsChanged(int width, int height); | 77 void ContentPhysicalBoundsChanged(int width, int height); |
76 void UIBoundsChanged(int width, int height); | 78 void UIBoundsChanged(int width, int height); |
77 void UIPhysicalBoundsChanged(int width, int height); | 79 void UIPhysicalBoundsChanged(int width, int height); |
78 base::WeakPtr<VrShellGl> GetWeakPtr(); | 80 base::WeakPtr<VrShellGl> GetWeakPtr(); |
79 | 81 |
80 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, | 82 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, |
81 const gvr::Rectf& right_bounds); | 83 const gvr::Rectf& right_bounds); |
82 gvr::GvrApi* gvr_api(); | 84 gvr::GvrApi* gvr_api(); |
83 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num); | 85 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num); |
84 gvr::Sizei GetWebVRCompositorSurfaceSize(); | 86 gvr::Sizei GetWebVRCompositorSurfaceSize(); |
85 | 87 |
86 void UpdateScene(std::unique_ptr<base::ListValue> commands); | 88 void UpdateScene(std::unique_ptr<base::ListValue> commands); |
87 | 89 |
| 90 void UpdateVSyncInterval(long timebase_nanos, double interval_seconds); |
| 91 |
| 92 void OnRequest(device::mojom::VRVSyncProviderRequest request); |
| 93 |
88 private: | 94 private: |
89 void GvrInit(gvr_context* gvr_api); | 95 void GvrInit(gvr_context* gvr_api); |
90 void InitializeRenderer(); | 96 void InitializeRenderer(); |
| 97 void DrawFrame(); |
91 void DrawVrShell(const gvr::Mat4f& head_pose, gvr::Frame &frame); | 98 void DrawVrShell(const gvr::Mat4f& head_pose, gvr::Frame &frame); |
92 void DrawUiView(const gvr::Mat4f* head_pose, | 99 void DrawUiView(const gvr::Mat4f* head_pose, |
93 const std::vector<const ContentRectangle*>& elements, | 100 const std::vector<const ContentRectangle*>& elements, |
94 const gvr::Sizei& render_size, int viewport_offset); | 101 const gvr::Sizei& render_size, int viewport_offset); |
95 void DrawElements(const gvr::Mat4f& render_matrix, | 102 void DrawElements(const gvr::Mat4f& render_matrix, |
96 const std::vector<const ContentRectangle*>& elements); | 103 const std::vector<const ContentRectangle*>& elements); |
97 void DrawCursor(const gvr::Mat4f& render_matrix); | 104 void DrawCursor(const gvr::Mat4f& render_matrix); |
98 void DrawWebVr(); | 105 void DrawWebVr(); |
99 bool WebVrPoseByteIsValid(int pose_index_byte); | 106 bool WebVrPoseByteIsValid(int pose_index_byte); |
100 | 107 |
101 void UpdateController(const gvr::Vec3f& forward_vector); | 108 void UpdateController(const gvr::Vec3f& forward_vector); |
102 void SendEventsToTarget(InputTarget input_target, int pixel_x, | 109 void SendEventsToTarget(InputTarget input_target, int pixel_x, |
103 int pixel_y); | 110 int pixel_y); |
104 void SendGesture(InputTarget input_target, | 111 void SendGesture(InputTarget input_target, |
105 std::unique_ptr<blink::WebInputEvent> event); | 112 std::unique_ptr<blink::WebInputEvent> event); |
106 | 113 |
107 void OnUIFrameAvailable(); | 114 void OnUIFrameAvailable(); |
108 void OnContentFrameAvailable(); | 115 void OnContentFrameAvailable(); |
| 116 bool GetPixelEncodedPoseIndexByte(int* pose_index); |
109 | 117 |
110 void UpdateVSyncParameters(const base::TimeTicks timebase, | 118 void OnVSync(); |
111 const base::TimeDelta interval); | 119 |
112 void ScheduleNextDrawFrame(); | 120 // VRVSyncProvider |
| 121 void GetVSync(const GetVSyncCallback& callback) override; |
113 | 122 |
114 void ForceExitVr(); | 123 void ForceExitVr(); |
115 | 124 |
| 125 device::mojom::VRPosePtr GetPose(); |
| 126 |
116 // samplerExternalOES texture data for UI content image. | 127 // samplerExternalOES texture data for UI content image. |
117 int ui_texture_id_ = 0; | 128 int ui_texture_id_ = 0; |
118 // samplerExternalOES texture data for main content image. | 129 // samplerExternalOES texture data for main content image. |
119 int content_texture_id_ = 0; | 130 int content_texture_id_ = 0; |
120 | 131 |
121 std::unique_ptr<UiScene> scene_; | 132 std::unique_ptr<UiScene> scene_; |
122 | 133 |
123 scoped_refptr<gl::GLSurface> surface_; | 134 scoped_refptr<gl::GLSurface> surface_; |
124 scoped_refptr<gl::GLContext> context_; | 135 scoped_refptr<gl::GLContext> context_; |
125 scoped_refptr<gl::SurfaceTexture> ui_surface_texture_; | 136 scoped_refptr<gl::SurfaceTexture> ui_surface_texture_; |
(...skipping 28 matching lines...) Expand all Loading... |
154 int content_tex_css_width_ = 0; | 165 int content_tex_css_width_ = 0; |
155 int content_tex_css_height_ = 0; | 166 int content_tex_css_height_ = 0; |
156 gvr::Sizei content_tex_physical_size_ = {0, 0}; | 167 gvr::Sizei content_tex_physical_size_ = {0, 0}; |
157 gvr::Sizei ui_tex_physical_size_ = {0, 0}; | 168 gvr::Sizei ui_tex_physical_size_ = {0, 0}; |
158 | 169 |
159 // The pose ring buffer size must be a power of two to avoid glitches when | 170 // The pose ring buffer size must be a power of two to avoid glitches when |
160 // the pose index wraps around. It should be large enough to handle the | 171 // the pose index wraps around. It should be large enough to handle the |
161 // current backlog of poses which is 2-3 frames. | 172 // current backlog of poses which is 2-3 frames. |
162 static constexpr int kPoseRingBufferSize = 8; | 173 static constexpr int kPoseRingBufferSize = 8; |
163 std::vector<gvr::Mat4f> webvr_head_pose_; | 174 std::vector<gvr::Mat4f> webvr_head_pose_; |
164 std::vector<bool> webvr_head_pose_valid_; | |
165 int webvr_texture_id_ = 0; | 175 int webvr_texture_id_ = 0; |
166 bool web_vr_mode_; | 176 bool web_vr_mode_; |
167 bool ready_to_draw_ = false; | 177 bool ready_to_draw_ = false; |
168 bool surfaceless_rendering_; | 178 bool surfaceless_rendering_; |
169 | 179 |
170 std::unique_ptr<VrController> controller_; | 180 std::unique_ptr<VrController> controller_; |
171 | 181 |
172 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 182 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
173 base::CancelableClosure draw_task_; | 183 base::CancelableClosure vsync_task_; |
174 base::TimeTicks vsync_timebase_; | 184 base::TimeTicks vsync_timebase_; |
175 base::TimeDelta vsync_interval_; | 185 base::TimeDelta vsync_interval_; |
176 | 186 |
| 187 base::TimeDelta pending_time_; |
| 188 bool pending_vsync_ = false; |
| 189 GetVSyncCallback callback_; |
| 190 bool received_frame_ = false; |
| 191 mojo::Binding<device::mojom::VRVSyncProvider> binding_; |
| 192 |
177 base::WeakPtr<VrShell> weak_vr_shell_; | 193 base::WeakPtr<VrShell> weak_vr_shell_; |
178 base::WeakPtr<VrInputManager> content_input_manager_; | 194 base::WeakPtr<VrInputManager> content_input_manager_; |
179 base::WeakPtr<VrInputManager> ui_input_manager_; | 195 base::WeakPtr<VrInputManager> ui_input_manager_; |
| 196 base::WeakPtr<VrShellDelegate> delegate_provider_; |
180 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 197 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
181 | 198 |
| 199 uint32_t pose_index_ = 1; |
| 200 int last_pose_ = 0; |
| 201 |
182 base::WeakPtrFactory<VrShellGl> weak_ptr_factory_; | 202 base::WeakPtrFactory<VrShellGl> weak_ptr_factory_; |
183 | 203 |
184 DISALLOW_COPY_AND_ASSIGN(VrShellGl); | 204 DISALLOW_COPY_AND_ASSIGN(VrShellGl); |
185 }; | 205 }; |
186 | 206 |
187 } // namespace vr_shell | 207 } // namespace vr_shell |
188 | 208 |
189 #endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_GL_H_ | 209 #endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_GL_H_ |
OLD | NEW |