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 "chrome/browser/android/vr_shell/vr_shell.h" | 5 #include "chrome/browser/android/vr_shell/vr_shell.h" |
6 | 6 |
7 #include <android/native_window_jni.h> | 7 #include <android/native_window_jni.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
13 #include "base/files/file_path.h" | |
14 #include "base/json/json_file_value_serializer.h" | |
13 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
14 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
17 #include "base/path_service.h" | |
15 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
16 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
17 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
19 #include "base/values.h" | 22 #include "base/values.h" |
20 #include "chrome/browser/android/tab_android.h" | 23 #include "chrome/browser/android/tab_android.h" |
21 #include "chrome/browser/android/vr_shell/android_ui_gesture_target.h" | 24 #include "chrome/browser/android/vr_shell/android_ui_gesture_target.h" |
25 #include "chrome/browser/android/vr_shell/gltf_parser.h" | |
26 #include "chrome/browser/android/vr_shell/ui_interface.h" | |
22 #include "chrome/browser/android/vr_shell/vr_compositor.h" | 27 #include "chrome/browser/android/vr_shell/vr_compositor.h" |
28 #include "chrome/browser/android/vr_shell/vr_controller_model.h" | |
23 #include "chrome/browser/android/vr_shell/vr_gl_thread.h" | 29 #include "chrome/browser/android/vr_shell/vr_gl_thread.h" |
24 #include "chrome/browser/android/vr_shell/vr_input_manager.h" | 30 #include "chrome/browser/android/vr_shell/vr_input_manager.h" |
25 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" | 31 #include "chrome/browser/android/vr_shell/vr_shell_delegate.h" |
26 #include "chrome/browser/android/vr_shell/vr_shell_gl.h" | 32 #include "chrome/browser/android/vr_shell/vr_shell_gl.h" |
27 #include "chrome/browser/android/vr_shell/vr_usage_monitor.h" | 33 #include "chrome/browser/android/vr_shell/vr_usage_monitor.h" |
28 #include "chrome/browser/android/vr_shell/vr_web_contents_observer.h" | 34 #include "chrome/browser/android/vr_shell/vr_web_contents_observer.h" |
35 #include "components/component_updater/component_updater_paths.h" | |
36 #include "content/public/browser/browser_thread.h" | |
29 #include "content/public/browser/navigation_controller.h" | 37 #include "content/public/browser/navigation_controller.h" |
30 #include "content/public/browser/render_view_host.h" | 38 #include "content/public/browser/render_view_host.h" |
31 #include "content/public/browser/render_widget_host.h" | 39 #include "content/public/browser/render_widget_host.h" |
32 #include "content/public/browser/render_widget_host_view.h" | 40 #include "content/public/browser/render_widget_host_view.h" |
33 #include "content/public/browser/web_contents.h" | 41 #include "content/public/browser/web_contents.h" |
34 #include "content/public/common/content_features.h" | 42 #include "content/public/common/content_features.h" |
35 #include "content/public/common/referrer.h" | 43 #include "content/public/common/referrer.h" |
36 #include "device/vr/android/gvr/gvr_device.h" | 44 #include "device/vr/android/gvr/gvr_device.h" |
37 #include "device/vr/android/gvr/gvr_device_provider.h" | 45 #include "device/vr/android/gvr/gvr_device_provider.h" |
38 #include "device/vr/android/gvr/gvr_gamepad_data_fetcher.h" | 46 #include "device/vr/android/gvr/gvr_gamepad_data_fetcher.h" |
39 #include "gpu/command_buffer/common/mailbox.h" | 47 #include "gpu/command_buffer/common/mailbox.h" |
40 #include "jni/VrShellImpl_jni.h" | 48 #include "jni/VrShellImpl_jni.h" |
41 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 49 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
42 #include "ui/android/view_android.h" | 50 #include "ui/android/view_android.h" |
43 #include "ui/android/window_android.h" | 51 #include "ui/android/window_android.h" |
44 #include "ui/base/page_transition_types.h" | 52 #include "ui/base/page_transition_types.h" |
45 #include "ui/display/display.h" | 53 #include "ui/display/display.h" |
46 #include "ui/display/screen.h" | 54 #include "ui/display/screen.h" |
55 #include "ui/gfx/codec/png_codec.h" | |
47 #include "ui/gfx/transform.h" | 56 #include "ui/gfx/transform.h" |
48 #include "ui/gfx/transform_util.h" | 57 #include "ui/gfx/transform_util.h" |
49 | 58 |
50 using base::android::JavaParamRef; | 59 using base::android::JavaParamRef; |
51 using base::android::JavaRef; | 60 using base::android::JavaRef; |
52 | 61 |
53 namespace vr_shell { | 62 namespace vr_shell { |
54 | 63 |
55 namespace { | 64 namespace { |
56 vr_shell::VrShell* g_instance; | 65 vr_shell::VrShell* g_instance; |
57 | 66 |
58 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; | 67 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; |
59 | 68 |
60 // Default downscale factor for computing the recommended WebVR | 69 // Default downscale factor for computing the recommended WebVR |
61 // renderWidth/Height from the 1:1 pixel mapped size. Using a rather | 70 // renderWidth/Height from the 1:1 pixel mapped size. Using a rather |
62 // aggressive downscale due to the high overhead of copying pixels | 71 // aggressive downscale due to the high overhead of copying pixels |
63 // twice before handing off to GVR. For comparison, the polyfill | 72 // twice before handing off to GVR. For comparison, the polyfill |
64 // uses approximately 0.55 on a Pixel XL. | 73 // uses approximately 0.55 on a Pixel XL. |
65 static constexpr float kWebVrRecommendedResolutionScale = 0.5; | 74 static constexpr float kWebVrRecommendedResolutionScale = 0.5; |
66 | 75 |
67 void SetIsInVR(content::WebContents* contents, bool is_in_vr) { | 76 void SetIsInVR(content::WebContents* contents, bool is_in_vr) { |
68 if (contents && contents->GetRenderWidgetHostView()) | 77 if (contents && contents->GetRenderWidgetHostView()) |
69 contents->GetRenderWidgetHostView()->SetIsInVR(is_in_vr); | 78 contents->GetRenderWidgetHostView()->SetIsInVR(is_in_vr); |
70 } | 79 } |
71 | 80 |
81 void LoadControllerModelTask( | |
asimjour1
2017/03/29 20:40:44
It should be moved to VrController
acondor_
2017/03/29 22:46:44
Done.
| |
82 base::WeakPtr<VrShell> weak_vr_shell, | |
83 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) { | |
84 base::FilePath models_path; | |
85 PathService::Get(component_updater::DIR_COMPONENT_USER, &models_path); | |
86 models_path = models_path.Append(VrControllerModel::kComponentName) | |
87 .Append(VrControllerModel::kDefaultVersion) | |
88 .Append(VrControllerModel::kModelsDirectory); | |
89 auto model_path = models_path.Append(VrControllerModel::kModelFilename); | |
90 | |
91 // No further action if model file is not present | |
92 if (!base::PathExists(model_path)) { | |
93 LOG(WARNING) << "Controller model files not found"; | |
94 return; | |
95 } | |
96 | |
97 GltfParser gltf_parser; | |
98 std::vector<std::unique_ptr<gltf::Buffer>> buffers; | |
99 auto asset = gltf_parser.Parse(model_path, &buffers); | |
100 if (!asset) { | |
101 LOG(WARNING) << "Failed to read controller model"; | |
102 return; | |
103 } | |
104 | |
105 auto controller_model = | |
106 base::MakeUnique<VrControllerModel>(std::move(asset), std::move(buffers)); | |
107 | |
108 auto textures_path = | |
109 models_path.Append(VrControllerModel::kTexturesDirectory); | |
110 | |
111 for (int i = 0; i < VrControllerModel::STATE_COUNT; i++) { | |
112 auto texture_path = | |
113 textures_path.Append(VrControllerModel::kTextureFilenames[i]); | |
114 std::string data; | |
115 auto bitmap = base::MakeUnique<SkBitmap>(); | |
116 if (!base::ReadFileToString(texture_path, &data) || | |
117 !gfx::PNGCodec::Decode( | |
118 reinterpret_cast<const unsigned char*>(data.data()), data.size(), | |
119 bitmap.get()) || | |
120 bitmap->colorType() != kRGBA_8888_SkColorType) { | |
121 LOG(ERROR) << "Failed to read controller texture"; | |
122 return; | |
123 } | |
124 controller_model->SetTexture(i, std::move(bitmap)); | |
125 } | |
126 | |
127 main_thread_task_runner->PostTask( | |
128 FROM_HERE, base::Bind(&VrShell::SubmitControllerModel, weak_vr_shell, | |
129 base::Passed(&controller_model))); | |
130 } | |
131 | |
72 } // namespace | 132 } // namespace |
73 | 133 |
74 VrShell::VrShell(JNIEnv* env, | 134 VrShell::VrShell(JNIEnv* env, |
75 jobject obj, | 135 jobject obj, |
76 ui::WindowAndroid* content_window, | 136 ui::WindowAndroid* content_window, |
77 content::WebContents* ui_contents, | 137 content::WebContents* ui_contents, |
78 ui::WindowAndroid* ui_window, | 138 ui::WindowAndroid* ui_window, |
79 bool for_web_vr, | 139 bool for_web_vr, |
80 VrShellDelegate* delegate, | 140 VrShellDelegate* delegate, |
81 gvr_context* gvr_api, | 141 gvr_context* gvr_api, |
(...skipping 21 matching lines...) Expand all Loading... | |
103 gl_thread_ = base::MakeUnique<VrGLThread>(weak_ptr_factory_.GetWeakPtr(), | 163 gl_thread_ = base::MakeUnique<VrGLThread>(weak_ptr_factory_.GetWeakPtr(), |
104 main_thread_task_runner_, gvr_api, | 164 main_thread_task_runner_, gvr_api, |
105 for_web_vr, reprojected_rendering_); | 165 for_web_vr, reprojected_rendering_); |
106 | 166 |
107 base::Thread::Options options(base::MessageLoop::TYPE_DEFAULT, 0); | 167 base::Thread::Options options(base::MessageLoop::TYPE_DEFAULT, 0); |
108 options.priority = base::ThreadPriority::DISPLAY; | 168 options.priority = base::ThreadPriority::DISPLAY; |
109 gl_thread_->StartWithOptions(options); | 169 gl_thread_->StartWithOptions(options); |
110 | 170 |
111 html_interface_ = base::MakeUnique<UiInterface>( | 171 html_interface_ = base::MakeUnique<UiInterface>( |
112 for_web_vr ? UiInterface::Mode::WEB_VR : UiInterface::Mode::STANDARD); | 172 for_web_vr ? UiInterface::Mode::WEB_VR : UiInterface::Mode::STANDARD); |
173 | |
174 content::BrowserThread::PostTask( | |
175 content::BrowserThread::FILE, FROM_HERE, | |
176 base::Bind(LoadControllerModelTask, weak_ptr_factory_.GetWeakPtr(), | |
177 main_thread_task_runner_)); | |
113 } | 178 } |
114 | 179 |
115 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 180 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
116 delete this; | 181 delete this; |
117 } | 182 } |
118 | 183 |
119 void VrShell::SwapContents( | 184 void VrShell::SwapContents( |
120 JNIEnv* env, | 185 JNIEnv* env, |
121 const JavaParamRef<jobject>& obj, | 186 const JavaParamRef<jobject>& obj, |
122 const JavaParamRef<jobject>& web_contents, | 187 const JavaParamRef<jobject>& web_contents, |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
342 | 407 |
343 void VrShell::SubmitWebVRFrame(int16_t frame_index, | 408 void VrShell::SubmitWebVRFrame(int16_t frame_index, |
344 const gpu::MailboxHolder& mailbox) { | 409 const gpu::MailboxHolder& mailbox) { |
345 TRACE_EVENT1("gpu", "SubmitWebVRFrame", "frame", frame_index); | 410 TRACE_EVENT1("gpu", "SubmitWebVRFrame", "frame", frame_index); |
346 | 411 |
347 PostToGlThreadWhenReady(base::Bind(&VrShellGl::SubmitWebVRFrame, | 412 PostToGlThreadWhenReady(base::Bind(&VrShellGl::SubmitWebVRFrame, |
348 gl_thread_->GetVrShellGl(), frame_index, | 413 gl_thread_->GetVrShellGl(), frame_index, |
349 mailbox)); | 414 mailbox)); |
350 } | 415 } |
351 | 416 |
417 void VrShell::SubmitControllerModel(std::unique_ptr<VrControllerModel> model) { | |
418 PostToGlThreadWhenReady(base::Bind(&VrShellGl::SetControllerModel, | |
419 gl_thread_->GetVrShellGl(), | |
420 base::Passed(&model))); | |
421 } | |
422 | |
352 void VrShell::UpdateWebVRTextureBounds(int16_t frame_index, | 423 void VrShell::UpdateWebVRTextureBounds(int16_t frame_index, |
353 const gvr::Rectf& left_bounds, | 424 const gvr::Rectf& left_bounds, |
354 const gvr::Rectf& right_bounds, | 425 const gvr::Rectf& right_bounds, |
355 const gvr::Sizei& source_size) { | 426 const gvr::Sizei& source_size) { |
356 PostToGlThreadWhenReady(base::Bind(&VrShellGl::UpdateWebVRTextureBounds, | 427 PostToGlThreadWhenReady(base::Bind(&VrShellGl::UpdateWebVRTextureBounds, |
357 gl_thread_->GetVrShellGl(), frame_index, | 428 gl_thread_->GetVrShellGl(), frame_index, |
358 left_bounds, right_bounds, source_size)); | 429 left_bounds, right_bounds, source_size)); |
359 } | 430 } |
360 | 431 |
361 bool VrShell::SupportsPresentation() { | 432 bool VrShell::SupportsPresentation() { |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
779 jboolean reprojected_rendering) { | 850 jboolean reprojected_rendering) { |
780 return reinterpret_cast<intptr_t>(new VrShell( | 851 return reinterpret_cast<intptr_t>(new VrShell( |
781 env, obj, reinterpret_cast<ui::WindowAndroid*>(content_window_android), | 852 env, obj, reinterpret_cast<ui::WindowAndroid*>(content_window_android), |
782 content::WebContents::FromJavaWebContents(ui_web_contents), | 853 content::WebContents::FromJavaWebContents(ui_web_contents), |
783 reinterpret_cast<ui::WindowAndroid*>(ui_window_android), for_web_vr, | 854 reinterpret_cast<ui::WindowAndroid*>(ui_window_android), for_web_vr, |
784 VrShellDelegate::GetNativeVrShellDelegate(env, delegate), | 855 VrShellDelegate::GetNativeVrShellDelegate(env, delegate), |
785 reinterpret_cast<gvr_context*>(gvr_api), reprojected_rendering)); | 856 reinterpret_cast<gvr_context*>(gvr_api), reprojected_rendering)); |
786 } | 857 } |
787 | 858 |
788 } // namespace vr_shell | 859 } // namespace vr_shell |
OLD | NEW |