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 <thread> | 7 #include <thread> |
8 | 8 |
9 #include "chrome/browser/android/vr_shell/ui_scene.h" | 9 #include "chrome/browser/android/vr_shell/ui_scene.h" |
10 #include "chrome/browser/android/vr_shell/vr_compositor.h" | 10 #include "chrome/browser/android/vr_shell/vr_compositor.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 // TODO(mthiesse): Handedness options. | 60 // TODO(mthiesse): Handedness options. |
61 static constexpr gvr::Vec3f kHandPosition = {0.2f, -0.5f, -0.2f}; | 61 static constexpr gvr::Vec3f kHandPosition = {0.2f, -0.5f, -0.2f}; |
62 | 62 |
63 // Fraction of the z-distance to the object the cursor is drawn at to avoid | 63 // Fraction of the z-distance to the object the cursor is drawn at to avoid |
64 // rounding errors drawing the cursor behind the object. | 64 // rounding errors drawing the cursor behind the object. |
65 static constexpr float kReticleZOffset = 0.99f; | 65 static constexpr float kReticleZOffset = 0.99f; |
66 | 66 |
67 // UI element 0 is the browser content rectangle. | 67 // UI element 0 is the browser content rectangle. |
68 static constexpr int kBrowserUiElementId = 0; | 68 static constexpr int kBrowserUiElementId = 0; |
69 | 69 |
70 // Positions and sizes of statically placed UI elements in the UI texture. | |
71 // TODO(klausw): replace the hardcoded positions with JS position/offset | |
72 // retrieval once the infrastructure for that is hooked up. | |
73 static constexpr vr_shell::Recti kWebVrWarningTransientRect = {0, 0, 256, 128}; | |
74 static constexpr vr_shell::Recti kWebVrWarningPermanentRect = {256, 0, 128, 64}; | |
75 | |
70 vr_shell::VrShell* g_instance; | 76 vr_shell::VrShell* g_instance; |
71 | 77 |
72 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; | 78 static const char kVrShellUIURL[] = "chrome://vr-shell-ui"; |
73 | 79 |
74 } // namespace | 80 } // namespace |
75 | 81 |
76 namespace vr_shell { | 82 namespace vr_shell { |
77 | 83 |
78 VrShell::VrShell(JNIEnv* env, | 84 VrShell::VrShell(JNIEnv* env, |
79 jobject obj, | 85 jobject obj, |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); | 249 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); |
244 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; | 250 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; |
245 head_pose_ = gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time); | 251 head_pose_ = gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time); |
246 head_pose_ = gvr_api_->ApplyNeckModel(head_pose_, 1.0f); | 252 head_pose_ = gvr_api_->ApplyNeckModel(head_pose_, 1.0f); |
247 | 253 |
248 // Bind back to the default framebuffer. | 254 // Bind back to the default framebuffer. |
249 frame.BindBuffer(0); | 255 frame.BindBuffer(0); |
250 | 256 |
251 if (webvr_mode_) { | 257 if (webvr_mode_) { |
252 DrawWebVr(); | 258 DrawWebVr(); |
259 if (!webvr_secure_origin_) { | |
260 DrawWebVrOverlay(target_time.monotonic_system_time_nanos); | |
261 } | |
253 } else { | 262 } else { |
254 DrawVrShell(target_time.monotonic_system_time_nanos); | 263 DrawVrShell(target_time.monotonic_system_time_nanos); |
255 } | 264 } |
256 | 265 |
257 frame.Unbind(); | 266 frame.Unbind(); |
258 frame.Submit(*buffer_viewport_list_, head_pose_); | 267 frame.Submit(*buffer_viewport_list_, head_pose_); |
259 } | 268 } |
260 | 269 |
261 void VrShell::DrawVrShell(int64_t time) { | 270 void VrShell::DrawVrShell(int64_t time) { |
262 float screen_tilt = desktop_screen_tilt_ * M_PI / 180.0f; | 271 float screen_tilt = desktop_screen_tilt_ * M_PI / 180.0f; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 projection_matrix_ = | 318 projection_matrix_ = |
310 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); | 319 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); |
311 | 320 |
312 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 321 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
313 | 322 |
314 // TODO(mthiesse): Draw order for transparency. | 323 // TODO(mthiesse): Draw order for transparency. |
315 DrawUI(); | 324 DrawUI(); |
316 DrawCursor(); | 325 DrawCursor(); |
317 } | 326 } |
318 | 327 |
328 bool VrShell::IsUiTextureReady() { | |
329 return ui_tex_width_ > 0 && ui_tex_height_ > 0; | |
330 } | |
331 | |
332 /** | |
333 * Converts a pixel rectangle to (0..1) float texture coordinates. | |
334 * | |
335 * Callers need to ensure that the texture width/height is initialized | |
336 * by checking IsUiTextureReady() first. | |
337 */ | |
338 Rectf VrShell::MakeUiPixelCopyRect(Recti pixel_rect) { | |
339 DCHECK(IsUiTextureReady()); | |
340 Rectf copy_rect = {(float)pixel_rect.x / ui_tex_width_, | |
cjgrant
2016/09/28 17:48:04
nits:
- Should use C++ casts as per style guide.
-
| |
341 (float)pixel_rect.y / ui_tex_height_, | |
342 (float)pixel_rect.width / ui_tex_width_, | |
343 (float)pixel_rect.height / ui_tex_height_}; | |
344 return copy_rect; | |
345 } | |
346 | |
319 void VrShell::DrawUI() { | 347 void VrShell::DrawUI() { |
320 for (const auto& rect : scene_.GetUiElements()) { | 348 for (const auto& rect : scene_.GetUiElements()) { |
321 if (!rect->visible) { | 349 if (!rect->visible) { |
322 continue; | 350 continue; |
323 } | 351 } |
324 | 352 |
325 gvr::Mat4f combined_matrix = MatrixMul(view_matrix_, | 353 gvr::Mat4f combined_matrix = MatrixMul(view_matrix_, |
326 rect->transform.to_world); | 354 rect->transform.to_world); |
327 combined_matrix = MatrixMul(projection_matrix_, combined_matrix); | 355 combined_matrix = MatrixMul(projection_matrix_, combined_matrix); |
328 | 356 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 glDisable(GL_DEPTH_TEST); | 426 glDisable(GL_DEPTH_TEST); |
399 glDisable(GL_SCISSOR_TEST); | 427 glDisable(GL_SCISSOR_TEST); |
400 glDisable(GL_BLEND); | 428 glDisable(GL_BLEND); |
401 glDisable(GL_POLYGON_OFFSET_FILL); | 429 glDisable(GL_POLYGON_OFFSET_FILL); |
402 | 430 |
403 // Don't need to clear, since we're drawing over the entire render target. | 431 // Don't need to clear, since we're drawing over the entire render target. |
404 glClear(GL_COLOR_BUFFER_BIT); | 432 glClear(GL_COLOR_BUFFER_BIT); |
405 | 433 |
406 glViewport(0, 0, render_size_.width, render_size_.height); | 434 glViewport(0, 0, render_size_.width, render_size_.height); |
407 vr_shell_renderer_->GetWebVrRenderer()->Draw(content_texture_id_); | 435 vr_shell_renderer_->GetWebVrRenderer()->Draw(content_texture_id_); |
436 } | |
408 | 437 |
409 if (!webvr_secure_origin_) { | 438 void VrShell::DrawWebVrOverlay(int64_t present_time_nanos) { |
410 // TODO(klausw): Draw the insecure origin warning here. | 439 // Draw WebVR security warning overlays for each eye. This uses |
440 // the eye-from-head matrices but not the pose, goal is to place | |
441 // the icons in an eye-relative position so that they follow along | |
442 // with head rotations. | |
443 | |
444 gvr::Mat4f left_eye_view_matrix = | |
445 gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE); | |
446 gvr::Mat4f right_eye_view_matrix = | |
447 gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE); | |
448 | |
449 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, | |
450 buffer_viewport_.get()); | |
451 DrawWebVrEye(left_eye_view_matrix, *buffer_viewport_, present_time_nanos); | |
452 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, | |
453 buffer_viewport_.get()); | |
454 DrawWebVrEye(right_eye_view_matrix, *buffer_viewport_, present_time_nanos); | |
455 } | |
456 | |
457 void VrShell::DrawWebVrEye(const gvr::Mat4f& view_matrix, | |
458 const gvr::BufferViewport& params, | |
459 int64_t present_time_nanos) { | |
460 gvr::Recti pixel_rect = | |
461 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); | |
462 glViewport(pixel_rect.left, pixel_rect.bottom, | |
463 pixel_rect.right - pixel_rect.left, | |
464 pixel_rect.top - pixel_rect.bottom); | |
465 glScissor(pixel_rect.left, pixel_rect.bottom, | |
466 pixel_rect.right - pixel_rect.left, | |
467 pixel_rect.top - pixel_rect.bottom); | |
468 | |
469 gvr::Mat4f projection_matrix = | |
470 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); | |
471 | |
472 if (!IsUiTextureReady()) { | |
473 // If the UI texture hasn't been initialized yet, we can't draw the overlay. | |
474 return; | |
411 } | 475 } |
476 | |
477 // Draw insecure content warning icons. | |
478 const float warning_depth = 0.7f; // Distance in meters. | |
479 | |
480 // Show IDS_WEBSITE_SETTINGS_INSECURE_WEBVR_CONTENT_PERMANENT text. | |
481 gvr::Mat4f icon_pos; | |
482 SetIdentityM(icon_pos); | |
483 const float small_icon_width = 2 * 0.15f * warning_depth; | |
484 const float small_icon_height = small_icon_width / 2.0f; // 2:1 aspect. | |
485 const float small_icon_angle = 20.0f * M_PI / 180.f; // Degrees to radians. | |
486 ScaleM(icon_pos, icon_pos, small_icon_width, small_icon_height, 1.0f); | |
487 TranslateM(icon_pos, icon_pos, 0.0f, 0.0f, -warning_depth); | |
488 icon_pos = MatrixMul( | |
489 QuatToMatrix(QuatFromAxisAngle(1.f, 0.f, 0.f, small_icon_angle)), | |
490 icon_pos); | |
491 gvr::Mat4f combined = MatrixMul(projection_matrix, | |
492 MatrixMul(view_matrix, icon_pos)); | |
493 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( | |
494 ui_texture_id_, combined, | |
495 MakeUiPixelCopyRect(kWebVrWarningPermanentRect)); | |
cjgrant
2016/09/28 17:48:04
This isn't making a pixel copy-rect, it's making a
| |
496 | |
497 // Check if we also need to show the transient warning. | |
498 if (present_time_nanos > webvr_warning_end_nanos_) { | |
499 return; | |
500 } | |
501 | |
502 // Show IDS_WEBSITE_SETTINGS_INSECURE_WEBVR_CONTENT_TRANSIENT text. | |
503 SetIdentityM(icon_pos); | |
504 const float large_icon_width = 2 * 0.25f * warning_depth; | |
505 const float large_icon_height = large_icon_width / 2.0f; // 2:1 aspect. | |
506 ScaleM(icon_pos, icon_pos, large_icon_width, large_icon_height, 1.0f); | |
507 TranslateM(icon_pos, icon_pos, 0.0f, 0.0f, -warning_depth); | |
508 combined = MatrixMul(projection_matrix, | |
509 MatrixMul(view_matrix, icon_pos)); | |
510 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( | |
511 ui_texture_id_, combined, | |
512 MakeUiPixelCopyRect(kWebVrWarningTransientRect)); | |
513 | |
412 } | 514 } |
413 | 515 |
414 void VrShell::OnPause(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 516 void VrShell::OnPause(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
415 if (gvr_api_ == nullptr) | 517 if (gvr_api_ == nullptr) |
416 return; | 518 return; |
417 gvr_api_->PauseTracking(); | 519 gvr_api_->PauseTracking(); |
418 } | 520 } |
419 | 521 |
420 void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 522 void VrShell::OnResume(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
421 if (gvr_api_ == nullptr) | 523 if (gvr_api_ == nullptr) |
(...skipping 18 matching lines...) Expand all Loading... | |
440 // TODO(bshe): ui_text_width_ and ui_tex_height_ should be only used on render | 542 // TODO(bshe): ui_text_width_ and ui_tex_height_ should be only used on render |
441 // thread. | 543 // thread. |
442 ui_tex_width_ = width; | 544 ui_tex_width_ = width; |
443 ui_tex_height_ = height; | 545 ui_tex_height_ = height; |
444 } | 546 } |
445 | 547 |
446 void VrShell::SetWebVrMode(JNIEnv* env, | 548 void VrShell::SetWebVrMode(JNIEnv* env, |
447 const base::android::JavaParamRef<jobject>& obj, | 549 const base::android::JavaParamRef<jobject>& obj, |
448 bool enabled) { | 550 bool enabled) { |
449 webvr_mode_ = enabled; | 551 webvr_mode_ = enabled; |
552 if (enabled) { | |
553 const int64_t warning_seconds = 30; | |
554 int64_t now = gvr::GvrApi::GetTimePointNow().monotonic_system_time_nanos; | |
555 webvr_warning_end_nanos_ = now + warning_seconds * 1000 * 1000 * 1000; | |
556 } else { | |
557 webvr_warning_end_nanos_ = 0; | |
558 } | |
450 } | 559 } |
451 | 560 |
452 void VrShell::SetWebVRSecureOrigin(bool secure_origin) { | 561 void VrShell::SetWebVRSecureOrigin(bool secure_origin) { |
453 webvr_secure_origin_ = secure_origin; | 562 webvr_secure_origin_ = secure_origin; |
454 } | 563 } |
455 | 564 |
456 void VrShell::SubmitWebVRFrame() { | 565 void VrShell::SubmitWebVRFrame() { |
457 } | 566 } |
458 | 567 |
459 void VrShell::UpdateWebVRTextureBounds( | 568 void VrShell::UpdateWebVRTextureBounds( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 content::WebContents::FromJavaWebContents(content_web_contents)); | 605 content::WebContents::FromJavaWebContents(content_web_contents)); |
497 content::ContentViewCore* ui_core = content::ContentViewCore::FromWebContents( | 606 content::ContentViewCore* ui_core = content::ContentViewCore::FromWebContents( |
498 content::WebContents::FromJavaWebContents(ui_web_contents)); | 607 content::WebContents::FromJavaWebContents(ui_web_contents)); |
499 return reinterpret_cast<intptr_t>(new VrShell( | 608 return reinterpret_cast<intptr_t>(new VrShell( |
500 env, obj, c_core, | 609 env, obj, c_core, |
501 reinterpret_cast<ui::WindowAndroid*>(content_window_android), ui_core, | 610 reinterpret_cast<ui::WindowAndroid*>(content_window_android), ui_core, |
502 reinterpret_cast<ui::WindowAndroid*>(ui_window_android))); | 611 reinterpret_cast<ui::WindowAndroid*>(ui_window_android))); |
503 } | 612 } |
504 | 613 |
505 } // namespace vr_shell | 614 } // namespace vr_shell |
OLD | NEW |