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