Commit 52e14d97 authored by yuqinganniesun's avatar yuqinganniesun Committed by Commit Bot

Adding synchronization between surface rendering and gl initialization

Block VrShellGl::InitializeGl until surface is set.

R=cjgrant@chromium.org, mthiesse@chromium.org

Change-Id: I31f18f3a8ed805174f5fd8e77ce99fc8ec539c59
Reviewed-on: https://chromium-review.googlesource.com/1099610Reviewed-by: default avatarMichael Thiessen <mthiesse@chromium.org>
Commit-Queue: Annie Sun <yuqinganniesun@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570474}
parent 420501d5
...@@ -27,21 +27,25 @@ namespace vr { ...@@ -27,21 +27,25 @@ namespace vr {
VrGLThread::VrGLThread( VrGLThread::VrGLThread(
const base::WeakPtr<VrShell>& weak_vr_shell, const base::WeakPtr<VrShell>& weak_vr_shell,
scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
base::WaitableEvent* gl_surface_created_event,
gvr_context* gvr_api, gvr_context* gvr_api,
const UiInitialState& ui_initial_state, const UiInitialState& ui_initial_state,
bool reprojected_rendering, bool reprojected_rendering,
bool daydream_support, bool daydream_support,
bool pause_content, bool pause_content,
bool low_density) bool low_density,
base::OnceCallback<gfx::AcceleratedWidget()> surface_callback)
: base::android::JavaHandlerThread("VrShellGL"), : base::android::JavaHandlerThread("VrShellGL"),
weak_vr_shell_(weak_vr_shell), weak_vr_shell_(weak_vr_shell),
main_thread_task_runner_(std::move(main_thread_task_runner)), main_thread_task_runner_(std::move(main_thread_task_runner)),
gl_surface_created_event_(gl_surface_created_event),
gvr_api_(gvr_api), gvr_api_(gvr_api),
ui_initial_state_(ui_initial_state), ui_initial_state_(ui_initial_state),
reprojected_rendering_(reprojected_rendering), reprojected_rendering_(reprojected_rendering),
daydream_support_(daydream_support), daydream_support_(daydream_support),
pause_content_(pause_content), pause_content_(pause_content),
low_density_(low_density) {} low_density_(low_density),
surface_callback_(std::move(surface_callback)) {}
VrGLThread::~VrGLThread() { VrGLThread::~VrGLThread() {
Stop(); Stop();
...@@ -91,8 +95,11 @@ void VrGLThread::Init() { ...@@ -91,8 +95,11 @@ void VrGLThread::Init() {
ui_initial_state_.in_web_vr, pause_content_, low_density_); ui_initial_state_.in_web_vr, pause_content_, low_density_);
weak_browser_ui_ = vr_shell_gl_->GetBrowserUiWeakPtr(); weak_browser_ui_ = vr_shell_gl_->GetBrowserUiWeakPtr();
task_runner()->PostTask(
vr_shell_gl_->Initialize(); FROM_HERE,
base::BindOnce(&VrShellGl::Initialize, vr_shell_gl_->GetWeakPtr(),
base::Unretained(gl_surface_created_event_),
base::Passed(std::move(surface_callback_))));
} }
void VrGLThread::CleanUp() { void VrGLThread::CleanUp() {
......
...@@ -23,9 +23,11 @@ ...@@ -23,9 +23,11 @@
#include "chrome/browser/vr/ui_browser_interface.h" #include "chrome/browser/vr/ui_browser_interface.h"
#include "chrome/browser/vr/ui_test_input.h" #include "chrome/browser/vr/ui_test_input.h"
#include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/gvr_types.h" #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/gvr_types.h"
#include "ui/gfx/native_widget_types.h"
namespace base { namespace base {
class Version; class Version;
class WaitableEvent;
} // namespace base } // namespace base
namespace vr { namespace vr {
...@@ -44,12 +46,14 @@ class VrGLThread : public base::android::JavaHandlerThread, ...@@ -44,12 +46,14 @@ class VrGLThread : public base::android::JavaHandlerThread,
VrGLThread( VrGLThread(
const base::WeakPtr<VrShell>& weak_vr_shell, const base::WeakPtr<VrShell>& weak_vr_shell,
scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
base::WaitableEvent* gl_surface_created_event,
gvr_context* gvr_api, gvr_context* gvr_api,
const UiInitialState& ui_initial_state, const UiInitialState& ui_initial_state,
bool reprojected_rendering, bool reprojected_rendering,
bool daydream_support, bool daydream_support,
bool pause_content, bool pause_content,
bool low_density); bool low_density,
base::OnceCallback<gfx::AcceleratedWidget()> surface_callback);
~VrGLThread() override; ~VrGLThread() override;
base::WeakPtr<VrShellGl> GetVrShellGl(); base::WeakPtr<VrShellGl> GetVrShellGl();
...@@ -172,12 +176,14 @@ class VrGLThread : public base::android::JavaHandlerThread, ...@@ -172,12 +176,14 @@ class VrGLThread : public base::android::JavaHandlerThread,
// This state is used for initializing vr_shell_gl_. // This state is used for initializing vr_shell_gl_.
scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
base::WaitableEvent* gl_surface_created_event_;
gvr_context* gvr_api_; gvr_context* gvr_api_;
UiInitialState ui_initial_state_; UiInitialState ui_initial_state_;
bool reprojected_rendering_; bool reprojected_rendering_;
bool daydream_support_; bool daydream_support_;
bool pause_content_; bool pause_content_;
bool low_density_; bool low_density_;
base::OnceCallback<gfx::AcceleratedWidget()> surface_callback_;
DISALLOW_COPY_AND_ASSIGN(VrGLThread); DISALLOW_COPY_AND_ASSIGN(VrGLThread);
}; };
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
...@@ -78,7 +79,6 @@ ...@@ -78,7 +79,6 @@
#include "ui/gfx/geometry/point_f.h" #include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_conversions.h" #include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gl/android/surface_texture.h" #include "ui/gl/android/surface_texture.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -157,16 +157,23 @@ VrShell::VrShell(JNIEnv* env, ...@@ -157,16 +157,23 @@ VrShell::VrShell(JNIEnv* env,
display_size_meters_(display_width_meters, display_height_meters), display_size_meters_(display_width_meters, display_height_meters),
display_size_pixels_(display_width_pixels, display_height_pixels), display_size_pixels_(display_width_pixels, display_height_pixels),
waiting_for_assets_component_timer_(false, false), waiting_for_assets_component_timer_(false, false),
gl_surface_created_event_(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DVLOG(1) << __FUNCTION__ << "=" << this; DVLOG(1) << __FUNCTION__ << "=" << this;
DCHECK(g_vr_shell_instance == nullptr); DCHECK(g_vr_shell_instance == nullptr);
g_vr_shell_instance = this; g_vr_shell_instance = this;
j_vr_shell_.Reset(env, obj); j_vr_shell_.Reset(env, obj);
base::OnceCallback<gfx::AcceleratedWidget()> surface_callback =
base::BindOnce(&VrShell::GetRenderSurface, base::Unretained(this));
gl_thread_ = std::make_unique<VrGLThread>( gl_thread_ = std::make_unique<VrGLThread>(
weak_ptr_factory_.GetWeakPtr(), main_thread_task_runner_, gvr_api, weak_ptr_factory_.GetWeakPtr(), main_thread_task_runner_,
ui_initial_state, reprojected_rendering_, HasDaydreamSupport(env), &gl_surface_created_event_, gvr_api, ui_initial_state,
pause_content, low_density); reprojected_rendering_, HasDaydreamSupport(env), pause_content,
low_density, std::move(surface_callback));
ui_ = gl_thread_.get(); ui_ = gl_thread_.get();
toolbar_ = std::make_unique<ToolbarHelper>(ui_, this); toolbar_ = std::make_unique<ToolbarHelper>(ui_, this);
autocomplete_controller_ = autocomplete_controller_ =
...@@ -533,9 +540,8 @@ void VrShell::SetSurface(JNIEnv* env, ...@@ -533,9 +540,8 @@ void VrShell::SetSurface(JNIEnv* env,
return; return;
gfx::AcceleratedWidget window = gfx::AcceleratedWidget window =
ANativeWindow_fromSurface(base::android::AttachCurrentThread(), surface); ANativeWindow_fromSurface(base::android::AttachCurrentThread(), surface);
PostToGlThread(FROM_HERE, base::BindOnce(&VrShellGl::InitializeGl, surface_window_ = window;
gl_thread_->GetVrShellGl(), gl_surface_created_event_.Signal();
base::Unretained(window)));
} }
void VrShell::SetWebVrMode(JNIEnv* env, void VrShell::SetWebVrMode(JNIEnv* env,
...@@ -1318,6 +1324,10 @@ std::unique_ptr<PageInfo> VrShell::CreatePageInfo() { ...@@ -1318,6 +1324,10 @@ std::unique_ptr<PageInfo> VrShell::CreatePageInfo() {
entry->GetVirtualURL(), security_info); entry->GetVirtualURL(), security_info);
} }
gfx::AcceleratedWidget VrShell::GetRenderSurface() {
return surface_window_;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Native JNI methods // Native JNI methods
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
......
...@@ -295,6 +295,7 @@ class VrShell : device::GvrGamepadDataProvider, ...@@ -295,6 +295,7 @@ class VrShell : device::GvrGamepadDataProvider,
jint action_type, jint action_type,
jfloat x, jfloat x,
jfloat y); jfloat y);
gfx::AcceleratedWidget GetRenderSurface();
private: private:
~VrShell() override; ~VrShell() override;
...@@ -377,6 +378,9 @@ class VrShell : device::GvrGamepadDataProvider, ...@@ -377,6 +378,9 @@ class VrShell : device::GvrGamepadDataProvider,
base::Timer waiting_for_assets_component_timer_; base::Timer waiting_for_assets_component_timer_;
bool can_load_new_assets_ = false; bool can_load_new_assets_ = false;
base::WaitableEvent gl_surface_created_event_;
gfx::AcceleratedWidget surface_window_ = nullptr;
base::WeakPtrFactory<VrShell> weak_ptr_factory_; base::WeakPtrFactory<VrShell> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(VrShell); DISALLOW_COPY_AND_ASSIGN(VrShell);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "base/metrics/field_trial_params.h" #include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/synchronization/waitable_event.h"
#include "base/task_scheduler/post_task.h" #include "base/task_scheduler/post_task.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
...@@ -353,11 +354,14 @@ VrShellGl::~VrShellGl() { ...@@ -353,11 +354,14 @@ VrShellGl::~VrShellGl() {
webxr_->EndPresentation(); webxr_->EndPresentation();
} }
void VrShellGl::Initialize() { void VrShellGl::Initialize(
base::WaitableEvent* gl_surface_created_event,
base::OnceCallback<gfx::AcceleratedWidget()> callback) {
if (surfaceless_rendering_) { if (surfaceless_rendering_) {
// If we're rendering surfaceless, we'll never get a java surface to render
// into, so we can initialize GL right away.
InitializeGl(nullptr); InitializeGl(nullptr);
} else {
gl_surface_created_event->Wait();
InitializeGl(std::move(callback).Run());
} }
} }
......
...@@ -35,6 +35,10 @@ ...@@ -35,6 +35,10 @@
#include "ui/gfx/geometry/size_f.h" #include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/native_widget_types.h" #include "ui/gfx/native_widget_types.h"
namespace base {
class WaitableEvent;
}
namespace gl { namespace gl {
class GLContext; class GLContext;
class GLFence; class GLFence;
...@@ -272,8 +276,9 @@ class VrShellGl : public device::mojom::VRPresentationProvider { ...@@ -272,8 +276,9 @@ class VrShellGl : public device::mojom::VRPresentationProvider {
bool low_density); bool low_density);
~VrShellGl() override; ~VrShellGl() override;
void Initialize(); void Initialize(base::WaitableEvent* gl_surface_created_event,
void InitializeGl(gfx::AcceleratedWidget window); base::OnceCallback<gfx::AcceleratedWidget()> callback);
void InitializeGl(gfx::AcceleratedWidget surface);
void OnTriggerEvent(bool pressed); void OnTriggerEvent(bool pressed);
void OnPause(); void OnPause();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment