svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include "config.h" |
| 27 | #include "VRDisplay.h" |
| 28 | |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 29 | #include "CanvasRenderingContext.h" |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 30 | #include "Chrome.h" |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 31 | #include "DOMException.h" |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 32 | #include "Page.h" |
| 33 | #include "ScriptedAnimationController.h" |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 34 | #include "UserGestureIndicator.h" |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 35 | #include "VRDisplayCapabilities.h" |
| 36 | #include "VREyeParameters.h" |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 37 | #include "VRFrameData.h" |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 38 | #include "VRLayerInit.h" |
svillar@igalia.com | 81ed840 | 2018-02-19 13:16:13 +0000 | [diff] [blame] | 39 | #include "VRPlatformDisplay.h" |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 40 | #include "VRPose.h" |
svillar@igalia.com | d08c235 | 2018-02-21 10:47:14 +0000 | [diff] [blame] | 41 | #include "VRStageParameters.h" |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 42 | |
| 43 | namespace WebCore { |
| 44 | |
svillar@igalia.com | 81ed840 | 2018-02-19 13:16:13 +0000 | [diff] [blame] | 45 | Ref<VRDisplay> VRDisplay::create(ScriptExecutionContext& context, WeakPtr<VRPlatformDisplay>&& platformDisplay) |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 46 | { |
svillar@igalia.com | 81ed840 | 2018-02-19 13:16:13 +0000 | [diff] [blame] | 47 | auto display = adoptRef(*new VRDisplay(context, WTFMove(platformDisplay))); |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 48 | display->suspendIfNeeded(); |
| 49 | return display; |
| 50 | } |
| 51 | |
svillar@igalia.com | 81ed840 | 2018-02-19 13:16:13 +0000 | [diff] [blame] | 52 | VRDisplay::VRDisplay(ScriptExecutionContext& context, WeakPtr<VRPlatformDisplay>&& platformDisplay) |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 53 | : ActiveDOMObject(&context) |
svillar@igalia.com | 81ed840 | 2018-02-19 13:16:13 +0000 | [diff] [blame] | 54 | , m_display(WTFMove(platformDisplay)) |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 55 | { |
svillar@igalia.com | b52fff9 | 2018-02-20 15:52:01 +0000 | [diff] [blame] | 56 | auto displayInfo = m_display->getDisplayInfo(); |
svillar@igalia.com | 764247c | 2018-02-28 08:45:26 +0000 | [diff] [blame] | 57 | m_capabilities = VRDisplayCapabilities::create(displayInfo.capabilityFlags()); |
| 58 | m_leftEyeParameters = VREyeParameters::create(displayInfo.eyeTranslation(VRPlatformDisplayInfo::EyeLeft), displayInfo.eyeFieldOfView(VRPlatformDisplayInfo::EyeLeft), displayInfo.renderSize()); |
| 59 | m_rightEyeParameters = VREyeParameters::create(displayInfo.eyeTranslation(VRPlatformDisplayInfo::EyeRight), displayInfo.eyeFieldOfView(VRPlatformDisplayInfo::EyeRight), displayInfo.renderSize()); |
| 60 | m_displayId = displayInfo.displayIdentifier(); |
| 61 | m_displayName = displayInfo.displayName(); |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | VRDisplay::~VRDisplay() = default; |
| 65 | |
| 66 | bool VRDisplay::isConnected() const |
| 67 | { |
svillar@igalia.com | 81ed840 | 2018-02-19 13:16:13 +0000 | [diff] [blame] | 68 | if (!m_display) |
| 69 | return false; |
| 70 | |
svillar@igalia.com | 764247c | 2018-02-28 08:45:26 +0000 | [diff] [blame] | 71 | return m_display->getDisplayInfo().isConnected(); |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 72 | } |
| 73 | |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 74 | const VRDisplayCapabilities& VRDisplay::capabilities() const |
| 75 | { |
svillar@igalia.com | b52fff9 | 2018-02-20 15:52:01 +0000 | [diff] [blame] | 76 | return *m_capabilities; |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 77 | } |
| 78 | |
svillar@igalia.com | d08c235 | 2018-02-21 10:47:14 +0000 | [diff] [blame] | 79 | RefPtr<VRStageParameters> VRDisplay::stageParameters() const |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 80 | { |
svillar@igalia.com | d08c235 | 2018-02-21 10:47:14 +0000 | [diff] [blame] | 81 | auto displayInfo = m_display->getDisplayInfo(); |
svillar@igalia.com | 764247c | 2018-02-28 08:45:26 +0000 | [diff] [blame] | 82 | return VRStageParameters::create(displayInfo.sittingToStandingTransform(), displayInfo.playAreaBounds()); |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 83 | } |
| 84 | |
svillar@igalia.com | b52fff9 | 2018-02-20 15:52:01 +0000 | [diff] [blame] | 85 | const VREyeParameters& VRDisplay::getEyeParameters(VREye eye) const |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 86 | { |
svillar@igalia.com | b52fff9 | 2018-02-20 15:52:01 +0000 | [diff] [blame] | 87 | return eye == VREye::Left ? *m_leftEyeParameters : *m_rightEyeParameters; |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 88 | } |
| 89 | |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 90 | bool VRDisplay::getFrameData(VRFrameData& frameData) const |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 91 | { |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 92 | if (!m_capabilities->hasPosition() || !m_capabilities->hasOrientation()) |
| 93 | return false; |
| 94 | |
| 95 | // FIXME: ensure that this is only called inside WebVR's rAF. |
| 96 | frameData.update(m_display->getTrackingInfo(), getEyeParameters(VREye::Left), getEyeParameters(VREye::Right), m_depthNear, m_depthFar); |
| 97 | return true; |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | Ref<VRPose> VRDisplay::getPose() const |
| 101 | { |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 102 | return VRPose::create(m_display->getTrackingInfo()); |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void VRDisplay::resetPose() |
| 106 | { |
| 107 | } |
| 108 | |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 109 | uint32_t VRDisplay::requestAnimationFrame(Ref<RequestAnimationFrameCallback>&& callback) |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 110 | { |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 111 | if (!m_scriptedAnimationController) { |
| 112 | auto* document = downcast<Document>(scriptExecutionContext()); |
graouts@webkit.org | 32e70bb | 2018-06-25 09:54:34 +0000 | [diff] [blame^] | 113 | m_scriptedAnimationController = ScriptedAnimationController::create(*document); |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return m_scriptedAnimationController->registerCallback(WTFMove(callback)); |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 117 | } |
| 118 | |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 119 | void VRDisplay::cancelAnimationFrame(uint32_t id) |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 120 | { |
svillar@igalia.com | 2bd2a9f | 2018-04-09 14:40:51 +0000 | [diff] [blame] | 121 | if (!m_scriptedAnimationController) |
| 122 | return; |
| 123 | |
| 124 | m_scriptedAnimationController->cancelCallback(id); |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 125 | } |
| 126 | |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 127 | void VRDisplay::requestPresent(const Vector<VRLayerInit>& layers, Ref<DeferredPromise>&& promise) |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 128 | { |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 129 | auto rejectRequestAndStopPresenting = [this, &promise] (ExceptionCode exceptionCode, ASCIILiteral message) { |
| 130 | promise->reject(Exception { exceptionCode, message }); |
| 131 | if (m_presentingLayer) |
| 132 | stopPresenting(); |
| 133 | }; |
| 134 | |
| 135 | if (!m_capabilities->canPresent()) { |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 136 | rejectRequestAndStopPresenting(NotSupportedError, "VRDisplay cannot present"_s); |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
| 140 | if (!layers.size() || layers.size() > m_capabilities->maxLayers()) { |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 141 | rejectRequestAndStopPresenting(InvalidStateError, layers.size() ? "Too many layers"_s : "Not enough layers"_s); |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | |
| 145 | if (!m_presentingLayer && !UserGestureIndicator::processingUserGesture()) { |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 146 | rejectRequestAndStopPresenting(InvalidAccessError, "Must request presentation from a user gesture handler."_s); |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
| 150 | RELEASE_ASSERT(layers.size() == 1); |
| 151 | auto layer = layers[0]; |
| 152 | |
| 153 | if (!layer.source) { |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 154 | rejectRequestAndStopPresenting(InvalidStateError, "Layer does not contain any source"_s); |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
| 158 | auto* canvasContext = layer.source->getContext("webgl"); |
| 159 | if (!canvasContext || !canvasContext->isWebGL()) { |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 160 | rejectRequestAndStopPresenting(NotSupportedError, "WebVR requires VRLayerInit with WebGL context."_s); |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
| 164 | if ((layer.leftBounds.size() && layer.leftBounds.size() != 4) |
| 165 | || (layer.rightBounds.size() && layer.rightBounds.size() != 4)) { |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 166 | rejectRequestAndStopPresenting(InvalidStateError, "Layer bounds must be either 0 or 4"_s); |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | |
| 170 | m_presentingLayer = layer; |
| 171 | promise->resolve(); |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 172 | } |
| 173 | |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 174 | void VRDisplay::stopPresenting() |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 175 | { |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 176 | m_presentingLayer = std::nullopt; |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 177 | } |
| 178 | |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 179 | void VRDisplay::exitPresent(Ref<DeferredPromise>&& promise) |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 180 | { |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 181 | if (!m_presentingLayer) { |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 182 | promise->reject(Exception { InvalidStateError, "VRDisplay is not presenting"_s }); |
svillar@igalia.com | ebd9831 | 2018-04-13 15:19:00 +0000 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | |
| 186 | stopPresenting(); |
| 187 | } |
| 188 | |
| 189 | Vector<VRLayerInit> VRDisplay::getLayers() const |
| 190 | { |
| 191 | Vector<VRLayerInit> layers; |
| 192 | if (m_presentingLayer) |
| 193 | layers.append(m_presentingLayer.value()); |
| 194 | return layers; |
svillar@igalia.com | 29e449b | 2017-09-13 11:17:27 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void VRDisplay::submitFrame() |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | bool VRDisplay::hasPendingActivity() const |
| 202 | { |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | const char* VRDisplay::activeDOMObjectName() const |
| 207 | { |
| 208 | return "VRDisplay"; |
| 209 | } |
| 210 | |
| 211 | bool VRDisplay::canSuspendForDocumentSuspension() const |
| 212 | { |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | void VRDisplay::stop() |
| 217 | { |
| 218 | } |
| 219 | |
| 220 | } // namespace WebCore |