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) {
devices->push_back(vr_device_.get());
}
void GvrDeviceProvider::SetClient(VRClientDispatcher* client) {
if (!client_)
client_.reset(client);
}
void GvrDeviceProvider::Initialize() {
if (!delegate_) {
if (!delegate_)
delegate_.reset(new GvrDeviceProviderDelegate());
}
}
void GvrDeviceProvider::OnDelegateInitialized(GvrDelegate* delegate) {
if (!vr_device_)
vr_device_.reset(new GvrDevice(this, delegate));
// Should fire a vrdisplayconnected event here.
client_->OnDeviceConnectionStatusChanged(vr_device_.get(), true);
}
void GvrDeviceProvider::OnDelegateShutdown() {
// Nothing to do here just yet. Eventually want to shut down the VRDevice and
// fire a vrdisplaydisconnected event.
if (client_ && vr_device_)
client_->OnDeviceConnectionStatusChanged(vr_device_.get(), false);
}
} // namespace device
......@@ -9,6 +9,7 @@
#include "base/macros.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_provider.h"
......@@ -28,7 +29,10 @@ class GvrDeviceProvider : public VRDeviceProvider, public GvrDelegateClient {
void OnDelegateInitialized(GvrDelegate* delegate) override;
void OnDelegateShutdown() override;
void SetClient(VRClientDispatcher* client) override;
private:
std::unique_ptr<VRClientDispatcher> client_;
std::unique_ptr<VRDevice> vr_device_;
std::unique_ptr<GvrDeviceProviderDelegate> delegate_;
......
......@@ -9,9 +9,13 @@
namespace device {
class VRDevice;
class VRClientDispatcher {
public:
virtual void OnDeviceChanged(VRDisplayPtr device) = 0;
virtual void OnDeviceConnectionStatusChanged(VRDevice* device,
bool is_connected) = 0;
};
} // namespace device
......
......@@ -246,13 +246,32 @@ void VRDeviceManager::SubmitFrame(VRServiceImpl* service,
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() {
if (vr_initialized_) {
return;
}
for (const auto& provider : providers_)
for (const auto& provider : providers_) {
provider->SetClient(this);
provider->Initialize();
}
vr_initialized_ = true;
}
......
......@@ -51,6 +51,8 @@ class VRDeviceManager : public VRClientDispatcher {
// VRClientDispatcher implementation
void OnDeviceChanged(VRDisplayPtr device) override;
void OnDeviceConnectionStatusChanged(VRDevice* device,
bool is_connected) override;
private:
friend class VRDeviceManagerTest;
......
......@@ -9,6 +9,7 @@
namespace device {
class VRClientDispatcher;
class VRDevice;
class VRDeviceProvider {
......@@ -22,6 +23,8 @@ class VRDeviceProvider {
virtual void Initialize() = 0;
virtual void PollEvents() {}
virtual void SetClient(VRClientDispatcher* client) {}
};
} // namespace device
......
......@@ -78,4 +78,6 @@ interface VRService {
interface VRServiceClient {
OnDisplayChanged(VRDisplay display);
OnExitPresent(uint32 index);
OnDisplayConnected(VRDisplay display);
OnDisplayDisconnected(uint32 index);
};
......@@ -27,6 +27,13 @@ class MockVRServiceClient : public VRServiceClient {
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_; }
private:
......@@ -180,4 +187,21 @@ TEST_F(VRServiceImplTest, DevicePresentationIsolation) {
EXPECT_EQ(device.get(), VRDeviceManager::GetAllowedDevice(
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)
}
}
void NavigatorVR::fireVREvent(VRDisplayEvent* event)
{
if (m_frame && m_frame->localDOMWindow()) {
m_frame->localDOMWindow()->enqueueWindowEvent(event);
}
}
} // namespace blink
......@@ -35,6 +35,8 @@ public:
VRController* controller();
Document* document();
void fireVREvent(VRDisplayEvent*);
DECLARE_VIRTUAL_TRACE();
private:
......
......@@ -177,6 +177,28 @@ void VRController::OnExitPresent(unsigned index)
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()
{
// If the document context was destroyed, shut down the client connection
......
......@@ -55,6 +55,8 @@ private:
// VRServiceClient.
void OnDisplayChanged(device::blink::VRDisplayPtr) override;
void OnExitPresent(unsigned index) override;
void OnDisplayConnected(device::blink::VRDisplayPtr) override;
void OnDisplayDisconnected(unsigned) override;
// ContextLifecycleObserver.
void contextDestroyed() override;
......
......@@ -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)
{
updatePose();
......
......@@ -91,6 +91,7 @@ protected:
void forceExitPresent();
void updateLayerBounds();
void disconnected();
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