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"
|
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"
|
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-03 11:18:56 +03:00
|
|
|
#include "WebGLTimerQuery.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
|
|
|
|
|
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)
|
2015-01-16 02:40:39 +03:00
|
|
|
, mBypassShaderValidation(false)
|
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)
|
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
|
|
|
|
|
|
|
mVertexAttrib0Vector[0] = 0;
|
|
|
|
mVertexAttrib0Vector[1] = 0;
|
|
|
|
mVertexAttrib0Vector[2] = 0;
|
|
|
|
mVertexAttrib0Vector[3] = 1;
|
2011-02-25 01:17:34 +03:00
|
|
|
mFakeVertexAttrib0BufferObjectVector[0] = 0;
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[1] = 0;
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[2] = 0;
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[3] = 1;
|
|
|
|
mFakeVertexAttrib0BufferObjectSize = 0;
|
|
|
|
mFakeVertexAttrib0BufferObject = 0;
|
2013-10-11 17:16:43 +04:00
|
|
|
mFakeVertexAttrib0BufferStatus = WebGLVertexAttrib0Status::Default;
|
2011-05-20 23:53:53 +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;
|
2014-08-22 20:55:00 +04:00
|
|
|
mContextLossHandler = new WebGLContextLossHandler(this);
|
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
|
|
|
|
2014-04-18 00:15:41 +04:00
|
|
|
mBackbufferNeedsClear = true;
|
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);
|
|
|
|
}
|
2014-08-22 20:55:00 +04:00
|
|
|
|
|
|
|
mContextLossHandler->DisableTimer();
|
|
|
|
mContextLossHandler = nullptr;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
2016-06-03 11:18:56 +03:00
|
|
|
template<typename T>
|
|
|
|
static void
|
|
|
|
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;
|
2013-08-20 19:36:20 +04:00
|
|
|
mBoundTransformFeedbackBuffer = nullptr;
|
2014-12-05 10:04:55 +03:00
|
|
|
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;
|
2013-08-07 01:23:46 +04:00
|
|
|
mActiveOcclusionQuery = 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;
|
|
|
|
|
2015-02-08 17:13:00 +03:00
|
|
|
mBoundTransformFeedbackBuffers.Clear();
|
|
|
|
mBoundUniformBuffers.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);
|
|
|
|
ClearLinkedList(mTextures);
|
|
|
|
ClearLinkedList(mTimerQueries);
|
|
|
|
ClearLinkedList(mTransformFeedbacks);
|
|
|
|
ClearLinkedList(mVertexArrays);
|
|
|
|
|
|
|
|
//////
|
2010-07-19 09:01:14 +04:00
|
|
|
|
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
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
if (mFakeVertexAttrib0BufferObject)
|
2011-02-25 01:17:34 +03:00
|
|
|
gl->fDeleteBuffers(1, &mFakeVertexAttrib0BufferObject);
|
|
|
|
|
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.
|
2010-09-13 19:40:01 +04:00
|
|
|
#ifdef DEBUG
|
2014-11-14 07:03:50 +03:00
|
|
|
if (gl->DebugMode())
|
2012-06-11 19:25:06 +04:00
|
|
|
printf_stderr("--- WebGL context destroyed: %p\n", gl.get());
|
2010-09-13 19:40:01 +04:00
|
|
|
#endif
|
2010-07-19 09:01:14 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
gl = nullptr;
|
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-05-20 20:14:15 +03:00
|
|
|
IsFeatureInBlacklist(const nsCOMPtr<nsIGfxInfo>& gfxInfo, int32_t feature, nsACString* const out_failureId)
|
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-05-20 20:14:15 +03:00
|
|
|
*out_failureId, &status)))
|
2014-08-28 03:16:22 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
baseCaps.surfaceAllocator = static_cast<layers::ISurfaceAllocator*>(forwarder);
|
|
|
|
} while (false);
|
2014-08-28 03:16:22 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Done with baseCaps construction.
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
bool forceAllowAA = gfxPrefs::WebGLForceMSAA();
|
2015-09-24 22:21:05 +03:00
|
|
|
nsCOMPtr<nsIGfxInfo> gfxInfo = services::GetGfxInfo();
|
2016-05-20 20:14:15 +03:00
|
|
|
nsCString discardFailureId;
|
2014-08-28 03:16:22 +04:00
|
|
|
if (!forceAllowAA &&
|
2016-05-20 20:14:15 +03:00
|
|
|
IsFeatureInBlacklist(gfxInfo, nsIGfxInfo::FEATURE_WEBGL_MSAA, &discardFailureId))
|
2014-08-28 03:16:22 +04:00
|
|
|
{
|
|
|
|
webgl->GenerateWarning("Disallowing antialiased backbuffers due"
|
|
|
|
" to blacklisting.");
|
|
|
|
baseCaps.antialias = false;
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
return baseCaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
static already_AddRefed<gl::GLContext>
|
|
|
|
CreateGLWithEGL(const gl::SurfaceCaps& caps, gl::CreateContextFlags flags,
|
2016-05-20 20:14:15 +03:00
|
|
|
WebGLContext* webgl, nsACString* const out_failReason,
|
|
|
|
nsACString* const out_failureId)
|
2015-09-24 22:21:05 +03:00
|
|
|
{
|
2016-04-14 23:46:37 +03:00
|
|
|
const gfx::IntSize dummySize(16, 16);
|
|
|
|
RefPtr<GLContext> gl = gl::GLContextProviderEGL::CreateOffscreen(dummySize, caps,
|
2016-06-17 15:33:06 +03:00
|
|
|
flags, *out_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-05-12 02:45:43 +03:00
|
|
|
if (out_failReason->Length()) {
|
|
|
|
out_failReason->AppendLiteral("\n");
|
|
|
|
}
|
|
|
|
out_failReason->AppendLiteral("Error during EGL OpenGL init.");
|
2016-06-09 22:20:49 +03:00
|
|
|
if (out_failureId->IsEmpty()) {
|
|
|
|
*out_failureId = "FEATURE_FAILURE_WEBGL_EGL_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-05-20 20:14:15 +03:00
|
|
|
WebGLContext* webgl, nsACString* const out_failReason,
|
|
|
|
nsACString* const out_failureId)
|
2015-09-24 22:21:05 +03:00
|
|
|
{
|
2016-04-14 23:46:37 +03:00
|
|
|
const gfx::IntSize dummySize(16, 16);
|
|
|
|
RefPtr<GLContext> gl = gl::GLContextProviderEGL::CreateOffscreen(dummySize, caps,
|
2016-06-17 15:33:06 +03:00
|
|
|
flags, *out_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-05-12 02:45:43 +03:00
|
|
|
if (out_failReason->Length()) {
|
|
|
|
out_failReason->AppendLiteral("\n");
|
|
|
|
}
|
|
|
|
out_failReason->AppendLiteral("Error during ANGLE OpenGL init.");
|
2016-06-09 22:20:49 +03:00
|
|
|
if (out_failureId->IsEmpty()) {
|
|
|
|
*out_failureId = "FEATURE_FAILURE_WEBGL_ANGLE_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-05-20 20:14:15 +03:00
|
|
|
WebGLContext* webgl, nsACString* const out_failReason,
|
|
|
|
nsACString* const out_failureId)
|
2015-09-24 22:21:05 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGfxInfo> gfxInfo = services::GetGfxInfo();
|
|
|
|
|
|
|
|
if (!(flags & CreateContextFlags::FORCE_ENABLE_HARDWARE) &&
|
2016-05-20 20:14:15 +03:00
|
|
|
IsFeatureInBlacklist(gfxInfo, nsIGfxInfo::FEATURE_WEBGL_OPENGL, out_failureId))
|
2015-09-24 22:21:05 +03:00
|
|
|
{
|
2016-05-12 02:45:43 +03:00
|
|
|
if (out_failReason->Length()) {
|
|
|
|
out_failReason->AppendASCII("\n");
|
|
|
|
}
|
|
|
|
out_failReason->AppendASCII("Refused to create native OpenGL context because of"
|
|
|
|
" blacklisting.");
|
2015-09-24 22:21:05 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-14 23:46:37 +03:00
|
|
|
const gfx::IntSize dummySize(16, 16);
|
2016-06-06 23:52:42 +03:00
|
|
|
RefPtr<GLContext> gl = gl::GLContextProvider::CreateOffscreen(dummySize, caps,
|
2016-06-17 15:33:06 +03:00
|
|
|
flags, *out_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-05-12 02:45:43 +03:00
|
|
|
if (out_failReason->Length()) {
|
|
|
|
out_failReason->AppendASCII("\n");
|
|
|
|
}
|
|
|
|
out_failReason->AppendASCII("Error during native OpenGL init.");
|
2016-06-09 22:20:49 +03:00
|
|
|
if (out_failureId->IsEmpty()) {
|
|
|
|
*out_failureId = "FEATURE_FAILURE_WEBGL_DEFAULT_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-05-20 20:14:15 +03:00
|
|
|
nsACString* const out_failReason,
|
|
|
|
nsACString* const out_failureId)
|
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.");
|
2015-09-24 22:21:05 +03:00
|
|
|
gl = nullptr;
|
2014-08-28 03:16:22 +04:00
|
|
|
while (!fallbackCaps.empty()) {
|
2015-09-24 22:21:05 +03:00
|
|
|
gl::SurfaceCaps& caps = fallbackCaps.front();
|
2014-08-28 03:16:22 +04:00
|
|
|
|
2016-05-20 20:14:15 +03:00
|
|
|
gl = fnCreateGL(caps, flags, this, out_failReason, out_failureId);
|
2015-09-24 22:21:05 +03:00
|
|
|
if (gl)
|
2014-08-28 03:16:22 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
fallbackCaps.pop();
|
|
|
|
}
|
2015-09-24 22:21:05 +03:00
|
|
|
if (!gl)
|
|
|
|
return false;
|
2014-08-28 03:16:22 +04:00
|
|
|
|
2016-05-20 20:14:15 +03:00
|
|
|
if (!InitAndValidateGL(out_failReason, out_failureId)) {
|
|
|
|
// The fail reason here should be specific enough for now.
|
2015-09-24 22:21:05 +03:00
|
|
|
gl = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-10-01 01:02:40 +04:00
|
|
|
}
|
2014-08-28 03:16:22 +04:00
|
|
|
|
|
|
|
bool
|
2016-05-20 20:14:15 +03:00
|
|
|
WebGLContext::CreateAndInitGL(bool forceEnabled, nsACString* const out_failReason, nsACString* const out_failureId)
|
2014-08-28 03:16:22 +04:00
|
|
|
{
|
2016-04-14 23:46:37 +03:00
|
|
|
const bool useEGL = PR_GetEnv("MOZ_WEBGL_PREFER_EGL");
|
2015-07-29 23:35:55 +03:00
|
|
|
|
2016-04-14 23:46:37 +03:00
|
|
|
bool useANGLE = false;
|
|
|
|
#ifdef XP_WIN
|
|
|
|
const bool disableANGLE = (gfxPrefs::WebGLDisableANGLE() ||
|
|
|
|
PR_GetEnv("MOZ_WEBGL_FORCE_OPENGL"));
|
|
|
|
useANGLE = !disableANGLE;
|
|
|
|
#endif
|
2014-08-28 03:16:22 +04:00
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
gl::CreateContextFlags flags = gl::CreateContextFlags::NONE;
|
|
|
|
if (forceEnabled) flags |= gl::CreateContextFlags::FORCE_ENABLE_HARDWARE;
|
|
|
|
if (!IsWebGL2()) flags |= gl::CreateContextFlags::REQUIRE_COMPAT_PROFILE;
|
2015-11-27 22:02:52 +03:00
|
|
|
if (IsWebGL2()) flags |= gl::CreateContextFlags::PREFER_ES3;
|
2014-08-28 03:16:22 +04:00
|
|
|
|
2015-09-24 22:21:05 +03:00
|
|
|
const gl::SurfaceCaps baseCaps = BaseCaps(mOptions, this);
|
2014-08-28 03:16:22 +04:00
|
|
|
|
2016-04-14 23:46:37 +03:00
|
|
|
if (useEGL)
|
2016-05-20 20:14:15 +03:00
|
|
|
return CreateAndInitGLWith(CreateGLWithEGL, baseCaps, flags, out_failReason, out_failureId);
|
2015-09-24 22:21:05 +03:00
|
|
|
|
2016-04-14 23:46:37 +03:00
|
|
|
if (useANGLE)
|
2016-05-20 20:14:15 +03:00
|
|
|
return CreateAndInitGLWith(CreateGLWithANGLE, baseCaps, flags, out_failReason, out_failureId);
|
2015-09-24 22:21:05 +03:00
|
|
|
|
2016-05-20 20:14:15 +03:00
|
|
|
return CreateAndInitGLWith(CreateGLWithDefault, baseCaps, flags, out_failReason, out_failureId);
|
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;
|
|
|
|
if (!target) {
|
|
|
|
target = mOffscreenCanvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
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
|
|
|
|
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-05-20 20:14:15 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID,
|
|
|
|
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-05-20 20:14:15 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID,
|
|
|
|
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-05-20 20:14:15 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID,
|
|
|
|
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-05-12 02:45:43 +03:00
|
|
|
nsCString failReason;
|
2016-05-20 20:14:15 +03:00
|
|
|
nsCString failureId;
|
|
|
|
if (!CreateAndInitGL(forceEnabled, &failReason, &failureId)) {
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID, failureId);
|
2016-05-12 02:45:43 +03:00
|
|
|
const nsPrintfCString text("WebGL creation failed: %s",
|
|
|
|
failReason.BeginReading());
|
|
|
|
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-06-06 20:33:13 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID,
|
|
|
|
NS_LITERAL_CSTRING("FEATURE_FAILURE_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 03:16:22 +04:00
|
|
|
if (!ResizeBackbuffer(width, height)) {
|
2016-05-20 20:14:15 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID,
|
|
|
|
NS_LITERAL_CSTRING("FEATURE_FAILURE_WEBGL_RESIZE"));
|
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;
|
|
|
|
}
|
|
|
|
|
2010-09-13 19:40:01 +04:00
|
|
|
#ifdef DEBUG
|
2014-11-14 07:03:50 +03:00
|
|
|
if (gl->DebugMode())
|
2012-06-11 19:25:06 +04:00
|
|
|
printf_stderr("--- WebGL context created: %p\n", gl.get());
|
2010-09-13 19:40:01 +04:00
|
|
|
#endif
|
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
|
|
|
|
|
|
|
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_FAILURE_ID,
|
|
|
|
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;
|
|
|
|
|
2015-07-02 02:18:59 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
|
|
|
GLuint fb = 0;
|
|
|
|
gl->GetUIntegerv(LOCAL_GL_FRAMEBUFFER_BINDING, &fb);
|
|
|
|
MOZ_ASSERT(fb == 0);
|
|
|
|
#endif
|
|
|
|
|
2014-04-18 00:15:41 +04:00
|
|
|
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())
|
2012-08-03 01:28:02 +04:00
|
|
|
NS_RUNTIMEABORT("Can't believe it's been 2^64 transactions already!");
|
|
|
|
|
|
|
|
mLastUseIndex = sIndex.value();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint8_t gWebGLLayerUserData;
|
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
|
|
|
|
|
|
|
// Present our screenbuffer, if needed.
|
2014-11-14 07:03:50 +03:00
|
|
|
webgl->PresentScreenBuffer();
|
|
|
|
webgl->mDrawCallsSinceLastFlush = 0;
|
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
|
|
|
|
2013-02-14 03:26:24 +04:00
|
|
|
// Mark ourselves as no longer invalidated.
|
2014-11-14 07:03:50 +03:00
|
|
|
webgl->MarkContextClean();
|
2012-08-03 01:28:02 +04:00
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
webgl->UpdateLastUseIndex();
|
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,
|
2014-11-14 07:03:50 +03:00
|
|
|
LayerManager* manager)
|
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 &&
|
|
|
|
oldLayer->HasUserData(&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;
|
2015-10-12 06:21:03 +03:00
|
|
|
if (builder->IsPaintingToWindow() && mCanvasElement) {
|
|
|
|
// 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
|
|
|
|
2011-03-28 03:59:47 +04:00
|
|
|
canvasLayer->SetUserData(&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;
|
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;
|
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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
nsCString fbStatusInfo;
|
|
|
|
const auto status = mBoundReadFramebuffer->CheckFramebufferStatus(&fbStatusInfo);
|
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
if (status != LOCAL_GL_FRAMEBUFFER_COMPLETE) {
|
2015-12-15 03:11:59 +03:00
|
|
|
nsCString errorText("Incomplete framebuffer");
|
|
|
|
|
|
|
|
if (fbStatusInfo.Length()) {
|
|
|
|
errorText += ": ";
|
|
|
|
errorText += fbStatusInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorInvalidFramebufferOperation("%s: %s.", funcName, errorText.BeginReading());
|
2014-11-14 07:03:50 +03:00
|
|
|
}
|
2012-01-25 01:12:31 +04:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
mContextLossHandler->RunTimer();
|
|
|
|
}
|
|
|
|
|
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.
|
2014-08-22 20:55:00 +04: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();
|
2015-02-19 03:57:05 +03:00
|
|
|
// TODO: Save, override, then restore glReadBuffer if present.
|
2014-05-13 06:20:26 +04:00
|
|
|
ReadPixelsIntoDataSurface(gl, surf);
|
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-04-01 09:06:33 +03:00
|
|
|
uint32_t* const out_width, uint32_t* const out_height,
|
|
|
|
GLenum* const out_mode)
|
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;
|
2016-04-01 09:06:33 +03:00
|
|
|
*out_mode = gl->Screen()->GetReadBufferMode();
|
2015-11-25 07:15:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mBoundReadFramebuffer->ValidateForRead(funcName, out_format, out_width,
|
2016-04-01 09:06:33 +03:00
|
|
|
out_height, out_mode);
|
2015-08-04 00:34:46 +03:00
|
|
|
}
|
|
|
|
|
2014-10-02 04:05:34 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
WebGLContext::ScopedMaskWorkaround::ScopedMaskWorkaround(WebGLContext& webgl)
|
|
|
|
: 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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
WebGLContext::ScopedMaskWorkaround::~ScopedMaskWorkaround()
|
|
|
|
{
|
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);
|
|
|
|
}
|
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
|
|
|
|
WebGLContext::ScopedMaskWorkaround::HasDepthButNoStencil(const WebGLFramebuffer* fb)
|
|
|
|
{
|
|
|
|
const auto& depth = fb->DepthAttachment();
|
|
|
|
const auto& stencil = fb->StencilAttachment();
|
|
|
|
return depth.IsDefined() && !stencil.IsDefined();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-12-15 03:14:03 +03:00
|
|
|
static bool
|
|
|
|
ZeroTexImageWithClear(WebGLContext* webgl, GLContext* gl, TexImageTarget target,
|
|
|
|
GLuint tex, uint32_t level, const webgl::FormatUsageInfo* usage,
|
|
|
|
uint32_t width, uint32_t height)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(gl->IsCurrent());
|
|
|
|
|
|
|
|
ScopedFramebuffer scopedFB(gl);
|
|
|
|
ScopedBindFramebuffer scopedBindFB(gl, scopedFB.FB());
|
|
|
|
|
|
|
|
const auto format = usage->format;
|
|
|
|
|
|
|
|
GLenum attachPoint = 0;
|
|
|
|
GLbitfield clearBits = 0;
|
|
|
|
|
|
|
|
if (format->isColorFormat) {
|
|
|
|
attachPoint = LOCAL_GL_COLOR_ATTACHMENT0;
|
|
|
|
clearBits = LOCAL_GL_COLOR_BUFFER_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format->hasDepth) {
|
|
|
|
attachPoint = LOCAL_GL_DEPTH_ATTACHMENT;
|
|
|
|
clearBits |= LOCAL_GL_DEPTH_BUFFER_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format->hasStencil) {
|
|
|
|
attachPoint = (format->hasDepth ? LOCAL_GL_DEPTH_STENCIL_ATTACHMENT
|
|
|
|
: LOCAL_GL_STENCIL_ATTACHMENT);
|
|
|
|
clearBits |= LOCAL_GL_STENCIL_BUFFER_BIT;
|
|
|
|
}
|
|
|
|
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(attachPoint && clearBits, "GFX: No bits cleared.");
|
2015-12-15 03:14:03 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
gl::GLContext::LocalErrorScope errorScope(*gl);
|
|
|
|
gl->fFramebufferTexture2D(LOCAL_GL_FRAMEBUFFER, attachPoint, target.get(), tex,
|
|
|
|
level);
|
|
|
|
if (errorScope.GetError()) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto status = gl->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER);
|
|
|
|
if (status != LOCAL_GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
{
|
|
|
|
gl::GLContext::LocalErrorScope errorScope(*gl);
|
|
|
|
|
|
|
|
const bool fakeNoAlpha = false;
|
|
|
|
webgl->ForceClearFramebufferWithDefaultValues(clearBits, fakeNoAlpha);
|
|
|
|
if (errorScope.GetError()) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
bool
|
|
|
|
ZeroTextureData(WebGLContext* webgl, const char* funcName, bool respecifyTexture,
|
2015-12-15 03:14:03 +03:00
|
|
|
GLuint tex, TexImageTarget target, uint32_t level,
|
2015-11-25 07:15:29 +03:00
|
|
|
const webgl::FormatUsageInfo* usage, uint32_t xOffset, uint32_t yOffset,
|
|
|
|
uint32_t zOffset, uint32_t width, uint32_t height, uint32_t depth)
|
|
|
|
{
|
|
|
|
// This has two usecases:
|
|
|
|
// 1. Lazy zeroing of uninitialized textures:
|
|
|
|
// a. Before draw, when FakeBlack isn't viable. (TexStorage + Draw*)
|
|
|
|
// b. Before partial upload. (TexStorage + TexSubImage)
|
|
|
|
// 2. Zero subrects from out-of-bounds blits. (CopyTex(Sub)Image)
|
|
|
|
|
|
|
|
// We have no sympathy for any of these cases.
|
|
|
|
|
|
|
|
// "Doctor, it hurts when I do this!" "Well don't do that!"
|
|
|
|
webgl->GenerateWarning("%s: This operation requires zeroing texture data. This is"
|
|
|
|
" slow.",
|
|
|
|
funcName);
|
|
|
|
|
|
|
|
gl::GLContext* gl = webgl->GL();
|
|
|
|
gl->MakeCurrent();
|
|
|
|
|
|
|
|
auto compression = usage->format->compression;
|
|
|
|
if (compression) {
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!xOffset && !yOffset && !zOffset, "GFX: Can't zero compressed texture with offsets.");
|
|
|
|
MOZ_RELEASE_ASSERT(!respecifyTexture, "GFX: respecifyTexture is set to true.");
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
auto sizedFormat = usage->format->sizedFormat;
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(sizedFormat, "GFX: texture sized format not set");
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
const auto fnSizeInBlocks = [](CheckedUint32 pixels, uint8_t pixelsPerBlock) {
|
|
|
|
return RoundUpToMultipleOf(pixels, pixelsPerBlock) / pixelsPerBlock;
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto widthBlocks = fnSizeInBlocks(width, compression->blockWidth);
|
|
|
|
const auto heightBlocks = fnSizeInBlocks(height, compression->blockHeight);
|
|
|
|
|
|
|
|
CheckedUint32 checkedByteCount = compression->bytesPerBlock;
|
|
|
|
checkedByteCount *= widthBlocks;
|
|
|
|
checkedByteCount *= heightBlocks;
|
|
|
|
checkedByteCount *= depth;
|
|
|
|
|
|
|
|
if (!checkedByteCount.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const size_t byteCount = checkedByteCount.value();
|
|
|
|
|
|
|
|
UniqueBuffer zeros = calloc(1, byteCount);
|
|
|
|
if (!zeros)
|
|
|
|
return false;
|
|
|
|
|
2015-12-15 03:14:03 +03:00
|
|
|
ScopedUnpackReset scopedReset(webgl);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 1); // Don't bother with striding it
|
|
|
|
// well.
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
GLenum error = DoCompressedTexSubImage(gl, target.get(), level, xOffset, yOffset,
|
|
|
|
zOffset, width, height, depth, sizedFormat,
|
|
|
|
byteCount, zeros.get());
|
|
|
|
if (error)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto driverUnpackInfo = usage->idealUnpack;
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(driverUnpackInfo, "GFX: ideal unpack info not set.");
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2015-12-15 03:14:03 +03:00
|
|
|
if (usage->isRenderable && depth == 1 &&
|
|
|
|
!xOffset && !yOffset && !zOffset)
|
|
|
|
{
|
|
|
|
// While we would like to skip the extra complexity of trying to zero with an FB
|
|
|
|
// clear, ANGLE_depth_texture requires this.
|
|
|
|
do {
|
|
|
|
if (respecifyTexture) {
|
|
|
|
const auto error = DoTexImage(gl, target, level, driverUnpackInfo, width,
|
|
|
|
height, depth, nullptr);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ZeroTexImageWithClear(webgl, gl, target, tex, level, usage, width,
|
|
|
|
height))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while (false);
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const webgl::PackingInfo packing = driverUnpackInfo->ToPacking();
|
|
|
|
|
|
|
|
const auto bytesPerPixel = webgl::BytesPerPixel(packing);
|
|
|
|
|
|
|
|
CheckedUint32 checkedByteCount = bytesPerPixel;
|
|
|
|
checkedByteCount *= width;
|
|
|
|
checkedByteCount *= height;
|
|
|
|
checkedByteCount *= depth;
|
|
|
|
|
|
|
|
if (!checkedByteCount.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const size_t byteCount = checkedByteCount.value();
|
|
|
|
|
|
|
|
UniqueBuffer zeros = calloc(1, byteCount);
|
|
|
|
if (!zeros)
|
|
|
|
return false;
|
|
|
|
|
2015-12-15 03:14:03 +03:00
|
|
|
ScopedUnpackReset scopedReset(webgl);
|
|
|
|
gl->fPixelStorei(LOCAL_GL_UNPACK_ALIGNMENT, 1); // Don't bother with striding it well.
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
GLenum error;
|
|
|
|
if (respecifyTexture) {
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!xOffset && !yOffset && !zOffset, "GFX: texture data, offsets, not zeroed.");
|
2015-11-25 07:15:29 +03:00
|
|
|
error = DoTexImage(gl, target, level, driverUnpackInfo, width, height, depth,
|
|
|
|
zeros.get());
|
|
|
|
} else {
|
|
|
|
error = DoTexSubImage(gl, target, level, xOffset, yOffset, zOffset, width, height,
|
|
|
|
depth, packing, zeros.get());
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-10-02 04:05:34 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2009-09-03 04:47:49 +04:00
|
|
|
// XPCOM goop
|
|
|
|
|
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,
|
|
|
|
mBoundTransformFeedbackBuffer,
|
|
|
|
mBoundUniformBuffer,
|
|
|
|
mCurrentProgram,
|
2015-01-13 02:05:21 +03:00
|
|
|
mBoundDrawFramebuffer,
|
|
|
|
mBoundReadFramebuffer,
|
2014-12-05 10:04:55 +03:00
|
|
|
mBoundRenderbuffer,
|
|
|
|
mBoundVertexArray,
|
|
|
|
mDefaultVertexArray,
|
|
|
|
mActiveOcclusionQuery,
|
|
|
|
mActiveTransformFeedbackQuery)
|
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
|