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 static org.junit.Assert.assertTrue; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.opengl.GLSurfaceView; 24 import android.os.Bundle; 25 import android.util.Log; 26 import android.view.WindowManager; 27 28 import java.util.concurrent.CountDownLatch; 29 import java.util.concurrent.Executor; 30 import java.util.concurrent.Executors; 31 import java.util.concurrent.TimeUnit; 32 33 import javax.microedition.khronos.egl.EGL10; 34 import javax.microedition.khronos.egl.EGLConfig; 35 import javax.microedition.khronos.egl.EGLContext; 36 import javax.microedition.khronos.egl.EGLDisplay; 37 38 /** 39 * {@link Activity} with a {@link GLSurfaceView} that chooses a specific configuration. 40 */ 41 public class EglConfigCtsActivity extends Activity { 42 43 private final String TAG = this.getClass().getSimpleName(); 44 45 public static final String CONFIG_ID_EXTRA = "eglConfigId"; 46 47 public static final String CONTEXT_CLIENT_VERSION_EXTRA = "eglContextClientVersion"; 48 49 private static final int EGL_OPENGL_ES_BIT = 0x1; 50 private static final int EGL_OPENGL_ES2_BIT = 0x4; 51 52 private EglConfigGLSurfaceView mView; 53 54 private CountDownLatch mFinishedDrawing; 55 private CountDownLatch mFinishedTesting; 56 57 @Override onCreate(Bundle savedInstanceState)58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 61 // Dismiss keyguard and keep screen on while this test is on. 62 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 63 WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | 64 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 65 66 int[] configIds = getEglConfigIds(EGL_OPENGL_ES_BIT); 67 int[] configIds2 = getEglConfigIds(EGL_OPENGL_ES2_BIT); 68 assertTrue(configIds.length + configIds2.length > 0); 69 mFinishedTesting = new CountDownLatch(configIds.length + configIds2.length); 70 try { 71 runConfigTests(configIds, 1); 72 runConfigTests(configIds2, 2); 73 } catch (InterruptedException e) { 74 Log.e(TAG, "Caught exception"); 75 } 76 } 77 78 @Override onResume()79 protected void onResume() { 80 super.onResume(); 81 if (mView != null) 82 { 83 mView.onResume(); 84 } 85 } 86 87 @Override onPause()88 protected void onPause() { 89 super.onPause(); 90 if (mView != null) 91 { 92 mView.onPause(); 93 } 94 } 95 runConfigTests(int[] configIds, int contextClientVersion)96 private void runConfigTests(int[] configIds, int contextClientVersion) 97 throws InterruptedException { 98 Context context = this; 99 Thread thread = new Thread() { 100 @Override 101 public void run() { 102 for (int configId : configIds) { 103 mFinishedDrawing = new CountDownLatch(1); 104 105 runOnUiThread(new Runnable() { 106 @Override 107 public void run() { 108 setTitle("EGL Config Id: " + configId + " Client Version: " + contextClientVersion); 109 mView = new EglConfigGLSurfaceView(context, configId, contextClientVersion, new Runnable() { 110 @Override 111 public void run() { 112 mFinishedDrawing.countDown(); 113 } 114 }); 115 setContentView(mView); 116 } 117 }); 118 119 try { 120 waitToFinishDrawing(); 121 } catch (Exception e) { 122 Log.e(TAG, "Timed out!"); 123 } 124 125 mFinishedTesting.countDown(); 126 } 127 } 128 }; 129 thread.start(); 130 } 131 waitToFinishDrawing()132 private void waitToFinishDrawing() throws InterruptedException { 133 if (!mFinishedDrawing.await(5, TimeUnit.SECONDS)) { 134 throw new IllegalStateException("Couldn't finish drawing frames!"); 135 } 136 } 137 waitToFinishTesting()138 void waitToFinishTesting() throws InterruptedException { 139 if (!mFinishedTesting.await(300, TimeUnit.SECONDS)) { 140 throw new IllegalStateException("Couldn't finish testing!"); 141 } 142 } 143 getEglConfigIds(int renderableType)144 private static int[] getEglConfigIds(int renderableType) { 145 EGL10 egl = (EGL10) EGLContext.getEGL(); 146 EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); 147 int[] numConfigs = new int[1]; 148 149 int[] attributeList = new int[] { 150 EGL10.EGL_RENDERABLE_TYPE, renderableType, 151 152 // Avoid configs like RGBA0008 which crash even though they have the window bit set. 153 EGL10.EGL_RED_SIZE, 1, 154 EGL10.EGL_GREEN_SIZE, 1, 155 EGL10.EGL_BLUE_SIZE, 1, 156 157 EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT, 158 EGL10.EGL_NONE 159 }; 160 161 if (egl.eglInitialize(display, null)) { 162 try { 163 if (egl.eglChooseConfig(display, attributeList, null, 0, numConfigs)) { 164 EGLConfig[] configs = new EGLConfig[numConfigs[0]]; 165 if (egl.eglChooseConfig(display, attributeList, configs, configs.length, 166 numConfigs)) { 167 int[] configIds = new int[numConfigs[0]]; 168 for (int i = 0; i < numConfigs[0]; i++) { 169 int[] value = new int[1]; 170 if (egl.eglGetConfigAttrib(display, configs[i], EGL10.EGL_CONFIG_ID, 171 value)) { 172 configIds[i] = value[0]; 173 } else { 174 throw new IllegalStateException("Couldn't call eglGetConfigAttrib"); 175 } 176 } 177 return configIds; 178 } else { 179 throw new IllegalStateException("Couldn't call eglChooseConfig (1)"); 180 } 181 } else { 182 throw new IllegalStateException("Couldn't call eglChooseConfig (2)"); 183 } 184 } finally { 185 egl.eglTerminate(display); 186 } 187 } else { 188 throw new IllegalStateException("Couldn't initialize EGL."); 189 } 190 } 191 } 192