Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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.opengl.EGL14;
     20 import android.opengl.EGLExt;
     21 
     22 import javax.microedition.khronos.egl.EGL10;
     23 import javax.microedition.khronos.egl.EGLConfig;
     24 import javax.microedition.khronos.egl.EGLDisplay;
     25 
     26 final class Egl10Utils {
     27     private Egl10Utils() {
     28     }
     29 
     30     static EGLConfig chooseConfig(EGL10 egl, EGLDisplay display, int[] configSpec) {
     31         int[] value = new int[1];
     32         if (!egl.eglChooseConfig(display, configSpec, null, 0,
     33                 value)) {
     34             throw new IllegalArgumentException("eglChooseConfig failed");
     35         }
     36 
     37         int numConfigs = value[0];
     38         if (numConfigs <= 0) {
     39             throw new IllegalArgumentException("No configs match configSpec");
     40         }
     41 
     42         EGLConfig[] configs = new EGLConfig[numConfigs];
     43         if (!egl.eglChooseConfig(display, configSpec, configs, numConfigs, value)) {
     44             throw new IllegalArgumentException("eglChooseConfig#2 failed");
     45         }
     46         EGLConfig config = chooseConfig(egl, display, configs, configSpec);
     47         if (config == null) {
     48             throw new IllegalArgumentException("No config chosen");
     49         }
     50         return config;
     51     }
     52 
     53     static int[] filterConfigSpec(int[] configSpec, int version) {
     54         if (version != 2 && version != 3) {
     55             return configSpec;
     56         }
     57 
     58         int len = configSpec.length;
     59         int[] newConfigSpec = new int[len + 2];
     60         System.arraycopy(configSpec, 0, newConfigSpec, 0, len-1);
     61 
     62         newConfigSpec[len-1] = EGL10.EGL_RENDERABLE_TYPE;
     63         if (version == 2) {
     64             newConfigSpec[len] = EGL14.EGL_OPENGL_ES2_BIT;  /* EGL_OPENGL_ES2_BIT */
     65         } else {
     66             newConfigSpec[len] = EGLExt.EGL_OPENGL_ES3_BIT_KHR; /* EGL_OPENGL_ES3_BIT_KHR */
     67         }
     68         newConfigSpec[len + 1] = EGL10.EGL_NONE;
     69 
     70         return newConfigSpec;
     71     }
     72 
     73     private static EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
     74             EGLConfig[] configs, int[] configSpec) {
     75 
     76         int redSize = findValue(configSpec, EGL10.EGL_RED_SIZE);
     77         int greenSize = findValue(configSpec, EGL10.EGL_GREEN_SIZE);
     78         int blueSize = findValue(configSpec, EGL10.EGL_BLUE_SIZE);
     79         int alphaSize = findValue(configSpec, EGL10.EGL_ALPHA_SIZE);
     80         int depthSize = findValue(configSpec, EGL10.EGL_DEPTH_SIZE);
     81         int stencilSize = findValue(configSpec, EGL10.EGL_STENCIL_SIZE);
     82 
     83         for (EGLConfig config : configs) {
     84             int d = findConfigAttrib(egl, display, config, EGL10.EGL_DEPTH_SIZE);
     85             int s = findConfigAttrib(egl, display, config, EGL10.EGL_STENCIL_SIZE);
     86             if ((d >= depthSize) && (s >= stencilSize)) {
     87                 int r = findConfigAttrib(egl, display, config, EGL10.EGL_RED_SIZE);
     88                 int g = findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE);
     89                 int b = findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE);
     90                 int a = findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE);
     91                 if ((r == redSize) && (g == greenSize) && (b == blueSize) && (a == alphaSize)) {
     92                     return config;
     93                 }
     94             }
     95         }
     96         return null;
     97     }
     98 
     99     private static int findValue(int[] configSpec, int name) {
    100         for (int i = 0; i < configSpec.length; i += 2) {
    101             if (configSpec[i] == name) {
    102                 return configSpec[i + 1];
    103             }
    104         }
    105         return 0;
    106     }
    107 
    108     private static int findConfigAttrib(EGL10 egl, EGLDisplay display,
    109             EGLConfig config, int attribute) {
    110         int[] value = new int[1];
    111         if (egl.eglGetConfigAttrib(display, config, attribute, value)) {
    112             return value[0];
    113         }
    114         return 0;
    115     }
    116 }
    117