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