1e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
2e5c31af7Sopenharmony_ci * drawElements Quality Program Tester Core
3e5c31af7Sopenharmony_ci * ----------------------------------------
4e5c31af7Sopenharmony_ci *
5e5c31af7Sopenharmony_ci * Copyright 2014 The Android Open Source Project
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
8e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
9e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
10e5c31af7Sopenharmony_ci *
11e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
14e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
15e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
17e5c31af7Sopenharmony_ci * limitations under the License.
18e5c31af7Sopenharmony_ci *
19e5c31af7Sopenharmony_ci *//*!
20e5c31af7Sopenharmony_ci * \file
21e5c31af7Sopenharmony_ci * \brief EGL utilities for interfacing with GL APIs.
22e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "egluGLUtil.hpp"
25e5c31af7Sopenharmony_ci
26e5c31af7Sopenharmony_ci#include "egluUtil.hpp"
27e5c31af7Sopenharmony_ci#include "eglwLibrary.hpp"
28e5c31af7Sopenharmony_ci#include "eglwEnums.hpp"
29e5c31af7Sopenharmony_ci#include "glwEnums.hpp"
30e5c31af7Sopenharmony_ci
31e5c31af7Sopenharmony_ci#include <vector>
32e5c31af7Sopenharmony_ci
33e5c31af7Sopenharmony_ciusing std::vector;
34e5c31af7Sopenharmony_ci
35e5c31af7Sopenharmony_cinamespace eglu
36e5c31af7Sopenharmony_ci{
37e5c31af7Sopenharmony_ci
38e5c31af7Sopenharmony_ciusing namespace eglw;
39e5c31af7Sopenharmony_ci
40e5c31af7Sopenharmony_ciglw::GLenum getImageGLTarget (EGLenum source)
41e5c31af7Sopenharmony_ci{
42e5c31af7Sopenharmony_ci	switch (source)
43e5c31af7Sopenharmony_ci	{
44e5c31af7Sopenharmony_ci		case EGL_GL_TEXTURE_2D_KHR:						return GL_TEXTURE_2D;
45e5c31af7Sopenharmony_ci		case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:	return GL_TEXTURE_CUBE_MAP_POSITIVE_X;
46e5c31af7Sopenharmony_ci		case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:	return GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
47e5c31af7Sopenharmony_ci		case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:	return GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
48e5c31af7Sopenharmony_ci		case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:	return GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
49e5c31af7Sopenharmony_ci		case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:	return GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
50e5c31af7Sopenharmony_ci		case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:	return GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
51e5c31af7Sopenharmony_ci		case EGL_GL_TEXTURE_3D_KHR:						return GL_TEXTURE_3D;
52e5c31af7Sopenharmony_ci		case EGL_GL_RENDERBUFFER_KHR:					return GL_RENDERBUFFER;
53e5c31af7Sopenharmony_ci		default:	DE_FATAL("Impossible");				return GL_NONE;
54e5c31af7Sopenharmony_ci	}
55e5c31af7Sopenharmony_ci}
56e5c31af7Sopenharmony_ci
57e5c31af7Sopenharmony_ciEGLint apiRenderableType (glu::ApiType apiType)
58e5c31af7Sopenharmony_ci{
59e5c31af7Sopenharmony_ci	switch (apiType.getProfile())
60e5c31af7Sopenharmony_ci	{
61e5c31af7Sopenharmony_ci		case glu::PROFILE_CORE:
62e5c31af7Sopenharmony_ci		case glu::PROFILE_COMPATIBILITY:
63e5c31af7Sopenharmony_ci			return EGL_OPENGL_BIT;
64e5c31af7Sopenharmony_ci		case glu::PROFILE_ES:
65e5c31af7Sopenharmony_ci			switch (apiType.getMajorVersion())
66e5c31af7Sopenharmony_ci			{
67e5c31af7Sopenharmony_ci				case 1:		return EGL_OPENGL_ES_BIT;
68e5c31af7Sopenharmony_ci				case 2:		return EGL_OPENGL_ES2_BIT;
69e5c31af7Sopenharmony_ci				case 3:		return EGL_OPENGL_ES3_BIT_KHR;
70e5c31af7Sopenharmony_ci				default:	DE_FATAL("Unknown OpenGL ES version"); break;
71e5c31af7Sopenharmony_ci			}
72e5c31af7Sopenharmony_ci			break;
73e5c31af7Sopenharmony_ci		default:
74e5c31af7Sopenharmony_ci			DE_FATAL("Unknown GL API");
75e5c31af7Sopenharmony_ci	}
76e5c31af7Sopenharmony_ci
77e5c31af7Sopenharmony_ci	return 0;
78e5c31af7Sopenharmony_ci}
79e5c31af7Sopenharmony_ci
80e5c31af7Sopenharmony_ciEGLContext createGLContext (const Library&					egl,
81e5c31af7Sopenharmony_ci							EGLDisplay						display,
82e5c31af7Sopenharmony_ci							EGLContext						eglConfig,
83e5c31af7Sopenharmony_ci							const glu::ContextType&			contextType,
84e5c31af7Sopenharmony_ci							eglw::EGLContext				sharedContext,
85e5c31af7Sopenharmony_ci							glu::ResetNotificationStrategy	resetNotificationStrategy)
86e5c31af7Sopenharmony_ci{
87e5c31af7Sopenharmony_ci	const bool			khrCreateContextSupported			= hasExtension(egl, display, "EGL_KHR_create_context");
88e5c31af7Sopenharmony_ci	const bool			khrCreateContextNoErrorSupported	= hasExtension(egl, display, "EGL_KHR_create_context_no_error");
89e5c31af7Sopenharmony_ci	EGLContext			context								= EGL_NO_CONTEXT;
90e5c31af7Sopenharmony_ci	EGLenum				api									= EGL_NONE;
91e5c31af7Sopenharmony_ci	vector<EGLint>		attribList;
92e5c31af7Sopenharmony_ci
93e5c31af7Sopenharmony_ci	if (glu::isContextTypeES(contextType))
94e5c31af7Sopenharmony_ci	{
95e5c31af7Sopenharmony_ci		api = EGL_OPENGL_ES_API;
96e5c31af7Sopenharmony_ci
97e5c31af7Sopenharmony_ci		if (contextType.getMajorVersion() <= 2)
98e5c31af7Sopenharmony_ci		{
99e5c31af7Sopenharmony_ci			attribList.push_back(EGL_CONTEXT_CLIENT_VERSION);
100e5c31af7Sopenharmony_ci			attribList.push_back(contextType.getMajorVersion());
101e5c31af7Sopenharmony_ci		}
102e5c31af7Sopenharmony_ci		else
103e5c31af7Sopenharmony_ci		{
104e5c31af7Sopenharmony_ci			if (!khrCreateContextSupported)
105e5c31af7Sopenharmony_ci				TCU_THROW(NotSupportedError, "EGL_KHR_create_context is required for OpenGL ES 3.0 and newer");
106e5c31af7Sopenharmony_ci
107e5c31af7Sopenharmony_ci			attribList.push_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
108e5c31af7Sopenharmony_ci			attribList.push_back(contextType.getMajorVersion());
109e5c31af7Sopenharmony_ci			attribList.push_back(EGL_CONTEXT_MINOR_VERSION_KHR);
110e5c31af7Sopenharmony_ci			attribList.push_back(contextType.getMinorVersion());
111e5c31af7Sopenharmony_ci		}
112e5c31af7Sopenharmony_ci	}
113e5c31af7Sopenharmony_ci	else
114e5c31af7Sopenharmony_ci	{
115e5c31af7Sopenharmony_ci		DE_ASSERT(glu::isContextTypeGLCore(contextType) || glu::isContextTypeGLCompatibility(contextType));
116e5c31af7Sopenharmony_ci
117e5c31af7Sopenharmony_ci		if (!khrCreateContextSupported)
118e5c31af7Sopenharmony_ci			TCU_THROW(NotSupportedError, "EGL_KHR_create_context is required for OpenGL context creation");
119e5c31af7Sopenharmony_ci
120e5c31af7Sopenharmony_ci		api = EGL_OPENGL_API;
121e5c31af7Sopenharmony_ci
122e5c31af7Sopenharmony_ci		attribList.push_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
123e5c31af7Sopenharmony_ci		attribList.push_back(contextType.getMajorVersion());
124e5c31af7Sopenharmony_ci		attribList.push_back(EGL_CONTEXT_MINOR_VERSION_KHR);
125e5c31af7Sopenharmony_ci		attribList.push_back(contextType.getMinorVersion());
126e5c31af7Sopenharmony_ci		attribList.push_back(EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR);
127e5c31af7Sopenharmony_ci		attribList.push_back(glu::isContextTypeGLCore(contextType) ? EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
128e5c31af7Sopenharmony_ci																   : EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR);
129e5c31af7Sopenharmony_ci	}
130e5c31af7Sopenharmony_ci
131e5c31af7Sopenharmony_ci	if (contextType.getFlags() != glu::ContextFlags(0))
132e5c31af7Sopenharmony_ci	{
133e5c31af7Sopenharmony_ci		EGLint flags = 0;
134e5c31af7Sopenharmony_ci
135e5c31af7Sopenharmony_ci		if (!khrCreateContextSupported)
136e5c31af7Sopenharmony_ci			TCU_THROW(NotSupportedError, "EGL_KHR_create_context is required for creating robust/debug/forward-compatible contexts");
137e5c31af7Sopenharmony_ci
138e5c31af7Sopenharmony_ci		if ((contextType.getFlags() & glu::CONTEXT_DEBUG) != 0)
139e5c31af7Sopenharmony_ci			flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
140e5c31af7Sopenharmony_ci
141e5c31af7Sopenharmony_ci		if ((contextType.getFlags() & glu::CONTEXT_ROBUST) != 0)
142e5c31af7Sopenharmony_ci		{
143e5c31af7Sopenharmony_ci			if (glu::isContextTypeES(contextType))
144e5c31af7Sopenharmony_ci			{
145e5c31af7Sopenharmony_ci				if (!hasExtension(egl, display, "EGL_EXT_create_context_robustness") && (getVersion(egl, display) < Version(1, 5)))
146e5c31af7Sopenharmony_ci					TCU_THROW(NotSupportedError, "EGL_EXT_create_context_robustness is required for creating robust context");
147e5c31af7Sopenharmony_ci
148e5c31af7Sopenharmony_ci				attribList.push_back(EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT);
149e5c31af7Sopenharmony_ci				attribList.push_back(EGL_TRUE);
150e5c31af7Sopenharmony_ci			}
151e5c31af7Sopenharmony_ci			else
152e5c31af7Sopenharmony_ci				flags |= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR;
153e5c31af7Sopenharmony_ci		}
154e5c31af7Sopenharmony_ci
155e5c31af7Sopenharmony_ci		if ((contextType.getFlags() & glu::CONTEXT_NO_ERROR) != 0)
156e5c31af7Sopenharmony_ci		{
157e5c31af7Sopenharmony_ci			if (khrCreateContextNoErrorSupported)
158e5c31af7Sopenharmony_ci			{
159e5c31af7Sopenharmony_ci				attribList.push_back(EGL_CONTEXT_OPENGL_NO_ERROR_KHR);
160e5c31af7Sopenharmony_ci				attribList.push_back(EGL_TRUE);
161e5c31af7Sopenharmony_ci			}
162e5c31af7Sopenharmony_ci			else
163e5c31af7Sopenharmony_ci				throw tcu::NotSupportedError("EGL_KHR_create_context_no_error is required for creating no-error contexts");
164e5c31af7Sopenharmony_ci		}
165e5c31af7Sopenharmony_ci
166e5c31af7Sopenharmony_ci		if ((contextType.getFlags() & glu::CONTEXT_FORWARD_COMPATIBLE) != 0)
167e5c31af7Sopenharmony_ci		{
168e5c31af7Sopenharmony_ci			if (!glu::isContextTypeGLCore(contextType))
169e5c31af7Sopenharmony_ci				TCU_THROW(InternalError, "Only OpenGL core contexts can be forward-compatible");
170e5c31af7Sopenharmony_ci
171e5c31af7Sopenharmony_ci			flags |= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR;
172e5c31af7Sopenharmony_ci		}
173e5c31af7Sopenharmony_ci
174e5c31af7Sopenharmony_ci		attribList.push_back(EGL_CONTEXT_FLAGS_KHR);
175e5c31af7Sopenharmony_ci		attribList.push_back(flags);
176e5c31af7Sopenharmony_ci
177e5c31af7Sopenharmony_ci		if (resetNotificationStrategy != glu::RESET_NOTIFICATION_STRATEGY_NOT_SPECIFIED)
178e5c31af7Sopenharmony_ci		{
179e5c31af7Sopenharmony_ci			if (getVersion(egl, display) >= Version(1, 5) || glu::isContextTypeGLCore(contextType) || glu::isContextTypeGLCompatibility(contextType))
180e5c31af7Sopenharmony_ci				attribList.push_back(EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR);
181e5c31af7Sopenharmony_ci			else if (hasExtension(egl, display, "EGL_EXT_create_context_robustness"))
182e5c31af7Sopenharmony_ci				attribList.push_back(EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT);
183e5c31af7Sopenharmony_ci			else
184e5c31af7Sopenharmony_ci				TCU_THROW(NotSupportedError, "EGL 1.5 or EGL_EXT_create_context_robustness is required for creating robust context");
185e5c31af7Sopenharmony_ci
186e5c31af7Sopenharmony_ci			if (resetNotificationStrategy == glu::RESET_NOTIFICATION_STRATEGY_NO_RESET_NOTIFICATION)
187e5c31af7Sopenharmony_ci				attribList.push_back(EGL_NO_RESET_NOTIFICATION_KHR);
188e5c31af7Sopenharmony_ci			else if (resetNotificationStrategy == glu::RESET_NOTIFICATION_STRATEGY_LOSE_CONTEXT_ON_RESET)
189e5c31af7Sopenharmony_ci				attribList.push_back(EGL_LOSE_CONTEXT_ON_RESET_KHR);
190e5c31af7Sopenharmony_ci			else
191e5c31af7Sopenharmony_ci				TCU_THROW(InternalError, "Unknown reset notification strategy");
192e5c31af7Sopenharmony_ci		}
193e5c31af7Sopenharmony_ci	}
194e5c31af7Sopenharmony_ci
195e5c31af7Sopenharmony_ci	attribList.push_back(EGL_NONE);
196e5c31af7Sopenharmony_ci
197e5c31af7Sopenharmony_ci	EGLU_CHECK_CALL(egl, bindAPI(api));
198e5c31af7Sopenharmony_ci	context = egl.createContext(display, eglConfig, sharedContext, &(attribList[0]));
199e5c31af7Sopenharmony_ci	EGLU_CHECK_MSG(egl, "eglCreateContext()");
200e5c31af7Sopenharmony_ci
201e5c31af7Sopenharmony_ci	return context;
202e5c31af7Sopenharmony_ci}
203e5c31af7Sopenharmony_ci
204e5c31af7Sopenharmony_cistatic bool configMatches (const eglw::Library& egl, eglw::EGLDisplay display, eglw::EGLConfig eglConfig, const glu::RenderConfig& renderConfig)
205e5c31af7Sopenharmony_ci{
206e5c31af7Sopenharmony_ci	// \todo [2014-03-12 pyry] Check other attributes like double-buffer bit.
207e5c31af7Sopenharmony_ci
208e5c31af7Sopenharmony_ci	{
209e5c31af7Sopenharmony_ci		EGLint		renderableType		= 0;
210e5c31af7Sopenharmony_ci		EGLint		requiredRenderable	= apiRenderableType(renderConfig.type.getAPI());
211e5c31af7Sopenharmony_ci
212e5c31af7Sopenharmony_ci		EGLU_CHECK_CALL(egl, getConfigAttrib(display, eglConfig, EGL_RENDERABLE_TYPE, &renderableType));
213e5c31af7Sopenharmony_ci
214e5c31af7Sopenharmony_ci		if ((renderableType & requiredRenderable) == 0)
215e5c31af7Sopenharmony_ci			return false;
216e5c31af7Sopenharmony_ci	}
217e5c31af7Sopenharmony_ci
218e5c31af7Sopenharmony_ci	if (renderConfig.surfaceType != glu::RenderConfig::SURFACETYPE_DONT_CARE)
219e5c31af7Sopenharmony_ci	{
220e5c31af7Sopenharmony_ci		EGLint		surfaceType		= 0;
221e5c31af7Sopenharmony_ci		EGLint		requiredSurface	= 0;
222e5c31af7Sopenharmony_ci
223e5c31af7Sopenharmony_ci		switch (renderConfig.surfaceType)
224e5c31af7Sopenharmony_ci		{
225e5c31af7Sopenharmony_ci			case glu::RenderConfig::SURFACETYPE_WINDOW:				requiredSurface = EGL_WINDOW_BIT;	break;
226e5c31af7Sopenharmony_ci			case glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE:	requiredSurface = EGL_PIXMAP_BIT;	break;
227e5c31af7Sopenharmony_ci			case glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC:	requiredSurface = EGL_PBUFFER_BIT;	break;
228e5c31af7Sopenharmony_ci			default:
229e5c31af7Sopenharmony_ci				DE_ASSERT(false);
230e5c31af7Sopenharmony_ci		}
231e5c31af7Sopenharmony_ci
232e5c31af7Sopenharmony_ci		EGLU_CHECK_CALL(egl, getConfigAttrib(display, eglConfig, EGL_SURFACE_TYPE, &surfaceType));
233e5c31af7Sopenharmony_ci
234e5c31af7Sopenharmony_ci		if ((surfaceType & requiredSurface) == 0)
235e5c31af7Sopenharmony_ci			return false;
236e5c31af7Sopenharmony_ci	}
237e5c31af7Sopenharmony_ci
238e5c31af7Sopenharmony_ci	{
239e5c31af7Sopenharmony_ci		static const struct
240e5c31af7Sopenharmony_ci		{
241e5c31af7Sopenharmony_ci			int	glu::RenderConfig::*field;
242e5c31af7Sopenharmony_ci			EGLint attrib;
243e5c31af7Sopenharmony_ci		} s_attribs[] =
244e5c31af7Sopenharmony_ci		{
245e5c31af7Sopenharmony_ci			{ &glu::RenderConfig::id,			EGL_CONFIG_ID		},
246e5c31af7Sopenharmony_ci			{ &glu::RenderConfig::redBits,		EGL_RED_SIZE		},
247e5c31af7Sopenharmony_ci			{ &glu::RenderConfig::greenBits,	EGL_GREEN_SIZE		},
248e5c31af7Sopenharmony_ci			{ &glu::RenderConfig::blueBits,		EGL_BLUE_SIZE		},
249e5c31af7Sopenharmony_ci			{ &glu::RenderConfig::alphaBits,	EGL_ALPHA_SIZE		},
250e5c31af7Sopenharmony_ci			{ &glu::RenderConfig::depthBits,	EGL_DEPTH_SIZE		},
251e5c31af7Sopenharmony_ci			{ &glu::RenderConfig::stencilBits,	EGL_STENCIL_SIZE	},
252e5c31af7Sopenharmony_ci			{ &glu::RenderConfig::numSamples,	EGL_SAMPLES			},
253e5c31af7Sopenharmony_ci		};
254e5c31af7Sopenharmony_ci
255e5c31af7Sopenharmony_ci		for (int attribNdx = 0; attribNdx < DE_LENGTH_OF_ARRAY(s_attribs); attribNdx++)
256e5c31af7Sopenharmony_ci		{
257e5c31af7Sopenharmony_ci			if (renderConfig.*s_attribs[attribNdx].field != glu::RenderConfig::DONT_CARE)
258e5c31af7Sopenharmony_ci			{
259e5c31af7Sopenharmony_ci				EGLint value = 0;
260e5c31af7Sopenharmony_ci				EGLU_CHECK_CALL(egl, getConfigAttrib(display, eglConfig, s_attribs[attribNdx].attrib, &value));
261e5c31af7Sopenharmony_ci				if (value != renderConfig.*s_attribs[attribNdx].field)
262e5c31af7Sopenharmony_ci					return false;
263e5c31af7Sopenharmony_ci			}
264e5c31af7Sopenharmony_ci		}
265e5c31af7Sopenharmony_ci	}
266e5c31af7Sopenharmony_ci
267e5c31af7Sopenharmony_ci	return true;
268e5c31af7Sopenharmony_ci}
269e5c31af7Sopenharmony_ci
270e5c31af7Sopenharmony_ciEGLConfig chooseConfig (const Library& egl, EGLDisplay display, const glu::RenderConfig& config)
271e5c31af7Sopenharmony_ci{
272e5c31af7Sopenharmony_ci	const std::vector<EGLConfig> configs = eglu::getConfigs(egl, display);
273e5c31af7Sopenharmony_ci
274e5c31af7Sopenharmony_ci	for (vector<EGLConfig>::const_iterator iter = configs.begin(); iter != configs.end(); ++iter)
275e5c31af7Sopenharmony_ci	{
276e5c31af7Sopenharmony_ci		if (configMatches(egl, display, *iter, config))
277e5c31af7Sopenharmony_ci			return *iter;
278e5c31af7Sopenharmony_ci	}
279e5c31af7Sopenharmony_ci
280e5c31af7Sopenharmony_ci	throw tcu::NotSupportedError("Matching EGL config not found", DE_NULL, __FILE__, __LINE__);
281e5c31af7Sopenharmony_ci}
282e5c31af7Sopenharmony_ci
283e5c31af7Sopenharmony_ci}
284