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 const Attachment
* GetReadBufferAttachment() const;
105 bool IsDeleted() const {
110 has_been_bound_
= true;
113 bool IsValid() const {
114 return has_been_bound_
&& !IsDeleted();
117 bool HasDepthAttachment() const;
118 bool HasStencilAttachment() const;
119 GLenum
GetColorAttachmentFormat() const;
120 // If the color attachment is a texture, returns its type; otherwise,
122 GLenum
GetColorAttachmentTextureType() const;
124 // Verify all the rules in OpenGL ES 2.0.25 4.4.5 are followed.
125 // Returns GL_FRAMEBUFFER_COMPLETE if there are no reasons we know we can't
126 // use this combination of attachments. Otherwise returns the value
127 // that glCheckFramebufferStatus should return for this set of attachments.
128 // Note that receiving GL_FRAMEBUFFER_COMPLETE from this function does
129 // not mean the real OpenGL will consider it framebuffer complete. It just
130 // means it passed our tests.
131 GLenum
IsPossiblyComplete() const;
133 // Implements optimized glGetFramebufferStatus.
134 GLenum
GetStatus(TextureManager
* texture_manager
, GLenum target
) const;
136 // Check all attachments are cleared
137 bool IsCleared() const;
139 GLenum
GetDrawBuffer(GLenum draw_buffer
) const;
141 void SetDrawBuffers(GLsizei n
, const GLenum
* bufs
);
143 // If a color buffer is attached to GL_COLOR_ATTACHMENTi, enable that
144 // draw buffer for glClear().
145 void PrepareDrawBuffersForClear() const;
147 // Restore draw buffers states that have been changed in
148 // PrepareDrawBuffersForClear().
149 void RestoreDrawBuffersAfterClear() const;
151 // Return true if any draw buffers has an alpha channel.
152 bool HasAlphaMRT() const;
154 static void ClearFramebufferCompleteComboMap();
156 static bool AllowFramebufferComboCompleteMapForTesting() {
157 return allow_framebuffer_combo_complete_map_
;
160 void OnTextureRefDetached(TextureRef
* texture
);
161 void OnWillRenderTo() const;
162 void OnDidRenderTo() const;
164 void set_read_buffer(GLenum read_buffer
) {
165 read_buffer_
= read_buffer
;
168 GLenum
read_buffer() const {
173 friend class FramebufferManager
;
174 friend class base::RefCounted
<Framebuffer
>;
178 void MarkAsDeleted();
180 void MarkAttachmentsAsCleared(
181 RenderbufferManager
* renderbuffer_manager
,
182 TextureManager
* texture_manager
,
185 void MarkAsComplete(unsigned state_id
) {
186 framebuffer_complete_state_count_id_
= state_id
;
189 unsigned framebuffer_complete_state_count_id() const {
190 return framebuffer_complete_state_count_id_
;
193 // Helper function for PrepareDrawBuffersForClear() and
194 // RestoreDrawBuffersAfterClear().
195 void ChangeDrawBuffersHelper(bool recover
) const;
197 // The managers that owns this.
198 FramebufferManager
* manager_
;
202 // Service side framebuffer id.
205 // Whether this framebuffer has ever been bound.
206 bool has_been_bound_
;
208 // state count when this framebuffer was last checked for completeness.
209 unsigned framebuffer_complete_state_count_id_
;
211 // A map of attachments.
212 typedef base::hash_map
<GLenum
, scoped_refptr
<Attachment
> > AttachmentMap
;
213 AttachmentMap attachments_
;
215 // A map of successful frame buffer combos. If it's in the map
216 // it should be FRAMEBUFFER_COMPLETE.
217 typedef base::hash_map
<std::string
, bool> FramebufferComboCompleteMap
;
218 static FramebufferComboCompleteMap
* framebuffer_combo_complete_map_
;
219 static bool allow_framebuffer_combo_complete_map_
;
221 scoped_ptr
<GLenum
[]> draw_buffers_
;
225 DISALLOW_COPY_AND_ASSIGN(Framebuffer
);
228 struct DecoderFramebufferState
{
229 DecoderFramebufferState();
230 ~DecoderFramebufferState();
232 // State saved for clearing so we can clear render buffers and then
233 // restore to these values.
234 bool clear_state_dirty
;
236 // The currently bound framebuffers
237 scoped_refptr
<Framebuffer
> bound_read_framebuffer
;
238 scoped_refptr
<Framebuffer
> bound_draw_framebuffer
;
241 // This class keeps track of the frambebuffers and their attached renderbuffers
242 // so we can correctly clear them.
243 class GPU_EXPORT FramebufferManager
{
245 class GPU_EXPORT TextureDetachObserver
{
247 TextureDetachObserver();
248 virtual ~TextureDetachObserver();
250 virtual void OnTextureRefDetachedFromFramebuffer(TextureRef
* texture
) = 0;
253 DISALLOW_COPY_AND_ASSIGN(TextureDetachObserver
);
256 FramebufferManager(uint32 max_draw_buffers
, uint32 max_color_attachments
,
257 ContextGroup::ContextType context_type
);
258 ~FramebufferManager();
260 // Must call before destruction.
261 void Destroy(bool have_context
);
263 // Creates a Framebuffer for the given framebuffer.
264 void CreateFramebuffer(GLuint client_id
, GLuint service_id
);
266 // Gets the framebuffer info for the given framebuffer.
267 Framebuffer
* GetFramebuffer(GLuint client_id
);
269 // Removes a framebuffer info for the given framebuffer.
270 void RemoveFramebuffer(GLuint client_id
);
272 // Gets a client id for a given service id.
273 bool GetClientId(GLuint service_id
, GLuint
* client_id
) const;
275 void MarkAttachmentsAsCleared(
276 Framebuffer
* framebuffer
,
277 RenderbufferManager
* renderbuffer_manager
,
278 TextureManager
* texture_manager
);
280 void MarkAsComplete(Framebuffer
* framebuffer
);
282 bool IsComplete(Framebuffer
* framebuffer
);
284 void IncFramebufferStateChangeCount() {
285 // make sure this is never 0.
286 framebuffer_state_change_count_
=
287 (framebuffer_state_change_count_
+ 1) | 0x80000000U
;
290 void AddObserver(TextureDetachObserver
* observer
) {
291 texture_detach_observers_
.push_back(observer
);
294 void RemoveObserver(TextureDetachObserver
* observer
) {
295 texture_detach_observers_
.erase(
296 std::remove(texture_detach_observers_
.begin(),
297 texture_detach_observers_
.end(),
299 texture_detach_observers_
.end());
302 ContextGroup::ContextType
context_type() const {
303 return context_type_
;
307 friend class Framebuffer
;
309 void StartTracking(Framebuffer
* framebuffer
);
310 void StopTracking(Framebuffer
* framebuffer
);
312 void OnTextureRefDetached(TextureRef
* texture
);
314 // Info for each framebuffer in the system.
315 typedef base::hash_map
<GLuint
, scoped_refptr
<Framebuffer
> >
317 FramebufferMap framebuffers_
;
319 // Incremented anytime anything changes that might effect framebuffer
321 unsigned framebuffer_state_change_count_
;
323 // Counts the number of Framebuffer allocated with 'this' as its manager.
324 // Allows to check no Framebuffer will outlive this.
325 unsigned int framebuffer_count_
;
329 uint32 max_draw_buffers_
;
330 uint32 max_color_attachments_
;
332 ContextGroup::ContextType context_type_
;
334 typedef std::vector
<TextureDetachObserver
*> TextureDetachObserverVector
;
335 TextureDetachObserverVector texture_detach_observers_
;
337 DISALLOW_COPY_AND_ASSIGN(FramebufferManager
);
343 #endif // GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_