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: 77 Library (void) 78 : eglw::DefaultLibrary("libEGL.so") 79 { 80 } 81 82 eglw::EGLBoolean copyBuffers (eglw::EGLDisplay dpy, eglw::EGLSurface surface, eglw::EGLNativePixmapType target) const 83 { 84 return ((eglX11CopyBuffersFunc)m_egl.copyBuffers)(dpy, surface, reinterpret_cast<EGLNativePixmapType>(target)); 85 } 86 87 eglw::EGLSurface createPixmapSurface (eglw::EGLDisplay dpy, eglw::EGLConfig config, eglw::EGLNativePixmapType pixmap, const eglw::EGLint *attrib_list) const 88 { 89 return ((eglX11CreatePixmapSurfaceFunc)m_egl.createPixmapSurface)(dpy, config, reinterpret_cast<EGLNativePixmapType>(pixmap), attrib_list); 90 } 91 92 eglw::EGLSurface createWindowSurface (eglw::EGLDisplay dpy, eglw::EGLConfig config, eglw::EGLNativeWindowType win, const eglw::EGLint *attrib_list) const 93 { 94 return ((eglX11CreateWindowSurfaceFunc)m_egl.createWindowSurface)(dpy, config, reinterpret_cast<EGLNativeWindowType>(win), attrib_list); 95 } 96 97 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); 108 109 Display (MovePtr<XlibDisplay> x11Display) 110 : NativeDisplay (CAPABILITIES, 111 EGL_PLATFORM_X11_EXT, 112 "EGL_EXT_platform_x11") 113 , m_display (x11Display) {} 114 115 void* getPlatformNative (void) { return m_display->getXDisplay(); } 116 eglw::EGLNativeDisplayType getLegacyNative (void) { return reinterpret_cast<eglw::EGLNativeDisplayType>(m_display->getXDisplay()); } 117 118 XlibDisplay& getX11Display (void) { return *m_display; } 119 const eglw::Library& getLibrary (void) const { return m_library; } 120 const eglw::EGLAttrib* getPlatformAttributes (void) const { return DE_NULL; } 121 122 private: 123 UniquePtr<XlibDisplay> m_display; 124 Library m_library; 125 }; 126 127 class Window : public NativeWindow 128 { 129 public: 130 static const Capability CAPABILITIES = Capability(CAPABILITY_CREATE_SURFACE_LEGACY | 131 CAPABILITY_CREATE_SURFACE_PLATFORM | 132 CAPABILITY_GET_SURFACE_SIZE | 133 CAPABILITY_SET_SURFACE_SIZE | 134 CAPABILITY_GET_SCREEN_SIZE); 135 136 Window (Display& display, 137 const WindowParams& params, 138 Visual* visual); 139 140 eglw::EGLNativeWindowType getLegacyNative (void) { return reinterpret_cast<eglw::EGLNativeWindowType>(m_window.getXID()); } 141 void* getPlatformNative (void) { return &m_window.getXID(); } 142 143 IVec2 getSurfaceSize (void) const; 144 void setSurfaceSize (IVec2 size); 145 IVec2 getScreenSize (void) const { return getSurfaceSize(); } 146 147 private: 148 XlibWindow m_window; 149 }; 150 151 Window::Window (Display& display, const WindowParams& params, Visual* visual) 152 : NativeWindow (CAPABILITIES) 153 , m_window (display.getX11Display(), params.width, params.height, visual) 154 { 155 m_window.setVisibility((params.visibility != WindowParams::VISIBILITY_HIDDEN)); 156 } 157 158 IVec2 Window::getSurfaceSize (void) const 159 { 160 IVec2 ret; 161 m_window.getDimensions(&ret.x(), &ret.y()); 162 return ret; 163 } 164 165 void Window::setSurfaceSize (IVec2 size) 166 { 167 m_window.setDimensions(size.x(), size.y()); 168 } 169 170 class WindowFactory : public NativeWindowFactory 171 { 172 public: 173 WindowFactory (void); 174 175 NativeWindow* createWindow (NativeDisplay* nativeDisplay, 176 const WindowParams& params) const; 177 178 NativeWindow* createWindow (NativeDisplay* nativeDisplay, 179 eglw::EGLDisplay display, 180 eglw::EGLConfig config, 181 const eglw::EGLAttrib* attribList, 182 const WindowParams& params) const; 183 }; 184 185 WindowFactory::WindowFactory (void) 186 : NativeWindowFactory ("window", "X11 Window", Window::CAPABILITIES) 187 { 188 } 189 190 NativeWindow* WindowFactory::createWindow (NativeDisplay* nativeDisplay, 191 const WindowParams& params) const 192 { 193 Display& display = *dynamic_cast<Display*>(nativeDisplay); 194 195 return new Window(display, params, DE_NULL); 196 } 197 198 NativeWindow* WindowFactory::createWindow (NativeDisplay* nativeDisplay, 199 eglw::EGLDisplay eglDisplay, 200 eglw::EGLConfig config, 201 const eglw::EGLAttrib* attribList, 202 const WindowParams& params) const 203 { 204 DE_UNREF(attribList); 205 206 Display& display = *dynamic_cast<Display*>(nativeDisplay); 207 eglw::EGLint visualID = 0; 208 ::Visual* visual = DE_NULL; 209 nativeDisplay->getLibrary().getConfigAttrib(eglDisplay, config, EGL_NATIVE_VISUAL_ID, &visualID); 210 211 if (visualID != 0) 212 visual = display.getX11Display().getVisual(visualID); 213 214 return new Window(display, params, visual); 215 } 216 217 #if 0 218 class Pixmap : public NativePixmap 219 { 220 public: 221 enum { 222 CAPABILITIES = (CAPABILITY_CREATE_SURFACE_LEGACY | 223 CAPABILITY_CREATE_SURFACE_PLATFORM | 224 CAPABILITY_READ_PIXELS) 225 }; 226 227 Pixmap (MovePtr<x11::Pixmap> x11Pixmap) 228 : NativePixmap (CAPABILITIES) 229 , m_pixmap (x11Pixmap) {} 230 231 void* getPlatformNative (void) { return &m_pixmap.getXID(); } 232 void readPixels (TextureLevel* dst); 233 234 private: 235 UniquePtr<x11::Pixmap> m_pixmap; 236 }; 237 238 class PixmapFactory : public NativePixmapFactory 239 { 240 public: 241 PixmapFactory (void) 242 : NativePixmapFactory ("pixmap", "X11 Pixmap", Pixmap::CAPABILITIES) {} 243 244 NativePixmap* createPixmap (NativeDisplay* nativeDisplay, 245 int width, 246 int height) const; 247 }; 248 249 NativePixmap* PixmapFactory::createPixmap (NativeDisplay* nativeDisplay, 250 int width, 251 int height) const 252 253 { 254 Display* display = dynamic_cast<Display*>(nativeDisplay); 255 MovePtr<x11::Pixmap> x11Pixmap (new x11::Pixmap(display->getX11Display(), 256 width, height)); 257 return new Pixmap(x11Pixmap); 258 } 259 #endif 260 261 class DisplayFactory : public NativeDisplayFactory 262 { 263 public: 264 DisplayFactory (EventState& eventState); 265 266 NativeDisplay* createDisplay (const eglw::EGLAttrib* attribList) const; 267 268 private: 269 EventState& m_eventState; 270 }; 271 272 DisplayFactory::DisplayFactory (EventState& eventState) 273 : NativeDisplayFactory ("x11", "Native X11 Display", 274 Display::CAPABILITIES, 275 EGL_PLATFORM_X11_SCREEN_EXT, 276 "EGL_EXT_platform_x11") 277 , m_eventState (eventState) 278 { 279 m_nativeWindowRegistry.registerFactory(new WindowFactory()); 280 // m_nativePixmapRegistry.registerFactory(new PixmapFactory()); 281 } 282 283 NativeDisplay* DisplayFactory::createDisplay (const eglw::EGLAttrib* attribList) const 284 { 285 DE_UNREF(attribList); 286 287 //! \todo [2014-03-18 lauri] Somehow make the display configurable from command line 288 MovePtr<XlibDisplay> x11Display (new XlibDisplay(m_eventState, DE_NULL)); 289 290 return new Display(x11Display); 291 } 292 293 NativeDisplayFactory* createDisplayFactory (EventState& eventState) 294 { 295 return new DisplayFactory(eventState); 296 } 297 298 } // egl 299 } // x11 300 } // lnx 301 } // tcu 302