michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef WEBGLCONTEXT_H_ michael@0: #define WEBGLCONTEXT_H_ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "GLDefs.h" michael@0: #include "WebGLActiveInfo.h" michael@0: #include "WebGLObjectModel.h" michael@0: #include michael@0: michael@0: #include "nsTArray.h" michael@0: #include "nsCycleCollectionNoteChild.h" michael@0: michael@0: #include "nsIDOMWebGLRenderingContext.h" michael@0: #include "nsICanvasRenderingContextInternal.h" michael@0: #include "mozilla/dom/HTMLCanvasElement.h" michael@0: #include "nsWrapperCache.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsLayoutUtils.h" michael@0: michael@0: #include "GLContextProvider.h" michael@0: michael@0: #include "mozilla/EnumeratedArray.h" michael@0: #include "mozilla/LinkedList.h" michael@0: #include "mozilla/CheckedInt.h" michael@0: #include "mozilla/Scoped.h" michael@0: #include "mozilla/gfx/2D.h" michael@0: michael@0: #ifdef XP_MACOSX michael@0: #include "ForceDiscreteGPUHelperCGL.h" michael@0: #endif michael@0: michael@0: #include "mozilla/dom/TypedArray.h" michael@0: #include "mozilla/ErrorResult.h" michael@0: michael@0: class nsIDocShell; michael@0: michael@0: /* michael@0: * Minimum value constants defined in 6.2 State Tables of OpenGL ES - 2.0.25 michael@0: * https://bugzilla.mozilla.org/show_bug.cgi?id=686732 michael@0: * michael@0: * Exceptions: some of the following values are set to higher values than in the spec because michael@0: * the values in the spec are ridiculously low. They are explicitly marked below michael@0: */ michael@0: #define MINVALUE_GL_MAX_TEXTURE_SIZE 1024 // Different from the spec, which sets it to 64 on page 162 michael@0: #define MINVALUE_GL_MAX_CUBE_MAP_TEXTURE_SIZE 512 // Different from the spec, which sets it to 16 on page 162 michael@0: #define MINVALUE_GL_MAX_VERTEX_ATTRIBS 8 // Page 164 michael@0: #define MINVALUE_GL_MAX_FRAGMENT_UNIFORM_VECTORS 16 // Page 164 michael@0: #define MINVALUE_GL_MAX_VERTEX_UNIFORM_VECTORS 128 // Page 164 michael@0: #define MINVALUE_GL_MAX_VARYING_VECTORS 8 // Page 164 michael@0: #define MINVALUE_GL_MAX_TEXTURE_IMAGE_UNITS 8 // Page 164 michael@0: #define MINVALUE_GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS 0 // Page 164 michael@0: #define MINVALUE_GL_MAX_RENDERBUFFER_SIZE 1024 // Different from the spec, which sets it to 1 on page 164 michael@0: #define MINVALUE_GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 8 // Page 164 michael@0: michael@0: namespace mozilla { michael@0: michael@0: class WebGLMemoryPressureObserver; michael@0: class WebGLContextBoundObject; michael@0: class WebGLActiveInfo; michael@0: class WebGLExtensionBase; michael@0: class WebGLBuffer; michael@0: class WebGLVertexAttribData; michael@0: class WebGLShader; michael@0: class WebGLProgram; michael@0: class WebGLQuery; michael@0: class WebGLUniformLocation; michael@0: class WebGLFramebuffer; michael@0: class WebGLRenderbuffer; michael@0: class WebGLShaderPrecisionFormat; michael@0: class WebGLTexture; michael@0: class WebGLVertexArray; michael@0: michael@0: namespace dom { michael@0: class ImageData; michael@0: michael@0: struct WebGLContextAttributes; michael@0: template struct Nullable; michael@0: } michael@0: michael@0: namespace gfx { michael@0: class SourceSurface; michael@0: } michael@0: michael@0: WebGLTexelFormat GetWebGLTexelFormat(GLenum format, GLenum type); michael@0: michael@0: struct WebGLContextOptions { michael@0: // these are defaults michael@0: WebGLContextOptions(); michael@0: michael@0: bool operator==(const WebGLContextOptions& other) const { michael@0: return michael@0: alpha == other.alpha && michael@0: depth == other.depth && michael@0: stencil == other.stencil && michael@0: premultipliedAlpha == other.premultipliedAlpha && michael@0: antialias == other.antialias && michael@0: preserveDrawingBuffer == other.preserveDrawingBuffer; michael@0: } michael@0: michael@0: bool operator!=(const WebGLContextOptions& other) const { michael@0: return !operator==(other); michael@0: } michael@0: michael@0: bool alpha; michael@0: bool depth; michael@0: bool stencil; michael@0: bool premultipliedAlpha; michael@0: bool antialias; michael@0: bool preserveDrawingBuffer; michael@0: }; michael@0: michael@0: class WebGLContext : michael@0: public nsIDOMWebGLRenderingContext, michael@0: public nsICanvasRenderingContextInternal, michael@0: public nsSupportsWeakReference, michael@0: public WebGLRectangleObject, michael@0: public nsWrapperCache michael@0: { michael@0: friend class WebGLContextUserData; michael@0: friend class WebGLExtensionCompressedTextureATC; michael@0: friend class WebGLExtensionCompressedTextureETC1; michael@0: friend class WebGLExtensionCompressedTexturePVRTC; michael@0: friend class WebGLExtensionCompressedTextureS3TC; michael@0: friend class WebGLExtensionDepthTexture; michael@0: friend class WebGLExtensionDrawBuffers; michael@0: friend class WebGLExtensionLoseContext; michael@0: friend class WebGLExtensionVertexArray; michael@0: friend class WebGLMemoryPressureObserver; michael@0: friend class WebGLMemoryTracker; michael@0: michael@0: enum { michael@0: UNPACK_FLIP_Y_WEBGL = 0x9240, michael@0: UNPACK_PREMULTIPLY_ALPHA_WEBGL = 0x9241, michael@0: CONTEXT_LOST_WEBGL = 0x9242, michael@0: UNPACK_COLORSPACE_CONVERSION_WEBGL = 0x9243, michael@0: BROWSER_DEFAULT_WEBGL = 0x9244, michael@0: UNMASKED_VENDOR_WEBGL = 0x9245, michael@0: UNMASKED_RENDERER_WEBGL = 0x9246 michael@0: }; michael@0: michael@0: public: michael@0: WebGLContext(); michael@0: virtual ~WebGLContext(); michael@0: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(WebGLContext, michael@0: nsIDOMWebGLRenderingContext) michael@0: michael@0: virtual JSObject* WrapObject(JSContext *cx) = 0; michael@0: michael@0: NS_DECL_NSIDOMWEBGLRENDERINGCONTEXT michael@0: michael@0: // nsICanvasRenderingContextInternal michael@0: #ifdef DEBUG michael@0: virtual int32_t GetWidth() const MOZ_OVERRIDE; michael@0: virtual int32_t GetHeight() const MOZ_OVERRIDE; michael@0: #endif michael@0: NS_IMETHOD SetDimensions(int32_t width, int32_t height) MOZ_OVERRIDE; michael@0: NS_IMETHOD InitializeWithSurface(nsIDocShell *docShell, gfxASurface *surface, int32_t width, int32_t height) MOZ_OVERRIDE michael@0: { return NS_ERROR_NOT_IMPLEMENTED; } michael@0: NS_IMETHOD Reset() MOZ_OVERRIDE michael@0: { /* (InitializeWithSurface) */ return NS_ERROR_NOT_IMPLEMENTED; } michael@0: virtual void GetImageBuffer(uint8_t** aImageBuffer, int32_t* aFormat); michael@0: NS_IMETHOD GetInputStream(const char* aMimeType, michael@0: const char16_t* aEncoderOptions, michael@0: nsIInputStream **aStream) MOZ_OVERRIDE; michael@0: mozilla::TemporaryRef GetSurfaceSnapshot(bool* aPremultAlpha) MOZ_OVERRIDE; michael@0: michael@0: NS_IMETHOD SetIsOpaque(bool b) MOZ_OVERRIDE { return NS_OK; }; michael@0: bool GetIsOpaque() MOZ_OVERRIDE { return false; } michael@0: NS_IMETHOD SetContextOptions(JSContext* aCx, michael@0: JS::Handle aOptions) MOZ_OVERRIDE; michael@0: michael@0: NS_IMETHOD SetIsIPC(bool b) MOZ_OVERRIDE { return NS_ERROR_NOT_IMPLEMENTED; } michael@0: NS_IMETHOD Redraw(const gfxRect&) { return NS_ERROR_NOT_IMPLEMENTED; } michael@0: NS_IMETHOD Swap(mozilla::ipc::Shmem& aBack, michael@0: int32_t x, int32_t y, int32_t w, int32_t h) michael@0: { return NS_ERROR_NOT_IMPLEMENTED; } michael@0: NS_IMETHOD Swap(uint32_t nativeID, michael@0: int32_t x, int32_t y, int32_t w, int32_t h) michael@0: { return NS_ERROR_NOT_IMPLEMENTED; } michael@0: michael@0: bool LoseContext(); michael@0: bool RestoreContext(); michael@0: michael@0: void SynthesizeGLError(GLenum err); michael@0: void SynthesizeGLError(GLenum err, const char *fmt, ...); michael@0: michael@0: void ErrorInvalidEnum(const char *fmt = 0, ...); michael@0: void ErrorInvalidOperation(const char *fmt = 0, ...); michael@0: void ErrorInvalidValue(const char *fmt = 0, ...); michael@0: void ErrorInvalidFramebufferOperation(const char *fmt = 0, ...); michael@0: void ErrorInvalidEnumInfo(const char *info, GLenum enumvalue); michael@0: void ErrorOutOfMemory(const char *fmt = 0, ...); michael@0: michael@0: const char *ErrorName(GLenum error); michael@0: bool IsTextureFormatCompressed(GLenum format); michael@0: michael@0: void DummyFramebufferOperation(const char *info); michael@0: michael@0: WebGLTexture *activeBoundTextureForTarget(GLenum target) const { michael@0: return target == LOCAL_GL_TEXTURE_2D ? mBound2DTextures[mActiveTexture] michael@0: : mBoundCubeMapTextures[mActiveTexture]; michael@0: } michael@0: michael@0: already_AddRefed GetCanvasLayer(nsDisplayListBuilder* aBuilder, michael@0: CanvasLayer *aOldLayer, michael@0: LayerManager *aManager) MOZ_OVERRIDE; michael@0: michael@0: // Note that 'clean' here refers to its invalidation state, not the michael@0: // contents of the buffer. michael@0: void MarkContextClean() MOZ_OVERRIDE { mInvalidated = false; } michael@0: michael@0: gl::GLContext* GL() const { return gl; } michael@0: michael@0: bool IsPremultAlpha() const { return mOptions.premultipliedAlpha; } michael@0: michael@0: bool PresentScreenBuffer(); michael@0: michael@0: // a number that increments every time we have an event that causes michael@0: // all context resources to be lost. michael@0: uint32_t Generation() { return mGeneration.value(); } michael@0: michael@0: // Returns null if the current bound FB is not likely complete. michael@0: const WebGLRectangleObject* CurValidFBRectObject() const; michael@0: michael@0: static const size_t sMaxColorAttachments = 16; michael@0: michael@0: // This is similar to GLContext::ClearSafely, but tries to minimize the michael@0: // amount of work it does. michael@0: // It only clears the buffers we specify, and can reset its state without michael@0: // first having to query anything, as WebGL knows its state at all times. michael@0: void ForceClearFramebufferWithDefaultValues(GLbitfield mask, const bool colorAttachmentsMask[sMaxColorAttachments]); michael@0: michael@0: // Calls ForceClearFramebufferWithDefaultValues() for the Context's 'screen'. michael@0: void ClearScreen(); michael@0: void ClearBackbufferIfNeeded(); michael@0: michael@0: bool MinCapabilityMode() const { return mMinCapability; } michael@0: michael@0: void RobustnessTimerCallback(nsITimer* timer); michael@0: static void RobustnessTimerCallbackStatic(nsITimer* timer, void *thisPointer); michael@0: void SetupContextLossTimer(); michael@0: void TerminateContextLossTimer(); michael@0: michael@0: // WebIDL WebGLRenderingContext API michael@0: dom::HTMLCanvasElement* GetCanvas() const { return mCanvasElement; } michael@0: GLsizei DrawingBufferWidth() const { return IsContextLost() ? 0 : mWidth; } michael@0: GLsizei DrawingBufferHeight() const { return IsContextLost() ? 0 : mHeight; } michael@0: michael@0: void GetContextAttributes(dom::Nullable& retval); michael@0: bool IsContextLost() const { return mContextStatus != ContextNotLost; } michael@0: void GetSupportedExtensions(JSContext *cx, dom::Nullable< nsTArray > &retval); michael@0: void GetExtension(JSContext* cx, const nsAString& aName, michael@0: JS::MutableHandle aRetval, michael@0: ErrorResult& rv); michael@0: void ActiveTexture(GLenum texture); michael@0: void AttachShader(WebGLProgram* program, WebGLShader* shader); michael@0: void BindAttribLocation(WebGLProgram* program, GLuint location, michael@0: const nsAString& name); michael@0: void BindFramebuffer(GLenum target, WebGLFramebuffer* wfb); michael@0: void BindRenderbuffer(GLenum target, WebGLRenderbuffer* wrb); michael@0: void BindTexture(GLenum target, WebGLTexture *tex); michael@0: void BindVertexArray(WebGLVertexArray *vao); michael@0: void BlendColor(GLclampf r, GLclampf g, GLclampf b, GLclampf a); michael@0: void BlendEquation(GLenum mode); michael@0: void BlendEquationSeparate(GLenum modeRGB, GLenum modeAlpha); michael@0: void BlendFunc(GLenum sfactor, GLenum dfactor); michael@0: void BlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, michael@0: GLenum srcAlpha, GLenum dstAlpha); michael@0: GLenum CheckFramebufferStatus(GLenum target); michael@0: void Clear(GLbitfield mask); michael@0: void ClearColor(GLclampf r, GLclampf g, GLclampf b, GLclampf a); michael@0: void ClearDepth(GLclampf v); michael@0: void ClearStencil(GLint v); michael@0: void ColorMask(WebGLboolean r, WebGLboolean g, WebGLboolean b, WebGLboolean a); michael@0: void CompileShader(WebGLShader *shader); michael@0: void CompressedTexImage2D(GLenum target, GLint level, michael@0: GLenum internalformat, GLsizei width, michael@0: GLsizei height, GLint border, michael@0: const dom::ArrayBufferView& view); michael@0: void CompressedTexSubImage2D(GLenum target, GLint level, michael@0: GLint xoffset, GLint yoffset, michael@0: GLsizei width, GLsizei height, michael@0: GLenum format, michael@0: const dom::ArrayBufferView& view); michael@0: void CopyTexImage2D(GLenum target, GLint level, michael@0: GLenum internalformat, GLint x, GLint y, michael@0: GLsizei width, GLsizei height, GLint border); michael@0: void CopyTexSubImage2D(GLenum target, GLint level, GLint xoffset, michael@0: GLint yoffset, GLint x, GLint y, michael@0: GLsizei width, GLsizei height); michael@0: already_AddRefed CreateFramebuffer(); michael@0: already_AddRefed CreateProgram(); michael@0: already_AddRefed CreateRenderbuffer(); michael@0: already_AddRefed CreateTexture(); michael@0: already_AddRefed CreateShader(GLenum type); michael@0: already_AddRefed CreateVertexArray(); michael@0: void CullFace(GLenum face); michael@0: void DeleteFramebuffer(WebGLFramebuffer *fbuf); michael@0: void DeleteProgram(WebGLProgram *prog); michael@0: void DeleteRenderbuffer(WebGLRenderbuffer *rbuf); michael@0: void DeleteShader(WebGLShader *shader); michael@0: void DeleteVertexArray(WebGLVertexArray *vao); michael@0: void DeleteTexture(WebGLTexture *tex); michael@0: void DepthFunc(GLenum func); michael@0: void DepthMask(WebGLboolean b); michael@0: void DepthRange(GLclampf zNear, GLclampf zFar); michael@0: void DetachShader(WebGLProgram *program, WebGLShader *shader); michael@0: void DrawBuffers(const dom::Sequence& buffers); michael@0: void Flush(); michael@0: void Finish(); michael@0: void FramebufferRenderbuffer(GLenum target, GLenum attachment, michael@0: GLenum rbtarget, WebGLRenderbuffer *wrb); michael@0: void FramebufferTexture2D(GLenum target, GLenum attachment, michael@0: GLenum textarget, WebGLTexture *tobj, michael@0: GLint level); michael@0: void FrontFace(GLenum mode); michael@0: void GenerateMipmap(GLenum target); michael@0: already_AddRefed GetActiveAttrib(WebGLProgram *prog, michael@0: GLuint index); michael@0: already_AddRefed GetActiveUniform(WebGLProgram *prog, michael@0: GLuint index); michael@0: void GetAttachedShaders(WebGLProgram* prog, michael@0: dom::Nullable< nsTArray > &retval); michael@0: GLint GetAttribLocation(WebGLProgram* prog, const nsAString& name); michael@0: JS::Value GetBufferParameter(GLenum target, GLenum pname); michael@0: void GetBufferParameter(JSContext* /* unused */, GLenum target, michael@0: GLenum pname, michael@0: JS::MutableHandle retval) { michael@0: retval.set(GetBufferParameter(target, pname)); michael@0: } michael@0: GLenum GetError(); michael@0: JS::Value GetFramebufferAttachmentParameter(JSContext* cx, michael@0: GLenum target, michael@0: GLenum attachment, michael@0: GLenum pname, michael@0: ErrorResult& rv); michael@0: void GetFramebufferAttachmentParameter(JSContext* cx, michael@0: GLenum target, michael@0: GLenum attachment, michael@0: GLenum pname, michael@0: JS::MutableHandle retval, michael@0: ErrorResult& rv) { michael@0: retval.set(GetFramebufferAttachmentParameter(cx, target, attachment, michael@0: pname, rv)); michael@0: } michael@0: JS::Value GetProgramParameter(WebGLProgram *prog, GLenum pname); michael@0: void GetProgramParameter(JSContext* /* unused */, WebGLProgram *prog, michael@0: GLenum pname, michael@0: JS::MutableHandle retval) { michael@0: retval.set(GetProgramParameter(prog, pname)); michael@0: } michael@0: void GetProgramInfoLog(WebGLProgram *prog, nsACString& retval); michael@0: void GetProgramInfoLog(WebGLProgram *prog, nsAString& retval); michael@0: JS::Value GetRenderbufferParameter(GLenum target, GLenum pname); michael@0: void GetRenderbufferParameter(JSContext* /* unused */, michael@0: GLenum target, GLenum pname, michael@0: JS::MutableHandle retval) { michael@0: retval.set(GetRenderbufferParameter(target, pname)); michael@0: } michael@0: JS::Value GetShaderParameter(WebGLShader *shader, GLenum pname); michael@0: void GetShaderParameter(JSContext* /* unused */, WebGLShader *shader, michael@0: GLenum pname, michael@0: JS::MutableHandle retval) { michael@0: retval.set(GetShaderParameter(shader, pname)); michael@0: } michael@0: already_AddRefed michael@0: GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype); michael@0: void GetShaderInfoLog(WebGLShader *shader, nsACString& retval); michael@0: void GetShaderInfoLog(WebGLShader *shader, nsAString& retval); michael@0: void GetShaderSource(WebGLShader *shader, nsAString& retval); michael@0: void GetShaderTranslatedSource(WebGLShader *shader, nsAString& retval); michael@0: JS::Value GetTexParameter(GLenum target, GLenum pname); michael@0: void GetTexParameter(JSContext * /* unused */, GLenum target, michael@0: GLenum pname, michael@0: JS::MutableHandle retval) { michael@0: retval.set(GetTexParameter(target, pname)); michael@0: } michael@0: JS::Value GetUniform(JSContext* cx, WebGLProgram *prog, michael@0: WebGLUniformLocation *location); michael@0: void GetUniform(JSContext* cx, WebGLProgram *prog, michael@0: WebGLUniformLocation *location, michael@0: JS::MutableHandle retval) { michael@0: retval.set(GetUniform(cx, prog, location)); michael@0: } michael@0: already_AddRefed michael@0: GetUniformLocation(WebGLProgram *prog, const nsAString& name); michael@0: void Hint(GLenum target, GLenum mode); michael@0: bool IsFramebuffer(WebGLFramebuffer *fb); michael@0: bool IsProgram(WebGLProgram *prog); michael@0: bool IsRenderbuffer(WebGLRenderbuffer *rb); michael@0: bool IsShader(WebGLShader *shader); michael@0: bool IsTexture(WebGLTexture *tex); michael@0: bool IsVertexArray(WebGLVertexArray *vao); michael@0: void LineWidth(GLfloat width); michael@0: void LinkProgram(WebGLProgram *program); michael@0: void PixelStorei(GLenum pname, GLint param); michael@0: void PolygonOffset(GLfloat factor, GLfloat units); michael@0: void ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, michael@0: GLenum format, GLenum type, michael@0: const Nullable &pixels, michael@0: ErrorResult& rv); michael@0: void RenderbufferStorage(GLenum target, GLenum internalformat, michael@0: GLsizei width, GLsizei height); michael@0: void SampleCoverage(GLclampf value, WebGLboolean invert); michael@0: void Scissor(GLint x, GLint y, GLsizei width, GLsizei height); michael@0: void ShaderSource(WebGLShader *shader, const nsAString& source); michael@0: void StencilFunc(GLenum func, GLint ref, GLuint mask); michael@0: void StencilFuncSeparate(GLenum face, GLenum func, GLint ref, michael@0: GLuint mask); michael@0: void StencilMask(GLuint mask); michael@0: void StencilMaskSeparate(GLenum face, GLuint mask); michael@0: void StencilOp(GLenum sfail, GLenum dpfail, GLenum dppass); michael@0: void StencilOpSeparate(GLenum face, GLenum sfail, GLenum dpfail, michael@0: GLenum dppass); michael@0: void TexImage2D(GLenum target, GLint level, michael@0: GLenum internalformat, GLsizei width, michael@0: GLsizei height, GLint border, GLenum format, michael@0: GLenum type, michael@0: const Nullable &pixels, michael@0: ErrorResult& rv); michael@0: void TexImage2D(GLenum target, GLint level, michael@0: GLenum internalformat, GLenum format, GLenum type, michael@0: dom::ImageData* pixels, ErrorResult& rv); michael@0: // Allow whatever element types the bindings are willing to pass michael@0: // us in TexImage2D michael@0: template michael@0: void TexImage2D(GLenum target, GLint level, michael@0: GLenum internalformat, GLenum format, GLenum type, michael@0: ElementType& elt, ErrorResult& rv) michael@0: { michael@0: if (IsContextLost()) michael@0: return; michael@0: RefPtr data; michael@0: WebGLTexelFormat srcFormat; michael@0: nsLayoutUtils::SurfaceFromElementResult res = SurfaceFromElement(elt); michael@0: rv = SurfaceFromElementResultToImageSurface(res, data, michael@0: &srcFormat); michael@0: if (rv.Failed() || !data) michael@0: return; michael@0: michael@0: gfx::IntSize size = data->GetSize(); michael@0: uint32_t byteLength = data->Stride() * size.height; michael@0: return TexImage2D_base(target, level, internalformat, michael@0: size.width, size.height, data->Stride(), michael@0: 0, format, type, data->GetData(), byteLength, michael@0: -1, srcFormat, mPixelStorePremultiplyAlpha); michael@0: } michael@0: void TexParameterf(GLenum target, GLenum pname, GLfloat param) { michael@0: TexParameter_base(target, pname, nullptr, ¶m); michael@0: } michael@0: void TexParameteri(GLenum target, GLenum pname, GLint param) { michael@0: TexParameter_base(target, pname, ¶m, nullptr); michael@0: } michael@0: michael@0: void TexSubImage2D(GLenum target, GLint level, michael@0: GLint xoffset, GLint yoffset, michael@0: GLsizei width, GLsizei height, GLenum format, michael@0: GLenum type, michael@0: const Nullable &pixels, michael@0: ErrorResult& rv); michael@0: void TexSubImage2D(GLenum target, GLint level, michael@0: GLint xoffset, GLint yoffset, GLenum format, michael@0: GLenum type, dom::ImageData* pixels, ErrorResult& rv); michael@0: // Allow whatever element types the bindings are willing to pass michael@0: // us in TexSubImage2D michael@0: template michael@0: void TexSubImage2D(GLenum target, GLint level, michael@0: GLint xoffset, GLint yoffset, GLenum format, michael@0: GLenum type, ElementType& elt, ErrorResult& rv) michael@0: { michael@0: if (IsContextLost()) michael@0: return; michael@0: RefPtr data; michael@0: WebGLTexelFormat srcFormat; michael@0: nsLayoutUtils::SurfaceFromElementResult res = SurfaceFromElement(elt); michael@0: rv = SurfaceFromElementResultToImageSurface(res, data, michael@0: &srcFormat); michael@0: if (rv.Failed() || !data) michael@0: return; michael@0: michael@0: gfx::IntSize size = data->GetSize(); michael@0: uint32_t byteLength = data->Stride() * size.height; michael@0: return TexSubImage2D_base(target, level, xoffset, yoffset, michael@0: size.width, size.height, michael@0: data->Stride(), format, type, michael@0: data->GetData(), byteLength, michael@0: -1, srcFormat, mPixelStorePremultiplyAlpha); michael@0: michael@0: } michael@0: michael@0: void Uniform1i(WebGLUniformLocation* location, GLint x); michael@0: void Uniform2i(WebGLUniformLocation* location, GLint x, GLint y); michael@0: void Uniform3i(WebGLUniformLocation* location, GLint x, GLint y, michael@0: GLint z); michael@0: void Uniform4i(WebGLUniformLocation* location, GLint x, GLint y, michael@0: GLint z, GLint w); michael@0: michael@0: void Uniform1f(WebGLUniformLocation* location, GLfloat x); michael@0: void Uniform2f(WebGLUniformLocation* location, GLfloat x, GLfloat y); michael@0: void Uniform3f(WebGLUniformLocation* location, GLfloat x, GLfloat y, michael@0: GLfloat z); michael@0: void Uniform4f(WebGLUniformLocation* location, GLfloat x, GLfloat y, michael@0: GLfloat z, GLfloat w); michael@0: michael@0: void Uniform1iv(WebGLUniformLocation* location, michael@0: const dom::Int32Array& arr) { michael@0: arr.ComputeLengthAndData(); michael@0: Uniform1iv_base(location, arr.Length(), arr.Data()); michael@0: } michael@0: void Uniform1iv(WebGLUniformLocation* location, michael@0: const dom::Sequence& arr) { michael@0: Uniform1iv_base(location, arr.Length(), arr.Elements()); michael@0: } michael@0: void Uniform1iv_base(WebGLUniformLocation* location, uint32_t arrayLength, michael@0: const GLint* data); michael@0: michael@0: void Uniform2iv(WebGLUniformLocation* location, michael@0: const dom::Int32Array& arr) { michael@0: arr.ComputeLengthAndData(); michael@0: Uniform2iv_base(location, arr.Length(), arr.Data()); michael@0: } michael@0: void Uniform2iv(WebGLUniformLocation* location, michael@0: const dom::Sequence& arr) { michael@0: Uniform2iv_base(location, arr.Length(), arr.Elements()); michael@0: } michael@0: void Uniform2iv_base(WebGLUniformLocation* location, uint32_t arrayLength, michael@0: const GLint* data); michael@0: michael@0: void Uniform3iv(WebGLUniformLocation* location, michael@0: const dom::Int32Array& arr) { michael@0: arr.ComputeLengthAndData(); michael@0: Uniform3iv_base(location, arr.Length(), arr.Data()); michael@0: } michael@0: void Uniform3iv(WebGLUniformLocation* location, michael@0: const dom::Sequence& arr) { michael@0: Uniform3iv_base(location, arr.Length(), arr.Elements()); michael@0: } michael@0: void Uniform3iv_base(WebGLUniformLocation* location, uint32_t arrayLength, michael@0: const GLint* data); michael@0: michael@0: void Uniform4iv(WebGLUniformLocation* location, michael@0: const dom::Int32Array& arr) { michael@0: arr.ComputeLengthAndData(); michael@0: Uniform4iv_base(location, arr.Length(), arr.Data()); michael@0: } michael@0: void Uniform4iv(WebGLUniformLocation* location, michael@0: const dom::Sequence& arr) { michael@0: Uniform4iv_base(location, arr.Length(), arr.Elements()); michael@0: } michael@0: void Uniform4iv_base(WebGLUniformLocation* location, uint32_t arrayLength, michael@0: const GLint* data); michael@0: michael@0: void Uniform1fv(WebGLUniformLocation* location, michael@0: const dom::Float32Array& arr) { michael@0: arr.ComputeLengthAndData(); michael@0: Uniform1fv_base(location, arr.Length(), arr.Data()); michael@0: } michael@0: void Uniform1fv(WebGLUniformLocation* location, michael@0: const dom::Sequence& arr) { michael@0: Uniform1fv_base(location, arr.Length(), arr.Elements()); michael@0: } michael@0: void Uniform1fv_base(WebGLUniformLocation* location, uint32_t arrayLength, michael@0: const GLfloat* data); michael@0: michael@0: void Uniform2fv(WebGLUniformLocation* location, michael@0: const dom::Float32Array& arr) { michael@0: arr.ComputeLengthAndData(); michael@0: Uniform2fv_base(location, arr.Length(), arr.Data()); michael@0: } michael@0: void Uniform2fv(WebGLUniformLocation* location, michael@0: const dom::Sequence& arr) { michael@0: Uniform2fv_base(location, arr.Length(), arr.Elements()); michael@0: } michael@0: void Uniform2fv_base(WebGLUniformLocation* location, uint32_t arrayLength, michael@0: const GLfloat* data); michael@0: michael@0: void Uniform3fv(WebGLUniformLocation* location, michael@0: const dom::Float32Array& arr) { michael@0: arr.ComputeLengthAndData(); michael@0: Uniform3fv_base(location, arr.Length(), arr.Data()); michael@0: } michael@0: void Uniform3fv(WebGLUniformLocation* location, michael@0: const dom::Sequence& arr) { michael@0: Uniform3fv_base(location, arr.Length(), arr.Elements()); michael@0: } michael@0: void Uniform3fv_base(WebGLUniformLocation* location, uint32_t arrayLength, michael@0: const GLfloat* data); michael@0: michael@0: void Uniform4fv(WebGLUniformLocation* location, michael@0: const dom::Float32Array& arr) { michael@0: arr.ComputeLengthAndData(); michael@0: Uniform4fv_base(location, arr.Length(), arr.Data()); michael@0: } michael@0: void Uniform4fv(WebGLUniformLocation* location, michael@0: const dom::Sequence& arr) { michael@0: Uniform4fv_base(location, arr.Length(), arr.Elements()); michael@0: } michael@0: void Uniform4fv_base(WebGLUniformLocation* location, uint32_t arrayLength, michael@0: const GLfloat* data); michael@0: michael@0: void UniformMatrix2fv(WebGLUniformLocation* location, michael@0: WebGLboolean transpose, michael@0: const dom::Float32Array &value) { michael@0: value.ComputeLengthAndData(); michael@0: UniformMatrix2fv_base(location, transpose, value.Length(), value.Data()); michael@0: } michael@0: void UniformMatrix2fv(WebGLUniformLocation* location, michael@0: WebGLboolean transpose, michael@0: const dom::Sequence &value) { michael@0: UniformMatrix2fv_base(location, transpose, value.Length(), michael@0: value.Elements()); michael@0: } michael@0: void UniformMatrix2fv_base(WebGLUniformLocation* location, michael@0: WebGLboolean transpose, uint32_t arrayLength, michael@0: const float* data); michael@0: michael@0: void UniformMatrix3fv(WebGLUniformLocation* location, michael@0: WebGLboolean transpose, michael@0: const dom::Float32Array &value) { michael@0: value.ComputeLengthAndData(); michael@0: UniformMatrix3fv_base(location, transpose, value.Length(), value.Data()); michael@0: } michael@0: void UniformMatrix3fv(WebGLUniformLocation* location, michael@0: WebGLboolean transpose, michael@0: const dom::Sequence &value) { michael@0: UniformMatrix3fv_base(location, transpose, value.Length(), michael@0: value.Elements()); michael@0: } michael@0: void UniformMatrix3fv_base(WebGLUniformLocation* location, michael@0: WebGLboolean transpose, uint32_t arrayLength, michael@0: const float* data); michael@0: michael@0: void UniformMatrix4fv(WebGLUniformLocation* location, michael@0: WebGLboolean transpose, michael@0: const dom::Float32Array &value) { michael@0: value.ComputeLengthAndData(); michael@0: UniformMatrix4fv_base(location, transpose, value.Length(), value.Data()); michael@0: } michael@0: void UniformMatrix4fv(WebGLUniformLocation* location, michael@0: WebGLboolean transpose, michael@0: const dom::Sequence &value) { michael@0: UniformMatrix4fv_base(location, transpose, value.Length(), michael@0: value.Elements()); michael@0: } michael@0: void UniformMatrix4fv_base(WebGLUniformLocation* location, michael@0: WebGLboolean transpose, uint32_t arrayLength, michael@0: const float* data); michael@0: michael@0: void UseProgram(WebGLProgram *prog); michael@0: bool ValidateAttribArraySetter(const char* name, uint32_t cnt, uint32_t arrayLength); michael@0: bool ValidateUniformArraySetter(const char* name, uint32_t expectedElemSize, WebGLUniformLocation *location_object, michael@0: GLint& location, uint32_t& numElementsToUpload, uint32_t arrayLength); michael@0: bool ValidateUniformMatrixArraySetter(const char* name, int dim, WebGLUniformLocation *location_object, michael@0: GLint& location, uint32_t& numElementsToUpload, uint32_t arrayLength, michael@0: WebGLboolean aTranspose); michael@0: bool ValidateUniformSetter(const char* name, WebGLUniformLocation *location_object, GLint& location); michael@0: void ValidateProgram(WebGLProgram *prog); michael@0: bool ValidateUniformLocation(const char* info, WebGLUniformLocation *location_object); michael@0: bool ValidateSamplerUniformSetter(const char* info, michael@0: WebGLUniformLocation *location, michael@0: GLint value); michael@0: michael@0: void Viewport(GLint x, GLint y, GLsizei width, GLsizei height); michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // Asynchronous Queries (WebGLContextAsyncQueries.cpp) michael@0: public: michael@0: already_AddRefed CreateQuery(); michael@0: void DeleteQuery(WebGLQuery *query); michael@0: void BeginQuery(GLenum target, WebGLQuery *query); michael@0: void EndQuery(GLenum target); michael@0: bool IsQuery(WebGLQuery *query); michael@0: already_AddRefed GetQuery(GLenum target, GLenum pname); michael@0: JS::Value GetQueryObject(JSContext* cx, WebGLQuery *query, GLenum pname); michael@0: void GetQueryObject(JSContext* cx, WebGLQuery *query, GLenum pname, michael@0: JS::MutableHandle retval) { michael@0: retval.set(GetQueryObject(cx, query, pname)); michael@0: } michael@0: michael@0: private: michael@0: // ANY_SAMPLES_PASSED(_CONSERVATIVE) slot michael@0: WebGLRefPtr mActiveOcclusionQuery; michael@0: michael@0: // LOCAL_GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN slot michael@0: WebGLRefPtr mActiveTransformFeedbackQuery; michael@0: michael@0: WebGLRefPtr* GetQueryTargetSlot(GLenum target, const char* infos); michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // Buffer Objects (WebGLContextBuffers.cpp) michael@0: public: michael@0: void BindBuffer(GLenum target, WebGLBuffer* buf); michael@0: void BindBufferBase(GLenum target, GLuint index, WebGLBuffer* buffer); michael@0: void BindBufferRange(GLenum target, GLuint index, WebGLBuffer* buffer, michael@0: WebGLintptr offset, WebGLsizeiptr size); michael@0: void BufferData(GLenum target, WebGLsizeiptr size, GLenum usage); michael@0: void BufferData(GLenum target, const dom::ArrayBufferView &data, michael@0: GLenum usage); michael@0: void BufferData(GLenum target, michael@0: const Nullable &maybeData, michael@0: GLenum usage); michael@0: void BufferSubData(GLenum target, WebGLsizeiptr byteOffset, michael@0: const dom::ArrayBufferView &data); michael@0: void BufferSubData(GLenum target, WebGLsizeiptr byteOffset, michael@0: const Nullable &maybeData); michael@0: already_AddRefed CreateBuffer(); michael@0: void DeleteBuffer(WebGLBuffer *buf); michael@0: bool IsBuffer(WebGLBuffer *buffer); michael@0: michael@0: private: michael@0: // ARRAY_BUFFER slot michael@0: WebGLRefPtr mBoundArrayBuffer; michael@0: michael@0: // TRANSFORM_FEEDBACK_BUFFER slot michael@0: WebGLRefPtr mBoundTransformFeedbackBuffer; michael@0: michael@0: // these two functions emit INVALID_ENUM for invalid `target`. michael@0: WebGLRefPtr* GetBufferSlotByTarget(GLenum target, const char* infos); michael@0: WebGLRefPtr* GetBufferSlotByTargetIndexed(GLenum target, GLuint index, const char* infos); michael@0: bool ValidateBufferUsageEnum(GLenum target, const char* infos); michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // State and State Requests (WebGLContextState.cpp) michael@0: public: michael@0: void Disable(GLenum cap); michael@0: void Enable(GLenum cap); michael@0: JS::Value GetParameter(JSContext* cx, GLenum pname, ErrorResult& rv); michael@0: void GetParameter(JSContext* cx, GLenum pname, michael@0: JS::MutableHandle retval, ErrorResult& rv) { michael@0: retval.set(GetParameter(cx, pname, rv)); michael@0: } michael@0: void GetParameterIndexed(JSContext* cx, GLenum pname, GLuint index, michael@0: JS::MutableHandle retval); michael@0: bool IsEnabled(GLenum cap); michael@0: michael@0: private: michael@0: // State tracking slots michael@0: realGLboolean mDitherEnabled; michael@0: realGLboolean mRasterizerDiscardEnabled; michael@0: realGLboolean mScissorTestEnabled; michael@0: michael@0: bool ValidateCapabilityEnum(GLenum cap, const char* info); michael@0: realGLboolean* GetStateTrackingSlot(GLenum cap); michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // Vertices Feature (WebGLContextVertices.cpp) michael@0: public: michael@0: void DrawArrays(GLenum mode, GLint first, GLsizei count); michael@0: void DrawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei primcount); michael@0: void DrawElements(GLenum mode, GLsizei count, GLenum type, WebGLintptr byteOffset); michael@0: void DrawElementsInstanced(GLenum mode, GLsizei count, GLenum type, michael@0: WebGLintptr byteOffset, GLsizei primcount); michael@0: michael@0: void EnableVertexAttribArray(GLuint index); michael@0: void DisableVertexAttribArray(GLuint index); michael@0: michael@0: JS::Value GetVertexAttrib(JSContext* cx, GLuint index, GLenum pname, michael@0: ErrorResult& rv); michael@0: void GetVertexAttrib(JSContext* cx, GLuint index, GLenum pname, michael@0: JS::MutableHandle retval, michael@0: ErrorResult& rv) { michael@0: retval.set(GetVertexAttrib(cx, index, pname, rv)); michael@0: } michael@0: WebGLsizeiptr GetVertexAttribOffset(GLuint index, GLenum pname); michael@0: michael@0: void VertexAttrib1f(GLuint index, GLfloat x0); michael@0: void VertexAttrib2f(GLuint index, GLfloat x0, GLfloat x1); michael@0: void VertexAttrib3f(GLuint index, GLfloat x0, GLfloat x1, michael@0: GLfloat x2); michael@0: void VertexAttrib4f(GLuint index, GLfloat x0, GLfloat x1, michael@0: GLfloat x2, GLfloat x3); michael@0: michael@0: void VertexAttrib1fv(GLuint idx, const dom::Float32Array &arr) { michael@0: arr.ComputeLengthAndData(); michael@0: VertexAttrib1fv_base(idx, arr.Length(), arr.Data()); michael@0: } michael@0: void VertexAttrib1fv(GLuint idx, const dom::Sequence& arr) { michael@0: VertexAttrib1fv_base(idx, arr.Length(), arr.Elements()); michael@0: } michael@0: michael@0: void VertexAttrib2fv(GLuint idx, const dom::Float32Array &arr) { michael@0: arr.ComputeLengthAndData(); michael@0: VertexAttrib2fv_base(idx, arr.Length(), arr.Data()); michael@0: } michael@0: void VertexAttrib2fv(GLuint idx, const dom::Sequence& arr) { michael@0: VertexAttrib2fv_base(idx, arr.Length(), arr.Elements()); michael@0: } michael@0: michael@0: void VertexAttrib3fv(GLuint idx, const dom::Float32Array &arr) { michael@0: arr.ComputeLengthAndData(); michael@0: VertexAttrib3fv_base(idx, arr.Length(), arr.Data()); michael@0: } michael@0: void VertexAttrib3fv(GLuint idx, const dom::Sequence& arr) { michael@0: VertexAttrib3fv_base(idx, arr.Length(), arr.Elements()); michael@0: } michael@0: michael@0: void VertexAttrib4fv(GLuint idx, const dom::Float32Array &arr) { michael@0: arr.ComputeLengthAndData(); michael@0: VertexAttrib4fv_base(idx, arr.Length(), arr.Data()); michael@0: } michael@0: void VertexAttrib4fv(GLuint idx, const dom::Sequence& arr) { michael@0: VertexAttrib4fv_base(idx, arr.Length(), arr.Elements()); michael@0: } michael@0: michael@0: void VertexAttribPointer(GLuint index, GLint size, GLenum type, michael@0: WebGLboolean normalized, GLsizei stride, michael@0: WebGLintptr byteOffset); michael@0: void VertexAttribDivisor(GLuint index, GLuint divisor); michael@0: michael@0: private: michael@0: // Cache the max number of vertices and instances that can be read from michael@0: // bound VBOs (result of ValidateBuffers). michael@0: bool mBufferFetchingIsVerified; michael@0: bool mBufferFetchingHasPerVertex; michael@0: uint32_t mMaxFetchedVertices; michael@0: uint32_t mMaxFetchedInstances; michael@0: michael@0: inline void InvalidateBufferFetching() michael@0: { michael@0: mBufferFetchingIsVerified = false; michael@0: mBufferFetchingHasPerVertex = false; michael@0: mMaxFetchedVertices = 0; michael@0: mMaxFetchedInstances = 0; michael@0: } michael@0: michael@0: bool DrawArrays_check(GLint first, GLsizei count, GLsizei primcount, const char* info); michael@0: bool DrawElements_check(GLsizei count, GLenum type, WebGLintptr byteOffset, michael@0: GLsizei primcount, const char* info, michael@0: GLuint* out_upperBound = nullptr); michael@0: bool DrawInstanced_check(const char* info); michael@0: void Draw_cleanup(); michael@0: michael@0: void VertexAttrib1fv_base(GLuint idx, uint32_t arrayLength, const GLfloat* ptr); michael@0: void VertexAttrib2fv_base(GLuint idx, uint32_t arrayLength, const GLfloat* ptr); michael@0: void VertexAttrib3fv_base(GLuint idx, uint32_t arrayLength, const GLfloat* ptr); michael@0: void VertexAttrib4fv_base(GLuint idx, uint32_t arrayLength, const GLfloat* ptr); michael@0: michael@0: bool ValidateBufferFetching(const char *info); michael@0: bool BindArrayAttribToLocation0(WebGLProgram *program); michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // PROTECTED michael@0: protected: michael@0: void SetFakeBlackStatus(WebGLContextFakeBlackStatus x) { michael@0: mFakeBlackStatus = x; michael@0: } michael@0: // Returns the current fake-black-status, except if it was Unknown, michael@0: // in which case this function resolves it first, so it never returns Unknown. michael@0: WebGLContextFakeBlackStatus ResolvedFakeBlackStatus(); michael@0: michael@0: void BindFakeBlackTextures(); michael@0: void UnbindFakeBlackTextures(); michael@0: michael@0: WebGLVertexAttrib0Status WhatDoesVertexAttrib0Need(); michael@0: bool DoFakeVertexAttrib0(GLuint vertexCount); michael@0: void UndoFakeVertexAttrib0(); michael@0: michael@0: static CheckedUint32 GetImageSize(GLsizei height, michael@0: GLsizei width, michael@0: uint32_t pixelSize, michael@0: uint32_t alignment); michael@0: michael@0: // Returns x rounded to the next highest multiple of y. michael@0: static CheckedUint32 RoundedToNextMultipleOf(CheckedUint32 x, CheckedUint32 y) { michael@0: return ((x + y - 1) / y) * y; michael@0: } michael@0: michael@0: nsRefPtr gl; michael@0: michael@0: CheckedUint32 mGeneration; michael@0: michael@0: WebGLContextOptions mOptions; michael@0: michael@0: bool mInvalidated; michael@0: bool mResetLayer; michael@0: bool mOptionsFrozen; michael@0: bool mMinCapability; michael@0: bool mDisableExtensions; michael@0: bool mHasRobustness; michael@0: bool mIsMesa; michael@0: bool mLoseContextOnHeapMinimize; michael@0: bool mCanLoseContextInForeground; michael@0: bool mShouldPresent; michael@0: bool mBackbufferNeedsClear; michael@0: bool mDisableFragHighP; michael@0: michael@0: template michael@0: void DeleteWebGLObjectsArray(nsTArray& array); michael@0: michael@0: GLuint mActiveTexture; michael@0: michael@0: // glGetError sources: michael@0: bool mEmitContextLostErrorOnce; michael@0: GLenum mWebGLError; michael@0: GLenum mUnderlyingGLError; michael@0: GLenum GetAndFlushUnderlyingGLErrors(); michael@0: michael@0: // whether shader validation is supported michael@0: bool mShaderValidation; michael@0: michael@0: // some GL constants michael@0: int32_t mGLMaxVertexAttribs; michael@0: int32_t mGLMaxTextureUnits; michael@0: int32_t mGLMaxTextureSize; michael@0: int32_t mGLMaxCubeMapTextureSize; michael@0: int32_t mGLMaxRenderbufferSize; michael@0: int32_t mGLMaxTextureImageUnits; michael@0: int32_t mGLMaxVertexTextureImageUnits; michael@0: int32_t mGLMaxVaryingVectors; michael@0: int32_t mGLMaxFragmentUniformVectors; michael@0: int32_t mGLMaxVertexUniformVectors; michael@0: int32_t mGLMaxColorAttachments; michael@0: int32_t mGLMaxDrawBuffers; michael@0: uint32_t mGLMaxTransformFeedbackSeparateAttribs; michael@0: michael@0: // Represents current status of the context with respect to context loss. michael@0: // That is, whether the context is lost, and what part of the context loss michael@0: // process we currently are at. michael@0: // This is used to support the WebGL spec's asyncronous nature in handling michael@0: // context loss. michael@0: enum ContextStatus { michael@0: // The context is stable; there either are none or we don't know of any. michael@0: ContextNotLost, michael@0: // The context has been lost, but we have not yet sent an event to the michael@0: // script informing it of this. michael@0: ContextLostAwaitingEvent, michael@0: // The context has been lost, and we have sent the script an event michael@0: // informing it of this. michael@0: ContextLost, michael@0: // The context is lost, an event has been sent to the script, and the michael@0: // script correctly handled the event. We are waiting for the context to michael@0: // be restored. michael@0: ContextLostAwaitingRestore michael@0: }; michael@0: michael@0: // ------------------------------------------------------------------------- michael@0: // WebGL extensions (implemented in WebGLContextExtensions.cpp) michael@0: typedef EnumeratedArray> ExtensionsArrayType; michael@0: michael@0: ExtensionsArrayType mExtensions; michael@0: michael@0: // enable an extension. the extension should not be enabled before. michael@0: void EnableExtension(WebGLExtensionID ext); michael@0: michael@0: // returns true if the extension has been enabled by calling getExtension. michael@0: bool IsExtensionEnabled(WebGLExtensionID ext) const; michael@0: michael@0: // returns true if the extension is supported for this JSContext (this decides what getSupportedExtensions exposes) michael@0: bool IsExtensionSupported(JSContext *cx, WebGLExtensionID ext) const; michael@0: bool IsExtensionSupported(WebGLExtensionID ext) const; michael@0: michael@0: static const char* GetExtensionString(WebGLExtensionID ext); michael@0: michael@0: nsTArray mCompressedTextureFormats; michael@0: michael@0: // ------------------------------------------------------------------------- michael@0: // WebGL 2 specifics (implemented in WebGL2Context.cpp) michael@0: michael@0: virtual bool IsWebGL2() const = 0; michael@0: michael@0: bool InitWebGL2(); michael@0: michael@0: michael@0: // ------------------------------------------------------------------------- michael@0: // Validation functions (implemented in WebGLContextValidate.cpp) michael@0: GLenum BaseTexFormat(GLenum internalFormat) const; michael@0: michael@0: bool InitAndValidateGL(); michael@0: bool ValidateBlendEquationEnum(GLenum cap, const char *info); michael@0: bool ValidateBlendFuncDstEnum(GLenum mode, const char *info); michael@0: bool ValidateBlendFuncSrcEnum(GLenum mode, const char *info); michael@0: bool ValidateBlendFuncEnumsCompatibility(GLenum sfactor, GLenum dfactor, const char *info); michael@0: bool ValidateTextureTargetEnum(GLenum target, const char *info); michael@0: bool ValidateComparisonEnum(GLenum target, const char *info); michael@0: bool ValidateStencilOpEnum(GLenum action, const char *info); michael@0: bool ValidateFaceEnum(GLenum face, const char *info); michael@0: bool ValidateTexInputData(GLenum type, int jsArrayType, WebGLTexImageFunc func); michael@0: bool ValidateDrawModeEnum(GLenum mode, const char *info); michael@0: bool ValidateAttribIndex(GLuint index, const char *info); michael@0: bool ValidateStencilParamsForDrawCall(); michael@0: michael@0: bool ValidateGLSLVariableName(const nsAString& name, const char *info); michael@0: bool ValidateGLSLCharacter(char16_t c); michael@0: bool ValidateGLSLString(const nsAString& string, const char *info); michael@0: michael@0: bool ValidateTexImage(GLuint dims, GLenum target, michael@0: GLint level, GLint internalFormat, michael@0: GLint xoffset, GLint yoffset, GLint zoffset, michael@0: GLint width, GLint height, GLint depth, michael@0: GLint border, GLenum format, GLenum type, michael@0: WebGLTexImageFunc func); michael@0: bool ValidateTexImageTarget(GLuint dims, GLenum target, WebGLTexImageFunc func); michael@0: bool ValidateTexImageFormat(GLenum format, WebGLTexImageFunc func); michael@0: bool ValidateTexImageType(GLenum type, WebGLTexImageFunc func); michael@0: bool ValidateTexImageFormatAndType(GLenum format, GLenum type, WebGLTexImageFunc func); michael@0: bool ValidateTexImageSize(GLenum target, GLint level, michael@0: GLint width, GLint height, GLint depth, michael@0: WebGLTexImageFunc func); michael@0: bool ValidateTexSubImageSize(GLint x, GLint y, GLint z, michael@0: GLsizei width, GLsizei height, GLsizei depth, michael@0: GLsizei baseWidth, GLsizei baseHeight, GLsizei baseDepth, michael@0: WebGLTexImageFunc func); michael@0: michael@0: bool ValidateCompTexImageSize(GLenum target, GLint level, GLenum format, michael@0: GLint xoffset, GLint yoffset, michael@0: GLsizei width, GLsizei height, michael@0: GLsizei levelWidth, GLsizei levelHeight, michael@0: WebGLTexImageFunc func); michael@0: bool ValidateCompTexImageDataSize(GLint level, GLenum format, michael@0: GLsizei width, GLsizei height, michael@0: uint32_t byteLength, WebGLTexImageFunc func); michael@0: michael@0: michael@0: static uint32_t GetBitsPerTexel(GLenum format, GLenum type); michael@0: michael@0: void Invalidate(); michael@0: void DestroyResourcesAndContext(); michael@0: michael@0: void MakeContextCurrent() const; michael@0: michael@0: // helpers michael@0: void TexImage2D_base(GLenum target, GLint level, GLenum internalformat, michael@0: GLsizei width, GLsizei height, GLsizei srcStrideOrZero, GLint border, michael@0: GLenum format, GLenum type, michael@0: void *data, uint32_t byteLength, michael@0: int jsArrayType, michael@0: WebGLTexelFormat srcFormat, bool srcPremultiplied); michael@0: void TexSubImage2D_base(GLenum target, GLint level, michael@0: GLint xoffset, GLint yoffset, michael@0: GLsizei width, GLsizei height, GLsizei srcStrideOrZero, michael@0: GLenum format, GLenum type, michael@0: void *pixels, uint32_t byteLength, michael@0: int jsArrayType, michael@0: WebGLTexelFormat srcFormat, bool srcPremultiplied); michael@0: void TexParameter_base(GLenum target, GLenum pname, michael@0: GLint *intParamPtr, GLfloat *floatParamPtr); michael@0: michael@0: void ConvertImage(size_t width, size_t height, size_t srcStride, size_t dstStride, michael@0: const uint8_t* src, uint8_t *dst, michael@0: WebGLTexelFormat srcFormat, bool srcPremultiplied, michael@0: WebGLTexelFormat dstFormat, bool dstPremultiplied, michael@0: size_t dstTexelSize); michael@0: michael@0: template michael@0: nsLayoutUtils::SurfaceFromElementResult SurfaceFromElement(ElementType* aElement) { michael@0: MOZ_ASSERT(aElement); michael@0: uint32_t flags = michael@0: nsLayoutUtils::SFE_WANT_IMAGE_SURFACE; michael@0: michael@0: if (mPixelStoreColorspaceConversion == LOCAL_GL_NONE) michael@0: flags |= nsLayoutUtils::SFE_NO_COLORSPACE_CONVERSION; michael@0: if (!mPixelStorePremultiplyAlpha) michael@0: flags |= nsLayoutUtils::SFE_PREFER_NO_PREMULTIPLY_ALPHA; michael@0: return nsLayoutUtils::SurfaceFromElement(aElement, flags); michael@0: } michael@0: template michael@0: nsLayoutUtils::SurfaceFromElementResult SurfaceFromElement(ElementType& aElement) michael@0: { michael@0: return SurfaceFromElement(&aElement); michael@0: } michael@0: michael@0: nsresult SurfaceFromElementResultToImageSurface(nsLayoutUtils::SurfaceFromElementResult& res, michael@0: RefPtr& imageOut, michael@0: WebGLTexelFormat *format); michael@0: michael@0: void CopyTexSubImage2D_base(GLenum target, michael@0: GLint level, michael@0: GLenum internalformat, michael@0: GLint xoffset, michael@0: GLint yoffset, michael@0: GLint x, michael@0: GLint y, michael@0: GLsizei width, michael@0: GLsizei height, michael@0: bool sub); michael@0: michael@0: // Returns false if aObject is null or not valid michael@0: template michael@0: bool ValidateObject(const char* info, ObjectType *aObject); michael@0: // Returns false if aObject is not valid. Considers null to be valid. michael@0: template michael@0: bool ValidateObjectAllowNull(const char* info, ObjectType *aObject); michael@0: // Returns false if aObject is not valid, but considers deleted michael@0: // objects and null objects valid. michael@0: template michael@0: bool ValidateObjectAllowDeletedOrNull(const char* info, ObjectType *aObject); michael@0: // Returns false if aObject is null or not valid, but considers deleted michael@0: // objects valid. michael@0: template michael@0: bool ValidateObjectAllowDeleted(const char* info, ObjectType *aObject); michael@0: private: michael@0: // Like ValidateObject, but only for cases when aObject is known michael@0: // to not be null already. michael@0: template michael@0: bool ValidateObjectAssumeNonNull(const char* info, ObjectType *aObject); michael@0: michael@0: protected: michael@0: int32_t MaxTextureSizeForTarget(GLenum target) const { michael@0: MOZ_ASSERT(target == LOCAL_GL_TEXTURE_2D || michael@0: (target >= LOCAL_GL_TEXTURE_CUBE_MAP_POSITIVE_X && michael@0: target <= LOCAL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z), michael@0: "Invalid target enum"); michael@0: return (target == LOCAL_GL_TEXTURE_2D) ? mGLMaxTextureSize : mGLMaxCubeMapTextureSize; michael@0: } michael@0: michael@0: /** like glBufferData but if the call may change the buffer size, checks any GL error generated michael@0: * by this glBufferData call and returns it */ michael@0: GLenum CheckedBufferData(GLenum target, michael@0: GLsizeiptr size, michael@0: const GLvoid *data, michael@0: GLenum usage); michael@0: /** like glTexImage2D but if the call may change the texture size, checks any GL error generated michael@0: * by this glTexImage2D call and returns it */ michael@0: GLenum CheckedTexImage2D(GLenum target, michael@0: GLint level, michael@0: GLenum internalFormat, michael@0: GLsizei width, michael@0: GLsizei height, michael@0: GLint border, michael@0: GLenum format, michael@0: GLenum type, michael@0: const GLvoid *data); michael@0: michael@0: void MaybeRestoreContext(); michael@0: void ForceLoseContext(); michael@0: void ForceRestoreContext(); michael@0: michael@0: nsTArray > mBound2DTextures; michael@0: nsTArray > mBoundCubeMapTextures; michael@0: michael@0: WebGLRefPtr mCurrentProgram; michael@0: michael@0: uint32_t mMaxFramebufferColorAttachments; michael@0: michael@0: WebGLRefPtr mBoundFramebuffer; michael@0: WebGLRefPtr mBoundRenderbuffer; michael@0: WebGLRefPtr mBoundVertexArray; michael@0: michael@0: LinkedList mTextures; michael@0: LinkedList mBuffers; michael@0: LinkedList mPrograms; michael@0: LinkedList mQueries; michael@0: LinkedList mShaders; michael@0: LinkedList mRenderbuffers; michael@0: LinkedList mFramebuffers; michael@0: LinkedList mVertexArrays; michael@0: michael@0: WebGLRefPtr mDefaultVertexArray; michael@0: michael@0: // PixelStore parameters michael@0: uint32_t mPixelStorePackAlignment, mPixelStoreUnpackAlignment, mPixelStoreColorspaceConversion; michael@0: bool mPixelStoreFlipY, mPixelStorePremultiplyAlpha; michael@0: michael@0: WebGLContextFakeBlackStatus mFakeBlackStatus; michael@0: michael@0: class FakeBlackTexture michael@0: { michael@0: gl::GLContext* mGL; michael@0: GLuint mGLName; michael@0: michael@0: public: michael@0: FakeBlackTexture(gl::GLContext* gl, GLenum target, GLenum format); michael@0: ~FakeBlackTexture(); michael@0: GLuint GLName() const { return mGLName; } michael@0: }; michael@0: michael@0: ScopedDeletePtr mBlackOpaqueTexture2D, michael@0: mBlackOpaqueTextureCubeMap, michael@0: mBlackTransparentTexture2D, michael@0: mBlackTransparentTextureCubeMap; michael@0: michael@0: void BindFakeBlackTexturesHelper( michael@0: GLenum target, michael@0: const nsTArray >& boundTexturesArray, michael@0: ScopedDeletePtr & opaqueTextureScopedPtr, michael@0: ScopedDeletePtr & transparentTextureScopedPtr); michael@0: michael@0: GLfloat mVertexAttrib0Vector[4]; michael@0: GLfloat mFakeVertexAttrib0BufferObjectVector[4]; michael@0: size_t mFakeVertexAttrib0BufferObjectSize; michael@0: GLuint mFakeVertexAttrib0BufferObject; michael@0: WebGLVertexAttrib0Status mFakeVertexAttrib0BufferStatus; michael@0: michael@0: GLint mStencilRefFront, mStencilRefBack; michael@0: GLuint mStencilValueMaskFront, mStencilValueMaskBack, michael@0: mStencilWriteMaskFront, mStencilWriteMaskBack; michael@0: realGLboolean mColorWriteMask[4]; michael@0: realGLboolean mDepthWriteMask; michael@0: GLfloat mColorClearValue[4]; michael@0: GLint mStencilClearValue; michael@0: GLfloat mDepthClearValue; michael@0: michael@0: GLint mViewportX; michael@0: GLint mViewportY; michael@0: GLsizei mViewportWidth; michael@0: GLsizei mViewportHeight; michael@0: bool mAlreadyWarnedAboutViewportLargerThanDest; michael@0: michael@0: nsCOMPtr mContextRestorer; michael@0: bool mAllowRestore; michael@0: bool mContextLossTimerRunning; michael@0: bool mDrawSinceContextLossTimerSet; michael@0: ContextStatus mContextStatus; michael@0: bool mContextLostErrorSet; michael@0: michael@0: // Used for some hardware (particularly Tegra 2 and 4) that likes to michael@0: // be Flushed while doing hundreds of draw calls. michael@0: int mDrawCallsSinceLastFlush; michael@0: michael@0: int mAlreadyGeneratedWarnings; michael@0: int mMaxWarnings; michael@0: bool mAlreadyWarnedAboutFakeVertexAttrib0; michael@0: michael@0: bool ShouldGenerateWarnings() const; michael@0: michael@0: uint64_t mLastUseIndex; michael@0: michael@0: void LoseOldestWebGLContextIfLimitExceeded(); michael@0: void UpdateLastUseIndex(); michael@0: michael@0: template michael@0: JS::Value WebGLObjectAsJSValue(JSContext *cx, const WebGLObjectType *, ErrorResult& rv) const; michael@0: template michael@0: JSObject* WebGLObjectAsJSObject(JSContext *cx, const WebGLObjectType *, ErrorResult& rv) const; michael@0: michael@0: #ifdef XP_MACOSX michael@0: // see bug 713305. This RAII helper guarantees that we're on the discrete GPU, during its lifetime michael@0: // Debouncing note: we don't want to switch GPUs too frequently, so try to not create and destroy michael@0: // these objects at high frequency. Having WebGLContext's hold one such object seems fine, michael@0: // because WebGLContext objects only go away during GC, which shouldn't happen too frequently. michael@0: // If in the future GC becomes much more frequent, we may have to revisit then (maybe use a timer). michael@0: ForceDiscreteGPUHelperCGL mForceDiscreteGPUHelper; michael@0: #endif michael@0: michael@0: nsRefPtr mMemoryPressureObserver; michael@0: michael@0: public: michael@0: // console logging helpers michael@0: void GenerateWarning(const char *fmt, ...); michael@0: void GenerateWarning(const char *fmt, va_list ap); michael@0: michael@0: friend class WebGLTexture; michael@0: friend class WebGLFramebuffer; michael@0: friend class WebGLRenderbuffer; michael@0: friend class WebGLProgram; michael@0: friend class WebGLQuery; michael@0: friend class WebGLBuffer; michael@0: friend class WebGLShader; michael@0: friend class WebGLUniformLocation; michael@0: friend class WebGLVertexArray; michael@0: }; michael@0: michael@0: // used by DOM bindings in conjunction with GetParentObject michael@0: inline nsISupports* michael@0: ToSupports(WebGLContext* context) michael@0: { michael@0: return static_cast(context); michael@0: } michael@0: michael@0: /** michael@0: ** Template implementations michael@0: **/ michael@0: michael@0: template michael@0: inline bool michael@0: WebGLContext::ValidateObjectAllowDeletedOrNull(const char* info, michael@0: ObjectType *aObject) michael@0: { michael@0: if (aObject && !aObject->IsCompatibleWithContext(this)) { michael@0: ErrorInvalidOperation("%s: object from different WebGL context " michael@0: "(or older generation of this one) " michael@0: "passed as argument", info); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: template michael@0: inline bool michael@0: WebGLContext::ValidateObjectAssumeNonNull(const char* info, ObjectType *aObject) michael@0: { michael@0: MOZ_ASSERT(aObject); michael@0: michael@0: if (!ValidateObjectAllowDeletedOrNull(info, aObject)) michael@0: return false; michael@0: michael@0: if (aObject->IsDeleted()) { michael@0: ErrorInvalidValue("%s: deleted object passed as argument", info); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: template michael@0: inline bool michael@0: WebGLContext::ValidateObjectAllowNull(const char* info, ObjectType *aObject) michael@0: { michael@0: if (!aObject) { michael@0: return true; michael@0: } michael@0: michael@0: return ValidateObjectAssumeNonNull(info, aObject); michael@0: } michael@0: michael@0: template michael@0: inline bool michael@0: WebGLContext::ValidateObjectAllowDeleted(const char* info, ObjectType *aObject) michael@0: { michael@0: if (!aObject) { michael@0: ErrorInvalidValue("%s: null object passed as argument", info); michael@0: return false; michael@0: } michael@0: michael@0: return ValidateObjectAllowDeletedOrNull(info, aObject); michael@0: } michael@0: michael@0: template michael@0: inline bool michael@0: WebGLContext::ValidateObject(const char* info, ObjectType *aObject) michael@0: { michael@0: if (!aObject) { michael@0: ErrorInvalidValue("%s: null object passed as argument", info); michael@0: return false; michael@0: } michael@0: michael@0: return ValidateObjectAssumeNonNull(info, aObject); michael@0: } michael@0: michael@0: class WebGLMemoryPressureObserver MOZ_FINAL michael@0: : public nsIObserver michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIOBSERVER michael@0: michael@0: WebGLMemoryPressureObserver(WebGLContext *context) michael@0: : mContext(context) michael@0: {} michael@0: michael@0: private: michael@0: WebGLContext *mContext; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif