Some time ago I’ve made a short post (here) about the basics of how to get opengl infos on an android device. For an app I’m currently working on it was required to get all interesting information once more in my android device information application. The basic idea is to add a respective opengl 1.1 or opengl 2.0 and possibly even opengl 3.0+ context to the active view, load all information in the onSurfaceCreate method of the renderer and remove the view afterwards again.
You’d use it appropriatelly like that in your activity:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @Override public void onPostCreate(Bundle savedInstanceState) { final GPU gpu = new GPU(context); // opengl 1.1 info gpu.loadOpenGLGles10Info(new GPU.OnCompleteCallback<OpenGLGles10Info>() { @Override public void onComplete(final OpenGLGles10Info info) { String toString = info.toString(); } }); // opengl 2.0 info gpu.loadOpenGLGles10Info(new GPU.OnCompleteCallback<OpenGLGles20Info>() { @Override public void onComplete(final OpenGLGles20Info info) { String toString = info.toString(); } }); } |
Here is what I came up with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | package net.kibotu.android.deviceinfo; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.content.pm.ConfigurationInfo; import android.opengl.GLES10; import android.opengl.GLES20; import android.opengl.GLSurfaceView; import android.widget.FrameLayout; import javax.microedition.khronos.egl.EGL10; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.egl.EGLDisplay; import javax.microedition.khronos.opengles.GL10; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; public class GPU { // region OpenGLGles10 public static class OpenGLGles10Info extends OpenGLInfo { // general public String GL_RENDERER; public String GL_VERSION; public String GL_VENDOR; // texture related public int GL_MAX_TEXTURE_SIZE; public int GL_MAX_TEXTURE_UNITS; public int[] GL_MAX_VIEWPORT_DIMS; // fixed function pipeline constrains public int GL_MAX_MODELVIEW_STACK_DEPTH; public int GL_MAX_PROJECTION_STACK_DEPTH; public int GL_MAX_TEXTURE_STACK_DEPTH; public int GL_MAX_LIGHTS; public int GL_SUBPIXEL_BITS; public int GL_DEPTH_BITS; public int GL_STENCIL_BITS; public int GL_MAX_ELEMENTS_INDICES; public int GL_MAX_ELEMENTS_VERTICES; // extensions public String GL_EXTENSIONS; protected OpenGLGles10Info() { super(1); } @Override protected void loadOnCreate() { GL_RENDERER = glGetString(GLES10.GL_RENDERER); GL_VERSION = glGetString(GLES10.GL_VERSION); GL_VENDOR = glGetString(GLES10.GL_VENDOR); GL_MAX_TEXTURE_SIZE = glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE); GL_MAX_TEXTURE_UNITS = glGetIntegerv(GLES10.GL_MAX_TEXTURE_UNITS); GL_MAX_LIGHTS = glGetIntegerv(GLES10.GL_MAX_LIGHTS); GL_SUBPIXEL_BITS = glGetIntegerv(GLES10.GL_SUBPIXEL_BITS); GL_MAX_ELEMENTS_VERTICES = glGetIntegerv(GLES10.GL_MAX_ELEMENTS_VERTICES); GL_MAX_ELEMENTS_INDICES = glGetIntegerv(GLES10.GL_MAX_ELEMENTS_INDICES); GL_MAX_MODELVIEW_STACK_DEPTH = glGetIntegerv(GLES10.GL_MAX_MODELVIEW_STACK_DEPTH); GL_MAX_PROJECTION_STACK_DEPTH = glGetIntegerv(GLES10.GL_MAX_PROJECTION_STACK_DEPTH); GL_MAX_TEXTURE_STACK_DEPTH = glGetIntegerv(GLES10.GL_MAX_TEXTURE_STACK_DEPTH); GL_DEPTH_BITS = glGetIntegerv(GLES10.GL_DEPTH_BITS); GL_STENCIL_BITS = glGetIntegerv(GLES10.GL_STENCIL_BITS); GL_EXTENSIONS = glGetString(GLES10.GL_EXTENSIONS); GL_MAX_VIEWPORT_DIMS = glGetIntegerv(GLES10.GL_MAX_VIEWPORT_DIMS, 2); } @Override public String toString() { return "OpenGLGles10Info{" + '\n' + "GL_RENDERER='" + GL_RENDERER + '\'' + '\n' + ", GL_VERSION='" + GL_VERSION + '\'' + '\n' + ", GL_VENDOR='" + GL_VENDOR + '\'' + '\n' + ", GL_MAX_TEXTURE_SIZE=" + GL_MAX_TEXTURE_SIZE + '\n' + ", GL_MAX_TEXTURE_UNITS=" + GL_MAX_TEXTURE_UNITS + '\n' + ", GL_MAX_VIEWPORT_DIMS=" + Arrays.toString(GL_MAX_VIEWPORT_DIMS) + '\n' + ", GL_MAX_MODELVIEW_STACK_DEPTH=" + GL_MAX_MODELVIEW_STACK_DEPTH + '\n' + ", GL_MAX_PROJECTION_STACK_DEPTH=" + GL_MAX_PROJECTION_STACK_DEPTH + '\n' + ", GL_MAX_TEXTURE_STACK_DEPTH=" + GL_MAX_TEXTURE_STACK_DEPTH + ", GL_MAX_LIGHTS=" + GL_MAX_LIGHTS + '\n' + ", GL_SUBPIXEL_BITS=" + GL_SUBPIXEL_BITS + '\n' + ", GL_DEPTH_BITS=" + GL_DEPTH_BITS + '\n' + ", GL_STENCIL_BITS=" + GL_STENCIL_BITS + '\n' + ", GL_MAX_ELEMENTS_INDICES=" + GL_MAX_ELEMENTS_INDICES + '\n' + ", GL_MAX_ELEMENTS_VERTICES=" + GL_MAX_ELEMENTS_VERTICES + '\n' + ", GL_EXTENSIONS='" + GL_EXTENSIONS + '\'' + '\n' + '}'; } } // endregion // region OpenGLGles20 public static class OpenGLGles20Info extends OpenGLInfo { // general public String GL_RENDERER; public String GL_VERSION; public String GL_VENDOR; public String GL_SHADING_LANGUAGE_VERSION; // texture related public int GL_MAX_TEXTURE_SIZE; public int GL_MAX_TEXTURE_UNITS; public int GL_MAX_TEXTURE_IMAGE_UNITS; public int GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS; public int GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS; public int GL_MAX_CUBE_MAP_TEXTURE_SIZE; public int[] GL_MAX_VIEWPORT_DIMS; public int GL_MAX_RENDERBUFFER_SIZE; public boolean Vertex_Texture_Fetch; // shader constrains public int GL_MAX_VERTEX_UNIFORM_VECTORS; public int GL_MAX_VERTEX_ATTRIBS; public int GL_MAX_VARYING_VECTORS; public int GL_MAX_FRAGMENT_UNIFORM_VECTORS; // extensions public String GL_EXTENSIONS; // precision [] { -range, range, precision } public int[] GL_VERTEX_SHADER_GL_LOW_INT; public int[] GL_VERTEX_SHADER_GL_MEDIUM_INT; public int[] GL_VERTEX_SHADER_GL_HIGH_INT; public int[] GL_VERTEX_SHADER_GL_LOW_FLOAT; public int[] GL_VERTEX_SHADER_GL_MEDIUM_FLOAT; public int[] GL_VERTEX_SHADER_GL_HIGH_FLOAT; public int[] GL_FRAGMENT_SHADER_GL_LOW_INT; public int[] GL_FRAGMENT_SHADER_GL_MEDIUM_INT; public int[] GL_FRAGMENT_SHADER_GL_HIGH_INT; public int[] GL_FRAGMENT_SHADER_GL_LOW_FLOAT; public int[] GL_FRAGMENT_SHADER_GL_MEDIUM_FLOAT; public int[] GL_FRAGMENT_SHADER_GL_HIGH_FLOAT; protected OpenGLGles20Info() { super(GPU.supportsOpenGLES2() ? 2 : 1); } @Override protected void loadOnCreate() { GL_RENDERER = glGetString(GLES10.GL_RENDERER); GL_VERSION = glGetString(GLES10.GL_VERSION); GL_VENDOR = glGetString(GLES10.GL_VENDOR); GL_SHADING_LANGUAGE_VERSION = glGetString(GLES20.GL_SHADING_LANGUAGE_VERSION); GL_MAX_TEXTURE_SIZE = glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE); GL_MAX_TEXTURE_UNITS = glGetIntegerv(GLES10.GL_MAX_TEXTURE_UNITS); GL_MAX_VERTEX_ATTRIBS = glGetIntegerv(GLES20.GL_MAX_VERTEX_ATTRIBS); GL_MAX_VERTEX_UNIFORM_VECTORS = glGetIntegerv(GLES20.GL_MAX_VERTEX_UNIFORM_VECTORS); GL_MAX_FRAGMENT_UNIFORM_VECTORS = glGetIntegerv(GLES20.GL_MAX_FRAGMENT_UNIFORM_VECTORS); GL_MAX_VARYING_VECTORS = glGetIntegerv(GLES20.GL_MAX_VARYING_VECTORS); Vertex_Texture_Fetch = isVTFSupported(); GL_MAX_TEXTURE_IMAGE_UNITS = glGetIntegerv(GLES20.GL_MAX_TEXTURE_IMAGE_UNITS); GL_MAX_VIEWPORT_DIMS = glGetIntegerv(GLES10.GL_MAX_VIEWPORT_DIMS, 2); GL_EXTENSIONS = glGetString(GLES10.GL_EXTENSIONS); GL_MAX_RENDERBUFFER_SIZE = glGetIntegerv(GLES20.GL_MAX_RENDERBUFFER_SIZE); GL_MAX_CUBE_MAP_TEXTURE_SIZE = glGetIntegerv(GLES20.GL_MAX_CUBE_MAP_TEXTURE_SIZE); GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS = glGetIntegerv(GLES20.GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS); GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS = glGetIntegerv(GLES20.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS); GL_VERTEX_SHADER_GL_LOW_INT = glGetShaderPrecisionFormat(GLES20.GL_VERTEX_SHADER, GLES20.GL_LOW_INT); GL_VERTEX_SHADER_GL_MEDIUM_INT = glGetShaderPrecisionFormat(GLES20.GL_VERTEX_SHADER, GLES20.GL_MEDIUM_INT); GL_VERTEX_SHADER_GL_HIGH_INT = glGetShaderPrecisionFormat(GLES20.GL_VERTEX_SHADER, GLES20.GL_HIGH_INT); GL_VERTEX_SHADER_GL_LOW_FLOAT = glGetShaderPrecisionFormat(GLES20.GL_VERTEX_SHADER, GLES20.GL_LOW_FLOAT); GL_VERTEX_SHADER_GL_MEDIUM_FLOAT = glGetShaderPrecisionFormat(GLES20.GL_VERTEX_SHADER, GLES20.GL_MEDIUM_FLOAT); GL_VERTEX_SHADER_GL_HIGH_FLOAT = glGetShaderPrecisionFormat(GLES20.GL_VERTEX_SHADER, GLES20.GL_HIGH_FLOAT); GL_FRAGMENT_SHADER_GL_LOW_INT = glGetShaderPrecisionFormat(GLES20.GL_FRAGMENT_SHADER, GLES20.GL_LOW_INT); GL_FRAGMENT_SHADER_GL_MEDIUM_INT = glGetShaderPrecisionFormat(GLES20.GL_FRAGMENT_SHADER, GLES20.GL_MEDIUM_INT); GL_FRAGMENT_SHADER_GL_HIGH_INT = glGetShaderPrecisionFormat(GLES20.GL_FRAGMENT_SHADER, GLES20.GL_HIGH_INT); GL_FRAGMENT_SHADER_GL_LOW_FLOAT = glGetShaderPrecisionFormat(GLES20.GL_FRAGMENT_SHADER, GLES20.GL_LOW_FLOAT); GL_FRAGMENT_SHADER_GL_MEDIUM_FLOAT = glGetShaderPrecisionFormat(GLES20.GL_FRAGMENT_SHADER, GLES20.GL_MEDIUM_FLOAT); GL_FRAGMENT_SHADER_GL_HIGH_FLOAT = glGetShaderPrecisionFormat(GLES20.GL_FRAGMENT_SHADER, GLES20.GL_HIGH_FLOAT); } @Override public String toString() { return "OpenGLGles20Info{" + "GL_RENDERER='" + GL_RENDERER + '\'' + ", GL_VERSION='" + GL_VERSION + '\'' + ", GL_VENDOR='" + GL_VENDOR + '\'' + ", GL_SHADING_LANGUAGE_VERSION='" + GL_SHADING_LANGUAGE_VERSION + '\'' + ", GL_MAX_TEXTURE_SIZE=" + GL_MAX_TEXTURE_SIZE + ", GL_MAX_TEXTURE_UNITS=" + GL_MAX_TEXTURE_UNITS + ", GL_MAX_TEXTURE_IMAGE_UNITS=" + GL_MAX_TEXTURE_IMAGE_UNITS + ", GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS=" + GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS + ", GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=" + GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS + ", GL_MAX_CUBE_MAP_TEXTURE_SIZE=" + GL_MAX_CUBE_MAP_TEXTURE_SIZE + ", GL_MAX_VIEWPORT_DIMS=" + Arrays.toString(GL_MAX_VIEWPORT_DIMS) + ", GL_MAX_RENDERBUFFER_SIZE=" + GL_MAX_RENDERBUFFER_SIZE + ", Vertex_Texture_Fetch=" + Vertex_Texture_Fetch + ", GL_MAX_VERTEX_UNIFORM_VECTORS=" + GL_MAX_VERTEX_UNIFORM_VECTORS + ", GL_MAX_VERTEX_ATTRIBS=" + GL_MAX_VERTEX_ATTRIBS + ", GL_MAX_VARYING_VECTORS=" + GL_MAX_VARYING_VECTORS + ", GL_MAX_FRAGMENT_UNIFORM_VECTORS=" + GL_MAX_FRAGMENT_UNIFORM_VECTORS + ", GL_EXTENSIONS='" + GL_EXTENSIONS + '\'' + ", GL_VERTEX_SHADER_GL_LOW_INT=" + Arrays.toString(GL_VERTEX_SHADER_GL_LOW_INT) + ", GL_VERTEX_SHADER_GL_MEDIUM_INT=" + Arrays.toString(GL_VERTEX_SHADER_GL_MEDIUM_INT) + ", GL_VERTEX_SHADER_GL_HIGH_INT=" + Arrays.toString(GL_VERTEX_SHADER_GL_HIGH_INT) + ", GL_VERTEX_SHADER_GL_LOW_FLOAT=" + Arrays.toString(GL_VERTEX_SHADER_GL_LOW_FLOAT) + ", GL_VERTEX_SHADER_GL_MEDIUM_FLOAT=" + Arrays.toString(GL_VERTEX_SHADER_GL_MEDIUM_FLOAT) + ", GL_VERTEX_SHADER_GL_HIGH_FLOAT=" + Arrays.toString(GL_VERTEX_SHADER_GL_HIGH_FLOAT) + ", GL_FRAGMENT_SHADER_GL_LOW_INT=" + Arrays.toString(GL_FRAGMENT_SHADER_GL_LOW_INT) + ", GL_FRAGMENT_SHADER_GL_MEDIUM_INT=" + Arrays.toString(GL_FRAGMENT_SHADER_GL_MEDIUM_INT) + ", GL_FRAGMENT_SHADER_GL_HIGH_INT=" + Arrays.toString(GL_FRAGMENT_SHADER_GL_HIGH_INT) + ", GL_FRAGMENT_SHADER_GL_LOW_FLOAT=" + Arrays.toString(GL_FRAGMENT_SHADER_GL_LOW_FLOAT) + ", GL_FRAGMENT_SHADER_GL_MEDIUM_FLOAT=" + Arrays.toString(GL_FRAGMENT_SHADER_GL_MEDIUM_FLOAT) + ", GL_FRAGMENT_SHADER_GL_HIGH_FLOAT=" + Arrays.toString(GL_FRAGMENT_SHADER_GL_HIGH_FLOAT) + '}'; } } // endregion // region glGet private static Activity context; private volatile static IntBuffer buffer = IntBuffer.allocate(1); private volatile static IntBuffer buffer2 = IntBuffer.allocate(2); private volatile static int[] arrayBuffer = new int[1]; public GPU(final Activity context) { GPU.context = context; } public synchronized static String glGetString(int value) { return GLES10.glGetString(value); } public synchronized static int glGetIntegerv(int value) { buffer.clear(); GLES10.glGetIntegerv(value, buffer); return buffer.get(0); } public synchronized static int[] glGetIntegerv(int value, int size) { final IntBuffer buffer = IntBuffer.allocate(size); GLES10.glGetIntegerv(value, buffer); return buffer.array(); } public synchronized static int[] glGetShaderPrecisionFormat(int shaderType, int precisionType) { if (!DeviceOld.supportsOpenGLES2()) return new int[]{0, 0, 0}; buffer2.clear(); GLES20.glGetShaderPrecisionFormat(shaderType, precisionType, buffer2, buffer); return new int[]{buffer2.get(0), buffer2.get(1), buffer.get(0)}; } public synchronized static int eglGetConfigAttrib(int eglType, final EGL10 egl, final EGLDisplay display, final EGLConfig eglConfig) { egl.eglGetConfigAttrib(display, eglConfig, eglType, arrayBuffer); return arrayBuffer[0]; } public static boolean isVTFSupported() { GLES10.glGetIntegerv(GLES20.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, arrayBuffer, 0); return arrayBuffer[0] != 0; } private static int getOpenGLVersion() { final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); return configurationInfo.reqGlEsVersion; } private static boolean supportsOpenGLES2() { return getOpenGLVersion() >= 0x20000; } // endregion // region GPU public void loadOpenGLGles10Info(final OnCompleteCallback<OpenGLGles10Info> callback) { new InfoLoader<OpenGLGles10Info>(new OpenGLGles10Info()).loadInfo(callback); } public void loadOpenGLGles20Info(final OnCompleteCallback<OpenGLGles20Info> callback) { new InfoLoader<OpenGLGles20Info>(new OpenGLGles20Info()).loadInfo(callback); } // endregion // region interfaces public interface OnCompleteCallback<T> { void onComplete(final T result); } private static abstract class OpenGLInfo { final int eGLContextClientVersion; public ArrayList<Egl> eglconfigs; protected OpenGLInfo(final int eGLContextClientVersion) { this.eGLContextClientVersion = eGLContextClientVersion; eglconfigs = new ArrayList<Egl>(); } /** * Basically run this in a successfully running GLSurfaceView.Renderer.onSurfaceCreated. * Guaranties valid OpenGL context. */ protected abstract void loadOnCreate(); } // endregion // region InfoLoader public static class InfoLoader<T extends OpenGLInfo> { private volatile GPURenderer<T> renderer; private T info; protected InfoLoader(final T info) { this.info = info; } private void loadInfo(final OnCompleteCallback<T> callback) { context.runOnUiThread(new Runnable() { @Override public void run() { // new view final GLSurfaceView glView = new GLSurfaceView(context); // egl info glView.setEGLConfigChooser(new EglChooser<T>(info)); // need to be on top to be rendered at least for one frame glView.setZOrderOnTop(true); // create new renderer; note: we only need the oncreate method for all the infos renderer = new GPURenderer<T>(glView, info, callback); // set opengl version glView.setEGLContextClientVersion(info.eGLContextClientVersion); // set renderer glView.setRenderer(renderer); // add opengl view to current active view final FrameLayout layout = (FrameLayout) context.findViewById(android.R.id.content); // Note: needs to be layout of current active view layout.addView(glView); } }); } } // endregion // region GPURenderer private static class GPURenderer<T extends OpenGLInfo> implements GLSurfaceView.Renderer { private GLSurfaceView glSurfaceView; private T result; private OnCompleteCallback<T> callback; protected GPURenderer(final GLSurfaceView glSurfaceView, final T result, final OnCompleteCallback<T> callback) { this.result = result; this.glSurfaceView = glSurfaceView; this.callback = callback; } @Override public void onSurfaceCreated(final GL10 gl, final EGLConfig eglConfig) { // loadOnCreate info result.loadOnCreate(); // remove view context.runOnUiThread(new Runnable() { @Override public void run() { final FrameLayout layout = (FrameLayout) context.findViewById(android.R.id.content); layout.removeView(glSurfaceView); // call callback if (callback != null) callback.onComplete(result); } }); } @Override public void onSurfaceChanged(final GL10 gl10, final int width, final int height) { // do nothing } @Override public void onDrawFrame(final GL10 gl) { // do nothing } } final private static class EglChooser<T extends OpenGLInfo> implements GLSurfaceView.EGLConfigChooser { public T info; private EglChooser(final T info) { this.info = info; } @Override public EGLConfig chooseConfig(final EGL10 egl, final EGLDisplay display) { //Querying number of configurations final int[] num_conf = new int[1]; egl.eglGetConfigs(display, null, 0, num_conf); //if configuration array is null it still returns the number of configurations final int configurations = num_conf[0]; //Querying actual configurations final EGLConfig[] conf = new EGLConfig[configurations]; egl.eglGetConfigs(display, conf, configurations, num_conf); EGLConfig result = null; for (int i = 0; i < configurations; i++) { result = better(result, conf[i], egl, display); final Egl config = new Egl(egl, display, conf[i]); if (config.EGL_RED_SIZE + config.EGL_BLUE_SIZE + config.EGL_GREEN_SIZE + config.EGL_ALPHA_SIZE >= 16) info.eglconfigs.add(config); } return result; } /** * Returns the best of the two EGLConfig passed according to depth and colours * * @param a The first candidate * @param b The second candidate * @return The chosen candidate */ private EGLConfig better(EGLConfig a, EGLConfig b, EGL10 egl, EGLDisplay display) { if (a == null) return b; EGLConfig result; int[] value = new int[1]; egl.eglGetConfigAttrib(display, a, EGL10.EGL_DEPTH_SIZE, value); int depthA = value[0]; egl.eglGetConfigAttrib(display, b, EGL10.EGL_DEPTH_SIZE, value); int depthB = value[0]; if (depthA > depthB) result = a; else if (depthA < depthB) result = b; else //if depthA == depthB { egl.eglGetConfigAttrib(display, a, EGL10.EGL_RED_SIZE, value); int redA = value[0]; egl.eglGetConfigAttrib(display, b, EGL10.EGL_RED_SIZE, value); int redB = value[0]; if (redA > redB) result = a; else if (redA < redB) result = b; else //if redA == redB { //Don't care result = a; } } return result; } } public final static class Egl { public final int EGL_NON_CONFORMANT_CONFIG; public final int EGL_SAMPLES; public final int EGL_STENCIL_SIZE; public final int EGL_DEPTH_SIZE; public final int EGL_ALPHA_SIZE; public final int EGL_BLUE_SIZE; public final int EGL_GREEN_SIZE; public final int EGL_RED_SIZE; public Egl(final EGL10 egl, final EGLDisplay display, final EGLConfig eglConfig) { EGL_RED_SIZE = eglGetConfigAttrib(EGL10.EGL_RED_SIZE, egl, display, eglConfig); EGL_BLUE_SIZE = eglGetConfigAttrib(EGL10.EGL_BLUE_SIZE, egl, display, eglConfig); EGL_GREEN_SIZE = eglGetConfigAttrib(EGL10.EGL_GREEN_SIZE, egl, display, eglConfig); EGL_ALPHA_SIZE = eglGetConfigAttrib(EGL10.EGL_ALPHA_SIZE, egl, display, eglConfig); EGL_DEPTH_SIZE = eglGetConfigAttrib(EGL10.EGL_DEPTH_SIZE, egl, display, eglConfig); EGL_STENCIL_SIZE = eglGetConfigAttrib(EGL10.EGL_STENCIL_SIZE, egl, display, eglConfig); EGL_SAMPLES = eglGetConfigAttrib(EGL10.EGL_SAMPLES, egl, display, eglConfig); EGL_NON_CONFORMANT_CONFIG = eglGetConfigAttrib(EGL10.EGL_NON_CONFORMANT_CONFIG, egl, display, eglConfig); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Egl egl = (Egl) o; if (EGL_ALPHA_SIZE != egl.EGL_ALPHA_SIZE) return false; if (EGL_BLUE_SIZE != egl.EGL_BLUE_SIZE) return false; if (EGL_DEPTH_SIZE != egl.EGL_DEPTH_SIZE) return false; if (EGL_GREEN_SIZE != egl.EGL_GREEN_SIZE) return false; if (EGL_NON_CONFORMANT_CONFIG != egl.EGL_NON_CONFORMANT_CONFIG) return false; if (EGL_RED_SIZE != egl.EGL_RED_SIZE) return false; if (EGL_SAMPLES != egl.EGL_SAMPLES) return false; if (EGL_STENCIL_SIZE != egl.EGL_STENCIL_SIZE) return false; return true; } @Override public int hashCode() { int result = EGL_NON_CONFORMANT_CONFIG; result = 31 * result + EGL_SAMPLES; result = 31 * result + EGL_STENCIL_SIZE; result = 31 * result + EGL_DEPTH_SIZE; result = 31 * result + EGL_ALPHA_SIZE; result = 31 * result + EGL_BLUE_SIZE; result = 31 * result + EGL_GREEN_SIZE; result = 31 * result + EGL_RED_SIZE; return result; } @Override public String toString() { // rgba (rgba) depth stencil samples non comfort return "RGB" + (EGL_ALPHA_SIZE > 0 ? "A" : "") + " " + (EGL_RED_SIZE + EGL_GREEN_SIZE + EGL_BLUE_SIZE + EGL_ALPHA_SIZE) + " bit" + " (" + EGL_RED_SIZE + "" + EGL_GREEN_SIZE + EGL_BLUE_SIZE + "" + (EGL_ALPHA_SIZE > 0 ? EGL_ALPHA_SIZE : "") + ")" + (EGL_DEPTH_SIZE > 0 ? " Depth " + EGL_DEPTH_SIZE + "bit" : "") + (EGL_STENCIL_SIZE > 0 ? " Stencil " + EGL_STENCIL_SIZE : "") + (EGL_SAMPLES > 0 ? " Samples x" + EGL_SAMPLES : "") + (EGL_NON_CONFORMANT_CONFIG > 0 ? " Non-Conformant" : ""); } } // endregion } |
Enjoy. (: