blob: 0d5643ff9cb721fd3534730bc3a5ddee1ea41175 [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
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 javax.microedition.khronos.egl.EGL10;
21import javax.microedition.khronos.egl.EGLConfig;
22import javax.microedition.khronos.egl.EGLDisplay;
23
24/*
25 * The code is copied/adapted from
26 * <code>android.opengl.GLSurfaceView.BaseConfigChooser</code>. Here we try to
27 * choose a configuration that support RGBA_8888 format and if possible,
28 * with stencil buffer, but is not required.
29 */
30class GalleryEGLConfigChooser implements EGLConfigChooser {
31
32 private static final String TAG = "GalleryEGLConfigChooser";
Owen Lina2fba682011-08-17 22:07:43 +080033
34 private final int mConfigSpec[] = new int[] {
35 EGL10.EGL_RED_SIZE, 5,
36 EGL10.EGL_GREEN_SIZE, 6,
37 EGL10.EGL_BLUE_SIZE, 5,
38 EGL10.EGL_ALPHA_SIZE, 0,
39 EGL10.EGL_NONE
40 };
41
Owen Lina2fba682011-08-17 22:07:43 +080042 public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
43 int[] numConfig = new int[1];
44 if (!egl.eglChooseConfig(display, mConfigSpec, null, 0, numConfig)) {
45 throw new RuntimeException("eglChooseConfig failed");
46 }
47
48 if (numConfig[0] <= 0) {
49 throw new RuntimeException("No configs match configSpec");
50 }
51
52 EGLConfig[] configs = new EGLConfig[numConfig[0]];
53 if (!egl.eglChooseConfig(display,
54 mConfigSpec, configs, configs.length, numConfig)) {
55 throw new RuntimeException();
56 }
57
58 return chooseConfig(egl, display, configs);
59 }
60
61 private EGLConfig chooseConfig(
62 EGL10 egl, EGLDisplay display, EGLConfig configs[]) {
63
64 EGLConfig result = null;
65 int minStencil = Integer.MAX_VALUE;
66 int value[] = new int[1];
67
68 // Because we need only one bit of stencil, try to choose a config that
69 // has stencil support but with smallest number of stencil bits. If
70 // none is found, choose any one.
71 for (int i = 0, n = configs.length; i < n; ++i) {
72 if (egl.eglGetConfigAttrib(
73 display, configs[i], EGL10.EGL_RED_SIZE, value)) {
74 // Filter out ARGB 8888 configs.
75 if (value[0] == 8) continue;
76 }
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);
Owen Lina2fba682011-08-17 22:07:43 +080092 logConfig(egl, display, result);
93 return result;
94 }
95
96 private static final int[] ATTR_ID = {
97 EGL10.EGL_RED_SIZE,
98 EGL10.EGL_GREEN_SIZE,
99 EGL10.EGL_BLUE_SIZE,
100 EGL10.EGL_ALPHA_SIZE,
101 EGL10.EGL_DEPTH_SIZE,
102 EGL10.EGL_STENCIL_SIZE,
103 EGL10.EGL_CONFIG_ID,
104 EGL10.EGL_CONFIG_CAVEAT
105 };
106
107 private static final String[] ATTR_NAME = {
108 "R", "G", "B", "A", "D", "S", "ID", "CAVEAT"
109 };
110
111 private void logConfig(EGL10 egl, EGLDisplay display, EGLConfig config) {
112 int value[] = new int[1];
113 StringBuilder sb = new StringBuilder();
114 for (int j = 0; j < ATTR_ID.length; j++) {
115 egl.eglGetConfigAttrib(display, config, ATTR_ID[j], value);
116 sb.append(ATTR_NAME[j] + value[0] + " ");
117 }
118 Log.i(TAG, "Config chosen: " + sb.toString());
119 }
120}