PlatformDisplayWayland.cpp [plain text]
#include "config.h"
#include "PlatformDisplayWayland.h"
#if PLATFORM(WAYLAND)
#include "GLContextEGL.h"
#include <cstring>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <wtf/Assertions.h>
namespace WebCore {
const struct wl_registry_listener PlatformDisplayWayland::s_registryListener = {
[](void* data, struct wl_registry*, uint32_t name, const char* interface, uint32_t) {
static_cast<PlatformDisplayWayland*>(data)->registryGlobal(interface, name);
},
[](void*, struct wl_registry*, uint32_t)
{
}
};
std::unique_ptr<PlatformDisplay> PlatformDisplayWayland::create()
{
struct wl_display* display = wl_display_connect(nullptr);
if (!display)
return nullptr;
return std::make_unique<PlatformDisplayWayland>(display, NativeDisplayOwned::Yes);
}
PlatformDisplayWayland::PlatformDisplayWayland(struct wl_display* display, NativeDisplayOwned displayOwned)
: PlatformDisplay(displayOwned)
{
initialize(display);
}
PlatformDisplayWayland::~PlatformDisplayWayland()
{
if (m_nativeDisplayOwned == NativeDisplayOwned::Yes)
wl_display_destroy(m_display);
}
void PlatformDisplayWayland::initialize(wl_display* display)
{
m_display = display;
if (!m_display)
return;
m_registry.reset(wl_display_get_registry(m_display));
wl_registry_add_listener(m_registry.get(), &s_registryListener, this);
wl_display_roundtrip(m_display);
#if USE(EGL)
#if defined(EGL_KHR_platform_wayland)
const char* extensions = eglQueryString(nullptr, EGL_EXTENSIONS);
if (GLContext::isExtensionSupported(extensions, "EGL_KHR_platform_base")) {
if (auto* getPlatformDisplay = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplay")))
m_eglDisplay = getPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, m_display, nullptr);
} else if (GLContext::isExtensionSupported(extensions, "EGL_EXT_platform_base")) {
if (auto* getPlatformDisplay = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT")))
m_eglDisplay = getPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, m_display, nullptr);
} else
#endif
m_eglDisplay = eglGetDisplay(m_display);
PlatformDisplay::initializeEGLDisplay();
#endif
}
void PlatformDisplayWayland::registryGlobal(const char* interface, uint32_t name)
{
if (!std::strcmp(interface, "wl_compositor"))
m_compositor.reset(static_cast<struct wl_compositor*>(wl_registry_bind(m_registry.get(), name, &wl_compositor_interface, 1)));
}
WlUniquePtr<struct wl_surface> PlatformDisplayWayland::createSurface() const
{
if (!m_compositor)
return nullptr;
return WlUniquePtr<struct wl_surface>(wl_compositor_create_surface(m_compositor.get()));
}
}
#endif // PLATFORM(WAYLAND)