Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: chrome/browser/android/vr_shell/vr_shell.h

Issue 2562733002: Implement our own GLThread for VR Shell. (Closed)
Patch Set: Minor cleanup Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_H_ 5 #ifndef CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_H_
6 #define CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_H_ 6 #define CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_H_
7 7
8 #include <jni.h> 8 #include <jni.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <queue>
12 11
13 #include "base/android/jni_weak_ref.h" 12 #include "base/android/jni_weak_ref.h"
14 #include "base/callback.h" 13 #include "base/callback.h"
15 #include "base/macros.h" 14 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
17 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/lock.h"
19 #include "chrome/browser/android/vr_shell/vr_math.h"
20 #include "content/public/browser/web_contents_observer.h" 17 #include "content/public/browser/web_contents_observer.h"
21 #include "device/vr/android/gvr/gvr_delegate.h" 18 #include "device/vr/android/gvr/gvr_delegate.h"
22 #include "third_party/WebKit/public/platform/WebInputEvent.h"
23 #include "third_party/gvr-android-sdk/src/ndk/include/vr/gvr/capi/include/gvr.h" 19 #include "third_party/gvr-android-sdk/src/ndk/include/vr/gvr/capi/include/gvr.h"
24 #include "third_party/gvr-android-sdk/src/ndk/include/vr/gvr/capi/include/gvr_ty pes.h" 20 #include "third_party/gvr-android-sdk/src/ndk/include/vr/gvr/capi/include/gvr_ty pes.h"
25 21
26 using blink::WebInputEvent; 22 namespace base {
23 class Thread;
24 }
27 25
28 namespace content { 26 namespace content {
29 class WebContents; 27 class WebContents;
30 } 28 }
31 29
32 namespace ui { 30 namespace ui {
33 class WindowAndroid; 31 class WindowAndroid;
34 } 32 }
35 33
36 namespace vr_shell { 34 namespace vr_shell {
37 35
38 class UiInterface; 36 class UiInterface;
39 class UiScene; 37 class UiScene;
40 class VrCompositor; 38 class VrCompositor;
41 class VrController;
42 class VrInputManager; 39 class VrInputManager;
43 class VrMetricsHelper; 40 class VrMetricsHelper;
44 class VrShellDelegate; 41 class VrShellDelegate;
45 class VrShellRenderer;
46 class VrWebContentsObserver; 42 class VrWebContentsObserver;
47 struct ContentRectangle;
48 43
49 enum UiAction { 44 enum UiAction {
50 HISTORY_BACK = 0, 45 HISTORY_BACK = 0,
51 HISTORY_FORWARD, 46 HISTORY_FORWARD,
52 RELOAD, 47 RELOAD,
53 ZOOM_OUT, 48 ZOOM_OUT,
54 ZOOM_IN, 49 ZOOM_IN,
55 RELOAD_UI 50 RELOAD_UI
56 }; 51 };
57 52
58 enum InputTarget {
59 NONE = 0,
60 CONTENT,
61 UI
62 };
63
64 class VrMetricsHelper; 53 class VrMetricsHelper;
65 54
66 class VrShell : public device::GvrDelegate, content::WebContentsObserver { 55 class VrShell : public device::GvrDelegate, content::WebContentsObserver {
67 public: 56 public:
68 VrShell(JNIEnv* env, jobject obj, 57 VrShell(JNIEnv* env, jobject obj,
69 content::WebContents* main_contents, 58 content::WebContents* main_contents,
70 ui::WindowAndroid* content_window, 59 ui::WindowAndroid* content_window,
71 content::WebContents* ui_contents, 60 content::WebContents* ui_contents,
72 ui::WindowAndroid* ui_window, 61 ui::WindowAndroid* ui_window,
73 bool for_web_vr); 62 bool for_web_vr,
63 VrShellDelegate* delegate,
64 gvr_context* gvr_api);
74 65
75 void UpdateCompositorLayersOnUI( 66 enum InputTarget {
dcheng 2016/12/12 20:52:34 Nit: consider enum class in new code (the enumerat
mthiesse 2016/12/12 23:26:55 Done. (Fixed in vr_shell_gl.h, I missed deleting t
76 JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); 67 NONE = 0,
77 void DestroyOnUI(JNIEnv* env, 68 CONTENT,
78 const base::android::JavaParamRef<jobject>& obj); 69 UI
79 void SetDelegateOnUI(JNIEnv* env, 70 };
80 const base::android::JavaParamRef<jobject>& obj, 71
81 const base::android::JavaParamRef<jobject>& delegate); 72 void LoadUIContent(JNIEnv* env,
82 void GvrInitOnGL(JNIEnv* env,
83 const base::android::JavaParamRef<jobject>& obj,
84 jlong native_gvr_api);
85 void InitializeGlOnGL(JNIEnv* env,
86 const base::android::JavaParamRef<jobject>& obj,
87 jint content_texture_handle,
88 jint ui_texture_handle);
89 void DrawFrameOnGL(JNIEnv* env,
90 const base::android::JavaParamRef<jobject>& obj); 73 const base::android::JavaParamRef<jobject>& obj);
91 void OnTriggerEventOnUI(JNIEnv* env, 74 void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
75 void OnTriggerEvent(JNIEnv* env,
92 const base::android::JavaParamRef<jobject>& obj); 76 const base::android::JavaParamRef<jobject>& obj);
93 void OnPauseOnUI(JNIEnv* env, 77 void OnPause(JNIEnv* env,
94 const base::android::JavaParamRef<jobject>& obj); 78 const base::android::JavaParamRef<jobject>& obj);
95 void OnResumeOnUI(JNIEnv* env, 79 void OnResume(JNIEnv* env,
96 const base::android::JavaParamRef<jobject>& obj); 80 const base::android::JavaParamRef<jobject>& obj);
97 void SetWebVrModeOnUI(JNIEnv* env, 81 void SetWebVrMode(JNIEnv* env,
98 const base::android::JavaParamRef<jobject>& obj, 82 const base::android::JavaParamRef<jobject>& obj,
99 bool enabled); 83 bool enabled);
100 84
101 void ContentWebContentsDestroyedOnUI(); 85 void ContentWebContentsDestroyed();
102 // Called when our WebContents have been hidden. Usually a sign that something 86 // Called when our WebContents have been hidden. Usually a sign that something
103 // like another tab placed in front of it. 87 // like another tab placed in front of it.
104 void ContentWasHiddenOnUI(); 88 void ContentWasHidden();
105 89
106 // html/js UI hooks. 90 // html/js UI hooks.
107 static base::WeakPtr<VrShell> GetWeakPtrOnUI( 91 static base::WeakPtr<VrShell> GetWeakPtr(
108 const content::WebContents* web_contents); 92 const content::WebContents* web_contents);
109 // TODO(mthiesse): Clean up threading around Scene. 93
110 UiScene* GetSceneOnGL(); 94 // Returns a pointer to the scene owned by the GL thread. Do not dereference
95 // this pointer off of the GL thread.
96 UiScene* GetScene();
111 // TODO(mthiesse): Clean up threading around UiInterface. 97 // TODO(mthiesse): Clean up threading around UiInterface.
112 UiInterface* GetUiInterfaceOnGL(); 98 UiInterface* GetUiInterface();
113 void OnDomContentsLoadedOnUI(); 99 void OnDomContentsLoaded();
114 100
115 // device::GvrDelegate implementation 101 // device::GvrDelegate implementation
116 // TODO(mthiesse): Clean up threading around GVR API. These functions are 102 // TODO(mthiesse): Clean up threading around GVR API. These functions are
117 // called on the UI thread, but use GL thread objects in a non-threadsafe way. 103 // called on the UI thread, but use GL thread objects in a non-threadsafe way.
118 void SetWebVRSecureOrigin(bool secure_origin) override; 104 void SetWebVRSecureOrigin(bool secure_origin) override;
119 void SubmitWebVRFrame() override; 105 void SubmitWebVRFrame() override;
120 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, 106 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds,
121 const gvr::Rectf& right_bounds) override; 107 const gvr::Rectf& right_bounds) override;
122 gvr::GvrApi* gvr_api() override; 108 gvr::GvrApi* gvr_api() override;
123 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num) override; 109 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num) override;
124 void SetWebVRRenderSurfaceSize(int width, int height) override; 110 void SetWebVRRenderSurfaceSize(int width, int height) override;
125 gvr::Sizei GetWebVRCompositorSurfaceSize() override; 111 gvr::Sizei GetWebVRCompositorSurfaceSize() override;
126 112
127 void SurfacesChangedOnUI( 113 void SurfacesChanged(jobject content_surface, jobject ui_surface);
128 JNIEnv* env, 114 void GvrDelegateReady();
129 const base::android::JavaParamRef<jobject>& object,
130 const base::android::JavaParamRef<jobject>& content_surface,
131 const base::android::JavaParamRef<jobject>& ui_surface);
132 115
133 void ContentBoundsChangedOnUI( 116 void ContentBoundsChanged(
134 JNIEnv* env, 117 JNIEnv* env,
135 const base::android::JavaParamRef<jobject>& object, 118 const base::android::JavaParamRef<jobject>& object,
136 jint width, jint height, jfloat dpr); 119 jint width, jint height, jfloat dpr);
137 120
138 void UIBoundsChangedOnUI( 121 void UIBoundsChanged(
139 JNIEnv* env, 122 JNIEnv* env,
140 const base::android::JavaParamRef<jobject>& object, 123 const base::android::JavaParamRef<jobject>& object,
141 jint width, jint height, jfloat dpr); 124 jint width, jint height, jfloat dpr);
142 125
143 // Called from non-render thread to queue a callback onto the render thread. 126 // Called from non-render thread to queue a callback onto the render thread.
144 // The render thread checks for callbacks and processes them between frames. 127 // The render thread checks for callbacks and processes them between frames.
145 void QueueTaskOnUI(base::Callback<void()>& callback); 128 void QueueTask(base::Callback<void()>& callback);
146 129
147 // Perform a UI action triggered by the javascript API. 130 // Perform a UI action triggered by the javascript API.
148 void DoUiActionOnUI(const UiAction action); 131 void DoUiAction(const UiAction action);
149 132
150 void SetContentCssSizeOnUI(float width, float height, float dpr); 133 void SetContentCssSize(float width, float height, float dpr);
151 void SetUiCssSizeOnUI(float width, float height, float dpr); 134 void SetUiCssSize(float width, float height, float dpr);
135
136 void ContentFrameWasResized(bool width_changed);
137
138 void ForceExitVR();
152 139
153 private: 140 private:
154 ~VrShell() override; 141 ~VrShell() override;
155 void LoadUIContentOnUI(); 142 void SetShowingOverscrollGlow(bool showing_glow);
156 void SetShowingOverscrollGlowOnUI(bool showing_glow);
157 void DrawVrShellOnGL(const gvr::Mat4f& head_pose, gvr::Frame &frame);
158 void DrawUiViewOnGL(const gvr::Mat4f* head_pose,
159 const std::vector<const ContentRectangle*>& elements,
160 const gvr::Sizei& render_size, int viewport_offset);
161 void DrawElementsOnGL(const gvr::Mat4f& render_matrix,
162 const std::vector<const ContentRectangle*>& elements);
163 void DrawCursorOnGL(const gvr::Mat4f& render_matrix);
164 void DrawWebVrOnGL();
165 bool WebVrPoseByteIsValidOnGL(int pose_index_byte);
166
167 void UpdateControllerOnGL(const gvr::Vec3f& forward_vector);
168 void SendEventsToTargetOnGL(InputTarget input_target, int pixel_x,
169 int pixel_y);
170 void SendGestureOnGL(InputTarget input_target,
171 std::unique_ptr<blink::WebInputEvent> event);
172
173 void HandleQueuedTasksOnGL();
174 143
175 // content::WebContentsObserver implementation. All called on UI thread. 144 // content::WebContentsObserver implementation. All called on UI thread.
176 void RenderViewHostChanged(content::RenderViewHost* old_host, 145 void RenderViewHostChanged(content::RenderViewHost* old_host,
177 content::RenderViewHost* new_host) override; 146 content::RenderViewHost* new_host) override;
178 void MainFrameWasResized(bool width_changed) override; 147 void MainFrameWasResized(bool width_changed) override;
179 void WebContentsDestroyed() override; 148 void WebContentsDestroyed() override;
180 149
181 // samplerExternalOES texture data for UI content image.
182 jint ui_texture_id_ = 0;
183 // samplerExternalOES texture data for main content image.
184 jint content_texture_id_ = 0;
185
186 std::unique_ptr<UiScene> scene_;
187 std::unique_ptr<UiInterface> html_interface_; 150 std::unique_ptr<UiInterface> html_interface_;
188 151
189 std::unique_ptr<gvr::GvrApi> gvr_api_;
190 std::unique_ptr<gvr::BufferViewportList> buffer_viewport_list_;
191 std::unique_ptr<gvr::BufferViewport> buffer_viewport_;
192 std::unique_ptr<gvr::BufferViewport> headlocked_left_viewport_;
193 std::unique_ptr<gvr::BufferViewport> headlocked_right_viewport_;
194 std::unique_ptr<gvr::BufferViewport> webvr_left_viewport_;
195 std::unique_ptr<gvr::BufferViewport> webvr_right_viewport_;
196 std::unique_ptr<gvr::SwapChain> swap_chain_;
197
198 // Current sizes for the render buffers.
199 gvr::Sizei render_size_primary_;
200 gvr::Sizei render_size_headlocked_;
201
202 // Intended size for the primary render buffer by UI mode.
203 // For WebVR, a size of 0x0 is used to indicate "not yet ready"
204 // to suppress rendering while still initializing.
205 gvr::Sizei render_size_primary_webvr_ = device::kInvalidRenderTargetSize;
206 gvr::Sizei render_size_primary_vrshell_;
207
208 std::queue<base::Callback<void()>> task_queue_;
209 base::Lock task_queue_lock_;
210 base::Lock gvr_init_lock_;
211
212 std::unique_ptr<VrCompositor> content_compositor_; 152 std::unique_ptr<VrCompositor> content_compositor_;
213 content::WebContents* main_contents_; 153 content::WebContents* main_contents_;
214 std::unique_ptr<VrCompositor> ui_compositor_; 154 std::unique_ptr<VrCompositor> ui_compositor_;
215 content::WebContents* ui_contents_; 155 content::WebContents* ui_contents_;
216 std::unique_ptr<VrWebContentsObserver> vr_web_contents_observer_; 156 std::unique_ptr<VrWebContentsObserver> vr_web_contents_observer_;
217 157
218 VrShellDelegate* delegate_ = nullptr; 158 VrShellDelegate* delegate_ = nullptr;
219 std::unique_ptr<VrShellRenderer> vr_shell_renderer_;
220 base::android::ScopedJavaGlobalRef<jobject> j_vr_shell_; 159 base::android::ScopedJavaGlobalRef<jobject> j_vr_shell_;
221 160
222 bool touch_pending_ = false; 161 std::unique_ptr<VrInputManager> content_input_manager_;
223 gvr::Quatf controller_quat_; 162 std::unique_ptr<VrInputManager> ui_input_manager_;
224 163
225 gvr::Vec3f target_point_;
226 const ContentRectangle* target_element_ = nullptr;
227 InputTarget current_input_target_ = NONE;
228 int ui_tex_css_width_ = 0;
229 int ui_tex_css_height_ = 0;
230 int content_tex_css_width_ = 0;
231 int content_tex_css_height_ = 0;
232 gvr::Sizei content_tex_physical_size_ = {0, 0};
233
234 // The pose ring buffer size must be a power of two to avoid glitches when
235 // the pose index wraps around. It should be large enough to handle the
236 // current backlog of poses which is 2-3 frames.
237 static constexpr int kPoseRingBufferSize = 8;
238 std::vector<gvr::Mat4f> webvr_head_pose_;
239 std::vector<bool> webvr_head_pose_valid_;
240 jint webvr_texture_id_ = 0;
241
242 std::unique_ptr<VrController> controller_;
243 std::unique_ptr<VrInputManager> content_input_manager_;
244 base::WeakPtr<VrInputManager> weak_content_input_manager_;
245 std::unique_ptr<VrInputManager> ui_input_manager_;
246 base::WeakPtr<VrInputManager> weak_ui_input_manager_;
247 scoped_refptr<VrMetricsHelper> metrics_helper_; 164 scoped_refptr<VrMetricsHelper> metrics_helper_;
248 165
249 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; 166 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
167 std::unique_ptr<base::Thread> gl_thread_;
250 168
251 base::WeakPtrFactory<VrShell> weak_ptr_factory_; 169 base::WeakPtrFactory<VrShell> weak_ptr_factory_;
252 170
253 DISALLOW_COPY_AND_ASSIGN(VrShell); 171 DISALLOW_COPY_AND_ASSIGN(VrShell);
254 }; 172 };
255 173
256 bool RegisterVrShell(JNIEnv* env); 174 bool RegisterVrShell(JNIEnv* env);
257 175
258 } // namespace vr_shell 176 } // namespace vr_shell
259 177
260 #endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_H_ 178 #endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698