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
GetDrawBufferInternalFormat() const;
120 GLenum
GetReadBufferInternalFormat() const;
121 // If the color attachment is a texture, returns its type; otherwise,
123 GLenum
GetReadBufferTextureType() const;
125 // Verify all the rules in OpenGL ES 2.0.25 4.4.5 are followed.
126 // Returns GL_FRAMEBUFFER_COMPLETE if there are no reasons we know we can't
127 // use this combination of attachments. Otherwise returns the value
128 // that glCheckFramebufferStatus should return for this set of attachments.
129 // Note that receiving GL_FRAMEBUFFER_COMPLETE from this function does
130 // not mean the real OpenGL will consider it framebuffer complete. It just
131 // means it passed our tests.
132 GLenum
IsPossiblyComplete() const;
134 // Implements optimized glGetFramebufferStatus.
135 GLenum
GetStatus(TextureManager
* texture_manager
, GLenum target
) const;
137 // Check all attachments are cleared
138 bool IsCleared() const;
140 GLenum
GetDrawBuffer(GLenum draw_buffer
) const;
142 void SetDrawBuffers(GLsizei n
, const GLenum
* bufs
);
144 // If a color buffer is attached to GL_COLOR_ATTACHMENTi, enable that
145 // draw buffer for glClear().
146 void PrepareDrawBuffersForClear() const;
148 // Restore draw buffers states that have been changed in
149 // PrepareDrawBuffersForClear().
150 void RestoreDrawBuffersAfterClear() const;
152 // Return true if any draw buffers has an alpha channel.
153 bool HasAlphaMRT() const;
155 // Return false if any two active color attachments have different internal
157 bool HasSameInternalFormatsMRT() const;
159 static void ClearFramebufferCompleteComboMap();
161 static bool AllowFramebufferComboCompleteMapForTesting() {
162 return allow_framebuffer_combo_complete_map_
;
165 void OnTextureRefDetached(TextureRef
* texture
);
166 void OnWillRenderTo() const;
167 void OnDidRenderTo() const;
169 void set_read_buffer(GLenum read_buffer
) {
170 read_buffer_
= read_buffer
;
173 GLenum
read_buffer() const {
178 friend class FramebufferManager
;
179 friend class base::RefCounted
<Framebuffer
>;
183 void MarkAsDeleted();
185 void MarkAttachmentsAsCleared(
186 RenderbufferManager
* renderbuffer_manager
,
187 TextureManager
* texture_manager
,
190 void MarkAsComplete(unsigned state_id
) {
191 framebuffer_complete_state_count_id_
= state_id
;
194 unsigned framebuffer_complete_state_count_id() const {
195 return framebuffer_complete_state_count_id_
;
198 // Helper function for PrepareDrawBuffersForClear() and
199 // RestoreDrawBuffersAfterClear().
200 void ChangeDrawBuffersHelper(bool recover
) const;
202 // The managers that owns this.
203 FramebufferManager
* manager_
;
207 // Service side framebuffer id.
210 // Whether this framebuffer has ever been bound.
211 bool has_been_bound_
;
213 // state count when this framebuffer was last checked for completeness.
214 unsigned framebuffer_complete_state_count_id_
;
216 // A map of attachments.
217 typedef base::hash_map
<GLenum
, scoped_refptr
<Attachment
> > AttachmentMap
;
218 AttachmentMap attachments_
;
220 // A map of successful frame buffer combos. If it's in the map
221 // it should be FRAMEBUFFER_COMPLETE.
222 typedef base::hash_map
<std::string
, bool> FramebufferComboCompleteMap
;
223 static FramebufferComboCompleteMap
* framebuffer_combo_complete_map_
;
224 static bool allow_framebuffer_combo_complete_map_
;
226 scoped_ptr
<GLenum
[]> draw_buffers_
;
230 DISALLOW_COPY_AND_ASSIGN(Framebuffer
);
233 struct DecoderFramebufferState
{
234 DecoderFramebufferState();
235 ~DecoderFramebufferState();
237 // State saved for clearing so we can clear render buffers and then
238 // restore to these values.
239 bool clear_state_dirty
;
241 // The currently bound framebuffers
242 scoped_refptr
<Framebuffer
> bound_read_framebuffer
;
243 scoped_refptr
<Framebuffer
> bound_draw_framebuffer
;
246 // This class keeps track of the frambebuffers and their attached renderbuffers
247 // so we can correctly clear them.
248 class GPU_EXPORT FramebufferManager
{
250 class GPU_EXPORT TextureDetachObserver
{
252 TextureDetachObserver();
253 virtual ~TextureDetachObserver();
255 virtual void OnTextureRefDetachedFromFramebuffer(TextureRef
* texture
) = 0;
258 DISALLOW_COPY_AND_ASSIGN(TextureDetachObserver
);
261 FramebufferManager(uint32 max_draw_buffers
, uint32 max_color_attachments
,
262 ContextGroup::ContextType context_type
);
263 ~FramebufferManager();
265 // Must call before destruction.
266 void Destroy(bool have_context
);
268 // Creates a Framebuffer for the given framebuffer.
269 void CreateFramebuffer(GLuint client_id
, GLuint service_id
);
271 // Gets the framebuffer info for the given framebuffer.
272 Framebuffer
* GetFramebuffer(GLuint client_id
);
274 // Removes a framebuffer info for the given framebuffer.
275 void RemoveFramebuffer(GLuint client_id
);
277 // Gets a client id for a given service id.
278 bool GetClientId(GLuint service_id
, GLuint
* client_id
) const;
280 void MarkAttachmentsAsCleared(
281 Framebuffer
* framebuffer
,
282 RenderbufferManager
* renderbuffer_manager
,
283 TextureManager
* texture_manager
);
285 void MarkAsComplete(Framebuffer
* framebuffer
);
287 bool IsComplete(Framebuffer
* framebuffer
);
289 void IncFramebufferStateChangeCount() {
290 // make sure this is never 0.
291 framebuffer_state_change_count_
=
292 (framebuffer_state_change_count_
+ 1) | 0x80000000U
;
295 void AddObserver(TextureDetachObserver
* observer
) {
296 texture_detach_observers_
.push_back(observer
);
299 void RemoveObserver(TextureDetachObserver
* observer
) {
300 texture_detach_observers_
.erase(
301 std::remove(texture_detach_observers_
.begin(),
302 texture_detach_observers_
.end(),
304 texture_detach_observers_
.end());
307 ContextGroup::ContextType
context_type() const {
308 return context_type_
;
312 friend class Framebuffer
;
314 void StartTracking(Framebuffer
* framebuffer
);
315 void StopTracking(Framebuffer
* framebuffer
);
317 void OnTextureRefDetached(TextureRef
* texture
);
319 // Info for each framebuffer in the system.
320 typedef base::hash_map
<GLuint
, scoped_refptr
<Framebuffer
> >
322 FramebufferMap framebuffers_
;
324 // Incremented anytime anything changes that might effect framebuffer
326 unsigned framebuffer_state_change_count_
;
328 // Counts the number of Framebuffer allocated with 'this' as its manager.
329 // Allows to check no Framebuffer will outlive this.
330 unsigned int framebuffer_count_
;
334 uint32 max_draw_buffers_
;
335 uint32 max_color_attachments_
;
337 ContextGroup::ContextType context_type_
;
339 typedef std::vector
<TextureDetachObserver
*> TextureDetachObserverVector
;
340 TextureDetachObserverVector texture_detach_observers_
;
342 DISALLOW_COPY_AND_ASSIGN(FramebufferManager
);
348 #endif // GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_