1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_
10 #include "base/basictypes.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "gpu/command_buffer/service/context_group.h"
15 #include "gpu/command_buffer/service/gl_utils.h"
16 #include "gpu/gpu_export.h"
21 class FramebufferManager
;
23 class RenderbufferManager
;
28 // Info about a particular Framebuffer.
29 class GPU_EXPORT Framebuffer
: public base::RefCounted
<Framebuffer
> {
31 class Attachment
: public base::RefCounted
<Attachment
> {
33 virtual GLsizei
width() const = 0;
34 virtual GLsizei
height() const = 0;
35 virtual GLenum
internal_format() const = 0;
36 virtual GLenum
texture_type() const = 0;
37 virtual GLsizei
samples() const = 0;
38 virtual GLuint
object_name() const = 0;
39 virtual bool cleared() const = 0;
40 virtual void SetCleared(
41 RenderbufferManager
* renderbuffer_manager
,
42 TextureManager
* texture_manager
,
44 virtual bool IsTexture(TextureRef
* texture
) const = 0;
45 virtual bool IsRenderbuffer(
46 Renderbuffer
* renderbuffer
) const = 0;
47 virtual bool CanRenderTo() const = 0;
48 virtual void DetachFromFramebuffer(Framebuffer
* framebuffer
) const = 0;
49 virtual bool ValidForAttachmentType(
50 GLenum attachment_type
, uint32 max_color_attachments
) = 0;
51 virtual size_t GetSignatureSize(TextureManager
* texture_manager
) const = 0;
52 virtual void AddToSignature(
53 TextureManager
* texture_manager
, std::string
* signature
) const = 0;
54 virtual void OnWillRenderTo() const = 0;
55 virtual void OnDidRenderTo() const = 0;
56 virtual bool FormsFeedbackLoop(TextureRef
* texture
, GLint level
) const = 0;
59 friend class base::RefCounted
<Attachment
>;
60 virtual ~Attachment() {}
63 Framebuffer(FramebufferManager
* manager
, GLuint service_id
);
65 GLuint
service_id() const {
69 bool HasUnclearedAttachment(GLenum attachment
) const;
70 bool HasUnclearedColorAttachments() const;
72 void MarkAttachmentAsCleared(
73 RenderbufferManager
* renderbuffer_manager
,
74 TextureManager
* texture_manager
,
78 // Unbinds all attachments from this framebuffer for workaround
79 // 'unbind_attachments_on_bound_render_fbo_delete'. The Framebuffer must be
80 // bound when calling this.
81 void DoUnbindGLAttachmentsForWorkaround(GLenum target
);
83 // Attaches a renderbuffer to a particlar attachment.
84 // Pass null to detach.
85 void AttachRenderbuffer(
86 GLenum attachment
, Renderbuffer
* renderbuffer
);
88 // Attaches a texture to a particlar attachment. Pass null to detach.
90 GLenum attachment
, TextureRef
* texture_ref
, GLenum target
,
91 GLint level
, GLsizei samples
);
93 // Unbinds the given renderbuffer if it is bound.
94 void UnbindRenderbuffer(
95 GLenum target
, Renderbuffer
* renderbuffer
);
97 // Unbinds the given texture if it is bound.
99 GLenum target
, TextureRef
* texture_ref
);
101 const Attachment
* GetAttachment(GLenum attachment
) const;
103 bool IsDeleted() const {
108 has_been_bound_
= true;
111 bool IsValid() const {
112 return has_been_bound_
&& !IsDeleted();
115 bool HasDepthAttachment() const;
116 bool HasStencilAttachment() const;
117 GLenum
GetColorAttachmentFormat() const;
118 // If the color attachment is a texture, returns its type; otherwise,
120 GLenum
GetColorAttachmentTextureType() const;
122 // Verify all the rules in OpenGL ES 2.0.25 4.4.5 are followed.
123 // Returns GL_FRAMEBUFFER_COMPLETE if there are no reasons we know we can't
124 // use this combination of attachments. Otherwise returns the value
125 // that glCheckFramebufferStatus should return for this set of attachments.
126 // Note that receiving GL_FRAMEBUFFER_COMPLETE from this function does
127 // not mean the real OpenGL will consider it framebuffer complete. It just
128 // means it passed our tests.
129 GLenum
IsPossiblyComplete() const;
131 // Implements optimized glGetFramebufferStatus.
132 GLenum
GetStatus(TextureManager
* texture_manager
, GLenum target
) const;
134 // Check all attachments are cleared
135 bool IsCleared() const;
137 GLenum
GetDrawBuffer(GLenum draw_buffer
) const;
139 void SetDrawBuffers(GLsizei n
, const GLenum
* bufs
);
141 // If a color buffer is attached to GL_COLOR_ATTACHMENTi, enable that
142 // draw buffer for glClear().
143 void PrepareDrawBuffersForClear() const;
145 // Restore draw buffers states that have been changed in
146 // PrepareDrawBuffersForClear().
147 void RestoreDrawBuffersAfterClear() const;
149 // Return true if any draw buffers has an alpha channel.
150 bool HasAlphaMRT() const;
152 static void ClearFramebufferCompleteComboMap();
154 static bool AllowFramebufferComboCompleteMapForTesting() {
155 return allow_framebuffer_combo_complete_map_
;
158 void OnTextureRefDetached(TextureRef
* texture
);
159 void OnWillRenderTo() const;
160 void OnDidRenderTo() const;
163 friend class FramebufferManager
;
164 friend class base::RefCounted
<Framebuffer
>;
168 void MarkAsDeleted();
170 void MarkAttachmentsAsCleared(
171 RenderbufferManager
* renderbuffer_manager
,
172 TextureManager
* texture_manager
,
175 void MarkAsComplete(unsigned state_id
) {
176 framebuffer_complete_state_count_id_
= state_id
;
179 unsigned framebuffer_complete_state_count_id() const {
180 return framebuffer_complete_state_count_id_
;
183 // Helper function for PrepareDrawBuffersForClear() and
184 // RestoreDrawBuffersAfterClear().
185 void ChangeDrawBuffersHelper(bool recover
) const;
187 // The managers that owns this.
188 FramebufferManager
* manager_
;
192 // Service side framebuffer id.
195 // Whether this framebuffer has ever been bound.
196 bool has_been_bound_
;
198 // state count when this framebuffer was last checked for completeness.
199 unsigned framebuffer_complete_state_count_id_
;
201 // A map of attachments.
202 typedef base::hash_map
<GLenum
, scoped_refptr
<Attachment
> > AttachmentMap
;
203 AttachmentMap attachments_
;
205 // A map of successful frame buffer combos. If it's in the map
206 // it should be FRAMEBUFFER_COMPLETE.
207 typedef base::hash_map
<std::string
, bool> FramebufferComboCompleteMap
;
208 static FramebufferComboCompleteMap
* framebuffer_combo_complete_map_
;
209 static bool allow_framebuffer_combo_complete_map_
;
211 scoped_ptr
<GLenum
[]> draw_buffers_
;
213 DISALLOW_COPY_AND_ASSIGN(Framebuffer
);
216 struct DecoderFramebufferState
{
217 DecoderFramebufferState();
218 ~DecoderFramebufferState();
220 // State saved for clearing so we can clear render buffers and then
221 // restore to these values.
222 bool clear_state_dirty
;
224 // The currently bound framebuffers
225 scoped_refptr
<Framebuffer
> bound_read_framebuffer
;
226 scoped_refptr
<Framebuffer
> bound_draw_framebuffer
;
229 // This class keeps track of the frambebuffers and their attached renderbuffers
230 // so we can correctly clear them.
231 class GPU_EXPORT FramebufferManager
{
233 class GPU_EXPORT TextureDetachObserver
{
235 TextureDetachObserver();
236 virtual ~TextureDetachObserver();
238 virtual void OnTextureRefDetachedFromFramebuffer(TextureRef
* texture
) = 0;
241 DISALLOW_COPY_AND_ASSIGN(TextureDetachObserver
);
244 FramebufferManager(uint32 max_draw_buffers
, uint32 max_color_attachments
,
245 ContextGroup::ContextType context_type
);
246 ~FramebufferManager();
248 // Must call before destruction.
249 void Destroy(bool have_context
);
251 // Creates a Framebuffer for the given framebuffer.
252 void CreateFramebuffer(GLuint client_id
, GLuint service_id
);
254 // Gets the framebuffer info for the given framebuffer.
255 Framebuffer
* GetFramebuffer(GLuint client_id
);
257 // Removes a framebuffer info for the given framebuffer.
258 void RemoveFramebuffer(GLuint client_id
);
260 // Gets a client id for a given service id.
261 bool GetClientId(GLuint service_id
, GLuint
* client_id
) const;
263 void MarkAttachmentsAsCleared(
264 Framebuffer
* framebuffer
,
265 RenderbufferManager
* renderbuffer_manager
,
266 TextureManager
* texture_manager
);
268 void MarkAsComplete(Framebuffer
* framebuffer
);
270 bool IsComplete(Framebuffer
* framebuffer
);
272 void IncFramebufferStateChangeCount() {
273 // make sure this is never 0.
274 framebuffer_state_change_count_
=
275 (framebuffer_state_change_count_
+ 1) | 0x80000000U
;
278 void AddObserver(TextureDetachObserver
* observer
) {
279 texture_detach_observers_
.push_back(observer
);
282 void RemoveObserver(TextureDetachObserver
* observer
) {
283 texture_detach_observers_
.erase(
284 std::remove(texture_detach_observers_
.begin(),
285 texture_detach_observers_
.end(),
287 texture_detach_observers_
.end());
290 ContextGroup::ContextType
context_type() const {
291 return context_type_
;
295 friend class Framebuffer
;
297 void StartTracking(Framebuffer
* framebuffer
);
298 void StopTracking(Framebuffer
* framebuffer
);
300 void OnTextureRefDetached(TextureRef
* texture
);
302 // Info for each framebuffer in the system.
303 typedef base::hash_map
<GLuint
, scoped_refptr
<Framebuffer
> >
305 FramebufferMap framebuffers_
;
307 // Incremented anytime anything changes that might effect framebuffer
309 unsigned framebuffer_state_change_count_
;
311 // Counts the number of Framebuffer allocated with 'this' as its manager.
312 // Allows to check no Framebuffer will outlive this.
313 unsigned int framebuffer_count_
;
317 uint32 max_draw_buffers_
;
318 uint32 max_color_attachments_
;
320 ContextGroup::ContextType context_type_
;
322 typedef std::vector
<TextureDetachObserver
*> TextureDetachObserverVector
;
323 TextureDetachObserverVector texture_detach_observers_
;
325 DISALLOW_COPY_AND_ASSIGN(FramebufferManager
);
331 #endif // GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_