1/*
2 * Copyright (C) 2017-2018 Igalia S.L. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "ActiveDOMObject.h"
29#include "EventTarget.h"
30#include "JSDOMPromiseDeferred.h"
31#include "VREye.h"
32#include "VRLayerInit.h"
33#include "VRPlatformDisplayClient.h"
34#include <wtf/RefCounted.h>
35
36namespace WebCore {
37
38enum ExceptionCode;
39class RequestAnimationFrameCallback;
40class ScriptedAnimationController;
41class VRDisplayCapabilities;
42class VREyeParameters;
43class VRFrameData;
44class VRPlatformDisplay;
45class VRPose;
46class VRStageParameters;
47
48class VRDisplay final : public RefCounted<VRDisplay>, public VRPlatformDisplayClient, public EventTargetWithInlineData, public ActiveDOMObject {
49 WTF_MAKE_ISO_ALLOCATED(VRDisplay);
50public:
51 static Ref<VRDisplay> create(ScriptExecutionContext&, WeakPtr<VRPlatformDisplay>&&);
52
53 virtual ~VRDisplay();
54
55 using RefCounted<VRDisplay>::ref;
56 using RefCounted<VRDisplay>::deref;
57
58 bool isConnected() const;
59 bool isPresenting() const { return !!m_presentingLayer; };
60
61 const VRDisplayCapabilities& capabilities() const;
62 RefPtr<VRStageParameters> stageParameters() const;
63
64 const VREyeParameters& getEyeParameters(VREye) const;
65
66 const String& displayName() const { return m_displayName; }
67 uint32_t displayId() const { return m_displayId; }
68
69 bool getFrameData(VRFrameData&) const;
70
71 Ref<VRPose> getPose() const;
72 void resetPose();
73
74 double depthNear() const { return m_depthNear; }
75 void setDepthNear(double depthNear) { m_depthNear = depthNear; }
76 double depthFar() const { return m_depthFar; }
77 void setDepthFar(double depthFar) { m_depthFar = depthFar; }
78
79 uint32_t requestAnimationFrame(Ref<RequestAnimationFrameCallback>&&);
80 void cancelAnimationFrame(uint32_t);
81
82 void requestPresent(const Vector<VRLayerInit>&, Ref<DeferredPromise>&&);
83 void exitPresent(Ref<DeferredPromise>&&);
84
85 Vector<VRLayerInit> getLayers() const;
86
87 void submitFrame();
88
89 // VRPlatformDisplayClient
90 void platformDisplayConnected() override;
91 void platformDisplayDisconnected() override;
92 void platformDisplayMounted() override;
93 void platformDisplayUnmounted() override;
94
95private:
96 VRDisplay(ScriptExecutionContext&, WeakPtr<VRPlatformDisplay>&&);
97
98 // EventTarget
99 EventTargetInterface eventTargetInterface() const override { return VRDisplayEventTargetInterfaceType; }
100 ScriptExecutionContext* scriptExecutionContext() const override { return ActiveDOMObject::scriptExecutionContext(); }
101 void refEventTarget() override { ref(); }
102 void derefEventTarget() override { deref(); }
103
104 // ActiveDOMObject
105 bool hasPendingActivity() const override;
106 const char* activeDOMObjectName() const override;
107 bool canSuspendForDocumentSuspension() const override;
108 void stop() override;
109
110 void stopPresenting();
111
112 Document* document() { return downcast<Document>(scriptExecutionContext()); }
113
114 WeakPtr<VRPlatformDisplay> m_display;
115
116 RefPtr<VRDisplayCapabilities> m_capabilities;
117 // We could likely store just one of the two following ones as the values should be identical
118 // (except the sign of the eye to head transform offset).
119 RefPtr<VREyeParameters> m_leftEyeParameters;
120 RefPtr<VREyeParameters> m_rightEyeParameters;
121 RefPtr<VRStageParameters> m_stageParameters;
122
123 String m_displayName;
124 uint32_t m_displayId;
125
126 double m_depthNear { 0.01 }; // Default value from the specs.
127 double m_depthFar { 10000 }; // Default value from the specs.
128
129 RefPtr<ScriptedAnimationController> m_scriptedAnimationController;
130
131 Optional<VRLayerInit> m_presentingLayer;
132};
133
134} // namespace WebCore
135