MozFramebuffer.h (3722B)
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */ 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/. */ 5 6 #ifndef MOZ_FRAMEBUFFER_H_ 7 #define MOZ_FRAMEBUFFER_H_ 8 9 #include "gfx2DGlue.h" 10 #include "GLConsts.h" 11 #include "GLContextTypes.h" 12 #include "mozilla/UniquePtr.h" 13 #include "mozilla/WeakPtr.h" 14 15 namespace mozilla { 16 namespace gl { 17 18 class DepthAndStencilBuffer final : public SupportsWeakPtr { 19 const WeakPtr<GLContext> mWeakGL; 20 const gfx::IntSize mSize; 21 22 public: 23 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DepthAndStencilBuffer) 24 25 const GLuint mDepthRB; 26 const GLuint mStencilRB; 27 28 static RefPtr<DepthAndStencilBuffer> Create(GLContext* const gl, 29 const gfx::IntSize& size, 30 const uint32_t samples); 31 32 RefPtr<GLContext> gl() const { return mWeakGL.get(); } 33 34 // 4 bytes per pixel (24-bit depth + 8-bit stencil). 35 uint64_t EstimateMemory() const { 36 return static_cast<uint64_t>(mSize.width) * 4 * mSize.height; 37 } 38 39 protected: 40 DepthAndStencilBuffer(GLContext* gl, const gfx::IntSize& size, GLuint depthRB, 41 GLuint stencilRB); 42 ~DepthAndStencilBuffer(); 43 }; 44 45 class MozFramebuffer final { 46 const WeakPtr<GLContext> mWeakGL; 47 48 public: 49 const gfx::IntSize mSize; 50 const uint32_t mSamples; 51 const GLuint mFB; 52 const GLenum mColorTarget; 53 54 private: 55 const RefPtr<DepthAndStencilBuffer> mDepthAndStencilBuffer; 56 const GLuint mColorName; 57 58 public: 59 // Create a new framebuffer with the specified properties. 60 static UniquePtr<MozFramebuffer> Create(GLContext* gl, 61 const gfx::IntSize& size, 62 uint32_t samples, bool depthStencil); 63 64 // Create a new framebuffer backed by an existing texture or buffer. 65 // Assumes that gl is the current context. 66 static UniquePtr<MozFramebuffer> CreateForBacking( 67 GLContext* gl, const gfx::IntSize& size, uint32_t samples, 68 bool depthStencil, GLenum colorTarget, GLuint colorName); 69 70 // Create a new framebuffer backed by an existing texture or buffer. 71 // Use the same GLContext, size, and samples as framebufferToShareWith. 72 // The new framebuffer will share its depth and stencil buffer with 73 // framebufferToShareWith. The depth and stencil buffers will be destroyed 74 // once the last MozFramebuffer using them is destroyed. 75 static UniquePtr<MozFramebuffer> CreateForBackingWithSharedDepthAndStencil( 76 const gfx::IntSize& size, const uint32_t samples, GLenum colorTarget, 77 GLuint colorName, 78 const RefPtr<DepthAndStencilBuffer>& depthAndStencilBuffer); 79 80 private: 81 MozFramebuffer(GLContext* gl, const gfx::IntSize& size, GLuint fb, 82 uint32_t samples, 83 RefPtr<DepthAndStencilBuffer> depthAndStencilBuffer, 84 GLenum colorTarget, GLuint colorName); 85 86 // gl must be the current context when this is called. 87 static UniquePtr<MozFramebuffer> CreateImpl( 88 GLContext* const gl, const gfx::IntSize& size, const uint32_t samples, 89 const RefPtr<DepthAndStencilBuffer>& depthAndStencilBuffer, 90 const GLenum colorTarget, const GLuint colorName); 91 92 public: 93 ~MozFramebuffer(); 94 95 GLuint ColorTex() const { 96 if (mColorTarget == LOCAL_GL_RENDERBUFFER) return 0; 97 return mColorName; 98 } 99 const auto& GetDepthAndStencilBuffer() const { 100 return mDepthAndStencilBuffer; 101 } 102 bool HasDepth() const; 103 bool HasStencil() const; 104 }; 105 106 } // namespace gl 107 } // namespace mozilla 108 109 #endif // MOZ_FRAMEBUFFER_H_