1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "VRDisplayPresentation.h"
9 #include "mozilla/dom/DocGroup.h"
10 #include "mozilla/Unused.h"
11 #include "VRDisplayClient.h"
12 #include "VRLayerChild.h"
14 using namespace mozilla
;
15 using namespace mozilla::gfx
;
17 VRDisplayPresentation::VRDisplayPresentation(
18 VRDisplayClient
* aDisplayClient
,
19 const nsTArray
<mozilla::dom::VRLayer
>& aLayers
, uint32_t aGroup
)
20 : mDisplayClient(aDisplayClient
), mDOMLayers(aLayers
), mGroup(aGroup
) {
24 void VRDisplayPresentation::UpdateLayers(
25 const nsTArray
<mozilla::dom::VRLayer
>& aLayers
) {
30 uint32_t VRDisplayPresentation::GetGroup() const { return mGroup
; }
32 void VRDisplayPresentation::CreateLayers() {
33 VRManagerChild
* manager
= VRManagerChild::Get();
35 // This should not happen, but let's log it and avoid a crash in case
37 NS_WARNING("VRManagerChild::Get returned null!");
41 unsigned int iLayer
= 0;
42 for (dom::VRLayer
& layer
: mDOMLayers
) {
43 dom::HTMLCanvasElement
* canvasElement
= layer
.mSource
;
45 /// XXX In the future we will support WebVR in WebWorkers here
49 Rect
leftBounds(0.0, 0.0, 0.5, 1.0);
50 if (layer
.mLeftBounds
.Length() == 4) {
51 leftBounds
.SetRect(layer
.mLeftBounds
[0], layer
.mLeftBounds
[1],
52 layer
.mLeftBounds
[2], layer
.mLeftBounds
[3]);
53 } else if (layer
.mLeftBounds
.Length() != 0) {
55 * We ignore layers with an incorrect number of values.
56 * In the future, VRDisplay.requestPresent may throw in
57 * this case. See https://github.com/w3c/webvr/issues/71
62 Rect
rightBounds(0.5, 0.0, 0.5, 1.0);
63 if (layer
.mRightBounds
.Length() == 4) {
64 rightBounds
.SetRect(layer
.mRightBounds
[0], layer
.mRightBounds
[1],
65 layer
.mRightBounds
[2], layer
.mRightBounds
[3]);
66 } else if (layer
.mRightBounds
.Length() != 0) {
68 * We ignore layers with an incorrect number of values.
69 * In the future, VRDisplay.requestPresent may throw in
70 * this case. See https://github.com/w3c/webvr/issues/71
75 nsCOMPtr
<nsIEventTarget
> target
=
76 canvasElement
->OwnerDoc()->EventTargetFor(TaskCategory::Other
);
78 if (mLayers
.Length() <= iLayer
) {
79 // Not enough layers, let's add one
80 RefPtr
<VRLayerChild
> vrLayer
=
81 static_cast<VRLayerChild
*>(manager
->CreateVRLayer(
82 mDisplayClient
->GetDisplayInfo().GetDisplayID(), target
, mGroup
));
84 NS_WARNING("CreateVRLayer returned null!");
87 vrLayer
->Initialize(canvasElement
, leftBounds
, rightBounds
);
88 mLayers
.AppendElement(vrLayer
);
90 // We already have a layer, let's update it
91 mLayers
[iLayer
]->Initialize(canvasElement
, leftBounds
, rightBounds
);
96 // Truncate any excess layers that weren't included in the updated list
97 mLayers
.SetLength(iLayer
);
100 void VRDisplayPresentation::DestroyLayers() {
101 for (VRLayerChild
* layer
: mLayers
) {
102 if (layer
->IsIPCOpen()) {
103 Unused
<< layer
->SendDestroy();
109 void VRDisplayPresentation::GetDOMLayers(nsTArray
<dom::VRLayer
>& result
) {
113 VRDisplayPresentation::~VRDisplayPresentation() {
115 mDisplayClient
->PresentationDestroyed();
118 void VRDisplayPresentation::SubmitFrame() {
119 for (VRLayerChild
* layer
: mLayers
) {
120 layer
->SubmitFrame(mDisplayClient
->GetDisplayInfo());
121 break; // Currently only one layer supported, submit only the first