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 "WaylandDisplay.h"
28
29#if PLATFORM(WAYLAND)
30
31#include "GLContextEGL.h"
32#include "WaylandSurface.h"
33#include <cstring>
34#include <glib.h>
35
36namespace WebCore {
37
38const struct wl_registry_listener WaylandDisplay::m_registryListener = {
39 WaylandDisplay::globalCallback,
40 WaylandDisplay::globalRemoveCallback
41};
42
43void WaylandDisplay::globalCallback(void* data, struct wl_registry* registry, uint32_t name, const char* interface, uint32_t)
44{
45 auto display = static_cast<WaylandDisplay*>(data);
46 if (!std::strcmp(interface, "wl_compositor"))
47 display->m_compositor = static_cast<struct wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 1));
48 else if (!std::strcmp(interface, "wl_webkitgtk"))
49 display->m_webkitgtk = static_cast<struct wl_webkitgtk*>(wl_registry_bind(registry, name, &wl_webkitgtk_interface, 1));
50}
51
52void WaylandDisplay::globalRemoveCallback(void*, struct wl_registry*, uint32_t)
53{
54 // FIXME: if this can happen without the UI Process getting shut down
55 // we should probably destroy our cached display instance.
56}
57
58WaylandDisplay* WaylandDisplay::instance()
59{
60 static WaylandDisplay* display = nullptr;
61 static bool initialized = false;
62 if (initialized)
63 return display;
64
65 initialized = true;
66 struct wl_display* wlDisplay = wl_display_connect("webkitgtk-wayland-compositor-socket");
67 if (!wlDisplay)
68 return nullptr;
69
70 display = new WaylandDisplay(wlDisplay);
71 if (!display->isInitialized()) {
72 delete display;
73 return nullptr;
74 }
75
76 return display;
77}
78
79WaylandDisplay::WaylandDisplay(struct wl_display* wlDisplay)
80 : m_display(wlDisplay)
81 , m_registry(wl_display_get_registry(m_display))
82 , m_eglConfigChosen(false)
83{
84 wl_registry_add_listener(m_registry, &m_registryListener, this);
85 wl_display_roundtrip(m_display);
86
87 static const EGLint configAttributes[] = {
88 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
89 EGL_RED_SIZE, 1,
90 EGL_GREEN_SIZE, 1,
91 EGL_BLUE_SIZE, 1,
92 EGL_ALPHA_SIZE, 1,
93 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
94 EGL_NONE
95 };
96
97 m_eglDisplay = eglGetDisplay(m_display);
98 if (m_eglDisplay == EGL_NO_DISPLAY) {
99 g_warning("WaylandDisplay initialization: failed to acquire EGL display.");
100 return;
101 }
102
103 if (eglInitialize(m_eglDisplay, 0, 0) == EGL_FALSE) {
104 g_warning("WaylandDisplay initialization: failed to initialize the EGL display.");
105 return;
106 }
107
108 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
109 g_warning("WaylandDisplay initialization: failed to set EGL_OPENGL_ES_API as the rendering API.");
110 return;
111 }
112
113 EGLint numberOfConfigs;
114 if (!eglChooseConfig(m_eglDisplay, configAttributes, &m_eglConfig, 1, &numberOfConfigs) || numberOfConfigs != 1) {
115 g_warning("WaylandDisplay initialization: failed to find the desired EGL configuration.");
116 return;
117 }
118
119 m_eglConfigChosen = true;
120}
121
122std::unique_ptr<WaylandSurface> WaylandDisplay::createSurface(const IntSize& size, int widgetId)
123{
124 struct wl_surface* wlSurface = wl_compositor_create_surface(m_compositor);
125 // We keep the minimum size at 1x1px since Mesa returns null values in wl_egl_window_create() for zero width or height.
126 EGLNativeWindowType nativeWindow = wl_egl_window_create(wlSurface, std::max(1, size.width()), std::max(1, size.height()));
127
128 wl_webkitgtk_set_surface_for_widget(m_webkitgtk, wlSurface, widgetId);
129 wl_display_roundtrip(m_display);
130
131 return std::make_unique<WaylandSurface>(wlSurface, nativeWindow);
132}
133
134PassOwnPtr<GLContextEGL> WaylandDisplay::createSharingGLContext()
135{
136 struct wl_surface* wlSurface = wl_compositor_create_surface(m_compositor);
137 EGLNativeWindowType nativeWindow = wl_egl_window_create(wlSurface, 1, 1);
138 return GLContextEGL::createWindowContext(nativeWindow, nullptr);
139}
140
141} // namespace WebCore
142
143#endif // PLATFORM(WAYLAND)