Merge vk-gl-cts/vulkan-cts-1.0.2 into vk-gl-cts/master
[platform/upstream/VK-GL-CTS.git] / framework / egl / egluGLUtil.cpp
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 EGL utilities for interfacing with GL APIs.
22  *//*--------------------------------------------------------------------*/
23
24 #include "egluGLUtil.hpp"
25
26 #include "egluUtil.hpp"
27 #include "eglwLibrary.hpp"
28 #include "eglwEnums.hpp"
29 #include "glwEnums.hpp"
30
31 #include <vector>
32
33 using std::vector;
34
35 namespace eglu
36 {
37
38 using namespace eglw;
39
40 glw::GLenum getImageGLTarget (EGLenum source)
41 {
42         switch (source)
43         {
44                 case EGL_GL_TEXTURE_2D_KHR:                                             return GL_TEXTURE_2D;
45                 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:    return GL_TEXTURE_CUBE_MAP_POSITIVE_X;
46                 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:    return GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
47                 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:    return GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
48                 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:    return GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
49                 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:    return GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
50                 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:    return GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
51                 case EGL_GL_TEXTURE_3D_KHR:                                             return GL_TEXTURE_3D;
52                 case EGL_GL_RENDERBUFFER_KHR:                                   return GL_RENDERBUFFER;
53                 default:        DE_FATAL("Impossible");                         return GL_NONE;
54         }
55 }
56
57 EGLint apiRenderableType (glu::ApiType apiType)
58 {
59         switch (apiType.getProfile())
60         {
61                 case glu::PROFILE_CORE:
62                 case glu::PROFILE_COMPATIBILITY:
63                         return EGL_OPENGL_BIT;
64                 case glu::PROFILE_ES:
65                         switch (apiType.getMajorVersion())
66                         {
67                                 case 1:         return EGL_OPENGL_ES_BIT;
68                                 case 2:         return EGL_OPENGL_ES2_BIT;
69                                 case 3:         return EGL_OPENGL_ES3_BIT_KHR;
70                                 default:        DE_FATAL("Unknown OpenGL ES version");
71                         }
72                 default:
73                         DE_FATAL("Unknown GL API");
74         }
75
76         return 0;
77 }
78
79 EGLContext createGLContext (const Library&                                      egl,
80                                                         EGLDisplay                                              display,
81                                                         EGLContext                                              eglConfig,
82                                                         const glu::ContextType&                 contextType,
83                                                         glu::ResetNotificationStrategy  resetNotificationStrategy)
84 {
85         const bool                      khrCreateContextSupported       = hasExtension(egl, display, "EGL_KHR_create_context");
86         EGLContext                      context                                         = EGL_NO_CONTEXT;
87         EGLenum                         api                                                     = EGL_NONE;
88         vector<EGLint>          attribList;
89
90         if (glu::isContextTypeES(contextType))
91         {
92                 api = EGL_OPENGL_ES_API;
93
94                 if (contextType.getMajorVersion() <= 2)
95                 {
96                         attribList.push_back(EGL_CONTEXT_CLIENT_VERSION);
97                         attribList.push_back(contextType.getMajorVersion());
98                 }
99                 else
100                 {
101                         if (!khrCreateContextSupported)
102                                 TCU_THROW(NotSupportedError, "EGL_KHR_create_context is required for OpenGL ES 3.0 and newer");
103
104                         attribList.push_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
105                         attribList.push_back(contextType.getMajorVersion());
106                         attribList.push_back(EGL_CONTEXT_MINOR_VERSION_KHR);
107                         attribList.push_back(contextType.getMinorVersion());
108                 }
109         }
110         else
111         {
112                 DE_ASSERT(glu::isContextTypeGLCore(contextType) || glu::isContextTypeGLCompatibility(contextType));
113
114                 if (!khrCreateContextSupported)
115                         TCU_THROW(NotSupportedError, "EGL_KHR_create_context is required for OpenGL context creation");
116
117                 api = EGL_OPENGL_API;
118
119                 attribList.push_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
120                 attribList.push_back(contextType.getMajorVersion());
121                 attribList.push_back(EGL_CONTEXT_MINOR_VERSION_KHR);
122                 attribList.push_back(contextType.getMinorVersion());
123                 attribList.push_back(EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR);
124                 attribList.push_back(glu::isContextTypeGLCore(contextType) ? EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
125                                                                                                                                    : EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR);
126         }
127
128         if (contextType.getFlags() != glu::ContextFlags(0))
129         {
130                 EGLint flags = 0;
131
132                 if (!khrCreateContextSupported)
133                         TCU_THROW(NotSupportedError, "EGL_KHR_create_context is required for creating robust/debug/forward-compatible contexts");
134
135                 if ((contextType.getFlags() & glu::CONTEXT_DEBUG) != 0)
136                         flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
137
138                 if ((contextType.getFlags() & glu::CONTEXT_ROBUST) != 0)
139                 {
140                         if (glu::isContextTypeES(contextType))
141                         {
142                                 if (!hasExtension(egl, display, "EGL_EXT_create_context_robustness") && (getVersion(egl, display) < Version(1, 5)))
143                                         TCU_THROW(NotSupportedError, "EGL_EXT_create_context_robustness is required for creating robust context");
144
145                                 attribList.push_back(EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT);
146                                 attribList.push_back(EGL_TRUE);
147                         }
148                         else
149                                 flags |= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR;
150                 }
151
152                 if ((contextType.getFlags() & glu::CONTEXT_FORWARD_COMPATIBLE) != 0)
153                 {
154                         if (!glu::isContextTypeGLCore(contextType))
155                                 TCU_THROW(InternalError, "Only OpenGL core contexts can be forward-compatible");
156
157                         flags |= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR;
158                 }
159
160                 attribList.push_back(EGL_CONTEXT_FLAGS_KHR);
161                 attribList.push_back(flags);
162
163                 if (resetNotificationStrategy != glu::RESET_NOTIFICATION_STRATEGY_NOT_SPECIFIED)
164                 {
165                         if (getVersion(egl, display) >= Version(1, 5) || glu::isContextTypeGLCore(contextType) || glu::isContextTypeGLCompatibility(contextType))
166                                 attribList.push_back(EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR);
167                         else if (hasExtension(egl, display, "EGL_EXT_create_context_robustness"))
168                                 attribList.push_back(EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT);
169                         else
170                                 TCU_THROW(NotSupportedError, "EGL 1.5 or EGL_EXT_create_context_robustness is required for creating robust context");
171
172                         if (resetNotificationStrategy == glu::RESET_NOTIFICATION_STRATEGY_NO_RESET_NOTIFICATION)
173                                 attribList.push_back(EGL_NO_RESET_NOTIFICATION_KHR);
174                         else if (resetNotificationStrategy == glu::RESET_NOTIFICATION_STRATEGY_LOSE_CONTEXT_ON_RESET)
175                                 attribList.push_back(EGL_LOSE_CONTEXT_ON_RESET_KHR);
176                         else
177                                 TCU_THROW(InternalError, "Unknown reset notification strategy");
178                 }
179         }
180
181         attribList.push_back(EGL_NONE);
182
183         EGLU_CHECK_CALL(egl, bindAPI(api));
184         context = egl.createContext(display, eglConfig, EGL_NO_CONTEXT, &(attribList[0]));
185         EGLU_CHECK_MSG(egl, "eglCreateContext()");
186
187         return context;
188 }
189
190 static bool configMatches (const eglw::Library& egl, eglw::EGLDisplay display, eglw::EGLConfig eglConfig, const glu::RenderConfig& renderConfig)
191 {
192         // \todo [2014-03-12 pyry] Check other attributes like double-buffer bit.
193
194         {
195                 EGLint          renderableType          = 0;
196                 EGLint          requiredRenderable      = apiRenderableType(renderConfig.type.getAPI());
197
198                 EGLU_CHECK_CALL(egl, getConfigAttrib(display, eglConfig, EGL_RENDERABLE_TYPE, &renderableType));
199
200                 if ((renderableType & requiredRenderable) == 0)
201                         return false;
202         }
203
204         if (renderConfig.surfaceType != glu::RenderConfig::SURFACETYPE_DONT_CARE)
205         {
206                 EGLint          surfaceType             = 0;
207                 EGLint          requiredSurface = 0;
208
209                 switch (renderConfig.surfaceType)
210                 {
211                         case glu::RenderConfig::SURFACETYPE_WINDOW:                             requiredSurface = EGL_WINDOW_BIT;       break;
212                         case glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE:   requiredSurface = EGL_PIXMAP_BIT;       break;
213                         case glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC:  requiredSurface = EGL_PBUFFER_BIT;      break;
214                         default:
215                                 DE_ASSERT(false);
216                 }
217
218                 EGLU_CHECK_CALL(egl, getConfigAttrib(display, eglConfig, EGL_SURFACE_TYPE, &surfaceType));
219
220                 if ((surfaceType & requiredSurface) == 0)
221                         return false;
222         }
223
224         {
225                 static const struct
226                 {
227                         int     glu::RenderConfig::*field;
228                         EGLint attrib;
229                 } s_attribs[] =
230                 {
231                         { &glu::RenderConfig::id,                       EGL_CONFIG_ID           },
232                         { &glu::RenderConfig::redBits,          EGL_RED_SIZE            },
233                         { &glu::RenderConfig::greenBits,        EGL_GREEN_SIZE          },
234                         { &glu::RenderConfig::blueBits,         EGL_BLUE_SIZE           },
235                         { &glu::RenderConfig::alphaBits,        EGL_ALPHA_SIZE          },
236                         { &glu::RenderConfig::depthBits,        EGL_DEPTH_SIZE          },
237                         { &glu::RenderConfig::stencilBits,      EGL_STENCIL_SIZE        },
238                         { &glu::RenderConfig::numSamples,       EGL_SAMPLES                     },
239                 };
240
241                 for (int attribNdx = 0; attribNdx < DE_LENGTH_OF_ARRAY(s_attribs); attribNdx++)
242                 {
243                         if (renderConfig.*s_attribs[attribNdx].field != glu::RenderConfig::DONT_CARE)
244                         {
245                                 EGLint value = 0;
246                                 EGLU_CHECK_CALL(egl, getConfigAttrib(display, eglConfig, s_attribs[attribNdx].attrib, &value));
247                                 if (value != renderConfig.*s_attribs[attribNdx].field)
248                                         return false;
249                         }
250                 }
251         }
252
253         return true;
254 }
255
256 EGLConfig chooseConfig (const Library& egl, EGLDisplay display, const glu::RenderConfig& config)
257 {
258         const std::vector<EGLConfig> configs = eglu::getConfigs(egl, display);
259
260         for (vector<EGLConfig>::const_iterator iter = configs.begin(); iter != configs.end(); ++iter)
261         {
262                 if (configMatches(egl, display, *iter, config))
263                         return *iter;
264         }
265
266         throw tcu::NotSupportedError("Matching EGL config not found", DE_NULL, __FILE__, __LINE__);
267 }
268
269 }