1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief GL context factory using EGL.
22 *//*--------------------------------------------------------------------*/
24 #include "egluGLContextFactory.hpp"
26 #include "tcuRenderTarget.hpp"
27 #include "tcuPlatform.hpp"
28 #include "tcuCommandLine.hpp"
30 #include "gluDefs.hpp"
32 #include "egluDefs.hpp"
33 #include "egluUtil.hpp"
34 #include "egluGLUtil.hpp"
35 #include "egluNativeWindow.hpp"
36 #include "egluNativePixmap.hpp"
37 #include "egluStrUtil.hpp"
39 #include "eglwLibrary.hpp"
40 #include "eglwEnums.hpp"
42 #include "glwInitFunctions.hpp"
43 #include "glwInitES20Direct.hpp"
44 #include "glwInitES30Direct.hpp"
45 #include "glwInitES31Direct.hpp"
46 #include "glwInitES32Direct.hpp"
48 #include "deDynamicLibrary.hpp"
49 #include "deSTLUtil.hpp"
58 // \todo [2014-03-12 pyry] Use command line arguments for libraries?
60 // Default library names
61 #if !defined(DEQP_GLES2_LIBRARY_PATH)
62 # if (DE_OS == DE_OS_WIN32)
63 # define DEQP_GLES2_LIBRARY_PATH "libGLESv2.dll"
65 # define DEQP_GLES2_LIBRARY_PATH "libGLESv2.so"
69 #if !defined(DEQP_GLES3_LIBRARY_PATH)
70 # define DEQP_GLES3_LIBRARY_PATH DEQP_GLES2_LIBRARY_PATH
73 #if !defined(DEQP_OPENGL_LIBRARY_PATH)
74 # if (DE_OS == DE_OS_WIN32)
75 # define DEQP_OPENGL_LIBRARY_PATH "opengl32.dll"
77 # define DEQP_OPENGL_LIBRARY_PATH "libGL.so"
91 DEFAULT_OFFSCREEN_WIDTH = 512,
92 DEFAULT_OFFSCREEN_HEIGHT = 512
95 class GetProcFuncLoader : public glw::FunctionLoader
98 GetProcFuncLoader (const Library& egl)
103 glw::GenericFuncType get (const char* name) const
105 return (glw::GenericFuncType)m_egl.getProcAddress(name);
109 const Library& m_egl;
112 class DynamicFuncLoader : public glw::FunctionLoader
115 DynamicFuncLoader (de::DynamicLibrary* library)
120 glw::GenericFuncType get (const char* name) const
122 return (glw::GenericFuncType)m_library->getFunction(name);
126 de::DynamicLibrary* m_library;
129 class RenderContext : public GLRenderContext
132 RenderContext (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config);
133 virtual ~RenderContext (void);
135 virtual glu::ContextType getType (void) const { return m_renderConfig.type; }
136 virtual const glw::Functions& getFunctions (void) const { return m_glFunctions; }
137 virtual const tcu::RenderTarget& getRenderTarget (void) const { return m_glRenderTarget; }
138 virtual void postIterate (void);
140 virtual EGLDisplay getEGLDisplay (void) const { return m_eglDisplay; }
141 virtual EGLContext getEGLContext (void) const { return m_eglContext; }
142 virtual EGLConfig getEGLConfig (void) const { return m_eglConfig; }
143 virtual const eglw::Library& getLibrary (void) const { return m_display->getLibrary(); }
145 virtual eglw::GenericFuncType getProcAddress (const char* name) const;
147 virtual void makeCurrent (void);
150 void create (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config);
153 const glu::RenderConfig m_renderConfig;
154 const NativeWindowFactory* const m_nativeWindowFactory; // Stored in case window must be re-created
156 NativeDisplay* m_display;
157 NativeWindow* m_window;
158 NativePixmap* m_pixmap;
160 EGLDisplay m_eglDisplay;
161 EGLConfig m_eglConfig;
162 EGLSurface m_eglSurface;
163 EGLContext m_eglContext;
165 tcu::RenderTarget m_glRenderTarget;
166 de::DynamicLibrary* m_dynamicGLLibrary;
167 glw::Functions m_glFunctions;
170 RenderContext::RenderContext (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config)
171 : m_renderConfig (config)
172 , m_nativeWindowFactory (windowFactory)
173 , m_display (DE_NULL)
177 , m_eglDisplay (EGL_NO_DISPLAY)
178 , m_eglSurface (EGL_NO_SURFACE)
179 , m_eglContext (EGL_NO_CONTEXT)
181 , m_dynamicGLLibrary (DE_NULL)
183 DE_ASSERT(displayFactory);
187 create(displayFactory, windowFactory, pixmapFactory, config);
196 RenderContext::~RenderContext(void)
204 // destroy() calls EGL functions that are checked and may throw exceptions
210 delete m_dynamicGLLibrary;
213 static WindowParams::Visibility getNativeWindowVisibility (glu::RenderConfig::Visibility visibility)
215 using glu::RenderConfig;
219 case RenderConfig::VISIBILITY_HIDDEN: return WindowParams::VISIBILITY_HIDDEN;
220 case RenderConfig::VISIBILITY_VISIBLE: return WindowParams::VISIBILITY_VISIBLE;
221 case RenderConfig::VISIBILITY_FULLSCREEN: return WindowParams::VISIBILITY_FULLSCREEN;
223 DE_ASSERT((int)visibility == RenderConfig::DONT_CARE);
224 return WindowParams::VISIBILITY_DONT_CARE;
228 typedef std::pair<NativeWindow*, EGLSurface> WindowSurfacePair;
229 typedef std::pair<NativePixmap*, EGLSurface> PixmapSurfacePair;
231 WindowSurfacePair createWindow (NativeDisplay* nativeDisplay, const NativeWindowFactory* windowFactory, EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig& config)
233 const int width = (config.width == glu::RenderConfig::DONT_CARE ? WindowParams::SIZE_DONT_CARE : config.width);
234 const int height = (config.height == glu::RenderConfig::DONT_CARE ? WindowParams::SIZE_DONT_CARE : config.height);
235 const WindowParams::Visibility visibility = getNativeWindowVisibility(config.windowVisibility);
236 NativeWindow* nativeWindow = DE_NULL;
237 EGLSurface surface = EGL_NO_SURFACE;
238 const EGLAttrib attribList[] = { EGL_NONE };
240 nativeWindow = windowFactory->createWindow(nativeDisplay, eglDisplay, eglConfig, &attribList[0], WindowParams(width, height, visibility));
244 surface = eglu::createWindowSurface(*nativeDisplay, *nativeWindow, eglDisplay, eglConfig, attribList);
252 return WindowSurfacePair(nativeWindow, surface);
255 PixmapSurfacePair createPixmap (NativeDisplay* nativeDisplay, const NativePixmapFactory* pixmapFactory, EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig& config)
257 const int width = (config.width == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_WIDTH : config.width);
258 const int height = (config.height == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_HEIGHT : config.height);
259 NativePixmap* nativePixmap = DE_NULL;
260 EGLSurface surface = EGL_NO_SURFACE;
261 const EGLAttrib attribList[] = { EGL_NONE };
263 nativePixmap = pixmapFactory->createPixmap(nativeDisplay, eglDisplay, eglConfig, &attribList[0], width, height);
267 surface = eglu::createPixmapSurface(*nativeDisplay, *nativePixmap, eglDisplay, eglConfig, attribList);
275 return PixmapSurfacePair(nativePixmap, surface);
278 EGLSurface createPBuffer (const Library& egl, EGLDisplay display, EGLConfig eglConfig, const glu::RenderConfig& config)
280 const int width = (config.width == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_WIDTH : config.width);
281 const int height = (config.height == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_HEIGHT : config.height);
283 const EGLint attribList[] =
290 surface = egl.createPbufferSurface(display, eglConfig, &(attribList[0]));
291 EGLU_CHECK_MSG(egl, "eglCreatePbufferSurface()");
296 void RenderContext::makeCurrent (void)
298 const Library& egl = m_display->getLibrary();
300 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
303 glw::GenericFuncType RenderContext::getProcAddress (const char* name) const
305 return (glw::GenericFuncType)m_display->getLibrary().getProcAddress(name);
308 void RenderContext::create (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config)
310 glu::RenderConfig::SurfaceType surfaceType = config.surfaceType;
312 DE_ASSERT(displayFactory);
314 m_display = displayFactory->createDisplay();
315 m_eglDisplay = eglu::getDisplay(*m_display);
317 const Library& egl = m_display->getLibrary();
322 EGLU_CHECK_CALL(egl, initialize(m_eglDisplay, &major, &minor));
325 m_eglConfig = chooseConfig(egl, m_eglDisplay, config);
327 if (surfaceType == glu::RenderConfig::SURFACETYPE_DONT_CARE)
329 // Choose based on what selected configuration supports
330 const EGLint supportedTypes = eglu::getConfigAttribInt(egl, m_eglDisplay, m_eglConfig, EGL_SURFACE_TYPE);
332 if ((supportedTypes & EGL_WINDOW_BIT) != 0)
333 surfaceType = glu::RenderConfig::SURFACETYPE_WINDOW;
334 else if ((supportedTypes & EGL_PBUFFER_BIT) != 0)
335 surfaceType = glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC;
336 else if ((supportedTypes & EGL_PIXMAP_BIT) != 0)
337 surfaceType = glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE;
339 throw tcu::NotSupportedError("Selected EGL config doesn't support any surface types", DE_NULL, __FILE__, __LINE__);
344 case glu::RenderConfig::SURFACETYPE_WINDOW:
348 const WindowSurfacePair windowSurface = createWindow(m_display, windowFactory, m_eglDisplay, m_eglConfig, config);
349 m_window = windowSurface.first;
350 m_eglSurface = windowSurface.second;
353 throw tcu::NotSupportedError("EGL platform doesn't support windows", DE_NULL, __FILE__, __LINE__);
357 case glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE:
361 const PixmapSurfacePair pixmapSurface = createPixmap(m_display, pixmapFactory, m_eglDisplay, m_eglConfig, config);
362 m_pixmap = pixmapSurface.first;
363 m_eglSurface = pixmapSurface.second;
366 throw tcu::NotSupportedError("EGL platform doesn't support pixmaps", DE_NULL, __FILE__, __LINE__);
370 case glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC:
371 m_eglSurface = createPBuffer(egl, m_eglDisplay, m_eglConfig, config);
375 throw tcu::InternalError("Invalid surface type");
378 m_eglContext = createGLContext(egl, m_eglDisplay, m_eglConfig, config.type, config.resetNotificationStrategy);
380 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
382 // Init core functions
384 if (hasExtension(egl, m_eglDisplay, "EGL_KHR_get_all_proc_addresses"))
386 // Use eglGetProcAddress() for core functions
387 GetProcFuncLoader funcLoader(egl);
388 glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
390 #if defined(DEQP_GLES2_DIRECT_LINK)
391 else if (config.type.getAPI() == glu::ApiType::es(2,0))
393 glw::initES20Direct(&m_glFunctions);
396 #if defined(DEQP_GLES3_DIRECT_LINK)
397 else if (config.type.getAPI() == glu::ApiType::es(3,0))
399 glw::initES30Direct(&m_glFunctions);
402 #if defined(DEQP_GLES31_DIRECT_LINK)
403 else if (config.type.getAPI() == glu::ApiType::es(3,1))
405 glw::initES31Direct(&m_glFunctions);
408 #if defined(DEQP_GLES32_DIRECT_LINK)
409 else if (config.type.getAPI() == glu::ApiType::es(3,2))
411 glw::initES32Direct(&m_glFunctions);
416 const char* libraryPath = DE_NULL;
418 if (glu::isContextTypeES(config.type))
420 if (config.type.getMinorVersion() <= 2)
421 libraryPath = DEQP_GLES2_LIBRARY_PATH;
423 libraryPath = DEQP_GLES3_LIBRARY_PATH;
426 libraryPath = DEQP_OPENGL_LIBRARY_PATH;
428 m_dynamicGLLibrary = new de::DynamicLibrary(libraryPath);
430 DynamicFuncLoader funcLoader(m_dynamicGLLibrary);
431 glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
434 // Init extension functions
436 GetProcFuncLoader extLoader(egl);
437 glu::initExtensionFunctions(&m_glFunctions, &extLoader, config.type.getAPI());
441 EGLint width, height, depthBits, stencilBits, numSamples;
442 tcu::PixelFormat pixelFmt;
444 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &width);
445 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &height);
447 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_RED_SIZE, &pixelFmt.redBits);
448 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_GREEN_SIZE, &pixelFmt.greenBits);
449 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_BLUE_SIZE, &pixelFmt.blueBits);
450 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_ALPHA_SIZE, &pixelFmt.alphaBits);
452 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_DEPTH_SIZE, &depthBits);
453 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_STENCIL_SIZE, &stencilBits);
454 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_SAMPLES, &numSamples);
456 EGLU_CHECK_MSG(egl, "Failed to query config attributes");
458 m_glRenderTarget = tcu::RenderTarget(width, height, pixelFmt, depthBits, stencilBits, numSamples);
462 void RenderContext::destroy (void)
464 if (m_eglDisplay != EGL_NO_DISPLAY)
466 const Library& egl = m_display->getLibrary();
468 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
470 if (m_eglSurface != EGL_NO_SURFACE)
471 EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
473 if (m_eglContext != EGL_NO_CONTEXT)
474 EGLU_CHECK_CALL(egl, destroyContext(m_eglDisplay, m_eglContext));
476 EGLU_CHECK_CALL(egl, terminate(m_eglDisplay));
478 m_eglDisplay = EGL_NO_DISPLAY;
479 m_eglSurface = EGL_NO_SURFACE;
480 m_eglContext = EGL_NO_CONTEXT;
486 delete m_dynamicGLLibrary;
491 m_dynamicGLLibrary = DE_NULL;
494 void RenderContext::postIterate (void)
496 const Library& egl = m_display->getLibrary();
500 EGLBoolean swapOk = egl.swapBuffers(m_eglDisplay, m_eglSurface);
501 EGLint error = egl.getError();
502 const bool badWindow = error == EGL_BAD_SURFACE || error == EGL_BAD_NATIVE_WINDOW;
504 if (!swapOk && !badWindow)
505 throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
509 m_window->processEvents();
511 catch (const WindowDestroyedError&)
513 tcu::print("Warning: Window destroyed, recreating...\n");
515 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
516 EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
517 m_eglSurface = EGL_NO_SURFACE;
524 WindowSurfacePair windowSurface = createWindow(m_display, m_nativeWindowFactory, m_eglDisplay, m_eglConfig, m_renderConfig);
525 m_window = windowSurface.first;
526 m_eglSurface = windowSurface.second;
528 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
533 catch (const std::exception& e)
537 egl.makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
538 egl.destroySurface(m_eglDisplay, m_eglSurface);
539 m_eglSurface = EGL_NO_SURFACE;
545 throw tcu::ResourceError(string("Failed to re-create window: ") + e.what());
551 DE_ASSERT(badWindow);
552 throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
555 // Refresh dimensions
560 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &newWidth);
561 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &newHeight);
562 EGLU_CHECK_MSG(egl, "Failed to query window size");
564 if (newWidth != m_glRenderTarget.getWidth() ||
565 newHeight != m_glRenderTarget.getHeight())
567 tcu::print("Warning: Window size changed (%dx%d -> %dx%d), test results might be invalid!\n",
568 m_glRenderTarget.getWidth(), m_glRenderTarget.getHeight(), newWidth, newHeight);
570 m_glRenderTarget = tcu::RenderTarget(newWidth, newHeight,
571 m_glRenderTarget.getPixelFormat(),
572 m_glRenderTarget.getDepthBits(),
573 m_glRenderTarget.getStencilBits(),
574 m_glRenderTarget.getNumSamples());
579 m_glFunctions.flush();
584 GLContextFactory::GLContextFactory (const NativeDisplayFactoryRegistry& displayFactoryRegistry)
585 : glu::ContextFactory ("egl", "EGL OpenGL Context")
586 , m_displayFactoryRegistry (displayFactoryRegistry)
590 glu::RenderContext* GLContextFactory::createContext (const glu::RenderConfig& config, const tcu::CommandLine& cmdLine) const
592 const NativeDisplayFactory& displayFactory = selectNativeDisplayFactory(m_displayFactoryRegistry, cmdLine);
594 const NativeWindowFactory* windowFactory;
595 const NativePixmapFactory* pixmapFactory;
599 windowFactory = &selectNativeWindowFactory(displayFactory, cmdLine);
601 catch (const tcu::NotSupportedError&)
603 windowFactory = DE_NULL;
608 pixmapFactory = &selectNativePixmapFactory(displayFactory, cmdLine);
610 catch (const tcu::NotSupportedError&)
612 pixmapFactory = DE_NULL;
615 return new RenderContext(&displayFactory, windowFactory, pixmapFactory, config);