[WTF] Add environment variable helpers
[WebKit-https.git] / Source / WebCore / platform / graphics / x11 / PlatformDisplayX11.cpp
1 /*
2  * Copyright (C) 2015 Igalia S.L
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "PlatformDisplayX11.h"
28
29 #include "GLContext.h"
30 #include <wtf/Environment.h>
31
32 #if PLATFORM(X11)
33 #include <X11/Xlib.h>
34 #include <X11/extensions/Xcomposite.h>
35 #if PLATFORM(GTK)
36 #include <X11/extensions/Xdamage.h>
37 #endif
38
39 #if USE(EGL)
40 #include <EGL/egl.h>
41 #include <EGL/eglext.h>
42 #endif
43
44 namespace WebCore {
45
46 std::unique_ptr<PlatformDisplay> PlatformDisplayX11::create()
47 {
48     Display* display = XOpenDisplay(Environment::getRaw("DISPLAY"));
49     if (!display)
50         return nullptr;
51
52     return std::unique_ptr<PlatformDisplayX11>(new PlatformDisplayX11(display, NativeDisplayOwned::Yes));
53 }
54
55 std::unique_ptr<PlatformDisplay> PlatformDisplayX11::create(Display* display)
56 {
57     return std::unique_ptr<PlatformDisplayX11>(new PlatformDisplayX11(display, NativeDisplayOwned::No));
58 }
59
60 PlatformDisplayX11::PlatformDisplayX11(Display* display, NativeDisplayOwned displayOwned)
61     : PlatformDisplay(displayOwned)
62     , m_display(display)
63 {
64 }
65
66 PlatformDisplayX11::~PlatformDisplayX11()
67 {
68 #if USE(EGL) || USE(GLX)
69     // Clear the sharing context before releasing the display.
70     m_sharingGLContext = nullptr;
71 #endif
72     if (m_nativeDisplayOwned == NativeDisplayOwned::Yes)
73         XCloseDisplay(m_display);
74 }
75
76 #if USE(EGL)
77 void PlatformDisplayX11::initializeEGLDisplay()
78 {
79 #if defined(EGL_KHR_platform_x11)
80     const char* extensions = eglQueryString(nullptr, EGL_EXTENSIONS);
81     if (GLContext::isExtensionSupported(extensions, "EGL_KHR_platform_base")) {
82         if (auto* getPlatformDisplay = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplay")))
83             m_eglDisplay = getPlatformDisplay(EGL_PLATFORM_X11_KHR, m_display, nullptr);
84     } else if (GLContext::isExtensionSupported(extensions, "EGL_EXT_platform_base")) {
85         if (auto* getPlatformDisplay = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT")))
86             m_eglDisplay = getPlatformDisplay(EGL_PLATFORM_X11_KHR, m_display, nullptr);
87     } else
88 #endif
89         m_eglDisplay = eglGetDisplay(m_display);
90
91     PlatformDisplay::initializeEGLDisplay();
92 }
93 #endif // USE(EGL)
94
95 bool PlatformDisplayX11::supportsXComposite() const
96 {
97     if (!m_supportsXComposite) {
98         if (m_display) {
99             int eventBase, errorBase;
100             m_supportsXComposite = XCompositeQueryExtension(m_display, &eventBase, &errorBase);
101         } else
102             m_supportsXComposite = false;
103     }
104     return m_supportsXComposite.value();
105 }
106
107 bool PlatformDisplayX11::supportsXDamage(Optional<int>& damageEventBase, Optional<int>& damageErrorBase) const
108 {
109     if (!m_supportsXDamage) {
110         m_supportsXDamage = false;
111 #if PLATFORM(GTK)
112         if (m_display) {
113             int eventBase, errorBase;
114             m_supportsXDamage = XDamageQueryExtension(m_display, &eventBase, &errorBase);
115             if (m_supportsXDamage.value()) {
116                 m_damageEventBase = eventBase;
117                 m_damageErrorBase = errorBase;
118             }
119         }
120 #endif
121     }
122
123     damageEventBase = m_damageEventBase;
124     damageErrorBase = m_damageErrorBase;
125     return m_supportsXDamage.value();
126 }
127
128 } // namespace WebCore
129
130 #endif // PLATFORM(X11)
131