Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / gpu / command_buffer / service / framebuffer_manager.h
blob9bf440a17d03b18bd3491e859bd90c93f7108c0f
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_
8 #include <vector>
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"
18 namespace gpu {
19 namespace gles2 {
21 class FramebufferManager;
22 class Renderbuffer;
23 class RenderbufferManager;
24 class Texture;
25 class TextureRef;
26 class TextureManager;
28 // Info about a particular Framebuffer.
29 class GPU_EXPORT Framebuffer : public base::RefCounted<Framebuffer> {
30 public:
31 class Attachment : public base::RefCounted<Attachment> {
32 public:
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,
43 bool cleared) = 0;
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;
58 protected:
59 friend class base::RefCounted<Attachment>;
60 virtual ~Attachment() {}
63 Framebuffer(FramebufferManager* manager, GLuint service_id);
65 GLuint service_id() const {
66 return service_id_;
69 bool HasUnclearedAttachment(GLenum attachment) const;
70 bool HasUnclearedColorAttachments() const;
72 void MarkAttachmentAsCleared(
73 RenderbufferManager* renderbuffer_manager,
74 TextureManager* texture_manager,
75 GLenum attachment,
76 bool cleared);
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.
89 void AttachTexture(
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.
98 void UnbindTexture(
99 GLenum target, TextureRef* texture_ref);
101 const Attachment* GetAttachment(GLenum attachment) const;
103 const Attachment* GetReadBufferAttachment() const;
105 bool IsDeleted() const {
106 return deleted_;
109 void MarkAsValid() {
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,
122 // returns 0.
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
156 // formats.
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 {
174 return read_buffer_;
177 private:
178 friend class FramebufferManager;
179 friend class base::RefCounted<Framebuffer>;
181 ~Framebuffer();
183 void MarkAsDeleted();
185 void MarkAttachmentsAsCleared(
186 RenderbufferManager* renderbuffer_manager,
187 TextureManager* texture_manager,
188 bool cleared);
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_;
205 bool deleted_;
207 // Service side framebuffer id.
208 GLuint service_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_;
228 GLenum read_buffer_;
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 {
249 public:
250 class GPU_EXPORT TextureDetachObserver {
251 public:
252 TextureDetachObserver();
253 virtual ~TextureDetachObserver();
255 virtual void OnTextureRefDetachedFromFramebuffer(TextureRef* texture) = 0;
257 private:
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(),
303 observer),
304 texture_detach_observers_.end());
307 ContextGroup::ContextType context_type() const {
308 return context_type_;
311 private:
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> >
321 FramebufferMap;
322 FramebufferMap framebuffers_;
324 // Incremented anytime anything changes that might effect framebuffer
325 // state.
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_;
332 bool have_context_;
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);
345 } // namespace gles2
346 } // namespace gpu
348 #endif // GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_