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 X11Egl Display Factory.
22 *//*--------------------------------------------------------------------*/
23
24 #include "tcuLnxX11EglDisplayFactory.hpp"
25 #include "tcuLnxX11.hpp"
26 #include "egluGLContextFactory.hpp"
27 #include "eglwLibrary.hpp"
28 #include "eglwFunctions.hpp"
29 #include "eglwEnums.hpp"
30 #include "deUniquePtr.hpp"
31
32 namespace tcu
33 {
34 namespace lnx
35 {
36 namespace x11
37 {
38 namespace egl
39 {
40
41 typedef ::Display* EGLNativeDisplayType;
42 typedef ::Pixmap EGLNativePixmapType;
43 typedef ::Window EGLNativeWindowType;
44
45 DE_STATIC_ASSERT(sizeof(EGLNativeDisplayType) <= sizeof(eglw::EGLNativeDisplayType));
46 DE_STATIC_ASSERT(sizeof(EGLNativePixmapType) <= sizeof(eglw::EGLNativePixmapType));
47 DE_STATIC_ASSERT(sizeof(EGLNativeWindowType) <= sizeof(eglw::EGLNativeWindowType));
48
49 extern "C"
50 {
51
52 typedef EGLW_APICALL eglw::EGLDisplay (EGLW_APIENTRY* eglX11GetDisplayFunc) (EGLNativeDisplayType display_id);
53 typedef EGLW_APICALL eglw::EGLBoolean (EGLW_APIENTRY* eglX11CopyBuffersFunc) (eglw::EGLDisplay dpy, eglw::EGLSurface surface, EGLNativePixmapType target);
54 typedef EGLW_APICALL eglw::EGLSurface (EGLW_APIENTRY* eglX11CreatePixmapSurfaceFunc) (eglw::EGLDisplay dpy, eglw::EGLConfig config, EGLNativePixmapType pixmap, const eglw::EGLint* attrib_list);
55 typedef EGLW_APICALL eglw::EGLSurface (EGLW_APIENTRY* eglX11CreateWindowSurfaceFunc) (eglw::EGLDisplay dpy, eglw::EGLConfig config, EGLNativeWindowType win, const eglw::EGLint* attrib_list);
56
57 }
58
59 using std::string;
60
61 using de::MovePtr;
62 using de::UniquePtr;
63 using glu::ContextFactory;
64 using eglu::GLContextFactory;
65 using eglu::NativeDisplay;
66 using eglu::NativeDisplayFactory;
67 using eglu::NativeWindow;
68 using eglu::NativeWindowFactory;
69 using eglu::NativePixmap;
70 using eglu::NativePixmapFactory;
71 using eglu::WindowParams;
72 using tcu::TextureLevel;
73
74 class Library : public eglw::DefaultLibrary
75 {
76 public:
Library(void)77 Library (void)
78 : eglw::DefaultLibrary("libEGL.so")
79 {
80 }
81
copyBuffers(eglw::EGLDisplay dpy,eglw::EGLSurface surface,eglw::EGLNativePixmapType target) const82 eglw::EGLBoolean copyBuffers (eglw::EGLDisplay dpy, eglw::EGLSurface surface, eglw::EGLNativePixmapType target) const
83 {
84 return (m_egl.copyBuffers)(dpy, surface, reinterpret_cast<EGLNativePixmapType*>(target));
85 }
86
createPixmapSurface(eglw::EGLDisplay dpy,eglw::EGLConfig config,eglw::EGLNativePixmapType pixmap,const eglw::EGLint * attrib_list) const87 eglw::EGLSurface createPixmapSurface (eglw::EGLDisplay dpy, eglw::EGLConfig config, eglw::EGLNativePixmapType pixmap, const eglw::EGLint *attrib_list) const
88 {
89 return (m_egl.createPixmapSurface)(dpy, config, reinterpret_cast<EGLNativePixmapType*>(pixmap), attrib_list);
90 }
91
createWindowSurface(eglw::EGLDisplay dpy,eglw::EGLConfig config,eglw::EGLNativeWindowType win,const eglw::EGLint * attrib_list) const92 eglw::EGLSurface createWindowSurface (eglw::EGLDisplay dpy, eglw::EGLConfig config, eglw::EGLNativeWindowType win, const eglw::EGLint *attrib_list) const
93 {
94 return (m_egl.createWindowSurface)(dpy, config, reinterpret_cast<EGLNativeWindowType*>(win), attrib_list);
95 }
96
getDisplay(eglw::EGLNativeDisplayType display_id) const97 eglw::EGLDisplay getDisplay (eglw::EGLNativeDisplayType display_id) const
98 {
99 return ((eglX11GetDisplayFunc)m_egl.getDisplay)(reinterpret_cast<EGLNativeDisplayType>(display_id));
100 }
101 };
102
103 class Display : public NativeDisplay
104 {
105 public:
106 static const Capability CAPABILITIES = Capability(CAPABILITY_GET_DISPLAY_LEGACY |
107 CAPABILITY_GET_DISPLAY_PLATFORM_EXT);
108
Display(MovePtr<XlibDisplay> x11Display)109 Display (MovePtr<XlibDisplay> x11Display)
110 : NativeDisplay (CAPABILITIES,
111 EGL_PLATFORM_X11_EXT,
112 "EGL_EXT_platform_x11")
113 , m_display (x11Display) {}
114
getPlatformNative(void)115 void* getPlatformNative (void) { return m_display->getXDisplay(); }
getPlatformExtension(void)116 eglw::EGLNativeDisplayType getPlatformExtension (void) { return reinterpret_cast<eglw::EGLNativeDisplayType>(m_display->getXDisplay()); }
getLegacyNative(void)117 eglw::EGLNativeDisplayType getLegacyNative (void) { return reinterpret_cast<eglw::EGLNativeDisplayType>(m_display->getXDisplay()); }
118
getX11Display(void)119 XlibDisplay& getX11Display (void) { return *m_display; }
getLibrary(void) const120 const eglw::Library& getLibrary (void) const { return m_library; }
getPlatformAttributes(void) const121 const eglw::EGLAttrib* getPlatformAttributes (void) const { return DE_NULL; }
122
123 private:
124 UniquePtr<XlibDisplay> m_display;
125 Library m_library;
126 };
127
128 class Window : public NativeWindow
129 {
130 public:
131 static const Capability CAPABILITIES = Capability(CAPABILITY_CREATE_SURFACE_LEGACY |
132 CAPABILITY_CREATE_SURFACE_PLATFORM |
133 CAPABILITY_CREATE_SURFACE_PLATFORM_EXTENSION |
134 CAPABILITY_GET_SURFACE_SIZE |
135 CAPABILITY_SET_SURFACE_SIZE |
136 CAPABILITY_GET_SCREEN_SIZE);
137
138 Window (Display& display,
139 const WindowParams& params,
140 Visual* visual);
141
getLegacyNative(void)142 eglw::EGLNativeWindowType getLegacyNative (void) { return reinterpret_cast<eglw::EGLNativeWindowType>(m_window.getXID()); }
getPlatformExtension(void)143 void* getPlatformExtension (void) { return &m_window.getXID(); }
getPlatformNative(void)144 void* getPlatformNative (void) { return &m_window.getXID(); }
145
146 IVec2 getSurfaceSize (void) const;
147 void setSurfaceSize (IVec2 size);
getScreenSize(void) const148 IVec2 getScreenSize (void) const { return getSurfaceSize(); }
149
150 private:
151 XlibWindow m_window;
152 };
153
Window(Display & display,const WindowParams & params,Visual * visual)154 Window::Window (Display& display, const WindowParams& params, Visual* visual)
155 : NativeWindow (CAPABILITIES)
156 , m_window (display.getX11Display(), params.width, params.height, visual)
157 {
158 m_window.setVisibility((params.visibility != WindowParams::VISIBILITY_HIDDEN));
159 }
160
getSurfaceSize(void) const161 IVec2 Window::getSurfaceSize (void) const
162 {
163 IVec2 ret;
164 m_window.getDimensions(&ret.x(), &ret.y());
165 return ret;
166 }
167
setSurfaceSize(IVec2 size)168 void Window::setSurfaceSize (IVec2 size)
169 {
170 m_window.setDimensions(size.x(), size.y());
171 }
172
173 class WindowFactory : public NativeWindowFactory
174 {
175 public:
176 WindowFactory (void);
177
178 NativeWindow* createWindow (NativeDisplay* nativeDisplay,
179 const WindowParams& params) const;
180
181 NativeWindow* createWindow (NativeDisplay* nativeDisplay,
182 eglw::EGLDisplay display,
183 eglw::EGLConfig config,
184 const eglw::EGLAttrib* attribList,
185 const WindowParams& params) const;
186 };
187
WindowFactory(void)188 WindowFactory::WindowFactory (void)
189 : NativeWindowFactory ("window", "X11 Window", Window::CAPABILITIES)
190 {
191 }
192
createWindow(NativeDisplay * nativeDisplay,const WindowParams & params) const193 NativeWindow* WindowFactory::createWindow (NativeDisplay* nativeDisplay,
194 const WindowParams& params) const
195 {
196 Display& display = *dynamic_cast<Display*>(nativeDisplay);
197
198 return new Window(display, params, DE_NULL);
199 }
200
createWindow(NativeDisplay * nativeDisplay,eglw::EGLDisplay eglDisplay,eglw::EGLConfig config,const eglw::EGLAttrib * attribList,const WindowParams & params) const201 NativeWindow* WindowFactory::createWindow (NativeDisplay* nativeDisplay,
202 eglw::EGLDisplay eglDisplay,
203 eglw::EGLConfig config,
204 const eglw::EGLAttrib* attribList,
205 const WindowParams& params) const
206 {
207 DE_UNREF(attribList);
208
209 Display& display = *dynamic_cast<Display*>(nativeDisplay);
210 eglw::EGLint visualID = 0;
211 ::Visual* visual = DE_NULL;
212 nativeDisplay->getLibrary().getConfigAttrib(eglDisplay, config, EGL_NATIVE_VISUAL_ID, &visualID);
213
214 if (visualID != 0)
215 visual = display.getX11Display().getVisual(visualID);
216
217 return new Window(display, params, visual);
218 }
219
220 #if 0
221 class Pixmap : public NativePixmap
222 {
223 public:
224 enum {
225 CAPABILITIES = (CAPABILITY_CREATE_SURFACE_LEGACY |
226 CAPABILITY_CREATE_SURFACE_PLATFORM_EXTENSION |
227 CAPABILITY_READ_PIXELS)
228 };
229
230 Pixmap (MovePtr<x11::Pixmap> x11Pixmap)
231 : NativePixmap (CAPABILITIES)
232 , m_pixmap (x11Pixmap) {}
233
234 void* getPlatformNative (void) { return &m_pixmap.getXID(); }
235 void readPixels (TextureLevel* dst);
236
237 private:
238 UniquePtr<x11::Pixmap> m_pixmap;
239 };
240
241 class PixmapFactory : public NativePixmapFactory
242 {
243 public:
244 PixmapFactory (void)
245 : NativePixmapFactory ("pixmap", "X11 Pixmap", Pixmap::CAPABILITIES) {}
246
247 NativePixmap* createPixmap (NativeDisplay* nativeDisplay,
248 int width,
249 int height) const;
250 };
251
252 NativePixmap* PixmapFactory::createPixmap (NativeDisplay* nativeDisplay,
253 int width,
254 int height) const
255
256 {
257 Display* display = dynamic_cast<Display*>(nativeDisplay);
258 MovePtr<x11::Pixmap> x11Pixmap (new x11::Pixmap(display->getX11Display(),
259 width, height));
260 return new Pixmap(x11Pixmap);
261 }
262 #endif
263
264 class DisplayFactory : public NativeDisplayFactory
265 {
266 public:
267 DisplayFactory (EventState& eventState);
268
269 NativeDisplay* createDisplay (const eglw::EGLAttrib* attribList) const;
270
271 private:
272 EventState& m_eventState;
273 };
274
DisplayFactory(EventState & eventState)275 DisplayFactory::DisplayFactory (EventState& eventState)
276 : NativeDisplayFactory ("x11", "Native X11 Display",
277 Display::CAPABILITIES,
278 EGL_PLATFORM_X11_SCREEN_EXT,
279 "EGL_EXT_platform_x11")
280 , m_eventState (eventState)
281 {
282 m_nativeWindowRegistry.registerFactory(new WindowFactory());
283 // m_nativePixmapRegistry.registerFactory(new PixmapFactory());
284 }
285
createDisplay(const eglw::EGLAttrib * attribList) const286 NativeDisplay* DisplayFactory::createDisplay (const eglw::EGLAttrib* attribList) const
287 {
288 DE_UNREF(attribList);
289
290 //! \todo [2014-03-18 lauri] Somehow make the display configurable from command line
291 MovePtr<XlibDisplay> x11Display (new XlibDisplay(m_eventState, DE_NULL));
292
293 return new Display(x11Display);
294 }
295
createDisplayFactory(EventState & eventState)296 NativeDisplayFactory* createDisplayFactory (EventState& eventState)
297 {
298 return new DisplayFactory(eventState);
299 }
300
301 } // egl
302 } // x11
303 } // lnx
304 } // tcu
305