Commit ec73e6ef authored by Anna Offenwanger's avatar Anna Offenwanger Committed by Commit Bot

Move runtime management logic out of XRDeviceImpl

Runtimes should only be handled by the XRRuntimeManager. Moving the
logic which handles getting current display info as well as checking for
session supports out of XRDeviceImpl is a step in that direction.

Bug: 842025
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:linux_vr;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel
Change-Id: I95bd331a30c91deb665aa12853f8a80d79c0e51b
Reviewed-on: https://chromium-review.googlesource.com/1181535Reviewed-by: default avatarMichael Thiessen <mthiesse@chromium.org>
Commit-Queue: Anna Offenwanger <offenwanger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584550}
parent c0af3bf1
......@@ -37,7 +37,7 @@ void BrowserXRRuntime::OnDisplayInfoChanged(
device::mojom::VRDisplayInfoPtr vr_device_info) {
display_info_ = std::move(vr_device_info);
for (XRDeviceImpl* device : renderer_device_connections_) {
device->OnChanged();
device->RuntimesChanged();
}
}
......
......@@ -99,7 +99,7 @@ void VRServiceImpl::ConnectRuntime(BrowserXRRuntime* runtime) {
// We just need to notify device_ when new capabilities were added after
// initialization.
if (device_) {
device_->OnRuntimeAvailable(runtime);
device_->RuntimesChanged();
}
if (client_) {
......@@ -109,7 +109,7 @@ void VRServiceImpl::ConnectRuntime(BrowserXRRuntime* runtime) {
void VRServiceImpl::RemoveRuntime(BrowserXRRuntime* runtime) {
if (device_) {
device_->OnRuntimeRemoved(runtime);
device_->RuntimesChanged();
}
if (client_) {
......
......@@ -53,56 +53,6 @@ device::mojom::XRRuntimeSessionOptionsPtr GetRuntimeOptions(
} // namespace
device::mojom::VRDisplayInfoPtr XRDeviceImpl::GetCurrentVRDisplayInfo() {
// Get an immersive_runtime_ device if there is one.
if (!immersive_runtime_) {
immersive_runtime_ = XRRuntimeManager::GetInstance()->GetImmersiveRuntime();
if (immersive_runtime_) {
// Listen to changes for this device.
immersive_runtime_->OnRendererDeviceAdded(this);
}
}
// Get an AR device if there is one.
if (!ar_runtime_) {
device::mojom::XRSessionOptions options = {};
options.provide_passthrough_camera = true;
ar_runtime_ =
XRRuntimeManager::GetInstance()->GetRuntimeForOptions(&options);
if (ar_runtime_) {
// Listen to changes for this device.
ar_runtime_->OnRendererDeviceAdded(this);
}
}
// If there is neither, use the generic non-immersive device.
if (!ar_runtime_ && !immersive_runtime_) {
if (!non_immersive_runtime_) {
device::mojom::XRSessionOptions options = {};
non_immersive_runtime_ =
XRRuntimeManager::GetInstance()->GetRuntimeForOptions(&options);
if (non_immersive_runtime_) {
// Listen to changes for this device.
non_immersive_runtime_->OnRendererDeviceAdded(this);
}
}
// If we don't have an AR or immersive device, return the generic non-
// immersive device's DisplayInfo if we have it.
return non_immersive_runtime_ ? non_immersive_runtime_->GetVRDisplayInfo()
: nullptr;
}
// Use the immersive or AR device. However, if we are using the immersive
// device's info, and AR is supported, reflect that in capabilities.
device::mojom::VRDisplayInfoPtr device_info =
immersive_runtime_ ? immersive_runtime_->GetVRDisplayInfo()
: ar_runtime_->GetVRDisplayInfo();
device_info->capabilities->can_provide_pass_through_images = !!ar_runtime_;
return device_info;
}
XRDeviceImpl::XRDeviceImpl(content::RenderFrameHost* render_frame_host,
device::mojom::XRDeviceRequest request)
: // TODO(https://crbug.com/846392): render_frame_host can be null because
......@@ -149,12 +99,7 @@ void XRDeviceImpl::OnSessionCreated(
}
XRDeviceImpl::~XRDeviceImpl() {
if (immersive_runtime_)
immersive_runtime_->OnRendererDeviceRemoved(this);
if (non_immersive_runtime_)
non_immersive_runtime_->OnRendererDeviceRemoved(this);
if (ar_runtime_)
ar_runtime_->OnRendererDeviceRemoved(this);
XRRuntimeManager::GetInstance()->OnRendererDeviceRemoved(this);
}
void XRDeviceImpl::RequestSession(
......@@ -220,11 +165,11 @@ void XRDeviceImpl::RequestSession(
}
}
void XRDeviceImpl::SupportsSession(device::mojom::XRSessionOptionsPtr options,
SupportsSessionCallback callback) {
bool supports = XRRuntimeManager::GetInstance()->GetRuntimeForOptions(
options.get()) != nullptr;
std::move(callback).Run(supports);
void XRDeviceImpl::SupportsSession(
device::mojom::XRSessionOptionsPtr options,
device::mojom::XRDevice::SupportsSessionCallback callback) {
XRRuntimeManager::GetInstance()->SupportsSession(std::move(options),
std::move(callback));
}
void XRDeviceImpl::ReportRequestPresent() {
......@@ -242,20 +187,19 @@ void XRDeviceImpl::ReportRequestPresent() {
}
void XRDeviceImpl::ExitPresent() {
if (immersive_runtime_)
immersive_runtime_->ExitPresent(this);
BrowserXRRuntime* immersive_runtime =
XRRuntimeManager::GetInstance()->GetImmersiveRuntime();
if (immersive_runtime)
immersive_runtime->ExitPresent(this);
}
void XRDeviceImpl::SetListeningForActivate(
device::mojom::VRDisplayClientPtr client) {
client_ = std::move(client);
if (!immersive_runtime_) {
// This function sets the runtimes.
GetCurrentVRDisplayInfo();
}
if (immersive_runtime_ && client_) {
immersive_runtime_->UpdateListeningForActivate(this);
BrowserXRRuntime* immersive_runtime =
XRRuntimeManager::GetInstance()->GetImmersiveRuntime();
if (immersive_runtime && client) {
immersive_runtime->UpdateListeningForActivate(this);
}
}
......@@ -270,10 +214,11 @@ void XRDeviceImpl::GetImmersiveVRDisplayInfo(
void XRDeviceImpl::SetInFocusedFrame(bool in_focused_frame) {
in_focused_frame_ = in_focused_frame;
if (ListeningForActivate() && immersive_runtime_) {
// No change, except focus.
immersive_runtime_->UpdateListeningForActivate(this);
if (ListeningForActivate()) {
BrowserXRRuntime* immersive_runtime =
XRRuntimeManager::GetInstance()->GetImmersiveRuntime();
if (immersive_runtime)
immersive_runtime->UpdateListeningForActivate(this);
}
magic_window_controllers_.ForAllPtrs(
......@@ -282,8 +227,9 @@ void XRDeviceImpl::SetInFocusedFrame(bool in_focused_frame) {
});
}
void XRDeviceImpl::OnChanged() {
device::mojom::VRDisplayInfoPtr display_info = GetCurrentVRDisplayInfo();
void XRDeviceImpl::RuntimesChanged() {
device::mojom::VRDisplayInfoPtr display_info =
XRRuntimeManager::GetInstance()->GetCurrentVRDisplayInfo(this);
if (display_info) {
session_clients_.ForAllPtrs(
[&display_info](device::mojom::XRSessionClient* client) {
......@@ -292,27 +238,6 @@ void XRDeviceImpl::OnChanged() {
}
}
void XRDeviceImpl::OnRuntimeRemoved(BrowserXRRuntime* runtime) {
if (runtime == immersive_runtime_) {
immersive_runtime_ = nullptr;
}
if (runtime == non_immersive_runtime_) {
non_immersive_runtime_ = nullptr;
}
if (runtime == ar_runtime_) {
ar_runtime_ = nullptr;
}
// Try to update our VRDisplayInfo.
OnChanged();
}
void XRDeviceImpl::OnRuntimeAvailable(BrowserXRRuntime* runtime) {
// Try to update our VRDisplayInfo. That may use the new device.
OnChanged();
}
void XRDeviceImpl::OnExitPresent() {
session_clients_.ForAllPtrs(
......
......@@ -40,8 +40,9 @@ class XRDeviceImpl : public device::mojom::XRDevice {
device::mojom::XRSessionOptionsPtr options,
bool triggered_by_displayactive,
device::mojom::XRDevice::RequestSessionCallback callback) override;
void SupportsSession(device::mojom::XRSessionOptionsPtr options,
SupportsSessionCallback callback) override;
void SupportsSession(
device::mojom::XRSessionOptionsPtr options,
device::mojom::XRDevice::SupportsSessionCallback callback) override;
void ExitPresent() override;
// device::mojom::XRDevice WebVR compatibility functions
void GetImmersiveVRDisplayInfo(
......@@ -52,12 +53,8 @@ class XRDeviceImpl : public device::mojom::XRDevice {
void SetInFocusedFrame(bool in_focused_frame);
// Notifications when devices are added/removed.
void OnRuntimeRemoved(BrowserXRRuntime* device);
void OnRuntimeAvailable(BrowserXRRuntime* device);
// Notifications/calls from BrowserXRRuntime:
void OnChanged();
void RuntimesChanged();
void OnExitPresent();
void OnBlur();
void OnFocus();
......@@ -88,8 +85,6 @@ class XRDeviceImpl : public device::mojom::XRDevice {
// object.
bool IsSecureContextRequirementSatisfied();
device::mojom::VRDisplayInfoPtr GetCurrentVRDisplayInfo();
bool in_focused_frame_ = false;
content::RenderFrameHost* render_frame_host_;
......@@ -102,12 +97,6 @@ class XRDeviceImpl : public device::mojom::XRDevice {
magic_window_controllers_;
int next_key_ = 0;
// If we start an immersive session, or are listening to immersive activation,
// notify this device if we are destroyed.
BrowserXRRuntime* immersive_runtime_ = nullptr;
BrowserXRRuntime* non_immersive_runtime_ = nullptr;
BrowserXRRuntime* ar_runtime_ = nullptr;
base::WeakPtrFactory<XRDeviceImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(XRDeviceImpl);
......
......@@ -152,6 +152,64 @@ BrowserXRRuntime* XRRuntimeManager::GetRuntimeForOptions(
return nullptr;
}
device::mojom::VRDisplayInfoPtr XRRuntimeManager::GetCurrentVRDisplayInfo(
XRDeviceImpl* device) {
// Get an immersive_runtime device if there is one.
auto* immersive_runtime = GetImmersiveRuntime();
if (immersive_runtime) {
// Listen to changes for this device.
immersive_runtime->OnRendererDeviceAdded(device);
}
// Get an AR device if there is one.
device::mojom::XRSessionOptions options = {};
options.provide_passthrough_camera = true;
auto* ar_runtime = GetRuntimeForOptions(&options);
if (ar_runtime) {
// Listen to changes for this device.
ar_runtime->OnRendererDeviceAdded(device);
}
// If there is neither, use the generic non-immersive device.
if (!ar_runtime && !immersive_runtime) {
device::mojom::XRSessionOptions options = {};
auto* non_immersive_runtime = GetRuntimeForOptions(&options);
if (non_immersive_runtime) {
// Listen to changes for this device.
non_immersive_runtime->OnRendererDeviceAdded(device);
}
// If we don't have an AR or immersive device, return the generic non-
// immersive device's DisplayInfo if we have it.
return non_immersive_runtime ? non_immersive_runtime->GetVRDisplayInfo()
: nullptr;
}
// Use the immersive or AR device. However, if we are using the immersive
// device's info, and AR is supported, reflect that in capabilities.
device::mojom::VRDisplayInfoPtr device_info =
immersive_runtime ? immersive_runtime->GetVRDisplayInfo()
: ar_runtime->GetVRDisplayInfo();
device_info->capabilities->can_provide_pass_through_images = !!ar_runtime;
return device_info;
}
void XRRuntimeManager::SupportsSession(
device::mojom::XRSessionOptionsPtr options,
device::mojom::XRDevice::SupportsSessionCallback callback) {
auto* runtime = GetRuntimeForOptions(options.get());
if (!runtime) {
std::move(callback).Run(false);
return;
}
// TODO(http://crbug.com/842025): Pass supports session on to the device
// runtimes.
std::move(callback).Run(true);
}
bool XRRuntimeManager::HasAnyRuntime() {
return runtimes_.size() > 0;
}
......@@ -196,6 +254,12 @@ void XRRuntimeManager::RemoveService(VRServiceImpl* service) {
}
}
void XRRuntimeManager::OnRendererDeviceRemoved(XRDeviceImpl* device) {
for (const auto& runtime : runtimes_) {
runtime.second->OnRendererDeviceRemoved(device);
}
}
void XRRuntimeManager::AddRuntime(device::mojom::XRDeviceId id,
device::mojom::VRDisplayInfoPtr info,
device::mojom::XRRuntimePtr runtime) {
......
......@@ -49,9 +49,15 @@ class VR_EXPORT XRRuntimeManager {
BrowserXRRuntime* GetRuntimeForOptions(
device::mojom::XRSessionOptions* options);
BrowserXRRuntime* GetImmersiveRuntime();
device::mojom::VRDisplayInfoPtr GetCurrentVRDisplayInfo(XRDeviceImpl* device);
void OnRendererDeviceRemoved(XRDeviceImpl* device);
bool HasAnyRuntime();
void SupportsSession(
device::mojom::XRSessionOptionsPtr options,
device::mojom::XRDevice::SupportsSessionCallback callback);
protected:
using ProviderList = std::vector<std::unique_ptr<device::VRDeviceProvider>>;
......
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