• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GL context factory using EGL.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "egluGLContextFactory.hpp"
25 
26 #include "tcuRenderTarget.hpp"
27 #include "tcuPlatform.hpp"
28 #include "tcuCommandLine.hpp"
29 
30 #include "gluDefs.hpp"
31 
32 #include "egluDefs.hpp"
33 #include "egluUtil.hpp"
34 #include "egluGLUtil.hpp"
35 #include "egluNativeWindow.hpp"
36 #include "egluNativePixmap.hpp"
37 #include "egluStrUtil.hpp"
38 
39 #include "eglwLibrary.hpp"
40 #include "eglwEnums.hpp"
41 
42 #include "glwInitFunctions.hpp"
43 #include "glwInitES20Direct.hpp"
44 #include "glwInitES30Direct.hpp"
45 #include "glwInitES31Direct.hpp"
46 #include "glwInitES32Direct.hpp"
47 
48 #include "deDynamicLibrary.hpp"
49 #include "deSTLUtil.hpp"
50 #include "deSharedPtr.hpp"
51 
52 #include <string>
53 #include <string>
54 #include <sstream>
55 
56 using std::string;
57 using std::vector;
58 
59 // \todo [2014-03-12 pyry] Use command line arguments for libraries?
60 
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"
65 #	else
66 #		define DEQP_GLES2_LIBRARY_PATH "libGLESv2.so"
67 #	endif
68 #endif
69 
70 #if !defined(DEQP_GLES3_LIBRARY_PATH)
71 #	define DEQP_GLES3_LIBRARY_PATH DEQP_GLES2_LIBRARY_PATH
72 #endif
73 
74 #if !defined(DEQP_OPENGL_LIBRARY_PATH)
75 #	if (DE_OS == DE_OS_WIN32)
76 #		define DEQP_OPENGL_LIBRARY_PATH "opengl32.dll"
77 #	else
78 #		define DEQP_OPENGL_LIBRARY_PATH "libGL.so"
79 #	endif
80 #endif
81 
82 namespace eglu
83 {
84 
85 using namespace eglw;
86 
87 namespace
88 {
89 
90 enum
91 {
92 	DEFAULT_OFFSCREEN_WIDTH		= 512,
93 	DEFAULT_OFFSCREEN_HEIGHT	= 512
94 };
95 
96 class GetProcFuncLoader : public glw::FunctionLoader
97 {
98 public:
GetProcFuncLoader(const Library & egl)99 	GetProcFuncLoader (const Library& egl)
100 		: m_egl(egl)
101 	{
102 	}
103 
get(const char * name) const104 	glw::GenericFuncType get (const char* name) const
105 	{
106 		return (glw::GenericFuncType)m_egl.getProcAddress(name);
107 	}
108 
109 protected:
110 	const Library& m_egl;
111 };
112 
113 class DynamicFuncLoader : public glw::FunctionLoader
114 {
115 public:
DynamicFuncLoader(de::DynamicLibrary * library)116 	DynamicFuncLoader	(de::DynamicLibrary* library)
117 		: m_library(library)
118 	{
119 	}
120 
get(const char * name) const121 	glw::GenericFuncType get (const char* name) const
122 	{
123 		return (glw::GenericFuncType)m_library->getFunction(name);
124 	}
125 
126 private:
127 	de::DynamicLibrary*	m_library;
128 };
129 
130 class RenderContext : public GLRenderContext
131 {
132 public:
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);
135 
getType(void) const136 	virtual glu::ContextType			getType					(void) const { return m_renderConfig.type;		}
getFunctions(void) const137 	virtual const glw::Functions&		getFunctions			(void) const { return m_glFunctions;			}
getRenderTarget(void) const138 	virtual const tcu::RenderTarget&	getRenderTarget			(void) const { return m_glRenderTarget;			}
139 	virtual void						postIterate				(void);
140 
getEGLDisplay(void) const141 	virtual EGLDisplay					getEGLDisplay			(void) const { return m_eglDisplay;				}
getEGLContext(void) const142 	virtual EGLContext					getEGLContext			(void) const { return m_eglContext;				}
getEGLConfig(void) const143 	virtual EGLConfig					getEGLConfig			(void) const { return m_eglConfig;				}
getLibrary(void) const144 	virtual const eglw::Library&		getLibrary				(void) const { return m_display->getLibrary();	}
145 
146 	virtual eglw::GenericFuncType		getProcAddress			(const char* name) const;
147 
148 	virtual void						makeCurrent				(void);
149 
150 private:
151 	void								create					(const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config, const glu::RenderContext* sharedContext);
152 	void								destroy					(void);
153 
154 	const glu::RenderConfig				m_renderConfig;
155 	const NativeWindowFactory* const	m_nativeWindowFactory;	// Stored in case window must be re-created
156 
157 	de::SharedPtr<NativeDisplay>		m_display;
158 	NativeWindow*						m_window;
159 	NativePixmap*						m_pixmap;
160 
161 	EGLDisplay							m_eglDisplay;
162 	EGLConfig							m_eglConfig;
163 	EGLSurface							m_eglSurface;
164 	EGLContext							m_eglContext;
165 	EGLContext							m_eglSharedContext;
166 
167 	tcu::RenderTarget					m_glRenderTarget;
168 	de::DynamicLibrary*					m_dynamicGLLibrary;
169 	glw::Functions						m_glFunctions;
170 };
171 
RenderContext(const NativeDisplayFactory * displayFactory,const NativeWindowFactory * windowFactory,const NativePixmapFactory * pixmapFactory,const glu::RenderConfig & config,const glu::RenderContext * sharedContext)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)
176 	, m_window				(DE_NULL)
177 	, m_pixmap				(DE_NULL)
178 
179 	, m_eglDisplay			(EGL_NO_DISPLAY)
180 	, m_eglSurface			(EGL_NO_SURFACE)
181 	, m_eglContext			(EGL_NO_CONTEXT)
182 	, m_eglSharedContext	(EGL_NO_CONTEXT)
183 
184 	, m_dynamicGLLibrary	(DE_NULL)
185 {
186 	DE_ASSERT(displayFactory);
187 
188 	try
189 	{
190 		create(displayFactory, windowFactory, pixmapFactory, config, sharedContext);
191 	}
192 	catch (...)
193 	{
194 		destroy();
195 		throw;
196 	}
197 }
198 
~RenderContext(void)199 RenderContext::~RenderContext(void)
200 {
201 	try
202 	{
203 		destroy();
204 	}
205 	catch (...)
206 	{
207 		// destroy() calls EGL functions that are checked and may throw exceptions
208 	}
209 
210 	delete m_window;
211 	delete m_pixmap;
212 	delete m_dynamicGLLibrary;
213 }
214 
getNativeWindowVisibility(glu::RenderConfig::Visibility visibility)215 static WindowParams::Visibility getNativeWindowVisibility (glu::RenderConfig::Visibility visibility)
216 {
217 	using glu::RenderConfig;
218 
219 	switch (visibility)
220 	{
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;
224 		default:
225 			DE_ASSERT((int)visibility == RenderConfig::DONT_CARE);
226 			return WindowParams::VISIBILITY_DONT_CARE;
227 	}
228 }
229 
230 typedef std::pair<NativeWindow*, EGLSurface> WindowSurfacePair;
231 typedef std::pair<NativePixmap*, EGLSurface> PixmapSurfacePair;
232 
createWindow(NativeDisplay * nativeDisplay,const NativeWindowFactory * windowFactory,EGLDisplay eglDisplay,EGLConfig eglConfig,const glu::RenderConfig & config)233 WindowSurfacePair createWindow (NativeDisplay* nativeDisplay, const NativeWindowFactory* windowFactory, EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig& config)
234 {
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 };
241 
242 	nativeWindow = windowFactory->createWindow(nativeDisplay, eglDisplay, eglConfig, &attribList[0], WindowParams(width, height, visibility));
243 
244 	try
245 	{
246 		surface = eglu::createWindowSurface(*nativeDisplay, *nativeWindow, eglDisplay, eglConfig, attribList);
247 	}
248 	catch (...)
249 	{
250 		delete nativeWindow;
251 		throw;
252 	}
253 
254 	return WindowSurfacePair(nativeWindow, surface);
255 }
256 
createPixmap(NativeDisplay * nativeDisplay,const NativePixmapFactory * pixmapFactory,EGLDisplay eglDisplay,EGLConfig eglConfig,const glu::RenderConfig & config)257 PixmapSurfacePair createPixmap (NativeDisplay* nativeDisplay, const NativePixmapFactory* pixmapFactory, EGLDisplay eglDisplay, EGLConfig eglConfig, const glu::RenderConfig& config)
258 {
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 };
264 
265 	nativePixmap = pixmapFactory->createPixmap(nativeDisplay, eglDisplay, eglConfig, &attribList[0], width, height);
266 
267 	try
268 	{
269 		surface = eglu::createPixmapSurface(*nativeDisplay, *nativePixmap, eglDisplay, eglConfig, attribList);
270 	}
271 	catch (...)
272 	{
273 		delete nativePixmap;
274 		throw;
275 	}
276 
277 	return PixmapSurfacePair(nativePixmap, surface);
278 }
279 
createPBuffer(const Library & egl,EGLDisplay display,EGLConfig eglConfig,const glu::RenderConfig & config)280 EGLSurface createPBuffer (const Library& egl, EGLDisplay display, EGLConfig eglConfig, const glu::RenderConfig& config)
281 {
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);
284 	EGLSurface		surface;
285 	const EGLint	attribList[]	=
286 	{
287 		EGL_WIDTH,	width,
288 		EGL_HEIGHT,	height,
289 		EGL_NONE
290 	};
291 
292 	surface = egl.createPbufferSurface(display, eglConfig, &(attribList[0]));
293 	EGLU_CHECK_MSG(egl, "eglCreatePbufferSurface()");
294 
295 	return surface;
296 }
297 
makeCurrent(void)298 void RenderContext::makeCurrent (void)
299 {
300 	const Library& egl = m_display->getLibrary();
301 
302 	EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
303 }
304 
getProcAddress(const char * name) const305 glw::GenericFuncType RenderContext::getProcAddress (const char* name) const
306 {
307 	return (glw::GenericFuncType)m_display->getLibrary().getProcAddress(name);
308 }
309 
create(const NativeDisplayFactory * displayFactory,const NativeWindowFactory * windowFactory,const NativePixmapFactory * pixmapFactory,const glu::RenderConfig & config,const glu::RenderContext * sharedContext)310 void RenderContext::create (const NativeDisplayFactory* displayFactory, const NativeWindowFactory* windowFactory, const NativePixmapFactory* pixmapFactory, const glu::RenderConfig& config, const glu::RenderContext *sharedContext)
311 {
312 	glu::RenderConfig::SurfaceType	surfaceType	= config.surfaceType;
313 
314 	DE_ASSERT(displayFactory);
315 
316 	if (DE_NULL == sharedContext)
317 		m_display = de::SharedPtr<NativeDisplay>(displayFactory->createDisplay());
318 	else
319 	{
320 		const RenderContext* context = dynamic_cast<const RenderContext*>(sharedContext);
321 		m_eglSharedContext			 = context->m_eglContext;
322 		m_display					 = context->m_display;
323 	}
324 
325 	m_eglDisplay	= eglu::getDisplay(*m_display);
326 	const Library& egl = m_display->getLibrary();
327 
328 	{
329 		EGLint major = 0;
330 		EGLint minor = 0;
331 		EGLU_CHECK_CALL(egl, initialize(m_eglDisplay, &major, &minor));
332 	}
333 
334 	m_eglConfig	= chooseConfig(egl, m_eglDisplay, config);
335 
336 	if (surfaceType == glu::RenderConfig::SURFACETYPE_DONT_CARE)
337 	{
338 		// Choose based on what selected configuration supports
339 		const EGLint supportedTypes = eglu::getConfigAttribInt(egl, m_eglDisplay, m_eglConfig, EGL_SURFACE_TYPE);
340 
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;
347 		else
348 			throw tcu::NotSupportedError("Selected EGL config doesn't support any surface types", DE_NULL, __FILE__, __LINE__);
349 	}
350 
351 	switch (surfaceType)
352 	{
353 		case glu::RenderConfig::SURFACETYPE_WINDOW:
354 		{
355 			if (windowFactory)
356 			{
357 				const WindowSurfacePair windowSurface = createWindow(m_display.get(), windowFactory, m_eglDisplay, m_eglConfig, config);
358 				m_window		= windowSurface.first;
359 				m_eglSurface	= windowSurface.second;
360 			}
361 			else
362 				throw tcu::NotSupportedError("EGL platform doesn't support windows", DE_NULL, __FILE__, __LINE__);
363 			break;
364 		}
365 
366 		case glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE:
367 		{
368 			if (pixmapFactory)
369 			{
370 				const PixmapSurfacePair pixmapSurface = createPixmap(m_display.get(), pixmapFactory, m_eglDisplay, m_eglConfig, config);
371 				m_pixmap		= pixmapSurface.first;
372 				m_eglSurface	= pixmapSurface.second;
373 			}
374 			else
375 				throw tcu::NotSupportedError("EGL platform doesn't support pixmaps", DE_NULL, __FILE__, __LINE__);
376 			break;
377 		}
378 
379 		case glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC:
380 			m_eglSurface = createPBuffer(egl, m_eglDisplay, m_eglConfig, config);
381 			break;
382 
383 		default:
384 			throw tcu::InternalError("Invalid surface type");
385 	}
386 
387 	m_eglContext = createGLContext(egl, m_eglDisplay, m_eglConfig, config.type, m_eglSharedContext, config.resetNotificationStrategy);
388 
389 	EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
390 
391 	// Init core functions
392 
393 	if (hasExtension(egl, m_eglDisplay, "EGL_KHR_get_all_proc_addresses"))
394 	{
395 		// Use eglGetProcAddress() for core functions
396 		GetProcFuncLoader funcLoader(egl);
397 		glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
398 	}
399 #if defined(DEQP_GLES2_DIRECT_LINK)
400 	else if (config.type.getAPI() == glu::ApiType::es(2,0))
401 	{
402 		glw::initES20Direct(&m_glFunctions);
403 	}
404 #endif
405 #if defined(DEQP_GLES3_DIRECT_LINK)
406 	else if (config.type.getAPI() == glu::ApiType::es(3,0))
407 	{
408 		glw::initES30Direct(&m_glFunctions);
409 	}
410 #endif
411 #if defined(DEQP_GLES31_DIRECT_LINK)
412 	else if (config.type.getAPI() == glu::ApiType::es(3,1))
413 	{
414 		glw::initES31Direct(&m_glFunctions);
415 	}
416 #endif
417 #if defined(DEQP_GLES32_DIRECT_LINK)
418 	else if (config.type.getAPI() == glu::ApiType::es(3,2))
419 	{
420 		glw::initES32Direct(&m_glFunctions);
421 	}
422 #endif
423 	else
424 	{
425 		const char* libraryPath = DE_NULL;
426 
427 		if (glu::isContextTypeES(config.type))
428 		{
429 			if (config.type.getMinorVersion() <= 2)
430 				libraryPath = DEQP_GLES2_LIBRARY_PATH;
431 			else
432 				libraryPath = DEQP_GLES3_LIBRARY_PATH;
433 		}
434 		else
435 			libraryPath = DEQP_OPENGL_LIBRARY_PATH;
436 
437 		m_dynamicGLLibrary = new de::DynamicLibrary(libraryPath);
438 
439 		DynamicFuncLoader funcLoader(m_dynamicGLLibrary);
440 		glu::initCoreFunctions(&m_glFunctions, &funcLoader, config.type.getAPI());
441 	}
442 
443 	// Init extension functions
444 	{
445 		GetProcFuncLoader extLoader(egl);
446 		glu::initExtensionFunctions(&m_glFunctions, &extLoader, config.type.getAPI());
447 	}
448 
449 	{
450 		EGLint				width, height, depthBits, stencilBits, numSamples;
451 		tcu::PixelFormat	pixelFmt;
452 
453 		egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH,		&width);
454 		egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT,	&height);
455 
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);
460 
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);
464 
465 		EGLU_CHECK_MSG(egl, "Failed to query config attributes");
466 
467 		m_glRenderTarget = tcu::RenderTarget(width, height, pixelFmt, depthBits, stencilBits, numSamples);
468 	}
469 }
470 
destroy(void)471 void RenderContext::destroy (void)
472 {
473 	if (m_eglDisplay != EGL_NO_DISPLAY)
474 	{
475 		const Library& egl = m_display->getLibrary();
476 
477 		EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
478 
479 		if (m_eglSurface != EGL_NO_SURFACE)
480 			EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
481 
482 		if (m_eglContext != EGL_NO_CONTEXT)
483 			EGLU_CHECK_CALL(egl, destroyContext(m_eglDisplay, m_eglContext));
484 
485 		if (m_eglSharedContext == EGL_NO_CONTEXT)
486 			EGLU_CHECK_CALL(egl, terminate(m_eglDisplay));
487 
488 		m_eglDisplay	= EGL_NO_DISPLAY;
489 		m_eglSurface	= EGL_NO_SURFACE;
490 		m_eglContext	= EGL_NO_CONTEXT;
491 	}
492 
493 	delete m_window;
494 	delete m_pixmap;
495 	delete m_dynamicGLLibrary;
496 
497 	m_window			= DE_NULL;
498 	m_pixmap			= DE_NULL;
499 	m_dynamicGLLibrary	= DE_NULL;
500 }
501 
postIterate(void)502 void RenderContext::postIterate (void)
503 {
504 	const Library& egl = m_display->getLibrary();
505 
506 	if (m_window)
507 	{
508 		EGLBoolean	swapOk		= egl.swapBuffers(m_eglDisplay, m_eglSurface);
509 		EGLint		error		= egl.getError();
510 		const bool	badWindow	= error == EGL_BAD_SURFACE || error == EGL_BAD_NATIVE_WINDOW;
511 
512 		if (!swapOk && !badWindow)
513 			throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
514 
515 		try
516 		{
517 			m_window->processEvents();
518 		}
519 		catch (const WindowDestroyedError&)
520 		{
521 			tcu::print("Warning: Window destroyed, recreating...\n");
522 
523 			EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
524 			EGLU_CHECK_CALL(egl, destroySurface(m_eglDisplay, m_eglSurface));
525 			m_eglSurface = EGL_NO_SURFACE;
526 
527 			delete m_window;
528 			m_window = DE_NULL;
529 
530 			try
531 			{
532 				WindowSurfacePair windowSurface = createWindow(m_display.get(), m_nativeWindowFactory, m_eglDisplay, m_eglConfig, m_renderConfig);
533 				m_window		= windowSurface.first;
534 				m_eglSurface	= windowSurface.second;
535 
536 				EGLU_CHECK_CALL(egl, makeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext));
537 
538 				swapOk	= EGL_TRUE;
539 				error	= EGL_SUCCESS;
540 			}
541 			catch (const std::exception& e)
542 			{
543 				if (m_eglSurface)
544 				{
545 					egl.makeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
546 					egl.destroySurface(m_eglDisplay, m_eglSurface);
547 					m_eglSurface = EGL_NO_SURFACE;
548 				}
549 
550 				delete m_window;
551 				m_window = DE_NULL;
552 
553 				throw tcu::ResourceError(string("Failed to re-create window: ") + e.what());
554 			}
555 		}
556 
557 		if (!swapOk)
558 		{
559 			DE_ASSERT(badWindow);
560 			throw tcu::ResourceError(string("eglSwapBuffers() failed: ") + getErrorStr(error).toString());
561 		}
562 
563 		// Refresh dimensions
564 		{
565 			int	newWidth	= 0;
566 			int	newHeight	= 0;
567 
568 			egl.querySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH,		&newWidth);
569 			egl.querySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT,	&newHeight);
570 			EGLU_CHECK_MSG(egl, "Failed to query window size");
571 
572 			if (newWidth	!= m_glRenderTarget.getWidth() ||
573 				newHeight	!= m_glRenderTarget.getHeight())
574 			{
575 				tcu::print("Warning: Window size changed (%dx%d -> %dx%d), test results might be invalid!\n",
576 						   m_glRenderTarget.getWidth(), m_glRenderTarget.getHeight(), newWidth, newHeight);
577 
578 				m_glRenderTarget = tcu::RenderTarget(newWidth, newHeight,
579 													 m_glRenderTarget.getPixelFormat(),
580 													 m_glRenderTarget.getDepthBits(),
581 													 m_glRenderTarget.getStencilBits(),
582 													 m_glRenderTarget.getNumSamples());
583 			}
584 		}
585 	}
586 	else
587 		m_glFunctions.flush();
588 }
589 
590 } // anonymous
591 
GLContextFactory(const NativeDisplayFactoryRegistry & displayFactoryRegistry)592 GLContextFactory::GLContextFactory (const NativeDisplayFactoryRegistry& displayFactoryRegistry)
593 	: glu::ContextFactory		("egl", "EGL OpenGL Context")
594 	, m_displayFactoryRegistry	(displayFactoryRegistry)
595 {
596 }
597 
createContext(const glu::RenderConfig & config,const tcu::CommandLine & cmdLine,const glu::RenderContext * sharedContext) const598 glu::RenderContext* GLContextFactory::createContext (const glu::RenderConfig& config, const tcu::CommandLine& cmdLine, const glu::RenderContext *sharedContext) const
599 {
600 	const NativeDisplayFactory& displayFactory = selectNativeDisplayFactory(m_displayFactoryRegistry, cmdLine);
601 
602 	const NativeWindowFactory*	windowFactory;
603 	const NativePixmapFactory*	pixmapFactory;
604 
605 	try
606 	{
607 		windowFactory = &selectNativeWindowFactory(displayFactory, cmdLine);
608 	}
609 	catch (const tcu::NotSupportedError&)
610 	{
611 		windowFactory = DE_NULL;
612 	}
613 
614 	try
615 	{
616 		pixmapFactory = &selectNativePixmapFactory(displayFactory, cmdLine);
617 	}
618 	catch (const tcu::NotSupportedError&)
619 	{
620 		pixmapFactory = DE_NULL;
621 	}
622 
623 	return new RenderContext(&displayFactory, windowFactory, pixmapFactory, config, sharedContext);
624 }
625 
626 } // eglu
627