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"
50 #include "deSharedPtr.hpp"
59 // \todo [2014-03-12 pyry] Use command line arguments for libraries?
61 // Default library names
62 #if !defined(DEQP_GLES2_LIBRARY_PATH)
63 # if (DE_OS == DE_OS_WIN32)
64 # define DEQP_GLES2_LIBRARY_PATH "libGLESv2.dll"
66 # define DEQP_GLES2_LIBRARY_PATH "libGLESv2.so"
70 #if !defined(DEQP_GLES3_LIBRARY_PATH)
71 # define DEQP_GLES3_LIBRARY_PATH DEQP_GLES2_LIBRARY_PATH
74 #if !defined(DEQP_OPENGL_LIBRARY_PATH)
75 # if (DE_OS == DE_OS_WIN32)
76 # define DEQP_OPENGL_LIBRARY_PATH "opengl32.dll"
78 # define DEQP_OPENGL_LIBRARY_PATH "libGL.so"
92 DEFAULT_OFFSCREEN_WIDTH = 512,
93 DEFAULT_OFFSCREEN_HEIGHT = 512
96 class GetProcFuncLoader : public glw::FunctionLoader
99 GetProcFuncLoader (const Library& egl)
104 glw::GenericFuncType get (const char* name) const
106 return (glw::GenericFuncType)m_egl.getProcAddress(name);
110 const Library& m_egl;
113 class DynamicFuncLoader : public glw::FunctionLoader
116 DynamicFuncLoader (de::DynamicLibrary* library)
121 glw::GenericFuncType get (const char* name) const
123 return (glw::GenericFuncType)m_library->getFunction(name);
127 de::DynamicLibrary* m_library;
130 class RenderContext : public GLRenderContext
133 RenderContext (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config, const glu::RenderContext* sharedContext = DE_NULL);
134 virtual ~RenderContext (void);
136 virtual glu::ContextType getType (void) const { return m_renderConfig.type; }
137 virtual const glw::Functions& getFunctions (void) const { return m_glFunctions; }
138 virtual const tcu::RenderTarget& getRenderTarget (void) const { return m_glRenderTarget; }
139 virtual void postIterate (void);
141 virtual EGLDisplay getEGLDisplay (void) const { return m_eglDisplay; }
142 virtual EGLContext getEGLContext (void) const { return m_eglContext; }
143 virtual EGLConfig getEGLConfig (void) const { return m_eglConfig; }
144 virtual const eglw::Library& getLibrary (void) const { return m_display->getLibrary(); }
146 virtual eglw::GenericFuncType getProcAddress (const char* name) const;
148 virtual void makeCurrent (void);
151 void create (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config, const glu::RenderContext* sharedContext);
154 const glu::RenderConfig m_renderConfig;
155 const NativeWindowFactory* const m_nativeWindowFactory; // Stored in case window must be re-created
157 de::SharedPtr<NativeDisplay> m_display;
158 NativeWindow* m_window;
159 NativePixmap* m_pixmap;
161 EGLDisplay m_eglDisplay;
162 EGLConfig m_eglConfig;
163 EGLSurface m_eglSurface;
164 EGLContext m_eglContext;
165 EGLContext m_eglSharedContext;
167 tcu::RenderTarget m_glRenderTarget;
168 de::DynamicLibrary* m_dynamicGLLibrary;
169 glw::Functions m_glFunctions;
172 RenderContext::RenderContext (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config, const glu::RenderContext* sharedContext)
173 : m_renderConfig (config)
174 , m_nativeWindowFactory (windowFactory)
175 , m_display (DE_NULL)
179 , m_eglDisplay (EGL_NO_DISPLAY)
180 , m_eglSurface (EGL_NO_SURFACE)
181 , m_eglContext (EGL_NO_CONTEXT)
182 , m_eglSharedContext (EGL_NO_CONTEXT)
184 , m_dynamicGLLibrary (DE_NULL)
186 DE_ASSERT(displayFactory);
190 create(displayFactory, windowFactory, pixmapFactory, config, sharedContext);
199 RenderContext::~RenderContext(void)
207 // destroy() calls EGL functions that are checked and may throw exceptions
212 delete m_dynamicGLLibrary;
215 static WindowParams::Visibility getNativeWindowVisibility (glu::RenderConfig::Visibility visibility)
217 using glu::RenderConfig;
221 case RenderConfig::VISIBILITY_HIDDEN: return WindowParams::VISIBILITY_HIDDEN;
222 case RenderConfig::VISIBILITY_VISIBLE: return WindowParams::VISIBILITY_VISIBLE;
223 case RenderConfig::VISIBILITY_FULLSCREEN: return WindowParams::VISIBILITY_FULLSCREEN;
225 DE_ASSERT((int)visibility == RenderConfig::DONT_CARE);
226 return WindowParams::VISIBILITY_DONT_CARE;
230 typedef std::pair<NativeWindow*, EGLSurface> WindowSurfacePair;
231 typedef std::pair<NativePixmap*, EGLSurface> PixmapSurfacePair;
233 WindowSurfacePair createWindow (NativeDisplay* nativeDisplay, const NativeWindowFactory* windowFactory, EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig& config)
235 const int width = (config.width == glu::RenderConfig::DONT_CARE ? WindowParams::SIZE_DONT_CARE : config.width);
236 const int height = (config.height == glu::RenderConfig::DONT_CARE ? WindowParams::SIZE_DONT_CARE : config.height);
237 const WindowParams::Visibility visibility = getNativeWindowVisibility(config.windowVisibility);
238 NativeWindow* nativeWindow = DE_NULL;
239 EGLSurface surface = EGL_NO_SURFACE;
240 const EGLAttrib attribList[] = { EGL_NONE };
242 nativeWindow = windowFactory->createWindow(nativeDisplay, eglDisplay, eglConfig, &attribList[0], WindowParams(width, height, visibility));
246 surface = eglu::createWindowSurface(*nativeDisplay, *nativeWindow, eglDisplay, eglConfig, attribList);
254 return WindowSurfacePair(nativeWindow, surface);
257 PixmapSurfacePair createPixmap (NativeDisplay* nativeDisplay, const NativePixmapFactory* pixmapFactory, EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig& config)
259 const int width = (config.width == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_WIDTH : config.width);
260 const int height = (config.height == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_HEIGHT : config.height);
261 NativePixmap* nativePixmap = DE_NULL;
262 EGLSurface surface = EGL_NO_SURFACE;
263 const EGLAttrib attribList[] = { EGL_NONE };
265 nativePixmap = pixmapFactory->createPixmap(nativeDisplay, eglDisplay, eglConfig, &attribList[0], width, height);
269 surface = eglu::createPixmapSurface(*nativeDisplay, *nativePixmap, eglDisplay, eglConfig, attribList);
277 return PixmapSurfacePair(nativePixmap, surface);
280 EGLSurface createPBuffer (const Library& egl, EGLDisplay display, EGLConfig eglConfig, const glu::RenderConfig& config)
282 const int width = (config.width == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_WIDTH : config.width);
283 const int height = (config.height == glu::RenderConfig::DONT_CARE ? DEFAULT_OFFSCREEN_HEIGHT : config.height);
285 const EGLint attribList[] =
292 surface = egl.createPbufferSurface(display, eglConfig, &(attribList[0]));
293 EGLU_CHECK_MSG(egl, "eglCreatePbufferSurface()");
298 void RenderContext::makeCurrent (void)
300 const Library& egl = m_display->getLibrary();
302 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
305 glw::GenericFuncType RenderContext::getProcAddress (const char* name) const
307 return (glw::GenericFuncType)m_display->getLibrary().getProcAddress(name);
310 void RenderContext::create (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config, const glu::RenderContext *sharedContext)
312 glu::RenderConfig::SurfaceType surfaceType = config.surfaceType;
314 DE_ASSERT(displayFactory);
316 if (DE_NULL == sharedContext)
317 m_display = de::SharedPtr<NativeDisplay>(displayFactory->createDisplay());
320 const RenderContext* context = dynamic_cast<const RenderContext*>(sharedContext);
321 m_eglSharedContext = context->m_eglContext;
322 m_display = context->m_display;
325 m_eglDisplay = eglu::getDisplay(*m_display);
326 const Library& egl = m_display->getLibrary();
331 EGLU_CHECK_CALL(egl, initialize(m_eglDisplay, &major, &minor));
334 m_eglConfig = chooseConfig(egl, m_eglDisplay, config);
336 if (surfaceType == glu::RenderConfig::SURFACETYPE_DONT_CARE)
338 // Choose based on what selected configuration supports
339 const EGLint supportedTypes = eglu::getConfigAttribInt(egl, m_eglDisplay, m_eglConfig, EGL_SURFACE_TYPE);
341 if ((supportedTypes & EGL_WINDOW_BIT) != 0)
342 surfaceType = glu::RenderConfig::SURFACETYPE_WINDOW;
343 else if ((supportedTypes & EGL_PBUFFER_BIT) != 0)
344 surfaceType = glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC;
345 else if ((supportedTypes & EGL_PIXMAP_BIT) != 0)
346 surfaceType = glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE;
348 throw tcu::NotSupportedError("Selected EGL config doesn't support any surface types", DE_NULL, __FILE__, __LINE__);
353 case glu::RenderConfig::SURFACETYPE_WINDOW:
357 const WindowSurfacePair windowSurface = createWindow(m_display.get(), windowFactory, m_eglDisplay, m_eglConfig, config);
358 m_window = windowSurface.first;
359 m_eglSurface = windowSurface.second;
362 throw tcu::NotSupportedError("EGL platform doesn't support windows", DE_NULL, __FILE__, __LINE__);
366 case glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE:
370 const PixmapSurfacePair pixmapSurface = createPixmap(m_display.get(), pixmapFactory, m_eglDisplay, m_eglConfig, config);
371 m_pixmap = pixmapSurface.first;
372 m_eglSurface = pixmapSurface.second;
375 throw tcu::NotSupportedError("EGL platform doesn't support pixmaps", DE_NULL, __FILE__, __LINE__);
379 case glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC:
380 m_eglSurface = createPBuffer(egl, m_eglDisplay, m_eglConfig, config);
384 throw tcu::InternalError("Invalid surface type");
387 m_eglContext = createGLContext(egl, m_eglDisplay, m_eglConfig, config.type, m_eglSharedContext, config.resetNotificationStrategy);
389 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
391 // Init core functions
393 if (hasExtension(egl, m_eglDisplay, "EGL_KHR_get_all_proc_addresses"))
395 // Use eglGetProcAddress() for core functions
396 GetProcFuncLoader funcLoader(egl);
397 glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
399 #if defined(DEQP_GLES2_DIRECT_LINK)
400 else if (config.type.getAPI() == glu::ApiType::es(2,0))
402 glw::initES20Direct(&m_glFunctions);
405 #if defined(DEQP_GLES3_DIRECT_LINK)
406 else if (config.type.getAPI() == glu::ApiType::es(3,0))
408 glw::initES30Direct(&m_glFunctions);
411 #if defined(DEQP_GLES31_DIRECT_LINK)
412 else if (config.type.getAPI() == glu::ApiType::es(3,1))
414 glw::initES31Direct(&m_glFunctions);
417 #if defined(DEQP_GLES32_DIRECT_LINK)
418 else if (config.type.getAPI() == glu::ApiType::es(3,2))
420 glw::initES32Direct(&m_glFunctions);
425 const char* libraryPath = DE_NULL;
427 if (glu::isContextTypeES(config.type))
429 if (config.type.getMinorVersion() <= 2)
430 libraryPath = DEQP_GLES2_LIBRARY_PATH;
432 libraryPath = DEQP_GLES3_LIBRARY_PATH;
435 libraryPath = DEQP_OPENGL_LIBRARY_PATH;
437 m_dynamicGLLibrary = new de::DynamicLibrary(libraryPath);
439 DynamicFuncLoader funcLoader(m_dynamicGLLibrary);
440 glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
443 // Init extension functions
445 GetProcFuncLoader extLoader(egl);
446 glu::initExtensionFunctions(&m_glFunctions, &extLoader, config.type.getAPI());
450 EGLint width, height, depthBits, stencilBits, numSamples;
451 tcu::PixelFormat pixelFmt;
453 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &width);
454 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &height);
456 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_RED_SIZE, &pixelFmt.redBits);
457 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_GREEN_SIZE, &pixelFmt.greenBits);
458 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_BLUE_SIZE, &pixelFmt.blueBits);
459 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_ALPHA_SIZE, &pixelFmt.alphaBits);
461 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_DEPTH_SIZE, &depthBits);
462 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_STENCIL_SIZE, &stencilBits);
463 egl.getConfigAttrib(m_eglDisplay, m_eglConfig, EGL_SAMPLES, &numSamples);
465 EGLU_CHECK_MSG(egl, "Failed to query config attributes");
467 m_glRenderTarget = tcu::RenderTarget(width, height, pixelFmt, depthBits, stencilBits, numSamples);
470 egl.swapInterval(m_eglDisplay, 0);
473 void RenderContext::destroy (void)
475 if (m_eglDisplay != EGL_NO_DISPLAY)
477 const Library& egl = m_display->getLibrary();
479 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
481 if (m_eglSurface != EGL_NO_SURFACE)
482 EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
484 if (m_eglContext != EGL_NO_CONTEXT)
485 EGLU_CHECK_CALL(egl, destroyContext(m_eglDisplay, m_eglContext));
487 if (m_eglSharedContext == EGL_NO_CONTEXT)
488 EGLU_CHECK_CALL(egl, terminate(m_eglDisplay));
490 m_eglDisplay = EGL_NO_DISPLAY;
491 m_eglSurface = EGL_NO_SURFACE;
492 m_eglContext = EGL_NO_CONTEXT;
497 delete m_dynamicGLLibrary;
501 m_dynamicGLLibrary = DE_NULL;
504 void RenderContext::postIterate (void)
506 const Library& egl = m_display->getLibrary();
510 EGLBoolean swapOk = egl.swapBuffers(m_eglDisplay, m_eglSurface);
511 EGLint error = egl.getError();
512 const bool badWindow = error == EGL_BAD_SURFACE || error == EGL_BAD_NATIVE_WINDOW;
514 if (!swapOk && !badWindow)
515 throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
519 m_window->processEvents();
521 catch (const WindowDestroyedError&)
523 tcu::print("Warning: Window destroyed, recreating...\n");
525 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
526 EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
527 m_eglSurface = EGL_NO_SURFACE;
534 WindowSurfacePair windowSurface = createWindow(m_display.get(), m_nativeWindowFactory, m_eglDisplay, m_eglConfig, m_renderConfig);
535 m_window = windowSurface.first;
536 m_eglSurface = windowSurface.second;
538 EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
543 catch (const std::exception& e)
547 egl.makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
548 egl.destroySurface(m_eglDisplay, m_eglSurface);
549 m_eglSurface = EGL_NO_SURFACE;
555 throw tcu::ResourceError(string("Failed to re-create window: ") + e.what());
561 DE_ASSERT(badWindow);
562 throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
565 // Refresh dimensions
570 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &newWidth);
571 egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &newHeight);
572 EGLU_CHECK_MSG(egl, "Failed to query window size");
574 if (newWidth != m_glRenderTarget.getWidth() ||
575 newHeight != m_glRenderTarget.getHeight())
577 tcu::print("Warning: Window size changed (%dx%d -> %dx%d), test results might be invalid!\n",
578 m_glRenderTarget.getWidth(), m_glRenderTarget.getHeight(), newWidth, newHeight);
580 m_glRenderTarget = tcu::RenderTarget(newWidth, newHeight,
581 m_glRenderTarget.getPixelFormat(),
582 m_glRenderTarget.getDepthBits(),
583 m_glRenderTarget.getStencilBits(),
584 m_glRenderTarget.getNumSamples());
589 m_glFunctions.flush();
594 GLContextFactory::GLContextFactory (const NativeDisplayFactoryRegistry& displayFactoryRegistry)
595 : glu::ContextFactory ("egl", "EGL OpenGL Context")
596 , m_displayFactoryRegistry (displayFactoryRegistry)
600 glu::RenderContext* GLContextFactory::createContext (const glu::RenderConfig& config, const tcu::CommandLine& cmdLine, const glu::RenderContext *sharedContext) const
602 const NativeDisplayFactory& displayFactory = selectNativeDisplayFactory(m_displayFactoryRegistry, cmdLine);
604 const NativeWindowFactory* windowFactory;
605 const NativePixmapFactory* pixmapFactory;
609 windowFactory = &selectNativeWindowFactory(displayFactory, cmdLine);
611 catch (const tcu::NotSupportedError&)
613 windowFactory = DE_NULL;
618 pixmapFactory = &selectNativePixmapFactory(displayFactory, cmdLine);
620 catch (const tcu::NotSupportedError&)
622 pixmapFactory = DE_NULL;
625 return new RenderContext(&displayFactory, windowFactory, pixmapFactory, config, sharedContext);