2020-04-06 22:33:32 +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
|
|
|
|
|
* 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/. */
|
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
#include "mozilla/dom/XRWebGLLayer.h"
|
2020-04-06 22:33:32 +03:00
|
|
|
|
#include "mozilla/dom/XRSession.h"
|
|
|
|
|
#include "mozilla/dom/XRView.h"
|
|
|
|
|
#include "mozilla/dom/XRViewport.h"
|
2020-04-07 21:48:13 +03:00
|
|
|
|
#include "mozilla/dom/WebGLRenderingContextBinding.h"
|
|
|
|
|
#include "WebGLFramebuffer.h"
|
2020-04-06 22:33:32 +03:00
|
|
|
|
#include "mozilla/StaticPrefs_dom.h"
|
|
|
|
|
#include "mozilla/StaticPrefs_webgl.h"
|
|
|
|
|
#include "GLContext.h"
|
|
|
|
|
#include "ScopedGLHelpers.h"
|
|
|
|
|
#include "MozFramebuffer.h"
|
|
|
|
|
#include "VRDisplayClient.h"
|
|
|
|
|
#include "ClientWebGLContext.h"
|
2020-04-07 21:48:13 +03:00
|
|
|
|
#include "nsIScriptError.h"
|
2020-04-06 22:33:32 +03:00
|
|
|
|
|
|
|
|
|
using namespace mozilla::gl;
|
|
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(XRWebGLLayer, mParent, mSession, mWebGL,
|
2020-05-01 04:42:39 +03:00
|
|
|
|
mFramebuffer, mLeftViewport,
|
|
|
|
|
mRightViewport)
|
2020-04-06 22:33:32 +03:00
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(XRWebGLLayer, AddRef)
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(XRWebGLLayer, Release)
|
|
|
|
|
|
|
|
|
|
XRWebGLLayer::XRWebGLLayer(
|
2020-04-07 21:48:13 +03:00
|
|
|
|
nsISupports* aParent, XRSession& aSession, bool aIgnoreDepthValues,
|
|
|
|
|
double aFramebufferScaleFactor,
|
|
|
|
|
RefPtr<mozilla::ClientWebGLContext> aWebGLContext,
|
|
|
|
|
RefPtr<WebGLFramebufferJS> aFramebuffer,
|
|
|
|
|
const Maybe<const webgl::OpaqueFramebufferOptions>& aOptions)
|
|
|
|
|
: mParent(aParent),
|
|
|
|
|
mSession(&aSession),
|
|
|
|
|
mWebGL(std::move(aWebGLContext)),
|
|
|
|
|
mFramebufferScaleFactor(aFramebufferScaleFactor),
|
2020-04-06 22:33:32 +03:00
|
|
|
|
mCompositionDisabled(!aSession.IsImmersive()),
|
2020-04-07 21:48:13 +03:00
|
|
|
|
mIgnoreDepthValues(aIgnoreDepthValues),
|
|
|
|
|
mFramebuffer(std::move(aFramebuffer)),
|
|
|
|
|
mFramebufferOptions(aOptions) {
|
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
|
}
|
2020-04-06 22:33:32 +03:00
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
XRWebGLLayer::~XRWebGLLayer() {
|
2020-04-16 00:58:13 +03:00
|
|
|
|
DeleteFramebuffer();
|
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void XRWebGLLayer::DeleteFramebuffer() {
|
2020-04-07 21:48:13 +03:00
|
|
|
|
if (mFramebuffer) {
|
|
|
|
|
mWebGL->DeleteFramebuffer(mFramebuffer.get(), true);
|
2020-04-16 00:58:13 +03:00
|
|
|
|
mFramebuffer = nullptr;
|
2020-04-06 22:33:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
/* static */
|
|
|
|
|
already_AddRefed<XRWebGLLayer> XRWebGLLayer::Constructor(
|
2020-04-06 22:33:32 +03:00
|
|
|
|
const GlobalObject& aGlobal, XRSession& aSession,
|
|
|
|
|
const WebGLRenderingContextOrWebGL2RenderingContext& aXRWebGLContext,
|
|
|
|
|
const XRWebGLLayerInit& aXRWebGLLayerInitDict, ErrorResult& aRv) {
|
|
|
|
|
// https://immersive-web.github.io/webxr/#dom-xrwebgllayer-xrwebgllayer
|
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
// Depth not supported in XR Compositor yet.
|
|
|
|
|
const bool ignoreDepthValues = true;
|
|
|
|
|
|
2020-04-06 22:33:32 +03:00
|
|
|
|
// If session’s ended value is true, throw an InvalidStateError and abort
|
|
|
|
|
// these steps.
|
|
|
|
|
if (aSession.IsEnded()) {
|
|
|
|
|
aRv.ThrowInvalidStateError(
|
|
|
|
|
"Can not create an XRWebGLLayer with an XRSession that has ended.");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-04-07 21:48:13 +03:00
|
|
|
|
gfx::VRDisplayClient* display = aSession.GetDisplayClient();
|
|
|
|
|
const gfx::VRDisplayInfo& displayInfo = display->GetDisplayInfo();
|
|
|
|
|
const gfx::VRDisplayState& displayState = displayInfo.mDisplayState;
|
2020-04-06 22:33:32 +03:00
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
RefPtr<ClientWebGLContext> gl;
|
2020-04-06 22:33:32 +03:00
|
|
|
|
if (aXRWebGLContext.IsWebGLRenderingContext()) {
|
|
|
|
|
gl = &aXRWebGLContext.GetAsWebGLRenderingContext();
|
|
|
|
|
} else {
|
|
|
|
|
gl = &aXRWebGLContext.GetAsWebGL2RenderingContext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If context is lost, throw an InvalidStateError and abort these steps.
|
|
|
|
|
if (gl->IsContextLost()) {
|
|
|
|
|
aRv.ThrowInvalidStateError(
|
|
|
|
|
"Could not create an XRWebGLLayer, as the WebGL context was lost.");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
RefPtr<mozilla::WebGLFramebufferJS> framebuffer;
|
|
|
|
|
Maybe<const webgl::OpaqueFramebufferOptions> framebufferOptions;
|
|
|
|
|
if (aSession.IsImmersive()) {
|
|
|
|
|
// If session is an immersive session and context’s XR compatible boolean
|
|
|
|
|
// is false, throw an InvalidStateError and abort these steps.
|
|
|
|
|
if (!gl->IsXRCompatible()) {
|
|
|
|
|
aRv.ThrowInvalidStateError(
|
|
|
|
|
"Can not create an XRWebGLLayer without first calling "
|
|
|
|
|
"makeXRCompatible "
|
|
|
|
|
"on the WebGLRenderingContext or WebGL2RenderingContext.");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-04-06 22:33:32 +03:00
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
const auto document = gl->GetParentObject()->OwnerDoc();
|
|
|
|
|
if (aXRWebGLLayerInitDict.mAlpha) {
|
|
|
|
|
nsContentUtils::ReportToConsoleNonLocalized(
|
2020-07-01 11:29:29 +03:00
|
|
|
|
u"XRWebGLLayer doesn't support no alpha value. "
|
|
|
|
|
"Alpha will be enabled."_ns,
|
|
|
|
|
nsIScriptError::warningFlag, "DOM"_ns, document);
|
2020-04-07 21:48:13 +03:00
|
|
|
|
}
|
|
|
|
|
if (aXRWebGLLayerInitDict.mDepth != aXRWebGLLayerInitDict.mStencil) {
|
|
|
|
|
nsContentUtils::ReportToConsoleNonLocalized(
|
2020-07-01 11:29:29 +03:00
|
|
|
|
nsLiteralString(
|
|
|
|
|
u"XRWebGLLayer doesn't support separate "
|
2020-04-07 21:48:13 +03:00
|
|
|
|
"depth or stencil buffers. They will be enabled together."),
|
2020-07-01 11:29:29 +03:00
|
|
|
|
nsIScriptError::warningFlag, "DOM"_ns, document);
|
2020-04-07 21:48:13 +03:00
|
|
|
|
}
|
2020-04-06 22:33:32 +03:00
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
bool antialias = aXRWebGLLayerInitDict.mAntialias;
|
2020-04-27 21:03:03 +03:00
|
|
|
|
if (antialias && !StaticPrefs::webgl_msaa_force()) {
|
2020-04-07 21:48:13 +03:00
|
|
|
|
antialias = false;
|
|
|
|
|
nsContentUtils::ReportToConsoleNonLocalized(
|
2020-07-01 11:29:29 +03:00
|
|
|
|
u"XRWebGLLayer antialiasing is not supported."
|
|
|
|
|
"Antialiasing will be disabled."_ns,
|
|
|
|
|
nsIScriptError::warningFlag, "DOM"_ns, document);
|
2020-04-07 21:48:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
webgl::OpaqueFramebufferOptions options;
|
|
|
|
|
options.antialias = antialias;
|
|
|
|
|
options.depthStencil =
|
|
|
|
|
aXRWebGLLayerInitDict.mDepth || aXRWebGLLayerInitDict.mStencil;
|
2020-04-06 22:33:32 +03:00
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
const float scaleFactor =
|
|
|
|
|
fmin(aXRWebGLLayerInitDict.mFramebufferScaleFactor, 1.0f);
|
|
|
|
|
|
|
|
|
|
options.width =
|
|
|
|
|
(int32_t)(2.0f * displayState.eyeResolution.width * scaleFactor);
|
|
|
|
|
options.height = (int32_t)(displayState.eyeResolution.height * scaleFactor);
|
|
|
|
|
framebuffer = gl->CreateOpaqueFramebuffer(options);
|
|
|
|
|
|
|
|
|
|
if (!framebuffer) {
|
|
|
|
|
aRv.ThrowOperationError(
|
|
|
|
|
"Could not create an XRWebGLLayer. XRFramebuffer creation failed.");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
framebufferOptions.emplace(options);
|
2020-04-06 22:33:32 +03:00
|
|
|
|
}
|
2020-04-07 21:48:13 +03:00
|
|
|
|
|
2020-04-06 22:33:32 +03:00
|
|
|
|
RefPtr<XRWebGLLayer> obj =
|
2020-04-07 21:48:13 +03:00
|
|
|
|
new XRWebGLLayer(aGlobal.GetAsSupports(), aSession, ignoreDepthValues,
|
|
|
|
|
aXRWebGLLayerInitDict.mFramebufferScaleFactor, gl,
|
|
|
|
|
framebuffer, framebufferOptions);
|
2020-04-06 22:33:32 +03:00
|
|
|
|
return obj.forget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSObject* XRWebGLLayer::WrapObject(JSContext* aCx,
|
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
|
return XRWebGLLayer_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsISupports* XRWebGLLayer::GetParentObject() const { return mParent; }
|
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
bool XRWebGLLayer::Antialias() {
|
|
|
|
|
if (mFramebufferOptions) {
|
|
|
|
|
return mFramebufferOptions->antialias;
|
|
|
|
|
}
|
|
|
|
|
return mWebGL->ActualContextParameters().antialias;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool XRWebGLLayer::Depth() {
|
|
|
|
|
if (mFramebufferOptions) {
|
|
|
|
|
return mFramebufferOptions->depthStencil;
|
|
|
|
|
}
|
|
|
|
|
return mWebGL->ActualContextParameters().depth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool XRWebGLLayer::Stencil() {
|
|
|
|
|
if (mFramebufferOptions) {
|
|
|
|
|
return mFramebufferOptions->depthStencil;
|
|
|
|
|
}
|
|
|
|
|
return mWebGL->ActualContextParameters().stencil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool XRWebGLLayer::Alpha() {
|
|
|
|
|
if (mFramebufferOptions) {
|
|
|
|
|
// Alpha is always true when using Opaque Framebuffers.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return mWebGL->ActualContextParameters().alpha;
|
|
|
|
|
}
|
2020-04-06 22:33:32 +03:00
|
|
|
|
|
|
|
|
|
bool XRWebGLLayer::IgnoreDepthValues() { return mIgnoreDepthValues; }
|
|
|
|
|
|
|
|
|
|
WebGLFramebufferJS* XRWebGLLayer::GetFramebuffer() {
|
2020-04-07 21:48:13 +03:00
|
|
|
|
return mFramebuffer.get();
|
2020-04-06 22:33:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
uint32_t XRWebGLLayer::FramebufferWidth() {
|
|
|
|
|
if (mFramebufferOptions) {
|
|
|
|
|
return mFramebufferOptions->width;
|
|
|
|
|
}
|
|
|
|
|
return mWebGL->GetWidth();
|
|
|
|
|
}
|
2020-04-06 22:33:32 +03:00
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
uint32_t XRWebGLLayer::FramebufferHeight() {
|
|
|
|
|
if (mFramebufferOptions) {
|
|
|
|
|
return mFramebufferOptions->height;
|
|
|
|
|
}
|
|
|
|
|
return mWebGL->GetHeight();
|
|
|
|
|
}
|
2020-04-06 22:33:32 +03:00
|
|
|
|
|
|
|
|
|
already_AddRefed<XRViewport> XRWebGLLayer::GetViewport(const XRView& aView) {
|
2020-04-07 21:48:13 +03:00
|
|
|
|
const int32_t width = (aView.Eye() == XREye::None) ? FramebufferWidth()
|
|
|
|
|
: (FramebufferWidth() / 2);
|
2020-05-01 04:42:39 +03:00
|
|
|
|
gfx::IntRect rect(0, 0, width, FramebufferHeight());
|
2020-04-06 22:33:32 +03:00
|
|
|
|
if (aView.Eye() == XREye::Right) {
|
2020-05-01 04:42:39 +03:00
|
|
|
|
rect.x = width;
|
2020-04-06 22:33:32 +03:00
|
|
|
|
}
|
2020-05-01 04:42:39 +03:00
|
|
|
|
RefPtr<XRViewport>& viewport =
|
|
|
|
|
aView.Eye() == XREye::Right ? mRightViewport : mLeftViewport;
|
|
|
|
|
if (!viewport) {
|
|
|
|
|
viewport = new XRViewport(mParent, rect);
|
|
|
|
|
} else {
|
|
|
|
|
viewport->mRect = rect;
|
|
|
|
|
}
|
|
|
|
|
RefPtr<XRViewport> result = viewport;
|
|
|
|
|
return result.forget();
|
2020-04-06 22:33:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* static */ double XRWebGLLayer::GetNativeFramebufferScaleFactor(
|
|
|
|
|
const GlobalObject& aGlobal, const XRSession& aSession) {
|
|
|
|
|
if (aSession.IsEnded()) {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
2020-04-07 21:48:13 +03:00
|
|
|
|
// TODO: Get the maximum framebuffer size from each display.
|
|
|
|
|
// Right now we assume that the recommended size is the maximum one.
|
2020-04-06 22:33:32 +03:00
|
|
|
|
return 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 21:48:13 +03:00
|
|
|
|
void XRWebGLLayer::StartAnimationFrame() {
|
|
|
|
|
if (mFramebuffer) {
|
|
|
|
|
mWebGL->SetFramebufferIsInOpaqueRAF(mFramebuffer.get(), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void XRWebGLLayer::EndAnimationFrame() {
|
|
|
|
|
if (mFramebuffer) {
|
|
|
|
|
mWebGL->SetFramebufferIsInOpaqueRAF(mFramebuffer.get(), false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HTMLCanvasElement* XRWebGLLayer::GetCanvas() {
|
|
|
|
|
return mWebGL->GetParentObject();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 17:37:23 +03:00
|
|
|
|
void XRWebGLLayer::SessionEnded() { DeleteFramebuffer(); }
|
2020-04-16 00:58:13 +03:00
|
|
|
|
|
2020-04-06 22:33:32 +03:00
|
|
|
|
} // namespace dom
|
|
|
|
|
} // namespace mozilla
|