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