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/gl_utils.h"
15 #include "gpu/gpu_export.h"
20 class FramebufferManager
;
22 class RenderbufferManager
;
27 // Info about a particular Framebuffer.
28 class GPU_EXPORT Framebuffer
: public base::RefCounted
<Framebuffer
> {
30 class Attachment
: public base::RefCounted
<Attachment
> {
32 virtual GLsizei
width() const = 0;
33 virtual GLsizei
height() const = 0;
34 virtual GLenum
internal_format() const = 0;
35 virtual GLenum
texture_type() const = 0;
36 virtual GLsizei
samples() const = 0;
37 virtual GLuint
object_name() const = 0;
38 virtual bool cleared() const = 0;
39 virtual void SetCleared(
40 RenderbufferManager
* renderbuffer_manager
,
41 TextureManager
* texture_manager
,
43 virtual bool IsTexture(TextureRef
* texture
) const = 0;
44 virtual bool IsRenderbuffer(
45 Renderbuffer
* renderbuffer
) const = 0;
46 virtual bool CanRenderTo() const = 0;
47 virtual void DetachFromFramebuffer(Framebuffer
* framebuffer
) const = 0;
48 virtual bool ValidForAttachmentType(
49 GLenum attachment_type
, uint32 max_color_attachments
) = 0;
50 virtual void AddToSignature(
51 TextureManager
* texture_manager
, std::string
* signature
) const = 0;
52 virtual void OnWillRenderTo() const = 0;
53 virtual void OnDidRenderTo() const = 0;
56 friend class base::RefCounted
<Attachment
>;
57 virtual ~Attachment() {}
60 Framebuffer(FramebufferManager
* manager
, GLuint service_id
);
62 GLuint
service_id() const {
66 bool HasUnclearedAttachment(GLenum attachment
) const;
67 bool HasUnclearedColorAttachments() const;
69 void MarkAttachmentAsCleared(
70 RenderbufferManager
* renderbuffer_manager
,
71 TextureManager
* texture_manager
,
75 // Attaches a renderbuffer to a particlar attachment.
76 // Pass null to detach.
77 void AttachRenderbuffer(
78 GLenum attachment
, Renderbuffer
* renderbuffer
);
80 // Attaches a texture to a particlar attachment. Pass null to detach.
82 GLenum attachment
, TextureRef
* texture_ref
, GLenum target
,
83 GLint level
, GLsizei samples
);
85 // Unbinds the given renderbuffer if it is bound.
86 void UnbindRenderbuffer(
87 GLenum target
, Renderbuffer
* renderbuffer
);
89 // Unbinds the given texture if it is bound.
91 GLenum target
, TextureRef
* texture_ref
);
93 const Attachment
* GetAttachment(GLenum attachment
) const;
95 bool IsDeleted() const {
100 has_been_bound_
= true;
103 bool IsValid() const {
104 return has_been_bound_
&& !IsDeleted();
107 bool HasDepthAttachment() const;
108 bool HasStencilAttachment() const;
109 GLenum
GetColorAttachmentFormat() const;
110 // If the color attachment is a texture, returns its type; otherwise,
112 GLenum
GetColorAttachmentTextureType() const;
114 // Verify all the rules in OpenGL ES 2.0.25 4.4.5 are followed.
115 // Returns GL_FRAMEBUFFER_COMPLETE if there are no reasons we know we can't
116 // use this combination of attachments. Otherwise returns the value
117 // that glCheckFramebufferStatus should return for this set of attachments.
118 // Note that receiving GL_FRAMEBUFFER_COMPLETE from this function does
119 // not mean the real OpenGL will consider it framebuffer complete. It just
120 // means it passed our tests.
121 GLenum
IsPossiblyComplete() const;
123 // Implements optimized glGetFramebufferStatus.
124 GLenum
GetStatus(TextureManager
* texture_manager
, GLenum target
) const;
126 // Check all attachments are cleared
127 bool IsCleared() const;
129 GLenum
GetDrawBuffer(GLenum draw_buffer
) const;
131 void SetDrawBuffers(GLsizei n
, const GLenum
* bufs
);
133 // If a color buffer is attached to GL_COLOR_ATTACHMENTi, enable that
134 // draw buffer for glClear().
135 void PrepareDrawBuffersForClear() const;
137 // Restore draw buffers states that have been changed in
138 // PrepareDrawBuffersForClear().
139 void RestoreDrawBuffersAfterClear() const;
141 // Return true if any draw buffers has an alpha channel.
142 bool HasAlphaMRT() const;
144 static void ClearFramebufferCompleteComboMap();
146 static bool AllowFramebufferComboCompleteMapForTesting() {
147 return allow_framebuffer_combo_complete_map_
;
150 void OnTextureRefDetached(TextureRef
* texture
);
151 void OnWillRenderTo() const;
152 void OnDidRenderTo() const;
155 friend class FramebufferManager
;
156 friend class base::RefCounted
<Framebuffer
>;
160 void MarkAsDeleted();
162 void MarkAttachmentsAsCleared(
163 RenderbufferManager
* renderbuffer_manager
,
164 TextureManager
* texture_manager
,
167 void MarkAsComplete(unsigned state_id
) {
168 framebuffer_complete_state_count_id_
= state_id
;
171 unsigned framebuffer_complete_state_count_id() const {
172 return framebuffer_complete_state_count_id_
;
175 // Helper function for PrepareDrawBuffersForClear() and
176 // RestoreDrawBuffersAfterClear().
177 void ChangeDrawBuffersHelper(bool recover
) const;
179 // The managers that owns this.
180 FramebufferManager
* manager_
;
184 // Service side framebuffer id.
187 // Whether this framebuffer has ever been bound.
188 bool has_been_bound_
;
190 // state count when this framebuffer was last checked for completeness.
191 unsigned framebuffer_complete_state_count_id_
;
193 // A map of attachments.
194 typedef base::hash_map
<GLenum
, scoped_refptr
<Attachment
> > AttachmentMap
;
195 AttachmentMap attachments_
;
197 // A map of successful frame buffer combos. If it's in the map
198 // it should be FRAMEBUFFER_COMPLETE.
199 typedef base::hash_map
<std::string
, bool> FramebufferComboCompleteMap
;
200 static FramebufferComboCompleteMap
* framebuffer_combo_complete_map_
;
201 static bool allow_framebuffer_combo_complete_map_
;
203 scoped_ptr
<GLenum
[]> draw_buffers_
;
205 DISALLOW_COPY_AND_ASSIGN(Framebuffer
);
208 struct DecoderFramebufferState
{
209 DecoderFramebufferState();
210 ~DecoderFramebufferState();
212 // State saved for clearing so we can clear render buffers and then
213 // restore to these values.
214 bool clear_state_dirty
;
216 // The currently bound framebuffers
217 scoped_refptr
<Framebuffer
> bound_read_framebuffer
;
218 scoped_refptr
<Framebuffer
> bound_draw_framebuffer
;
221 // This class keeps track of the frambebuffers and their attached renderbuffers
222 // so we can correctly clear them.
223 class GPU_EXPORT FramebufferManager
{
225 class GPU_EXPORT TextureDetachObserver
{
227 TextureDetachObserver();
228 virtual ~TextureDetachObserver();
230 virtual void OnTextureRefDetachedFromFramebuffer(TextureRef
* texture
) = 0;
233 DISALLOW_COPY_AND_ASSIGN(TextureDetachObserver
);
236 FramebufferManager(uint32 max_draw_buffers
, uint32 max_color_attachments
);
237 ~FramebufferManager();
239 // Must call before destruction.
240 void Destroy(bool have_context
);
242 // Creates a Framebuffer for the given framebuffer.
243 void CreateFramebuffer(GLuint client_id
, GLuint service_id
);
245 // Gets the framebuffer info for the given framebuffer.
246 Framebuffer
* GetFramebuffer(GLuint client_id
);
248 // Removes a framebuffer info for the given framebuffer.
249 void RemoveFramebuffer(GLuint client_id
);
251 // Gets a client id for a given service id.
252 bool GetClientId(GLuint service_id
, GLuint
* client_id
) const;
254 void MarkAttachmentsAsCleared(
255 Framebuffer
* framebuffer
,
256 RenderbufferManager
* renderbuffer_manager
,
257 TextureManager
* texture_manager
);
259 void MarkAsComplete(Framebuffer
* framebuffer
);
261 bool IsComplete(Framebuffer
* framebuffer
);
263 void IncFramebufferStateChangeCount() {
264 // make sure this is never 0.
265 framebuffer_state_change_count_
=
266 (framebuffer_state_change_count_
+ 1) | 0x80000000U
;
269 void AddObserver(TextureDetachObserver
* observer
) {
270 texture_detach_observers_
.push_back(observer
);
273 void RemoveObserver(TextureDetachObserver
* observer
) {
274 texture_detach_observers_
.erase(
275 std::remove(texture_detach_observers_
.begin(),
276 texture_detach_observers_
.end(),
278 texture_detach_observers_
.end());
282 friend class Framebuffer
;
284 void StartTracking(Framebuffer
* framebuffer
);
285 void StopTracking(Framebuffer
* framebuffer
);
287 void OnTextureRefDetached(TextureRef
* texture
);
289 // Info for each framebuffer in the system.
290 typedef base::hash_map
<GLuint
, scoped_refptr
<Framebuffer
> >
292 FramebufferMap framebuffers_
;
294 // Incremented anytime anything changes that might effect framebuffer
296 unsigned framebuffer_state_change_count_
;
298 // Counts the number of Framebuffer allocated with 'this' as its manager.
299 // Allows to check no Framebuffer will outlive this.
300 unsigned int framebuffer_count_
;
304 uint32 max_draw_buffers_
;
305 uint32 max_color_attachments_
;
307 typedef std::vector
<TextureDetachObserver
*> TextureDetachObserverVector
;
308 TextureDetachObserverVector texture_detach_observers_
;
310 DISALLOW_COPY_AND_ASSIGN(FramebufferManager
);
316 #endif // GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_