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. 4 5 // This file contains the GLES2Decoder class. 6 7 #ifndef GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_H_ 8 #define GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_H_ 9 10 #include <vector> 11 12 #include "base/callback.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/time/time.h" 15 #include "build/build_config.h" 16 #include "gpu/command_buffer/common/capabilities.h" 17 #include "gpu/command_buffer/service/common_decoder.h" 18 #include "gpu/command_buffer/service/logger.h" 19 #include "ui/gfx/size.h" 20 #include "ui/gl/gl_context.h" 21 22 namespace gfx { 23 class GLContext; 24 class GLSurface; 25 } 26 27 namespace gpu { 28 29 class AsyncPixelTransferDelegate; 30 class AsyncPixelTransferManager; 31 class StreamTextureManager; 32 struct Mailbox; 33 34 namespace gles2 { 35 36 class ContextGroup; 37 class ErrorState; 38 class GLES2Util; 39 class Logger; 40 class QueryManager; 41 class VertexArrayManager; 42 43 struct DisallowedFeatures { 44 DisallowedFeatures() 45 : multisampling(false), 46 gpu_memory_manager(false) { 47 } 48 49 bool multisampling; 50 bool gpu_memory_manager; 51 }; 52 53 typedef base::Callback<void(const std::string& key, 54 const std::string& shader)> ShaderCacheCallback; 55 56 // This class implements the AsyncAPIInterface interface, decoding GLES2 57 // commands and calling GL. 58 class GPU_EXPORT GLES2Decoder : public base::SupportsWeakPtr<GLES2Decoder>, 59 public CommonDecoder { 60 public: 61 typedef error::Error Error; 62 typedef base::Callback<bool(uint32 id)> WaitSyncPointCallback; 63 64 // Creates a decoder. 65 static GLES2Decoder* Create(ContextGroup* group); 66 67 virtual ~GLES2Decoder(); 68 69 bool initialized() const { 70 return initialized_; 71 } 72 73 void set_initialized() { 74 initialized_ = true; 75 } 76 77 bool debug() const { 78 return debug_; 79 } 80 81 // Set to true to call glGetError after every command. 82 void set_debug(bool debug) { 83 debug_ = debug; 84 } 85 86 bool log_commands() const { 87 return log_commands_; 88 } 89 90 // Set to true to LOG every command. 91 void set_log_commands(bool log_commands) { 92 log_commands_ = log_commands; 93 } 94 95 // Initializes the graphics context. Can create an offscreen 96 // decoder with a frame buffer that can be referenced from the parent. 97 // Takes ownership of GLContext. 98 // Parameters: 99 // surface: the GL surface to render to. 100 // context: the GL context to render to. 101 // offscreen: whether to make the context offscreen or not. When FBO 0 is 102 // bound, offscreen contexts render to an internal buffer, onscreen ones 103 // to the surface. 104 // size: the size if the GL context is offscreen. 105 // Returns: 106 // true if successful. 107 virtual bool Initialize(const scoped_refptr<gfx::GLSurface>& surface, 108 const scoped_refptr<gfx::GLContext>& context, 109 bool offscreen, 110 const gfx::Size& size, 111 const DisallowedFeatures& disallowed_features, 112 const std::vector<int32>& attribs) = 0; 113 114 // Destroys the graphics context. 115 virtual void Destroy(bool have_context) = 0; 116 117 // Set the surface associated with the default FBO. 118 virtual void SetSurface(const scoped_refptr<gfx::GLSurface>& surface) = 0; 119 120 virtual void ProduceFrontBuffer(const Mailbox& mailbox) = 0; 121 122 // Resize an offscreen frame buffer. 123 virtual bool ResizeOffscreenFrameBuffer(const gfx::Size& size) = 0; 124 125 // Make this decoder's GL context current. 126 virtual bool MakeCurrent() = 0; 127 128 // Have the decoder release the context. 129 virtual void ReleaseCurrent() = 0; 130 131 // Gets the GLES2 Util which holds info. 132 virtual GLES2Util* GetGLES2Util() = 0; 133 134 // Gets the associated GLContext. 135 virtual gfx::GLContext* GetGLContext() = 0; 136 137 // Gets the associated ContextGroup 138 virtual ContextGroup* GetContextGroup() = 0; 139 140 virtual Capabilities GetCapabilities() = 0; 141 142 // Restores all of the decoder GL state. 143 virtual void RestoreState() const = 0; 144 145 // Restore States. 146 virtual void RestoreActiveTexture() const = 0; 147 virtual void RestoreAllTextureUnitBindings() const = 0; 148 virtual void RestoreAttribute(unsigned index) const = 0; 149 virtual void RestoreBufferBindings() const = 0; 150 virtual void RestoreFramebufferBindings() const = 0; 151 virtual void RestoreGlobalState() const = 0; 152 virtual void RestoreProgramBindings() const = 0; 153 virtual void RestoreRenderbufferBindings() const = 0; 154 virtual void RestoreTextureState(unsigned service_id) const = 0; 155 virtual void RestoreTextureUnitBindings(unsigned unit) const = 0; 156 157 // Gets the QueryManager for this context. 158 virtual QueryManager* GetQueryManager() = 0; 159 160 // Gets the VertexArrayManager for this context. 161 virtual VertexArrayManager* GetVertexArrayManager() = 0; 162 163 // Process any pending queries. Returns false if there are no pending queries. 164 virtual bool ProcessPendingQueries() = 0; 165 166 // Returns false if there are no idle work to be made. 167 virtual bool HasMoreIdleWork() = 0; 168 169 virtual void PerformIdleWork() = 0; 170 171 // Sets a callback which is called when a glResizeCHROMIUM command 172 // is processed. 173 virtual void SetResizeCallback( 174 const base::Callback<void(gfx::Size, float)>& callback) = 0; 175 176 // Interface to performing async pixel transfers. 177 virtual AsyncPixelTransferManager* GetAsyncPixelTransferManager() = 0; 178 virtual void ResetAsyncPixelTransferManagerForTest() = 0; 179 virtual void SetAsyncPixelTransferManagerForTest( 180 AsyncPixelTransferManager* manager) = 0; 181 182 // Get the service texture ID corresponding to a client texture ID. 183 // If no such record is found then return false. 184 virtual bool GetServiceTextureId(uint32 client_texture_id, 185 uint32* service_texture_id); 186 187 // Provides detail about a lost context if one occurred. 188 virtual error::ContextLostReason GetContextLostReason() = 0; 189 190 // Clears a level of a texture 191 // Returns false if a GL error should be generated. 192 virtual bool ClearLevel( 193 unsigned service_id, 194 unsigned bind_target, 195 unsigned target, 196 int level, 197 unsigned format, 198 unsigned type, 199 int width, 200 int height, 201 bool is_texture_immutable) = 0; 202 203 virtual ErrorState* GetErrorState() = 0; 204 205 // A callback for messages from the decoder. 206 virtual void SetShaderCacheCallback(const ShaderCacheCallback& callback) = 0; 207 208 // Sets the callback for waiting on a sync point. The callback returns the 209 // scheduling status (i.e. true if the channel is still scheduled). 210 virtual void SetWaitSyncPointCallback( 211 const WaitSyncPointCallback& callback) = 0; 212 213 virtual void WaitForReadPixels(base::Closure callback) = 0; 214 virtual uint32 GetTextureUploadCount() = 0; 215 virtual base::TimeDelta GetTotalTextureUploadTime() = 0; 216 virtual base::TimeDelta GetTotalProcessingCommandsTime() = 0; 217 virtual void AddProcessingCommandsTime(base::TimeDelta) = 0; 218 219 // Returns true if the context was lost either by GL_ARB_robustness, forced 220 // context loss or command buffer parse error. 221 virtual bool WasContextLost() = 0; 222 223 // Returns true if the context was lost specifically by GL_ARB_robustness. 224 virtual bool WasContextLostByRobustnessExtension() = 0; 225 226 // Lose this context. 227 virtual void LoseContext(uint32 reset_status) = 0; 228 229 virtual Logger* GetLogger() = 0; 230 231 protected: 232 GLES2Decoder(); 233 234 private: 235 bool initialized_; 236 bool debug_; 237 bool log_commands_; 238 239 DISALLOW_COPY_AND_ASSIGN(GLES2Decoder); 240 }; 241 242 } // namespace gles2 243 } // namespace gpu 244 #endif // GPU_COMMAND_BUFFER_SERVICE_GLES2_CMD_DECODER_H_ 245