aboutsummaryrefslogtreecommitdiff
path: root/engine/src/android/com/jme3/system/android/AndroidConfigChooser.java
blob: e55fa55a04a339b7d182580d19b16c17e36d651c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.jme3.system.android;

import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView.EGLConfigChooser;
import java.util.logging.Logger;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;

/**
 * AndroidConfigChooser is used to determine the best suited EGL Config
 * @author larynx
 *
 */
public class AndroidConfigChooser implements EGLConfigChooser 
{
    private static final Logger logger = Logger.getLogger(AndroidConfigChooser.class.getName());
    
    protected int clientOpenGLESVersion = 0;
    protected EGLConfig bestConfig = null;
    protected EGLConfig fastestConfig = null;
    protected EGLConfig choosenConfig = null;
    protected ConfigType type;
    protected int pixelFormat;
    
    protected boolean verbose = false;
    
    private final static int EGL_OPENGL_ES2_BIT = 4;

    public enum ConfigType 
    {
        /**
         * RGB565, 0 alpha, 16 depth, 0 stencil
         */
        FASTEST,
        /**
         * RGB???, 0 alpha, >=16 depth, 0 stencil
         */
        BEST,
        /**
         * Turn off config chooser and use hardcoded
         * setEGLContextClientVersion(2);
         * setEGLConfigChooser(5, 6, 5, 0, 16, 0);
         */
        LEGACY
    }
    
    public AndroidConfigChooser(ConfigType type, boolean verbose)
    {
        this.type = type;
        this.verbose = verbose;
    }
        
