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