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