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 "tcuWayland.hpp"
27 #include "gluRenderConfig.hpp"
28 #include "deMemory.h"
29 
30 #include <stdio.h>
31 
32 namespace tcu
33 {
34 namespace wayland
35 {
36 
EventState(void)37 EventState::EventState (void)
38 	: m_quit(false)
39 {
40 }
41 
~EventState(void)42 EventState::~EventState (void)
43 {
44 }
45 
setQuitFlag(bool quit)46 void EventState::setQuitFlag (bool quit)
47 {
48 	de::ScopedLock lock(m_mutex);
49 	m_quit = quit;
50 }
51 
getQuitFlag(void)52 bool EventState::getQuitFlag (void)
53 {
54 	de::ScopedLock lock(m_mutex);
55 	return m_quit;
56 }
57 const struct wl_registry_listener Display::s_registryListener =
58 {
59 	Display::handleGlobal,
60 	Display::handleGlobalRemove
61 };
62 
63 const struct wl_shell_surface_listener Window::s_shellSurfaceListener =
64 {
65 	Window::handlePing,
66 	Window::handleConfigure,
67 	Window::handlePopupDone,
68 };
69 
handleGlobal(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)70 void Display::handleGlobal (void* data, struct wl_registry* registry, uint32_t id, const char* interface, uint32_t version)
71 {
72 	Display* _this = static_cast<Display*>(data);
73 	DE_UNREF(version);
74 
75 	if (!strcmp(interface, "wl_compositor"))
76 		_this->m_compositor = static_cast<struct wl_compositor*>(wl_registry_bind(registry, id, &wl_compositor_interface, 3));
77 	/* Todo: when the xdg_shell protocol has stablized, we should move wl_shell to xdg_shell. */
78 	if (!strcmp(interface, "wl_shell"))
79 		_this->m_shell = static_cast<struct wl_shell*>(wl_registry_bind(registry, id, &wl_shell_interface, 1));
80 }
81 
handleGlobalRemove(void * data,struct wl_registry * registry,uint32_t name)82 void Display::handleGlobalRemove (void* data, struct wl_registry* registry, uint32_t name)
83 {
84 	DE_UNREF(data);
85 	DE_UNREF(registry);
86 	DE_UNREF(name);
87 }
88 
Display(EventState & eventState,const char * name)89 Display::Display (EventState& eventState, const char* name)
90 	: m_eventState	(eventState)
91 	, m_display		(DE_NULL)
92 	, m_registry	(DE_NULL)
93 	, m_compositor	(DE_NULL)
94 	, m_shell		(DE_NULL)
95 {
96 	try
97 	{
98 		m_display = wl_display_connect(name);
99 		if (!m_display)
100 			throw ResourceError("Failed to open display", name, __FILE__, __LINE__);
101 
102 		m_registry = wl_display_get_registry(m_display);
103 		if (!m_registry)
104 			throw ResourceError("Failed to get registry", name, __FILE__, __LINE__);
105 
106 		wl_registry_add_listener(m_registry, &s_registryListener, this);
107 		wl_display_roundtrip(m_display);
108 		if (!m_compositor)
109 			throw ResourceError("Failed to bind compositor", name, __FILE__, __LINE__);
110 		if (!m_shell)
111 			throw ResourceError("Failed to bind shell", name, __FILE__, __LINE__);
112 	}
113 	catch (...)
114 	{
115 		if (m_shell)
116 			wl_shell_destroy(m_shell);
117 
118 		if (m_compositor)
119 			wl_compositor_destroy(m_compositor);
120 
121 		if (m_registry)
122 			wl_registry_destroy(m_registry);
123 
124 		if (m_display)
125 			wl_display_disconnect(m_display);
126 
127 		throw;
128 	}
129 }
130 
~Display(void)131 Display::~Display (void)
132 {
133 	if (m_shell)
134 		wl_shell_destroy(m_shell);
135 
136 	if (m_compositor)
137 		wl_compositor_destroy(m_compositor);
138 
139 	if (m_registry)
140 		wl_registry_destroy(m_registry);
141 
142 	if (m_display)
143 		wl_display_disconnect(m_display);
144 }
145 
processEvents(void)146 void Display::processEvents (void)
147 {
148 }
149 
Window(Display & display,int width,int height)150 Window::Window (Display& display, int width, int height)
151 	: m_display		(display)
152 {
153 	try
154 	{
155 		m_surface = wl_compositor_create_surface(display.getCompositor());
156 		if (!m_surface)
157 			throw ResourceError("Failed to create ", "surface", __FILE__, __LINE__);
158 
159 		m_shellSurface = wl_shell_get_shell_surface(display.getShell(), m_surface);
160 		if (!m_shellSurface)
161 			throw ResourceError("Failed to create ", "shell_surface", __FILE__, __LINE__);
162 
163 		wl_shell_surface_add_listener(m_shellSurface, &s_shellSurfaceListener, this);
164 		wl_shell_surface_set_title(m_shellSurface, "CTS for OpenGL (ES)");
165 		wl_shell_surface_set_toplevel(m_shellSurface);
166 
167 		if (width == glu::RenderConfig::DONT_CARE)
168 			width = DEFAULT_WINDOW_WIDTH;
169 		if (height == glu::RenderConfig::DONT_CARE)
170 			height = DEFAULT_WINDOW_HEIGHT;
171 
172 		m_window = wl_egl_window_create(m_surface, width, height);
173 		if (!m_window)
174 			throw ResourceError("Failed to create ", "window", __FILE__, __LINE__);
175 	}
176 	catch (...)
177 	{
178 		throw;
179 	}
180 	TCU_CHECK(m_window);
181 }
182 
setVisibility(bool visible)183 void Window::setVisibility (bool visible)
184 {
185 	m_visible = visible;
186 }
187 
getDimensions(int * width,int * height) const188 void Window::getDimensions (int* width, int* height) const
189 {
190 	wl_egl_window_get_attached_size(m_window, width, height);
191 }
192 
setDimensions(int width,int height)193 void Window::setDimensions (int width, int height)
194 {
195 	wl_egl_window_resize(m_window, width, height, 0, 0);
196 }
197 
processEvents(void)198 void Window::processEvents (void)
199 {
200 }
201 
handlePing(void * data,struct wl_shell_surface * shellSurface,uint32_t serial)202 void Window::handlePing (void* data, struct wl_shell_surface* shellSurface, uint32_t serial)
203 {
204 	DE_UNREF(data);
205 	wl_shell_surface_pong(shellSurface, serial);
206 }
207 
handleConfigure(void * data,struct wl_shell_surface * shellSurface,uint32_t edges,int32_t width,int32_t height)208 void Window::handleConfigure (void* data, struct wl_shell_surface* shellSurface, uint32_t edges, int32_t width, int32_t height)
209 {
210 	DE_UNREF(data);
211 	DE_UNREF(shellSurface);
212 	DE_UNREF(edges);
213 	DE_UNREF(width);
214 	DE_UNREF(height);
215 }
216 
handlePopupDone(void * data,struct wl_shell_surface * shellSurface)217 void Window::handlePopupDone (void* data, struct wl_shell_surface* shellSurface)
218 {
219 	DE_UNREF(data);
220 	DE_UNREF(shellSurface);
221 }
222 
~Window(void)223 Window::~Window (void)
224 {
225 	if (m_window)
226 		wl_egl_window_destroy(m_window);
227 	if (m_shellSurface)
228 		wl_shell_surface_destroy(m_shellSurface);
229 	if (m_surface)
230 		wl_surface_destroy(m_surface);
231 }
232 
233 } // wayland
234 } // tcu
235