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

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

Issue 2775283004: Rendering Daydream controller in a fixed position. (Closed)
Patch Set: Using textures for corresponding button states. Created 3 years, 8 months 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 #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(
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)
cjgrant 2017/03/29 13:20:54 Formatting looks off here.
acondor_ 2017/03/29 20:13:26 This is what cl format does, even if I try to chan
cjgrant 2017/03/29 20:22:37 Acknowledged.
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))
cjgrant 2017/03/29 13:20:54 Is this worth a warning-level print?
acondor_ 2017/03/29 20:13:26 Done.
93 return;
94
95 GltfParser gltf_parser;
96 auto asset = gltf_parser.Parse(model_path);
97 if (!asset) {
98 LOG(ERROR) << "LoadControllerModelTask: Failed to parse model.";
99 return;
100 }
101
102 auto controller_model = base::MakeUnique<VrControllerModel>(std::move(asset));
103
104 auto textures_path =
105 models_path.Append(VrControllerModel::kTexturesDirectory);
106
107 for (int i = 0; i < VrControllerModel::STATE_COUNT; i++) {
108 auto texture_path =
109 textures_path.Append(VrControllerModel::kTextureFilenames[i]);
110 std::string data;
111 auto bitmap = base::MakeUnique<SkBitmap>();
112 if (!base::ReadFileToString(texture_path, &data) ||
113 !gfx::PNGCodec::Decode(
114 reinterpret_cast<const unsigned char*>(data.data()), data.size(),
115 bitmap.get()) ||
116 bitmap->colorType() != kRGBA_8888_SkColorType) {
117 LOG(ERROR) << "LoadControllerModelTask: Failed to read texture";
cjgrant 2017/03/29 13:20:54 Rather than including the function name, how about
acondor_ 2017/03/29 20:13:26 Done.
118 return;
119 }
120 controller_model->SetTexture(i, std::move(bitmap));
121 }
122
123 main_thread_task_runner->PostTask(
124 FROM_HERE, base::Bind(&VrShell::SubmitControllerModel, weak_vr_shell,
125 base::Passed(&controller_model)));
126 }
127
72 } // namespace 128 } // namespace
73 129
74 VrShell::VrShell(JNIEnv* env, 130 VrShell::VrShell(JNIEnv* env,
75 jobject obj, 131 jobject obj,
76 ui::WindowAndroid* content_window, 132 ui::WindowAndroid* content_window,
77 content::WebContents* ui_contents, 133 content::WebContents* ui_contents,
78 ui::WindowAndroid* ui_window, 134 ui::WindowAndroid* ui_window,
79 bool for_web_vr, 135 bool for_web_vr,
80 VrShellDelegate* delegate, 136 VrShellDelegate* delegate,
81 gvr_context* gvr_api, 137 gvr_context* gvr_api,
(...skipping 21 matching lines...) Expand all
103 gl_thread_ = base::MakeUnique<VrGLThread>(weak_ptr_factory_.GetWeakPtr(), 159 gl_thread_ = base::MakeUnique<VrGLThread>(weak_ptr_factory_.GetWeakPtr(),
104 main_thread_task_runner_, gvr_api, 160 main_thread_task_runner_, gvr_api,
105 for_web_vr, reprojected_rendering_); 161 for_web_vr, reprojected_rendering_);
106 162
107 base::Thread::Options options(base::MessageLoop::TYPE_DEFAULT, 0); 163 base::Thread::Options options(base::MessageLoop::TYPE_DEFAULT, 0);
108 options.priority = base::ThreadPriority::DISPLAY; 164 options.priority = base::ThreadPriority::DISPLAY;
109 gl_thread_->StartWithOptions(options); 165 gl_thread_->StartWithOptions(options);
110 166
111 html_interface_ = base::MakeUnique<UiInterface>( 167 html_interface_ = base::MakeUnique<UiInterface>(
112 for_web_vr ? UiInterface::Mode::WEB_VR : UiInterface::Mode::STANDARD); 168 for_web_vr ? UiInterface::Mode::WEB_VR : UiInterface::Mode::STANDARD);
169
170 content::BrowserThread::PostTask(
171 content::BrowserThread::FILE, FROM_HERE,
172 base::Bind(LoadControllerModelTask, weak_ptr_factory_.GetWeakPtr(),
173 main_thread_task_runner_));
113 } 174 }
114 175
115 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { 176 void VrShell::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) {
116 delete this; 177 delete this;
117 } 178 }
118 179
119 void VrShell::SwapContents( 180 void VrShell::SwapContents(
120 JNIEnv* env, 181 JNIEnv* env,
121 const JavaParamRef<jobject>& obj, 182 const JavaParamRef<jobject>& obj,
122 const JavaParamRef<jobject>& web_contents, 183 const JavaParamRef<jobject>& web_contents,
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 403
343 void VrShell::SubmitWebVRFrame(int16_t frame_index, 404 void VrShell::SubmitWebVRFrame(int16_t frame_index,
344 const gpu::MailboxHolder& mailbox) { 405 const gpu::MailboxHolder& mailbox) {
345 TRACE_EVENT1("gpu", "SubmitWebVRFrame", "frame", frame_index); 406 TRACE_EVENT1("gpu", "SubmitWebVRFrame", "frame", frame_index);
346 407
347 PostToGlThreadWhenReady(base::Bind(&VrShellGl::SubmitWebVRFrame, 408 PostToGlThreadWhenReady(base::Bind(&VrShellGl::SubmitWebVRFrame,
348 gl_thread_->GetVrShellGl(), frame_index, 409 gl_thread_->GetVrShellGl(), frame_index,
349 mailbox)); 410 mailbox));
350 } 411 }
351 412
413 void VrShell::SubmitControllerModel(std::unique_ptr<VrControllerModel> model) {
414 PostToGlThreadWhenReady(base::Bind(&VrShellGl::SetControllerModel,
415 gl_thread_->GetVrShellGl(),
416 base::Passed(&model)));
417 }
418
352 void VrShell::UpdateWebVRTextureBounds(int16_t frame_index, 419 void VrShell::UpdateWebVRTextureBounds(int16_t frame_index,
353 const gvr::Rectf& left_bounds, 420 const gvr::Rectf& left_bounds,
354 const gvr::Rectf& right_bounds, 421 const gvr::Rectf& right_bounds,
355 const gvr::Sizei& source_size) { 422 const gvr::Sizei& source_size) {
356 PostToGlThreadWhenReady(base::Bind(&VrShellGl::UpdateWebVRTextureBounds, 423 PostToGlThreadWhenReady(base::Bind(&VrShellGl::UpdateWebVRTextureBounds,
357 gl_thread_->GetVrShellGl(), frame_index, 424 gl_thread_->GetVrShellGl(), frame_index,
358 left_bounds, right_bounds, source_size)); 425 left_bounds, right_bounds, source_size));
359 } 426 }
360 427
361 bool VrShell::SupportsPresentation() { 428 bool VrShell::SupportsPresentation() {
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 jboolean reprojected_rendering) { 846 jboolean reprojected_rendering) {
780 return reinterpret_cast<intptr_t>(new VrShell( 847 return reinterpret_cast<intptr_t>(new VrShell(
781 env, obj, reinterpret_cast<ui::WindowAndroid*>(content_window_android), 848 env, obj, reinterpret_cast<ui::WindowAndroid*>(content_window_android),
782 content::WebContents::FromJavaWebContents(ui_web_contents), 849 content::WebContents::FromJavaWebContents(ui_web_contents),
783 reinterpret_cast<ui::WindowAndroid*>(ui_window_android), for_web_vr, 850 reinterpret_cast<ui::WindowAndroid*>(ui_window_android), for_web_vr,
784 VrShellDelegate::GetNativeVrShellDelegate(env, delegate), 851 VrShellDelegate::GetNativeVrShellDelegate(env, delegate),
785 reinterpret_cast<gvr_context*>(gvr_api), reprojected_rendering)); 852 reinterpret_cast<gvr_context*>(gvr_api), reprojected_rendering));
786 } 853 }
787 854
788 } // namespace vr_shell 855 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698