gl_surface_egl.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_SURFACE_EGL_H_
  5. #define UI_GL_GL_SURFACE_EGL_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "build/build_config.h"
  8. #if BUILDFLAG(IS_WIN)
  9. #include <windows.h>
  10. #endif
  11. #include <EGL/egl.h>
  12. #include <EGL/eglext.h>
  13. #include <memory>
  14. #include <vector>
  15. #include "base/command_line.h"
  16. #include "base/containers/queue.h"
  17. #include "base/time/time.h"
  18. #include "ui/gfx/geometry/size.h"
  19. #include "ui/gfx/vsync_provider.h"
  20. #include "ui/gl/egl_timestamps.h"
  21. #include "ui/gl/gl_display.h"
  22. #include "ui/gl/gl_export.h"
  23. #include "ui/gl/gl_surface.h"
  24. #include "ui/gl/gl_surface_overlay.h"
  25. namespace gl {
  26. class GLSurfacePresentationHelper;
  27. // Interface for EGL surface.
  28. class GL_EXPORT GLSurfaceEGL : public GLSurface {
  29. public:
  30. explicit GLSurfaceEGL(GLDisplayEGL* display);
  31. GLSurfaceEGL(const GLSurfaceEGL&) = delete;
  32. GLSurfaceEGL& operator=(const GLSurfaceEGL&) = delete;
  33. virtual EGLint GetNativeVisualID() const;
  34. // Implement GLSurface.
  35. GLDisplay* GetGLDisplay() override;
  36. EGLConfig GetConfig() override;
  37. GLSurfaceFormat GetFormat() override;
  38. EGLDisplay GetEGLDisplay();
  39. static GLDisplayEGL* GetGLDisplayEGL();
  40. protected:
  41. ~GLSurfaceEGL() override;
  42. EGLConfig config_ = nullptr;
  43. GLSurfaceFormat format_;
  44. raw_ptr<GLDisplayEGL> display_ = nullptr;
  45. };
  46. // Encapsulates an EGL surface bound to a view.
  47. class GL_EXPORT NativeViewGLSurfaceEGL : public GLSurfaceEGL,
  48. public EGLTimestampClient {
  49. public:
  50. NativeViewGLSurfaceEGL(GLDisplayEGL* display,
  51. EGLNativeWindowType window,
  52. std::unique_ptr<gfx::VSyncProvider> vsync_provider);
  53. NativeViewGLSurfaceEGL(const NativeViewGLSurfaceEGL&) = delete;
  54. NativeViewGLSurfaceEGL& operator=(const NativeViewGLSurfaceEGL&) = delete;
  55. // Implement GLSurface.
  56. bool Initialize(GLSurfaceFormat format) override;
  57. bool SupportsSwapTimestamps() const override;
  58. void SetEnableSwapTimestamps() override;
  59. void Destroy() override;
  60. bool Resize(const gfx::Size& size,
  61. float scale_factor,
  62. const gfx::ColorSpace& color_space,
  63. bool has_alpha) override;
  64. bool Recreate() override;
  65. bool IsOffscreen() override;
  66. gfx::SwapResult SwapBuffers(PresentationCallback callback) override;
  67. gfx::Size GetSize() override;
  68. EGLSurface GetHandle() override;
  69. bool SupportsPostSubBuffer() override;
  70. gfx::SwapResult PostSubBuffer(int x,
  71. int y,
  72. int width,
  73. int height,
  74. PresentationCallback callback) override;
  75. bool SupportsCommitOverlayPlanes() override;
  76. gfx::SwapResult CommitOverlayPlanes(PresentationCallback callback) override;
  77. bool OnMakeCurrent(GLContext* context) override;
  78. gfx::VSyncProvider* GetVSyncProvider() override;
  79. void SetVSyncEnabled(bool enabled) override;
  80. bool ScheduleOverlayPlane(
  81. GLImage* image,
  82. std::unique_ptr<gfx::GpuFence> gpu_fence,
  83. const gfx::OverlayPlaneData& overlay_plane_data) override;
  84. gfx::SurfaceOrigin GetOrigin() const override;
  85. EGLTimestampClient* GetEGLTimestampClient() override;
  86. // EGLTimestampClient implementation.
  87. bool IsEGLTimestampSupported() const override;
  88. bool GetFrameTimestampInfoIfAvailable(base::TimeTicks* presentation_time,
  89. base::TimeDelta* composite_interval,
  90. base::TimeTicks* writes_done_time,
  91. uint32_t* presentation_flags,
  92. int frame_id) override;
  93. // Takes care of the platform dependant bits, of any, for creating the window.
  94. virtual bool InitializeNativeWindow();
  95. protected:
  96. ~NativeViewGLSurfaceEGL() override;
  97. EGLNativeWindowType window_ = 0;
  98. gfx::Size size_ = gfx::Size(1, 1);
  99. bool enable_fixed_size_angle_ = true;
  100. GLSurfacePresentationHelper* presentation_helper() const {
  101. return presentation_helper_.get();
  102. }
  103. gfx::SwapResult SwapBuffersWithDamage(const std::vector<int>& rects,
  104. PresentationCallback callback);
  105. private:
  106. struct SwapInfo {
  107. bool frame_id_is_valid;
  108. EGLuint64KHR frame_id;
  109. };
  110. void UpdateSwapEvents(EGLuint64KHR newFrameId, bool newFrameIdIsValid);
  111. void TraceSwapEvents(EGLuint64KHR oldFrameId);
  112. // Some platforms may provide a custom implementation of vsync provider.
  113. virtual std::unique_ptr<gfx::VSyncProvider> CreateVsyncProviderInternal();
  114. EGLSurface surface_ = nullptr;
  115. bool supports_post_sub_buffer_ = false;
  116. bool supports_swap_buffer_with_damage_ = false;
  117. gfx::SurfaceOrigin surface_origin_ = gfx::SurfaceOrigin::kBottomLeft;
  118. std::unique_ptr<gfx::VSyncProvider> vsync_provider_external_;
  119. std::unique_ptr<gfx::VSyncProvider> vsync_provider_internal_;
  120. // Stored in separate vectors so we can pass the egl timestamps
  121. // directly to the EGL functions.
  122. bool use_egl_timestamps_ = false;
  123. std::vector<EGLint> supported_egl_timestamps_;
  124. std::vector<const char*> supported_event_names_;
  125. // PresentationFeedback support.
  126. int presentation_feedback_index_ = -1;
  127. int composition_start_index_ = -1;
  128. int writes_done_index_ = -1;
  129. uint32_t presentation_flags_ = 0;
  130. base::queue<SwapInfo> swap_info_queue_;
  131. bool vsync_enabled_ = true;
  132. std::unique_ptr<GLSurfacePresentationHelper> presentation_helper_;
  133. };
  134. // Encapsulates a pbuffer EGL surface.
  135. class GL_EXPORT PbufferGLSurfaceEGL : public GLSurfaceEGL {
  136. public:
  137. PbufferGLSurfaceEGL(GLDisplayEGL* display, const gfx::Size& size);
  138. PbufferGLSurfaceEGL(const PbufferGLSurfaceEGL&) = delete;
  139. PbufferGLSurfaceEGL& operator=(const PbufferGLSurfaceEGL&) = delete;
  140. // Implement GLSurface.
  141. bool Initialize(GLSurfaceFormat format) override;
  142. void Destroy() override;
  143. bool IsOffscreen() override;
  144. gfx::SwapResult SwapBuffers(PresentationCallback callback) override;
  145. gfx::Size GetSize() override;
  146. bool Resize(const gfx::Size& size,
  147. float scale_factor,
  148. const gfx::ColorSpace& color_space,
  149. bool has_alpha) override;
  150. EGLSurface GetHandle() override;
  151. void* GetShareHandle() override;
  152. protected:
  153. ~PbufferGLSurfaceEGL() override;
  154. private:
  155. gfx::Size size_;
  156. EGLSurface surface_;
  157. };
  158. // SurfacelessEGL is used as Offscreen surface when platform supports
  159. // KHR_surfaceless_context and GL_OES_surfaceless_context. This would avoid the
  160. // need to create a dummy EGLsurface in case we render to client API targets.
  161. class GL_EXPORT SurfacelessEGL : public GLSurfaceEGL {
  162. public:
  163. SurfacelessEGL(GLDisplayEGL* display, const gfx::Size& size);
  164. SurfacelessEGL(const SurfacelessEGL&) = delete;
  165. SurfacelessEGL& operator=(const SurfacelessEGL&) = delete;
  166. // Implement GLSurface.
  167. bool Initialize(GLSurfaceFormat format) override;
  168. void Destroy() override;
  169. bool IsOffscreen() override;
  170. bool IsSurfaceless() const override;
  171. gfx::SwapResult SwapBuffers(PresentationCallback callback) override;
  172. gfx::Size GetSize() override;
  173. bool Resize(const gfx::Size& size,
  174. float scale_factor,
  175. const gfx::ColorSpace& color_space,
  176. bool has_alpha) override;
  177. EGLSurface GetHandle() override;
  178. void* GetShareHandle() override;
  179. protected:
  180. ~SurfacelessEGL() override;
  181. private:
  182. gfx::Size size_;
  183. };
  184. } // namespace gl
  185. #endif // UI_GL_GL_SURFACE_EGL_H_