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/. */
6 #ifndef MOZ_FRAMEBUFFER_H_
7 #define MOZ_FRAMEBUFFER_H_
11 #include "GLContextTypes.h"
12 #include "mozilla/UniquePtr.h"
13 #include "mozilla/WeakPtr.h"
18 class DepthAndStencilBuffer final
: public SupportsWeakPtr
{
19 const WeakPtr
<GLContext
> mWeakGL
;
20 const gfx::IntSize mSize
;
23 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DepthAndStencilBuffer
)
25 const GLuint mDepthRB
;
26 const GLuint mStencilRB
;
28 static RefPtr
<DepthAndStencilBuffer
> Create(GLContext
* const gl
,
29 const gfx::IntSize
& size
,
30 const uint32_t samples
);
32 RefPtr
<GLContext
> gl() const { return mWeakGL
.get(); }
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
;
40 DepthAndStencilBuffer(GLContext
* gl
, const gfx::IntSize
& size
, GLuint depthRB
,
42 ~DepthAndStencilBuffer();
45 class MozFramebuffer final
{
46 const WeakPtr
<GLContext
> mWeakGL
;
49 const gfx::IntSize mSize
;
50 const uint32_t mSamples
;
52 const GLenum mColorTarget
;
55 const RefPtr
<DepthAndStencilBuffer
> mDepthAndStencilBuffer
;
56 const GLuint mColorName
;
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
);
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
);
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
,
78 const RefPtr
<DepthAndStencilBuffer
>& depthAndStencilBuffer
);
81 MozFramebuffer(GLContext
* gl
, const gfx::IntSize
& size
, GLuint fb
,
83 RefPtr
<DepthAndStencilBuffer
> depthAndStencilBuffer
,
84 GLenum colorTarget
, GLuint colorName
);
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
);
95 GLuint
ColorTex() const {
96 if (mColorTarget
== LOCAL_GL_RENDERBUFFER
) return 0;
99 const auto& GetDepthAndStencilBuffer() const {
100 return mDepthAndStencilBuffer
;
102 bool HasDepth() const;
103 bool HasStencil() const;
107 } // namespace mozilla
109 #endif // MOZ_FRAMEBUFFER_H_