| 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_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 |
| 55 // The native instance of the Java VrShell. This class is not threadsafe and |
| 56 // must only be used on the UI thread. |
| 66 class VrShell : public device::GvrDelegate, content::WebContentsObserver { | 57 class VrShell : public device::GvrDelegate, content::WebContentsObserver { |
| 67 public: | 58 public: |
| 68 VrShell(JNIEnv* env, jobject obj, | 59 VrShell(JNIEnv* env, jobject obj, |
| 69 content::WebContents* main_contents, | 60 content::WebContents* main_contents, |
| 70 ui::WindowAndroid* content_window, | 61 ui::WindowAndroid* content_window, |
| 71 content::WebContents* ui_contents, | 62 content::WebContents* ui_contents, |
| 72 ui::WindowAndroid* ui_window, | 63 ui::WindowAndroid* ui_window, |
| 73 bool for_web_vr); | 64 bool for_web_vr, |
| 65 VrShellDelegate* delegate, |
| 66 gvr_context* gvr_api); |
| 74 | 67 |
| 75 void UpdateCompositorLayersOnUI( | 68 void LoadUIContent(JNIEnv* env, |
| 76 JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); | |
| 77 void DestroyOnUI(JNIEnv* env, | |
| 78 const base::android::JavaParamRef<jobject>& obj); | |
| 79 void SetDelegateOnUI(JNIEnv* env, | |
| 80 const base::android::JavaParamRef<jobject>& obj, | |
| 81 const base::android::JavaParamRef<jobject>& delegate); | |
| 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); | 69 const base::android::JavaParamRef<jobject>& obj); |
| 91 void OnTriggerEventOnUI(JNIEnv* env, | 70 void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); |
| 71 void OnTriggerEvent(JNIEnv* env, |
| 92 const base::android::JavaParamRef<jobject>& obj); | 72 const base::android::JavaParamRef<jobject>& obj); |
| 93 void OnPauseOnUI(JNIEnv* env, | 73 void OnPause(JNIEnv* env, |
| 94 const base::android::JavaParamRef<jobject>& obj); | 74 const base::android::JavaParamRef<jobject>& obj); |
| 95 void OnResumeOnUI(JNIEnv* env, | 75 void OnResume(JNIEnv* env, |
| 96 const base::android::JavaParamRef<jobject>& obj); | 76 const base::android::JavaParamRef<jobject>& obj); |
| 97 void SetWebVrModeOnUI(JNIEnv* env, | 77 void SetWebVrMode(JNIEnv* env, |
| 98 const base::android::JavaParamRef<jobject>& obj, | 78 const base::android::JavaParamRef<jobject>& obj, |
| 99 bool enabled); | 79 bool enabled); |
| 100 | 80 |
| 101 void ContentWebContentsDestroyedOnUI(); | 81 void ContentWebContentsDestroyed(); |
| 102 // Called when our WebContents have been hidden. Usually a sign that something | 82 // Called when our WebContents have been hidden. Usually a sign that something |
| 103 // like another tab placed in front of it. | 83 // like another tab placed in front of it. |
| 104 void ContentWasHiddenOnUI(); | 84 void ContentWasHidden(); |
| 105 | 85 |
| 106 // html/js UI hooks. | 86 // html/js UI hooks. |
| 107 static base::WeakPtr<VrShell> GetWeakPtrOnUI( | 87 static base::WeakPtr<VrShell> GetWeakPtr( |
| 108 const content::WebContents* web_contents); | 88 const content::WebContents* web_contents); |
| 109 // TODO(mthiesse): Clean up threading around Scene. | 89 |
| 110 UiScene* GetSceneOnGL(); | 90 // Returns a pointer to the scene owned by the GL thread. Do not dereference |
| 91 // this pointer off of the GL thread. |
| 92 UiScene* GetScene(); |
| 111 // TODO(mthiesse): Clean up threading around UiInterface. | 93 // TODO(mthiesse): Clean up threading around UiInterface. |
| 112 UiInterface* GetUiInterfaceOnGL(); | 94 UiInterface* GetUiInterface(); |
| 113 void OnDomContentsLoadedOnUI(); | 95 void OnDomContentsLoaded(); |
| 114 | 96 |
| 115 // device::GvrDelegate implementation | 97 // device::GvrDelegate implementation |
| 116 // TODO(mthiesse): Clean up threading around GVR API. These functions are | 98 // 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. | 99 // called on the UI thread, but use GL thread objects in a non-threadsafe way. |
| 118 void SetWebVRSecureOrigin(bool secure_origin) override; | 100 void SetWebVRSecureOrigin(bool secure_origin) override; |
| 119 void SubmitWebVRFrame() override; | 101 void SubmitWebVRFrame() override; |
| 120 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, | 102 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, |
| 121 const gvr::Rectf& right_bounds) override; | 103 const gvr::Rectf& right_bounds) override; |
| 122 gvr::GvrApi* gvr_api() override; | 104 gvr::GvrApi* gvr_api() override; |
| 123 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num) override; | 105 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num) override; |
| 124 void SetWebVRRenderSurfaceSize(int width, int height) override; | 106 void SetWebVRRenderSurfaceSize(int width, int height) override; |
| 125 gvr::Sizei GetWebVRCompositorSurfaceSize() override; | 107 gvr::Sizei GetWebVRCompositorSurfaceSize() override; |
| 126 | 108 |
| 127 void SurfacesChangedOnUI( | 109 void SurfacesChanged(jobject content_surface, jobject ui_surface); |
| 128 JNIEnv* env, | 110 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 | 111 |
| 133 void ContentBoundsChangedOnUI( | 112 void ContentBoundsChanged( |
| 134 JNIEnv* env, | 113 JNIEnv* env, |
| 135 const base::android::JavaParamRef<jobject>& object, | 114 const base::android::JavaParamRef<jobject>& object, |
| 136 jint width, jint height, jfloat dpr); | 115 jint width, jint height, jfloat dpr); |
| 137 | 116 |
| 138 void UIBoundsChangedOnUI( | 117 void UIBoundsChanged( |
| 139 JNIEnv* env, | 118 JNIEnv* env, |
| 140 const base::android::JavaParamRef<jobject>& object, | 119 const base::android::JavaParamRef<jobject>& object, |
| 141 jint width, jint height, jfloat dpr); | 120 jint width, jint height, jfloat dpr); |
| 142 | 121 |
| 143 // Called from non-render thread to queue a callback onto the render thread. | 122 // 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. | 123 // The render thread checks for callbacks and processes them between frames. |
| 145 void QueueTaskOnUI(base::Callback<void()>& callback); | 124 void QueueTask(base::Callback<void()>& callback); |
| 146 | 125 |
| 147 // Perform a UI action triggered by the javascript API. | 126 // Perform a UI action triggered by the javascript API. |
| 148 void DoUiActionOnUI(const UiAction action); | 127 void DoUiAction(const UiAction action); |
| 149 | 128 |
| 150 void SetContentCssSizeOnUI(float width, float height, float dpr); | 129 void SetContentCssSize(float width, float height, float dpr); |
| 151 void SetUiCssSizeOnUI(float width, float height, float dpr); | 130 void SetUiCssSize(float width, float height, float dpr); |
| 131 |
| 132 void ContentFrameWasResized(bool width_changed); |
| 133 |
| 134 void ForceExitVR(); |
| 152 | 135 |
| 153 private: | 136 private: |
| 154 ~VrShell() override; | 137 ~VrShell() override; |
| 155 void LoadUIContentOnUI(); | 138 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 | 139 |
| 175 // content::WebContentsObserver implementation. All called on UI thread. | 140 // content::WebContentsObserver implementation. All called on UI thread. |
| 176 void RenderViewHostChanged(content::RenderViewHost* old_host, | 141 void RenderViewHostChanged(content::RenderViewHost* old_host, |
| 177 content::RenderViewHost* new_host) override; | 142 content::RenderViewHost* new_host) override; |
| 178 void MainFrameWasResized(bool width_changed) override; | 143 void MainFrameWasResized(bool width_changed) override; |
| 179 void WebContentsDestroyed() override; | 144 void WebContentsDestroyed() override; |
| 180 | 145 |
| 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_; | 146 std::unique_ptr<UiInterface> html_interface_; |
| 188 | 147 |
| 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_; | 148 std::unique_ptr<VrCompositor> content_compositor_; |
| 213 content::WebContents* main_contents_; | 149 content::WebContents* main_contents_; |
| 214 std::unique_ptr<VrCompositor> ui_compositor_; | 150 std::unique_ptr<VrCompositor> ui_compositor_; |
| 215 content::WebContents* ui_contents_; | 151 content::WebContents* ui_contents_; |
| 216 std::unique_ptr<VrWebContentsObserver> vr_web_contents_observer_; | 152 std::unique_ptr<VrWebContentsObserver> vr_web_contents_observer_; |
| 217 | 153 |
| 218 VrShellDelegate* delegate_ = nullptr; | 154 VrShellDelegate* delegate_ = nullptr; |
| 219 std::unique_ptr<VrShellRenderer> vr_shell_renderer_; | |
| 220 base::android::ScopedJavaGlobalRef<jobject> j_vr_shell_; | 155 base::android::ScopedJavaGlobalRef<jobject> j_vr_shell_; |
| 221 | 156 |
| 222 bool touch_pending_ = false; | 157 std::unique_ptr<VrInputManager> content_input_manager_; |
| 223 gvr::Quatf controller_quat_; | 158 std::unique_ptr<VrInputManager> ui_input_manager_; |
| 224 | 159 |
| 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_; | 160 scoped_refptr<VrMetricsHelper> metrics_helper_; |
| 248 | 161 |
| 249 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 162 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
| 163 std::unique_ptr<base::Thread> gl_thread_; |
| 250 | 164 |
| 251 base::WeakPtrFactory<VrShell> weak_ptr_factory_; | 165 base::WeakPtrFactory<VrShell> weak_ptr_factory_; |
| 252 | 166 |
| 253 DISALLOW_COPY_AND_ASSIGN(VrShell); | 167 DISALLOW_COPY_AND_ASSIGN(VrShell); |
| 254 }; | 168 }; |
| 255 | 169 |
| 256 bool RegisterVrShell(JNIEnv* env); | 170 bool RegisterVrShell(JNIEnv* env); |
| 257 | 171 |
| 258 } // namespace vr_shell | 172 } // namespace vr_shell |
| 259 | 173 |
| 260 #endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_H_ | 174 #endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_H_ |
| OLD | NEW |