2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2017-10-28 01:55:37 +03:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2016-02-25 02:54:50 +03:00
|
|
|
|
|
|
|
#include "VRDisplayPresentation.h"
|
|
|
|
|
2017-03-23 09:19:11 +03:00
|
|
|
#include "mozilla/dom/DocGroup.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-02-25 02:54:50 +03:00
|
|
|
#include "VRDisplayClient.h"
|
|
|
|
#include "VRLayerChild.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
|
|
|
VRDisplayPresentation::VRDisplayPresentation(VRDisplayClient *aDisplayClient,
|
2017-05-09 02:01:36 +03:00
|
|
|
const nsTArray<mozilla::dom::VRLayer>& aLayers,
|
|
|
|
uint32_t aGroup)
|
2016-02-25 02:54:50 +03:00
|
|
|
: mDisplayClient(aDisplayClient)
|
|
|
|
, mDOMLayers(aLayers)
|
2017-05-09 02:01:36 +03:00
|
|
|
, mGroup(aGroup)
|
2016-02-25 02:54:50 +03:00
|
|
|
{
|
|
|
|
CreateLayers();
|
|
|
|
}
|
|
|
|
|
2017-08-29 02:20:59 +03:00
|
|
|
void
|
|
|
|
VRDisplayPresentation::UpdateLayers(const nsTArray<mozilla::dom::VRLayer>& aLayers)
|
|
|
|
{
|
|
|
|
mDOMLayers = aLayers;
|
|
|
|
CreateLayers();
|
|
|
|
}
|
|
|
|
|
2017-05-09 02:01:36 +03:00
|
|
|
uint32_t
|
|
|
|
VRDisplayPresentation::GetGroup() const
|
|
|
|
{
|
|
|
|
return mGroup;
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
void
|
|
|
|
VRDisplayPresentation::CreateLayers()
|
|
|
|
{
|
2017-08-29 02:20:59 +03:00
|
|
|
VRManagerChild *manager = VRManagerChild::Get();
|
|
|
|
if (!manager) {
|
|
|
|
// This should not happen, but let's log it and avoid a crash in case
|
|
|
|
// of regression.
|
|
|
|
NS_WARNING("VRManagerChild::Get returned null!");
|
2016-02-25 02:54:50 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-29 02:20:59 +03:00
|
|
|
unsigned int iLayer=0;
|
2016-02-25 02:54:50 +03:00
|
|
|
for (dom::VRLayer& layer : mDOMLayers) {
|
|
|
|
dom::HTMLCanvasElement* canvasElement = layer.mSource;
|
|
|
|
if (!canvasElement) {
|
|
|
|
/// XXX In the future we will support WebVR in WebWorkers here
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect leftBounds(0.0, 0.0, 0.5, 1.0);
|
|
|
|
if (layer.mLeftBounds.Length() == 4) {
|
|
|
|
leftBounds.x = layer.mLeftBounds[0];
|
|
|
|
leftBounds.y = layer.mLeftBounds[1];
|
2017-08-14 15:29:28 +03:00
|
|
|
leftBounds.SetWidth(layer.mLeftBounds[2]);
|
|
|
|
leftBounds.SetHeight(layer.mLeftBounds[3]);
|
2016-02-25 02:54:50 +03:00
|
|
|
} else if (layer.mLeftBounds.Length() != 0) {
|
|
|
|
/**
|
|
|
|
* We ignore layers with an incorrect number of values.
|
|
|
|
* In the future, VRDisplay.requestPresent may throw in
|
|
|
|
* this case. See https://github.com/w3c/webvr/issues/71
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect rightBounds(0.5, 0.0, 0.5, 1.0);
|
|
|
|
if (layer.mRightBounds.Length() == 4) {
|
|
|
|
rightBounds.x = layer.mRightBounds[0];
|
|
|
|
rightBounds.y = layer.mRightBounds[1];
|
2017-08-14 15:29:28 +03:00
|
|
|
rightBounds.SetWidth(layer.mRightBounds[2]);
|
|
|
|
rightBounds.SetHeight(layer.mRightBounds[3]);
|
2016-02-25 02:54:50 +03:00
|
|
|
} else if (layer.mRightBounds.Length() != 0) {
|
|
|
|
/**
|
|
|
|
* We ignore layers with an incorrect number of values.
|
|
|
|
* In the future, VRDisplay.requestPresent may throw in
|
|
|
|
* this case. See https://github.com/w3c/webvr/issues/71
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-23 09:19:11 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target;
|
|
|
|
nsIDocument* doc;
|
|
|
|
doc = canvasElement->OwnerDoc();
|
|
|
|
if (doc) {
|
|
|
|
target = doc->EventTargetFor(TaskCategory::Other);
|
|
|
|
}
|
|
|
|
|
2017-08-29 02:20:59 +03:00
|
|
|
if (mLayers.Length() <= iLayer) {
|
|
|
|
// Not enough layers, let's add one
|
|
|
|
RefPtr<VRLayerChild> vrLayer =
|
|
|
|
static_cast<VRLayerChild*>(manager->CreateVRLayer(mDisplayClient->GetDisplayInfo().GetDisplayID(),
|
|
|
|
target, mGroup));
|
|
|
|
if (!vrLayer) {
|
|
|
|
NS_WARNING("CreateVRLayer returned null!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
vrLayer->Initialize(canvasElement, leftBounds, rightBounds);
|
|
|
|
mLayers.AppendElement(vrLayer);
|
|
|
|
} else {
|
|
|
|
// We already have a layer, let's update it
|
|
|
|
mLayers[iLayer]->Initialize(canvasElement, leftBounds, rightBounds);
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
2017-08-29 02:20:59 +03:00
|
|
|
iLayer++;
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
2017-08-29 02:20:59 +03:00
|
|
|
|
|
|
|
// Truncate any excess layers that weren't included in the updated list
|
|
|
|
mLayers.SetLength(iLayer);
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VRDisplayPresentation::DestroyLayers()
|
|
|
|
{
|
|
|
|
for (VRLayerChild* layer : mLayers) {
|
2017-05-03 13:43:39 +03:00
|
|
|
if (layer->IsIPCOpen()) {
|
|
|
|
Unused << layer->SendDestroy();
|
|
|
|
}
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
|
|
|
mLayers.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VRDisplayPresentation::GetDOMLayers(nsTArray<dom::VRLayer>& result)
|
|
|
|
{
|
|
|
|
result = mDOMLayers;
|
|
|
|
}
|
|
|
|
|
|
|
|
VRDisplayPresentation::~VRDisplayPresentation()
|
|
|
|
{
|
|
|
|
DestroyLayers();
|
|
|
|
mDisplayClient->PresentationDestroyed();
|
|
|
|
}
|
|
|
|
|
2016-10-01 02:43:24 +03:00
|
|
|
void VRDisplayPresentation::SubmitFrame()
|
2016-02-25 02:54:50 +03:00
|
|
|
{
|
|
|
|
for (VRLayerChild *layer : mLayers) {
|
2017-09-01 02:29:14 +03:00
|
|
|
layer->SubmitFrame(mDisplayClient->GetDisplayInfo().GetFrameId());
|
2016-02-25 02:54:50 +03:00
|
|
|
break; // Currently only one layer supported, submit only the first
|
|
|
|
}
|
|
|
|
}
|