Home | History | Annotate | Download | only in cts
      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.Instrumentation;
     22 import android.content.Intent;
     23 import android.support.test.InstrumentationRegistry;
     24 import android.support.test.filters.LargeTest;
     25 import android.support.test.rule.ActivityTestRule;
     26 import android.support.test.runner.AndroidJUnit4;
     27 
     28 import org.junit.Before;
     29 import org.junit.Rule;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     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  * Test that gets a list of EGL configurations and tries to use each one in a GLSurfaceView.
     40  */
     41 @LargeTest
     42 @RunWith(AndroidJUnit4.class)
     43 public class EglConfigTest {
     44     private static final int EGL_OPENGL_ES_BIT = 0x1;
     45     private static final int EGL_OPENGL_ES2_BIT = 0x4;
     46 
     47     private Instrumentation mInstrumentation;
     48 
     49     @Rule
     50     public ActivityTestRule<EglConfigCtsActivity> mActivityRule =
     51             new ActivityTestRule<>(EglConfigCtsActivity.class, false, false);
     52 
     53     @Before
     54     public void setup() {
     55         mInstrumentation = InstrumentationRegistry.getInstrumentation();
     56     }
     57 
     58     @Test
     59     public void testEglConfigs() throws Exception {
     60         int[] configIds = getEglConfigIds(EGL_OPENGL_ES_BIT);
     61         int[] configIds2 = getEglConfigIds(EGL_OPENGL_ES2_BIT);
     62         assertTrue(configIds.length + configIds2.length > 0);
     63         runConfigTests(configIds, 1);
     64         runConfigTests(configIds2, 2);
     65     }
     66 
     67     private void runConfigTests(int[] configIds, int contextClientVersion)
     68             throws InterruptedException {
     69         for (int configId : configIds) {
     70             Intent intent = new Intent(InstrumentationRegistry.getTargetContext(),
     71                     EglConfigCtsActivity.class);
     72             intent.putExtra(EglConfigCtsActivity.CONFIG_ID_EXTRA, configId);
     73             intent.putExtra(EglConfigCtsActivity.CONTEXT_CLIENT_VERSION_EXTRA,
     74                     contextClientVersion);
     75             EglConfigCtsActivity activity = mActivityRule.launchActivity(intent);
     76             activity.waitToFinishDrawing();
     77             // TODO(b/30948621): Remove the sleep below once b/30948621 is fixed.
     78             Thread.sleep(500);
     79             activity.finish();
     80             mInstrumentation.waitForIdleSync();
     81         }
     82     }
     83 
     84     private static int[] getEglConfigIds(int renderableType) {
     85         EGL10 egl = (EGL10) EGLContext.getEGL();
     86         EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
     87         int[] numConfigs = new int[1];
     88 
     89         int[] attributeList = new int[] {
     90                 EGL10.EGL_RENDERABLE_TYPE, renderableType,
     91 
     92                 // Avoid configs like RGBA0008 which crash even though they have the window bit set.
     93                 EGL10.EGL_RED_SIZE, 1,
     94                 EGL10.EGL_GREEN_SIZE, 1,
     95                 EGL10.EGL_BLUE_SIZE, 1,
     96 
     97                 EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT,
     98                 EGL10.EGL_NONE
     99         };
    100 
    101         if (egl.eglInitialize(display, null)) {
    102             try {
    103                 if (egl.eglChooseConfig(display, attributeList, null, 0, numConfigs)) {
    104                     EGLConfig[] configs = new EGLConfig[numConfigs[0]];
    105                     if (egl.eglChooseConfig(display, attributeList, configs, configs.length,
    106                             numConfigs)) {
    107                         int[] configIds = new int[numConfigs[0]];
    108                         for (int i = 0; i < numConfigs[0]; i++) {
    109                             int[] value = new int[1];
    110                             if (egl.eglGetConfigAttrib(display, configs[i], EGL10.EGL_CONFIG_ID,
    111                                     value)) {
    112                                 configIds[i] = value[0];
    113                             } else {
    114                               throw new IllegalStateException("Couldn't call eglGetConfigAttrib");
    115                             }
    116                         }
    117                         return configIds;
    118                     } else {
    119                         throw new IllegalStateException("Couldn't call eglChooseConfig (1)");
    120                     }
    121                 } else {
    122                     throw new IllegalStateException("Couldn't call eglChooseConfig (2)");
    123                 }
    124             } finally {
    125                 egl.eglTerminate(display);
    126             }
    127         } else {
    128             throw new IllegalStateException("Couldn't initialize EGL.");
    129         }
    130     }
    131 }
    132