gl_surface_glx.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_GLX_H_
  5. #define UI_GL_GL_SURFACE_GLX_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/memory/raw_ptr.h"
  10. #include "ui/gfx/geometry/size.h"
  11. #include "ui/gfx/native_widget_types.h"
  12. #include "ui/gfx/x/event.h"
  13. #include "ui/gfx/x/glx.h"
  14. #include "ui/gl/gl_export.h"
  15. #include "ui/gl/gl_surface.h"
  16. using GLXFBConfig = struct __GLXFBConfigRec*;
  17. namespace gfx {
  18. class VSyncProvider;
  19. }
  20. namespace gl {
  21. class GLSurfacePresentationHelper;
  22. // Base class for GLX surfaces.
  23. class GL_EXPORT GLSurfaceGLX : public GLSurface {
  24. public:
  25. GLSurfaceGLX();
  26. GLSurfaceGLX(const GLSurfaceGLX&) = delete;
  27. GLSurfaceGLX& operator=(const GLSurfaceGLX&) = delete;
  28. static bool InitializeOneOff();
  29. static bool InitializeExtensionSettingsOneOff();
  30. static void ShutdownOneOff();
  31. // These aren't particularly tied to surfaces, but since we already
  32. // have the static InitializeOneOff here, it's easiest to reuse its
  33. // initialization guards.
  34. static std::string QueryGLXExtensions();
  35. static const char* GetGLXExtensions();
  36. static bool HasGLXExtension(const char* name);
  37. static bool IsCreateContextSupported();
  38. static bool IsCreateContextRobustnessSupported();
  39. static bool IsRobustnessVideoMemoryPurgeSupported();
  40. static bool IsCreateContextProfileSupported();
  41. static bool IsCreateContextES2ProfileSupported();
  42. static bool IsTextureFromPixmapSupported();
  43. static bool IsOMLSyncControlSupported();
  44. static bool IsEXTSwapControlSupported();
  45. static bool IsMESASwapControlSupported();
  46. GLDisplay* GetGLDisplay() override;
  47. // Get the FB config that the surface was created with or NULL if it is not
  48. // a GLX drawable.
  49. void* GetConfig() override = 0;
  50. protected:
  51. ~GLSurfaceGLX() override;
  52. private:
  53. static bool initialized_;
  54. raw_ptr<GLDisplayX11> display_;
  55. };
  56. // A surface used to render to a view.
  57. class GL_EXPORT NativeViewGLSurfaceGLX : public GLSurfaceGLX {
  58. public:
  59. explicit NativeViewGLSurfaceGLX(gfx::AcceleratedWidget window);
  60. NativeViewGLSurfaceGLX(const NativeViewGLSurfaceGLX&) = delete;
  61. NativeViewGLSurfaceGLX& operator=(const NativeViewGLSurfaceGLX&) = delete;
  62. // Implement GLSurfaceGLX.
  63. bool Initialize(GLSurfaceFormat format) override;
  64. void Destroy() override;
  65. bool Resize(const gfx::Size& size,
  66. float scale_factor,
  67. const gfx::ColorSpace& color_space,
  68. bool has_alpha) override;
  69. bool IsOffscreen() override;
  70. gfx::SwapResult SwapBuffers(PresentationCallback callback) override;
  71. gfx::Size GetSize() override;
  72. void* GetHandle() override;
  73. bool SupportsPostSubBuffer() override;
  74. void* GetConfig() override;
  75. GLSurfaceFormat GetFormat() override;
  76. gfx::SwapResult PostSubBuffer(int x,
  77. int y,
  78. int width,
  79. int height,
  80. PresentationCallback callback) override;
  81. bool OnMakeCurrent(GLContext* context) override;
  82. gfx::VSyncProvider* GetVSyncProvider() override;
  83. void SetVSyncEnabled(bool enabled) override;
  84. protected:
  85. ~NativeViewGLSurfaceGLX() override;
  86. // Handle registering and unregistering for Expose events.
  87. virtual void RegisterEvents() = 0;
  88. virtual void UnregisterEvents() = 0;
  89. // Forwards Expose event to child window.
  90. void ForwardExposeEvent(const x11::Event& xevent);
  91. // Checks if event is Expose for child window.
  92. bool CanHandleEvent(const x11::Event& xevent);
  93. gfx::AcceleratedWidget window() const {
  94. return static_cast<gfx::AcceleratedWidget>(window_);
  95. }
  96. private:
  97. // The handle for the drawable to make current or swap.
  98. uint32_t GetDrawableHandle() const;
  99. // Window passed in at creation. Always valid.
  100. gfx::AcceleratedWidget parent_window_;
  101. // Child window, used to control resizes so that they're in-order with GL.
  102. x11::Window window_;
  103. // GLXDrawable for the window.
  104. x11::Glx::Window glx_window_;
  105. GLXFBConfig config_;
  106. gfx::Size size_;
  107. bool has_swapped_buffers_;
  108. std::unique_ptr<gfx::VSyncProvider> vsync_provider_;
  109. std::unique_ptr<GLSurfacePresentationHelper> presentation_helper_;
  110. };
  111. // A surface used to render to an offscreen pbuffer.
  112. class GL_EXPORT UnmappedNativeViewGLSurfaceGLX : public GLSurfaceGLX {
  113. public:
  114. explicit UnmappedNativeViewGLSurfaceGLX(const gfx::Size& size);
  115. UnmappedNativeViewGLSurfaceGLX(const UnmappedNativeViewGLSurfaceGLX&) =
  116. delete;
  117. UnmappedNativeViewGLSurfaceGLX& operator=(
  118. const UnmappedNativeViewGLSurfaceGLX&) = delete;
  119. // Implement GLSurfaceGLX.
  120. bool Initialize(GLSurfaceFormat format) override;
  121. void Destroy() override;
  122. bool IsOffscreen() override;
  123. gfx::SwapResult SwapBuffers(PresentationCallback callback) override;
  124. gfx::Size GetSize() override;
  125. void* GetHandle() override;
  126. void* GetConfig() override;
  127. GLSurfaceFormat GetFormat() override;
  128. protected:
  129. ~UnmappedNativeViewGLSurfaceGLX() override;
  130. private:
  131. gfx::Size size_;
  132. GLXFBConfig config_;
  133. // Unmapped dummy window, used to provide a compatible surface.
  134. x11::Window window_;
  135. // GLXDrawable for the window.
  136. x11::Glx::Window glx_window_;
  137. };
  138. } // namespace gl
  139. #endif // UI_GL_GL_SURFACE_GLX_H_