00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 package com.ros.pcl.hellopcl;
00017
00018 import java.util.Date;
00019
00020 import javax.microedition.khronos.egl.EGL10;
00021 import javax.microedition.khronos.egl.EGLConfig;
00022 import javax.microedition.khronos.egl.EGLContext;
00023 import javax.microedition.khronos.egl.EGLDisplay;
00024 import javax.microedition.khronos.opengles.GL10;
00025
00026 import android.content.Context;
00027 import android.graphics.PixelFormat;
00028 import android.opengl.GLSurfaceView;
00029 import android.util.Log;
00030
00049 class GL2JNIView extends GLSurfaceView {
00050 private static String TAG = "GL2JNIView";
00051 private static final boolean DEBUG = false;
00052
00053 public GL2JNIView(Context context) {
00054 super(context);
00055 init(false, 0, 0);
00056 }
00057
00058 public GL2JNIView(Context context, boolean translucent, int depth, int stencil) {
00059 super(context);
00060 init(translucent, depth, stencil);
00061 }
00062
00063 private void init(boolean translucent, int depth, int stencil) {
00064
00065
00066
00067
00068
00069
00070 if (translucent) {
00071 this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
00072 }
00073
00074
00075
00076
00077 setEGLContextFactory(new ContextFactory());
00078
00079
00080
00081
00082
00083
00084 setEGLConfigChooser( translucent ?
00085 new ConfigChooser(8, 8, 8, 8, depth, stencil) :
00086 new ConfigChooser(5, 6, 5, 0, depth, stencil) );
00087
00088
00089 setRenderer(new Renderer());
00090 }
00091
00092 private static class ContextFactory implements GLSurfaceView.EGLContextFactory {
00093 private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
00094 public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
00095 Log.w(TAG, "creating OpenGL ES 2.0 context");
00096 checkEglError("Before eglCreateContext", egl);
00097 int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
00098 EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
00099 checkEglError("After eglCreateContext", egl);
00100 return context;
00101 }
00102
00103 public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
00104 egl.eglDestroyContext(display, context);
00105 }
00106 }
00107
00108 private static void checkEglError(String prompt, EGL10 egl) {
00109 int error;
00110 while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) {
00111 Log.e(TAG, String.format("%s: EGL error: 0x%x", prompt, error));
00112 }
00113 }
00114
00115 private static class ConfigChooser implements GLSurfaceView.EGLConfigChooser {
00116
00117 public ConfigChooser(int r, int g, int b, int a, int depth, int stencil) {
00118 mRedSize = r;
00119 mGreenSize = g;
00120 mBlueSize = b;
00121 mAlphaSize = a;
00122 mDepthSize = depth;
00123 mStencilSize = stencil;
00124 }
00125
00126
00127
00128
00129
00130 private static int EGL_OPENGL_ES2_BIT = 4;
00131 private static int[] s_configAttribs2 =
00132 {
00133 EGL10.EGL_RED_SIZE, 4,
00134 EGL10.EGL_GREEN_SIZE, 4,
00135 EGL10.EGL_BLUE_SIZE, 4,
00136 EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
00137 EGL10.EGL_NONE
00138 };
00139
00140 public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
00141
00142
00143
00144 int[] num_config = new int[1];
00145 egl.eglChooseConfig(display, s_configAttribs2, null, 0, num_config);
00146
00147 int numConfigs = num_config[0];
00148
00149 if (numConfigs <= 0) {
00150 throw new IllegalArgumentException("No configs match configSpec");
00151 }
00152
00153
00154
00155 EGLConfig[] configs = new EGLConfig[numConfigs];
00156 egl.eglChooseConfig(display, s_configAttribs2, configs, numConfigs, num_config);
00157
00158 if (DEBUG) {
00159 printConfigs(egl, display, configs);
00160 }
00161
00162
00163 return chooseConfig(egl, display, configs);
00164 }
00165
00166 public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
00167 EGLConfig[] configs) {
00168 for(EGLConfig config : configs) {
00169 int d = findConfigAttrib(egl, display, config,
00170 EGL10.EGL_DEPTH_SIZE, 0);
00171 int s = findConfigAttrib(egl, display, config,
00172 EGL10.EGL_STENCIL_SIZE, 0);
00173
00174
00175 if (d < mDepthSize || s < mStencilSize)
00176 continue;
00177
00178
00179 int r = findConfigAttrib(egl, display, config,
00180 EGL10.EGL_RED_SIZE, 0);
00181 int g = findConfigAttrib(egl, display, config,
00182 EGL10.EGL_GREEN_SIZE, 0);
00183 int b = findConfigAttrib(egl, display, config,
00184 EGL10.EGL_BLUE_SIZE, 0);
00185 int a = findConfigAttrib(egl, display, config,
00186 EGL10.EGL_ALPHA_SIZE, 0);
00187
00188 if (r == mRedSize && g == mGreenSize && b == mBlueSize && a == mAlphaSize)
00189 return config;
00190 }
00191 return null;
00192 }
00193
00194 private int findConfigAttrib(EGL10 egl, EGLDisplay display,
00195 EGLConfig config, int attribute, int defaultValue) {
00196
00197 if (egl.eglGetConfigAttrib(display, config, attribute, mValue)) {
00198 return mValue[0];
00199 }
00200 return defaultValue;
00201 }
00202
00203 private void printConfigs(EGL10 egl, EGLDisplay display,
00204 EGLConfig[] configs) {
00205 int numConfigs = configs.length;
00206 Log.w(TAG, String.format("%d configurations", numConfigs));
00207 for (int i = 0; i < numConfigs; i++) {
00208 Log.w(TAG, String.format("Configuration %d:\n", i));
00209 printConfig(egl, display, configs[i]);
00210 }
00211 }
00212
00213 private void printConfig(EGL10 egl, EGLDisplay display,
00214 EGLConfig config) {
00215 int[] attributes = {
00216 EGL10.EGL_BUFFER_SIZE,
00217 EGL10.EGL_ALPHA_SIZE,
00218 EGL10.EGL_BLUE_SIZE,
00219 EGL10.EGL_GREEN_SIZE,
00220 EGL10.EGL_RED_SIZE,
00221 EGL10.EGL_DEPTH_SIZE,
00222 EGL10.EGL_STENCIL_SIZE,
00223 EGL10.EGL_CONFIG_CAVEAT,
00224 EGL10.EGL_CONFIG_ID,
00225 EGL10.EGL_LEVEL,
00226 EGL10.EGL_MAX_PBUFFER_HEIGHT,
00227 EGL10.EGL_MAX_PBUFFER_PIXELS,
00228 EGL10.EGL_MAX_PBUFFER_WIDTH,
00229 EGL10.EGL_NATIVE_RENDERABLE,
00230 EGL10.EGL_NATIVE_VISUAL_ID,
00231 EGL10.EGL_NATIVE_VISUAL_TYPE,
00232 0x3030,
00233 EGL10.EGL_SAMPLES,
00234 EGL10.EGL_SAMPLE_BUFFERS,
00235 EGL10.EGL_SURFACE_TYPE,
00236 EGL10.EGL_TRANSPARENT_TYPE,
00237 EGL10.EGL_TRANSPARENT_RED_VALUE,
00238 EGL10.EGL_TRANSPARENT_GREEN_VALUE,
00239 EGL10.EGL_TRANSPARENT_BLUE_VALUE,
00240 0x3039,
00241 0x303A,
00242 0x303B,
00243 0x303C,
00244 EGL10.EGL_LUMINANCE_SIZE,
00245 EGL10.EGL_ALPHA_MASK_SIZE,
00246 EGL10.EGL_COLOR_BUFFER_TYPE,
00247 EGL10.EGL_RENDERABLE_TYPE,
00248 0x3042
00249 };
00250 String[] names = {
00251 "EGL_BUFFER_SIZE",
00252 "EGL_ALPHA_SIZE",
00253 "EGL_BLUE_SIZE",
00254 "EGL_GREEN_SIZE",
00255 "EGL_RED_SIZE",
00256 "EGL_DEPTH_SIZE",
00257 "EGL_STENCIL_SIZE",
00258 "EGL_CONFIG_CAVEAT",
00259 "EGL_CONFIG_ID",
00260 "EGL_LEVEL",
00261 "EGL_MAX_PBUFFER_HEIGHT",
00262 "EGL_MAX_PBUFFER_PIXELS",
00263 "EGL_MAX_PBUFFER_WIDTH",
00264 "EGL_NATIVE_RENDERABLE",
00265 "EGL_NATIVE_VISUAL_ID",
00266 "EGL_NATIVE_VISUAL_TYPE",
00267 "EGL_PRESERVED_RESOURCES",
00268 "EGL_SAMPLES",
00269 "EGL_SAMPLE_BUFFERS",
00270 "EGL_SURFACE_TYPE",
00271 "EGL_TRANSPARENT_TYPE",
00272 "EGL_TRANSPARENT_RED_VALUE",
00273 "EGL_TRANSPARENT_GREEN_VALUE",
00274 "EGL_TRANSPARENT_BLUE_VALUE",
00275 "EGL_BIND_TO_TEXTURE_RGB",
00276 "EGL_BIND_TO_TEXTURE_RGBA",
00277 "EGL_MIN_SWAP_INTERVAL",
00278 "EGL_MAX_SWAP_INTERVAL",
00279 "EGL_LUMINANCE_SIZE",
00280 "EGL_ALPHA_MASK_SIZE",
00281 "EGL_COLOR_BUFFER_TYPE",
00282 "EGL_RENDERABLE_TYPE",
00283 "EGL_CONFORMANT"
00284 };
00285 int[] value = new int[1];
00286 for (int i = 0; i < attributes.length; i++) {
00287 int attribute = attributes[i];
00288 String name = names[i];
00289 if ( egl.eglGetConfigAttrib(display, config, attribute, value)) {
00290 Log.w(TAG, String.format(" %s: %d\n", name, value[0]));
00291 } else {
00292
00293 while (egl.eglGetError() != EGL10.EGL_SUCCESS);
00294 }
00295 }
00296 }
00297
00298
00299 protected int mRedSize;
00300 protected int mGreenSize;
00301 protected int mBlueSize;
00302 protected int mAlphaSize;
00303 protected int mDepthSize;
00304 protected int mStencilSize;
00305 private int[] mValue = new int[1];
00306 }
00307
00308 private static class Renderer implements GLSurfaceView.Renderer {
00309 private Date start = new Date();
00310 private int fcount = 0;
00311 public void onDrawFrame(GL10 gl) {
00312
00313 GL2JNILib.step();
00314
00315 fcount++;
00316 if (fcount % 100 == 0) {
00317 double ms = (new Date()).getTime() - start.getTime();
00318 Log.i("GL2JNIVIEW", "fps:" + fcount / (ms / 1000.0));
00319 start = new Date();
00320 fcount = 0;
00321 }
00322
00323 }
00324
00325 public void onSurfaceChanged(GL10 gl, int width, int height) {
00326 GL2JNILib.init(width, height);
00327 }
00328
00329 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
00330
00331 }
00332 }
00333 }