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