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"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2020-04-07 21:48:13 +03:00
|
|
|
#include "mozilla/dom/XRWebGLLayer.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)
|
2020-05-06 14:55:46 +03:00
|
|
|
: mDisplayClient(aDisplayClient),
|
|
|
|
mDOMLayers(aLayers.Clone()),
|
|
|
|
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) {
|
2020-05-06 14:55:46 +03:00
|
|
|
mDOMLayers = aLayers.Clone();
|
2017-08-29 02:20:59 +03:00
|
|
|
CreateLayers();
|
|
|
|
}
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
void VRDisplayPresentation::UpdateXRWebGLLayer(dom::XRWebGLLayer* aLayer) {
|
|
|
|
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!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::HTMLCanvasElement* canvasElement = aLayer->GetCanvas();
|
2020-06-22 12:38:34 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> target =
|
2020-04-07 21:48:13 +03:00
|
|
|
canvasElement->OwnerDoc()->EventTargetFor(TaskCategory::Other);
|
|
|
|
|
2020-04-16 00:59:44 +03:00
|
|
|
if (mLayers.Length() == 0) {
|
|
|
|
// WebXR uses a single layer for now.
|
|
|
|
RefPtr<VRLayerChild> vrLayer =
|
|
|
|
static_cast<VRLayerChild*>(manager->CreateVRLayer(
|
|
|
|
mDisplayClient->GetDisplayInfo().GetDisplayID(), target, mGroup));
|
|
|
|
mLayers.AppendElement(vrLayer);
|
|
|
|
}
|
|
|
|
RefPtr<VRLayerChild> vrLayer = mLayers[0];
|
2020-04-07 21:48:13 +03:00
|
|
|
|
|
|
|
Rect leftBounds(0.0, 0.0, 0.5, 1.0);
|
|
|
|
Rect rightBounds(0.5, 0.0, 0.5, 1.0);
|
|
|
|
|
|
|
|
vrLayer->Initialize(canvasElement, leftBounds, rightBounds);
|
|
|
|
vrLayer->SetXRFramebuffer(aLayer->GetFramebuffer());
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-12-19 23:48:39 +03:00
|
|
|
leftBounds.SetRect(layer.mLeftBounds[0], layer.mLeftBounds[1],
|
|
|
|
layer.mLeftBounds[2], 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) {
|
2017-12-19 23:48:39 +03:00
|
|
|
rightBounds.SetRect(layer.mRightBounds[0], layer.mRightBounds[1],
|
|
|
|
layer.mRightBounds[2], 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;
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:38:34 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> target =
|
2019-01-02 16:05:23 +03:00
|
|
|
canvasElement->OwnerDoc()->EventTargetFor(TaskCategory::Other);
|
2017-03-23 09:19:11 +03:00
|
|
|
|
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) {
|
2020-05-06 14:55:46 +03:00
|
|
|
result = mDOMLayers.Clone();
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VRDisplayPresentation::~VRDisplayPresentation() {
|
|
|
|
DestroyLayers();
|
|
|
|
mDisplayClient->PresentationDestroyed();
|
|
|
|
}
|
|
|
|
|
2016-10-01 02:43:24 +03:00
|
|
|
void VRDisplayPresentation::SubmitFrame() {
|
2021-09-24 22:14:46 +03:00
|
|
|
// Currently only one layer supported, submit only the first
|
|
|
|
if (mLayers.Length() >= 1) {
|
|
|
|
VRLayerChild* layer = mLayers.ElementAt(0);
|
2018-08-24 18:32:12 +03:00
|
|
|
layer->SubmitFrame(mDisplayClient->GetDisplayInfo());
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
|
|
|
}
|