Fix Vulkan null driver
[platform/upstream/VK-GL-CTS.git] / framework / platform / X11 / tcuX11.cpp
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program Tester Core
3  * ----------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief X11 utilities.
22  *//*--------------------------------------------------------------------*/
23
24 #include "tcuX11.hpp"
25 #include "gluRenderConfig.hpp"
26 #include "deMemory.h"
27
28 #include <X11/Xutil.h>
29
30 namespace tcu
31 {
32 namespace x11
33 {
34
35 EventState::EventState (void)
36         : m_quit(false)
37 {
38 }
39
40 EventState::~EventState (void)
41 {
42 }
43
44 void EventState::setQuitFlag (bool quit)
45 {
46         de::ScopedLock lock(m_mutex);
47         m_quit = quit;
48 }
49
50 bool EventState::getQuitFlag (void)
51 {
52         de::ScopedLock lock(m_mutex);
53         return m_quit;
54 }
55
56 DisplayBase::DisplayBase (EventState& platform)
57         : m_eventState  (platform)
58 {
59 }
60
61 DisplayBase::~DisplayBase (void)
62 {
63 }
64
65 WindowBase::WindowBase ()
66         : m_visible     (false)
67 {
68 }
69
70 WindowBase::~WindowBase (void)
71 {
72 }
73
74 XlibDisplay::XlibDisplay (EventState& eventState, const char* name)
75         : DisplayBase   (eventState)
76 {
77         m_display = XOpenDisplay((char*)name); // Won't modify argument string.
78         if (!m_display)
79                 throw ResourceError("Failed to open display", name, __FILE__, __LINE__);
80
81         m_deleteAtom    = XInternAtom(m_display, "WM_DELETE_WINDOW", False);
82 }
83
84 XlibDisplay::~XlibDisplay (void)
85 {
86         XCloseDisplay(m_display);
87 }
88
89 void XlibDisplay::processEvents (void)
90 {
91         XEvent  event;
92
93         while (XPending(m_display))
94         {
95                 XNextEvent(m_display, &event);
96
97                 // \todo [2010-10-27 pyry] Handle ConfigureNotify?
98                 if (event.type == ClientMessage && (unsigned)event.xclient.data.l[0] == m_deleteAtom)
99                         m_eventState.setQuitFlag(true);
100         }
101 }
102
103 bool XlibDisplay::getVisualInfo (VisualID visualID, XVisualInfo& dst)
104 {
105         XVisualInfo             query;
106         query.visualid = visualID;
107         int                             numVisuals      = 0;
108         XVisualInfo*    response        = XGetVisualInfo(m_display, VisualIDMask, &query, &numVisuals);
109         bool                    succ            = false;
110
111         if (response != DE_NULL)
112         {
113                 if (numVisuals > 0) // should be 1, but you never know...
114                 {
115                         dst = response[0];
116                         succ = true;
117                 }
118                 XFree(response);
119         }
120
121         return succ;
122 }
123
124 ::Visual* XlibDisplay::getVisual (VisualID visualID)
125 {
126         XVisualInfo             info;
127
128         if (getVisualInfo(visualID, info))
129                 return info.visual;
130
131         return DE_NULL;
132 }
133
134 XlibWindow::XlibWindow (XlibDisplay& display, int width, int height, ::Visual* visual)
135         : WindowBase    ()
136         , m_display             (display)
137         , m_colormap    (None)
138         , m_window              (None)
139 {
140         XSetWindowAttributes    swa;
141         ::Display* const                dpy                                     = m_display.getXDisplay();
142         ::Window                                root                            = DefaultRootWindow(dpy);
143         unsigned long                   mask                            = CWBorderPixel | CWEventMask;
144
145         // If redirect is enabled, window size can't be guaranteed and it is up to
146         // the window manager to decide whether to honor sizing requests. However,
147         // overriding that causes window to appear as an overlay, which causes
148         // other issues, so this is disabled by default.
149         const bool                              overrideRedirect        = false;
150
151         if (overrideRedirect)
152         {
153                 mask |= CWOverrideRedirect;
154                 swa.override_redirect = true;
155         }
156
157         if (visual == DE_NULL)
158                 visual = CopyFromParent;
159         else
160         {
161                 XVisualInfo     info    = XVisualInfo();
162                 bool            succ    = display.getVisualInfo(XVisualIDFromVisual(visual), info);
163
164                 TCU_CHECK_INTERNAL(succ);
165
166                 root                            = RootWindow(dpy, info.screen);
167                 m_colormap                      = XCreateColormap(dpy, root, visual, AllocNone);
168                 swa.colormap            = m_colormap;
169                 mask |= CWColormap;
170         }
171
172         swa.border_pixel        = 0;
173         swa.event_mask          = ExposureMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask;
174
175         if (width == glu::RenderConfig::DONT_CARE)
176                 width = DEFAULT_WINDOW_WIDTH;
177         if (height == glu::RenderConfig::DONT_CARE)
178                 height = DEFAULT_WINDOW_HEIGHT;
179
180         m_window = XCreateWindow(dpy, root, 0, 0, width, height, 0,
181                                                          CopyFromParent, InputOutput, visual, mask, &swa);
182         TCU_CHECK(m_window);
183
184         Atom deleteAtom = m_display.getDeleteAtom();
185         XSetWMProtocols(dpy, m_window, &deleteAtom, 1);
186         XSync(dpy,false);
187 }
188
189 void XlibWindow::setVisibility (bool visible)
190 {
191         ::Display*      dpy                     = m_display.getXDisplay();
192         int                     eventType       = None;
193         XEvent          event;
194
195         if (visible == m_visible)
196                 return;
197
198         if (visible)
199         {
200                 XMapWindow(dpy, m_window);
201                 eventType = MapNotify;
202         }
203         else
204         {
205                 XUnmapWindow(dpy, m_window);
206                 eventType = UnmapNotify;
207         }
208
209         // We are only interested about exposure/structure notify events, not user input
210         XSelectInput(dpy, m_window, ExposureMask | StructureNotifyMask);
211
212         do
213         {
214                 XNextEvent(dpy, &event);
215         } while (event.type != eventType);
216
217         m_visible = visible;
218 }
219
220 void XlibWindow::getDimensions (int* width, int* height) const
221 {
222         int x, y;
223         ::Window root;
224         unsigned width_, height_, borderWidth, depth;
225
226         XGetGeometry(m_display.getXDisplay(), m_window, &root, &x, &y, &width_, &height_, &borderWidth, &depth);
227         if (width != DE_NULL)
228                 *width = static_cast<int>(width_);
229         if (height != DE_NULL)
230                 *height = static_cast<int>(height_);
231 }
232
233 void XlibWindow::setDimensions (int width, int height)
234 {
235         const unsigned int      mask            = CWWidth | CWHeight;
236         XWindowChanges          changes;
237         ::Display*                      dpy                     = m_display.getXDisplay();
238         XEvent                          myevent;
239         changes.width   = width;
240         changes.height  = height;
241         XConfigureWindow(dpy, m_window, mask, &changes);
242         XFlush(dpy);
243
244         for(;;)
245         {
246                 XNextEvent(dpy, &myevent);
247                 if (myevent.type == ConfigureNotify)
248                         break;
249         }
250 }
251
252 void XlibWindow::processEvents (void)
253 {
254         // A bit of a hack, since we don't really handle all the events.
255         m_display.processEvents();
256 }
257
258 XlibWindow::~XlibWindow (void)
259 {
260         XDestroyWindow(m_display.getXDisplay(), m_window);
261         if (m_colormap != None)
262                 XFreeColormap(m_display.getXDisplay(), m_colormap);
263 }
264
265 } // x11
266 } // tcu