1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
4 *
5 * Copyright (c) 2014 The Android Open Source Project
6 * Copyright (c) 2016 The Khronos Group Inc.
7 * Copyright (c) 2016 Mun Gwan-gyeong <elongbug@gmail.com>
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief wayland utilities.
24 *//*--------------------------------------------------------------------*/
25
26 #include "tcuLnxWayland.hpp"
27 #include "gluRenderConfig.hpp"
28 #include "deMemory.h"
29
30 #include <stdio.h>
31
32 namespace tcu
33 {
34 namespace lnx
35 {
36 namespace wayland
37 {
38
39 const struct wl_registry_listener Display::s_registryListener
40 {
41 Display::handleGlobal,
42 Display::handleGlobalRemove
43 };
44
45 Display::DisplayState Display::s_displayState = Display::DISPLAY_STATE_UNKNOWN;
46
47 bool Window::s_addWMBaseListener = true;
48
49 const struct xdg_surface_listener Window::s_xdgSurfaceListener
50 {
51 Window::handleConfigure
52 };
53
54 const struct xdg_wm_base_listener Window::s_wmBaseListener
55 {
56 Window::handlePing
57 };
58
handleGlobal(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)59 void Display::handleGlobal (void* data, struct wl_registry* registry, uint32_t id, const char* interface, uint32_t version)
60 {
61 Display* _this = static_cast<Display*>(data);
62 DE_UNREF(version);
63
64 if (!strcmp(interface, "wl_compositor"))
65 _this->m_compositor = static_cast<struct wl_compositor*>(wl_registry_bind(registry, id, &wl_compositor_interface, version > 3 ? version : 3));
66 if (!strcmp(interface, "xdg_wm_base"))
67 _this->m_shell = static_cast<struct xdg_wm_base*>(wl_registry_bind(registry, id, &xdg_wm_base_interface, 1));
68 }
69
handleGlobalRemove(void * data,struct wl_registry * registry,uint32_t name)70 void Display::handleGlobalRemove (void* data, struct wl_registry* registry, uint32_t name)
71 {
72 DE_UNREF(data);
73 DE_UNREF(registry);
74 DE_UNREF(name);
75 }
76
hasDisplay(const char * name)77 bool Display::hasDisplay (const char* name)
78 {
79 if (s_displayState == DISPLAY_STATE_UNKNOWN)
80 {
81 struct wl_display *display = wl_display_connect(name);
82 if (display)
83 {
84 s_displayState = DISPLAY_STATE_AVAILABLE;
85 wl_display_disconnect(display);
86 } else
87 s_displayState = DISPLAY_STATE_UNAVAILABLE;
88 }
89 return s_displayState == DISPLAY_STATE_AVAILABLE ? true : false;
90 }
91
Display(EventState & eventState,const char * name)92 Display::Display (EventState& eventState, const char* name)
93 : m_eventState (eventState)
94 , m_display (DE_NULL)
95 , m_registry (DE_NULL)
96 , m_compositor (DE_NULL)
97 , m_shell (DE_NULL)
98 {
99 try
100 {
101 m_display = wl_display_connect(name);
102 if (!m_display)
103 throw ResourceError("Failed to open display", name, __FILE__, __LINE__);
104
105 m_registry = wl_display_get_registry(m_display);
106 if (!m_registry)
107 throw ResourceError("Failed to get registry", name, __FILE__, __LINE__);
108
109 wl_registry_add_listener(m_registry, &s_registryListener, this);
110 wl_display_roundtrip(m_display);
111 if (!m_compositor)
112 throw ResourceError("Failed to bind compositor", name, __FILE__, __LINE__);
113 if (!m_shell)
114 throw ResourceError("Failed to bind shell", name, __FILE__, __LINE__);
115 }
116 catch (...)
117 {
118 if (m_shell)
119 xdg_wm_base_destroy(m_shell);
120
121 if (m_compositor)
122 wl_compositor_destroy(m_compositor);
123
124 if (m_registry)
125 wl_registry_destroy(m_registry);
126
127 if (m_display)
128 wl_display_disconnect(m_display);
129
130 throw;
131 }
132 }
133
~Display(void)134 Display::~Display (void)
135 {
136 if (m_shell)
137 xdg_wm_base_destroy(m_shell);
138
139 if (m_compositor)
140 wl_compositor_destroy(m_compositor);
141
142 if (m_registry)
143 wl_registry_destroy(m_registry);
144
145 if (m_display)
146 wl_display_disconnect(m_display);
147 }
148
processEvents(void)149 void Display::processEvents (void)
150 {
151 }
152
Window(Display & display,int width,int height)153 Window::Window (Display& display, int width, int height)
154 : m_display (display)
155 {
156 try
157 {
158 m_surface = wl_compositor_create_surface(display.getCompositor());
159 if (!m_surface)
160 throw ResourceError("Failed to create ", "surface", __FILE__, __LINE__);
161
162 m_xdgSurface = xdg_wm_base_get_xdg_surface(display.getShell(), m_surface);
163 if (!m_xdgSurface)
164 throw ResourceError("Failed to create ", "shell_surface", __FILE__, __LINE__);
165
166 // add wm base listener once
167 if (s_addWMBaseListener)
168 {
169 xdg_wm_base_add_listener(display.getShell(), &s_wmBaseListener, this);
170 s_addWMBaseListener = false;
171 }
172 xdg_surface_add_listener(m_xdgSurface, &s_xdgSurfaceListener, this);
173
174 // select xdg surface role
175 m_topLevel = xdg_surface_get_toplevel(m_xdgSurface);
176 xdg_toplevel_set_title(m_topLevel, "CTS for OpenGL (ES)");
177
178 // configure xdg surface
179 m_configured = false;
180 wl_surface_commit(m_surface);
181
182 // wait till xdg surface is configured
183 int dispatchedEvents = 0;
184 while(dispatchedEvents != -1)
185 {
186 dispatchedEvents = wl_display_dispatch(display.getDisplay());
187 if (m_configured)
188 break;
189 }
190
191 if (width == glu::RenderConfig::DONT_CARE)
192 width = DEFAULT_WINDOW_WIDTH;
193 if (height == glu::RenderConfig::DONT_CARE)
194 height = DEFAULT_WINDOW_HEIGHT;
195
196 m_window = wl_egl_window_create(m_surface, width, height);
197 if (!m_window)
198 throw ResourceError("Failed to create ", "window", __FILE__, __LINE__);
199 }
200 catch (...)
201 {
202 throw;
203 }
204 TCU_CHECK(m_window);
205 }
206
setVisibility(bool visible)207 void Window::setVisibility (bool visible)
208 {
209 m_visible = visible;
210 }
211
getDimensions(int * width,int * height) const212 void Window::getDimensions (int* width, int* height) const
213 {
214 wl_egl_window_get_attached_size(m_window, width, height);
215 }
216
setDimensions(int width,int height)217 void Window::setDimensions (int width, int height)
218 {
219 wl_egl_window_resize(m_window, width, height, 0, 0);
220 }
221
processEvents(void)222 void Window::processEvents (void)
223 {
224 }
225
handlePing(void * data,struct xdg_wm_base * shell,uint32_t serial)226 void Window::handlePing (void* data, struct xdg_wm_base* shell, uint32_t serial)
227 {
228 DE_UNREF(data);
229 xdg_wm_base_pong(shell, serial);
230 }
231
handleConfigure(void * data,struct xdg_surface * xdgSurface,uint32_t serial)232 void Window::handleConfigure (void* data, struct xdg_surface* xdgSurface, uint32_t serial)
233 {
234 Window* window = reinterpret_cast<Window*>(data);
235 window->m_configured = true;
236
237 xdg_surface_ack_configure(xdgSurface, serial);
238 }
239
~Window(void)240 Window::~Window (void)
241 {
242 if (m_window)
243 wl_egl_window_destroy(m_window);
244 if (m_topLevel)
245 xdg_toplevel_destroy(m_topLevel);
246 if (m_xdgSurface)
247 xdg_surface_destroy(m_xdgSurface);
248 if (m_surface)
249 wl_surface_destroy(m_surface);
250 }
251
252 } // wayland
253 } // lnx
254 } // tcu
255