Bug 1567650 [wpt PR 17950] - [ElementTiming] Replace responseEnd with loadTime, a...
[gecko.git] / gfx / vr / VRDisplayPresentation.cpp
blob36791684d1555bdcf2a523b7dad6a4f2df4767e7
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) {
21 CreateLayers();
24 void VRDisplayPresentation::UpdateLayers(
25 const nsTArray<mozilla::dom::VRLayer>& aLayers) {
26 mDOMLayers = aLayers;
27 CreateLayers();
30 uint32_t VRDisplayPresentation::GetGroup() const { return mGroup; }
32 void VRDisplayPresentation::CreateLayers() {
33 VRManagerChild* manager = VRManagerChild::Get();
34 if (!manager) {
35 // This should not happen, but let's log it and avoid a crash in case
36 // of regression.
37 NS_WARNING("VRManagerChild::Get returned null!");
38 return;
41 unsigned int iLayer = 0;
42 for (dom::VRLayer& layer : mDOMLayers) {
43 dom::HTMLCanvasElement* canvasElement = layer.mSource;
44 if (!canvasElement) {
45 /// XXX In the future we will support WebVR in WebWorkers here
46 continue;
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) {
54 /**
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
59 continue;
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) {
67 /**
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
72 continue;
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));
83 if (!vrLayer) {
84 NS_WARNING("CreateVRLayer returned null!");
85 continue;
87 vrLayer->Initialize(canvasElement, leftBounds, rightBounds);
88 mLayers.AppendElement(vrLayer);
89 } else {
90 // We already have a layer, let's update it
91 mLayers[iLayer]->Initialize(canvasElement, leftBounds, rightBounds);
93 iLayer++;
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();
106 mLayers.Clear();
109 void VRDisplayPresentation::GetDOMLayers(nsTArray<dom::VRLayer>& result) {
110 result = mDOMLayers;
113 VRDisplayPresentation::~VRDisplayPresentation() {
114 DestroyLayers();
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