1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.opengl.cts; 18 19 import android.app.Instrumentation; 20 import android.os.Bundle; 21 import android.test.ActivityInstrumentationTestCase2; 22 23 import javax.microedition.khronos.egl.EGL10; 24 import javax.microedition.khronos.egl.EGLConfig; 25 import javax.microedition.khronos.egl.EGLContext; 26 import javax.microedition.khronos.egl.EGLDisplay; 27 28 /** 29 * Test that gets a list of EGL configurations and tries to use each one in a GLSurfaceView. 30 */ 31 public class EglConfigTest extends ActivityInstrumentationTestCase2<EglConfigCtsActivity> { 32 33 private static final int EGL_OPENGL_ES_BIT = 0x1; 34 35 private static final int EGL_OPENGL_ES2_BIT = 0x4; 36 37 private Instrumentation mInstrumentation; 38 39 public EglConfigTest() { 40 super("com.android.cts.graphics", EglConfigCtsActivity.class); 41 } 42 43 @Override 44 protected void setUp() throws Exception { 45 super.setUp(); 46 mInstrumentation = getInstrumentation(); 47 } 48 49 public void testEglConfigs() throws Exception { 50 int[] configIds = getEglConfigIds(EGL_OPENGL_ES_BIT); 51 int[] configIds2 = getEglConfigIds(EGL_OPENGL_ES2_BIT); 52 assertTrue(configIds.length + configIds2.length > 0); 53 runConfigTests(configIds, 1); 54 runConfigTests(configIds2, 2); 55 } 56 57 private void runConfigTests(int[] configIds, int contextClientVersion) 58 throws InterruptedException { 59 for (int configId : configIds) { 60 Bundle extras = new Bundle(); 61 extras.putInt(EglConfigCtsActivity.CONFIG_ID_EXTRA, configId); 62 extras.putInt(EglConfigCtsActivity.CONTEXT_CLIENT_VERSION_EXTRA, contextClientVersion); 63 EglConfigCtsActivity activity = launchActivity("com.android.cts.graphics", 64 EglConfigCtsActivity.class, extras); 65 activity.waitToFinishDrawing(); 66 activity.finish(); 67 mInstrumentation.waitForIdleSync(); 68 } 69 } 70 71 private static int[] getEglConfigIds(int renderableType) { 72 EGL10 egl = (EGL10) EGLContext.getEGL(); 73 EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); 74 int[] numConfigs = new int[1]; 75 76 int[] attributeList = new int[] { 77 EGL10.EGL_RENDERABLE_TYPE, renderableType, 78 79 // Avoid configs like RGBA0008 which crash even though they have the window bit set. 80 EGL10.EGL_RED_SIZE, 1, 81 EGL10.EGL_GREEN_SIZE, 1, 82 EGL10.EGL_BLUE_SIZE, 1, 83 84 EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT, 85 EGL10.EGL_NONE 86 }; 87 88 if (egl.eglInitialize(display, null)) { 89 try { 90 if (egl.eglChooseConfig(display, attributeList, null, 0, numConfigs)) { 91 EGLConfig[] configs = new EGLConfig[numConfigs[0]]; 92 if (egl.eglChooseConfig(display, attributeList, configs, configs.length, 93 numConfigs)) { 94 int[] configIds = new int[numConfigs[0]]; 95 for (int i = 0; i < numConfigs[0]; i++) { 96 int[] value = new int[1]; 97 if (egl.eglGetConfigAttrib(display, configs[i], EGL10.EGL_CONFIG_ID, 98 value)) { 99 configIds[i] = value[0]; 100 } else { 101 throw new IllegalStateException("Couldn't call eglGetConfigAttrib"); 102 } 103 } 104 return configIds; 105 } else { 106 throw new IllegalStateException("Couldn't call eglChooseConfig (1)"); 107 } 108 } else { 109 throw new IllegalStateException("Couldn't call eglChooseConfig (2)"); 110 } 111 } finally { 112 egl.eglTerminate(display); 113 } 114 } else { 115 throw new IllegalStateException("Couldn't initialize EGL."); 116 } 117 } 118 } 119