gl_implementation.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #ifndef UI_GL_GL_IMPLEMENTATION_H_
  5. #define UI_GL_GL_IMPLEMENTATION_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/files/file_path.h"
  10. #include "base/native_library.h"
  11. #include "build/build_config.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "ui/gfx/extension_set.h"
  14. #include "ui/gl/buildflags.h"
  15. #include "ui/gl/gl_export.h"
  16. #include "ui/gl/gl_switches.h"
  17. namespace base {
  18. class CommandLine;
  19. }
  20. namespace gl {
  21. class GLApi;
  22. // The GL implementation currently in use.
  23. // These values are persisted to logs. Entries should not be renumbered and
  24. // numeric values should never be reused. It should match enum GLImplementation
  25. // in /tool/metrics/histograms/enums.xml
  26. enum GLImplementation {
  27. kGLImplementationNone = 0,
  28. kGLImplementationDesktopGL = 1,
  29. kGLImplementationDesktopGLCoreProfile = 2,
  30. // Note: 3 and 4 are skipped and should not be reused.
  31. // 3 used to be legacy SwiftShader.
  32. // 4 used to be Apple's software GL.
  33. kGLImplementationEGLGLES2 = 5, // Native EGL/GLES2
  34. kGLImplementationMockGL = 6,
  35. kGLImplementationStubGL = 7,
  36. kGLImplementationDisabled = 8,
  37. kGLImplementationEGLANGLE = 9, // EGL/GL implemented using ANGLE
  38. kMaxValue = kGLImplementationEGLANGLE,
  39. };
  40. enum class ANGLEImplementation {
  41. kNone = 0,
  42. kD3D9 = 1,
  43. kD3D11 = 2,
  44. kOpenGL = 3,
  45. kOpenGLES = 4,
  46. kNull = 5,
  47. kVulkan = 6,
  48. kSwiftShader = 7,
  49. kMetal = 8,
  50. kDefault = 9,
  51. kMaxValue = kDefault,
  52. };
  53. struct GL_EXPORT GLImplementationParts {
  54. constexpr explicit GLImplementationParts(const ANGLEImplementation angle_impl)
  55. : gl(kGLImplementationEGLANGLE),
  56. angle(MakeANGLEImplementation(kGLImplementationEGLANGLE, angle_impl)) {}
  57. constexpr explicit GLImplementationParts(const GLImplementation gl_impl)
  58. : gl(gl_impl),
  59. angle(MakeANGLEImplementation(gl_impl, ANGLEImplementation::kDefault)) {
  60. }
  61. GLImplementation gl = kGLImplementationNone;
  62. ANGLEImplementation angle = ANGLEImplementation::kNone;
  63. constexpr bool operator==(const GLImplementationParts& other) const {
  64. return (gl == other.gl && angle == other.angle);
  65. }
  66. constexpr bool operator==(const ANGLEImplementation angle_impl) const {
  67. return operator==(GLImplementationParts(angle_impl));
  68. }
  69. constexpr bool operator==(const GLImplementation gl_impl) const {
  70. return operator==(GLImplementationParts(gl_impl));
  71. }
  72. bool IsValid() const;
  73. bool IsAllowed(const std::vector<GLImplementationParts>& allowed_impls) const;
  74. std::string ToString() const;
  75. private:
  76. static constexpr ANGLEImplementation MakeANGLEImplementation(
  77. const GLImplementation gl_impl,
  78. const ANGLEImplementation angle_impl) {
  79. if (gl_impl == kGLImplementationEGLANGLE) {
  80. if (angle_impl == ANGLEImplementation::kNone) {
  81. return ANGLEImplementation::kDefault;
  82. } else {
  83. return angle_impl;
  84. }
  85. } else {
  86. return ANGLEImplementation::kNone;
  87. }
  88. }
  89. };
  90. struct GL_EXPORT GLWindowSystemBindingInfo {
  91. GLWindowSystemBindingInfo();
  92. ~GLWindowSystemBindingInfo();
  93. std::string vendor;
  94. std::string version;
  95. std::string extensions;
  96. std::string direct_rendering_version;
  97. };
  98. using GLFunctionPointerType = void (*)();
  99. #if BUILDFLAG(IS_WIN)
  100. typedef GLFunctionPointerType(WINAPI* GLGetProcAddressProc)(const char* name);
  101. #else
  102. typedef GLFunctionPointerType (*GLGetProcAddressProc)(const char* name);
  103. #endif
  104. // Initialize stub methods for drawing operations in the GL bindings. The
  105. // null draw bindings default to enabled, so that draw operations do nothing.
  106. GL_EXPORT void InitializeNullDrawGLBindings();
  107. // TODO(danakj): Remove this when all test suites are using null-draw.
  108. GL_EXPORT bool HasInitializedNullDrawGLBindings();
  109. // Filter a list of disabled_extensions from GL style space-separated
  110. // extension_list, returning a space separated list of filtered extensions, in
  111. // the same order as the input.
  112. GL_EXPORT std::string FilterGLExtensionList(
  113. const char* extension_list,
  114. const std::vector<std::string>& disabled_extensions);
  115. // Once initialized, instantiating this turns the stub methods for drawing
  116. // operations off allowing drawing will occur while the object is alive.
  117. class GL_EXPORT DisableNullDrawGLBindings {
  118. public:
  119. DisableNullDrawGLBindings();
  120. ~DisableNullDrawGLBindings();
  121. private:
  122. bool initial_enabled_;
  123. };
  124. // Set the current GL and ANGLE implementation.
  125. GL_EXPORT void SetGLImplementationParts(
  126. const GLImplementationParts& implementation);
  127. // Get the current GL and ANGLE implementation.
  128. GL_EXPORT const GLImplementationParts& GetGLImplementationParts();
  129. // Set the current GL implementation.
  130. GL_EXPORT void SetGLImplementation(GLImplementation implementation);
  131. // Get the current GL implementation.
  132. GL_EXPORT GLImplementation GetGLImplementation();
  133. // Set the current ANGLE implementation.
  134. GL_EXPORT void SetANGLEImplementation(ANGLEImplementation implementation);
  135. // Get the current ANGLE implementation.
  136. GL_EXPORT ANGLEImplementation GetANGLEImplementation();
  137. // Get the software GL implementation
  138. GL_EXPORT GLImplementationParts GetSoftwareGLImplementation();
  139. // Set the software GL implementation on the provided command line
  140. GL_EXPORT void SetSoftwareGLCommandLineSwitches(
  141. base::CommandLine* command_line);
  142. // Set the software WebGL implementation on the provided command line
  143. GL_EXPORT void SetSoftwareWebGLCommandLineSwitches(
  144. base::CommandLine* command_line);
  145. // Return requested GL implementation by checking commandline. If there isn't
  146. // gl related argument, nullopt is returned.
  147. GL_EXPORT absl::optional<GLImplementationParts>
  148. GetRequestedGLImplementationFromCommandLine(
  149. const base::CommandLine* command_line,
  150. bool* fallback_to_software_gl);
  151. // Whether the implementation is one of the software GL implementations
  152. GL_EXPORT bool IsSoftwareGLImplementation(GLImplementationParts implementation);
  153. // Does the underlying GL support all features from Desktop GL 2.0 that were
  154. // removed from the ES 2.0 spec without requiring specific extension strings.
  155. GL_EXPORT bool HasDesktopGLFeatures();
  156. // Get the GL implementation with a given name.
  157. GL_EXPORT GLImplementationParts
  158. GetNamedGLImplementation(const std::string& gl_name,
  159. const std::string& angle_name);
  160. // Get the name of a GL implementation.
  161. GL_EXPORT const char* GetGLImplementationGLName(
  162. GLImplementationParts implementation);
  163. GL_EXPORT const char* GetGLImplementationANGLEName(
  164. GLImplementationParts implementation);
  165. // Add a native library to those searched for GL entry points.
  166. GL_EXPORT void AddGLNativeLibrary(base::NativeLibrary library);
  167. // Unloads all native libraries.
  168. GL_EXPORT void UnloadGLNativeLibraries(bool due_to_fallback);
  169. // Set an additional function that will be called to find GL entry points.
  170. // Exported so that tests may set the function used in the mock implementation.
  171. GL_EXPORT void SetGLGetProcAddressProc(GLGetProcAddressProc proc);
  172. // Find an entry point in the current GL implementation. Note that the function
  173. // may return a non-null pointer to something else than the GL function if an
  174. // unsupported function is queried. Spec-compliant eglGetProcAddress and
  175. // glxGetProcAddress are allowed to return garbage for unsupported functions,
  176. // and when querying functions from the EGL library supplied by Android, it may
  177. // return a function that prints a log message about the function being
  178. // unsupported.
  179. GL_EXPORT GLFunctionPointerType GetGLProcAddress(const char* name);
  180. // Helper for fetching the OpenGL extensions from the current context.
  181. // This helper abstracts over differences between the desktop OpenGL
  182. // core profile, and OpenGL ES and the compatibility profile. It's
  183. // intended for users of the bindings, not the implementation of the
  184. // bindings themselves. This is a relatively expensive call, so
  185. // callers should cache the result.
  186. GL_EXPORT std::string GetGLExtensionsFromCurrentContext();
  187. GL_EXPORT std::string GetGLExtensionsFromCurrentContext(GLApi* api);
  188. GL_EXPORT gfx::ExtensionSet GetRequestableGLExtensionsFromCurrentContext();
  189. GL_EXPORT gfx::ExtensionSet GetRequestableGLExtensionsFromCurrentContext(
  190. GLApi* api);
  191. // Helper for the GL bindings implementation to understand whether
  192. // glGetString(GL_EXTENSIONS) or glGetStringi(GL_EXTENSIONS, i) will
  193. // be used in the function above.
  194. GL_EXPORT bool WillUseGLGetStringForExtensions();
  195. GL_EXPORT bool WillUseGLGetStringForExtensions(GLApi* api);
  196. // Helpers to load a library and log error on failure.
  197. GL_EXPORT base::NativeLibrary LoadLibraryAndPrintError(
  198. const base::FilePath::CharType* filename);
  199. GL_EXPORT base::NativeLibrary LoadLibraryAndPrintError(
  200. const base::FilePath& filename);
  201. #if BUILDFLAG(USE_OPENGL_APITRACE)
  202. // Notify end of frame at buffer swap request.
  203. GL_EXPORT void TerminateFrame();
  204. #endif
  205. } // namespace gl
  206. #endif // UI_GL_GL_IMPLEMENTATION_H_