1 /* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
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 /* GLScreenBuffer is the abstraction for the "default framebuffer" used
7 * by an offscreen GLContext. Since it's only for offscreen GLContext's,
8 * it's only useful for things like WebGL, and is NOT used by the
9 * compositor's GLContext. Remember that GLContext provides an abstraction
10 * so that even if you want to draw to the 'screen', even if that's not
11 * actually the screen, just draw to 0. This GLScreenBuffer class takes the
12 * logic handling out of GLContext.
15 #ifndef SCREEN_BUFFER_H_
16 #define SCREEN_BUFFER_H_
18 #include "SurfaceTypes.h"
19 #include "SurfaceStream.h"
20 #include "GLContextTypes.h"
22 #include "mozilla/gfx/2D.h"
23 #include "mozilla/gfx/Point.h"
24 #include "mozilla/UniquePtr.h"
37 // But it may return true with *out_buffer==nullptr if unneeded.
38 static bool Create(GLContext
* const gl
,
39 const SurfaceCaps
& caps
,
40 const GLFormats
& formats
,
41 const gfx::IntSize
& size
,
42 UniquePtr
<DrawBuffer
>* out_buffer
);
47 const gfx::IntSize mSize
;
50 const GLuint mColorMSRB
;
51 const GLuint mDepthRB
;
52 const GLuint mStencilRB
;
54 DrawBuffer(GLContext
* gl
,
55 const gfx::IntSize
& size
,
63 , mColorMSRB(colorMSRB
)
65 , mStencilRB(stencilRB
)
69 virtual ~DrawBuffer();
75 // Infallible, always non-null.
76 static UniquePtr
<ReadBuffer
> Create(GLContext
* gl
,
77 const SurfaceCaps
& caps
,
78 const GLFormats
& formats
,
86 // mFB has the following attachments:
87 const GLuint mDepthRB
;
88 const GLuint mStencilRB
;
89 // note no mColorRB here: this is provided by mSurf.
90 SharedSurface
* mSurf
; // Owned by GLScreenBuffer's SurfaceStream.
92 ReadBuffer(GLContext
* gl
,
100 , mStencilRB(stencilRB
)
105 virtual ~ReadBuffer();
107 // Cannot attach a surf of a different AttachType or Size than before.
108 void Attach(SharedSurface
* surf
);
110 const gfx::IntSize
& Size() const;
112 SharedSurface
* SharedSurf() const {
122 static UniquePtr
<GLScreenBuffer
> Create(GLContext
* gl
,
123 const gfx::IntSize
& size
,
124 const SurfaceCaps
& caps
);
127 GLContext
* const mGL
; // Owns us.
129 const SurfaceCaps mCaps
;
131 UniquePtr
<SurfaceFactory
> mFactory
;
132 RefPtr
<SurfaceStream
> mStream
;
134 UniquePtr
<DrawBuffer
> mDraw
;
135 UniquePtr
<ReadBuffer
> mRead
;
139 // Below are the parts that help us pretend to be framebuffer 0:
142 GLuint mInternalDrawFB
;
143 GLuint mInternalReadFB
;
146 bool mInInternalMode_DrawFB
;
147 bool mInInternalMode_ReadFB
;
150 GLScreenBuffer(GLContext
* gl
,
151 const SurfaceCaps
& caps
,
152 UniquePtr
<SurfaceFactory
> factory
,
153 const RefPtr
<SurfaceStream
>& stream
)
156 , mFactory(Move(factory
))
166 , mInInternalMode_DrawFB(true)
167 , mInInternalMode_ReadFB(true)
172 virtual ~GLScreenBuffer();
174 SurfaceStream
* Stream() const {
178 SurfaceFactory
* Factory() const {
179 return mFactory
.get();
182 SharedSurface
* SharedSurf() const {
184 return mRead
->SharedSurf();
187 bool PreserveBuffer() const {
188 return mCaps
.preserve
;
191 GLuint
DrawFB() const {
198 GLuint
ReadFB() const {
202 void DeletingFB(GLuint fb
);
204 const gfx::IntSize
& Size() const {
206 MOZ_ASSERT(!mDraw
|| mDraw
->mSize
== mRead
->Size());
207 return mRead
->Size();
210 void BindAsFramebuffer(GLContext
* const gl
, GLenum target
) const;
213 void AssureBlitted();
214 void AfterDrawCall();
215 void BeforeReadCall();
218 * Attempts to read pixels from the current bound framebuffer, if
219 * it is backed by a SharedSurface.
221 * Returns true if the pixel data has been read back, false
224 bool ReadPixels(GLint x
, GLint y
, GLsizei width
, GLsizei height
,
225 GLenum format
, GLenum type
, GLvoid
*pixels
);
227 /* Morph swaps out our SurfaceStream mechanism and replaces it with
228 * one best suited to our platform and compositor configuration.
230 * Must be called on the producing thread.
231 * We haven't made any guarantee that rendering is actually
232 * done when Morph is run, just that it can't run concurrently
233 * with rendering. This means that we can't just drop the contents
234 * of the buffer, since we may only be partially done rendering.
236 * Once you pass newFactory into Morph, newFactory will be owned by
237 * GLScreenBuffer, so `forget` any references to it that still exist.
239 void Morph(UniquePtr
<SurfaceFactory
> newFactory
,
240 SurfaceStreamType streamType
);
243 // Returns false on error or inability to resize.
244 bool Swap(const gfx::IntSize
& size
);
247 bool PublishFrame(const gfx::IntSize
& size
);
249 bool Resize(const gfx::IntSize
& size
);
251 void Readback(SharedSurface
* src
, gfx::DataSourceSurface
* dest
);
254 bool Attach(SharedSurface
* surf
, const gfx::IntSize
& size
);
256 bool CreateDraw(const gfx::IntSize
& size
, UniquePtr
<DrawBuffer
>* out_buffer
);
257 UniquePtr
<ReadBuffer
> CreateRead(SharedSurface
* surf
);
260 /* `fb` in these functions is the framebuffer the GLContext is hoping to
261 * bind. When this is 0, we intercept the call and bind our own
262 * framebuffers. As a client of these functions, just bind 0 when you want
263 * to draw to the default framebuffer/'screen'.
265 void BindFB(GLuint fb
);
266 void BindDrawFB(GLuint fb
);
267 void BindReadFB(GLuint fb
);
268 GLuint
GetFB() const;
269 GLuint
GetDrawFB() const;
270 GLuint
GetReadFB() const;
272 // Here `fb` is the actual framebuffer you want bound. Binding 0 will
273 // bind the (generally useless) default framebuffer.
274 void BindFB_Internal(GLuint fb
);
275 void BindDrawFB_Internal(GLuint fb
);
276 void BindReadFB_Internal(GLuint fb
);
280 } // namespace mozilla
282 #endif // SCREEN_BUFFER_H_