Bug 1732409 let fake:true getUserMedia() parameter override loopback prefs r=jib
[gecko.git] / dom / canvas / WebGLRenderbuffer.cpp
blob7f6d8e791403356adb46ccc895af36b18e126bb6
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "WebGLRenderbuffer.h"
8 #include "GLContext.h"
9 #include "mozilla/dom/WebGLRenderingContextBinding.h"
10 #include "ScopedGLHelpers.h"
11 #include "WebGLContext.h"
12 #include "WebGLFormats.h"
13 #include "WebGLStrongTypes.h"
14 #include "WebGLTexture.h"
16 namespace mozilla {
18 static GLenum DepthFormatForDepthStencilEmu(gl::GLContext* gl) {
19 // We might not be able to get 24-bit, so let's pretend!
20 if (gl->IsGLES() && !gl->IsExtensionSupported(gl::GLContext::OES_depth24))
21 return LOCAL_GL_DEPTH_COMPONENT16;
23 return LOCAL_GL_DEPTH_COMPONENT24;
26 static GLuint DoCreateRenderbuffer(gl::GLContext* gl) {
27 MOZ_ASSERT(gl->IsCurrent());
29 GLuint ret = 0;
30 gl->fGenRenderbuffers(1, &ret);
31 return ret;
34 static bool EmulatePackedDepthStencil(gl::GLContext* gl) {
35 return !gl->IsSupported(gl::GLFeature::packed_depth_stencil);
38 WebGLRenderbuffer::WebGLRenderbuffer(WebGLContext* webgl)
39 : WebGLContextBoundObject(webgl),
40 mPrimaryRB(DoCreateRenderbuffer(webgl->gl)),
41 mEmulatePackedDepthStencil(EmulatePackedDepthStencil(webgl->gl)),
42 mSecondaryRB(0) {
43 // Bind our RB, or we might end up calling FramebufferRenderbuffer before we
44 // ever call BindRenderbuffer, since webgl.bindRenderbuffer doesn't actually
45 // call glBindRenderbuffer anymore.
46 mContext->gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mPrimaryRB);
47 mContext->gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, 0);
50 WebGLRenderbuffer::~WebGLRenderbuffer() {
51 mImageInfo = webgl::ImageInfo();
53 if (!mContext) return;
55 mContext->gl->fDeleteRenderbuffers(1, &mPrimaryRB);
56 if (mSecondaryRB) {
57 mContext->gl->fDeleteRenderbuffers(1, &mSecondaryRB);
61 static GLenum DoRenderbufferStorageMaybeMultisample(gl::GLContext* gl,
62 GLsizei samples,
63 GLenum internalFormat,
64 GLsizei width,
65 GLsizei height) {
66 MOZ_ASSERT_IF(samples >= 1,
67 gl->IsSupported(gl::GLFeature::framebuffer_multisample));
69 // Certain OpenGL ES renderbuffer formats may not exist on desktop OpenGL.
70 switch (internalFormat) {
71 case LOCAL_GL_RGBA4:
72 case LOCAL_GL_RGB5_A1:
73 // 16-bit RGBA formats are not supported on desktop GL.
74 if (!gl->IsGLES()) internalFormat = LOCAL_GL_RGBA8;
75 break;
77 case LOCAL_GL_RGB565:
78 // RGB565 is not supported on desktop GL.
79 if (!gl->IsGLES()) internalFormat = LOCAL_GL_RGB8;
80 break;
82 case LOCAL_GL_DEPTH_COMPONENT16:
83 if (!gl->IsGLES() || gl->IsExtensionSupported(gl::GLContext::OES_depth24))
84 internalFormat = LOCAL_GL_DEPTH_COMPONENT24;
85 else if (gl->IsSupported(gl::GLFeature::packed_depth_stencil))
86 internalFormat = LOCAL_GL_DEPTH24_STENCIL8;
87 break;
89 case LOCAL_GL_DEPTH_STENCIL:
90 MOZ_CRASH("GFX: GL_DEPTH_STENCIL is not valid here.");
91 break;
93 default:
94 break;
97 gl::GLContext::LocalErrorScope errorScope(*gl);
99 if (samples > 0) {
100 gl->fRenderbufferStorageMultisample(LOCAL_GL_RENDERBUFFER, samples,
101 internalFormat, width, height);
102 } else {
103 gl->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, internalFormat, width,
104 height);
107 return errorScope.GetError();
110 GLenum WebGLRenderbuffer::DoRenderbufferStorage(
111 uint32_t samples, const webgl::FormatUsageInfo* format, uint32_t width,
112 uint32_t height) {
113 gl::GLContext* gl = mContext->gl;
114 MOZ_ASSERT(samples <= 256); // Sanity check.
116 GLenum primaryFormat = format->format->sizedFormat;
117 GLenum secondaryFormat = 0;
119 if (mEmulatePackedDepthStencil &&
120 primaryFormat == LOCAL_GL_DEPTH24_STENCIL8) {
121 primaryFormat = DepthFormatForDepthStencilEmu(gl);
122 secondaryFormat = LOCAL_GL_STENCIL_INDEX8;
125 gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mPrimaryRB);
126 GLenum error = DoRenderbufferStorageMaybeMultisample(
127 gl, samples, primaryFormat, width, height);
128 gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, 0);
129 if (error) return error;
131 if (secondaryFormat) {
132 if (!mSecondaryRB) {
133 gl->fGenRenderbuffers(1, &mSecondaryRB);
136 gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mSecondaryRB);
137 error = DoRenderbufferStorageMaybeMultisample(gl, samples, secondaryFormat,
138 width, height);
139 gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, 0);
140 if (error) return error;
141 } else if (mSecondaryRB) {
142 gl->fDeleteRenderbuffers(1, &mSecondaryRB);
143 mSecondaryRB = 0;
146 return 0;
149 void WebGLRenderbuffer::RenderbufferStorage(uint32_t samples,
150 GLenum internalFormat,
151 uint32_t width, uint32_t height) {
152 const auto usage = mContext->mFormatUsage->GetRBUsage(internalFormat);
153 if (!usage) {
154 mContext->ErrorInvalidEnum("Invalid `internalFormat`: 0x%04x.",
155 internalFormat);
156 return;
159 if (width > mContext->mGLMaxRenderbufferSize ||
160 height > mContext->mGLMaxRenderbufferSize) {
161 mContext->ErrorInvalidValue(
162 "Width or height exceeds maximum renderbuffer"
163 " size.");
164 return;
167 const auto maxSamples = usage->MaxSamples(*mContext->gl);
168 if (samples > maxSamples) {
169 mContext->ErrorInvalidOperation("`samples` is out of the valid range.");
170 return;
173 // Validation complete.
175 const GLenum error = DoRenderbufferStorage(samples, usage, width, height);
176 if (error) {
177 mContext->GenerateWarning("Unexpected error %s", EnumString(error).c_str());
178 if (error == LOCAL_GL_OUT_OF_MEMORY) {
179 // Truncate.
180 mImageInfo = {};
181 InvalidateCaches();
183 return;
186 mContext->OnDataAllocCall();
188 const uint32_t depth = 1;
189 auto uninitializedSlices = Some(std::vector<bool>(depth, true));
190 mImageInfo = {
191 usage, width, height, depth, std::move(uninitializedSlices),
192 uint8_t(samples)};
193 InvalidateCaches();
196 void WebGLRenderbuffer::DoFramebufferRenderbuffer(
197 const GLenum attachment) const {
198 gl::GLContext* gl = mContext->gl;
200 if (attachment == LOCAL_GL_DEPTH_STENCIL_ATTACHMENT) {
201 const GLuint stencilRB = (mSecondaryRB ? mSecondaryRB : mPrimaryRB);
202 gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
203 LOCAL_GL_DEPTH_ATTACHMENT,
204 LOCAL_GL_RENDERBUFFER, mPrimaryRB);
205 gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER,
206 LOCAL_GL_STENCIL_ATTACHMENT,
207 LOCAL_GL_RENDERBUFFER, stencilRB);
208 return;
211 gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, attachment,
212 LOCAL_GL_RENDERBUFFER, mPrimaryRB);
215 GLint WebGLRenderbuffer::GetRenderbufferParameter(RBParam pname) const {
216 gl::GLContext* gl = mContext->gl;
218 switch (pname.get()) {
219 case LOCAL_GL_RENDERBUFFER_STENCIL_SIZE:
220 if (!mImageInfo.mFormat) return 0;
222 if (!mImageInfo.mFormat->format->s) return 0;
224 return 8;
226 case LOCAL_GL_RENDERBUFFER_SAMPLES:
227 case LOCAL_GL_RENDERBUFFER_WIDTH:
228 case LOCAL_GL_RENDERBUFFER_HEIGHT:
229 case LOCAL_GL_RENDERBUFFER_RED_SIZE:
230 case LOCAL_GL_RENDERBUFFER_GREEN_SIZE:
231 case LOCAL_GL_RENDERBUFFER_BLUE_SIZE:
232 case LOCAL_GL_RENDERBUFFER_ALPHA_SIZE:
233 case LOCAL_GL_RENDERBUFFER_DEPTH_SIZE: {
234 gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mPrimaryRB);
235 GLint i = 0;
236 gl->fGetRenderbufferParameteriv(LOCAL_GL_RENDERBUFFER, pname.get(), &i);
237 gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, 0);
238 return i;
241 case LOCAL_GL_RENDERBUFFER_INTERNAL_FORMAT: {
242 GLenum ret = LOCAL_GL_RGBA4;
243 if (mImageInfo.mFormat) {
244 ret = mImageInfo.mFormat->format->sizedFormat;
246 if (!mContext->IsWebGL2() && ret == LOCAL_GL_DEPTH24_STENCIL8) {
247 ret = LOCAL_GL_DEPTH_STENCIL;
250 return ret;
254 MOZ_ASSERT(false, "This function should only be called with valid `pname`.");
255 return 0;
258 } // namespace mozilla