Flutter Impeller
proc_table_gles.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 #ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PROC_TABLE_GLES_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PROC_TABLE_GLES_H_
7 
8 #include <functional>
9 #include <string>
10 
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/mapping.h"
16 
17 namespace impeller {
18 
19 const char* GLErrorToString(GLenum value);
20 bool GLErrorIsFatal(GLenum value);
21 
23  const PFNGLGETERRORPROC error_fn;
24 
25  // TODO(matanlurey) Change to string_view.
26  // https://github.com/flutter/flutter/issues/135922
27  const char* name;
28 
29  AutoErrorCheck(PFNGLGETERRORPROC error, const char* name)
30  : error_fn(error), name(name) {}
31 
33  if (error_fn) {
34  auto error = error_fn();
35  if (error == GL_NO_ERROR) {
36  return;
37  }
38  if (GLErrorIsFatal(error)) {
39  FML_LOG(FATAL) << "Fatal GL Error " << GLErrorToString(error) << "("
40  << error << ")"
41  << " encountered on call to " << name;
42  } else {
43  FML_LOG(ERROR) << "GL Error " << GLErrorToString(error) << "(" << error
44  << ")"
45  << " encountered on call to " << name;
46  }
47  }
48  }
49 };
50 
51 template <class T>
52 struct GLProc {
53  using GLFunctionType = T;
54 
55  // TODO(matanlurey) Change to string_view.
56  // https://github.com/flutter/flutter/issues/135922
57 
58  //----------------------------------------------------------------------------
59  /// The name of the GL function.
60  ///
61  const char* name = nullptr;
62 
63  //----------------------------------------------------------------------------
64  /// The pointer to the GL function.
65  ///
66  GLFunctionType* function = nullptr;
67 
68  //----------------------------------------------------------------------------
69  /// An optional error function. If present, all calls will be followed by an
70  /// error check.
71  ///
72  PFNGLGETERRORPROC error_fn = nullptr;
73 
74  //----------------------------------------------------------------------------
75  /// @brief Call the GL function with the appropriate parameters. Lookup
76  /// the documentation for the GL function being called to
77  /// understand the arguments and return types. The arguments
78  /// types must match and will be type checked.
79  ///
80  template <class... Args>
81  auto operator()(Args&&... args) const {
82 #ifdef IMPELLER_DEBUG
84  // We check for the existence of extensions, and reset the function pointer
85  // but it's still called unconditionally below, and will segfault. This
86  // validation log will at least give us a hint as to what's going on.
87  FML_CHECK(IsAvailable()) << "GL function " << name << " is not available. "
88  << "This is likely due to a missing extension.";
89 #endif // IMPELLER_DEBUG
90 #ifdef IMPELLER_TRACE_ALL_GL_CALLS
91  TRACE_EVENT0("impeller", name);
92 #endif // IMPELLER_TRACE_ALL_GL_CALLS
93  return function(std::forward<Args>(args)...);
94  }
95 
96  constexpr bool IsAvailable() const { return function != nullptr; }
97 
98  void Reset() {
99  function = nullptr;
100  error_fn = nullptr;
101  }
102 };
103 
104 #define FOR_EACH_IMPELLER_PROC(PROC) \
105  PROC(ActiveTexture); \
106  PROC(AttachShader); \
107  PROC(BindAttribLocation); \
108  PROC(BindBuffer); \
109  PROC(BindFramebuffer); \
110  PROC(BindRenderbuffer); \
111  PROC(BindTexture); \
112  PROC(BlendEquationSeparate); \
113  PROC(BlendFuncSeparate); \
114  PROC(BufferData); \
115  PROC(CheckFramebufferStatus); \
116  PROC(Clear); \
117  PROC(ClearColor); \
118  PROC(ClearDepthf); \
119  PROC(ClearStencil); \
120  PROC(ColorMask); \
121  PROC(CompileShader); \
122  PROC(CreateProgram); \
123  PROC(CreateShader); \
124  PROC(CullFace); \
125  PROC(DeleteBuffers); \
126  PROC(DeleteFramebuffers); \
127  PROC(DeleteProgram); \
128  PROC(DeleteRenderbuffers); \
129  PROC(DeleteShader); \
130  PROC(DeleteTextures); \
131  PROC(DepthFunc); \
132  PROC(DepthMask); \
133  PROC(DepthRangef); \
134  PROC(DetachShader); \
135  PROC(Disable); \
136  PROC(DisableVertexAttribArray); \
137  PROC(DrawArrays); \
138  PROC(DrawElements); \
139  PROC(Enable); \
140  PROC(EnableVertexAttribArray); \
141  PROC(Flush); \
142  PROC(FramebufferRenderbuffer); \
143  PROC(FramebufferTexture2D); \
144  PROC(FrontFace); \
145  PROC(GenBuffers); \
146  PROC(GenerateMipmap); \
147  PROC(GenFramebuffers); \
148  PROC(GenRenderbuffers); \
149  PROC(GenTextures); \
150  PROC(GetActiveUniform); \
151  PROC(GetBooleanv); \
152  PROC(GetFloatv); \
153  PROC(GetFramebufferAttachmentParameteriv); \
154  PROC(GetIntegerv); \
155  PROC(GetProgramInfoLog); \
156  PROC(GetProgramiv); \
157  PROC(GetShaderInfoLog); \
158  PROC(GetShaderiv); \
159  PROC(GetString); \
160  PROC(GetStringi); \
161  PROC(GetUniformLocation); \
162  PROC(IsBuffer); \
163  PROC(IsFramebuffer); \
164  PROC(IsProgram); \
165  PROC(IsRenderbuffer); \
166  PROC(IsShader); \
167  PROC(IsTexture); \
168  PROC(LinkProgram); \
169  PROC(RenderbufferStorage); \
170  PROC(Scissor); \
171  PROC(ShaderBinary); \
172  PROC(ShaderSource); \
173  PROC(StencilFuncSeparate); \
174  PROC(StencilMaskSeparate); \
175  PROC(StencilOpSeparate); \
176  PROC(TexImage2D); \
177  PROC(TexParameteri); \
178  PROC(TexParameterfv); \
179  PROC(Uniform1fv); \
180  PROC(Uniform1i); \
181  PROC(Uniform2fv); \
182  PROC(Uniform3fv); \
183  PROC(Uniform4fv); \
184  PROC(UniformMatrix4fv); \
185  PROC(UseProgram); \
186  PROC(VertexAttribPointer); \
187  PROC(Viewport); \
188  PROC(GetShaderSource); \
189  PROC(ReadPixels);
190 
191 #define FOR_EACH_IMPELLER_GLES3_PROC(PROC) PROC(BlitFramebuffer);
192 
193 #define FOR_EACH_IMPELLER_EXT_PROC(PROC) \
194  PROC(DebugMessageControlKHR); \
195  PROC(DiscardFramebufferEXT); \
196  PROC(FramebufferTexture2DMultisampleEXT); \
197  PROC(PushDebugGroupKHR); \
198  PROC(PopDebugGroupKHR); \
199  PROC(ObjectLabelKHR); \
200  PROC(RenderbufferStorageMultisampleEXT); \
201  PROC(GenQueriesEXT); \
202  PROC(DeleteQueriesEXT); \
203  PROC(GetQueryObjectui64vEXT); \
204  PROC(BeginQueryEXT); \
205  PROC(EndQueryEXT); \
206  PROC(GetQueryObjectuivEXT);
207 
208 enum class DebugResourceType {
209  kTexture,
210  kBuffer,
211  kProgram,
212  kShader,
214  kFrameBuffer,
215 };
216 
218  public:
219  using Resolver = std::function<void*(const char* function_name)>;
220  explicit ProcTableGLES(Resolver resolver);
221  ProcTableGLES(ProcTableGLES&& other) = default;
222 
223  ~ProcTableGLES();
224 
225 #define IMPELLER_PROC(name) \
226  GLProc<decltype(gl##name)> name = {"gl" #name, nullptr};
227 
231 
232 #undef IMPELLER_PROC
233 
234  bool IsValid() const;
235 
236  /// @brief Set the source for the attached [shader].
237  ///
238  /// Optionally, [defines] may contain a string value that will be
239  /// append to the shader source after the version marker. This can be used to
240  /// support static specialization. For example, setting "#define Foo 1".
241  void ShaderSourceMapping(GLuint shader,
242  const fml::Mapping& mapping,
243  const std::vector<Scalar>& defines = {}) const;
244 
245  const DescriptionGLES* GetDescription() const;
246 
247  const std::shared_ptr<const CapabilitiesGLES>& GetCapabilities() const;
248 
249  std::string DescribeCurrentFramebuffer() const;
250 
251  std::string GetProgramInfoLogString(GLuint program) const;
252 
253  bool IsCurrentFramebufferComplete() const;
254 
256  GLint name,
257  const std::string& label) const;
258 
259  void PushDebugGroup(const std::string& string) const;
260 
261  void PopDebugGroup() const;
262 
263  // Visible For testing.
264  std::optional<std::string> ComputeShaderWithDefines(
265  const fml::Mapping& mapping,
266  const std::vector<Scalar>& defines) const;
267 
268  private:
269  bool is_valid_ = false;
270  std::unique_ptr<DescriptionGLES> description_;
271  std::shared_ptr<const CapabilitiesGLES> capabilities_;
272  GLint debug_label_max_length_ = 0;
273 
274  ProcTableGLES(const ProcTableGLES&) = delete;
275 
276  ProcTableGLES& operator=(const ProcTableGLES&) = delete;
277 };
278 
279 } // namespace impeller
280 
281 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_PROC_TABLE_GLES_H_
impeller::ProcTableGLES::ShaderSourceMapping
void ShaderSourceMapping(GLuint shader, const fml::Mapping &mapping, const std::vector< Scalar > &defines={}) const
Set the source for the attached [shader].
Definition: proc_table_gles.cc:142
impeller::ProcTableGLES::FOR_EACH_IMPELLER_PROC
FOR_EACH_IMPELLER_PROC(IMPELLER_PROC)
impeller::ProcTableGLES::ProcTableGLES
ProcTableGLES(Resolver resolver)
Definition: proc_table_gles.cc:74
IMPELLER_PROC
#define IMPELLER_PROC(name)
Definition: proc_table_gles.h:225
impeller::ProcTableGLES::FOR_EACH_IMPELLER_GLES3_PROC
FOR_EACH_IMPELLER_GLES3_PROC(IMPELLER_PROC)
impeller::ProcTableGLES::PushDebugGroup
void PushDebugGroup(const std::string &string) const
Definition: proc_table_gles.cc:365
impeller::AutoErrorCheck::AutoErrorCheck
AutoErrorCheck(PFNGLGETERRORPROC error, const char *name)
Definition: proc_table_gles.h:29
impeller::AutoErrorCheck::name
const char * name
Definition: proc_table_gles.h:27
impeller::GLProc::IsAvailable
constexpr bool IsAvailable() const
Definition: proc_table_gles.h:96
impeller::DebugResourceType::kBuffer
@ kBuffer
impeller::ProcTableGLES::SetDebugLabel
bool SetDebugLabel(DebugResourceType type, GLint name, const std::string &label) const
Definition: proc_table_gles.cc:339
impeller::ProcTableGLES::IsValid
bool IsValid() const
Definition: proc_table_gles.cc:138
impeller::DebugResourceType::kProgram
@ kProgram
impeller::ProcTableGLES::FOR_EACH_IMPELLER_EXT_PROC
FOR_EACH_IMPELLER_EXT_PROC(IMPELLER_PROC)
impeller::AutoErrorCheck
Definition: proc_table_gles.h:22
impeller::GLProc
Definition: proc_table_gles.h:52
impeller::ProcTableGLES::Resolver
std::function< void *(const char *function_name)> Resolver
Definition: proc_table_gles.h:219
impeller::DebugResourceType::kTexture
@ kTexture
impeller::ProcTableGLES::GetCapabilities
const std::shared_ptr< const CapabilitiesGLES > & GetCapabilities() const
Definition: proc_table_gles.cc:194
impeller::ProcTableGLES
Definition: proc_table_gles.h:217
impeller::ProcTableGLES::ComputeShaderWithDefines
std::optional< std::string > ComputeShaderWithDefines(const fml::Mapping &mapping, const std::vector< Scalar > &defines) const
Definition: proc_table_gles.cc:166
impeller::GLProc::error_fn
PFNGLGETERRORPROC error_fn
Definition: proc_table_gles.h:72
impeller::ProcTableGLES::GetProgramInfoLogString
std::string GetProgramInfoLogString(GLuint program) const
Definition: proc_table_gles.cc:392
impeller::DebugResourceType::kShader
@ kShader
impeller::DebugResourceType::kRenderBuffer
@ kRenderBuffer
gles.h
description_gles.h
impeller::ProcTableGLES::DescribeCurrentFramebuffer
std::string DescribeCurrentFramebuffer() const
Definition: proc_table_gles.cc:259
impeller::GLProc::GLFunctionType
T GLFunctionType
Definition: proc_table_gles.h:53
impeller::DebugResourceType
DebugResourceType
Definition: proc_table_gles.h:208
impeller::GLProc::operator()
auto operator()(Args &&... args) const
Call the GL function with the appropriate parameters. Lookup the documentation for the GL function be...
Definition: proc_table_gles.h:81
impeller::ProcTableGLES::IsCurrentFramebufferComplete
bool IsCurrentFramebufferComplete() const
Definition: proc_table_gles.cc:290
impeller::DebugResourceType::kFrameBuffer
@ kFrameBuffer
impeller::GLErrorToString
const char * GLErrorToString(GLenum value)
Definition: proc_table_gles.cc:18
impeller::AutoErrorCheck::~AutoErrorCheck
~AutoErrorCheck()
Definition: proc_table_gles.h:32
impeller::AutoErrorCheck::error_fn
const PFNGLGETERRORPROC error_fn
Definition: proc_table_gles.h:23
capabilities_gles.h
impeller
Definition: aiks_context.cc:10
impeller::GLProc::name
const char * name
Definition: proc_table_gles.h:61
impeller::ProcTableGLES::PopDebugGroup
void PopDebugGroup() const
Definition: proc_table_gles.cc:382
impeller::GLErrorIsFatal
bool GLErrorIsFatal(GLenum value)
Definition: proc_table_gles.cc:38
impeller::GLProc::Reset
void Reset()
Definition: proc_table_gles.h:98
impeller::ProcTableGLES::GetDescription
const DescriptionGLES * GetDescription() const
Definition: proc_table_gles.cc:190
impeller::ProcTableGLES::~ProcTableGLES
~ProcTableGLES()