    /**
     * Gets called by the GLSurfaceView class to return the best config
     */    
    @Override
    public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display)
    {
        logger.info("GLSurfaceView asks for egl config, returning: ");
        logEGLConfig(choosenConfig, display, egl);
        return choosenConfig;
    }
    
    /**
     * findConfig is used to locate the best config and init the chooser with
     * @param egl
     * @param display
     * @return true if successfull, false if no config was found
     */
    public boolean findConfig(EGL10 egl, EGLDisplay display)
    {           
        
        if (type == ConfigType.BEST)
        {        	
        	ComponentSizeChooser compChooser = new ComponentSizeChooser(8, 8, 8, 8, 32, 0);
        	choosenConfig = compChooser.chooseConfig(egl, display);

        	if (choosenConfig == null)
        	{
                compChooser = new ComponentSizeChooser(8, 8, 8, 0, 32, 0);
                choosenConfig = compChooser.chooseConfig(egl, display);
                if (choosenConfig == null)
                {
                    compChooser = new ComponentSizeChooser(8, 8, 8, 8, 16, 0);
                    choosenConfig = compChooser.chooseConfig(egl, display);
                    if (choosenConfig == null)
                    {
                        compChooser = new ComponentSizeChooser(8, 8, 8, 0, 16, 0);
                        choosenConfig = compChooser.chooseConfig(egl, display);
                    }
                }
        	}
        	
            logger.info("JME3 using best EGL configuration available here: ");
        }
        else
        {
        	ComponentSizeChooser compChooser = new ComponentSizeChooser(5, 6, 5, 0, 16, 0);
        	choosenConfig = compChooser.chooseConfig(egl, display);
            logger.info("JME3 using fastest EGL configuration available here: ");
        }
        
        if (choosenConfig != null)
        {
            logger.info("JME3 using choosen config: "); 
            logEGLConfig(choosenConfig, display, egl);               
            pixelFormat = getPixelFormat(choosenConfig, display, egl);
            clientOpenGLESVersion = getOpenGLVersion(choosenConfig, display, egl);
            return true;
        }
        else
        {
            logger.severe("###ERROR### Unable to get a valid OpenGL ES 2.0 config, nether Fastest nor Best found! Bug. Please report this.");
            clientOpenGLESVersion = 1;
            pixelFormat = PixelFormat.UNKNOWN;
            return false;
        }        
    }
    
    
    private int getPixelFormat(EGLConfig conf, EGLDisplay display, EGL10 egl)
    {
        int[] value = new int[1];
        int result = PixelFormat.RGB_565;                

        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_RED_SIZE, value);
        if (value[0] == 8)
        {
            result = PixelFormat.RGBA_8888;
            /*
            egl.eglGetConfigAttrib(display, conf, EGL10.EGL_ALPHA_SIZE, value);
            if (value[0] == 8)
            {
                result = PixelFormat.RGBA_8888;
            }
            else
            {
                result = PixelFormat.RGB_888;
            }*/
        }
        
        if (verbose)
        {
            logger.info("Using PixelFormat " + result);                            
        }
    
        //return result; TODO Test pixelformat
        return PixelFormat.TRANSPARENT;
    }
    
    private int getOpenGLVersion(EGLConfig conf, EGLDisplay display, EGL10 egl)
    {
        int[] value = new int[1];
        int result = 1;
                        
        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_RENDERABLE_TYPE, value);
        // Check if conf is OpenGL ES 2.0
        if ((value[0] & EGL_OPENGL_ES2_BIT) != 0)
        {
            result = 2;
        }

        return result;                    
    }
    
    /**
     * log output with egl config details
     * @param conf
     * @param display
     * @param egl
     */
    public void logEGLConfig(EGLConfig conf, EGLDisplay display, EGL10 egl)
    {
        int[] value = new int[1];

        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_RED_SIZE, value);
        logger.info(String.format("EGL_RED_SIZE  = %d", value[0] ) );
        
        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_GREEN_SIZE, value);
        logger.info(String.format("EGL_GREEN_SIZE  = %d", value[0] ) );
        
        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_BLUE_SIZE, value);
        logger.info(String.format("EGL_BLUE_SIZE  = %d", value[0] ) );
        
        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_ALPHA_SIZE, value);
        logger.info(String.format("EGL_ALPHA_SIZE  = %d", value[0] ) );
        
        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_DEPTH_SIZE, value);
        logger.info(String.format("EGL_DEPTH_SIZE  = %d", value[0] ) );
                
        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_STENCIL_SIZE, value);
        logger.info(String.format("EGL_STENCIL_SIZE  = %d", value[0] ) );

        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_RENDERABLE_TYPE, value);
        logger.info(String.format("EGL_RENDERABLE_TYPE  = %d", value[0] ) );
        
        egl.eglGetConfigAttrib(display, conf, EGL10.EGL_SURFACE_TYPE, value);
        logger.info(String.format("EGL_SURFACE_TYPE  = %d", value[0] ) );               
    }
        
    public int getClientOpenGLESVersion() 
    {
        return clientOpenGLESVersion;
    }

    public void setClientOpenGLESVersion(int clientOpenGLESVersion) 
    {
        this.clientOpenGLESVersion = clientOpenGLESVersion;
    }
    
    public int getPixelFormat() 
    {
        return pixelFormat;
    }
    
    
    
    private abstract class BaseConfigChooser implements EGLConfigChooser 
    {
        private boolean bClientOpenGLESVersionSet;
        
		public BaseConfigChooser(int[] configSpec) 
		{
		    bClientOpenGLESVersionSet = false;
		    mConfigSpec = filterConfigSpec(configSpec);
		}
		
		public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) 
		{
		    int[] num_config = new int[1];
		    if (!egl.eglChooseConfig(display, mConfigSpec, null, 0,
		            num_config)) {
		        throw new IllegalArgumentException("eglChooseConfig failed");
		    }
		
		    int numConfigs = num_config[0];
		
		    if (numConfigs <= 0) 
		    {
		        //throw new IllegalArgumentException("No configs match configSpec");
		        
		        return null;
		    }
		
		    EGLConfig[] configs = new EGLConfig[numConfigs];
		    if (!egl.eglChooseConfig(display, mConfigSpec, configs, numConfigs,
		            num_config)) {
		        throw new IllegalArgumentException("eglChooseConfig#2 failed");
		    }
		    EGLConfig config = chooseConfig(egl, display, configs);
		    //if (config == null) {
		    //    throw new IllegalArgumentException("No config chosen");
		    //}
		    return config;
		}
		
		abstract EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
		        EGLConfig[] configs);
		
		protected int[] mConfigSpec;
		
		private int[] filterConfigSpec(int[] configSpec) 
		{
		    if (bClientOpenGLESVersionSet == true) {
		        return configSpec;
		    }
		    /* We know none of the subclasses define EGL_RENDERABLE_TYPE.
		     * And we know the configSpec is well formed.
		     */
		    int len = configSpec.length;
		    int[] newConfigSpec = new int[len + 2];
		    System.arraycopy(configSpec, 0, newConfigSpec, 0, len-1);
		    newConfigSpec[len-1] = EGL10.EGL_RENDERABLE_TYPE;
		    newConfigSpec[len] = 4; /* EGL_OPENGL_ES2_BIT */
		    newConfigSpec[len+1] = EGL10.EGL_NONE;
		    
		    bClientOpenGLESVersionSet = true;
		    
		    return newConfigSpec;
		}
    }
    
    /**
     * Choose a configuration with exactly the specified r,g,b,a sizes,
     * and at least the specified depth and stencil sizes.
     */
    private class ComponentSizeChooser extends BaseConfigChooser 
    {
        public ComponentSizeChooser(int redSize, int greenSize, int blueSize,
                int alphaSize, int depthSize, int stencilSize) 
        {
            super(new int[] {
                    EGL10.EGL_RED_SIZE, redSize,
                    EGL10.EGL_GREEN_SIZE, greenSize,
                    EGL10.EGL_BLUE_SIZE, blueSize,
                    EGL10.EGL_ALPHA_SIZE, alphaSize,
                    EGL10.EGL_DEPTH_SIZE, depthSize,
                    EGL10.EGL_STENCIL_SIZE, stencilSize,
                    EGL10.EGL_NONE});
            mValue = new int[1];
            mRedSize = redSize;
            mGreenSize = greenSize;
            mBlueSize = blueSize;
            mAlphaSize = alphaSize;
            mDepthSize = depthSize;
            mStencilSize = stencilSize;
       }

        @Override
        public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs) 
        {
            for (EGLConfig config : configs) 
            {
                int d = findConfigAttrib(egl, display, config,
                        EGL10.EGL_DEPTH_SIZE, 0);
                int s = findConfigAttrib(egl, display, config,
                        EGL10.EGL_STENCIL_SIZE, 0);
                if ((d >= mDepthSize) && (s >= mStencilSize)) 
                {
                    int r = findConfigAttrib(egl, display, config,
                            EGL10.EGL_RED_SIZE, 0);
                    int g = findConfigAttrib(egl, display, config,
                             EGL10.EGL_GREEN_SIZE, 0);
                    int b = findConfigAttrib(egl, display, config,
                              EGL10.EGL_BLUE_SIZE, 0);
                    int a = findConfigAttrib(egl, display, config,
                            EGL10.EGL_ALPHA_SIZE, 0);
                    if ((r == mRedSize) && (g == mGreenSize)
                            && (b == mBlueSize) && (a == mAlphaSize)) {
                        return config;
                    }
                }
            }
            return null;
        }

        private int findConfigAttrib(EGL10 egl, EGLDisplay display,
                EGLConfig config, int attribute, int defaultValue) 
        {

            if (egl.eglGetConfigAttrib(display, config, attribute, mValue)) 
            {
                return mValue[0];
            }
            return defaultValue;
        }

        private int[] mValue;
        // Subclasses can adjust these values:
        protected int mRedSize;
        protected int mGreenSize;
        protected int mBlueSize;
        protected int mAlphaSize;
        protected int mDepthSize;
        protected int mStencilSize;
    }
    
    
    
    
}