GL2JNIView.java revision 919583553781f1e1885fa17f76d54008ebeca3c1
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
17package com.android.gl2jni;
18/*
19 * Copyright (C) 2008 The Android Open Source Project
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License");
22 * you may not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 *      http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
32 */
33
34
35import android.content.Context;
36import android.opengl.GLSurfaceView;
37import android.util.AttributeSet;
38import android.util.Log;
39import android.view.KeyEvent;
40import android.view.MotionEvent;
41
42import javax.microedition.khronos.egl.EGL10;
43import javax.microedition.khronos.egl.EGLConfig;
44import javax.microedition.khronos.egl.EGLContext;
45import javax.microedition.khronos.egl.EGLDisplay;
46import javax.microedition.khronos.opengles.GL10;
47/**
48 * An implementation of SurfaceView that uses the dedicated surface for
49 * displaying an OpenGL animation.  This allows the animation to run in a
50 * separate thread, without requiring that it be driven by the update mechanism
51 * of the view hierarchy.
52 *
53 * The application-specific rendering code is delegated to a GLView.Renderer
54 * instance.
55 */
56class GL2JNIView extends GLSurfaceView {
57    private static String TAG = "GL2JNIView";
58    GL2JNIView(Context context) {
59        super(context);
60        init();
61    }
62
63    public GL2JNIView(Context context, AttributeSet attrs) {
64        super(context, attrs);
65        init();
66    }
67
68    private void init() {
69        setEGLContextFactory(new ContextFactory());
70        // setEGLConfigChooser(new ConfigChooser());
71        setRenderer(new Renderer());
72    }
73
74    private static class ContextFactory implements GLSurfaceView.EGLContextFactory {
75        private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
76        public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
77            checkEglError("Before eglCreateContext", egl);
78            int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
79            EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
80            checkEglError("After eglCreateContext", egl);
81            return context;
82        }
83
84        public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
85            egl.eglDestroyContext(display, context);
86        }
87    }
88
89    private static void checkEglError(String prompt, EGL10 egl) {
90        int error;
91        while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) {
92            Log.e(TAG, String.format("%s: EGL error: 0x%x", prompt, error));
93        }
94    }
95
96    private static class ConfigChooser implements GLSurfaceView.EGLConfigChooser {
97        private static int EGL_OPENGL_ES2_BIT = 4;
98        private static int[]  s_configAttribs2 =
99        {
100                EGL10.EGL_DEPTH_SIZE,     16,
101                // EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
102                EGL10.EGL_NONE
103        };
104        public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
105
106            int[] num_config = new int[1];
107            egl.eglChooseConfig(display, s_configAttribs2, null, 0, num_config);
108
109            int numConfigs = num_config[0];
110
111            if (numConfigs <= 0) {
112                throw new IllegalArgumentException("No configs match configSpec");
113            }
114            EGLConfig[] configs = new EGLConfig[numConfigs];
115            egl.eglChooseConfig(display, s_configAttribs2, configs, numConfigs, num_config);
116            return configs[0];
117        }
118    }
119
120    private static class Renderer implements GLSurfaceView.Renderer {
121        public void onDrawFrame(GL10 gl) {
122            GL2JNILib.step();
123        }
124
125        public void onSurfaceChanged(GL10 gl, int width, int height) {
126            GL2JNILib.init(width, height);
127        }
128
129        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
130            // Do nothing.
131        }
132    }
133}
134
135