Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2009 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 package com.android.camera.ui;
     17 
     18 import android.opengl.GLSurfaceView.EGLConfigChooser;
     19 import javax.microedition.khronos.egl.EGL10;
     20 import javax.microedition.khronos.egl.EGLConfig;
     21 import javax.microedition.khronos.egl.EGLDisplay;
     22 
     23 /*
     24  * The code is copied/adapted from
     25  * <code>android.opengl.GLSurfaceView.BaseConfigChooser</code>. Here we try to
     26  * choose a configuration that support RGBA_8888 format and if possible,
     27  * with stencil buffer, but is not required.
     28  */
     29 class CameraEGLConfigChooser implements EGLConfigChooser {
     30 
     31     private static final int COLOR_BITS = 8;
     32 
     33     private int mStencilBits;
     34 
     35     private final int mConfigSpec[] = new int[] {
     36             EGL10.EGL_RED_SIZE, COLOR_BITS,
     37             EGL10.EGL_GREEN_SIZE, COLOR_BITS,
     38             EGL10.EGL_BLUE_SIZE, COLOR_BITS,
     39             EGL10.EGL_ALPHA_SIZE, COLOR_BITS,
     40             EGL10.EGL_NONE
     41     };
     42 
     43     public int getStencilBits() {
     44         return mStencilBits;
     45     }
     46 
     47     public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
     48         int[] numConfig = new int[1];
     49         if (!egl.eglChooseConfig(display, mConfigSpec, null, 0, numConfig)) {
     50             throw new RuntimeException("eglChooseConfig failed");
     51         }
     52 
     53         if (numConfig[0] <= 0) {
     54             throw new RuntimeException("No configs match configSpec");
     55         }
     56 
     57         EGLConfig[] configs = new EGLConfig[numConfig[0]];
     58         if (!egl.eglChooseConfig(display,
     59                 mConfigSpec, configs, configs.length, numConfig)) {
     60             throw new RuntimeException();
     61         }
     62 
     63         return chooseConfig(egl, display, configs);
     64     }
     65 
     66     private EGLConfig chooseConfig(
     67             EGL10 egl, EGLDisplay display, EGLConfig configs[]) {
     68 
     69         EGLConfig result = null;
     70         int minStencil = Integer.MAX_VALUE;
     71         int value[] = new int[1];
     72 
     73         // Because we need only one bit of stencil, try to choose a config that
     74         // has stencil support but with smallest number of stencil bits. If
     75         // none is found, choose any one.
     76         for (int i = 0, n = configs.length; i < n; ++i) {
     77             if (egl.eglGetConfigAttrib(
     78                     display, configs[i], EGL10.EGL_STENCIL_SIZE, value)) {
     79                 if (value[0] == 0) continue;
     80                 if (value[0] < minStencil) {
     81                     minStencil = value[0];
     82                     result = configs[i];
     83                 }
     84             } else {
     85                 throw new RuntimeException(
     86                         "eglGetConfigAttrib error: " + egl.eglGetError());
     87             }
     88         }
     89         if (result == null) result = configs[0];
     90         egl.eglGetConfigAttrib(
     91                 display, result, EGL10.EGL_STENCIL_SIZE, value);
     92         mStencilBits = value[0];
     93         return result;
     94     }
     95 }
     96