Commit 66ce5b08 authored by shaobo.yan's avatar shaobo.yan Committed by Commit bot

Add support of vrdisplayconnect and vrdisplaydisconnect event

This patch adds vrdisplayconnect and vrdisplaydisconnect event support
of google vr sdk for android. It defines 'connect' status as
nativeContext is available and defines 'disconnect' status as
nativeContext is unavailable.

Note that connect event may dispatch before user get vr devices
because devices in connection status could already deliever user some device
info and user could use this event to do some preparing work.

BUG=389343
R=bajones@chromium.org, kenrd@chromium.org, ochang@chromium.org

Review-Url: https://codereview.chromium.org/2317483002
Cr-Commit-Position: refs/heads/master@{#419392}
parent 197c495e
...@@ -85,22 +85,26 @@ void GvrDeviceProvider::GetDevices(std::vector<VRDevice*>* devices) { ...@@ -85,22 +85,26 @@ void GvrDeviceProvider::GetDevices(std::vector<VRDevice*>* devices) {
devices->push_back(vr_device_.get()); devices->push_back(vr_device_.get());
} }
void GvrDeviceProvider::SetClient(VRClientDispatcher* client) {
if (!client_)
client_.reset(client);
}
void GvrDeviceProvider::Initialize() { void GvrDeviceProvider::Initialize() {
if (!delegate_) { if (!delegate_)
delegate_.reset(new GvrDeviceProviderDelegate()); delegate_.reset(new GvrDeviceProviderDelegate());
}
} }
void GvrDeviceProvider::OnDelegateInitialized(GvrDelegate* delegate) { void GvrDeviceProvider::OnDelegateInitialized(GvrDelegate* delegate) {
if (!vr_device_) if (!vr_device_)
vr_device_.reset(new GvrDevice(this, delegate)); vr_device_.reset(new GvrDevice(this, delegate));
// Should fire a vrdisplayconnected event here. client_->OnDeviceConnectionStatusChanged(vr_device_.get(), true);
} }
void GvrDeviceProvider::OnDelegateShutdown() { void GvrDeviceProvider::OnDelegateShutdown() {
// Nothing to do here just yet. Eventually want to shut down the VRDevice and if (client_ && vr_device_)
// fire a vrdisplaydisconnected event. client_->OnDeviceConnectionStatusChanged(vr_device_.get(), false);
} }
} // namespace device } // namespace device
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "device/vr/android/gvr/gvr_delegate.h" #include "device/vr/android/gvr/gvr_delegate.h"
#include "device/vr/vr_client_dispatcher.h"
#include "device/vr/vr_device.h" #include "device/vr/vr_device.h"
#include "device/vr/vr_device_provider.h" #include "device/vr/vr_device_provider.h"
...@@ -28,7 +29,10 @@ class GvrDeviceProvider : public VRDeviceProvider, public GvrDelegateClient { ...@@ -28,7 +29,10 @@ class GvrDeviceProvider : public VRDeviceProvider, public GvrDelegateClient {
void OnDelegateInitialized(GvrDelegate* delegate) override; void OnDelegateInitialized(GvrDelegate* delegate) override;
void OnDelegateShutdown() override; void OnDelegateShutdown() override;
void SetClient(VRClientDispatcher* client) override;
private: private:
std::unique_ptr<VRClientDispatcher> client_;
std::unique_ptr<VRDevice> vr_device_; std::unique_ptr<VRDevice> vr_device_;
std::unique_ptr<GvrDeviceProviderDelegate> delegate_; std::unique_ptr<GvrDeviceProviderDelegate> delegate_;
......
...@@ -9,9 +9,13 @@ ...@@ -9,9 +9,13 @@
namespace device { namespace device {
class VRDevice;
class VRClientDispatcher { class VRClientDispatcher {
public: public:
virtual void OnDeviceChanged(VRDisplayPtr device) = 0; virtual void OnDeviceChanged(VRDisplayPtr device) = 0;
virtual void OnDeviceConnectionStatusChanged(VRDevice* device,
bool is_connected) = 0;
}; };
} // namespace device } // namespace device
......
...@@ -246,13 +246,32 @@ void VRDeviceManager::SubmitFrame(VRServiceImpl* service, ...@@ -246,13 +246,32 @@ void VRDeviceManager::SubmitFrame(VRServiceImpl* service,
presenting_device_->SubmitFrame(std::move(pose)); presenting_device_->SubmitFrame(std::move(pose));
} }
void VRDeviceManager::OnDeviceConnectionStatusChanged(VRDevice* device,
bool is_connected) {
if (is_connected) {
VRDisplayPtr vr_device_info = device->GetVRDevice();
if (vr_device_info.is_null())
return;
vr_device_info->index = device->id();
for (const auto& service : services_)
service->client()->OnDisplayConnected(vr_device_info.Clone());
} else {
for (const auto& service : services_)
service->client()->OnDisplayDisconnected(device->id());
}
}
void VRDeviceManager::InitializeProviders() { void VRDeviceManager::InitializeProviders() {
if (vr_initialized_) { if (vr_initialized_) {
return; return;
} }
for (const auto& provider : providers_) for (const auto& provider : providers_) {
provider->SetClient(this);
provider->Initialize(); provider->Initialize();
}
vr_initialized_ = true; vr_initialized_ = true;
} }
......
...@@ -51,6 +51,8 @@ class VRDeviceManager : public VRClientDispatcher { ...@@ -51,6 +51,8 @@ class VRDeviceManager : public VRClientDispatcher {
// VRClientDispatcher implementation // VRClientDispatcher implementation
void OnDeviceChanged(VRDisplayPtr device) override; void OnDeviceChanged(VRDisplayPtr device) override;
void OnDeviceConnectionStatusChanged(VRDevice* device,
bool is_connected) override;
private: private:
friend class VRDeviceManagerTest; friend class VRDeviceManagerTest;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
namespace device { namespace device {
class VRClientDispatcher;
class VRDevice; class VRDevice;
class VRDeviceProvider { class VRDeviceProvider {
...@@ -22,6 +23,8 @@ class VRDeviceProvider { ...@@ -22,6 +23,8 @@ class VRDeviceProvider {
virtual void Initialize() = 0; virtual void Initialize() = 0;
virtual void PollEvents() {} virtual void PollEvents() {}
virtual void SetClient(VRClientDispatcher* client) {}
}; };
} // namespace device } // namespace device
......
...@@ -78,4 +78,6 @@ interface VRService { ...@@ -78,4 +78,6 @@ interface VRService {
interface VRServiceClient { interface VRServiceClient {
OnDisplayChanged(VRDisplay display); OnDisplayChanged(VRDisplay display);
OnExitPresent(uint32 index); OnExitPresent(uint32 index);
OnDisplayConnected(VRDisplay display);
OnDisplayDisconnected(uint32 index);
}; };
...@@ -27,6 +27,13 @@ class MockVRServiceClient : public VRServiceClient { ...@@ -27,6 +27,13 @@ class MockVRServiceClient : public VRServiceClient {
MOCK_METHOD1(OnExitPresent, void(uint32_t index)); MOCK_METHOD1(OnExitPresent, void(uint32_t index));
MOCK_METHOD1(OnDisplayConnected, void(const VRDisplay& display));
void OnDisplayConnected(VRDisplayPtr display) override {
OnDisplayConnected(*display);
last_display_ = std::move(display);
}
void OnDisplayDisconnected(unsigned index) override {}
const VRDisplayPtr& LastDisplay() { return last_display_; } const VRDisplayPtr& LastDisplay() { return last_display_; }
private: private:
...@@ -180,4 +187,21 @@ TEST_F(VRServiceImplTest, DevicePresentationIsolation) { ...@@ -180,4 +187,21 @@ TEST_F(VRServiceImplTest, DevicePresentationIsolation) {
EXPECT_EQ(device.get(), VRDeviceManager::GetAllowedDevice( EXPECT_EQ(device.get(), VRDeviceManager::GetAllowedDevice(
service_2->service(), device->id())); service_2->service(), device->id()));
} }
// Ensure that DeviceChanged calls are dispatched to all active services.
TEST_F(VRServiceImplTest, DeviceConnectedDispatched) {
std::unique_ptr<VRServiceTestBinding> service_1 = BindService();
std::unique_ptr<VRServiceTestBinding> service_2 = BindService();
EXPECT_CALL(service_1->client(), OnDisplayConnected(_));
EXPECT_CALL(service_2->client(), OnDisplayConnected(_));
std::unique_ptr<FakeVRDevice> device(new FakeVRDevice(provider_));
device_manager_->OnDeviceConnectionStatusChanged(device.get(), true);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(device->id(), service_1->client().LastDisplay()->index);
EXPECT_EQ(device->id(), service_2->client().LastDisplay()->index);
}
} }
...@@ -111,4 +111,11 @@ void NavigatorVR::fireVRDisplayPresentChange(VRDisplay* display) ...@@ -111,4 +111,11 @@ void NavigatorVR::fireVRDisplayPresentChange(VRDisplay* display)
} }
} }
void NavigatorVR::fireVREvent(VRDisplayEvent* event)
{
if (m_frame && m_frame->localDOMWindow()) {
m_frame->localDOMWindow()->enqueueWindowEvent(event);
}
}
} // namespace blink } // namespace blink
...@@ -35,6 +35,8 @@ public: ...@@ -35,6 +35,8 @@ public:
VRController* controller(); VRController* controller();
Document* document(); Document* document();
void fireVREvent(VRDisplayEvent*);
DECLARE_VIRTUAL_TRACE(); DECLARE_VIRTUAL_TRACE();
private: private:
......
...@@ -177,6 +177,28 @@ void VRController::OnExitPresent(unsigned index) ...@@ -177,6 +177,28 @@ void VRController::OnExitPresent(unsigned index)
vrDisplay->forceExitPresent(); vrDisplay->forceExitPresent();
} }
void VRController::OnDisplayConnected(device::blink::VRDisplayPtr display)
{
VRDisplay* vrDisplay = createOrUpdateDisplay(display);
if (!vrDisplay)
return;
m_navigatorVR->fireVREvent(VRDisplayEvent::create(
EventTypeNames::vrdisplayconnect, true, false, vrDisplay, "connect"));
}
void VRController::OnDisplayDisconnected(unsigned index)
{
VRDisplay* vrDisplay = getDisplayForIndex(index);
if (!vrDisplay)
return;
vrDisplay->disconnected();
m_navigatorVR->fireVREvent(VRDisplayEvent::create(
EventTypeNames::vrdisplaydisconnect, true, false, vrDisplay, "disconnect"));
}
void VRController::contextDestroyed() void VRController::contextDestroyed()
{ {
// If the document context was destroyed, shut down the client connection // If the document context was destroyed, shut down the client connection
......
...@@ -55,6 +55,8 @@ private: ...@@ -55,6 +55,8 @@ private:
// VRServiceClient. // VRServiceClient.
void OnDisplayChanged(device::blink::VRDisplayPtr) override; void OnDisplayChanged(device::blink::VRDisplayPtr) override;
void OnExitPresent(unsigned index) override; void OnExitPresent(unsigned index) override;
void OnDisplayConnected(device::blink::VRDisplayPtr) override;
void OnDisplayDisconnected(unsigned) override;
// ContextLifecycleObserver. // ContextLifecycleObserver.
void contextDestroyed() override; void contextDestroyed() override;
......
...@@ -83,6 +83,12 @@ void VRDisplay::update(const device::blink::VRDisplayPtr& display) ...@@ -83,6 +83,12 @@ void VRDisplay::update(const device::blink::VRDisplayPtr& display)
} }
} }
void VRDisplay::disconnected()
{
if (m_isConnected)
m_isConnected = !m_isConnected;
}
bool VRDisplay::getFrameData(VRFrameData* frameData) bool VRDisplay::getFrameData(VRFrameData* frameData)
{ {
updatePose(); updatePose();
......
...@@ -91,6 +91,7 @@ protected: ...@@ -91,6 +91,7 @@ protected:
void forceExitPresent(); void forceExitPresent();
void updateLayerBounds(); void updateLayerBounds();
void disconnected();
VRController* controller(); VRController* controller();
......
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