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 <string> 9 10 #include "base/compiler_specific.h" 11 #include "ui/gfx/native_widget_types.h" 12 #include "ui/gfx/size.h" 13 #include "ui/gfx/vsync_provider.h" 14 #include "ui/gfx/x/x11_types.h" 15 #include "ui/gl/gl_export.h" 16 #include "ui/gl/gl_surface.h" 17 18 namespace gfx { 19 20 // Base class for GLX surfaces. 21 class GL_EXPORT GLSurfaceGLX : public GLSurface { 22 public: 23 GLSurfaceGLX(); 24 25 static bool InitializeOneOff(); 26 27 // These aren't particularly tied to surfaces, but since we already 28 // have the static InitializeOneOff here, it's easiest to reuse its 29 // initialization guards. 30 static const char* GetGLXExtensions(); 31 static bool HasGLXExtension(const char* name); 32 static bool IsCreateContextSupported(); 33 static bool IsCreateContextRobustnessSupported(); 34 static bool IsTextureFromPixmapSupported(); 35 static bool IsOMLSyncControlSupported(); 36 37 virtual void* GetDisplay() OVERRIDE; 38 39 // Get the FB config that the surface was created with or NULL if it is not 40 // a GLX drawable. 41 virtual void* GetConfig() = 0; 42 43 protected: 44 virtual ~GLSurfaceGLX(); 45 46 private: 47 DISALLOW_COPY_AND_ASSIGN(GLSurfaceGLX); 48 }; 49 50 // A surface used to render to a view. 51 class GL_EXPORT NativeViewGLSurfaceGLX : public GLSurfaceGLX { 52 public: 53 explicit NativeViewGLSurfaceGLX(gfx::AcceleratedWidget window); 54 55 // Implement GLSurfaceGLX. 56 virtual bool Initialize() OVERRIDE; 57 virtual void Destroy() OVERRIDE; 58 virtual bool Resize(const gfx::Size& size) OVERRIDE; 59 virtual bool IsOffscreen() OVERRIDE; 60 virtual bool SwapBuffers() OVERRIDE; 61 virtual gfx::Size GetSize() OVERRIDE; 62 virtual void* GetHandle() OVERRIDE; 63 virtual std::string GetExtensions() OVERRIDE; 64 virtual void* GetConfig() OVERRIDE; 65 virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE; 66 virtual VSyncProvider* GetVSyncProvider() OVERRIDE; 67 68 protected: 69 NativeViewGLSurfaceGLX(); 70 virtual ~NativeViewGLSurfaceGLX(); 71 72 private: 73 // The handle for the drawable to make current or swap. 74 gfx::AcceleratedWidget GetDrawableHandle() const; 75 76 // Window passed in at creation. Always valid. 77 gfx::AcceleratedWidget parent_window_; 78 79 #if defined(TOOLKIT_GTK) 80 // Some NVIDIA drivers don't allow deleting GLX windows separately from their 81 // parent X windows. Work around this by creating a child X window to the 82 // window passed in to the constructor, creating the GLX window against the 83 // child window, and then destroying the child window to destroy the GLX 84 // window. 85 // http://crbug.com/145600 86 void CreateChildWindow(); 87 void DestroyChildWindow(); 88 89 // Destroy the child window when both the front and back buffers are 90 // deallocated. 91 virtual bool SetBackbufferAllocation(bool allocated) OVERRIDE; 92 virtual void SetFrontbufferAllocation(bool allocated) OVERRIDE; 93 void AdjustBufferAllocation(); 94 95 // Child window which is used with GLX, and is discarded when it is 96 // backgrounded. 97 gfx::AcceleratedWidget child_window_; 98 99 // Dummy 1x1 window which is supplied to glXMakeCurrent when making 100 // the context current while its output surface is destroyed. 101 gfx::AcceleratedWidget dummy_window_; 102 103 bool backbuffer_allocated_; 104 bool frontbuffer_allocated_; 105 #endif 106 107 void* config_; 108 gfx::Size size_; 109 110 scoped_ptr<VSyncProvider> vsync_provider_; 111 112 DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceGLX); 113 }; 114 115 // A surface used to render to an offscreen pbuffer. 116 class GL_EXPORT PbufferGLSurfaceGLX : public GLSurfaceGLX { 117 public: 118 explicit PbufferGLSurfaceGLX(const gfx::Size& size); 119 120 // Implement GLSurfaceGLX. 121 virtual bool Initialize() OVERRIDE; 122 virtual void Destroy() OVERRIDE; 123 virtual bool IsOffscreen() OVERRIDE; 124 virtual bool SwapBuffers() OVERRIDE; 125 virtual gfx::Size GetSize() OVERRIDE; 126 virtual void* GetHandle() OVERRIDE; 127 virtual void* GetConfig() OVERRIDE; 128 129 protected: 130 virtual ~PbufferGLSurfaceGLX(); 131 132 private: 133 gfx::Size size_; 134 void* config_; 135 XID pbuffer_; 136 137 DISALLOW_COPY_AND_ASSIGN(PbufferGLSurfaceGLX); 138 }; 139 140 } // namespace gfx 141 142 #endif // UI_GL_GL_SURFACE_GLX_H_ 143