| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_ | 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_ |
| 6 #define GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_ | 6 #define GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/hash_tables.h" | 9 #include "base/hash_tables.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "gpu/command_buffer/service/gl_utils.h" | 12 #include "gpu/command_buffer/service/gl_utils.h" |
| 13 #include "gpu/command_buffer/service/renderbuffer_manager.h" | 13 #include "gpu/command_buffer/service/renderbuffer_manager.h" |
| 14 #include "gpu/command_buffer/service/texture_manager.h" | 14 #include "gpu/command_buffer/service/texture_manager.h" |
| 15 #include "gpu/gpu_export.h" | 15 #include "gpu/gpu_export.h" |
| 16 | 16 |
| 17 namespace gpu { | 17 namespace gpu { |
| 18 namespace gles2 { | 18 namespace gles2 { |
| 19 | 19 |
| 20 class FramebufferManager; |
| 21 |
| 22 // Info about a particular Framebuffer. |
| 23 class GPU_EXPORT Framebuffer : public base::RefCounted<Framebuffer> { |
| 24 public: |
| 25 class Attachment : public base::RefCounted<Attachment> { |
| 26 public: |
| 27 virtual GLsizei width() const = 0; |
| 28 virtual GLsizei height() const = 0; |
| 29 virtual GLenum internal_format() const = 0; |
| 30 virtual GLsizei samples() const = 0; |
| 31 virtual bool cleared() const = 0; |
| 32 virtual void SetCleared( |
| 33 RenderbufferManager* renderbuffer_manager, |
| 34 TextureManager* texture_manager, |
| 35 bool cleared) = 0; |
| 36 virtual bool IsTexture(Texture* texture) const = 0; |
| 37 virtual bool IsRenderbuffer( |
| 38 Renderbuffer* renderbuffer) const = 0; |
| 39 virtual bool CanRenderTo() const = 0; |
| 40 virtual void DetachFromFramebuffer() const = 0; |
| 41 virtual bool ValidForAttachmentType(GLenum attachment_type) = 0; |
| 42 virtual void AddToSignature( |
| 43 TextureManager* texture_manager, std::string* signature) const = 0; |
| 44 |
| 45 protected: |
| 46 friend class base::RefCounted<Attachment>; |
| 47 virtual ~Attachment() {} |
| 48 }; |
| 49 |
| 50 Framebuffer(FramebufferManager* manager, GLuint service_id); |
| 51 |
| 52 GLuint service_id() const { |
| 53 return service_id_; |
| 54 } |
| 55 |
| 56 bool HasUnclearedAttachment(GLenum attachment) const; |
| 57 |
| 58 void MarkAttachmentAsCleared( |
| 59 RenderbufferManager* renderbuffer_manager, |
| 60 TextureManager* texture_manager, |
| 61 GLenum attachment, |
| 62 bool cleared); |
| 63 |
| 64 // Attaches a renderbuffer to a particlar attachment. |
| 65 // Pass null to detach. |
| 66 void AttachRenderbuffer( |
| 67 GLenum attachment, Renderbuffer* renderbuffer); |
| 68 |
| 69 // Attaches a texture to a particlar attachment. Pass null to detach. |
| 70 void AttachTexture( |
| 71 GLenum attachment, Texture* texture, GLenum target, |
| 72 GLint level); |
| 73 |
| 74 // Unbinds the given renderbuffer if it is bound. |
| 75 void UnbindRenderbuffer( |
| 76 GLenum target, Renderbuffer* renderbuffer); |
| 77 |
| 78 // Unbinds the given texture if it is bound. |
| 79 void UnbindTexture( |
| 80 GLenum target, Texture* texture); |
| 81 |
| 82 const Attachment* GetAttachment(GLenum attachment) const; |
| 83 |
| 84 bool IsDeleted() const { |
| 85 return deleted_; |
| 86 } |
| 87 |
| 88 void MarkAsValid() { |
| 89 has_been_bound_ = true; |
| 90 } |
| 91 |
| 92 bool IsValid() const { |
| 93 return has_been_bound_ && !IsDeleted(); |
| 94 } |
| 95 |
| 96 bool HasDepthAttachment() const; |
| 97 bool HasStencilAttachment() const; |
| 98 GLenum GetColorAttachmentFormat() const; |
| 99 |
| 100 // Verify all the rules in OpenGL ES 2.0.25 4.4.5 are followed. |
| 101 // Returns GL_FRAMEBUFFER_COMPLETE if there are no reasons we know we can't |
| 102 // use this combination of attachments. Otherwise returns the value |
| 103 // that glCheckFramebufferStatus should return for this set of attachments. |
| 104 // Note that receiving GL_FRAMEBUFFER_COMPLETE from this function does |
| 105 // not mean the real OpenGL will consider it framebuffer complete. It just |
| 106 // means it passed our tests. |
| 107 GLenum IsPossiblyComplete() const; |
| 108 |
| 109 // Implements optimized glGetFramebufferStatus. |
| 110 GLenum GetStatus(TextureManager* texture_manager, GLenum target) const; |
| 111 |
| 112 // Check all attachments are cleared |
| 113 bool IsCleared() const; |
| 114 |
| 115 static void ClearFramebufferCompleteComboMap(); |
| 116 |
| 117 private: |
| 118 friend class FramebufferManager; |
| 119 friend class base::RefCounted<Framebuffer>; |
| 120 |
| 121 ~Framebuffer(); |
| 122 |
| 123 void MarkAsDeleted(); |
| 124 |
| 125 void MarkAttachmentsAsCleared( |
| 126 RenderbufferManager* renderbuffer_manager, |
| 127 TextureManager* texture_manager, |
| 128 bool cleared); |
| 129 |
| 130 void MarkAsComplete(unsigned state_id) { |
| 131 framebuffer_complete_state_count_id_ = state_id; |
| 132 } |
| 133 |
| 134 unsigned framebuffer_complete_state_count_id() const { |
| 135 return framebuffer_complete_state_count_id_; |
| 136 } |
| 137 |
| 138 // The managers that owns this. |
| 139 FramebufferManager* manager_; |
| 140 |
| 141 bool deleted_; |
| 142 |
| 143 // Service side framebuffer id. |
| 144 GLuint service_id_; |
| 145 |
| 146 // Whether this framebuffer has ever been bound. |
| 147 bool has_been_bound_; |
| 148 |
| 149 // state count when this framebuffer was last checked for completeness. |
| 150 unsigned framebuffer_complete_state_count_id_; |
| 151 |
| 152 // A map of attachments. |
| 153 typedef base::hash_map<GLenum, scoped_refptr<Attachment> > AttachmentMap; |
| 154 AttachmentMap attachments_; |
| 155 |
| 156 // A map of successful frame buffer combos. If it's in the map |
| 157 // it should be FRAMEBUFFER_COMPLETE. |
| 158 typedef base::hash_map<std::string, bool> FramebufferComboCompleteMap; |
| 159 static FramebufferComboCompleteMap* framebuffer_combo_complete_map_; |
| 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(Framebuffer); |
| 162 }; |
| 163 |
| 20 // This class keeps track of the frambebuffers and their attached renderbuffers | 164 // This class keeps track of the frambebuffers and their attached renderbuffers |
| 21 // so we can correctly clear them. | 165 // so we can correctly clear them. |
| 22 class GPU_EXPORT FramebufferManager { | 166 class GPU_EXPORT FramebufferManager { |
| 23 public: | 167 public: |
| 24 // Info about Framebuffers currently in the system. | |
| 25 class GPU_EXPORT FramebufferInfo : public base::RefCounted<FramebufferInfo> { | |
| 26 public: | |
| 27 typedef scoped_refptr<FramebufferInfo> Ref; | |
| 28 | |
| 29 class Attachment : public base::RefCounted<Attachment> { | |
| 30 public: | |
| 31 typedef scoped_refptr<Attachment> Ref; | |
| 32 | |
| 33 virtual GLsizei width() const = 0; | |
| 34 virtual GLsizei height() const = 0; | |
| 35 virtual GLenum internal_format() const = 0; | |
| 36 virtual GLsizei samples() const = 0; | |
| 37 virtual bool cleared() const = 0; | |
| 38 virtual void SetCleared( | |
| 39 RenderbufferManager* renderbuffer_manager, | |
| 40 TextureManager* texture_manager, | |
| 41 bool cleared) = 0; | |
| 42 virtual bool IsTexture(TextureManager::TextureInfo* texture) const = 0; | |
| 43 virtual bool IsRenderbuffer( | |
| 44 RenderbufferManager::RenderbufferInfo* renderbuffer) const = 0; | |
| 45 virtual bool CanRenderTo() const = 0; | |
| 46 virtual void DetachFromFramebuffer() const = 0; | |
| 47 virtual bool ValidForAttachmentType(GLenum attachment_type) = 0; | |
| 48 virtual void AddToSignature( | |
| 49 TextureManager* texture_manager, std::string* signature) const = 0; | |
| 50 | |
| 51 protected: | |
| 52 friend class base::RefCounted<Attachment>; | |
| 53 virtual ~Attachment() {} | |
| 54 }; | |
| 55 | |
| 56 FramebufferInfo(FramebufferManager* manager, GLuint service_id); | |
| 57 | |
| 58 GLuint service_id() const { | |
| 59 return service_id_; | |
| 60 } | |
| 61 | |
| 62 bool HasUnclearedAttachment(GLenum attachment) const; | |
| 63 | |
| 64 void MarkAttachmentAsCleared( | |
| 65 RenderbufferManager* renderbuffer_manager, | |
| 66 TextureManager* texture_manager, | |
| 67 GLenum attachment, | |
| 68 bool cleared); | |
| 69 | |
| 70 // Attaches a renderbuffer to a particlar attachment. | |
| 71 // Pass null to detach. | |
| 72 void AttachRenderbuffer( | |
| 73 GLenum attachment, RenderbufferManager::RenderbufferInfo* renderbuffer); | |
| 74 | |
| 75 // Attaches a texture to a particlar attachment. Pass null to detach. | |
| 76 void AttachTexture( | |
| 77 GLenum attachment, TextureManager::TextureInfo* texture, GLenum target, | |
| 78 GLint level); | |
| 79 | |
| 80 // Unbinds the given renderbuffer if it is bound. | |
| 81 void UnbindRenderbuffer( | |
| 82 GLenum target, RenderbufferManager::RenderbufferInfo* renderbuffer); | |
| 83 | |
| 84 // Unbinds the given texture if it is bound. | |
| 85 void UnbindTexture( | |
| 86 GLenum target, TextureManager::TextureInfo* texture); | |
| 87 | |
| 88 const Attachment* GetAttachment(GLenum attachment) const; | |
| 89 | |
| 90 bool IsDeleted() const { | |
| 91 return deleted_; | |
| 92 } | |
| 93 | |
| 94 void MarkAsValid() { | |
| 95 has_been_bound_ = true; | |
| 96 } | |
| 97 | |
| 98 bool IsValid() const { | |
| 99 return has_been_bound_ && !IsDeleted(); | |
| 100 } | |
| 101 | |
| 102 bool HasDepthAttachment() const; | |
| 103 bool HasStencilAttachment() const; | |
| 104 GLenum GetColorAttachmentFormat() const; | |
| 105 | |
| 106 // Verify all the rules in OpenGL ES 2.0.25 4.4.5 are followed. | |
| 107 // Returns GL_FRAMEBUFFER_COMPLETE if there are no reasons we know we can't | |
| 108 // use this combination of attachments. Otherwise returns the value | |
| 109 // that glCheckFramebufferStatus should return for this set of attachments. | |
| 110 // Note that receiving GL_FRAMEBUFFER_COMPLETE from this function does | |
| 111 // not mean the real OpenGL will consider it framebuffer complete. It just | |
| 112 // means it passed our tests. | |
| 113 GLenum IsPossiblyComplete() const; | |
| 114 | |
| 115 // Implements optimized glGetFramebufferStatus. | |
| 116 GLenum GetStatus(TextureManager* texture_manager, GLenum target) const; | |
| 117 | |
| 118 // Check all attachments are cleared | |
| 119 bool IsCleared() const; | |
| 120 | |
| 121 static void ClearFramebufferCompleteComboMap(); | |
| 122 | |
| 123 private: | |
| 124 friend class FramebufferManager; | |
| 125 friend class base::RefCounted<FramebufferInfo>; | |
| 126 | |
| 127 ~FramebufferInfo(); | |
| 128 | |
| 129 void MarkAsDeleted(); | |
| 130 | |
| 131 void MarkAttachmentsAsCleared( | |
| 132 RenderbufferManager* renderbuffer_manager, | |
| 133 TextureManager* texture_manager, | |
| 134 bool cleared); | |
| 135 | |
| 136 void MarkAsComplete(unsigned state_id) { | |
| 137 framebuffer_complete_state_count_id_ = state_id; | |
| 138 } | |
| 139 | |
| 140 unsigned framebuffer_complete_state_count_id() const { | |
| 141 return framebuffer_complete_state_count_id_; | |
| 142 } | |
| 143 | |
| 144 // The managers that owns this. | |
| 145 FramebufferManager* manager_; | |
| 146 | |
| 147 bool deleted_; | |
| 148 | |
| 149 // Service side framebuffer id. | |
| 150 GLuint service_id_; | |
| 151 | |
| 152 // Whether this framebuffer has ever been bound. | |
| 153 bool has_been_bound_; | |
| 154 | |
| 155 // state count when this framebuffer was last checked for completeness. | |
| 156 unsigned framebuffer_complete_state_count_id_; | |
| 157 | |
| 158 // A map of attachments. | |
| 159 typedef base::hash_map<GLenum, Attachment::Ref> AttachmentMap; | |
| 160 AttachmentMap attachments_; | |
| 161 | |
| 162 // A map of successful frame buffer combos. If it's in the map | |
| 163 // it should be FRAMEBUFFER_COMPLETE. | |
| 164 typedef base::hash_map<std::string, bool> FramebufferComboCompleteMap; | |
| 165 static FramebufferComboCompleteMap* framebuffer_combo_complete_map_; | |
| 166 | |
| 167 DISALLOW_COPY_AND_ASSIGN(FramebufferInfo); | |
| 168 }; | |
| 169 | |
| 170 FramebufferManager(); | 168 FramebufferManager(); |
| 171 ~FramebufferManager(); | 169 ~FramebufferManager(); |
| 172 | 170 |
| 173 // Must call before destruction. | 171 // Must call before destruction. |
| 174 void Destroy(bool have_context); | 172 void Destroy(bool have_context); |
| 175 | 173 |
| 176 // Creates a FramebufferInfo for the given framebuffer. | 174 // Creates a Framebuffer for the given framebuffer. |
| 177 void CreateFramebufferInfo(GLuint client_id, GLuint service_id); | 175 void CreateFramebuffer(GLuint client_id, GLuint service_id); |
| 178 | 176 |
| 179 // Gets the framebuffer info for the given framebuffer. | 177 // Gets the framebuffer info for the given framebuffer. |
| 180 FramebufferInfo* GetFramebufferInfo(GLuint client_id); | 178 Framebuffer* GetFramebuffer(GLuint client_id); |
| 181 | 179 |
| 182 // Removes a framebuffer info for the given framebuffer. | 180 // Removes a framebuffer info for the given framebuffer. |
| 183 void RemoveFramebufferInfo(GLuint client_id); | 181 void RemoveFramebuffer(GLuint client_id); |
| 184 | 182 |
| 185 // Gets a client id for a given service id. | 183 // Gets a client id for a given service id. |
| 186 bool GetClientId(GLuint service_id, GLuint* client_id) const; | 184 bool GetClientId(GLuint service_id, GLuint* client_id) const; |
| 187 | 185 |
| 188 void MarkAttachmentsAsCleared( | 186 void MarkAttachmentsAsCleared( |
| 189 FramebufferInfo* framebuffer, | 187 Framebuffer* framebuffer, |
| 190 RenderbufferManager* renderbuffer_manager, | 188 RenderbufferManager* renderbuffer_manager, |
| 191 TextureManager* texture_manager); | 189 TextureManager* texture_manager); |
| 192 | 190 |
| 193 void MarkAsComplete(FramebufferInfo* framebuffer); | 191 void MarkAsComplete(Framebuffer* framebuffer); |
| 194 | 192 |
| 195 bool IsComplete(FramebufferInfo* framebuffer); | 193 bool IsComplete(Framebuffer* framebuffer); |
| 196 | 194 |
| 197 void IncFramebufferStateChangeCount() { | 195 void IncFramebufferStateChangeCount() { |
| 198 // make sure this is never 0. | 196 // make sure this is never 0. |
| 199 framebuffer_state_change_count_ = | 197 framebuffer_state_change_count_ = |
| 200 (framebuffer_state_change_count_ + 1) | 0x80000000U; | 198 (framebuffer_state_change_count_ + 1) | 0x80000000U; |
| 201 } | 199 } |
| 202 | 200 |
| 203 private: | 201 private: |
| 204 void StartTracking(FramebufferInfo* info); | 202 friend class Framebuffer; |
| 205 void StopTracking(FramebufferInfo* info); | 203 |
| 204 void StartTracking(Framebuffer* info); |
| 205 void StopTracking(Framebuffer* info); |
| 206 | 206 |
| 207 // Info for each framebuffer in the system. | 207 // Info for each framebuffer in the system. |
| 208 typedef base::hash_map<GLuint, FramebufferInfo::Ref> FramebufferInfoMap; | 208 typedef base::hash_map<GLuint, scoped_refptr<Framebuffer> > |
| 209 FramebufferInfoMap; |
| 209 FramebufferInfoMap framebuffer_infos_; | 210 FramebufferInfoMap framebuffer_infos_; |
| 210 | 211 |
| 211 // Incremented anytime anything changes that might effect framebuffer | 212 // Incremented anytime anything changes that might effect framebuffer |
| 212 // state. | 213 // state. |
| 213 unsigned framebuffer_state_change_count_; | 214 unsigned framebuffer_state_change_count_; |
| 214 | 215 |
| 215 // Counts the number of FramebufferInfo allocated with 'this' as its manager. | 216 // Counts the number of Framebuffer allocated with 'this' as its manager. |
| 216 // Allows to check no FramebufferInfo will outlive this. | 217 // Allows to check no Framebuffer will outlive this. |
| 217 unsigned int framebuffer_info_count_; | 218 unsigned int framebuffer_info_count_; |
| 218 | 219 |
| 219 bool have_context_; | 220 bool have_context_; |
| 220 | 221 |
| 221 DISALLOW_COPY_AND_ASSIGN(FramebufferManager); | 222 DISALLOW_COPY_AND_ASSIGN(FramebufferManager); |
| 222 }; | 223 }; |
| 223 | 224 |
| 224 } // namespace gles2 | 225 } // namespace gles2 |
| 225 } // namespace gpu | 226 } // namespace gpu |
| 226 | 227 |
| 227 #endif // GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_ | 228 #endif // GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_MANAGER_H_ |
| OLD | NEW |