Chromium Code Reviews| 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 |
| 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 { |
| 76 JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); | 67 NONE = 0, |
| 77 void DestroyOnUI(JNIEnv* env, | 68 CONTENT, |
| 69 UI | |
| 70 }; | |
|
cjgrant
2016/12/08 17:02:49
Blank line?
mthiesse
2016/12/09 01:28:42
Done.
| |
| 71 void LoadUIContent(JNIEnv* env, | |
| 72 const base::android::JavaParamRef<jobject>& obj); | |
| 73 void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); | |
| 74 void OnTriggerEvent(JNIEnv* env, | |
| 75 const base::android::JavaParamRef<jobject>& obj); | |
| 76 void OnPause(JNIEnv* env, | |
| 78 const base::android::JavaParamRef<jobject>& obj); | 77 const base::android::JavaParamRef<jobject>& obj); |
| 79 void SetDelegateOnUI(JNIEnv* env, | 78 void OnResume(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); | |
| 91 void OnTriggerEventOnUI(JNIEnv* env, | |
| 92 const base::android::JavaParamRef<jobject>& obj); | |
| 93 void OnPauseOnUI(JNIEnv* env, | |
| 94 const base::android::JavaParamRef<jobject>& obj); | |
| 95 void OnResumeOnUI(JNIEnv* env, | |
| 96 const base::android::JavaParamRef<jobject>& obj); | 79 const base::android::JavaParamRef<jobject>& obj); |
| 97 void SetWebVrModeOnUI(JNIEnv* env, | 80 void SetWebVrMode(JNIEnv* env, |
| 98 const base::android::JavaParamRef<jobject>& obj, | 81 const base::android::JavaParamRef<jobject>& obj, |
| 99 bool enabled); | 82 bool enabled); |
| 100 | 83 |
| 101 void ContentWebContentsDestroyedOnUI(); | 84 void ContentWebContentsDestroyed(); |
| 102 // Called when our WebContents have been hidden. Usually a sign that something | 85 // Called when our WebContents have been hidden. Usually a sign that something |
| 103 // like another tab placed in front of it. | 86 // like another tab placed in front of it. |
| 104 void ContentWasHiddenOnUI(); | 87 void ContentWasHidden(); |
| 105 | 88 |
| 106 // html/js UI hooks. | 89 // html/js UI hooks. |
| 107 static base::WeakPtr<VrShell> GetWeakPtrOnUI( | 90 static base::WeakPtr<VrShell> GetWeakPtr( |
| 108 const content::WebContents* web_contents); | 91 const content::WebContents* web_contents); |
| 109 // TODO(mthiesse): Clean up threading around Scene. | 92 |
| 110 UiScene* GetSceneOnGL(); | 93 // Returns a pointer to the scene owned by the GL thread. Do not dereference |
| 94 // this pointer off of the GL thread. | |
| 95 UiScene* GetScene(); | |
| 111 // TODO(mthiesse): Clean up threading around UiInterface. | 96 // TODO(mthiesse): Clean up threading around UiInterface. |
| 112 UiInterface* GetUiInterfaceOnGL(); | 97 UiInterface* GetUiInterface(); |
| 113 void OnDomContentsLoadedOnUI(); | 98 void OnDomContentsLoaded(); |
| 114 | 99 |
| 115 // device::GvrDelegate implementation | 100 // device::GvrDelegate implementation |
| 116 // TODO(mthiesse): Clean up threading around GVR API. These functions are | 101 // 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. | 102 // called on the UI thread, but use GL thread objects in a non-threadsafe way. |
| 118 void SetWebVRSecureOrigin(bool secure_origin) override; | 103 void SetWebVRSecureOrigin(bool secure_origin) override; |
| 119 void SubmitWebVRFrame() override; | 104 void SubmitWebVRFrame() override; |
| 120 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, | 105 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, |
| 121 const gvr::Rectf& right_bounds) override; | 106 const gvr::Rectf& right_bounds) override; |
| 122 gvr::GvrApi* gvr_api() override; | 107 gvr::GvrApi* gvr_api() override; |
| 123 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num) override; | 108 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num) override; |
| 124 void SetWebVRRenderSurfaceSize(int width, int height) override; | 109 void SetWebVRRenderSurfaceSize(int width, int height) override; |
| 125 gvr::Sizei GetWebVRCompositorSurfaceSize() override; | 110 gvr::Sizei GetWebVRCompositorSurfaceSize() override; |
| 126 | 111 |
| 127 void SurfacesChangedOnUI( | 112 void SurfacesChanged(jobject content_surface, jobject ui_surface); |
| 128 JNIEnv* env, | 113 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 | 114 |
| 133 void ContentBoundsChangedOnUI( | 115 void ContentBoundsChanged( |
| 134 JNIEnv* env, | 116 JNIEnv* env, |
| 135 const base::android::JavaParamRef<jobject>& object, | 117 const base::android::JavaParamRef<jobject>& object, |
| 136 jint width, jint height, jfloat dpr); | 118 jint width, jint height, jfloat dpr); |
| 137 | 119 |
| 138 void UIBoundsChangedOnUI( | 120 void UIBoundsChanged( |
| 139 JNIEnv* env, | 121 JNIEnv* env, |
| 140 const base::android::JavaParamRef<jobject>& object, | 122 const base::android::JavaParamRef<jobject>& object, |
| 141 jint width, jint height, jfloat dpr); | 123 jint width, jint height, jfloat dpr); |
| 142 | 124 |
| 143 // Called from non-render thread to queue a callback onto the render thread. | 125 // 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. | 126 // The render thread checks for callbacks and processes them between frames. |
| 145 void QueueTaskOnUI(base::Callback<void()>& callback); | 127 void QueueTask(base::Callback<void()>& callback); |
| 146 | 128 |
| 147 // Perform a UI action triggered by the javascript API. | 129 // Perform a UI action triggered by the javascript API. |
| 148 void DoUiActionOnUI(const UiAction action); | 130 void DoUiAction(const UiAction action); |
| 149 | 131 |
| 150 void SetContentCssSizeOnUI(float width, float height, float dpr); | 132 void SetContentCssSize(float width, float height, float dpr); |
| 151 void SetUiCssSizeOnUI(float width, float height, float dpr); | 133 void SetUiCssSize(float width, float height, float dpr); |
| 134 | |
| 135 void ContentFrameWasResized(bool width_changed); | |
| 152 | 136 |
| 153 private: | 137 private: |
| 154 ~VrShell() override; | 138 ~VrShell() override; |
| 155 void LoadUIContentOnUI(); | |
| 156 void DrawVrShellOnGL(const gvr::Mat4f& head_pose, gvr::Frame &frame); | |
| 157 void DrawUiViewOnGL(const gvr::Mat4f* head_pose, | |
| 158 const std::vector<const ContentRectangle*>& elements, | |
| 159 const gvr::Sizei& render_size, int viewport_offset); | |
| 160 void DrawElementsOnGL(const gvr::Mat4f& render_matrix, | |
| 161 const std::vector<const ContentRectangle*>& elements); | |
| 162 void DrawCursorOnGL(const gvr::Mat4f& render_matrix); | |
| 163 void DrawWebVrOnGL(); | |
| 164 bool WebVrPoseByteIsValidOnGL(int pose_index_byte); | |
| 165 | |
| 166 void UpdateControllerOnGL(const gvr::Vec3f& forward_vector); | |
| 167 void SendEventsToTargetOnGL(InputTarget input_target, int pixel_x, | |
| 168 int pixel_y); | |
| 169 void SendGestureOnGL(InputTarget input_target, | |
| 170 std::unique_ptr<blink::WebInputEvent> event); | |
| 171 | |
| 172 void HandleQueuedTasksOnGL(); | |
| 173 | 139 |
| 174 // content::WebContentsObserver implementation. All called on UI thread. | 140 // content::WebContentsObserver implementation. All called on UI thread. |
| 175 void RenderViewHostChanged(content::RenderViewHost* old_host, | 141 void RenderViewHostChanged(content::RenderViewHost* old_host, |
| 176 content::RenderViewHost* new_host) override; | 142 content::RenderViewHost* new_host) override; |
| 177 void MainFrameWasResized(bool width_changed) override; | 143 void MainFrameWasResized(bool width_changed) override; |
| 178 void WebContentsDestroyed() override; | 144 void WebContentsDestroyed() override; |
| 179 | 145 |
| 180 // samplerExternalOES texture data for UI content image. | |
| 181 jint ui_texture_id_ = 0; | |
| 182 // samplerExternalOES texture data for main content image. | |
| 183 jint content_texture_id_ = 0; | |
| 184 | |
| 185 std::unique_ptr<UiScene> scene_; | |
| 186 std::unique_ptr<UiInterface> html_interface_; | 146 std::unique_ptr<UiInterface> html_interface_; |
| 187 | 147 |
| 188 std::unique_ptr<gvr::GvrApi> gvr_api_; | |
| 189 std::unique_ptr<gvr::BufferViewportList> buffer_viewport_list_; | |
| 190 std::unique_ptr<gvr::BufferViewport> buffer_viewport_; | |
| 191 std::unique_ptr<gvr::BufferViewport> headlocked_left_viewport_; | |
| 192 std::unique_ptr<gvr::BufferViewport> headlocked_right_viewport_; | |
| 193 std::unique_ptr<gvr::BufferViewport> webvr_left_viewport_; | |
| 194 std::unique_ptr<gvr::BufferViewport> webvr_right_viewport_; | |
| 195 std::unique_ptr<gvr::SwapChain> swap_chain_; | |
| 196 | |
| 197 // Current sizes for the render buffers. | |
| 198 gvr::Sizei render_size_primary_; | |
| 199 gvr::Sizei render_size_headlocked_; | |
| 200 | |
| 201 // Intended size for the primary render buffer by UI mode. | |
| 202 // For WebVR, a size of 0x0 is used to indicate "not yet ready" | |
| 203 // to suppress rendering while still initializing. | |
| 204 gvr::Sizei render_size_primary_webvr_ = device::kInvalidRenderTargetSize; | |
| 205 gvr::Sizei render_size_primary_vrshell_; | |
| 206 | |
| 207 std::queue<base::Callback<void()>> task_queue_; | |
| 208 base::Lock task_queue_lock_; | |
| 209 base::Lock gvr_init_lock_; | |
| 210 | |
| 211 std::unique_ptr<VrCompositor> content_compositor_; | 148 std::unique_ptr<VrCompositor> content_compositor_; |
| 212 content::WebContents* main_contents_; | 149 content::WebContents* main_contents_; |
| 213 std::unique_ptr<VrCompositor> ui_compositor_; | 150 std::unique_ptr<VrCompositor> ui_compositor_; |
| 214 content::WebContents* ui_contents_; | 151 content::WebContents* ui_contents_; |
| 215 std::unique_ptr<VrWebContentsObserver> vr_web_contents_observer_; | 152 std::unique_ptr<VrWebContentsObserver> vr_web_contents_observer_; |
| 216 | 153 |
| 217 VrShellDelegate* delegate_ = nullptr; | 154 VrShellDelegate* delegate_ = nullptr; |
| 218 std::unique_ptr<VrShellRenderer> vr_shell_renderer_; | |
| 219 base::android::ScopedJavaGlobalRef<jobject> j_vr_shell_; | 155 base::android::ScopedJavaGlobalRef<jobject> j_vr_shell_; |
| 220 | 156 |
| 221 bool touch_pending_ = false; | 157 std::unique_ptr<VrInputManager> content_input_manager_; |
| 222 gvr::Quatf controller_quat_; | 158 std::unique_ptr<VrInputManager> ui_input_manager_; |
| 223 | 159 |
| 224 gvr::Vec3f target_point_; | |
| 225 const ContentRectangle* target_element_ = nullptr; | |
| 226 InputTarget current_input_target_ = NONE; | |
| 227 int ui_tex_css_width_ = 0; | |
| 228 int ui_tex_css_height_ = 0; | |
| 229 int content_tex_css_width_ = 0; | |
| 230 int content_tex_css_height_ = 0; | |
| 231 gvr::Sizei content_tex_physical_size_ = {0, 0}; | |
| 232 | |
| 233 // The pose ring buffer size must be a power of two to avoid glitches when | |
| 234 // the pose index wraps around. It should be large enough to handle the | |
| 235 // current backlog of poses which is 2-3 frames. | |
| 236 static constexpr int kPoseRingBufferSize = 8; | |
| 237 std::vector<gvr::Mat4f> webvr_head_pose_; | |
| 238 std::vector<bool> webvr_head_pose_valid_; | |
| 239 jint webvr_texture_id_ = 0; | |
| 240 | |
| 241 std::unique_ptr<VrController> controller_; | |
| 242 std::unique_ptr<VrInputManager> content_input_manager_; | |
| 243 base::WeakPtr<VrInputManager> weak_content_input_manager_; | |
| 244 std::unique_ptr<VrInputManager> ui_input_manager_; | |
| 245 base::WeakPtr<VrInputManager> weak_ui_input_manager_; | |
| 246 scoped_refptr<VrMetricsHelper> metrics_helper_; | 160 scoped_refptr<VrMetricsHelper> metrics_helper_; |
| 247 | 161 |
| 248 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_; | |
| 249 | 164 |
| 250 base::WeakPtrFactory<VrShell> weak_ptr_factory_; | 165 base::WeakPtrFactory<VrShell> weak_ptr_factory_; |
| 251 | 166 |
| 252 DISALLOW_COPY_AND_ASSIGN(VrShell); | 167 DISALLOW_COPY_AND_ASSIGN(VrShell); |
| 253 }; | 168 }; |
| 254 | 169 |
| 255 bool RegisterVrShell(JNIEnv* env); | 170 bool RegisterVrShell(JNIEnv* env); |
| 256 | 171 |
| 257 } // namespace vr_shell | 172 } // namespace vr_shell |
| 258 | 173 |
| 259 #endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_H_ | 174 #endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_H_ |
| OLD | NEW |