@@ -10,7 +10,8 @@ class AllegroEGL
10
10
{
11
11
private static final String TAG = "AllegroEGL" ;
12
12
13
- private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098 ;
13
+ private static final int EGL_CONTEXT_MAJOR_VERSION = 0x3098 ;
14
+ private static final int EGL_CONTEXT_MINOR_VERSION = 0x30fb ;
14
15
private static final int EGL_OPENGL_ES_BIT = 1 ;
15
16
private static final int EGL_OPENGL_ES2_BIT = 4 ;
16
17
@@ -184,11 +185,26 @@ void egl_getConfigAttribs(int index, int ret[])
184
185
}
185
186
}
186
187
188
+ private int [] versionAttribList (int major , int minor )
189
+ {
190
+ return new int [] {
191
+ EGL_CONTEXT_MAJOR_VERSION , major ,
192
+ EGL_CONTEXT_MINOR_VERSION , minor ,
193
+ EGL10 .EGL_NONE
194
+ };
195
+ }
196
+
197
+ private int versionCode (int major , int minor )
198
+ {
199
+ return (major << 8 ) | minor ;
200
+ }
201
+
187
202
/* Return values:
188
203
* 0 - failure
189
204
* 1 - success
190
205
*/
191
- int egl_createContext (int configIndex , boolean programmable_pipeline )
206
+ int egl_createContext (int configIndex , boolean programmable_pipeline ,
207
+ int major , int minor , boolean isRequiredMajor , boolean isRequiredMinor )
192
208
{
193
209
Log .d (TAG , "egl_createContext" );
194
210
@@ -198,21 +214,55 @@ int egl_createContext(int configIndex, boolean programmable_pipeline)
198
214
matchingConfigs = null ;
199
215
attribMap = null ;
200
216
201
- int version = (programmable_pipeline ) ? 2 : 1 ;
202
- int [] attribs = {
203
- EGL_CONTEXT_CLIENT_VERSION , version ,
204
- EGL10 .EGL_NONE
205
- };
217
+ // we'll attempt to create a GLES context of version major.minor.
218
+ // if major == minor == 0, then the user did not request a specific version.
219
+ // minMajor.minMinor is the minimum acceptable version.
220
+
221
+ int minMajor = (programmable_pipeline || major >= 2 ) ? 2 : 1 ;
222
+ int minMinor = 0 ;
223
+
224
+ if (isRequiredMajor && major > minMajor )
225
+ minMajor = major ;
226
+ if (isRequiredMinor && minor > minMinor )
227
+ minMinor = minor ;
228
+
229
+ int minVersion = versionCode (minMajor , minMinor );
230
+ int wantedVersion = versionCode (major , minor );
231
+ if (wantedVersion < minVersion ) {
232
+ if (isRequiredMajor || isRequiredMinor ) {
233
+ Log .d (TAG , "Can't require OpenGL ES version " + major + "." + minor );
234
+ return 0 ;
235
+ }
206
236
237
+ major = minMajor ;
238
+ minor = minMinor ;
239
+ wantedVersion = minVersion ;
240
+ }
241
+
242
+ Log .d (TAG , "egl_createContext: requesting OpenGL ES " + major + "." + minor );
243
+
244
+ // request a GLES context version major.minor
207
245
EGLContext ctx = egl .eglCreateContext (egl_Display , chosenConfig ,
208
- EGL10 .EGL_NO_CONTEXT , attribs );
246
+ EGL10 .EGL_NO_CONTEXT , versionAttribList ( major , minor ) );
209
247
if (ctx == EGL10 .EGL_NO_CONTEXT ) {
248
+ // failed to create a GLES context of the requested version
210
249
checkEglError ("eglCreateContext" , egl );
211
- Log .d (TAG , "egl_createContext no context" );
212
- return 0 ;
250
+ Log .d (TAG , "egl_createContext failed. min version is " +
251
+ minMajor + "." + minMinor );
252
+
253
+ // try the min version instead, unless the user required the failed version
254
+ if ((wantedVersion == minVersion ) || (EGL10 .EGL_NO_CONTEXT == (ctx =
255
+ egl .eglCreateContext (egl_Display , chosenConfig , EGL10 .EGL_NO_CONTEXT ,
256
+ versionAttribList (minMajor , minMinor ))
257
+ ))) {
258
+ // failed again
259
+ checkEglError ("eglCreateContext" , egl );
260
+ Log .d (TAG , "egl_createContext no context" );
261
+ return 0 ;
262
+ }
213
263
}
214
264
215
- Log .d (TAG , "EGL context (OpenGL ES " + version + ") created " );
265
+ Log .d (TAG , "egl_createContext: success " );
216
266
217
267
egl_Context = ctx ;
218
268
return 1 ;
0 commit comments