gl_display.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) 2022 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_DISPLAY_H_
  5. #define UI_GL_GL_DISPLAY_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "ui/gl/gl_export.h"
  10. #if defined(USE_EGL)
  11. #include <EGL/egl.h>
  12. #include "ui/gl/gpu_switching_manager.h"
  13. #endif // defined(USE_EGL)
  14. namespace base {
  15. class CommandLine;
  16. } // namespace base
  17. namespace gl {
  18. struct DisplayExtensionsEGL;
  19. template <typename GLDisplayPlatform>
  20. class GLDisplayManager;
  21. class EGLDisplayPlatform {
  22. public:
  23. constexpr EGLDisplayPlatform()
  24. : display_(EGL_DEFAULT_DISPLAY), platform_(0), valid_(false) {}
  25. explicit constexpr EGLDisplayPlatform(EGLNativeDisplayType display,
  26. int platform = 0)
  27. : display_(display), platform_(platform), valid_(true) {}
  28. bool Valid() const { return valid_; }
  29. int GetPlatform() const { return platform_; }
  30. EGLNativeDisplayType GetDisplay() const { return display_; }
  31. private:
  32. EGLNativeDisplayType display_;
  33. // 0 for default, or EGL_PLATFORM_* enum.
  34. int platform_;
  35. bool valid_;
  36. };
  37. // If adding a new type, also add it to EGLDisplayType in
  38. // tools/metrics/histograms/enums.xml. Don't remove or reorder entries.
  39. enum DisplayType {
  40. DEFAULT = 0,
  41. SWIFT_SHADER = 1,
  42. ANGLE_WARP = 2,
  43. ANGLE_D3D9 = 3,
  44. ANGLE_D3D11 = 4,
  45. ANGLE_OPENGL = 5,
  46. ANGLE_OPENGLES = 6,
  47. ANGLE_NULL = 7,
  48. ANGLE_D3D11_NULL = 8,
  49. ANGLE_OPENGL_NULL = 9,
  50. ANGLE_OPENGLES_NULL = 10,
  51. ANGLE_VULKAN = 11,
  52. ANGLE_VULKAN_NULL = 12,
  53. ANGLE_D3D11on12 = 13,
  54. ANGLE_SWIFTSHADER = 14,
  55. ANGLE_OPENGL_EGL = 15,
  56. ANGLE_OPENGLES_EGL = 16,
  57. ANGLE_METAL = 17,
  58. ANGLE_METAL_NULL = 18,
  59. DISPLAY_TYPE_MAX = 19,
  60. };
  61. enum DisplayPlatform {
  62. NONE = 0,
  63. EGL = 1,
  64. X11 = 2,
  65. };
  66. GL_EXPORT void GetEGLInitDisplaysForTesting(
  67. bool supports_angle_d3d,
  68. bool supports_angle_opengl,
  69. bool supports_angle_null,
  70. bool supports_angle_vulkan,
  71. bool supports_angle_swiftshader,
  72. bool supports_angle_egl,
  73. bool supports_angle_metal,
  74. const base::CommandLine* command_line,
  75. std::vector<DisplayType>* init_displays);
  76. class GL_EXPORT GLDisplay {
  77. public:
  78. GLDisplay(const GLDisplay&) = delete;
  79. GLDisplay& operator=(const GLDisplay&) = delete;
  80. uint64_t system_device_id() const { return system_device_id_; }
  81. virtual ~GLDisplay();
  82. virtual void* GetDisplay() = 0;
  83. virtual void Shutdown() = 0;
  84. virtual bool IsInitialized() = 0;
  85. template <typename GLDisplayPlatform>
  86. GLDisplayPlatform* GetAs();
  87. protected:
  88. GLDisplay(uint64_t system_device_id, DisplayPlatform type);
  89. uint64_t system_device_id_ = 0;
  90. DisplayPlatform type_ = NONE;
  91. };
  92. #if defined(USE_EGL)
  93. class GL_EXPORT GLDisplayEGL : public GLDisplay {
  94. public:
  95. GLDisplayEGL(const GLDisplayEGL&) = delete;
  96. GLDisplayEGL& operator=(const GLDisplayEGL&) = delete;
  97. ~GLDisplayEGL() override;
  98. static GLDisplayEGL* GetDisplayForCurrentContext();
  99. EGLDisplay GetDisplay() override;
  100. void Shutdown() override;
  101. bool IsInitialized() override;
  102. void SetDisplay(EGLDisplay display);
  103. EGLDisplayPlatform GetNativeDisplay() const;
  104. DisplayType GetDisplayType() const;
  105. bool IsEGLSurfacelessContextSupported();
  106. bool IsEGLContextPrioritySupported();
  107. bool IsAndroidNativeFenceSyncSupported();
  108. bool IsANGLEExternalContextAndSurfaceSupported();
  109. bool Initialize(EGLDisplayPlatform native_display);
  110. void InitializeForTesting();
  111. bool InitializeExtensionSettings();
  112. std::unique_ptr<DisplayExtensionsEGL> ext;
  113. private:
  114. friend class GLDisplayManager<GLDisplayEGL>;
  115. friend class EGLApiTest;
  116. class EGLGpuSwitchingObserver final : public ui::GpuSwitchingObserver {
  117. public:
  118. explicit EGLGpuSwitchingObserver(EGLDisplay display);
  119. ~EGLGpuSwitchingObserver() override = default;
  120. void OnGpuSwitched(GpuPreference active_gpu_heuristic) override;
  121. private:
  122. EGLDisplay display_ = EGL_NO_DISPLAY;
  123. };
  124. explicit GLDisplayEGL(uint64_t system_device_id);
  125. bool InitializeDisplay(EGLDisplayPlatform native_display);
  126. void InitializeCommon();
  127. EGLDisplay display_ = EGL_NO_DISPLAY;
  128. EGLDisplayPlatform native_display_ = EGLDisplayPlatform(EGL_DEFAULT_DISPLAY);
  129. DisplayType display_type_ = DisplayType::DEFAULT;
  130. bool egl_surfaceless_context_supported_ = false;
  131. bool egl_context_priority_supported_ = false;
  132. bool egl_android_native_fence_sync_supported_ = false;
  133. std::unique_ptr<EGLGpuSwitchingObserver> gpu_switching_observer_;
  134. };
  135. #endif // defined(USE_EGL)
  136. #if defined(USE_GLX)
  137. class GL_EXPORT GLDisplayX11 : public GLDisplay {
  138. public:
  139. GLDisplayX11(const GLDisplayX11&) = delete;
  140. GLDisplayX11& operator=(const GLDisplayX11&) = delete;
  141. ~GLDisplayX11() override;
  142. void* GetDisplay() override;
  143. void Shutdown() override;
  144. bool IsInitialized() override;
  145. private:
  146. friend class GLDisplayManager<GLDisplayX11>;
  147. explicit GLDisplayX11(uint64_t system_device_id);
  148. };
  149. #endif // defined(USE_GLX)
  150. } // namespace gl
  151. #endif // UI_GL_GL_DISPLAY_H_