GL2JNIView.java revision 43123766f5fc3a73df77a03390679ec536ebe6e0
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/**
49 * An implementation of SurfaceView that uses the dedicated surface for
50 * displaying an OpenGL animation.  This allows the animation to run in a
51 * separate thread, without requiring that it be driven by the update mechanism
52 * of the view hierarchy.
53 *
54 * The application-specific rendering code is delegated to a GLView.Renderer
55 * instance.
56 */
57class GL2JNIView extends GLSurfaceView {
58    private static String TAG = "GL2JNIView";
59    GL2JNIView(Context context) {
60        super(context);
61        init();
62    }
63
64    public GL2JNIView(Context context, AttributeSet attrs) {
65        super(context, attrs);
66        init();
67    }
68
69    private void init() {
70        setEGLContextFactory(new ContextFactory());
71        setEGLConfigChooser(new ConfigChooser());
72        setRenderer(new Renderer());
73    }
74
75    private static class ContextFactory implements GLSurfaceView.EGLContextFactory {
76        private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
77        public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
78            Log.w(TAG, "creating OpenGL ES 2.0 context");
79            checkEglError("Before eglCreateContext", egl);
80            int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
81            EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
82            checkEglError("After eglCreateContext", egl);
83            return context;
84        }
85
86        public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
87            egl.eglDestroyContext(display, context);
88        }
89    }
90
91    private static void checkEglError(String prompt, EGL10 egl) {
92        int error;
93        while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) {
94            Log.e(TAG, String.format("%s: EGL error: 0x%x", prompt, error));
95        }
96    }
97
98    private static class ConfigChooser implements GLSurfaceView.EGLConfigChooser {
99        private static int EGL_OPENGL_ES2_BIT = 4;
100        private static int[] s_configAttribs2 =
101        {
102            EGL10.EGL_RED_SIZE, 4,
103            EGL10.EGL_GREEN_SIZE, 4,
104            EGL10.EGL_BLUE_SIZE, 4,
105            EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
106            EGL10.EGL_NONE
107        };
108        public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
109
110            int[] num_config = new int[1];
111            egl.eglChooseConfig(display, s_configAttribs2, null, 0, num_config);
112
113            int numConfigs = num_config[0];
114
115            Log.w(TAG, String.format("Found %d configurations", numConfigs));
116            if (numConfigs <= 0) {
117                throw new IllegalArgumentException("No configs match configSpec");
118            }
119            EGLConfig[] configs = new EGLConfig[numConfigs];
120            egl.eglChooseConfig(display, s_configAttribs2, configs, numConfigs, num_config);
121            return configs[0s];
122        }
123    }
124
125    private static class Renderer implements GLSurfaceView.Renderer {
126        public void onDrawFrame(GL10 gl) {
127            GL2JNILib.step();
128        }
129
130        public void onSurfaceChanged(GL10 gl, int width, int height) {
131            GL2JNILib.init(width, height);
132        }
133
134        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
135            // Do nothing.
136        }
137    }
138}
139
140