2009-09-03 04:47:49 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2009-09-03 04:47:49 +04:00
|
|
|
#include "WebGLContext.h"
|
2014-08-22 20:55:00 +04:00
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
#include <queue>
|
|
|
|
|
2012-10-17 23:11:51 +04:00
|
|
|
#include "AccessCheck.h"
|
2009-09-03 04:47:49 +04:00
|
|
|
#include "gfxContext.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "gfxCrashReporterUtils.h"
|
2009-09-03 04:47:49 +04:00
|
|
|
#include "gfxPattern.h"
|
2014-07-04 03:09:04 +04:00
|
|
|
#include "gfxPrefs.h"
|
2010-07-01 20:43:33 +04:00
|
|
|
#include "gfxUtils.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "GLBlitHelper.h"
|
2013-09-04 16:14:52 +04:00
|
|
|
#include "GLContext.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "GLContextProvider.h"
|
2014-01-02 19:17:29 +04:00
|
|
|
#include "GLReadTexImageHelper.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
#include "GLScreenBuffer.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "ImageContainer.h"
|
|
|
|
#include "ImageEncoder.h"
|
|
|
|
#include "Layers.h"
|
2015-10-22 23:04:35 +03:00
|
|
|
#include "LayerUserData.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "mozilla/dom/Event.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "mozilla/dom/HTMLVideoElement.h"
|
|
|
|
#include "mozilla/dom/ImageData.h"
|
2016-05-12 02:45:43 +03:00
|
|
|
#include "mozilla/dom/WebGLContextEvent.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "mozilla/EnumeratedArrayCycleCollection.h"
|
2011-07-07 06:00:02 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "mozilla/ProcessPriorityManager.h"
|
2016-07-22 20:26:05 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2012-06-06 11:40:02 +04:00
|
|
|
#include "mozilla/Services.h"
|
2011-09-20 00:08:56 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDisplayList.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
#include "nsIDOMEvent.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "nsIGfxInfo.h"
|
2012-04-22 00:48:22 +04:00
|
|
|
#include "nsIObserverService.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "nsIVariant.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsSVGEffects.h"
|
|
|
|
#include "prenv.h"
|
|
|
|
#include "ScopedGLHelpers.h"
|
2016-02-25 02:54:50 +03:00
|
|
|
#include "VRManagerChild.h"
|
|
|
|
#include "mozilla/layers/TextureClientSharedSurface.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "mozilla/layers/ShadowLayers.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Local
|
|
|
|
#include "CanvasUtils.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "WebGL1Context.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
#include "WebGLActiveInfo.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "WebGLBuffer.h"
|
|
|
|
#include "WebGLContextLossHandler.h"
|
|
|
|
#include "WebGLContextUtils.h"
|
|
|
|
#include "WebGLExtensions.h"
|
|
|
|
#include "WebGLFramebuffer.h"
|
|
|
|
#include "WebGLMemoryTracker.h"
|
|
|
|
#include "WebGLObjectModel.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
#include "WebGLProgram.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "WebGLQuery.h"
|
|
|
|
#include "WebGLSampler.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
#include "WebGLShader.h"
|
2016-06-23 12:01:36 +03:00
|
|
|
#include "WebGLSync.h"
|
2014-11-26 05:00:06 +03:00
|
|
|
#include "WebGLTransformFeedback.h"
|
2014-11-14 07:03:50 +03:00
|
|
|
#include "WebGLVertexArray.h"
|
|
|
|
#include "WebGLVertexAttribData.h"
|
2012-08-21 08:06:46 +04:00
|
|
|
|
2016-02-19 04:59:24 +03:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
|
|
|
#include "nsCocoaFeatures.h"
|
|
|
|
#endif
|
|
|
|
|
2016-06-21 06:38:47 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "WGLLibrary.h"
|
|
|
|
#endif
|
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
// Generated
|
|
|
|
#include "mozilla/dom/WebGLRenderingContextBinding.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace mozilla {
|
2013-05-27 18:12:13 +04:00
|
|
|
|
2012-10-04 01:13:05 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-02-14 03:26:24 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla::gl;
|
2010-09-02 13:18:40 +04:00
|
|
|
using namespace mozilla::layers;
|
2009-09-03 04:47:49 +04:00
|
|
|
|
2012-08-01 22:58:44 +04:00
|
|
|
WebGLContextOptions::WebGLContextOptions()
|
2014-11-14 07:03:50 +03:00
|
|
|
: alpha(true)
|
|
|
|
, depth(true)
|
|
|
|
, stencil(false)
|
|
|
|
, premultipliedAlpha(true)
|
|
|
|
, antialias(true)
|
|
|
|
, preserveDrawingBuffer(false)
|
2015-05-21 18:51:49 +03:00
|
|
|
, failIfMajorPerformanceCaveat(false)
|
2012-08-01 22:58:44 +04:00
|
|
|
{
|
|
|
|
// Set default alpha state based on preference.
|
2015-10-12 06:21:03 +03:00
|
|
|
if (gfxPrefs::WebGLDefaultNoAlpha())
|
2012-08-02 02:40:24 +04:00
|
|
|
alpha = false;
|
2012-08-01 22:58:44 +04:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
/*static*/ const uint32_t WebGLContext::kMinMaxColorAttachments = 4;
|
|
|
|
/*static*/ const uint32_t WebGLContext::kMinMaxDrawBuffers = 4;
|
|
|
|
|
2009-09-03 04:47:49 +04:00
|
|
|
WebGLContext::WebGLContext()
|
2014-10-27 02:40:37 +03:00
|
|
|
: WebGLContextUnchecked(nullptr)
|
2016-01-12 21:16:59 +03:00
|
|
|
, mBufferFetchingIsVerified(false)
|
|
|
|
, mBufferFetchingHasPerVertex(false)
|
|
|
|
, mMaxFetchedVertices(0)
|
|
|
|
, mMaxFetchedInstances(0)
|
2016-09-09 00:16:07 +03:00
|
|
|
, mLayerIsMirror(false)
|
2015-01-16 02:40:39 +03:00
|
|
|
, mBypassShaderValidation(false)
|
2016-12-29 11:08:39 +03:00
|
|
|
, mEmptyTFO(0)
|
2016-06-16 20:01:44 +03:00
|
|
|
, mContextLossHandler(this)
|
2014-10-02 04:05:34 +04:00
|
|
|
, mNeedsFakeNoAlpha(false)
|
2015-09-24 22:21:05 +03:00
|
|
|
, mNeedsFakeNoDepth(false)
|
2015-03-12 04:23:56 +03:00
|
|
|
, mNeedsFakeNoStencil(false)
|
2016-02-19 04:59:24 +03:00
|
|
|
, mNeedsEmulatedLoneDepthStencil(false)
|
2016-12-01 05:46:06 +03:00
|
|
|
, mAllowFBInvalidation(gfxPrefs::WebGLFBInvalidation())
|
2009-09-03 04:47:49 +04:00
|
|
|
{
|
2010-07-15 07:52:34 +04:00
|
|
|
mGeneration = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
mInvalidated = false;
|
2015-09-17 06:37:05 +03:00
|
|
|
mCapturedFrameInvalidated = false;
|
2013-02-14 03:26:24 +04:00
|
|
|
mShouldPresent = true;
|
2011-10-17 18:59:28 +04:00
|
|
|
mResetLayer = true;
|
|
|
|
mOptionsFrozen = false;
|
2016-01-12 21:16:59 +03:00
|
|
|
mMinCapability = false;
|
|
|
|
mDisableExtensions = false;
|
|
|
|
mIsMesa = false;
|
|
|
|
mEmitContextLostErrorOnce = false;
|
|
|
|
mWebGLError = 0;
|
|
|
|
mUnderlyingGLError = 0;
|
2010-07-15 07:52:34 +04:00
|
|
|
|
|
|
|
mActiveTexture = 0;
|
2010-09-02 18:34:08 +04:00
|
|
|
|
2016-01-12 21:16:59 +03:00
|
|
|
mStencilRefFront = 0;
|
|
|
|
mStencilRefBack = 0;
|
|
|
|
mStencilValueMaskFront = 0;
|
|
|
|
mStencilValueMaskBack = 0;
|
|
|
|
mStencilWriteMaskFront = 0;
|
|
|
|
mStencilWriteMaskBack = 0;
|
|
|
|
mDepthWriteMask = 0;
|
|
|
|
mStencilClearValue = 0;
|
|
|
|
mDepthClearValue = 0;
|
|
|
|
mContextLostErrorSet = false;
|
|
|
|
|
2014-01-10 17:41:04 +04:00
|
|
|
mViewportX = 0;
|
|
|
|
mViewportY = 0;
|
|
|
|
mViewportWidth = 0;
|
|
|
|
mViewportHeight = 0;
|
|
|
|
|
2011-05-20 23:53:53 +04:00
|
|
|
mDitherEnabled = 1;
|
2013-08-23 04:11:40 +04:00
|
|
|
mRasterizerDiscardEnabled = 0; // OpenGL ES 3.0 spec p244
|
2015-03-12 04:23:56 +03:00
|
|
|
mScissorTestEnabled = 0;
|
2015-09-24 22:21:05 +03:00
|
|
|
mDepthTestEnabled = 0;
|
2015-03-12 04:23:56 +03:00
|
|
|
mStencilTestEnabled = 0;
|
2013-05-22 11:05:38 +04:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
// XXX mtseng: bug 709490, not thread safe
|
|
|
|
WebGLMemoryTracker::AddWebGLContext(this);
|
|
|
|
}
|
2011-10-27 00:00:44 +04:00
|
|
|
|
2014-06-03 01:40:15 +04:00
|
|
|
mAllowContextRestore = true;
|
|
|
|
mLastLossWasSimulated = false;
|
2013-09-04 16:14:44 +04:00
|
|
|
mContextStatus = ContextNotLost;
|
2014-06-23 03:16:00 +04:00
|
|
|
mLoseContextOnMemoryPressure = false;
|
2013-01-04 03:39:25 +04:00
|
|
|
mCanLoseContextInForeground = true;
|
2014-06-23 03:16:00 +04:00
|
|
|
mRestoreWhenVisible = false;
|
2012-05-23 20:07:10 +04:00
|
|
|
|
2012-05-29 22:44:31 +04:00
|
|
|
mAlreadyGeneratedWarnings = 0;
|
2012-07-25 20:13:45 +04:00
|
|
|
mAlreadyWarnedAboutFakeVertexAttrib0 = false;
|
2014-01-10 17:41:04 +04:00
|
|
|
mAlreadyWarnedAboutViewportLargerThanDest = false;
|
2014-11-14 07:03:50 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
mMaxWarnings = gfxPrefs::WebGLMaxWarningsPerContext();
|
2014-11-14 07:03:50 +03:00
|
|
|
if (mMaxWarnings < -1) {
|
2013-06-04 23:44:08 +04:00
|
|
|
GenerateWarning("webgl.max-warnings-per-context size is too large (seems like a negative value wrapped)");
|
|
|
|
mMaxWarnings = 0;
|
|
|
|
}
|
2012-08-03 01:28:02 +04:00
|
|
|
|
|
|
|
mLastUseIndex = 0;
|
2012-11-19 19:49:51 +04:00
|
|
|
|
2013-08-14 02:11:01 +04:00
|
|
|
InvalidateBufferFetching();
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2013-05-16 01:50:52 +04:00
|
|
|
mDisableFragHighP = false;
|
2013-05-22 11:05:38 +04:00
|
|
|
|
|
|
|
mDrawCallsSinceLastFlush = 0;
|
2009-09-03 04:47:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
WebGLContext::~WebGLContext()
|
|
|
|
{
|
2014-09-10 05:40:51 +04:00
|
|
|
RemovePostRefreshObserver();
|
2014-06-23 03:16:00 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
DestroyResourcesAndContext();
|
2015-10-12 06:21:03 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
// XXX mtseng: bug 709490, not thread safe
|
|
|
|
WebGLMemoryTracker::RemoveWebGLContext(this);
|
|
|
|
}
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
2016-06-03 11:18:56 +03:00
|
|
|
template<typename T>
|
2016-11-30 05:30:28 +03:00
|
|
|
void
|
2016-06-03 11:18:56 +03:00
|
|
|
ClearLinkedList(LinkedList<T>& list)
|
|
|
|
{
|
|
|
|
while (!list.isEmpty()) {
|
|
|
|
list.getLast()->DeleteOnce();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
void
|
|
|
|
WebGLContext::DestroyResourcesAndContext()
|
|
|
|
{
|
|
|
|
if (!gl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
2011-12-04 23:15:42 +04:00
|
|
|
mBound2DTextures.Clear();
|
|
|
|
mBoundCubeMapTextures.Clear();
|
2014-10-14 03:42:20 +04:00
|
|
|
mBound3DTextures.Clear();
|
2015-11-25 07:15:29 +03:00
|
|
|
mBound2DArrayTextures.Clear();
|
2015-06-09 03:18:19 +03:00
|
|
|
mBoundSamplers.Clear();
|
2012-07-30 18:20:58 +04:00
|
|
|
mBoundArrayBuffer = nullptr;
|
2014-12-05 10:04:55 +03:00
|
|
|
mBoundCopyReadBuffer = nullptr;
|
|
|
|
mBoundCopyWriteBuffer = nullptr;
|
|
|
|
mBoundPixelPackBuffer = nullptr;
|
|
|
|
mBoundPixelUnpackBuffer = nullptr;
|
|
|
|
mBoundUniformBuffer = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mCurrentProgram = nullptr;
|
2015-01-16 02:40:39 +03:00
|
|
|
mActiveProgramLinkInfo = nullptr;
|
2015-01-13 02:05:21 +03:00
|
|
|
mBoundDrawFramebuffer = nullptr;
|
|
|
|
mBoundReadFramebuffer = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mBoundRenderbuffer = nullptr;
|
2013-06-28 01:07:21 +04:00
|
|
|
mBoundVertexArray = nullptr;
|
|
|
|
mDefaultVertexArray = nullptr;
|
2014-11-26 05:00:06 +03:00
|
|
|
mBoundTransformFeedback = nullptr;
|
|
|
|
mDefaultTransformFeedback = nullptr;
|
|
|
|
|
2016-10-04 04:33:52 +03:00
|
|
|
mQuerySlot_SamplesPassed = nullptr;
|
|
|
|
mQuerySlot_TFPrimsWritten = nullptr;
|
|
|
|
mQuerySlot_TimeElapsed = nullptr;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
mIndexedUniformBufferBindings.clear();
|
2014-12-05 10:04:55 +03:00
|
|
|
|
2016-06-03 11:18:56 +03:00
|
|
|
//////
|
|
|
|
|
|
|
|
ClearLinkedList(mBuffers);
|
|
|
|
ClearLinkedList(mFramebuffers);
|
|
|
|
ClearLinkedList(mPrograms);
|
|
|
|
ClearLinkedList(mQueries);
|
|
|
|
ClearLinkedList(mRenderbuffers);
|
|
|
|
ClearLinkedList(mSamplers);
|
|
|
|
ClearLinkedList(mShaders);
|
2016-06-23 12:01:36 +03:00
|
|
|
ClearLinkedList(mSyncs);
|
2016-06-03 11:18:56 +03:00
|
|
|
ClearLinkedList(mTextures);
|
|
|
|
ClearLinkedList(mTransformFeedbacks);
|
|
|
|
ClearLinkedList(mVertexArrays);
|
|
|
|
|
|
|
|
//////
|
2010-07-19 09:01:14 +04:00
|
|
|
|
2016-12-29 11:08:39 +03:00
|
|
|
if (mEmptyTFO) {
|
|
|
|
gl->fDeleteTransformFeedbacks(1, &mEmptyTFO);
|
|
|
|
mEmptyTFO = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
mFakeBlack_2D_0000 = nullptr;
|
|
|
|
mFakeBlack_2D_0001 = nullptr;
|
|
|
|
mFakeBlack_CubeMap_0000 = nullptr;
|
|
|
|
mFakeBlack_CubeMap_0001 = nullptr;
|
|
|
|
mFakeBlack_3D_0000 = nullptr;
|
|
|
|
mFakeBlack_3D_0001 = nullptr;
|
|
|
|
mFakeBlack_2D_Array_0000 = nullptr;
|
|
|
|
mFakeBlack_2D_Array_0001 = nullptr;
|
2010-08-24 01:03:53 +04:00
|
|
|
|
2016-07-21 11:12:15 +03:00
|
|
|
if (mFakeVertexAttrib0BufferObject) {
|
2011-02-25 01:17:34 +03:00
|
|
|
gl->fDeleteBuffers(1, &mFakeVertexAttrib0BufferObject);
|
2016-07-21 11:12:15 +03:00
|
|
|
mFakeVertexAttrib0BufferObject = 0;
|
|
|
|
}
|
2011-02-25 01:17:34 +03:00
|
|
|
|
2013-11-05 13:35:50 +04:00
|
|
|
// disable all extensions except "WEBGL_lose_context". see bug #927969
|
|
|
|
// spec: http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
|
2014-04-26 06:34:07 +04:00
|
|
|
for (size_t i = 0; i < size_t(WebGLExtensionID::Max); ++i) {
|
2013-11-05 13:35:50 +04:00
|
|
|
WebGLExtensionID extension = WebGLExtensionID(i);
|
|
|
|
|
2014-04-26 06:34:07 +04:00
|
|
|
if (!IsExtensionEnabled(extension) || (extension == WebGLExtensionID::WEBGL_lose_context))
|
2013-11-05 13:35:50 +04:00
|
|
|
continue;
|
|
|
|
|
2014-03-12 06:51:39 +04:00
|
|
|
mExtensions[extension]->MarkLost();
|
2013-11-05 13:35:50 +04:00
|
|
|
mExtensions[extension] = nullptr;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
// We just got rid of everything, so the context had better
|
|
|
|
// have been going away.
|
2016-06-07 02:29:08 +03:00
|
|
|
if (GLContext::ShouldSpew()) {
|
2012-06-11 19:25:06 +04:00
|
|
|
printf_stderr("--- WebGL context destroyed: %p\n", gl.get());
|
2016-06-07 02:29:08 +03:00
|
|
|
}
|
2010-07-19 09:01:14 +04:00
|
|
|
|
2016-07-21 11:12:15 +03:00
|
|
|
MOZ_ASSERT(gl);
|
|
|
|
mGL_OnlyClearInDestroyResourcesAndContext = nullptr;
|
|
|
|
MOZ_ASSERT(!gl);
|
2009-09-03 04:47:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::Invalidate()
|
|
|
|
{
|
2015-09-17 06:37:05 +03:00
|
|
|
if (!mCanvasElement)
|
2011-03-28 03:59:47 +04:00
|
|
|
return;
|
|
|
|
|
2015-09-17 06:37:05 +03:00
|
|
|
mCapturedFrameInvalidated = true;
|
|
|
|
|
|
|
|
if (mInvalidated)
|
2009-09-03 04:47:49 +04:00
|
|
|
return;
|
|
|
|
|
2012-06-06 11:42:47 +04:00
|
|
|
nsSVGEffects::InvalidateDirectRenderingObservers(mCanvasElement);
|
2010-08-13 17:36:13 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mInvalidated = true;
|
2012-07-30 18:20:58 +04:00
|
|
|
mCanvasElement->InvalidateCanvasContent(nullptr);
|
2009-09-03 04:47:49 +04:00
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void
|
|
|
|
WebGLContext::OnVisibilityChange()
|
|
|
|
{
|
|
|
|
if (!IsContextLost()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRestoreWhenVisible || mLastLossWasSimulated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ForceRestoreContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::OnMemoryPressure()
|
|
|
|
{
|
|
|
|
bool shouldLoseContext = mLoseContextOnMemoryPressure;
|
|
|
|
|
|
|
|
if (!mCanLoseContextInForeground &&
|
|
|
|
ProcessPriorityManager::CurrentProcessIsForeground())
|
|
|
|
{
|
|
|
|
shouldLoseContext = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldLoseContext)
|
|
|
|
ForceLoseContext();
|
|
|
|
}
|
|
|
|
|
2009-09-03 04:47:49 +04:00
|
|
|
//
|
|
|
|
// nsICanvasRenderingContextInternal
|
|
|
|
//
|
|
|
|
|
2010-11-17 07:33:03 +03:00
|
|
|
NS_IMETHODIMP
|
2015-10-18 14:14:22 +03:00
|
|
|
WebGLContext::SetContextOptions(JSContext* cx, JS::Handle<JS::Value> options,
|
|
|
|
ErrorResult& aRvForDictionaryInit)
|
2010-11-17 07:33:03 +03:00
|
|
|
{
|
2014-11-14 07:03:50 +03:00
|
|
|
if (options.isNullOrUndefined() && mOptionsFrozen)
|
2013-06-12 22:42:27 +04:00
|
|
|
return NS_OK;
|
2013-06-13 00:04:18 +04:00
|
|
|
|
2013-06-07 22:42:12 +04:00
|
|
|
WebGLContextAttributes attributes;
|
2015-10-18 14:14:22 +03:00
|
|
|
if (!attributes.Init(cx, options)) {
|
|
|
|
aRvForDictionaryInit.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2010-11-17 07:33:03 +03:00
|
|
|
|
|
|
|
WebGLContextOptions newOpts;
|
|
|
|
|
2013-06-07 22:42:12 +04:00
|
|
|
newOpts.stencil = attributes.mStencil;
|
|
|
|
newOpts.depth = attributes.mDepth;
|
|
|
|
newOpts.premultipliedAlpha = attributes.mPremultipliedAlpha;
|
|
|
|
newOpts.antialias = attributes.mAntialias;
|
|
|
|
newOpts.preserveDrawingBuffer = attributes.mPreserveDrawingBuffer;
|
2015-05-21 18:51:49 +03:00
|
|
|
newOpts.failIfMajorPerformanceCaveat = attributes.mFailIfMajorPerformanceCaveat;
|
2014-08-28 03:16:22 +04:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
if (attributes.mAlpha.WasPassed())
|
2014-08-28 03:16:22 +04:00
|
|
|
newOpts.alpha = attributes.mAlpha.Value();
|
2012-07-06 17:19:27 +04:00
|
|
|
|
2014-07-04 03:09:04 +04:00
|
|
|
// Don't do antialiasing if we've disabled MSAA.
|
2014-11-14 07:03:50 +03:00
|
|
|
if (!gfxPrefs::MSAALevel())
|
2014-07-04 03:09:04 +04:00
|
|
|
newOpts.antialias = false;
|
|
|
|
|
2010-12-04 01:44:01 +03:00
|
|
|
#if 0
|
2012-05-23 20:07:29 +04:00
|
|
|
GenerateWarning("aaHint: %d stencil: %d depth: %d alpha: %d premult: %d preserve: %d\n",
|
2011-05-20 23:53:53 +04:00
|
|
|
newOpts.antialias ? 1 : 0,
|
2010-11-17 07:33:03 +03:00
|
|
|
newOpts.stencil ? 1 : 0,
|
|
|
|
newOpts.depth ? 1 : 0,
|
|
|
|
newOpts.alpha ? 1 : 0,
|
2011-05-20 23:53:53 +04:00
|
|
|
newOpts.premultipliedAlpha ? 1 : 0,
|
|
|
|
newOpts.preserveDrawingBuffer ? 1 : 0);
|
2010-12-04 01:44:01 +03:00
|
|
|
#endif
|
2010-11-17 07:33:03 +03:00
|
|
|
|
|
|
|
if (mOptionsFrozen && newOpts != mOptions) {
|
|
|
|
// Error if the options are already frozen, and the ones that were asked for
|
|
|
|
// aren't the same as what they were originally.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOptions = newOpts;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-01 01:02:40 +04:00
|
|
|
int32_t
|
|
|
|
WebGLContext::GetWidth() const
|
|
|
|
{
|
2014-08-28 03:16:22 +04:00
|
|
|
return mWidth;
|
2013-10-01 01:02:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
WebGLContext::GetHeight() const
|
|
|
|
{
|
2014-08-28 03:16:22 +04:00
|
|
|
return mHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* So there are a number of points of failure here. We might fail based
|
|
|
|
* on EGL vs. WGL, or we might fail to alloc a too-large size, or we
|
|
|
|
* might not be able to create a context with a certain combo of context
|
|
|
|
* creation attribs.
|
|
|
|
*
|
|
|
|
* We don't want to test the complete fallback matrix. (for now, at
|
|
|
|
* least) Instead, attempt creation in this order:
|
|
|
|
* 1. By platform API. (e.g. EGL vs. WGL)
|
|
|
|
* 2. By context creation attribs.
|
|
|
|
* 3. By size.
|
|
|
|
*
|
|
|
|
* That is, try to create headless contexts based on the platform API.
|
|
|
|
* Next, create dummy-sized backbuffers for the contexts with the right
|
|
|
|
* caps. Finally, resize the backbuffer to an acceptable size given the
|
|
|
|
* requested size.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool
|
2016-06-21 06:28:57 +03:00
|
|
|
IsFeatureInBlacklist(const nsCOMPtr<nsIGfxInfo>& gfxInfo, int32_t feature,
|
|
|
|
nsCString* const out_blacklistId)
|
2014-08-28 03:16:22 +04:00
|
|
|
{
|
|
|
|
int32_t status;
|
2016-04-14 00:12:47 +03:00
|
|
|
if (!NS_SUCCEEDED(gfxUtils::ThreadSafeGetFeatureStatus(gfxInfo, feature,
|
2016-06-21 06:28:57 +03:00
|
|
|
*out_blacklistId, &status)))
|
|
|
|
{
|
2014-08-28 03:16:22 +04:00
|
|
|
return false;
|
2016-06-21 06:28:57 +03:00
|
|
|
}
|
2014-08-28 03:16:22 +04:00
|
|
|
|
|
|
|
return status != nsIGfxInfo::FEATURE_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-21 18:51:49 +03:00
|
|
|
static bool
|
|
|
|
HasAcceleratedLayers(const nsCOMPtr<nsIGfxInfo>& gfxInfo)
|
|
|
|
{
|
|
|
|
int32_t status;
|
|
|
|
|
2016-04-14 00:12:47 +03:00
|
|
|
nsCString discardFailureId;
|
2015-10-12 06:21:03 +03:00
|
|
|
gfxUtils::ThreadSafeGetFeatureStatus(gfxInfo,
|
|
|
|
nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS,
|
2016-04-14 00:12:47 +03:00
|
|
|
discardFailureId,
|
2015-10-12 06:21:03 +03:00
|
|
|
&status);
|
2015-05-21 18:51:49 +03:00
|
|
|
if (status)
|
|
|
|
return true;
|
2015-10-12 06:21:03 +03:00
|
|
|
gfxUtils::ThreadSafeGetFeatureStatus(gfxInfo,
|
|
|
|
nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS,
|
2016-04-14 00:12:47 +03:00
|
|
|
discardFailureId,
|
2015-10-12 06:21:03 +03:00
|
|
|
&status);
|
2015-05-21 18:51:49 +03:00
|
|
|
if (status)
|
|
|
|
return true;
|
2015-10-12 06:21:03 +03:00
|
|
|
gfxUtils::ThreadSafeGetFeatureStatus(gfxInfo,
|
|
|
|
nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS,
|
2016-04-14 00:12:47 +03:00
|
|
|
discardFailureId,
|
2015-10-12 06:21:03 +03:00
|
|
|
&status);
|
2015-05-21 18:51:49 +03:00
|
|
|
if (status)
|
|
|
|
return true;
|
2015-10-12 06:21:03 +03:00
|
|
|
gfxUtils::ThreadSafeGetFeatureStatus(gfxInfo,
|
|
|
|
nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS,
|
2016-04-14 00:12:47 +03:00
|
|
|
discardFailureId,
|
2015-10-12 06:21:03 +03:00
|
|
|
&status);
|
2015-05-21 18:51:49 +03:00
|
|
|
if (status)
|
|
|
|
return true;
|
2015-10-12 06:21:03 +03:00
|
|
|
gfxUtils::ThreadSafeGetFeatureStatus(gfxInfo,
|
|
|
|
nsIGfxInfo::FEATURE_OPENGL_LAYERS,
|
2016-04-14 00:12:47 +03:00
|
|
|
discardFailureId,
|
2015-10-12 06:21:03 +03:00
|
|
|
&status);
|
2015-05-21 18:51:49 +03:00
|
|
|
if (status)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-28 03:16:22 +04:00
|
|
|
static void
|
2015-09-24 22:21:05 +03:00
|
|
|
PopulateCapFallbackQueue(const gl::SurfaceCaps& baseCaps,
|
|
|
|
std::queue<gl::SurfaceCaps>* out_fallbackCaps)
|
2014-08-28 03:16:22 +04:00
|
|
|
{
|
2014-11-14 07:03:50 +03:00
|
|
|
out_fallbackCaps->push(baseCaps);
|
2014-08-28 03:16:22 +04:00
|
|
|
|
|
|
|
// Dropping antialias drops our quality, but not our correctness.
|
|
|
|
// The user basically doesn't have to handle if this fails, they
|
|
|
|
// just get reduced quality.
|
|
|
|
if (baseCaps.antialias) {
|
2015-09-24 22:21:05 +03:00
|
|
|
gl::SurfaceCaps nextCaps(baseCaps);
|
2014-08-28 03:16:22 +04:00
|
|
|
nextCaps.antialias = false;
|
2014-11-14 07:03:50 +03:00
|
|
|
PopulateCapFallbackQueue(nextCaps, out_fallbackCaps);
|
2014-08-28 03:16:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have to drop one of depth or stencil, we'd prefer to keep
|
|
|
|
// depth. However, the client app will need to handle if this
|
|
|
|
// doesn't work.
|
|
|
|
if (baseCaps.stencil) {
|
2015-09-24 22:21:05 +03:00
|
|
|
gl::SurfaceCaps nextCaps(baseCaps);
|
2014-08-28 03:16:22 +04:00
|
|
|
nextCaps.stencil = false;
|
2014-11-14 07:03:50 +03:00
|
|
|
PopulateCapFallbackQueue(nextCaps, out_fallbackCaps);
|
2014-08-28 03:16:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (baseCaps.depth) {
|
2015-09-24 22:21:05 +03:00
|
|
|
gl::SurfaceCaps nextCaps(baseCaps);
|
2014-08-28 03:16:22 +04:00
|
|
|
nextCaps.depth = false;
|
2014-11-14 07:03:50 +03:00
|
|
|
PopulateCapFallbackQueue(nextCaps, out_fallbackCaps);
|
2014-08-28 03:16:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
static gl::SurfaceCaps
|
|
|
|
BaseCaps(const WebGLContextOptions& options, WebGLContext* webgl)
|
2014-08-28 03:16:22 +04:00
|
|
|
{
|
2015-09-24 22:21:05 +03:00
|
|
|
gl::SurfaceCaps baseCaps;
|
2014-08-28 03:16:22 +04:00
|
|
|
|
|
|
|
baseCaps.color = true;
|
|
|
|
baseCaps.alpha = options.alpha;
|
|
|
|
baseCaps.antialias = options.antialias;
|
|
|
|
baseCaps.depth = options.depth;
|
2014-10-08 08:11:54 +04:00
|
|
|
baseCaps.premultAlpha = options.premultipliedAlpha;
|
2014-08-28 03:16:22 +04:00
|
|
|
baseCaps.preserve = options.preserveDrawingBuffer;
|
|
|
|
baseCaps.stencil = options.stencil;
|
|
|
|
|
2014-10-08 08:16:14 +04:00
|
|
|
if (!baseCaps.alpha)
|
|
|
|
baseCaps.premultAlpha = true;
|
|
|
|
|
2014-08-28 03:16:22 +04:00
|
|
|
// we should really have this behind a
|
|
|
|
// |gfxPlatform::GetPlatform()->GetScreenDepth() == 16| check, but
|
|
|
|
// for now it's just behind a pref for testing/evaluation.
|
2015-10-12 06:21:03 +03:00
|
|
|
baseCaps.bpp16 = gfxPrefs::WebGLPrefer16bpp();
|
2014-08-28 03:16:22 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2015-09-24 22:21:05 +03:00
|
|
|
do {
|
|
|
|
auto canvasElement = webgl->GetCanvas();
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!canvasElement)
|
|
|
|
break;
|
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
auto ownerDoc = canvasElement->OwnerDoc();
|
|
|
|
nsIWidget* docWidget = nsContentUtils::WidgetForDocument(ownerDoc);
|
|
|
|
if (!docWidget)
|
|
|
|
break;
|
|
|
|
|
|
|
|
layers::LayerManager* layerManager = docWidget->GetLayerManager();
|
|
|
|
if (!layerManager)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// XXX we really want "AsSurfaceAllocator" here for generality
|
|
|
|
layers::ShadowLayerForwarder* forwarder = layerManager->AsShadowForwarder();
|
|
|
|
if (!forwarder)
|
|
|
|
break;
|
|
|
|
|
2016-09-27 11:46:41 +03:00
|
|
|
baseCaps.surfaceAllocator = forwarder->GetTextureForwarder();
|
2015-09-24 22:21:05 +03:00
|
|
|
} while (false);
|
2014-08-28 03:16:22 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Done with baseCaps construction.
|
|
|
|
|
2016-06-21 06:28:57 +03:00
|
|
|
if (!gfxPrefs::WebGLForceMSAA()) {
|
|
|
|
const nsCOMPtr<nsIGfxInfo> gfxInfo = services::GetGfxInfo();
|
|
|
|
|
|
|
|
nsCString blocklistId;
|
|
|
|
if (IsFeatureInBlacklist(gfxInfo, nsIGfxInfo::FEATURE_WEBGL_MSAA, &blocklistId)) {
|
|
|
|
webgl->GenerateWarning("Disallowing antialiased backbuffers due"
|
|
|
|
" to blacklisting.");
|
|
|
|
baseCaps.antialias = false;
|
|
|
|
}
|
2014-08-28 03:16:22 +04:00
|
|
|
}
|
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
return baseCaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
static already_AddRefed<gl::GLContext>
|
|
|
|
CreateGLWithEGL(const gl::SurfaceCaps& caps, gl::CreateContextFlags flags,
|
2016-06-21 06:42:10 +03:00
|
|
|
WebGLContext* webgl,
|
|
|
|
std::vector<WebGLContext::FailureReason>* const out_failReasons)
|
2015-09-24 22:21:05 +03:00
|
|
|
{
|
2016-04-14 23:46:37 +03:00
|
|
|
const gfx::IntSize dummySize(16, 16);
|
2016-06-22 04:02:07 +03:00
|
|
|
nsCString failureId;
|
2016-04-14 23:46:37 +03:00
|
|
|
RefPtr<GLContext> gl = gl::GLContextProviderEGL::CreateOffscreen(dummySize, caps,
|
2016-06-22 04:02:07 +03:00
|
|
|
flags, &failureId);
|
2016-04-14 23:46:37 +03:00
|
|
|
if (gl && gl->IsANGLE()) {
|
|
|
|
gl = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
if (!gl) {
|
2016-06-21 06:42:10 +03:00
|
|
|
out_failReasons->push_back(WebGLContext::FailureReason(
|
2016-06-22 04:02:07 +03:00
|
|
|
failureId,
|
2016-06-21 06:42:10 +03:00
|
|
|
"Error during EGL OpenGL init."
|
|
|
|
));
|
2015-09-24 22:21:05 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<GLContext>
|
|
|
|
CreateGLWithANGLE(const gl::SurfaceCaps& caps, gl::CreateContextFlags flags,
|
2016-06-21 06:42:10 +03:00
|
|
|
WebGLContext* webgl,
|
|
|
|
std::vector<WebGLContext::FailureReason>* const out_failReasons)
|
2015-09-24 22:21:05 +03:00
|
|
|
{
|
2016-04-14 23:46:37 +03:00
|
|
|
const gfx::IntSize dummySize(16, 16);
|
2016-06-22 04:02:07 +03:00
|
|
|
nsCString failureId;
|
2016-04-14 23:46:37 +03:00
|
|
|
RefPtr<GLContext> gl = gl::GLContextProviderEGL::CreateOffscreen(dummySize, caps,
|
2016-06-22 04:02:07 +03:00
|
|
|
flags, &failureId);
|
2016-04-14 23:46:37 +03:00
|
|
|
if (gl && !gl->IsANGLE()) {
|
|
|
|
gl = nullptr;
|
|
|
|
}
|
2015-09-24 22:21:05 +03:00
|
|
|
|
|
|
|
if (!gl) {
|
2016-06-21 06:42:10 +03:00
|
|
|
out_failReasons->push_back(WebGLContext::FailureReason(
|
2016-06-22 04:02:07 +03:00
|
|
|
failureId,
|
2016-06-21 06:42:10 +03:00
|
|
|
"Error during ANGLE OpenGL init."
|
|
|
|
));
|
2015-09-24 22:21:05 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<gl::GLContext>
|
|
|
|
CreateGLWithDefault(const gl::SurfaceCaps& caps, gl::CreateContextFlags flags,
|
2016-06-21 06:28:57 +03:00
|
|
|
WebGLContext* webgl,
|
2016-06-21 06:42:10 +03:00
|
|
|
std::vector<WebGLContext::FailureReason>* const out_failReasons)
|
2015-09-24 22:21:05 +03:00
|
|
|
{
|
2016-04-14 23:46:37 +03:00
|
|
|
const gfx::IntSize dummySize(16, 16);
|
2016-06-22 04:02:07 +03:00
|
|
|
nsCString failureId;
|
2016-06-06 23:52:42 +03:00
|
|
|
RefPtr<GLContext> gl = gl::GLContextProvider::CreateOffscreen(dummySize, caps,
|
2016-06-22 04:02:07 +03:00
|
|
|
flags, &failureId);
|
2016-04-14 23:46:37 +03:00
|
|
|
if (gl && gl->IsANGLE()) {
|
|
|
|
gl = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
if (!gl) {
|
2016-06-21 06:42:10 +03:00
|
|
|
out_failReasons->push_back(WebGLContext::FailureReason(
|
2016-06-22 04:02:07 +03:00
|
|
|
failureId,
|
2016-06-21 06:42:10 +03:00
|
|
|
"Error during native OpenGL init."
|
|
|
|
));
|
2015-09-24 22:21:05 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebGLContext::CreateAndInitGLWith(FnCreateGL_T fnCreateGL,
|
|
|
|
const gl::SurfaceCaps& baseCaps,
|
2016-05-12 02:45:43 +03:00
|
|
|
gl::CreateContextFlags flags,
|
2016-06-21 06:28:57 +03:00
|
|
|
std::vector<FailureReason>* const out_failReasons)
|
2015-09-24 22:21:05 +03:00
|
|
|
{
|
|
|
|
std::queue<gl::SurfaceCaps> fallbackCaps;
|
2014-08-28 03:16:22 +04:00
|
|
|
PopulateCapFallbackQueue(baseCaps, &fallbackCaps);
|
|
|
|
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!gl, "GFX: Already have a context.");
|
2016-07-21 11:12:15 +03:00
|
|
|
RefPtr<gl::GLContext> potentialGL;
|
2014-08-28 03:16:22 +04:00
|
|
|
while (!fallbackCaps.empty()) {
|
2016-06-21 06:28:57 +03:00
|
|
|
const gl::SurfaceCaps& caps = fallbackCaps.front();
|
2016-07-21 11:12:15 +03:00
|
|
|
potentialGL = fnCreateGL(caps, flags, this, out_failReasons);
|
|
|
|
if (potentialGL)
|
2014-08-28 03:16:22 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
fallbackCaps.pop();
|
|
|
|
}
|
2016-09-13 21:17:29 +03:00
|
|
|
if (!potentialGL) {
|
|
|
|
out_failReasons->push_back(FailureReason("FEATURE_FAILURE_WEBGL_EXHAUSTED_CAPS",
|
|
|
|
"Exhausted GL driver caps."));
|
2015-09-24 22:21:05 +03:00
|
|
|
return false;
|
2016-09-13 21:17:29 +03:00
|
|
|
}
|
2014-08-28 03:16:22 +04:00
|
|
|
|
2016-06-21 06:28:57 +03:00
|
|
|
FailureReason reason;
|
2016-07-21 11:12:15 +03:00
|
|
|
|
|
|
|
mGL_OnlyClearInDestroyResourcesAndContext = potentialGL;
|
|
|
|
MOZ_RELEASE_ASSERT(gl);
|
2016-06-21 06:42:10 +03:00
|
|
|
if (!InitAndValidateGL(&reason)) {
|
2016-07-21 11:12:15 +03:00
|
|
|
DestroyResourcesAndContext();
|
|
|
|
MOZ_RELEASE_ASSERT(!gl);
|
|
|
|
|
2016-05-20 20:14:15 +03:00
|
|
|
// The fail reason here should be specific enough for now.
|
2016-06-21 06:28:57 +03:00
|
|
|
out_failReasons->push_back(reason);
|
2015-09-24 22:21:05 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-10-01 01:02:40 +04:00
|
|
|
}
|
2014-08-28 03:16:22 +04:00
|
|
|
|
|
|
|
bool
|
2016-06-21 06:28:57 +03:00
|
|
|
WebGLContext::CreateAndInitGL(bool forceEnabled,
|
|
|
|
std::vector<FailureReason>* const out_failReasons)
|
2014-08-28 03:16:22 +04:00
|
|
|
{
|
2016-06-22 22:47:03 +03:00
|
|
|
const gl::SurfaceCaps baseCaps = BaseCaps(mOptions, this);
|
|
|
|
gl::CreateContextFlags flags = gl::CreateContextFlags::NO_VALIDATION;
|
|
|
|
bool tryNativeGL = true;
|
|
|
|
bool tryANGLE = false;
|
|
|
|
|
2016-06-21 06:43:06 +03:00
|
|
|
if (forceEnabled) {
|
2016-06-22 22:47:03 +03:00
|
|
|
flags |= gl::CreateContextFlags::FORCE_ENABLE_HARDWARE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsWebGL2()) {
|
|
|
|
flags |= gl::CreateContextFlags::PREFER_ES3;
|
|
|
|
} else {
|
|
|
|
flags |= gl::CreateContextFlags::REQUIRE_COMPAT_PROFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
|
|
|
|
|
|
|
const bool useEGL = PR_GetEnv("MOZ_WEBGL_FORCE_EGL");
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2016-09-12 12:39:54 +03:00
|
|
|
tryNativeGL = false;
|
|
|
|
tryANGLE = true;
|
2016-06-22 22:47:03 +03:00
|
|
|
|
|
|
|
if (gfxPrefs::WebGLDisableWGL()) {
|
|
|
|
tryNativeGL = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gfxPrefs::WebGLDisableANGLE() || PR_GetEnv("MOZ_WEBGL_FORCE_OPENGL") || useEGL) {
|
|
|
|
tryNativeGL = true;
|
|
|
|
tryANGLE = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (tryNativeGL && !forceEnabled) {
|
2016-06-21 06:28:57 +03:00
|
|
|
const nsCOMPtr<nsIGfxInfo> gfxInfo = services::GetGfxInfo();
|
2016-06-21 06:43:06 +03:00
|
|
|
const auto feature = nsIGfxInfo::FEATURE_WEBGL_OPENGL;
|
2016-06-21 06:28:57 +03:00
|
|
|
|
2016-06-21 06:28:57 +03:00
|
|
|
FailureReason reason;
|
2016-06-21 06:43:06 +03:00
|
|
|
if (IsFeatureInBlacklist(gfxInfo, feature, &reason.key)) {
|
2016-06-21 06:28:57 +03:00
|
|
|
reason.info = "Refused to create native OpenGL context because of blacklist"
|
|
|
|
" entry: ";
|
2016-06-21 06:42:10 +03:00
|
|
|
reason.info.Append(reason.key);
|
2016-06-21 06:28:57 +03:00
|
|
|
|
|
|
|
out_failReasons->push_back(reason);
|
|
|
|
|
2016-06-21 06:42:10 +03:00
|
|
|
GenerateWarning(reason.info.BeginReading());
|
2016-06-22 22:47:03 +03:00
|
|
|
tryNativeGL = false;
|
2016-06-21 06:28:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
2014-08-28 03:16:22 +04:00
|
|
|
|
2016-06-22 22:47:03 +03:00
|
|
|
if (tryNativeGL) {
|
2016-06-21 06:28:57 +03:00
|
|
|
if (useEGL)
|
|
|
|
return CreateAndInitGLWith(CreateGLWithEGL, baseCaps, flags, out_failReasons);
|
|
|
|
|
2016-06-22 22:47:03 +03:00
|
|
|
if (CreateAndInitGLWith(CreateGLWithDefault, baseCaps, flags, out_failReasons))
|
|
|
|
return true;
|
2016-06-21 06:28:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
|
|
|
|
2016-06-22 22:47:03 +03:00
|
|
|
if (tryANGLE)
|
2016-06-21 06:28:57 +03:00
|
|
|
return CreateAndInitGLWith(CreateGLWithANGLE, baseCaps, flags, out_failReasons);
|
|
|
|
|
|
|
|
//////
|
2016-06-21 06:28:57 +03:00
|
|
|
|
2016-06-21 06:42:10 +03:00
|
|
|
out_failReasons->push_back(FailureReason("FEATURE_FAILURE_WEBGL_EXHAUSTED_DRIVERS",
|
|
|
|
"Exhausted GL driver options."));
|
2016-06-21 06:28:57 +03:00
|
|
|
return false;
|
2014-08-28 03:16:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback for resizes:
|
|
|
|
bool
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLContext::ResizeBackbuffer(uint32_t requestedWidth,
|
|
|
|
uint32_t requestedHeight)
|
2014-08-28 03:16:22 +04:00
|
|
|
{
|
|
|
|
uint32_t width = requestedWidth;
|
|
|
|
uint32_t height = requestedHeight;
|
|
|
|
|
|
|
|
bool resized = false;
|
|
|
|
while (width || height) {
|
|
|
|
width = width ? width : 1;
|
|
|
|
height = height ? height : 1;
|
|
|
|
|
|
|
|
gfx::IntSize curSize(width, height);
|
|
|
|
if (gl->ResizeOffscreen(curSize)) {
|
|
|
|
resized = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
width /= 2;
|
|
|
|
height /= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!resized)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mWidth = gl->OffscreenSize().width;
|
|
|
|
mHeight = gl->OffscreenSize().height;
|
|
|
|
MOZ_ASSERT((uint32_t)mWidth == width);
|
|
|
|
MOZ_ASSERT((uint32_t)mHeight == height);
|
|
|
|
|
|
|
|
if (width != requestedWidth ||
|
|
|
|
height != requestedHeight)
|
|
|
|
{
|
|
|
|
GenerateWarning("Requested size %dx%d was too large, but resize"
|
|
|
|
" to %dx%d succeeded.",
|
|
|
|
requestedWidth, requestedHeight,
|
|
|
|
width, height);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-12 02:45:43 +03:00
|
|
|
void
|
|
|
|
WebGLContext::ThrowEvent_WebGLContextCreationError(const nsACString& text)
|
|
|
|
{
|
|
|
|
RefPtr<EventTarget> target = mCanvasElement;
|
2016-09-30 10:29:34 +03:00
|
|
|
if (!target && mOffscreenCanvas) {
|
2016-05-12 02:45:43 +03:00
|
|
|
target = mOffscreenCanvas;
|
2016-09-30 10:29:34 +03:00
|
|
|
} else if (!target) {
|
|
|
|
GenerateWarning("Failed to create WebGL context: %s", text.BeginReading());
|
|
|
|
return;
|
2016-05-12 02:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto kEventName = NS_LITERAL_STRING("webglcontextcreationerror");
|
|
|
|
|
|
|
|
WebGLContextEventInit eventInit;
|
|
|
|
// eventInit.mCancelable = true; // The spec says this, but it's silly.
|
|
|
|
eventInit.mStatusMessage = NS_ConvertASCIItoUTF16(text);
|
|
|
|
|
|
|
|
const RefPtr<WebGLContextEvent> event = WebGLContextEvent::Constructor(target,
|
|
|
|
kEventName,
|
|
|
|
eventInit);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
bool didPreventDefault;
|
|
|
|
target->DispatchEvent(event, &didPreventDefault);
|
|
|
|
|
|
|
|
//////
|
|
|
|
|
|
|
|
GenerateWarning("Failed to create WebGL context: %s", text.BeginReading());
|
|
|
|
}
|
|
|
|
|
2009-09-03 04:47:49 +04:00
|
|
|
NS_IMETHODIMP
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLContext::SetDimensions(int32_t signedWidth, int32_t signedHeight)
|
2009-09-03 04:47:49 +04:00
|
|
|
{
|
2014-11-14 07:03:50 +03:00
|
|
|
if (signedWidth < 0 || signedHeight < 0) {
|
2016-07-22 20:26:05 +03:00
|
|
|
if (!gl) {
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID,
|
|
|
|
NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_SIZE"));
|
|
|
|
}
|
2012-09-27 18:13:45 +04:00
|
|
|
GenerateWarning("Canvas size is too large (seems like a negative value wrapped)");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-08-03 01:28:02 +04:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
uint32_t width = signedWidth;
|
|
|
|
uint32_t height = signedHeight;
|
2012-09-27 18:13:44 +04:00
|
|
|
|
2015-07-02 02:19:09 +03:00
|
|
|
// Early success return cases
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
// May have a OffscreenCanvas instead of an HTMLCanvasElement
|
|
|
|
if (GetCanvas())
|
|
|
|
GetCanvas()->InvalidateCanvas();
|
2011-03-28 03:59:47 +04:00
|
|
|
|
2011-05-19 14:49:18 +04:00
|
|
|
// Zero-sized surfaces can cause problems.
|
2014-11-14 07:03:50 +03:00
|
|
|
if (width == 0)
|
2011-05-19 14:49:18 +04:00
|
|
|
width = 1;
|
2014-11-14 07:03:50 +03:00
|
|
|
|
|
|
|
if (height == 0)
|
2011-05-19 14:49:18 +04:00
|
|
|
height = 1;
|
|
|
|
|
2011-10-19 23:09:57 +04:00
|
|
|
// If we already have a gl context, then we just need to resize it
|
|
|
|
if (gl) {
|
2014-08-28 03:16:22 +04:00
|
|
|
if ((uint32_t)mWidth == width &&
|
|
|
|
(uint32_t)mHeight == height)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsContextLost())
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-12-20 03:47:54 +04:00
|
|
|
MakeContextCurrent();
|
|
|
|
|
2013-10-31 17:52:24 +04:00
|
|
|
// If we've already drawn, we should commit the current buffer.
|
|
|
|
PresentScreenBuffer();
|
|
|
|
|
2015-03-19 05:55:44 +03:00
|
|
|
if (IsContextLost()) {
|
|
|
|
GenerateWarning("WebGL context was lost due to swap failure.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:52:24 +04:00
|
|
|
// ResizeOffscreen scraps the current prod buffer before making a new one.
|
2014-08-28 03:16:22 +04:00
|
|
|
if (!ResizeBackbuffer(width, height)) {
|
|
|
|
GenerateWarning("WebGL context failed to resize.");
|
|
|
|
ForceLoseContext();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-10-19 23:09:57 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
// everything's good, we're done here
|
2011-10-17 18:59:28 +04:00
|
|
|
mResetLayer = true;
|
2014-04-18 00:15:41 +04:00
|
|
|
mBackbufferNeedsClear = true;
|
2011-12-20 03:47:54 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-10 02:07:12 +04:00
|
|
|
|
2016-07-22 20:26:05 +03:00
|
|
|
nsCString failureId = NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_UNKOWN");
|
|
|
|
auto autoTelemetry = mozilla::MakeScopeExit([&] {
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID,
|
|
|
|
failureId);
|
|
|
|
});
|
|
|
|
|
2012-09-27 18:13:45 +04:00
|
|
|
// End of early return cases.
|
|
|
|
// At this point we know that we're not just resizing an existing context,
|
|
|
|
// we are initializing a new context.
|
2011-06-10 23:02:06 +04:00
|
|
|
|
2012-08-03 01:28:02 +04:00
|
|
|
// if we exceeded either the global or the per-principal limit for WebGL contexts,
|
|
|
|
// lose the oldest-used context now to free resources. Note that we can't do that
|
|
|
|
// in the WebGLContext constructor as we don't have a canvas element yet there.
|
|
|
|
// Here is the right place to do so, as we are about to create the OpenGL context
|
|
|
|
// and that is what can fail if we already have too many.
|
|
|
|
LoseOldestWebGLContextIfLimitExceeded();
|
2011-06-10 23:02:06 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
// We're going to create an entirely new context. If our
|
|
|
|
// generation is not 0 right now (that is, if this isn't the first
|
|
|
|
// context we're creating), we may have to dispatch a context lost
|
|
|
|
// event.
|
2010-06-10 02:07:12 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
// If incrementing the generation would cause overflow,
|
|
|
|
// don't allow it. Allowing this would allow us to use
|
|
|
|
// resource handles created from older context generations.
|
2014-08-28 03:16:22 +04:00
|
|
|
if (!(mGeneration + 1).isValid()) {
|
2016-05-12 02:45:43 +03:00
|
|
|
// exit without changing the value of mGeneration
|
2016-07-22 20:26:05 +03:00
|
|
|
failureId = NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_TOO_MANY");
|
2016-05-12 02:45:43 +03:00
|
|
|
const nsLiteralCString text("Too many WebGL contexts created this run.");
|
|
|
|
ThrowEvent_WebGLContextCreationError(text);
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-05-27 18:12:13 +04:00
|
|
|
}
|
|
|
|
|
2015-07-21 07:38:52 +03:00
|
|
|
// increment the generation number - Do this early because later
|
|
|
|
// in CreateOffscreenGL(), "default" objects are created that will
|
|
|
|
// pick up the old generation.
|
|
|
|
++mGeneration;
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
bool disabled = gfxPrefs::WebGLDisabled();
|
2015-06-16 00:05:02 +03:00
|
|
|
|
|
|
|
// TODO: When we have software webgl support we should use that instead.
|
|
|
|
disabled |= gfxPlatform::InSafeMode();
|
|
|
|
|
2014-08-28 03:16:22 +04:00
|
|
|
if (disabled) {
|
2016-07-29 21:28:51 +03:00
|
|
|
if (gfxPlatform::InSafeMode()) {
|
|
|
|
failureId = NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_SAFEMODE");
|
|
|
|
} else {
|
|
|
|
failureId = NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_DISABLED");
|
|
|
|
}
|
2016-05-12 02:45:43 +03:00
|
|
|
const nsLiteralCString text("WebGL is currently disabled.");
|
|
|
|
ThrowEvent_WebGLContextCreationError(text);
|
2014-08-28 03:16:22 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-27 13:15:40 +04:00
|
|
|
}
|
|
|
|
|
2016-05-25 23:18:00 +03:00
|
|
|
if (gfxPrefs::WebGLDisableFailIfMajorPerformanceCaveat()) {
|
|
|
|
mOptions.failIfMajorPerformanceCaveat = false;
|
|
|
|
}
|
2016-05-12 02:45:43 +03:00
|
|
|
|
2016-05-25 23:18:00 +03:00
|
|
|
if (mOptions.failIfMajorPerformanceCaveat) {
|
2016-05-12 02:45:43 +03:00
|
|
|
nsCOMPtr<nsIGfxInfo> gfxInfo = services::GetGfxInfo();
|
|
|
|
if (!HasAcceleratedLayers(gfxInfo)) {
|
2016-07-22 20:26:05 +03:00
|
|
|
failureId = NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_PERF_CAVEAT");
|
2016-05-12 02:45:43 +03:00
|
|
|
const nsLiteralCString text("failIfMajorPerformanceCaveat: Compositor is not"
|
|
|
|
" hardware-accelerated.");
|
|
|
|
ThrowEvent_WebGLContextCreationError(text);
|
2015-05-21 18:51:49 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 03:16:22 +04:00
|
|
|
// Alright, now let's start trying.
|
2015-10-12 06:21:03 +03:00
|
|
|
bool forceEnabled = gfxPrefs::WebGLForceEnabled();
|
2014-08-28 03:16:22 +04:00
|
|
|
ScopedGfxFeatureReporter reporter("WebGL", forceEnabled);
|
2014-08-27 13:15:40 +04:00
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
MOZ_ASSERT(!gl);
|
2016-06-21 06:28:57 +03:00
|
|
|
std::vector<FailureReason> failReasons;
|
|
|
|
if (!CreateAndInitGL(forceEnabled, &failReasons)) {
|
|
|
|
nsCString text("WebGL creation failed: ");
|
|
|
|
for (const auto& cur : failReasons) {
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID, cur.key);
|
|
|
|
|
|
|
|
text.AppendASCII("\n* ");
|
|
|
|
text.Append(cur.info);
|
|
|
|
}
|
2016-07-22 20:26:05 +03:00
|
|
|
failureId = NS_LITERAL_CSTRING("FEATURE_FAILURE_REASON");
|
2016-05-12 02:45:43 +03:00
|
|
|
ThrowEvent_WebGLContextCreationError(text);
|
2014-08-28 03:16:22 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-27 13:15:40 +04:00
|
|
|
}
|
2014-08-28 03:16:22 +04:00
|
|
|
MOZ_ASSERT(gl);
|
2015-09-24 22:21:05 +03:00
|
|
|
MOZ_ASSERT_IF(mOptions.alpha, gl->Caps().alpha);
|
|
|
|
|
2016-05-25 23:18:00 +03:00
|
|
|
if (mOptions.failIfMajorPerformanceCaveat) {
|
|
|
|
if (gl->IsWARP()) {
|
2016-07-21 11:12:15 +03:00
|
|
|
DestroyResourcesAndContext();
|
|
|
|
MOZ_ASSERT(!gl);
|
2016-06-21 06:38:47 +03:00
|
|
|
|
2016-07-22 20:26:05 +03:00
|
|
|
failureId = NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_PERF_WARP");
|
2016-05-25 23:18:00 +03:00
|
|
|
const nsLiteralCString text("failIfMajorPerformanceCaveat: Driver is not"
|
|
|
|
" hardware-accelerated.");
|
|
|
|
ThrowEvent_WebGLContextCreationError(text);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-06-21 06:38:47 +03:00
|
|
|
|
2016-06-22 04:02:07 +03:00
|
|
|
#ifdef XP_WIN
|
2016-06-21 06:38:47 +03:00
|
|
|
if (gl->GetContextType() == gl::GLContextType::WGL &&
|
|
|
|
!gl::sWGLLib.HasDXInterop2())
|
|
|
|
{
|
2016-07-21 11:12:15 +03:00
|
|
|
DestroyResourcesAndContext();
|
|
|
|
MOZ_ASSERT(!gl);
|
2016-06-21 06:38:47 +03:00
|
|
|
|
2016-07-22 20:26:05 +03:00
|
|
|
failureId = NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_DXGL_INTEROP2");
|
2016-06-21 06:38:47 +03:00
|
|
|
const nsLiteralCString text("Caveat: WGL without DXGLInterop2.");
|
|
|
|
ThrowEvent_WebGLContextCreationError(text);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-06-22 04:02:07 +03:00
|
|
|
#endif
|
2016-05-25 23:18:00 +03:00
|
|
|
}
|
|
|
|
|
2014-08-28 03:16:22 +04:00
|
|
|
if (!ResizeBackbuffer(width, height)) {
|
2016-07-22 20:26:05 +03:00
|
|
|
failureId = NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_BACKBUFFER");
|
2016-05-12 02:45:43 +03:00
|
|
|
const nsLiteralCString text("Initializing WebGL backbuffer failed.");
|
|
|
|
ThrowEvent_WebGLContextCreationError(text);
|
2010-08-07 09:09:18 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-06-07 02:29:08 +03:00
|
|
|
if (GLContext::ShouldSpew()) {
|
2012-06-11 19:25:06 +04:00
|
|
|
printf_stderr("--- WebGL context created: %p\n", gl.get());
|
2016-06-07 02:29:08 +03:00
|
|
|
}
|
2010-08-07 09:09:18 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mResetLayer = true;
|
|
|
|
mOptionsFrozen = true;
|
2009-09-03 04:47:49 +04:00
|
|
|
|
2014-10-09 03:37:30 +04:00
|
|
|
// Update our internal stuff:
|
2015-07-30 19:40:56 +03:00
|
|
|
if (gl->WorkAroundDriverBugs()) {
|
2014-11-14 07:03:50 +03:00
|
|
|
if (!mOptions.alpha && gl->Caps().alpha)
|
2014-10-09 03:37:30 +04:00
|
|
|
mNeedsFakeNoAlpha = true;
|
2015-03-12 04:23:56 +03:00
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
if (!mOptions.depth && gl->Caps().depth)
|
|
|
|
mNeedsFakeNoDepth = true;
|
|
|
|
|
|
|
|
if (!mOptions.stencil && gl->Caps().stencil)
|
2015-03-12 04:23:56 +03:00
|
|
|
mNeedsFakeNoStencil = true;
|
2016-02-19 04:59:24 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
|
|
|
if (!nsCocoaFeatures::IsAtLeastVersion(10, 12) &&
|
|
|
|
gl->Vendor() == GLVendor::Intel)
|
|
|
|
{
|
|
|
|
mNeedsEmulatedLoneDepthStencil = true;
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-09 03:37:30 +04:00
|
|
|
}
|
2014-08-28 03:16:22 +04:00
|
|
|
|
2014-10-23 01:17:02 +04:00
|
|
|
// Update mOptions.
|
2015-09-24 22:21:05 +03:00
|
|
|
if (!gl->Caps().depth)
|
|
|
|
mOptions.depth = false;
|
|
|
|
|
|
|
|
if (!gl->Caps().stencil)
|
|
|
|
mOptions.stencil = false;
|
|
|
|
|
2014-10-23 01:17:02 +04:00
|
|
|
mOptions.antialias = gl->Caps().antialias;
|
|
|
|
|
2016-06-01 00:44:46 +03:00
|
|
|
//////
|
|
|
|
// Initial setup.
|
|
|
|
|
2014-10-09 03:37:30 +04:00
|
|
|
MakeContextCurrent();
|
|
|
|
|
|
|
|
gl->fViewport(0, 0, mWidth, mHeight);
|
|
|
|
mViewportWidth = mWidth;
|
|
|
|
mViewportHeight = mHeight;
|
|
|
|
|
2015-03-12 06:08:41 +03:00
|
|
|
gl->fScissor(0, 0, mWidth, mHeight);
|
2013-02-14 03:26:24 +04:00
|
|
|
gl->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, 0);
|
2011-12-20 03:47:54 +04:00
|
|
|
|
2016-06-01 00:44:46 +03:00
|
|
|
//////
|
|
|
|
// Check everything
|
2015-07-02 02:19:09 +03:00
|
|
|
|
2016-06-01 00:44:46 +03:00
|
|
|
AssertCachedBindings();
|
|
|
|
AssertCachedGlobalState();
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2014-08-28 03:16:22 +04:00
|
|
|
MOZ_ASSERT(gl->Caps().color);
|
2015-09-24 22:21:05 +03:00
|
|
|
|
2014-10-09 03:37:30 +04:00
|
|
|
MOZ_ASSERT_IF(!mNeedsFakeNoAlpha, gl->Caps().alpha == mOptions.alpha);
|
|
|
|
MOZ_ASSERT_IF(mNeedsFakeNoAlpha, !mOptions.alpha && gl->Caps().alpha);
|
2015-09-24 22:21:05 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT_IF(!mNeedsFakeNoDepth, gl->Caps().depth == mOptions.depth);
|
|
|
|
MOZ_ASSERT_IF(mNeedsFakeNoDepth, !mOptions.depth && gl->Caps().depth);
|
|
|
|
|
|
|
|
MOZ_ASSERT_IF(!mNeedsFakeNoStencil, gl->Caps().stencil == mOptions.stencil);
|
|
|
|
MOZ_ASSERT_IF(mNeedsFakeNoStencil, !mOptions.stencil && gl->Caps().stencil);
|
|
|
|
|
2014-10-23 01:17:02 +04:00
|
|
|
MOZ_ASSERT(gl->Caps().antialias == mOptions.antialias);
|
2014-08-28 03:16:22 +04:00
|
|
|
MOZ_ASSERT(gl->Caps().preserve == mOptions.preserveDrawingBuffer);
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2016-06-01 00:44:46 +03:00
|
|
|
//////
|
|
|
|
// Clear immediately, because we need to present the cleared initial buffer
|
|
|
|
mBackbufferNeedsClear = true;
|
|
|
|
ClearBackbufferIfNeeded();
|
|
|
|
|
|
|
|
mShouldPresent = true;
|
|
|
|
|
|
|
|
//////
|
2014-05-22 06:03:09 +04:00
|
|
|
|
2011-03-02 23:50:36 +03:00
|
|
|
reporter.SetSuccessful();
|
2016-05-20 20:14:15 +03:00
|
|
|
|
2016-07-22 20:26:05 +03:00
|
|
|
failureId = NS_LITERAL_CSTRING("SUCCESS");
|
2009-09-03 04:47:49 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-18 00:15:41 +04:00
|
|
|
void
|
|
|
|
WebGLContext::ClearBackbufferIfNeeded()
|
|
|
|
{
|
|
|
|
if (!mBackbufferNeedsClear)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ClearScreen();
|
|
|
|
|
|
|
|
mBackbufferNeedsClear = false;
|
|
|
|
}
|
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
void
|
|
|
|
WebGLContext::LoseOldestWebGLContextIfLimitExceeded()
|
2012-08-03 01:28:02 +04:00
|
|
|
{
|
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
|
|
|
// some mobile devices can't have more than 8 GL contexts overall
|
|
|
|
const size_t kMaxWebGLContextsPerPrincipal = 2;
|
|
|
|
const size_t kMaxWebGLContexts = 4;
|
|
|
|
#else
|
2012-09-21 21:44:35 +04:00
|
|
|
const size_t kMaxWebGLContextsPerPrincipal = 16;
|
|
|
|
const size_t kMaxWebGLContexts = 32;
|
2012-08-03 01:28:02 +04:00
|
|
|
#endif
|
|
|
|
MOZ_ASSERT(kMaxWebGLContextsPerPrincipal < kMaxWebGLContexts);
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
// XXX mtseng: bug 709490, WebGLMemoryTracker is not thread safe.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-03 01:28:02 +04:00
|
|
|
// it's important to update the index on a new context before losing old contexts,
|
|
|
|
// otherwise new unused contexts would all have index 0 and we couldn't distinguish older ones
|
|
|
|
// when choosing which one to lose first.
|
|
|
|
UpdateLastUseIndex();
|
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLMemoryTracker::ContextsArrayType& contexts = WebGLMemoryTracker::Contexts();
|
2012-08-03 01:28:02 +04:00
|
|
|
|
|
|
|
// quick exit path, should cover a majority of cases
|
2014-11-14 07:03:50 +03:00
|
|
|
if (contexts.Length() <= kMaxWebGLContextsPerPrincipal)
|
2012-08-03 01:28:02 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
// note that here by "context" we mean "non-lost context". See the check for
|
|
|
|
// IsContextLost() below. Indeed, the point of this function is to maybe lose
|
|
|
|
// some currently non-lost context.
|
|
|
|
|
|
|
|
uint64_t oldestIndex = UINT64_MAX;
|
|
|
|
uint64_t oldestIndexThisPrincipal = UINT64_MAX;
|
2014-11-14 07:03:50 +03:00
|
|
|
const WebGLContext* oldestContext = nullptr;
|
|
|
|
const WebGLContext* oldestContextThisPrincipal = nullptr;
|
2012-08-03 01:28:02 +04:00
|
|
|
size_t numContexts = 0;
|
|
|
|
size_t numContextsThisPrincipal = 0;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < contexts.Length(); ++i) {
|
|
|
|
// don't want to lose ourselves.
|
|
|
|
if (contexts[i] == this)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (contexts[i]->IsContextLost())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!contexts[i]->GetCanvas()) {
|
|
|
|
// Zombie context: the canvas is already destroyed, but something else
|
|
|
|
// (typically the compositor) is still holding on to the context.
|
|
|
|
// Killing zombies is a no-brainer.
|
|
|
|
const_cast<WebGLContext*>(contexts[i])->LoseContext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
numContexts++;
|
|
|
|
if (contexts[i]->mLastUseIndex < oldestIndex) {
|
|
|
|
oldestIndex = contexts[i]->mLastUseIndex;
|
|
|
|
oldestContext = contexts[i];
|
|
|
|
}
|
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
nsIPrincipal* ourPrincipal = GetCanvas()->NodePrincipal();
|
|
|
|
nsIPrincipal* theirPrincipal = contexts[i]->GetCanvas()->NodePrincipal();
|
2012-08-03 01:28:02 +04:00
|
|
|
bool samePrincipal;
|
|
|
|
nsresult rv = ourPrincipal->Equals(theirPrincipal, &samePrincipal);
|
|
|
|
if (NS_SUCCEEDED(rv) && samePrincipal) {
|
|
|
|
numContextsThisPrincipal++;
|
|
|
|
if (contexts[i]->mLastUseIndex < oldestIndexThisPrincipal) {
|
|
|
|
oldestIndexThisPrincipal = contexts[i]->mLastUseIndex;
|
|
|
|
oldestContextThisPrincipal = contexts[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numContextsThisPrincipal > kMaxWebGLContextsPerPrincipal) {
|
|
|
|
GenerateWarning("Exceeded %d live WebGL contexts for this principal, losing the "
|
|
|
|
"least recently used one.", kMaxWebGLContextsPerPrincipal);
|
|
|
|
MOZ_ASSERT(oldestContextThisPrincipal); // if we reach this point, this can't be null
|
|
|
|
const_cast<WebGLContext*>(oldestContextThisPrincipal)->LoseContext();
|
|
|
|
} else if (numContexts > kMaxWebGLContexts) {
|
|
|
|
GenerateWarning("Exceeded %d live WebGL contexts, losing the least recently used one.",
|
|
|
|
kMaxWebGLContexts);
|
|
|
|
MOZ_ASSERT(oldestContext); // if we reach this point, this can't be null
|
|
|
|
const_cast<WebGLContext*>(oldestContext)->LoseContext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 21:31:12 +03:00
|
|
|
UniquePtr<uint8_t[]>
|
|
|
|
WebGLContext::GetImageBuffer(int32_t* out_format)
|
2009-09-03 04:47:49 +04:00
|
|
|
{
|
2014-11-14 07:03:50 +03:00
|
|
|
*out_format = 0;
|
2013-09-09 18:56:00 +04:00
|
|
|
|
2014-04-17 09:31:44 +04:00
|
|
|
// Use GetSurfaceSnapshot() to make sure that appropriate y-flip gets applied
|
|
|
|
bool premult;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> snapshot =
|
2014-04-17 09:31:44 +04:00
|
|
|
GetSurfaceSnapshot(mOptions.premultipliedAlpha ? nullptr : &premult);
|
2015-10-26 21:31:12 +03:00
|
|
|
if (!snapshot) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-14 07:03:50 +03:00
|
|
|
|
2014-04-17 09:31:44 +04:00
|
|
|
MOZ_ASSERT(mOptions.premultipliedAlpha || !premult, "We must get unpremult when we ask for it!");
|
2013-10-08 23:00:17 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> dataSurface = snapshot->GetDataSurface();
|
2014-04-17 09:31:44 +04:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
return gfxUtils::GetImageBuffer(dataSurface, mOptions.premultipliedAlpha,
|
2015-10-26 21:31:12 +03:00
|
|
|
out_format);
|
2013-10-17 06:55:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLContext::GetInputStream(const char* mimeType,
|
|
|
|
const char16_t* encoderOptions,
|
|
|
|
nsIInputStream** out_stream)
|
2013-10-17 06:55:08 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(gl, "GetInputStream on invalid context?");
|
|
|
|
if (!gl)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
// Use GetSurfaceSnapshot() to make sure that appropriate y-flip gets applied
|
|
|
|
bool premult;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> snapshot =
|
2015-10-12 06:21:03 +03:00
|
|
|
GetSurfaceSnapshot(mOptions.premultipliedAlpha ? nullptr : &premult);
|
|
|
|
if (!snapshot)
|
2013-10-17 06:55:08 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-09-03 04:47:49 +04:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
MOZ_ASSERT(mOptions.premultipliedAlpha || !premult, "We must get unpremult when we ask for it!");
|
2013-11-21 04:55:53 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> dataSurface = snapshot->GetDataSurface();
|
2015-10-12 06:21:03 +03:00
|
|
|
return gfxUtils::GetInputStream(dataSurface, mOptions.premultipliedAlpha, mimeType,
|
|
|
|
encoderOptions, out_stream);
|
2009-09-03 04:47:49 +04:00
|
|
|
}
|
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
void
|
|
|
|
WebGLContext::UpdateLastUseIndex()
|
2012-08-03 01:28:02 +04:00
|
|
|
{
|
|
|
|
static CheckedInt<uint64_t> sIndex = 0;
|
|
|
|
|
|
|
|
sIndex++;
|
|
|
|
|
|
|
|
// should never happen with 64-bit; trying to handle this would be riskier than
|
|
|
|
// not handling it as the handler code would never get exercised.
|
2014-11-14 07:03:50 +03:00
|
|
|
if (!sIndex.isValid())
|
2016-12-03 00:46:53 +03:00
|
|
|
MOZ_CRASH("Can't believe it's been 2^64 transactions already!");
|
2012-08-03 01:28:02 +04:00
|
|
|
mLastUseIndex = sIndex.value();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint8_t gWebGLLayerUserData;
|
2016-07-04 23:11:07 +03:00
|
|
|
static uint8_t gWebGLMirrorLayerUserData;
|
2010-07-16 01:07:46 +04:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
class WebGLContextUserData : public LayerUserData
|
|
|
|
{
|
2011-03-28 03:59:47 +04:00
|
|
|
public:
|
2014-11-14 07:03:50 +03:00
|
|
|
explicit WebGLContextUserData(HTMLCanvasElement* canvas)
|
|
|
|
: mCanvas(canvas)
|
2013-02-14 03:26:24 +04:00
|
|
|
{}
|
2011-11-01 01:14:12 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
/* PreTransactionCallback gets called by the Layers code every time the
|
|
|
|
* WebGL canvas is going to be composited.
|
|
|
|
*/
|
2014-11-14 07:03:50 +03:00
|
|
|
static void PreTransactionCallback(void* data) {
|
2013-02-14 03:26:24 +04:00
|
|
|
WebGLContextUserData* userdata = static_cast<WebGLContextUserData*>(data);
|
2014-11-14 07:03:50 +03:00
|
|
|
HTMLCanvasElement* canvas = userdata->mCanvas;
|
|
|
|
WebGLContext* webgl = static_cast<WebGLContext*>(canvas->GetContextAtIndex(0));
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2016-08-20 00:44:37 +03:00
|
|
|
// Prepare the context for composition
|
|
|
|
webgl->BeginComposition();
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** DidTransactionCallback gets called by the Layers code everytime the WebGL canvas gets composite,
|
|
|
|
* so it really is the right place to put actions that have to be performed upon compositing
|
|
|
|
*/
|
2014-11-14 07:03:50 +03:00
|
|
|
static void DidTransactionCallback(void* data) {
|
|
|
|
WebGLContextUserData* userdata = static_cast<WebGLContextUserData*>(data);
|
|
|
|
HTMLCanvasElement* canvas = userdata->mCanvas;
|
|
|
|
WebGLContext* webgl = static_cast<WebGLContext*>(canvas->GetContextAtIndex(0));
|
2011-11-01 01:14:12 +04:00
|
|
|
|
2016-08-20 00:44:37 +03:00
|
|
|
// Clean up the context after composition
|
|
|
|
webgl->EndComposition();
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
2011-03-28 03:59:47 +04:00
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HTMLCanvasElement> mCanvas;
|
2011-03-28 03:59:47 +04:00
|
|
|
};
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
already_AddRefed<layers::Layer>
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLContext::GetCanvasLayer(nsDisplayListBuilder* builder,
|
2015-12-18 09:52:17 +03:00
|
|
|
Layer* oldLayer,
|
2016-07-04 23:11:07 +03:00
|
|
|
LayerManager* manager,
|
|
|
|
bool aMirror /*= false*/)
|
2010-05-18 08:04:22 +04:00
|
|
|
{
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-10-27 00:00:44 +04:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
if (!mResetLayer && oldLayer &&
|
2016-07-04 23:11:07 +03:00
|
|
|
oldLayer->HasUserData(aMirror ? &gWebGLMirrorLayerUserData : &gWebGLLayerUserData)) {
|
2015-12-18 09:52:17 +03:00
|
|
|
RefPtr<layers::Layer> ret = oldLayer;
|
2013-04-22 15:15:59 +04:00
|
|
|
return ret.forget();
|
2010-07-16 01:07:46 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CanvasLayer> canvasLayer = manager->CreateCanvasLayer();
|
2010-05-18 08:04:22 +04:00
|
|
|
if (!canvasLayer) {
|
|
|
|
NS_WARNING("CreateCanvasLayer returned null!");
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-09-03 04:47:49 +04:00
|
|
|
}
|
2014-11-14 07:03:50 +03:00
|
|
|
|
|
|
|
WebGLContextUserData* userData = nullptr;
|
2016-07-04 23:11:07 +03:00
|
|
|
if (builder->IsPaintingToWindow() && mCanvasElement && !aMirror) {
|
2015-10-12 06:21:03 +03:00
|
|
|
// Make the layer tell us whenever a transaction finishes (including
|
|
|
|
// the current transaction), so we can clear our invalidation state and
|
|
|
|
// start invalidating again. We need to do this for the layer that is
|
|
|
|
// being painted to a window (there shouldn't be more than one at a time,
|
|
|
|
// and if there is, flushing the invalidation state more often than
|
|
|
|
// necessary is harmless).
|
|
|
|
|
|
|
|
// The layer will be destroyed when we tear down the presentation
|
|
|
|
// (at the latest), at which time this userData will be destroyed,
|
|
|
|
// releasing the reference to the element.
|
|
|
|
// The userData will receive DidTransactionCallbacks, which flush the
|
|
|
|
// the invalidation state to indicate that the canvas is up to date.
|
|
|
|
userData = new WebGLContextUserData(mCanvasElement);
|
|
|
|
canvasLayer->SetDidTransactionCallback(
|
|
|
|
WebGLContextUserData::DidTransactionCallback, userData);
|
|
|
|
canvasLayer->SetPreTransactionCallback(
|
|
|
|
WebGLContextUserData::PreTransactionCallback, userData);
|
2011-03-28 03:59:47 +04:00
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
|
2016-07-04 23:11:07 +03:00
|
|
|
canvasLayer->SetUserData(aMirror ? &gWebGLMirrorLayerUserData : &gWebGLLayerUserData, userData);
|
2009-09-03 04:47:49 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
CanvasLayer::Data data;
|
2013-02-14 03:26:24 +04:00
|
|
|
data.mGLContext = gl;
|
2010-05-18 08:04:22 +04:00
|
|
|
data.mSize = nsIntSize(mWidth, mHeight);
|
2014-09-10 03:57:12 +04:00
|
|
|
data.mHasAlpha = gl->Caps().alpha;
|
2014-06-19 04:04:06 +04:00
|
|
|
data.mIsGLAlphaPremult = IsPremultAlpha() || !data.mHasAlpha;
|
2016-07-04 23:11:07 +03:00
|
|
|
data.mIsMirror = aMirror;
|
2010-05-18 08:04:22 +04:00
|
|
|
|
|
|
|
canvasLayer->Initialize(data);
|
2014-09-10 03:57:12 +04:00
|
|
|
uint32_t flags = gl->Caps().alpha ? 0 : Layer::CONTENT_OPAQUE;
|
|
|
|
canvasLayer->SetContentFlags(flags);
|
2011-03-28 03:59:46 +04:00
|
|
|
canvasLayer->Updated();
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mResetLayer = false;
|
2016-09-09 00:16:07 +03:00
|
|
|
// We only wish to update mLayerIsMirror when a new layer is returned.
|
|
|
|
// If a cached layer is returned above, aMirror is not changing since
|
|
|
|
// the last cached layer was created and mLayerIsMirror is still valid.
|
|
|
|
mLayerIsMirror = aMirror;
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2013-04-22 15:15:59 +04:00
|
|
|
return canvasLayer.forget();
|
2009-09-03 04:47:49 +04:00
|
|
|
}
|
|
|
|
|
2015-08-18 01:10:12 +03:00
|
|
|
layers::LayersBackend
|
|
|
|
WebGLContext::GetCompositorBackendType() const
|
|
|
|
{
|
2015-10-12 06:21:03 +03:00
|
|
|
if (mCanvasElement) {
|
|
|
|
return mCanvasElement->GetCompositorBackendType();
|
|
|
|
} else if (mOffscreenCanvas) {
|
|
|
|
return mOffscreenCanvas->GetCompositorBackendType();
|
2015-08-18 01:10:12 +03:00
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
|
2015-08-18 01:10:12 +03:00
|
|
|
return LayersBackend::LAYERS_NONE;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void
|
|
|
|
WebGLContext::Commit()
|
|
|
|
{
|
|
|
|
if (mOffscreenCanvas) {
|
|
|
|
mOffscreenCanvas->CommitFrameToCompositor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::GetCanvas(Nullable<dom::OwningHTMLCanvasElementOrOffscreenCanvas>& retval)
|
|
|
|
{
|
|
|
|
if (mCanvasElement) {
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mOffscreenCanvas, "GFX: Canvas is offscreen.");
|
2015-11-15 16:48:32 +03:00
|
|
|
|
|
|
|
if (mCanvasElement->IsInNativeAnonymousSubtree()) {
|
|
|
|
retval.SetNull();
|
|
|
|
} else {
|
|
|
|
retval.SetValue().SetAsHTMLCanvasElement() = mCanvasElement;
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
} else if (mOffscreenCanvas) {
|
|
|
|
retval.SetValue().SetAsOffscreenCanvas() = mOffscreenCanvas;
|
|
|
|
} else {
|
|
|
|
retval.SetNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 21:08:52 +04:00
|
|
|
void
|
2015-07-15 03:37:28 +03:00
|
|
|
WebGLContext::GetContextAttributes(dom::Nullable<dom::WebGLContextAttributes>& retval)
|
2010-11-17 07:33:03 +03:00
|
|
|
{
|
2012-12-01 03:30:05 +04:00
|
|
|
retval.SetNull();
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2012-10-22 21:08:52 +04:00
|
|
|
return;
|
2012-12-01 03:30:05 +04:00
|
|
|
|
|
|
|
dom::WebGLContextAttributes& result = retval.SetValue();
|
2010-11-17 07:33:03 +03:00
|
|
|
|
2014-10-09 03:32:50 +04:00
|
|
|
result.mAlpha.Construct(mOptions.alpha);
|
|
|
|
result.mDepth = mOptions.depth;
|
|
|
|
result.mStencil = mOptions.stencil;
|
|
|
|
result.mAntialias = mOptions.antialias;
|
2012-12-18 01:44:04 +04:00
|
|
|
result.mPremultipliedAlpha = mOptions.premultipliedAlpha;
|
|
|
|
result.mPreserveDrawingBuffer = mOptions.preserveDrawingBuffer;
|
2015-05-21 18:51:49 +03:00
|
|
|
result.mFailIfMajorPerformanceCaveat = mOptions.failIfMajorPerformanceCaveat;
|
2010-11-17 07:33:03 +03:00
|
|
|
}
|
|
|
|
|
2010-12-16 23:49:54 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
WebGLContext::MozGetUnderlyingParamString(uint32_t pname, nsAString& retval)
|
2010-12-16 23:49:54 +03:00
|
|
|
{
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2011-10-27 00:00:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
retval.SetIsVoid(true);
|
2010-12-16 23:49:54 +03:00
|
|
|
|
|
|
|
MakeContextCurrent();
|
|
|
|
|
|
|
|
switch (pname) {
|
|
|
|
case LOCAL_GL_VENDOR:
|
|
|
|
case LOCAL_GL_RENDERER:
|
|
|
|
case LOCAL_GL_VERSION:
|
|
|
|
case LOCAL_GL_SHADING_LANGUAGE_VERSION:
|
2014-11-14 07:03:50 +03:00
|
|
|
case LOCAL_GL_EXTENSIONS:
|
|
|
|
{
|
|
|
|
const char* s = (const char*)gl->fGetString(pname);
|
|
|
|
retval.Assign(NS_ConvertASCIItoUTF16(nsDependentCString(s)));
|
|
|
|
break;
|
|
|
|
}
|
2010-12-16 23:49:54 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-20 23:53:53 +04:00
|
|
|
void
|
2013-02-14 03:26:24 +04:00
|
|
|
WebGLContext::ClearScreen()
|
|
|
|
{
|
|
|
|
MakeContextCurrent();
|
|
|
|
ScopedBindFramebuffer autoFB(gl, 0);
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const bool changeDrawBuffers = (mDefaultFB_DrawBuffer0 != LOCAL_GL_BACK);
|
|
|
|
if (changeDrawBuffers) {
|
2016-01-26 23:42:39 +03:00
|
|
|
gl->Screen()->SetDrawBuffer(LOCAL_GL_BACK);
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GLbitfield bufferBits = LOCAL_GL_COLOR_BUFFER_BIT;
|
2013-02-14 03:26:24 +04:00
|
|
|
if (mOptions.depth)
|
2015-11-25 07:15:29 +03:00
|
|
|
bufferBits |= LOCAL_GL_DEPTH_BUFFER_BIT;
|
2013-02-14 03:26:24 +04:00
|
|
|
if (mOptions.stencil)
|
2015-11-25 07:15:29 +03:00
|
|
|
bufferBits |= LOCAL_GL_STENCIL_BUFFER_BIT;
|
2013-02-14 03:26:24 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
ForceClearFramebufferWithDefaultValues(bufferBits, mNeedsFakeNoAlpha);
|
2013-06-22 03:44:17 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
if (changeDrawBuffers) {
|
2016-01-26 23:42:39 +03:00
|
|
|
gl->Screen()->SetDrawBuffer(mDefaultFB_DrawBuffer0);
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-11-25 07:15:29 +03:00
|
|
|
WebGLContext::ForceClearFramebufferWithDefaultValues(GLbitfield clearBits,
|
|
|
|
bool fakeNoAlpha)
|
2011-05-20 23:53:53 +04:00
|
|
|
{
|
|
|
|
MakeContextCurrent();
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const bool initializeColorBuffer = bool(clearBits & LOCAL_GL_COLOR_BUFFER_BIT);
|
|
|
|
const bool initializeDepthBuffer = bool(clearBits & LOCAL_GL_DEPTH_BUFFER_BIT);
|
|
|
|
const bool initializeStencilBuffer = bool(clearBits & LOCAL_GL_STENCIL_BUFFER_BIT);
|
2011-05-20 23:53:53 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// Fun GL fact: No need to worry about the viewport here, glViewport is just
|
|
|
|
// setting up a coordinates transformation, it doesn't affect glClear at all.
|
2016-06-01 00:44:46 +03:00
|
|
|
AssertCachedGlobalState();
|
2013-02-14 03:26:24 +04:00
|
|
|
|
|
|
|
// Prepare GL state for clearing.
|
2011-05-20 23:53:53 +04:00
|
|
|
gl->fDisable(LOCAL_GL_SCISSOR_TEST);
|
|
|
|
|
|
|
|
if (initializeColorBuffer) {
|
|
|
|
gl->fColorMask(1, 1, 1, 1);
|
2014-10-02 04:05:34 +04:00
|
|
|
|
2015-05-01 03:30:26 +03:00
|
|
|
if (fakeNoAlpha) {
|
2014-10-02 04:05:34 +04:00
|
|
|
gl->fClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
} else {
|
|
|
|
gl->fClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
2011-05-20 23:53:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (initializeDepthBuffer) {
|
|
|
|
gl->fDepthMask(1);
|
|
|
|
gl->fClearDepth(1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initializeStencilBuffer) {
|
2013-02-14 03:26:24 +04:00
|
|
|
// "The clear operation always uses the front stencil write mask
|
|
|
|
// when clearing the stencil buffer."
|
|
|
|
gl->fStencilMaskSeparate(LOCAL_GL_FRONT, 0xffffffff);
|
|
|
|
gl->fStencilMaskSeparate(LOCAL_GL_BACK, 0xffffffff);
|
2011-05-20 23:53:53 +04:00
|
|
|
gl->fClearStencil(0);
|
|
|
|
}
|
|
|
|
|
2013-08-23 04:11:40 +04:00
|
|
|
if (mRasterizerDiscardEnabled) {
|
|
|
|
gl->fDisable(LOCAL_GL_RASTERIZER_DISCARD);
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// Do the clear!
|
2015-11-25 07:15:29 +03:00
|
|
|
gl->fClear(clearBits);
|
2011-05-20 23:53:53 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// And reset!
|
|
|
|
if (mScissorTestEnabled)
|
|
|
|
gl->fEnable(LOCAL_GL_SCISSOR_TEST);
|
|
|
|
|
2013-08-23 04:11:40 +04:00
|
|
|
if (mRasterizerDiscardEnabled) {
|
|
|
|
gl->fEnable(LOCAL_GL_RASTERIZER_DISCARD);
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// Restore GL state after clearing.
|
2011-05-20 23:53:53 +04:00
|
|
|
if (initializeColorBuffer) {
|
|
|
|
gl->fColorMask(mColorWriteMask[0],
|
|
|
|
mColorWriteMask[1],
|
|
|
|
mColorWriteMask[2],
|
|
|
|
mColorWriteMask[3]);
|
|
|
|
gl->fClearColor(mColorClearValue[0],
|
|
|
|
mColorClearValue[1],
|
|
|
|
mColorClearValue[2],
|
|
|
|
mColorClearValue[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initializeDepthBuffer) {
|
|
|
|
gl->fDepthMask(mDepthWriteMask);
|
|
|
|
gl->fClearDepth(mDepthClearValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initializeStencilBuffer) {
|
2011-05-20 23:53:53 +04:00
|
|
|
gl->fStencilMaskSeparate(LOCAL_GL_FRONT, mStencilWriteMaskFront);
|
2013-02-14 03:26:24 +04:00
|
|
|
gl->fStencilMaskSeparate(LOCAL_GL_BACK, mStencilWriteMaskBack);
|
2011-05-20 23:53:53 +04:00
|
|
|
gl->fClearStencil(mStencilClearValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// For an overview of how WebGL compositing works, see:
|
|
|
|
// https://wiki.mozilla.org/Platform/GFX/WebGL/Compositing
|
|
|
|
bool
|
|
|
|
WebGLContext::PresentScreenBuffer()
|
2011-05-20 23:53:53 +04:00
|
|
|
{
|
2013-11-24 06:21:45 +04:00
|
|
|
if (IsContextLost()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
if (!mShouldPresent) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-02 02:19:09 +03:00
|
|
|
MOZ_ASSERT(!mBackbufferNeedsClear);
|
2011-05-20 23:53:53 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
gl->MakeCurrent();
|
2014-10-08 08:11:54 +04:00
|
|
|
|
2014-10-09 00:04:19 +04:00
|
|
|
GLScreenBuffer* screen = gl->Screen();
|
2014-10-08 08:11:54 +04:00
|
|
|
MOZ_ASSERT(screen);
|
|
|
|
|
2014-10-09 00:04:19 +04:00
|
|
|
if (!screen->PublishFrame(screen->Size())) {
|
2014-08-28 03:16:22 +04:00
|
|
|
ForceLoseContext();
|
2013-02-14 03:26:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-20 23:53:53 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
if (!mOptions.preserveDrawingBuffer) {
|
2014-04-18 00:15:41 +04:00
|
|
|
mBackbufferNeedsClear = true;
|
2013-02-14 03:26:24 +04:00
|
|
|
}
|
2013-02-20 19:01:20 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
mShouldPresent = false;
|
2013-02-21 23:54:25 +04:00
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
return true;
|
2011-05-20 23:53:53 +04:00
|
|
|
}
|
|
|
|
|
2016-08-20 00:44:37 +03:00
|
|
|
// Prepare the context for capture before compositing
|
|
|
|
void
|
|
|
|
WebGLContext::BeginComposition()
|
|
|
|
{
|
|
|
|
// Present our screenbuffer, if needed.
|
|
|
|
PresentScreenBuffer();
|
|
|
|
mDrawCallsSinceLastFlush = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the context after captured for compositing
|
|
|
|
void
|
|
|
|
WebGLContext::EndComposition()
|
|
|
|
{
|
|
|
|
// Mark ourselves as no longer invalidated.
|
|
|
|
MarkContextClean();
|
|
|
|
UpdateLastUseIndex();
|
|
|
|
}
|
|
|
|
|
2012-05-04 20:38:44 +04:00
|
|
|
void
|
2015-12-15 03:11:59 +03:00
|
|
|
WebGLContext::DummyReadFramebufferOperation(const char* funcName)
|
2012-01-25 01:12:31 +04:00
|
|
|
{
|
2015-12-15 03:11:59 +03:00
|
|
|
if (!mBoundReadFramebuffer)
|
|
|
|
return; // Infallible.
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
const auto status = mBoundReadFramebuffer->CheckFramebufferStatus(funcName);
|
2015-12-15 03:11:59 +03:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
if (status != LOCAL_GL_FRAMEBUFFER_COMPLETE) {
|
2016-07-19 10:36:54 +03:00
|
|
|
ErrorInvalidFramebufferOperation("%s: Framebuffer must be complete.",
|
|
|
|
funcName);
|
2014-11-14 07:03:50 +03:00
|
|
|
}
|
2012-01-25 01:12:31 +04:00
|
|
|
}
|
|
|
|
|
2016-07-17 20:37:00 +03:00
|
|
|
bool
|
2016-10-04 04:33:52 +03:00
|
|
|
WebGLContext::Has64BitTimestamps() const
|
2016-07-17 20:37:00 +03:00
|
|
|
{
|
|
|
|
// 'sync' provides glGetInteger64v either by supporting ARB_sync, GL3+, or GLES3+.
|
|
|
|
return gl->IsSupported(GLFeature::sync);
|
|
|
|
}
|
|
|
|
|
2014-06-03 01:40:15 +04:00
|
|
|
static bool
|
2014-11-14 07:03:50 +03:00
|
|
|
CheckContextLost(GLContext* gl, bool* const out_isGuilty)
|
2014-06-03 01:40:15 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(gl);
|
|
|
|
MOZ_ASSERT(out_isGuilty);
|
|
|
|
|
|
|
|
bool isEGL = gl->GetContextType() == gl::GLContextType::EGL;
|
|
|
|
|
|
|
|
GLenum resetStatus = LOCAL_GL_NO_ERROR;
|
2016-04-22 02:32:18 +03:00
|
|
|
if (gl->IsSupported(GLFeature::robustness)) {
|
2014-06-03 01:40:15 +04:00
|
|
|
gl->MakeCurrent();
|
|
|
|
resetStatus = gl->fGetGraphicsResetStatus();
|
|
|
|
} else if (isEGL) {
|
|
|
|
// Simulate a ARB_robustness guilty context loss for when we
|
|
|
|
// get an EGL_CONTEXT_LOST error. It may not actually be guilty,
|
|
|
|
// but we can't make any distinction.
|
|
|
|
if (!gl->MakeCurrent(true) && gl->IsContextLost()) {
|
|
|
|
resetStatus = LOCAL_GL_UNKNOWN_CONTEXT_RESET_ARB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resetStatus == LOCAL_GL_NO_ERROR) {
|
|
|
|
*out_isGuilty = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume guilty unless we find otherwise!
|
|
|
|
bool isGuilty = true;
|
|
|
|
switch (resetStatus) {
|
|
|
|
case LOCAL_GL_INNOCENT_CONTEXT_RESET_ARB:
|
|
|
|
// Either nothing wrong, or not our fault.
|
|
|
|
isGuilty = false;
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_GUILTY_CONTEXT_RESET_ARB:
|
|
|
|
NS_WARNING("WebGL content on the page definitely caused the graphics"
|
|
|
|
" card to reset.");
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_UNKNOWN_CONTEXT_RESET_ARB:
|
|
|
|
NS_WARNING("WebGL content on the page might have caused the graphics"
|
|
|
|
" card to reset");
|
|
|
|
// If we can't tell, assume guilty.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unreachable.");
|
|
|
|
// If we do get here, let's pretend to be guilty as an escape plan.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isGuilty) {
|
|
|
|
NS_WARNING("WebGL context on this page is considered guilty, and will"
|
|
|
|
" not be restored.");
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_isGuilty = isGuilty;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebGLContext::TryToRestoreContext()
|
|
|
|
{
|
|
|
|
if (NS_FAILED(SetDimensions(mWidth, mHeight)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-22 20:55:00 +04:00
|
|
|
void
|
|
|
|
WebGLContext::RunContextLossTimer()
|
|
|
|
{
|
2016-06-16 20:01:44 +03:00
|
|
|
mContextLossHandler.RunTimer();
|
2014-08-22 20:55:00 +04:00
|
|
|
}
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
class UpdateContextLossStatusTask : public CancelableRunnable
|
2014-06-03 01:40:15 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WebGLContext> mWebGL;
|
2014-06-03 01:40:15 +04:00
|
|
|
|
|
|
|
public:
|
2014-11-14 07:03:50 +03:00
|
|
|
explicit UpdateContextLossStatusTask(WebGLContext* webgl)
|
|
|
|
: mWebGL(webgl)
|
2014-06-03 01:40:15 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2015-10-12 06:21:03 +03:00
|
|
|
if (mWebGL)
|
|
|
|
mWebGL->UpdateContextLossStatus();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-10-05 05:50:16 +03:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult Cancel() override {
|
2015-10-12 06:21:03 +03:00
|
|
|
mWebGL = nullptr;
|
2015-10-05 05:50:16 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-06-03 01:40:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::EnqueueUpdateContextLossStatus()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> task = new UpdateContextLossStatusTask(this);
|
|
|
|
NS_DispatchToCurrentThread(task);
|
|
|
|
}
|
|
|
|
|
2012-01-05 01:12:03 +04:00
|
|
|
// We use this timer for many things. Here are the things that it is activated for:
|
|
|
|
// 1) If a script is using the MOZ_WEBGL_lose_context extension.
|
|
|
|
// 2) If we are using EGL and _NOT ANGLE_, we query periodically to see if the
|
|
|
|
// CONTEXT_LOST_WEBGL error has been triggered.
|
|
|
|
// 3) If we are using ANGLE, or anything that supports ARB_robustness, query the
|
|
|
|
// GPU periodically to see if the reset status bit has been set.
|
|
|
|
// In all of these situations, we use this timer to send the script context lost
|
|
|
|
// and restored events asynchronously. For example, if it triggers a context loss,
|
|
|
|
// the webglcontextlost event will be sent to it the next time the robustness timer
|
|
|
|
// fires.
|
|
|
|
// Note that this timer mechanism is not used unless one of these 3 criteria
|
|
|
|
// are met.
|
|
|
|
// At a bare minimum, from context lost to context restores, it would take 3
|
|
|
|
// full timer iterations: detection, webglcontextlost, webglcontextrestored.
|
2012-10-23 20:21:23 +04:00
|
|
|
void
|
2014-06-03 01:40:15 +04:00
|
|
|
WebGLContext::UpdateContextLossStatus()
|
2011-10-27 00:00:44 +04:00
|
|
|
{
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!mCanvasElement && !mOffscreenCanvas) {
|
2012-04-22 00:48:22 +04:00
|
|
|
// the canvas is gone. That happens when the page was closed before we got
|
|
|
|
// this timer event. In this case, there's nothing to do here, just don't crash.
|
2012-10-23 20:21:23 +04:00
|
|
|
return;
|
2012-04-22 00:48:22 +04:00
|
|
|
}
|
2014-06-03 01:40:15 +04:00
|
|
|
if (mContextStatus == ContextNotLost) {
|
|
|
|
// We don't know that we're lost, but we might be, so we need to
|
|
|
|
// check. If we're guilty, don't allow restores, though.
|
|
|
|
|
|
|
|
bool isGuilty = true;
|
2014-06-03 01:40:46 +04:00
|
|
|
MOZ_ASSERT(gl); // Shouldn't be missing gl if we're NotLost.
|
2014-06-03 01:40:15 +04:00
|
|
|
bool isContextLost = CheckContextLost(gl, &isGuilty);
|
|
|
|
|
|
|
|
if (isContextLost) {
|
|
|
|
if (isGuilty)
|
|
|
|
mAllowContextRestore = false;
|
|
|
|
|
|
|
|
ForceLoseContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall through.
|
|
|
|
}
|
2012-04-22 00:48:22 +04:00
|
|
|
|
2012-01-05 01:12:03 +04:00
|
|
|
if (mContextStatus == ContextLostAwaitingEvent) {
|
2014-06-03 01:40:15 +04:00
|
|
|
// The context has been lost and we haven't yet triggered the
|
|
|
|
// callback, so do that now.
|
2016-05-12 02:45:43 +03:00
|
|
|
const auto kEventName = NS_LITERAL_STRING("webglcontextlost");
|
|
|
|
const bool kCanBubble = true;
|
|
|
|
const bool kIsCancelable = true;
|
2014-06-03 01:40:15 +04:00
|
|
|
bool useDefaultHandler;
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
if (mCanvasElement) {
|
|
|
|
nsContentUtils::DispatchTrustedEvent(
|
|
|
|
mCanvasElement->OwnerDoc(),
|
|
|
|
static_cast<nsIDOMHTMLCanvasElement*>(mCanvasElement),
|
2016-05-12 02:45:43 +03:00
|
|
|
kEventName,
|
|
|
|
kCanBubble,
|
|
|
|
kIsCancelable,
|
2015-10-12 06:21:03 +03:00
|
|
|
&useDefaultHandler);
|
|
|
|
} else {
|
|
|
|
// OffscreenCanvas case
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Event> event = new Event(mOffscreenCanvas, nullptr, nullptr);
|
2016-05-12 02:45:43 +03:00
|
|
|
event->InitEvent(kEventName, kCanBubble, kIsCancelable);
|
2015-10-12 06:21:03 +03:00
|
|
|
event->SetTrusted(true);
|
|
|
|
mOffscreenCanvas->DispatchEvent(event, &useDefaultHandler);
|
|
|
|
}
|
|
|
|
|
2014-06-03 01:40:15 +04:00
|
|
|
// We sent the callback, so we're just 'regular lost' now.
|
|
|
|
mContextStatus = ContextLost;
|
|
|
|
// If we're told to use the default handler, it means the script
|
|
|
|
// didn't bother to handle the event. In this case, we shouldn't
|
|
|
|
// auto-restore the context.
|
|
|
|
if (useDefaultHandler)
|
|
|
|
mAllowContextRestore = false;
|
|
|
|
|
|
|
|
// Fall through.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mContextStatus == ContextLost) {
|
|
|
|
// Context is lost, and we've already sent the callback. We
|
|
|
|
// should try to restore the context if we're both allowed to,
|
|
|
|
// and supposed to.
|
|
|
|
|
|
|
|
// Are we allowed to restore the context?
|
|
|
|
if (!mAllowContextRestore)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If we're only simulated-lost, we shouldn't auto-restore, and
|
|
|
|
// instead we should wait for restoreContext() to be called.
|
|
|
|
if (mLastLossWasSimulated)
|
|
|
|
return;
|
|
|
|
|
2014-06-23 03:16:00 +04:00
|
|
|
// Restore when the app is visible
|
|
|
|
if (mRestoreWhenVisible)
|
|
|
|
return;
|
|
|
|
|
2014-06-03 01:40:15 +04:00
|
|
|
ForceRestoreContext();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mContextStatus == ContextLostAwaitingRestore) {
|
|
|
|
// Context is lost, but we should try to restore it.
|
|
|
|
|
|
|
|
if (!mAllowContextRestore) {
|
|
|
|
// We might decide this after thinking we'd be OK restoring
|
|
|
|
// the context, so downgrade.
|
2012-01-05 01:12:03 +04:00
|
|
|
mContextStatus = ContextLost;
|
2014-06-03 01:40:15 +04:00
|
|
|
return;
|
2012-01-05 01:12:03 +04:00
|
|
|
}
|
2014-06-03 01:40:15 +04:00
|
|
|
|
|
|
|
if (!TryToRestoreContext()) {
|
|
|
|
// Failed to restore. Try again later.
|
2016-06-16 20:01:44 +03:00
|
|
|
mContextLossHandler.RunTimer();
|
2012-10-23 20:21:23 +04:00
|
|
|
return;
|
2012-01-05 01:12:03 +04:00
|
|
|
}
|
2014-06-03 01:40:15 +04:00
|
|
|
|
|
|
|
// Revival!
|
2013-09-04 16:14:44 +04:00
|
|
|
mContextStatus = ContextNotLost;
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
if (mCanvasElement) {
|
|
|
|
nsContentUtils::DispatchTrustedEvent(
|
|
|
|
mCanvasElement->OwnerDoc(),
|
|
|
|
static_cast<nsIDOMHTMLCanvasElement*>(mCanvasElement),
|
|
|
|
NS_LITERAL_STRING("webglcontextrestored"),
|
|
|
|
true,
|
|
|
|
true);
|
|
|
|
} else {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Event> event = new Event(mOffscreenCanvas, nullptr, nullptr);
|
2015-10-12 06:21:03 +03:00
|
|
|
event->InitEvent(NS_LITERAL_STRING("webglcontextrestored"), true, true);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
bool unused;
|
|
|
|
mOffscreenCanvas->DispatchEvent(event, &unused);
|
|
|
|
}
|
|
|
|
|
2014-03-08 01:16:34 +04:00
|
|
|
mEmitContextLostErrorOnce = true;
|
2011-10-27 00:00:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 03:16:00 +04:00
|
|
|
WebGLContext::ForceLoseContext(bool simulateLosing)
|
2011-10-27 00:00:44 +04:00
|
|
|
{
|
2014-06-03 01:40:15 +04:00
|
|
|
printf_stderr("WebGL(%p)::ForceLoseContext\n", this);
|
|
|
|
MOZ_ASSERT(!IsContextLost());
|
2014-05-29 22:33:28 +04:00
|
|
|
mContextStatus = ContextLostAwaitingEvent;
|
2014-06-03 01:40:15 +04:00
|
|
|
mContextLostErrorSet = false;
|
|
|
|
|
|
|
|
// Burn it all!
|
2011-10-27 00:00:44 +04:00
|
|
|
DestroyResourcesAndContext();
|
2014-06-23 03:16:00 +04:00
|
|
|
mLastLossWasSimulated = simulateLosing;
|
|
|
|
|
2014-06-03 01:40:15 +04:00
|
|
|
// Queue up a task, since we know the status changed.
|
|
|
|
EnqueueUpdateContextLossStatus();
|
2011-10-27 00:00:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::ForceRestoreContext()
|
|
|
|
{
|
2014-06-03 01:40:15 +04:00
|
|
|
printf_stderr("WebGL(%p)::ForceRestoreContext\n", this);
|
2012-01-05 01:12:03 +04:00
|
|
|
mContextStatus = ContextLostAwaitingRestore;
|
2014-06-03 01:40:15 +04:00
|
|
|
mAllowContextRestore = true; // Hey, you did say 'force'.
|
|
|
|
|
|
|
|
// Queue up a task, since we know the status changed.
|
|
|
|
EnqueueUpdateContextLossStatus();
|
2011-10-27 00:00:44 +04:00
|
|
|
}
|
|
|
|
|
2013-09-04 16:14:52 +04:00
|
|
|
void
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLContext::MakeContextCurrent() const
|
|
|
|
{
|
|
|
|
gl->MakeCurrent();
|
|
|
|
}
|
2013-09-04 16:14:52 +04:00
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<mozilla::gfx::SourceSurface>
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLContext::GetSurfaceSnapshot(bool* out_premultAlpha)
|
2013-10-02 01:01:19 +04:00
|
|
|
{
|
2014-04-17 09:28:17 +04:00
|
|
|
if (!gl)
|
|
|
|
return nullptr;
|
|
|
|
|
2014-08-08 06:03:24 +04:00
|
|
|
bool hasAlpha = mOptions.alpha;
|
|
|
|
SurfaceFormat surfFormat = hasAlpha ? SurfaceFormat::B8G8R8A8
|
|
|
|
: SurfaceFormat::B8G8R8X8;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> surf;
|
2014-08-08 06:03:24 +04:00
|
|
|
surf = Factory::CreateDataSourceSurfaceWithStride(IntSize(mWidth, mHeight),
|
|
|
|
surfFormat,
|
|
|
|
mWidth * 4);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!surf)) {
|
2014-04-17 09:28:17 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->MakeCurrent();
|
2014-04-18 00:15:41 +04:00
|
|
|
{
|
|
|
|
ScopedBindFramebuffer autoFB(gl, 0);
|
|
|
|
ClearBackbufferIfNeeded();
|
2016-07-18 10:15:54 +03:00
|
|
|
|
|
|
|
// Save, override, then restore glReadBuffer.
|
|
|
|
const GLenum readBufferMode = gl->Screen()->GetReadBufferMode();
|
|
|
|
|
|
|
|
if (readBufferMode != LOCAL_GL_BACK) {
|
|
|
|
gl->fReadBuffer(LOCAL_GL_BACK);
|
|
|
|
}
|
2014-05-13 06:20:26 +04:00
|
|
|
ReadPixelsIntoDataSurface(gl, surf);
|
2016-07-18 10:15:54 +03:00
|
|
|
|
|
|
|
if (readBufferMode != LOCAL_GL_BACK) {
|
|
|
|
gl->fReadBuffer(readBufferMode);
|
|
|
|
}
|
2014-04-18 00:15:41 +04:00
|
|
|
}
|
2014-04-17 09:28:17 +04:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
if (out_premultAlpha) {
|
|
|
|
*out_premultAlpha = true;
|
2014-04-17 09:28:17 +04:00
|
|
|
}
|
|
|
|
bool srcPremultAlpha = mOptions.premultipliedAlpha;
|
|
|
|
if (!srcPremultAlpha) {
|
2014-11-14 07:03:50 +03:00
|
|
|
if (out_premultAlpha) {
|
|
|
|
*out_premultAlpha = false;
|
2015-05-30 02:27:58 +03:00
|
|
|
} else if(hasAlpha) {
|
2014-06-19 04:04:05 +04:00
|
|
|
gfxUtils::PremultiplyDataSurface(surf, surf);
|
2014-04-17 09:28:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DrawTarget> dt =
|
2014-04-17 09:28:17 +04:00
|
|
|
Factory::CreateDrawTarget(BackendType::CAIRO,
|
|
|
|
IntSize(mWidth, mHeight),
|
|
|
|
SurfaceFormat::B8G8R8A8);
|
|
|
|
|
|
|
|
if (!dt) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-10 21:29:35 +04:00
|
|
|
dt->SetTransform(Matrix::Translation(0.0, mHeight).PreScale(1.0, -1.0));
|
2014-04-17 09:28:17 +04:00
|
|
|
|
2014-05-13 06:20:26 +04:00
|
|
|
dt->DrawSurface(surf,
|
2014-04-17 09:28:17 +04:00
|
|
|
Rect(0, 0, mWidth, mHeight),
|
|
|
|
Rect(0, 0, mWidth, mHeight),
|
|
|
|
DrawSurfaceOptions(),
|
|
|
|
DrawOptions(1.0f, CompositionOp::OP_SOURCE));
|
|
|
|
|
|
|
|
return dt->Snapshot();
|
2013-10-02 01:01:19 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 05:40:51 +04:00
|
|
|
void
|
|
|
|
WebGLContext::DidRefresh()
|
|
|
|
{
|
|
|
|
if (gl) {
|
|
|
|
gl->FlushIfHeavyGLCallsSinceLastFlush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
bool
|
|
|
|
WebGLContext::ValidateCurFBForRead(const char* funcName,
|
|
|
|
const webgl::FormatUsageInfo** const out_format,
|
2016-07-13 10:58:16 +03:00
|
|
|
uint32_t* const out_width, uint32_t* const out_height)
|
2015-02-25 01:09:09 +03:00
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
if (!mBoundReadFramebuffer) {
|
2016-05-27 04:07:52 +03:00
|
|
|
const GLenum readBufferMode = gl->Screen()->GetReadBufferMode();
|
|
|
|
if (readBufferMode == LOCAL_GL_NONE) {
|
|
|
|
ErrorInvalidOperation("%s: Can't read from backbuffer when readBuffer mode is"
|
|
|
|
" NONE.",
|
|
|
|
funcName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
ClearBackbufferIfNeeded();
|
2015-11-24 06:27:13 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
// FIXME - here we're assuming that the default framebuffer is backed by
|
|
|
|
// UNSIGNED_BYTE that might not always be true, say if we had a 16bpp default
|
|
|
|
// framebuffer.
|
|
|
|
auto effFormat = mOptions.alpha ? webgl::EffectiveFormat::RGBA8
|
|
|
|
: webgl::EffectiveFormat::RGB8;
|
|
|
|
|
|
|
|
*out_format = mFormatUsage->GetUsage(effFormat);
|
|
|
|
MOZ_ASSERT(*out_format);
|
|
|
|
|
|
|
|
*out_width = mWidth;
|
|
|
|
*out_height = mHeight;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mBoundReadFramebuffer->ValidateForRead(funcName, out_format, out_width,
|
2016-07-13 10:58:16 +03:00
|
|
|
out_height);
|
2015-08-04 00:34:46 +03:00
|
|
|
}
|
|
|
|
|
2014-10-02 04:05:34 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-12-21 10:31:22 +03:00
|
|
|
WebGLContext::ScopedDrawCallWrapper::ScopedDrawCallWrapper(WebGLContext& webgl)
|
2014-10-02 04:05:34 +04:00
|
|
|
: mWebGL(webgl)
|
2015-03-12 04:23:56 +03:00
|
|
|
, mFakeNoAlpha(ShouldFakeNoAlpha(webgl))
|
2015-09-24 22:21:05 +03:00
|
|
|
, mFakeNoDepth(ShouldFakeNoDepth(webgl))
|
2015-03-12 04:23:56 +03:00
|
|
|
, mFakeNoStencil(ShouldFakeNoStencil(webgl))
|
2014-10-02 04:05:34 +04:00
|
|
|
{
|
2016-12-21 10:31:22 +03:00
|
|
|
if (!mWebGL.mBoundDrawFramebuffer) {
|
|
|
|
mWebGL.ClearBackbufferIfNeeded();
|
|
|
|
}
|
|
|
|
|
2015-03-12 04:23:56 +03:00
|
|
|
if (mFakeNoAlpha) {
|
2014-10-02 04:05:34 +04:00
|
|
|
mWebGL.gl->fColorMask(mWebGL.mColorWriteMask[0],
|
|
|
|
mWebGL.mColorWriteMask[1],
|
|
|
|
mWebGL.mColorWriteMask[2],
|
|
|
|
false);
|
|
|
|
}
|
2015-09-24 22:21:05 +03:00
|
|
|
if (mFakeNoDepth) {
|
|
|
|
mWebGL.gl->fDisable(LOCAL_GL_DEPTH_TEST);
|
|
|
|
}
|
2015-03-12 04:23:56 +03:00
|
|
|
if (mFakeNoStencil) {
|
|
|
|
mWebGL.gl->fDisable(LOCAL_GL_STENCIL_TEST);
|
|
|
|
}
|
2014-10-02 04:05:34 +04:00
|
|
|
}
|
|
|
|
|
2016-12-21 10:31:22 +03:00
|
|
|
WebGLContext::ScopedDrawCallWrapper::~ScopedDrawCallWrapper()
|
2014-10-02 04:05:34 +04:00
|
|
|
{
|
2015-03-12 04:23:56 +03:00
|
|
|
if (mFakeNoAlpha) {
|
2014-10-02 04:05:34 +04:00
|
|
|
mWebGL.gl->fColorMask(mWebGL.mColorWriteMask[0],
|
|
|
|
mWebGL.mColorWriteMask[1],
|
|
|
|
mWebGL.mColorWriteMask[2],
|
|
|
|
mWebGL.mColorWriteMask[3]);
|
|
|
|
}
|
2015-09-24 22:21:05 +03:00
|
|
|
if (mFakeNoDepth) {
|
|
|
|
mWebGL.gl->fEnable(LOCAL_GL_DEPTH_TEST);
|
|
|
|
}
|
2015-03-12 04:23:56 +03:00
|
|
|
if (mFakeNoStencil) {
|
2016-02-19 04:59:24 +03:00
|
|
|
MOZ_ASSERT(mWebGL.mStencilTestEnabled);
|
2015-03-12 04:23:56 +03:00
|
|
|
mWebGL.gl->fEnable(LOCAL_GL_STENCIL_TEST);
|
|
|
|
}
|
2016-12-21 10:31:22 +03:00
|
|
|
|
|
|
|
if (!mWebGL.mBoundDrawFramebuffer) {
|
|
|
|
mWebGL.Invalidate();
|
|
|
|
mWebGL.mShouldPresent = true;
|
|
|
|
}
|
2014-10-02 04:05:34 +04:00
|
|
|
}
|
2015-02-25 01:09:09 +03:00
|
|
|
|
2016-02-19 04:59:24 +03:00
|
|
|
/*static*/ bool
|
2016-12-21 10:31:22 +03:00
|
|
|
WebGLContext::ScopedDrawCallWrapper::HasDepthButNoStencil(const WebGLFramebuffer* fb)
|
2016-02-19 04:59:24 +03:00
|
|
|
{
|
|
|
|
const auto& depth = fb->DepthAttachment();
|
|
|
|
const auto& stencil = fb->StencilAttachment();
|
|
|
|
return depth.IsDefined() && !stencil.IsDefined();
|
|
|
|
}
|
|
|
|
|
2016-12-24 05:54:04 +03:00
|
|
|
////
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::OnBeforeReadCall()
|
|
|
|
{
|
|
|
|
if (!mBoundReadFramebuffer) {
|
|
|
|
ClearBackbufferIfNeeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
IndexedBufferBinding::IndexedBufferBinding()
|
|
|
|
: mRangeStart(0)
|
|
|
|
, mRangeSize(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
IndexedBufferBinding::ByteCount() const
|
|
|
|
{
|
|
|
|
if (!mBufferBinding)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint64_t bufferSize = mBufferBinding->ByteLength();
|
|
|
|
if (!mRangeSize) // BindBufferBase
|
|
|
|
return bufferSize;
|
|
|
|
|
|
|
|
if (mRangeStart >= bufferSize)
|
|
|
|
return 0;
|
|
|
|
bufferSize -= mRangeStart;
|
|
|
|
|
|
|
|
return std::min(bufferSize, mRangeSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
ScopedUnpackReset::ScopedUnpackReset(WebGLContext* webgl)
|
|
|
|
: ScopedGLWrapper<ScopedUnpackReset>(webgl->gl)
|
|
|
|
, mWebGL(webgl)
|
|
|
|
{
|
|
|
|
if (mWebGL->mPixelStore_UnpackAlignment != 4) mGL->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
|
|
|
|
if (mWebGL->IsWebGL2()) {
|
|
|
|
if (mWebGL->mPixelStore_UnpackRowLength != 0) mGL->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH , 0);
|
|
|
|
if (mWebGL->mPixelStore_UnpackImageHeight != 0) mGL->fPixelStorei(LOCAL_GL_UNPACK_IMAGE_HEIGHT, 0);
|
|
|
|
if (mWebGL->mPixelStore_UnpackSkipPixels != 0) mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_PIXELS , 0);
|
|
|
|
if (mWebGL->mPixelStore_UnpackSkipRows != 0) mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_ROWS , 0);
|
|
|
|
if (mWebGL->mPixelStore_UnpackSkipImages != 0) mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_IMAGES , 0);
|
|
|
|
|
|
|
|
if (mWebGL->mBoundPixelUnpackBuffer) mGL->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScopedUnpackReset::UnwrapImpl()
|
|
|
|
{
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, mWebGL->mPixelStore_UnpackAlignment);
|
|
|
|
|
|
|
|
if (mWebGL->IsWebGL2()) {
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_ROW_LENGTH , mWebGL->mPixelStore_UnpackRowLength );
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_IMAGE_HEIGHT, mWebGL->mPixelStore_UnpackImageHeight);
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_PIXELS , mWebGL->mPixelStore_UnpackSkipPixels );
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_ROWS , mWebGL->mPixelStore_UnpackSkipRows );
|
|
|
|
mGL->fPixelStorei(LOCAL_GL_UNPACK_SKIP_IMAGES , mWebGL->mPixelStore_UnpackSkipImages );
|
|
|
|
|
|
|
|
GLuint pbo = 0;
|
|
|
|
if (mWebGL->mBoundPixelUnpackBuffer) {
|
|
|
|
pbo = mWebGL->mBoundPixelUnpackBuffer->mGLName;
|
|
|
|
}
|
|
|
|
|
|
|
|
mGL->fBindBuffer(LOCAL_GL_PIXEL_UNPACK_BUFFER, pbo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 01:56:58 +03:00
|
|
|
////////////////////
|
|
|
|
|
|
|
|
void
|
|
|
|
ScopedFBRebinder::UnwrapImpl()
|
|
|
|
{
|
|
|
|
const auto fnName = [&](WebGLFramebuffer* fb) {
|
|
|
|
return fb ? fb->mGLName : 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (mWebGL->IsWebGL2()) {
|
|
|
|
mGL->fBindFramebuffer(LOCAL_GL_DRAW_FRAMEBUFFER, fnName(mWebGL->mBoundDrawFramebuffer));
|
|
|
|
mGL->fBindFramebuffer(LOCAL_GL_READ_FRAMEBUFFER, fnName(mWebGL->mBoundReadFramebuffer));
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mWebGL->mBoundDrawFramebuffer == mWebGL->mBoundReadFramebuffer);
|
|
|
|
mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, fnName(mWebGL->mBoundDrawFramebuffer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 23:38:56 +03:00
|
|
|
////////////////////
|
|
|
|
|
|
|
|
static GLenum
|
|
|
|
TargetIfLazy(GLenum target)
|
|
|
|
{
|
|
|
|
switch (target) {
|
|
|
|
case LOCAL_GL_PIXEL_PACK_BUFFER:
|
|
|
|
case LOCAL_GL_PIXEL_UNPACK_BUFFER:
|
|
|
|
return target;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedLazyBind::ScopedLazyBind(gl::GLContext* gl, GLenum target, const WebGLBuffer* buf)
|
|
|
|
: ScopedGLWrapper<ScopedLazyBind>(gl)
|
|
|
|
, mTarget(buf ? TargetIfLazy(target) : 0)
|
|
|
|
, mBuf(buf)
|
|
|
|
{
|
|
|
|
if (mTarget) {
|
|
|
|
mGL->fBindBuffer(mTarget, mBuf->mGLName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScopedLazyBind::UnwrapImpl()
|
|
|
|
{
|
|
|
|
if (mTarget) {
|
|
|
|
mGL->fBindBuffer(mTarget, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
void
|
|
|
|
Intersect(uint32_t srcSize, int32_t dstStartInSrc, uint32_t dstSize,
|
|
|
|
uint32_t* const out_intStartInSrc, uint32_t* const out_intStartInDst,
|
|
|
|
uint32_t* const out_intSize)
|
|
|
|
{
|
|
|
|
// Only >0 if dstStartInSrc is >0:
|
|
|
|
// 0 3 // src coords
|
|
|
|
// | [========] // dst box
|
|
|
|
// ^--^
|
|
|
|
*out_intStartInSrc = std::max<int32_t>(0, dstStartInSrc);
|
|
|
|
|
|
|
|
// Only >0 if dstStartInSrc is <0:
|
|
|
|
//-6 0 // src coords
|
|
|
|
// [=====|==] // dst box
|
|
|
|
// ^-----^
|
|
|
|
*out_intStartInDst = std::max<int32_t>(0, 0 - dstStartInSrc);
|
|
|
|
|
|
|
|
int32_t intEndInSrc = std::min<int32_t>(srcSize, dstStartInSrc + dstSize);
|
|
|
|
*out_intSize = std::max<int32_t>(0, intEndInSrc - *out_intStartInSrc);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CheckedUint32
|
|
|
|
WebGLContext::GetUnpackSize(bool isFunc3D, uint32_t width, uint32_t height,
|
|
|
|
uint32_t depth, uint8_t bytesPerPixel)
|
|
|
|
{
|
|
|
|
if (!width || !height || !depth)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
const auto& maybeRowLength = mPixelStore_UnpackRowLength;
|
|
|
|
const auto& maybeImageHeight = mPixelStore_UnpackImageHeight;
|
|
|
|
|
|
|
|
const auto usedPixelsPerRow = CheckedUint32(mPixelStore_UnpackSkipPixels) + width;
|
|
|
|
const auto stridePixelsPerRow = (maybeRowLength ? CheckedUint32(maybeRowLength)
|
|
|
|
: usedPixelsPerRow);
|
|
|
|
|
|
|
|
const auto usedRowsPerImage = CheckedUint32(mPixelStore_UnpackSkipRows) + height;
|
|
|
|
const auto strideRowsPerImage = (maybeImageHeight ? CheckedUint32(maybeImageHeight)
|
|
|
|
: usedRowsPerImage);
|
|
|
|
|
|
|
|
const uint32_t skipImages = (isFunc3D ? mPixelStore_UnpackSkipImages
|
|
|
|
: 0);
|
|
|
|
const CheckedUint32 usedImages = CheckedUint32(skipImages) + depth;
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
CheckedUint32 strideBytesPerRow = bytesPerPixel * stridePixelsPerRow;
|
|
|
|
strideBytesPerRow = RoundUpToMultipleOf(strideBytesPerRow,
|
|
|
|
mPixelStore_UnpackAlignment);
|
|
|
|
|
|
|
|
const CheckedUint32 strideBytesPerImage = strideBytesPerRow * strideRowsPerImage;
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
CheckedUint32 usedBytesPerRow = bytesPerPixel * usedPixelsPerRow;
|
|
|
|
// Don't round this to the alignment, since alignment here is really just used for
|
|
|
|
// establishing stride, particularly in WebGL 1, where you can't set ROW_LENGTH.
|
|
|
|
|
|
|
|
CheckedUint32 totalBytes = strideBytesPerImage * (usedImages - 1);
|
|
|
|
totalBytes += strideBytesPerRow * (usedRowsPerImage - 1);
|
|
|
|
totalBytes += usedBytesPerRow;
|
|
|
|
|
|
|
|
return totalBytes;
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
already_AddRefed<layers::SharedSurfaceTextureClient>
|
|
|
|
WebGLContext::GetVRFrame()
|
|
|
|
{
|
2016-09-09 00:16:07 +03:00
|
|
|
if (!mLayerIsMirror) {
|
|
|
|
/**
|
|
|
|
* Do not allow VR frame submission until a mirroring canvas layer has
|
|
|
|
* been returned by GetCanvasLayer
|
|
|
|
*/
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-20 00:44:37 +03:00
|
|
|
VRManagerChild* vrmc = VRManagerChild::Get();
|
|
|
|
if (!vrmc) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-25 02:54:50 +03:00
|
|
|
|
2016-08-20 00:44:37 +03:00
|
|
|
/**
|
|
|
|
* Swap buffers as though composition has occurred.
|
|
|
|
* We will then share the resulting front buffer to be submitted to the VR
|
|
|
|
* compositor.
|
|
|
|
*/
|
|
|
|
BeginComposition();
|
|
|
|
EndComposition();
|
2016-02-25 02:54:50 +03:00
|
|
|
|
2016-08-20 00:44:37 +03:00
|
|
|
gl::GLScreenBuffer* screen = gl->Screen();
|
|
|
|
if (!screen) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-25 02:54:50 +03:00
|
|
|
|
2016-08-20 00:44:37 +03:00
|
|
|
RefPtr<SharedSurfaceTextureClient> sharedSurface = screen->Front();
|
|
|
|
if (!sharedSurface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-25 02:54:50 +03:00
|
|
|
|
2016-08-20 00:44:37 +03:00
|
|
|
if (sharedSurface && sharedSurface->GetAllocator() != vrmc) {
|
|
|
|
RefPtr<SharedSurfaceTextureClient> dest =
|
|
|
|
screen->Factory()->NewTexClient(sharedSurface->GetSize());
|
|
|
|
if (!dest) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
gl::SharedSurface* destSurf = dest->Surf();
|
|
|
|
destSurf->ProducerAcquire();
|
|
|
|
SharedSurface::ProdCopy(sharedSurface->Surf(), dest->Surf(),
|
|
|
|
screen->Factory());
|
|
|
|
destSurf->ProducerRelease();
|
2016-02-25 02:54:50 +03:00
|
|
|
|
2016-08-20 00:44:37 +03:00
|
|
|
return dest.forget();
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sharedSurface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebGLContext::StartVRPresentation()
|
|
|
|
{
|
2016-08-20 00:44:37 +03:00
|
|
|
VRManagerChild* vrmc = VRManagerChild::Get();
|
|
|
|
if (!vrmc) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gl::GLScreenBuffer* screen = gl->Screen();
|
|
|
|
if (!screen) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gl::SurfaceCaps caps = screen->mCaps;
|
|
|
|
|
|
|
|
UniquePtr<gl::SurfaceFactory> factory =
|
|
|
|
gl::GLScreenBuffer::CreateFactory(gl,
|
|
|
|
caps,
|
|
|
|
vrmc,
|
|
|
|
vrmc->GetBackendType(),
|
|
|
|
TextureFlags::ORIGIN_BOTTOM_LEFT);
|
|
|
|
|
|
|
|
screen->Morph(Move(factory));
|
|
|
|
return true;
|
2016-02-25 02:54:50 +03:00
|
|
|
}
|
|
|
|
|
2016-10-12 01:39:23 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-11-12 04:43:53 +03:00
|
|
|
static inline size_t
|
|
|
|
SizeOfViewElem(const dom::ArrayBufferView& view)
|
|
|
|
{
|
|
|
|
const auto& elemType = view.Type();
|
|
|
|
if (elemType == js::Scalar::MaxTypedArrayViewType) // DataViews.
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return js::Scalar::byteSize(elemType);
|
|
|
|
}
|
|
|
|
|
2016-10-12 01:39:23 +03:00
|
|
|
bool
|
|
|
|
WebGLContext::ValidateArrayBufferView(const char* funcName,
|
|
|
|
const dom::ArrayBufferView& view, GLuint elemOffset,
|
|
|
|
GLuint elemCountOverride, uint8_t** const out_bytes,
|
|
|
|
size_t* const out_byteLen)
|
|
|
|
{
|
|
|
|
view.ComputeLengthAndData();
|
|
|
|
uint8_t* const bytes = view.DataAllowShared();
|
|
|
|
const size_t byteLen = view.LengthAllowShared();
|
|
|
|
|
2016-11-12 04:43:53 +03:00
|
|
|
const auto& elemSize = SizeOfViewElem(view);
|
2016-10-12 01:39:23 +03:00
|
|
|
|
|
|
|
size_t elemCount = byteLen / elemSize;
|
|
|
|
if (elemOffset > elemCount) {
|
|
|
|
ErrorInvalidValue("%s: Invalid offset into ArrayBufferView.", funcName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
elemCount -= elemOffset;
|
|
|
|
|
|
|
|
if (elemCountOverride) {
|
|
|
|
if (elemCountOverride > elemCount) {
|
|
|
|
ErrorInvalidValue("%s: Invalid sub-length for ArrayBufferView.", funcName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
elemCount = elemCountOverride;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_bytes = bytes + (elemOffset * elemSize);
|
|
|
|
*out_byteLen = elemCount * elemSize;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-02 04:05:34 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2009-09-03 04:47:49 +04:00
|
|
|
// XPCOM goop
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& callback,
|
|
|
|
const std::vector<IndexedBufferBinding>& field,
|
|
|
|
const char* name, uint32_t flags)
|
|
|
|
{
|
|
|
|
for (const auto& cur : field) {
|
|
|
|
ImplCycleCollectionTraverse(callback, cur.mBufferBinding, name, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImplCycleCollectionUnlink(std::vector<IndexedBufferBinding>& field)
|
|
|
|
{
|
|
|
|
field.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2011-03-06 14:11:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(WebGLContext)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(WebGLContext)
|
2010-06-16 01:38:05 +04:00
|
|
|
|
2014-04-29 12:57:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(WebGLContext,
|
2014-12-05 10:04:55 +03:00
|
|
|
mCanvasElement,
|
2015-10-12 06:21:03 +03:00
|
|
|
mOffscreenCanvas,
|
2014-12-05 10:04:55 +03:00
|
|
|
mExtensions,
|
|
|
|
mBound2DTextures,
|
|
|
|
mBoundCubeMapTextures,
|
|
|
|
mBound3DTextures,
|
2015-11-25 07:15:29 +03:00
|
|
|
mBound2DArrayTextures,
|
2015-06-09 03:18:19 +03:00
|
|
|
mBoundSamplers,
|
2014-12-05 10:04:55 +03:00
|
|
|
mBoundArrayBuffer,
|
|
|
|
mBoundCopyReadBuffer,
|
|
|
|
mBoundCopyWriteBuffer,
|
|
|
|
mBoundPixelPackBuffer,
|
|
|
|
mBoundPixelUnpackBuffer,
|
2016-09-10 07:02:54 +03:00
|
|
|
mBoundTransformFeedback,
|
2014-12-05 10:04:55 +03:00
|
|
|
mBoundUniformBuffer,
|
|
|
|
mCurrentProgram,
|
2015-01-13 02:05:21 +03:00
|
|
|
mBoundDrawFramebuffer,
|
|
|
|
mBoundReadFramebuffer,
|
2014-12-05 10:04:55 +03:00
|
|
|
mBoundRenderbuffer,
|
|
|
|
mBoundVertexArray,
|
|
|
|
mDefaultVertexArray,
|
2016-10-04 04:33:52 +03:00
|
|
|
mQuerySlot_SamplesPassed,
|
|
|
|
mQuerySlot_TFPrimsWritten,
|
|
|
|
mQuerySlot_TimeElapsed)
|
2009-09-03 04:47:49 +04:00
|
|
|
|
2010-06-16 01:38:05 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebGLContext)
|
2014-11-14 07:03:50 +03:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMWebGLRenderingContext)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICanvasRenderingContextInternal)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
// If the exact way we cast to nsISupports here ever changes, fix our
|
|
|
|
// ToSupports() method.
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMWebGLRenderingContext)
|
2009-09-03 04:47:49 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
2015-07-15 03:37:28 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|