Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
graphicsmanager.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFX_GRAPHICSMANAGER_H_
19 #define ION_GFX_GRAPHICSMANAGER_H_
20 
21 #include <bitset>
22 #include <cstring>
23 #include <iostream> // NOLINT
24 #include <memory>
25 #include <sstream>
26 #include <string>
27 
28 #include "ion/base/logging.h"
29 #include "ion/base/referent.h"
35 #include "ion/math/range.h"
37 
38 namespace ion {
39 namespace gfx {
40 
54 class ION_API GraphicsManager : public base::Referent {
55  public:
57  struct ShaderPrecision {
58  ShaderPrecision(const math::Range1i& range_in, const int precision_in)
59  : range(range_in), precision(precision_in) {}
61  bool IsValid() const {
62  return (range != math::Range1i(0, 0)) || precision != 0;
63  }
64  inline bool operator==(const ShaderPrecision& other) const {
65  return range == other.range && precision == other.precision;
66  }
68  int precision;
69  };
70 
76  enum Capability {
77  kAliasedLineWidthRange, // math::Range1f
78  kAliasedPointSizeRange, // math::Range1f
79  kCompressedTextureFormats, // std::vector<int>
85  kMaxDrawBuffers, // int
95  kMaxTransformFeedbackSeparateAttribs, // int
102  kMaxViewportDims, // math::Range1i
103  kShaderBinaryFormats, // std::vector<int>
106  kMaxDebugMessageLength, // int
118  kVertexShaderLowIntPrecisionFormat,
122  };
123 
125  kCore, // Core OpenGL ES2 functions.
147  kRaw,
158  };
159 
161  enum GlApi {
164  kWeb,
165 
166  kNumApis
167  };
168 
170  enum GlProfile {
173  };
174 
175  private:
176  class FunctionGroup;
177 
180  class WrapperBase {
181  public:
182  WrapperBase(const char* func_name, FunctionGroupId group)
183  : ptr_(NULL),
184  func_name_(func_name),
185  group_(group) {
187  GraphicsManager::AddWrapper(this);
188  }
189  const char* GetFuncName() const { return func_name_; }
190  bool Init(GraphicsManager* gm) {
191  const std::string gl_name = "gl" + std::string(func_name_);
192  ptr_ = gm->Lookup(gl_name.c_str(), group_ == kCore);
194  gm->AddFunctionToGroup(group_, func_name_, ptr_);
195  return ptr_ != NULL;
196  }
197  void Reset() { ptr_ = NULL; }
198 
199  protected:
200  void* ptr_;
201 
202  private:
203  const char* func_name_;
204  FunctionGroupId group_;
205  };
206 
207  public:
208  GraphicsManager();
209 
217  template <typename T> const T GetCapabilityValue(Capability cap);
218 
221  bool IsFunctionAvailable(const std::string& function_name) const {
222  return wrapped_function_names_.count(function_name) > 0;
223  }
224 
226  bool IsFunctionGroupAvailable(FunctionGroupId group) const;
227 
229  void EnableFunctionGroup(FunctionGroupId group, bool enable);
230 
233  bool IsExtensionSupported(const std::string& name) const;
234 
237  void EnableErrorChecking(bool enable) { is_error_checking_enabled_ = enable; }
238  bool IsErrorCheckingEnabled() const { return is_error_checking_enabled_; }
239 
243  void SetTracingStream(std::ostream* s) { tracing_ostream_ = s; }
244  std::ostream* GetTracingStream() const { return tracing_ostream_; }
245 
247  void SetTracingPrefix(const std::string& s) { tracing_prefix_ = s; }
248  const std::string& GetTracingPrefix() const { return tracing_prefix_; }
249 
251  GLuint GetGlVersion() const { return gl_version_; }
253  const std::string& GetGlVersionString() const { return gl_version_string_; }
255  const std::string& GetGlRenderer() const { return gl_renderer_; }
257  GlApi GetGlApiStandard() const { return gl_api_standard_; }
259  GlProfile GetGlProfileType() const { return gl_profile_type_; }
260 
264  return valid_statetable_caps_.test(cap);
265  }
266 
268 
269  public:
271  static const char* ErrorString(GLenum error_code);
272 
273  protected:
280  struct GlVersions {
281  GlVersions(GLuint desktop, GLuint es, GLuint web) {
282  versions[kDesktop] = desktop;
283  versions[kEs] = es;
284  versions[kWeb] = web;
285  }
286  GLuint operator[](GlApi i) const { return versions[i]; }
287  GLuint versions[kNumApis];
288  };
289 
293 
296  ~GraphicsManager() override;
297 
300  void ReinitFunctions();
301 
303  void InitGlInfo();
304 
315  virtual void EnableFunctionGroupIfAvailable(
316  FunctionGroupId group, const GlVersions& versions,
317  const std::string& extensions, const std::string& disabled_renderers);
319  private:
324 
326  class WrapperVecHolder;
327 
329  class CapabilityHelper;
337  class ErrorChecker {
338  public:
339  ErrorChecker(GraphicsManager* gm, const std::string& func_call)
340  : graphics_manager_(gm),
341  func_call_(func_call) {
342  graphics_manager_->CheckForErrors("before", func_call_);
343  }
344  ~ErrorChecker() {
345  graphics_manager_->CheckForErrors("after", func_call_);
346  }
347  private:
348  GraphicsManager* graphics_manager_;
349  const std::string func_call_;
350  };
354  static WrapperVecHolder* GetWrapperVecHolder();
358  static void AddWrapper(WrapperBase* wrapper);
361  void AddFunctionToGroup(
362  FunctionGroupId group, const char* func_name, void* function);
363 
366  void Init(bool init_functions_from_gl);
367 
370  void InitFunctions();
371 
373  void AddWrappedFunctionName(const std::string& function_name) {
374  wrapped_function_names_.insert(function_name);
375  }
378  void CheckForErrors(const char* when, const std::string& func_call);
379 
383  virtual void* Lookup(const char* name, bool is_core);
387  WrapperVec wrappers_;
390  FunctionGroupVector function_groups_;
391 
393  std::unique_ptr<CapabilityHelper> capability_helper_;
397  base::AllocSet<std::string> wrapped_function_names_;
398 
400  std::string extensions_;
401 
403  bool is_error_checking_enabled_;
406  std::ostream* tracing_ostream_;
407 
409  std::string tracing_prefix_;
410 
412  TracingHelper tracing_helper_;
413 
415  std::string gl_renderer_;
416 
418  std::string gl_version_string_;
421  GLuint gl_version_;
422 
424  GlApi gl_api_standard_;
425 
427  GlProfile gl_profile_type_;
431  std::bitset<StateTable::kNumCapabilities> valid_statetable_caps_;
432 };
433 
437 } // namespace gfx
438 } // namespace ion
439 
441 
442 #endif // ION_GFX_GRAPHICSMANAGER_H_
GraphicsManager manages the graphics library for an application.
See https://www.opengl.org/registry/specs/EXT/gpu_shader4.txt.
This is used for Apple devices running pre-es-3.0 device with the APPLE_framebuffer_multisample exten...
bool IsErrorCheckingEnabled() const
Range< 1, int32 > Range1i
Definition: range.h:363
const std::string & GetGlRenderer() const
Returns the GL renderer string. Thread-safe.
Simple wrapper for a set of GL versions.
bool operator==(const ShaderPrecision &other) const
void SetTracingStream(std::ostream *s)
Sets an output stream to use for tracing OpenGL calls.
void EnableErrorChecking(bool enable)
Sets/returns a flag indicating whether glGetError() should be called after every OpenGL call to check...
GlApi GetGlApiStandard() const
Returns the GL API standard. Thread-safe.
GlApi
OpenGL platform SDK standard.
bool IsValid() const
Returns true if the requested precision is unsupported.
Capability
Enumerated types for StateTable items.
Definition: statetable.h:55
Information about shader precision, see below.
std::ostream * GetTracingStream() const
Thread-safe abstract base class.
Definition: referent.h:49
const std::string & GetTracingPrefix() const
The below are returned as a ShaderPrecision struct.
void SetTracingPrefix(const std::string &s)
Sets a prefix to be printed when tracing OpenGL calls.
ION_API bool IsExtensionSupported(const std::string &unprefixed_extension, const std::string &extensions_string)
Returns whether the currently bound OpenGL implementation supports the named extension.
std::string name
Definition: printer.cc:324
This internal class is used by the GraphicsManager to print argument values when tracing OpenGL calls...
Definition: tracinghelper.h:29
This covers both OES_EGL_image and OES_EGL_image_external.
Copyright 2016 Google Inc.
base::ReferentPtr< GraphicsManager >::Type GraphicsManagerPtr
Convenience typedef for shared pointer to a GraphicsManager.
const std::string & GetGlVersionString() const
Returns the GL version string. Thread-safe.
GlProfile
OpenGL profile type.
GLuint GetGlVersion() const
Returns the GL version as an integer. Thread-safe.
bool IsFunctionAvailable(const std::string &function_name) const
Returns true if the named function is available.
int precision
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
bool IsValidStateTableCapability(const StateTable::Capability cap) const
Returns whether the given state table capability is valid given the capabilities of the local GL plat...
ShaderPrecision(const math::Range1i &range_in, const int precision_in)