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"
46 #include "deDynamicLibrary.hpp"
47 #include "deSTLUtil.hpp"
56 // \todo [2014-03-12 pyry] Use command line arguments for libraries?
58 // Default library names
59 #if !defined(DEQP_GLES2_LIBRARY_PATH)
60 # if (DE_OS == DE_OS_WIN32)
61 # define DEQP_GLES2_LIBRARY_PATH "libGLESv2.dll"
63 # define DEQP_GLES2_LIBRARY_PATH "libGLESv2.so"
67 #if !defined(DEQP_GLES3_LIBRARY_PATH)
68 # define DEQP_GLES3_LIBRARY_PATH DEQP_GLES2_LIBRARY_PATH
71 #if !defined(DEQP_OPENGL_LIBRARY_PATH)
72 # if (DE_OS == DE_OS_WIN32)
73 # define DEQP_OPENGL_LIBRARY_PATH "opengl32.dll"
75 # define DEQP_OPENGL_LIBRARY_PATH "libGL.so"
89 DEFAULT_OFFSCREEN_WIDTH = 512,
90 DEFAULT_OFFSCREEN_HEIGHT = 512
93 class GetProcFuncLoader : public glw::FunctionLoader
96 GetProcFuncLoader (const Library& egl)
101 glw::GenericFuncType get (const char* name) const
103 return (glw::GenericFuncType)m_egl.getProcAddress(name);
107 const Library& m_egl;
110 class DynamicFuncLoader : public glw::FunctionLoader
113 DynamicFuncLoader (de::DynamicLibrary* library)
118 glw::GenericFuncType get (const char* name) const
120 return (glw::GenericFuncType)m_library->getFunction(name);
124 de::DynamicLibrary* m_library;
127 class RenderContext : public GLRenderContext
130 RenderContext (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config);
131 virtual ~RenderContext (void);
133 virtual glu::ContextType getType (void) const { return m_renderConfig.type; }
134 virtual const glw::Functions& getFunctions (void) const { return m_glFunctions; }
135 virtual const tcu::RenderTarget& getRenderTarget (void) const { return m_glRenderTarget; }
136 virtual void postIterate (void);
138 virtual EGLDisplay getEGLDisplay (void) const { return m_eglDisplay; }
139 virtual EGLContext getEGLContext (void) const { return m_eglContext; }
142 void create (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config);
145 const glu::RenderConfig m_renderConfig;
146 const NativeWindowFactory* const m_nativeWindowFactory; // Stored in case window must be re-created
148 NativeDisplay* m_display;
149 NativeWindow* m_window;
150 NativePixmap* m_pixmap;
152 EGLDisplay m_eglDisplay;
153 EGLConfig m_eglConfig;
154 EGLSurface m_eglSurface;
155 EGLContext m_eglContext;
157 tcu::RenderTarget m_glRenderTarget;
158 de::DynamicLibrary* m_dynamicGLLibrary;
159 glw::Functions m_glFunctions;
162 RenderContext::RenderContext (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config)
163 : m_renderConfig (config)
164 , m_nativeWindowFactory (windowFactory)
165 , m_display (DE_NULL)
169 , m_eglDisplay (EGL_NO_DISPLAY)
170 , m_eglSurface (EGL_NO_SURFACE)
171 , m_eglContext (EGL_NO_CONTEXT)
173 , m_dynamicGLLibrary (DE_NULL)
175 DE_ASSERT(displayFactory);
179 create(displayFactory, windowFactory, pixmapFactory, config);
188 RenderContext::~RenderContext(void)
196 // destroy() calls EGL functions that are checked and may throw exceptions
202 delete m_dynamicGLLibrary;
205 static WindowParams::Visibility getNativeWindowVisibility (glu::RenderConfig::Visibility visibility)
207 using glu::RenderConfig;
211 case RenderConfig::VISIBILITY_HIDDEN: return WindowParams::VISIBILITY_HIDDEN;
212 case RenderConfig::VISIBILITY_VISIBLE: return WindowParams::VISIBILITY_VISIBLE;
213 case RenderConfig::VISIBILITY_FULLSCREEN: return WindowParams::VISIBILITY_FULLSCREEN;
215 DE_ASSERT((int)visibility == RenderConfig::DONT_CARE);
216 return WindowParams::VISIBILITY_DONT_CARE;
220 typedef std::pair<NativeWindow*, EGLSurface> WindowSurfacePair;
221 typedef std::pair<NativePixmap*, EGLSurface> PixmapSurfacePair;
223 WindowSurfacePair createWindow (NativeDisplay* nativeDisplay, const NativeWindowFactory* windowFactory, EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig& config)
225 const int width = (config.width == glu::RenderConfig::DONT_CARE ? WindowParams::SIZE_DONT_CARE : config.width);
226 const int height = (config.height == glu::RenderConfig::DONT_CARE ? WindowParams::SIZE_DONT_CARE : config.height);
227 const WindowParams::Visibility visibility = getNativeWindowVisibility(config.windowVisibility);
228 NativeWindow* nativeWindow = DE_NULL;
229 EGLSurface surface = EGL_NO_SURFACE;
230 const EGLAttrib attribList[] = { EGL_NONE };
232 nativeWindow = windowFactory->createWindow(nativeDisplay, eglDisplay, eglConfig, &attribList[0], WindowParams(width, height, visibility));
236 surface = eglu::createWindowSurface(*nativeDisplay, *nativeWindow, eglDisplay, eglConfig, attribList);
244 return WindowSurfacePair(nativeWindow, surface);
247 PixmapSurfacePair createPixmap (NativeDisplay* nativeDisplay, const NativePixmapFactory* pixmapFactory, EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig& config)
249 const int width = (config.width == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_WIDTH : config.width);
250 const int height = (config.height == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_HEIGHT : config.height);
251 NativePixmap* nativePixmap = DE_NULL;
252 EGLSurface surface = EGL_NO_SURFACE;
253 const EGLAttrib attribList[] = { EGL_NONE };
255 nativePixmap = pixmapFactory->createPixmap(nativeDisplay, eglDisplay, eglConfig, &attribList[0], width, height);
259 surface = eglu::createPixmapSurface(*nativeDisplay, *nativePixmap, eglDisplay, eglConfig, attribList);
267 return PixmapSurfacePair(nativePixmap, surface);
270 EGLSurface createPBuffer (const Library& egl, EGLDisplay display, EGLConfig eglConfig, const glu::RenderConfig& config)
272 const int width = (config.width == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_WIDTH : config.width);
273 const int height = (config.height == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_HEIGHT : config.height);
275 const EGLint attribList[] =
282 surface = egl.createPbufferSurface(display, eglConfig, &(attribList[0]));
283 EGLU_CHECK_MSG(egl, "eglCreatePbufferSurface()");
288 void RenderContext::create (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config)
290 glu::RenderConfig::SurfaceType surfaceType = config.surfaceType;
292 DE_ASSERT(displayFactory);
294 m_display = displayFactory->createDisplay();
295 m_eglDisplay = eglu::getDisplay(*m_display);
297 const Library& egl = m_display->getLibrary();
302 EGLU_CHECK_CALL(egl, initialize(m_eglDisplay, &major, &minor));
305 m_eglConfig = chooseConfig(egl, m_eglDisplay, config);
307 if (surfaceType == glu::RenderConfig::SURFACETYPE_DONT_CARE)
309 // Choose based on what selected configuration supports
310 const EGLint supportedTypes = eglu::getConfigAttribInt(egl, m_eglDisplay, m_eglConfig, EGL_SURFACE_TYPE);
312 if ((supportedTypes & EGL_WINDOW_BIT) != 0)
313 surfaceType = glu::RenderConfig::SURFACETYPE_WINDOW;
314 else if ((supportedTypes & EGL_PBUFFER_BIT) != 0)
315 surfaceType = glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC;
316 else if ((supportedTypes & EGL_PIXMAP_BIT) != 0)
317 surfaceType = glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE;
319 throw tcu::NotSupportedError("Selected EGL config doesn't support any surface types", DE_NULL, __FILE__, __LINE__);
324 case glu::RenderConfig::SURFACETYPE_WINDOW:
328 const WindowSurfacePair windowSurface = createWindow(m_display, windowFactory, m_eglDisplay, m_eglConfig, config);
329 m_window = windowSurface.first;
330 m_eglSurface = windowSurface.second;
333 throw tcu::NotSupportedError("EGL platform doesn't support windows", DE_NULL, __FILE__, __LINE__);
337 case glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE:
341 const PixmapSurfacePair pixmapSurface = createPixmap(m_display, pixmapFactory, m_eglDisplay, m_eglConfig, config);
342 m_pixmap = pixmapSurface.first;
343 m_eglSurface = pixmapSurface.second;
346 throw tcu::NotSupportedError("EGL platform doesn't support pixmaps", DE_NULL, __FILE__, __LINE__);
350 case glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC:
351 m_eglSurface = createPBuffer(egl, m_eglDisplay, m_eglConfig, config);
355 throw tcu::InternalError("Invalid surface type");
358 m_eglContext = createGLContext(egl, m_eglDisplay, m_eglConfig, config.type);
360 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
362 // Init core functions
364 if (hasExtension(egl, m_eglDisplay, "EGL_KHR_get_all_proc_addresses"))
366 // Use eglGetProcAddress() for core functions
367 GetProcFuncLoader funcLoader(egl);
368 glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
370 #if !defined(DEQP_GLES2_RUNTIME_LOAD)
371 else if (config.type.getAPI() == glu::ApiType::es(2,0))
373 glw::initES20Direct(&m_glFunctions);
376 #if !defined(DEQP_GLES3_RUNTIME_LOAD)
377 else if (config.type.getAPI() == glu::ApiType::es(3,0))
379 glw::initES30Direct(&m_glFunctions);
384 const char* libraryPath = DE_NULL;
386 if (glu::isContextTypeES(config.type))
388 if (config.type.getMinorVersion() <= 2)
389 libraryPath = DEQP_GLES2_LIBRARY_PATH;
391 libraryPath = DEQP_GLES3_LIBRARY_PATH;
394 libraryPath = DEQP_OPENGL_LIBRARY_PATH;
396 m_dynamicGLLibrary = new de::DynamicLibrary(libraryPath);
398 DynamicFuncLoader funcLoader(m_dynamicGLLibrary);
399 glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
402 // Init extension functions
404 GetProcFuncLoader extLoader(egl);
405 glu::initExtensionFunctions(&m_glFunctions, &extLoader, config.type.getAPI());
409 EGLint width, height, depthBits, stencilBits, numSamples;
410 tcu::PixelFormat pixelFmt;
412 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &width);
413 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &height);
415 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_RED_SIZE, &pixelFmt.redBits);
416 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_GREEN_SIZE, &pixelFmt.greenBits);
417 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_BLUE_SIZE, &pixelFmt.blueBits);
418 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_ALPHA_SIZE, &pixelFmt.alphaBits);
420 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_DEPTH_SIZE, &depthBits);
421 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_STENCIL_SIZE, &stencilBits);
422 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_SAMPLES, &numSamples);
424 EGLU_CHECK_MSG(egl, "Failed to query config attributes");
426 m_glRenderTarget = tcu::RenderTarget(width, height, pixelFmt, depthBits, stencilBits, numSamples);
430 void RenderContext::destroy (void)
432 const Library& egl = m_display->getLibrary();
434 if (m_eglDisplay != EGL_NO_DISPLAY)
436 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
438 if (m_eglSurface != EGL_NO_SURFACE)
439 EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
441 if (m_eglContext != EGL_NO_CONTEXT)
442 EGLU_CHECK_CALL(egl, destroyContext(m_eglDisplay, m_eglContext));
444 EGLU_CHECK_CALL(egl, terminate(m_eglDisplay));
446 m_eglDisplay = EGL_NO_DISPLAY;
447 m_eglSurface = EGL_NO_SURFACE;
448 m_eglContext = EGL_NO_CONTEXT;
454 delete m_dynamicGLLibrary;
459 m_dynamicGLLibrary = DE_NULL;
462 void RenderContext::postIterate (void)
464 const Library& egl = m_display->getLibrary();
468 EGLBoolean swapOk = egl.swapBuffers(m_eglDisplay, m_eglSurface);
469 EGLint error = egl.getError();
470 const bool badWindow = error == EGL_BAD_SURFACE || error == EGL_BAD_NATIVE_WINDOW;
472 if (!swapOk && !badWindow)
473 throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
477 m_window->processEvents();
479 catch (const WindowDestroyedError&)
481 tcu::print("Warning: Window destroyed, recreating...\n");
483 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
484 EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
485 m_eglSurface = EGL_NO_SURFACE;
492 WindowSurfacePair windowSurface = createWindow(m_display, m_nativeWindowFactory, m_eglDisplay, m_eglConfig, m_renderConfig);
493 m_window = windowSurface.first;
494 m_eglSurface = windowSurface.second;
496 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
501 catch (const std::exception& e)
505 egl.makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
506 egl.destroySurface(m_eglDisplay, m_eglSurface);
507 m_eglSurface = EGL_NO_SURFACE;
513 throw tcu::ResourceError(string("Failed to re-create window: ") + e.what());
519 DE_ASSERT(badWindow);
520 throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
523 // Refresh dimensions
528 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &newWidth);
529 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &newHeight);
530 EGLU_CHECK_MSG(egl, "Failed to query window size");
532 if (newWidth != m_glRenderTarget.getWidth() ||
533 newHeight != m_glRenderTarget.getHeight())
535 tcu::print("Warning: Window size changed (%dx%d -> %dx%d), test results might be invalid!\n",
536 m_glRenderTarget.getWidth(), m_glRenderTarget.getHeight(), newWidth, newHeight);
538 m_glRenderTarget = tcu::RenderTarget(newWidth, newHeight,
539 m_glRenderTarget.getPixelFormat(),
540 m_glRenderTarget.getDepthBits(),
541 m_glRenderTarget.getStencilBits(),
542 m_glRenderTarget.getNumSamples());
547 m_glFunctions.flush();
552 GLContextFactory::GLContextFactory (const NativeDisplayFactoryRegistry& displayFactoryRegistry)
553 : glu::ContextFactory ("egl", "EGL OpenGL Context")
554 , m_displayFactoryRegistry (displayFactoryRegistry)
558 glu::RenderContext* GLContextFactory::createContext (const glu::RenderConfig& config, const tcu::CommandLine& cmdLine) const
560 const NativeDisplayFactory& displayFactory = selectNativeDisplayFactory(m_displayFactoryRegistry, cmdLine);
562 const NativeWindowFactory* windowFactory;
563 const NativePixmapFactory* pixmapFactory;
567 windowFactory = &selectNativeWindowFactory(displayFactory, cmdLine);
569 catch (const tcu::NotSupportedError&)
571 windowFactory = DE_NULL;
576 pixmapFactory = &selectNativePixmapFactory(displayFactory, cmdLine);
578 catch (const tcu::NotSupportedError&)
580 pixmapFactory = DE_NULL;
583 return new RenderContext(&displayFactory, windowFactory, pixmapFactory, config);