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 EGL utilities
22 *//*--------------------------------------------------------------------*/
24 #include "egluUtil.hpp"
25 #include "egluDefs.hpp"
26 #include "egluNativeDisplay.hpp"
27 #include "egluConfigFilter.hpp"
28 #include "eglwLibrary.hpp"
29 #include "eglwEnums.hpp"
30 #include "tcuCommandLine.hpp"
31 #include "deSTLUtil.hpp"
32 #include "deStringUtil.hpp"
33 #include "glwEnums.hpp"
46 vector<EGLint> attribMapToList (const AttribMap& attribs)
48 vector<EGLint> attribList;
50 for (AttribMap::const_iterator it = attribs.begin(); it != attribs.end(); ++it)
52 attribList.push_back(it->first);
53 attribList.push_back(it->second);
56 attribList.push_back(EGL_NONE);
61 Version getVersion (const Library& egl, EGLDisplay display)
65 // eglInitialize on already initialized displays just returns the version.
66 EGLU_CHECK_CALL(egl, initialize(display, &major, &minor));
68 return Version(major, minor);
71 vector<string> getExtensions (const Library& egl, EGLDisplay display)
73 const char* const extensionStr = egl.queryString(display, EGL_EXTENSIONS);
75 EGLU_CHECK_MSG(egl, "Querying extensions failed");
77 return de::splitString(extensionStr, ' ');
80 bool hasExtension (const Library& egl, EGLDisplay display, const string& str)
82 const vector<string> extensions = getExtensions(egl, display);
83 return de::contains(extensions.begin(), extensions.end(), str);
86 vector<string> getClientExtensions (const Library& egl)
88 const char* const extensionStr = egl.queryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
89 const EGLint eglError = egl.getError();
90 if (eglError == EGL_BAD_DISPLAY && extensionStr == DE_NULL) {
91 // We do not support client extensions
92 TCU_THROW(NotSupportedError, "EGL_EXT_client_extensions not supported");
95 EGLU_CHECK_MSG(egl, "Querying extensions failed");
97 return de::splitString(extensionStr, ' ');
100 vector<string> getDisplayExtensions (const Library& egl, EGLDisplay display)
102 DE_ASSERT(display != EGL_NO_DISPLAY);
104 return getExtensions(egl, display);
107 vector<EGLConfig> getConfigs (const Library& egl, EGLDisplay display)
109 vector<EGLConfig> configs;
110 EGLint configCount = 0;
111 EGLU_CHECK_CALL(egl, getConfigs(display, DE_NULL, 0, &configCount));
115 configs.resize(configCount);
116 EGLU_CHECK_CALL(egl, getConfigs(display, &(configs[0]), (EGLint)configs.size(), &configCount));
122 vector<EGLConfig> chooseConfigs (const Library& egl, EGLDisplay display, const EGLint* attribList)
124 EGLint numConfigs = 0;
126 EGLU_CHECK_CALL(egl, chooseConfig(display, attribList, DE_NULL, 0, &numConfigs));
129 vector<EGLConfig> configs(numConfigs);
132 EGLU_CHECK_CALL(egl, chooseConfig(display, attribList, &configs.front(), numConfigs, &numConfigs));
138 vector<EGLConfig> chooseConfigs (const Library& egl, EGLDisplay display, const FilterList& filters)
140 const vector<EGLConfig> allConfigs (getConfigs(egl, display));
141 vector<EGLConfig> matchingConfigs;
143 for (vector<EGLConfig>::const_iterator cfg = allConfigs.begin(); cfg != allConfigs.end(); ++cfg)
145 if (filters.match(egl, display, *cfg))
146 matchingConfigs.push_back(*cfg);
149 return matchingConfigs;
152 EGLConfig chooseSingleConfig (const Library& egl, EGLDisplay display, const FilterList& filters)
154 const vector<EGLConfig> allConfigs (getConfigs(egl, display));
156 for (vector<EGLConfig>::const_iterator cfg = allConfigs.begin(); cfg != allConfigs.end(); ++cfg)
158 if (filters.match(egl, display, *cfg))
162 TCU_THROW(NotSupportedError, "No matching EGL config found");
165 EGLConfig chooseSingleConfig (const Library& egl, EGLDisplay display, const EGLint* attribList)
167 const vector<EGLConfig> configs (chooseConfigs(egl, display, attribList));
169 TCU_THROW(NotSupportedError, "No matching EGL config found");
171 return configs.front();
174 vector<EGLConfig> chooseConfigs (const Library& egl, EGLDisplay display, const AttribMap& attribs)
176 const vector<EGLint> attribList = attribMapToList(attribs);
177 return chooseConfigs(egl, display, &attribList.front());
180 EGLConfig chooseSingleConfig (const Library& egl, EGLDisplay display, const AttribMap& attribs)
182 const vector<EGLint> attribList = attribMapToList(attribs);
183 return chooseSingleConfig(egl, display, &attribList.front());
186 EGLConfig chooseConfigByID (const Library& egl, EGLDisplay display, EGLint id)
190 attribs[EGL_CONFIG_ID] = id;
191 attribs[EGL_TRANSPARENT_TYPE] = EGL_DONT_CARE;
192 attribs[EGL_COLOR_BUFFER_TYPE] = EGL_DONT_CARE;
193 attribs[EGL_RENDERABLE_TYPE] = EGL_DONT_CARE;
194 attribs[EGL_SURFACE_TYPE] = EGL_DONT_CARE;
196 return chooseSingleConfig(egl, display, attribs);
199 EGLint getConfigAttribInt (const Library& egl, EGLDisplay display, EGLConfig config, EGLint attrib)
202 EGLU_CHECK_CALL(egl, getConfigAttrib(display, config, attrib, &value));
206 EGLint getConfigID (const Library& egl, EGLDisplay display, EGLConfig config)
208 return getConfigAttribInt(egl, display, config, EGL_CONFIG_ID);
211 EGLint querySurfaceInt (const Library& egl, EGLDisplay display, EGLSurface surface, EGLint attrib)
214 EGLU_CHECK_CALL(egl, querySurface(display, surface, attrib, &value));
218 tcu::IVec2 getSurfaceSize (const Library& egl, EGLDisplay display, EGLSurface surface)
220 const EGLint width = querySurfaceInt(egl, display, surface, EGL_WIDTH);
221 const EGLint height = querySurfaceInt(egl, display, surface, EGL_HEIGHT);
222 return tcu::IVec2(width, height);
225 tcu::IVec2 getSurfaceResolution (const Library& egl, EGLDisplay display, EGLSurface surface)
227 const EGLint hRes = querySurfaceInt(egl, display, surface, EGL_HORIZONTAL_RESOLUTION);
228 const EGLint vRes = querySurfaceInt(egl, display, surface, EGL_VERTICAL_RESOLUTION);
230 if (hRes == EGL_UNKNOWN || vRes == EGL_UNKNOWN)
231 TCU_THROW(NotSupportedError, "Surface doesn't support pixel density queries");
232 return tcu::IVec2(hRes, vRes);
235 //! Get EGLdisplay using eglGetDisplay() or eglGetPlatformDisplayEXT()
236 EGLDisplay getDisplay (NativeDisplay& nativeDisplay)
238 const Library& egl = nativeDisplay.getLibrary();
239 const bool supportsLegacyGetDisplay = (nativeDisplay.getCapabilities() & NativeDisplay::CAPABILITY_GET_DISPLAY_LEGACY) != 0;
240 bool maySupportPlatformGetDisplay = (nativeDisplay.getCapabilities() & NativeDisplay::CAPABILITY_GET_DISPLAY_PLATFORM) != 0;
241 bool maySupportPlatformGetDisplayEXT = (nativeDisplay.getCapabilities() & NativeDisplay::CAPABILITY_GET_DISPLAY_PLATFORM_EXT) != 0;
242 bool usePlatformExt = false;
243 EGLDisplay display = EGL_NO_DISPLAY;
245 TCU_CHECK_INTERNAL(supportsLegacyGetDisplay || maySupportPlatformGetDisplay);
247 if (maySupportPlatformGetDisplayEXT)
251 const vector<string> platformExts = eglu::getClientExtensions(egl);
252 usePlatformExt = de::contains(platformExts.begin(), platformExts.end(), string("EGL_EXT_platform_base")) &&
253 nativeDisplay.getPlatformExtensionName() &&
254 de::contains(platformExts.begin(), platformExts.end(), string(nativeDisplay.getPlatformExtensionName()));
257 catch (const tcu::NotSupportedError&)
259 // If we can't get the client extension string we must not have EGL 1.5 support or the appropriate extensions.
260 maySupportPlatformGetDisplay = false;
261 maySupportPlatformGetDisplayEXT = false;
262 usePlatformExt = false;
266 if (maySupportPlatformGetDisplay)
268 display = egl.getPlatformDisplay(nativeDisplay.getPlatformType(), nativeDisplay.getPlatformNative(), nativeDisplay.getPlatformAttributes());
269 EGLU_CHECK_MSG(egl, "eglGetPlatformDisplay()");
270 TCU_CHECK(display != EGL_NO_DISPLAY);
272 else if (usePlatformExt)
274 const vector<EGLint> legacyAttribs = toLegacyAttribList(nativeDisplay.getPlatformAttributes());
276 display = egl.getPlatformDisplayEXT(nativeDisplay.getPlatformType(), nativeDisplay.getPlatformNative(), &legacyAttribs[0]);
277 EGLU_CHECK_MSG(egl, "eglGetPlatformDisplayEXT()");
278 TCU_CHECK(display != EGL_NO_DISPLAY);
280 else if (supportsLegacyGetDisplay)
282 display = egl.getDisplay(nativeDisplay.getLegacyNative());
283 EGLU_CHECK_MSG(egl, "eglGetDisplay()");
284 TCU_CHECK(display != EGL_NO_DISPLAY);
287 throw tcu::InternalError("No supported way to get EGL display", DE_NULL, __FILE__, __LINE__);
289 DE_ASSERT(display != EGL_NO_DISPLAY);
293 EGLDisplay getAndInitDisplay (NativeDisplay& nativeDisplay, Version* version)
295 const Library& egl = nativeDisplay.getLibrary();
296 EGLDisplay display = getDisplay(nativeDisplay);
299 EGLU_CHECK_CALL(egl, initialize(display, &major, &minor));
302 *version = Version(major, minor);
307 void terminateDisplay(const Library& egl, EGLDisplay display)
309 EGLU_CHECK_CALL(egl, terminate(display));
312 //! Create EGL window surface using eglCreatePlatformWindowSurface, eglCreateWindowSurface() or eglCreatePlatformWindowSurfaceEXT()
313 EGLSurface createWindowSurface (NativeDisplay& nativeDisplay, NativeWindow& window, EGLDisplay display, EGLConfig config, const EGLAttrib* attribList)
315 const Library& egl = nativeDisplay.getLibrary();
316 const bool supportsLegacyCreate = (window.getCapabilities() & NativeWindow::CAPABILITY_CREATE_SURFACE_LEGACY) != 0;
317 bool maySupportPlatformCreate = ((window.getCapabilities() & NativeWindow::CAPABILITY_CREATE_SURFACE_PLATFORM) != 0
318 && eglu::getVersion(egl, display) >= eglu::Version(1, 5));
319 bool maySupportPlatformCreateExtension = (window.getCapabilities() & NativeWindow::CAPABILITY_CREATE_SURFACE_PLATFORM_EXTENSION) != 0;
320 bool usePlatformExt = false;
321 EGLSurface surface = EGL_NO_SURFACE;
323 TCU_CHECK_INTERNAL(supportsLegacyCreate || maySupportPlatformCreateExtension || maySupportPlatformCreate);
325 if (maySupportPlatformCreateExtension)
329 const vector<string> platformExts = eglu::getClientExtensions(egl);
330 usePlatformExt = de::contains(platformExts.begin(), platformExts.end(), string("EGL_EXT_platform_base")) &&
331 nativeDisplay.getPlatformExtensionName() &&
332 de::contains(platformExts.begin(), platformExts.end(), string(nativeDisplay.getPlatformExtensionName()));
335 catch (const tcu::NotSupportedError&)
337 maySupportPlatformCreate = false;
338 maySupportPlatformCreateExtension = false;
339 usePlatformExt = false;
343 if (maySupportPlatformCreate)
345 surface = egl.createPlatformWindowSurface(display, config, window.getPlatformNative(), attribList);
346 EGLU_CHECK_MSG(egl, "eglCreatePlatformWindowSurface()");
347 TCU_CHECK(surface != EGL_NO_SURFACE);
349 else if (usePlatformExt)
351 const vector<EGLint> legacyAttribs = toLegacyAttribList(attribList);
352 surface = egl.createPlatformWindowSurfaceEXT(display, config, window.getPlatformExtension(), &legacyAttribs[0]);
353 EGLU_CHECK_MSG(egl, "eglCreatePlatformWindowSurfaceEXT()");
354 TCU_CHECK(surface != EGL_NO_SURFACE);
356 else if (supportsLegacyCreate)
358 const vector<EGLint> legacyAttribs = toLegacyAttribList(attribList);
359 surface = egl.createWindowSurface(display, config, window.getLegacyNative(), &legacyAttribs[0]);
360 EGLU_CHECK_MSG(egl, "eglCreateWindowSurface()");
361 TCU_CHECK(surface != EGL_NO_SURFACE);
364 throw tcu::InternalError("No supported way to create EGL window surface", DE_NULL, __FILE__, __LINE__);
366 DE_ASSERT(surface != EGL_NO_SURFACE);
370 //! Create EGL pixmap surface using eglCreatePixmapSurface() or eglCreatePlatformPixmapSurfaceEXT()
371 EGLSurface createPixmapSurface (NativeDisplay& nativeDisplay, NativePixmap& pixmap, EGLDisplay display, EGLConfig config, const EGLAttrib* attribList)
373 const Library& egl = nativeDisplay.getLibrary();
374 const bool supportsLegacyCreate = (pixmap.getCapabilities() & NativePixmap::CAPABILITY_CREATE_SURFACE_LEGACY) != 0;
375 bool maySupportPlatformCreateExtension = (pixmap.getCapabilities() & NativePixmap::CAPABILITY_CREATE_SURFACE_PLATFORM_EXTENSION) != 0;
376 bool maySupportPlatformCreate = ((pixmap.getCapabilities() & NativePixmap::CAPABILITY_CREATE_SURFACE_PLATFORM) != 0
377 && eglu::getVersion(egl, display) >= eglu::Version(1, 5));
378 bool usePlatformExt = false;
379 EGLSurface surface = EGL_NO_SURFACE;
381 TCU_CHECK_INTERNAL(supportsLegacyCreate || maySupportPlatformCreateExtension || maySupportPlatformCreate);
383 if (maySupportPlatformCreateExtension)
387 const vector<string> platformExts = eglu::getClientExtensions(egl);
388 usePlatformExt = de::contains(platformExts.begin(), platformExts.end(), string("EGL_EXT_platform_base")) &&
389 nativeDisplay.getPlatformExtensionName() &&
390 de::contains(platformExts.begin(), platformExts.end(), string(nativeDisplay.getPlatformExtensionName()));
393 catch (const tcu::NotSupportedError&)
395 maySupportPlatformCreate = false;
396 maySupportPlatformCreateExtension = false;
397 usePlatformExt = false;
401 if (maySupportPlatformCreate)
403 surface = egl.createPlatformPixmapSurface(display, config, pixmap.getPlatformNative(), attribList);
404 EGLU_CHECK_MSG(egl, "eglCreatePlatformPixmapSurface()");
405 TCU_CHECK(surface != EGL_NO_SURFACE);
407 else if (usePlatformExt)
409 const vector<EGLint> legacyAttribs = toLegacyAttribList(attribList);
411 surface = egl.createPlatformPixmapSurfaceEXT(display, config, pixmap.getPlatformExtension(), &legacyAttribs[0]);
412 EGLU_CHECK_MSG(egl, "eglCreatePlatformPixmapSurfaceEXT()");
413 TCU_CHECK(surface != EGL_NO_SURFACE);
415 else if (supportsLegacyCreate)
417 const vector<EGLint> legacyAttribs = toLegacyAttribList(attribList);
418 surface = egl.createPixmapSurface(display, config, pixmap.getLegacyNative(), &legacyAttribs[0]);
419 EGLU_CHECK_MSG(egl, "eglCreatePixmapSurface()");
420 TCU_CHECK(surface != EGL_NO_SURFACE);
423 throw tcu::InternalError("No supported way to create EGL pixmap surface", DE_NULL, __FILE__, __LINE__);
425 DE_ASSERT(surface != EGL_NO_SURFACE);
429 static WindowParams::Visibility getWindowVisibility (tcu::WindowVisibility visibility)
433 case tcu::WINDOWVISIBILITY_WINDOWED: return WindowParams::VISIBILITY_VISIBLE;
434 case tcu::WINDOWVISIBILITY_FULLSCREEN: return WindowParams::VISIBILITY_FULLSCREEN;
435 case tcu::WINDOWVISIBILITY_HIDDEN: return WindowParams::VISIBILITY_HIDDEN;
439 return WindowParams::VISIBILITY_DONT_CARE;
443 WindowParams::Visibility parseWindowVisibility (const tcu::CommandLine& commandLine)
445 return getWindowVisibility(commandLine.getVisibility());
448 EGLenum parseClientAPI (const std::string& api)
451 return EGL_OPENGL_API;
452 else if (api == "OpenGL_ES")
453 return EGL_OPENGL_ES_API;
454 else if (api == "OpenVG")
455 return EGL_OPENVG_API;
457 throw tcu::InternalError("Unknown EGL client API '" + api + "'");
460 vector<EGLenum> parseClientAPIs (const std::string& apiList)
462 const vector<string> apiStrs = de::splitString(apiList, ' ');
463 vector<EGLenum> apis;
465 for (vector<string>::const_iterator api = apiStrs.begin(); api != apiStrs.end(); ++api)
466 apis.push_back(parseClientAPI(*api));
471 vector<EGLenum> getClientAPIs (const eglw::Library& egl, eglw::EGLDisplay display)
473 return parseClientAPIs(egl.queryString(display, EGL_CLIENT_APIS));
476 EGLint getRenderableAPIsMask (const eglw::Library& egl, eglw::EGLDisplay display)
478 const vector<EGLConfig> configs = getConfigs(egl, display);
481 for (vector<EGLConfig>::const_iterator i = configs.begin(); i != configs.end(); ++i)
482 allAPIs |= getConfigAttribInt(egl, display, *i, EGL_RENDERABLE_TYPE);
487 vector<EGLint> toLegacyAttribList (const EGLAttrib* attribs)
489 const deUint64 attribMask = 0xffffffffull; //!< Max bits that can be used
490 vector<EGLint> legacyAttribs;
494 for (const EGLAttrib* attrib = attribs; *attrib != EGL_NONE; attrib += 2)
496 if ((attrib[0] & ~attribMask) || (attrib[1] & ~attribMask))
497 throw tcu::InternalError("Failed to translate EGLAttrib to EGLint", DE_NULL, __FILE__, __LINE__);
499 legacyAttribs.push_back((EGLint)attrib[0]);
500 legacyAttribs.push_back((EGLint)attrib[1]);
504 legacyAttribs.push_back(EGL_NONE);
506 return legacyAttribs;
509 template<typename Factory>
510 static const Factory& selectFactory (const tcu::FactoryRegistry<Factory>& registry, const char* objectTypeName, const char* cmdLineArg)
514 const Factory* factory = registry.getFactoryByName(cmdLineArg);
520 tcu::print("ERROR: Unknown or unsupported EGL %s type '%s'", objectTypeName, cmdLineArg);
521 tcu::print("Available EGL %s types:\n", objectTypeName);
522 for (size_t ndx = 0; ndx < registry.getFactoryCount(); ndx++)
523 tcu::print(" %s: %s\n", registry.getFactoryByIndex(ndx)->getName(), registry.getFactoryByIndex(ndx)->getDescription());
525 TCU_THROW(NotSupportedError, (string("Unsupported or unknown EGL ") + objectTypeName + " type '" + cmdLineArg + "'").c_str());
528 else if (!registry.empty())
529 return *registry.getDefaultFactory();
531 TCU_THROW(NotSupportedError, (string("No factory supporting EGL '") + objectTypeName + "' type").c_str());
534 const NativeDisplayFactory& selectNativeDisplayFactory (const NativeDisplayFactoryRegistry& registry, const tcu::CommandLine& cmdLine)
536 return selectFactory(registry, "display", cmdLine.getEGLDisplayType());
539 const NativeWindowFactory& selectNativeWindowFactory (const NativeDisplayFactory& factory, const tcu::CommandLine& cmdLine)
541 return selectFactory(factory.getNativeWindowRegistry(), "window", cmdLine.getEGLWindowType());
544 const NativePixmapFactory& selectNativePixmapFactory (const NativeDisplayFactory& factory, const tcu::CommandLine& cmdLine)
546 return selectFactory(factory.getNativePixmapRegistry(), "pixmap", cmdLine.getEGLPixmapType());