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 */
16package com.android.gallery3d.ui;
17
18import android.opengl.GLSurfaceView.EGLConfigChooser;
19
20import com.android.gallery3d.common.ApiHelper;
21
22import javax.microedition.khronos.egl.EGL10;
23import javax.microedition.khronos.egl.EGLConfig;
24import javax.microedition.khronos.egl.EGLDisplay;
25
26/*
27 * The code is copied/adapted from
28 * <code>android.opengl.GLSurfaceView.BaseConfigChooser</code>. Here we try to
29 * choose a configuration that support RGBA_8888 format and if possible,
30 * with stencil buffer, but is not required.
31 */
32class GalleryEGLConfigChooser implements EGLConfigChooser {
33
34    private static final String TAG = "GalleryEGLConfigChooser";
35
36    private final int mConfigSpec565[] = new int[] {
37            EGL10.EGL_RED_SIZE, 5,
38            EGL10.EGL_GREEN_SIZE, 6,
39            EGL10.EGL_BLUE_SIZE, 5,
40            EGL10.EGL_ALPHA_SIZE, 0,
41            EGL10.EGL_NONE
42    };
43
44    private final int mConfigSpec888[] = new int[] {
45            EGL10.EGL_RED_SIZE, 8,
46            EGL10.EGL_GREEN_SIZE, 8,
47            EGL10.EGL_BLUE_SIZE, 8,
48            EGL10.EGL_ALPHA_SIZE, 0,
49            EGL10.EGL_NONE
50    };
51
52    @Override
53    public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
54        int[] numConfig = new int[1];
55        int mConfigSpec[] = ApiHelper.USE_888_PIXEL_FORMAT
56                ? mConfigSpec888 : mConfigSpec565;
57        if (!egl.eglChooseConfig(display, mConfigSpec, null, 0, numConfig)) {
58            throw new RuntimeException("eglChooseConfig failed");
59        }
60
61        if (numConfig[0] <= 0) {
62            throw new RuntimeException("No configs match configSpec");
63        }
64
65        EGLConfig[] configs = new EGLConfig[numConfig[0]];
66        if (!egl.eglChooseConfig(display,
67                mConfigSpec, configs, configs.length, numConfig)) {
68            throw new RuntimeException();
69        }
70
71        return chooseConfig(egl, display, configs);
72    }
73
74    private EGLConfig chooseConfig(
75            EGL10 egl, EGLDisplay display, EGLConfig configs[]) {
76
77        EGLConfig result = null;
78        int minStencil = Integer.MAX_VALUE;
79        int value[] = new int[1];
80
81        // Because we need only one bit of stencil, try to choose a config that
82        // has stencil support but with smallest number of stencil bits. If
83        // none is found, choose any one.
84        for (int i = 0, n = configs.length; i < n; ++i) {
85            if (!ApiHelper.USE_888_PIXEL_FORMAT) {
86                if (egl.eglGetConfigAttrib(
87                    display, configs[i], EGL10.EGL_RED_SIZE, value)) {
88                    // Filter out ARGB 8888 configs.
89                    if (value[0] == 8) continue;
90                }
91            }
92            if (egl.eglGetConfigAttrib(
93                    display, configs[i], EGL10.EGL_STENCIL_SIZE, value)) {
94                if (value[0] == 0) continue;
95                if (value[0] < minStencil) {
96                    minStencil = value[0];
97                    result = configs[i];
98                }
99            } else {
100                throw new RuntimeException(
101                        "eglGetConfigAttrib error: " + egl.eglGetError());
102            }
103        }
104        if (result == null) result = configs[0];
105        egl.eglGetConfigAttrib(
106                display, result, EGL10.EGL_STENCIL_SIZE, value);
107        logConfig(egl, display, result);
108        return result;
109    }
110
111    private static final int[] ATTR_ID = {
112            EGL10.EGL_RED_SIZE,
113            EGL10.EGL_GREEN_SIZE,
114            EGL10.EGL_BLUE_SIZE,
115            EGL10.EGL_ALPHA_SIZE,
116            EGL10.EGL_DEPTH_SIZE,
117            EGL10.EGL_STENCIL_SIZE,
118            EGL10.EGL_CONFIG_ID,
119            EGL10.EGL_CONFIG_CAVEAT
120    };
121
122    private static final String[] ATTR_NAME = {
123        "R", "G", "B", "A", "D", "S", "ID", "CAVEAT"
124    };
125
126    private void logConfig(EGL10 egl, EGLDisplay display, EGLConfig config) {
127        int value[] = new int[1];
128        StringBuilder sb = new StringBuilder();
129        for (int j = 0; j < ATTR_ID.length; j++) {
130            egl.eglGetConfigAttrib(display, config, ATTR_ID[j], value);
131            sb.append(ATTR_NAME[j] + value[0] + " ");
132        }
133        Log.i(TAG, "Config chosen: " + sb.toString());
134    }
135}
136