content/canvas/src/WebGLRenderbuffer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "WebGLContext.h"
michael@0 7 #include "WebGLRenderbuffer.h"
michael@0 8 #include "WebGLTexture.h"
michael@0 9 #include "mozilla/dom/WebGLRenderingContextBinding.h"
michael@0 10 #include "GLContext.h"
michael@0 11 #include "ScopedGLHelpers.h"
michael@0 12
michael@0 13 using namespace mozilla;
michael@0 14 using namespace mozilla::gl;
michael@0 15
michael@0 16 static GLenum
michael@0 17 DepthStencilDepthFormat(GLContext* gl) {
michael@0 18 // We might not be able to get 24-bit, so let's pretend!
michael@0 19 if (gl->IsGLES() && !gl->IsExtensionSupported(gl::GLContext::OES_depth24))
michael@0 20 return LOCAL_GL_DEPTH_COMPONENT16;
michael@0 21
michael@0 22 return LOCAL_GL_DEPTH_COMPONENT24;
michael@0 23 }
michael@0 24
michael@0 25 static bool
michael@0 26 SupportsDepthStencil(GLContext* gl) {
michael@0 27 return gl->IsExtensionSupported(GLContext::EXT_packed_depth_stencil) ||
michael@0 28 gl->IsExtensionSupported(GLContext::OES_packed_depth_stencil);
michael@0 29 }
michael@0 30
michael@0 31 static bool
michael@0 32 NeedsDepthStencilEmu(GLContext* gl, GLenum internalFormat) {
michael@0 33 MOZ_ASSERT(internalFormat != LOCAL_GL_DEPTH_STENCIL);
michael@0 34 if (internalFormat != LOCAL_GL_DEPTH24_STENCIL8)
michael@0 35 return false;
michael@0 36
michael@0 37 return !SupportsDepthStencil(gl);
michael@0 38 }
michael@0 39
michael@0 40 JSObject*
michael@0 41 WebGLRenderbuffer::WrapObject(JSContext *cx) {
michael@0 42 return dom::WebGLRenderbufferBinding::Wrap(cx, this);
michael@0 43 }
michael@0 44
michael@0 45 WebGLRenderbuffer::WebGLRenderbuffer(WebGLContext *context)
michael@0 46 : WebGLContextBoundObject(context)
michael@0 47 , mPrimaryRB(0)
michael@0 48 , mSecondaryRB(0)
michael@0 49 , mInternalFormat(0)
michael@0 50 , mInternalFormatForGL(0)
michael@0 51 , mHasEverBeenBound(false)
michael@0 52 , mImageDataStatus(WebGLImageDataStatus::NoImageData)
michael@0 53 {
michael@0 54 SetIsDOMBinding();
michael@0 55 mContext->MakeContextCurrent();
michael@0 56
michael@0 57 mContext->gl->fGenRenderbuffers(1, &mPrimaryRB);
michael@0 58 if (!SupportsDepthStencil(mContext->gl))
michael@0 59 mContext->gl->fGenRenderbuffers(1, &mSecondaryRB);
michael@0 60
michael@0 61 mContext->mRenderbuffers.insertBack(this);
michael@0 62 }
michael@0 63
michael@0 64 void
michael@0 65 WebGLRenderbuffer::Delete() {
michael@0 66 mContext->MakeContextCurrent();
michael@0 67
michael@0 68 mContext->gl->fDeleteRenderbuffers(1, &mPrimaryRB);
michael@0 69 if (mSecondaryRB)
michael@0 70 mContext->gl->fDeleteRenderbuffers(1, &mSecondaryRB);
michael@0 71
michael@0 72 LinkedListElement<WebGLRenderbuffer>::removeFrom(mContext->mRenderbuffers);
michael@0 73 }
michael@0 74
michael@0 75 int64_t
michael@0 76 WebGLRenderbuffer::MemoryUsage() const {
michael@0 77 int64_t pixels = int64_t(Width()) * int64_t(Height());
michael@0 78
michael@0 79 GLenum primaryFormat = InternalFormatForGL();
michael@0 80 // If there is no defined format, we're not taking up any memory
michael@0 81 if (!primaryFormat) {
michael@0 82 return 0;
michael@0 83 }
michael@0 84
michael@0 85 int64_t secondarySize = 0;
michael@0 86 if (mSecondaryRB) {
michael@0 87 if (NeedsDepthStencilEmu(mContext->gl, primaryFormat)) {
michael@0 88 primaryFormat = DepthStencilDepthFormat(mContext->gl);
michael@0 89 secondarySize = 1*pixels; // STENCIL_INDEX8
michael@0 90 } else {
michael@0 91 secondarySize = 1*1*2; // 1x1xRGBA4
michael@0 92 }
michael@0 93 }
michael@0 94
michael@0 95 int64_t primarySize = 0;
michael@0 96 switch (primaryFormat) {
michael@0 97 case LOCAL_GL_STENCIL_INDEX8:
michael@0 98 primarySize = 1*pixels;
michael@0 99 break;
michael@0 100 case LOCAL_GL_RGBA4:
michael@0 101 case LOCAL_GL_RGB5_A1:
michael@0 102 case LOCAL_GL_RGB565:
michael@0 103 case LOCAL_GL_DEPTH_COMPONENT16:
michael@0 104 primarySize = 2*pixels;
michael@0 105 break;
michael@0 106 case LOCAL_GL_RGB8:
michael@0 107 case LOCAL_GL_DEPTH_COMPONENT24:
michael@0 108 primarySize = 3*pixels;
michael@0 109 break;
michael@0 110 case LOCAL_GL_RGBA8:
michael@0 111 case LOCAL_GL_SRGB8_ALPHA8_EXT:
michael@0 112 case LOCAL_GL_DEPTH24_STENCIL8:
michael@0 113 case LOCAL_GL_DEPTH_COMPONENT32:
michael@0 114 primarySize = 4*pixels;
michael@0 115 break;
michael@0 116 case LOCAL_GL_RGB16F:
michael@0 117 primarySize = 2*3*pixels;
michael@0 118 break;
michael@0 119 case LOCAL_GL_RGBA16F:
michael@0 120 primarySize = 2*4*pixels;
michael@0 121 break;
michael@0 122 case LOCAL_GL_RGB32F:
michael@0 123 primarySize = 4*3*pixels;
michael@0 124 break;
michael@0 125 case LOCAL_GL_RGBA32F:
michael@0 126 primarySize = 4*4*pixels;
michael@0 127 break;
michael@0 128 default:
michael@0 129 MOZ_ASSERT(false, "Unknown `primaryFormat`.");
michael@0 130 break;
michael@0 131 }
michael@0 132
michael@0 133 return primarySize + secondarySize;
michael@0 134 }
michael@0 135
michael@0 136 void
michael@0 137 WebGLRenderbuffer::BindRenderbuffer() const {
michael@0 138 /* Do this explicitly here, since the meaning changes for depth-stencil emu.
michael@0 139 * Under normal circumstances, there's only one RB: `mPrimaryRB`.
michael@0 140 * `mSecondaryRB` is used when we have to pretend that the renderbuffer is
michael@0 141 * DEPTH_STENCIL, when it's actually one DEPTH buffer `mPrimaryRB` and one
michael@0 142 * STENCIL buffer `mSecondaryRB`.
michael@0 143 *
michael@0 144 * In the DEPTH_STENCIL emulation case, we're actually juggling two RBs, but
michael@0 145 * we can only bind one of them at a time. We choose to unconditionally bind
michael@0 146 * the depth RB. When we need to ask about the stencil buffer (say, how many
michael@0 147 * stencil bits we have), we temporarily bind the stencil RB, so that it
michael@0 148 * looks like we're just asking the question of a combined DEPTH_STENCIL
michael@0 149 * buffer.
michael@0 150 */
michael@0 151 mContext->gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mPrimaryRB);
michael@0 152 }
michael@0 153
michael@0 154 void
michael@0 155 WebGLRenderbuffer::RenderbufferStorage(GLenum internalFormat, GLsizei width, GLsizei height) const {
michael@0 156 GLContext* gl = mContext->gl;
michael@0 157
michael@0 158 GLenum primaryFormat = internalFormat;
michael@0 159 GLenum secondaryFormat = 0;
michael@0 160
michael@0 161 if (NeedsDepthStencilEmu(mContext->gl, primaryFormat)) {
michael@0 162 primaryFormat = DepthStencilDepthFormat(gl);
michael@0 163 secondaryFormat = LOCAL_GL_STENCIL_INDEX8;
michael@0 164 }
michael@0 165
michael@0 166 gl->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, primaryFormat, width, height);
michael@0 167
michael@0 168 if (!mSecondaryRB) {
michael@0 169 MOZ_ASSERT(!secondaryFormat);
michael@0 170 return;
michael@0 171 }
michael@0 172 // We can't leave the secondary RB unspecified either, since we should
michael@0 173 // handle the case where we attach a non-depth-stencil RB to a
michael@0 174 // depth-stencil attachment point, or attach this depth-stencil RB to a
michael@0 175 // non-depth-stencil attachment point.
michael@0 176 ScopedBindRenderbuffer autoRB(gl, mSecondaryRB);
michael@0 177 if (secondaryFormat) {
michael@0 178 gl->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, secondaryFormat, width, height);
michael@0 179 } else {
michael@0 180 gl->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, LOCAL_GL_RGBA4, 1, 1);
michael@0 181 }
michael@0 182 }
michael@0 183
michael@0 184 void
michael@0 185 WebGLRenderbuffer::FramebufferRenderbuffer(GLenum attachment) const {
michael@0 186 GLContext* gl = mContext->gl;
michael@0 187 if (attachment != LOCAL_GL_DEPTH_STENCIL_ATTACHMENT) {
michael@0 188 gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, attachment, LOCAL_GL_RENDERBUFFER, mPrimaryRB);
michael@0 189 return;
michael@0 190 }
michael@0 191
michael@0 192 GLuint stencilRB = mPrimaryRB;
michael@0 193 if (NeedsDepthStencilEmu(mContext->gl, InternalFormatForGL())) {
michael@0 194 printf_stderr("DEV-ONLY: Using secondary buffer to emulate DepthStencil.\n");
michael@0 195 MOZ_ASSERT(mSecondaryRB);
michael@0 196 stencilRB = mSecondaryRB;
michael@0 197 }
michael@0 198 gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, LOCAL_GL_DEPTH_ATTACHMENT, LOCAL_GL_RENDERBUFFER, mPrimaryRB);
michael@0 199 gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, LOCAL_GL_STENCIL_ATTACHMENT, LOCAL_GL_RENDERBUFFER, stencilRB);
michael@0 200 }
michael@0 201
michael@0 202 GLint
michael@0 203 WebGLRenderbuffer::GetRenderbufferParameter(GLenum target, GLenum pname) const {
michael@0 204 GLContext* gl = mContext->gl;
michael@0 205
michael@0 206 switch (pname) {
michael@0 207 case LOCAL_GL_RENDERBUFFER_STENCIL_SIZE: {
michael@0 208 if (NeedsDepthStencilEmu(mContext->gl, InternalFormatForGL())) {
michael@0 209 if (gl->WorkAroundDriverBugs() &&
michael@0 210 gl->Renderer() == GLRenderer::Tegra)
michael@0 211 {
michael@0 212 return 8;
michael@0 213 }
michael@0 214
michael@0 215 ScopedBindRenderbuffer autoRB(gl, mSecondaryRB);
michael@0 216
michael@0 217 GLint i = 0;
michael@0 218 gl->fGetRenderbufferParameteriv(target, pname, &i);
michael@0 219 return i;
michael@0 220 }
michael@0 221 // Fall through otherwise.
michael@0 222 }
michael@0 223 case LOCAL_GL_RENDERBUFFER_WIDTH:
michael@0 224 case LOCAL_GL_RENDERBUFFER_HEIGHT:
michael@0 225 case LOCAL_GL_RENDERBUFFER_RED_SIZE:
michael@0 226 case LOCAL_GL_RENDERBUFFER_GREEN_SIZE:
michael@0 227 case LOCAL_GL_RENDERBUFFER_BLUE_SIZE:
michael@0 228 case LOCAL_GL_RENDERBUFFER_ALPHA_SIZE:
michael@0 229 case LOCAL_GL_RENDERBUFFER_DEPTH_SIZE: {
michael@0 230 GLint i = 0;
michael@0 231 gl->fGetRenderbufferParameteriv(target, pname, &i);
michael@0 232 return i;
michael@0 233 }
michael@0 234 }
michael@0 235
michael@0 236 MOZ_ASSERT(false, "This function should only be called with valid `pname`.");
michael@0 237 return 0;
michael@0 238 }
michael@0 239
michael@0 240
michael@0 241 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WebGLRenderbuffer)
michael@0 242
michael@0 243 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLRenderbuffer, AddRef)
michael@0 244 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLRenderbuffer, Release)

mercurial