1/*
2 * Copyright (C) 2014 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "PlatformDisplayWayland.h"
28
29#if PLATFORM(WAYLAND)
30
31#include "GLContextEGL.h"
32#include <cstring>
33// These includes need to be in this order because wayland-egl.h defines WL_EGL_PLATFORM
34// and egl.h checks that to decide whether it's Wayland platform.
35#include <wayland-egl.h>
36#include <EGL/egl.h>
37#include <EGL/eglext.h>
38#include <wtf/Assertions.h>
39
40namespace WebCore {
41
42const struct wl_registry_listener PlatformDisplayWayland::s_registryListener = {
43 // globalCallback
44 [](void* data, struct wl_registry*, uint32_t name, const char* interface, uint32_t) {
45 static_cast<PlatformDisplayWayland*>(data)->registryGlobal(interface, name);
46 },
47 // globalRemoveCallback
48 [](void*, struct wl_registry*, uint32_t)
49 {
50 }
51};
52
53std::unique_ptr<PlatformDisplay> PlatformDisplayWayland::create()
54{
55 struct wl_display* display = wl_display_connect(nullptr);
56 if (!display)
57 return nullptr;
58
59 auto platformDisplay = std::unique_ptr<PlatformDisplayWayland>(new PlatformDisplayWayland(display, NativeDisplayOwned::Yes));
60 platformDisplay->initialize();
61 return platformDisplay;
62}
63
64std::unique_ptr<PlatformDisplay> PlatformDisplayWayland::create(struct wl_display* display)
65{
66 auto platformDisplay = std::unique_ptr<PlatformDisplayWayland>(new PlatformDisplayWayland(display, NativeDisplayOwned::No));
67 platformDisplay->initialize();
68 return platformDisplay;
69}
70
71PlatformDisplayWayland::PlatformDisplayWayland(struct wl_display* display, NativeDisplayOwned displayOwned)
72 : PlatformDisplay(displayOwned)
73 , m_display(display)
74{
75}
76
77PlatformDisplayWayland::~PlatformDisplayWayland()
78{
79 if (m_nativeDisplayOwned == NativeDisplayOwned::Yes) {
80 m_compositor = nullptr;
81 m_registry = nullptr;
82 wl_display_disconnect(m_display);
83 }
84}
85
86void PlatformDisplayWayland::initialize()
87{
88 if (!m_display)
89 return;
90
91 m_registry.reset(wl_display_get_registry(m_display));
92 wl_registry_add_listener(m_registry.get(), &s_registryListener, this);
93 wl_display_roundtrip(m_display);
94
95#if USE(EGL)
96#if defined(EGL_KHR_platform_wayland)
97 const char* extensions = eglQueryString(nullptr, EGL_EXTENSIONS);
98 if (GLContext::isExtensionSupported(extensions, "EGL_KHR_platform_base")) {
99 if (auto* getPlatformDisplay = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplay")))
100 m_eglDisplay = getPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, m_display, nullptr);
101 } else if (GLContext::isExtensionSupported(extensions, "EGL_EXT_platform_base")) {
102 if (auto* getPlatformDisplay = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT")))
103 m_eglDisplay = getPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, m_display, nullptr);
104 } else
105#endif
106 m_eglDisplay = eglGetDisplay(m_display);
107
108 PlatformDisplay::initializeEGLDisplay();
109#endif
110}
111
112void PlatformDisplayWayland::registryGlobal(const char* interface, uint32_t name)
113{
114 if (!std::strcmp(interface, "wl_compositor"))
115 m_compositor.reset(static_cast<struct wl_compositor*>(wl_registry_bind(m_registry.get(), name, &wl_compositor_interface, 1)));
116}
117
118WlUniquePtr<struct wl_surface> PlatformDisplayWayland::createSurface() const
119{
120 if (!m_compositor)
121 return nullptr;
122
123 return WlUniquePtr<struct wl_surface>(wl_compositor_create_surface(m_compositor.get()));
124}
125
126} // namespace WebCore
127
128#endif // PLATFORM(WAYLAND)
129