3dphotoshoot

changeset 6:e31e23ead56f

GAMO TO XRISTO KAI TIN PANAGIA TOU ANDROID
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 21 May 2015 19:03:00 +0300
parents 31cc6615428d
children 7f6e6eb3d20e
files .clang_complete src/android/MainActivity.java src/android/amain.c src/android/camera.c src/camera.h src/opengl.h
diffstat 6 files changed, 161 insertions(+), 23 deletions(-) [+]
line diff
     1.1 --- a/.clang_complete	Thu May 21 13:47:04 2015 +0300
     1.2 +++ b/.clang_complete	Thu May 21 19:03:00 2015 +0300
     1.3 @@ -1,3 +1,6 @@
     1.4 +-std=c99
     1.5  -Isrc
     1.6  -Isrc/android
     1.7  -I$NDK/platforms/android-21/arch-arm/usr/include
     1.8 +-DAPP_NAME=\"photoshoot3d\"
     1.9 +-D__ANDROID__=1
     2.1 --- a/src/android/MainActivity.java	Thu May 21 13:47:04 2015 +0300
     2.2 +++ b/src/android/MainActivity.java	Thu May 21 19:03:00 2015 +0300
     2.3 @@ -13,7 +13,7 @@
     2.4  */
     2.5  
     2.6  public class MainActivity extends NativeActivity {
     2.7 -	private static String tag = "photoshoot3d";
     2.8 +	public static String tag = "photoshoot3d";
     2.9  
    2.10  	public static void foo()
    2.11  	{
    2.12 @@ -62,8 +62,10 @@
    2.13  }
    2.14  
    2.15  class FrameHandler implements SurfaceTexture.OnFrameAvailableListener {
    2.16 -	public void onFrameAvailable(SurfaceTexture stex)
    2.17 +	@Override
    2.18 +	public synchronized void onFrameAvailable(SurfaceTexture stex)
    2.19  	{
    2.20 +		Log.i(MainActivity.tag, "new video frame");
    2.21  		stex.updateTexImage();
    2.22  	}
    2.23  }
     3.1 --- a/src/android/amain.c	Thu May 21 13:47:04 2015 +0300
     3.2 +++ b/src/android/amain.c	Thu May 21 19:03:00 2015 +0300
     3.3 @@ -6,6 +6,7 @@
     3.4  #include "native_glue.h"
     3.5  #include "logger.h"
     3.6  #include "game.h"
     3.7 +#include "camera.h"
     3.8  #include "timer.h"
     3.9  
    3.10  
    3.11 @@ -29,21 +30,7 @@
    3.12  
    3.13  static unsigned int camtex;
    3.14  
    3.15 -static int call_foo()
    3.16 -{
    3.17 -	jmethodID foo_id;
    3.18  
    3.19 -	printf("call_foo() [activity_class=%p]\n", (void*)activity_class);
    3.20 -
    3.21 -	if(!(foo_id = (*jni)->GetStaticMethodID(jni, activity_class, "foo", "()V"))) {
    3.22 -		fprintf(stderr, "static method foo not found\n");
    3.23 -		return -1;
    3.24 -	}
    3.25 -
    3.26 -	(*jni)->CallStaticIntMethod(jni, activity_class, foo_id);
    3.27 -
    3.28 -	return 0;
    3.29 -}
    3.30  
    3.31  void android_main(struct android_app *app_ptr)
    3.32  {
    3.33 @@ -78,7 +65,6 @@
    3.34  		}
    3.35  
    3.36  		if(init_done) {
    3.37 -			call_foo();
    3.38  			game_display(get_time_msec());
    3.39  			eglSwapBuffers(dpy, surf);
    3.40  		}
    3.41 @@ -95,6 +81,8 @@
    3.42  
    3.43  static void handle_command(struct android_app *app, int32_t cmd)
    3.44  {
    3.45 +	struct cam_android_platform_data cam_data;
    3.46 +
    3.47  	switch(cmd) {
    3.48  	case APP_CMD_SAVE_STATE:
    3.49  		/* save the application state to be reloaded on restart if needed */
    3.50 @@ -105,12 +93,14 @@
    3.51  			exit(1);
    3.52  		}
    3.53  
    3.54 -		// create the camera texture
    3.55 -		glGenTextures(1, &camtex);
    3.56 -		glBindTexture(GL_TEXTURE_EXTERNAL_OES, camtex);
    3.57 -		glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    3.58 -		glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    3.59 -		assert(glGetError() == GL_NO_ERROR);
    3.60 +		cam_data.vm = jvm;
    3.61 +		cam_data.jni = jni;
    3.62 +		cam_data.activity_class = activity_class;
    3.63 +
    3.64 +		if(cam_init(&cam_data) == -1) {
    3.65 +			exit(1);
    3.66 +		}
    3.67 +		cam_start_video();
    3.68  
    3.69  		/* initialize the application */
    3.70  		if(game_init() == -1) {
    3.71 @@ -123,15 +113,18 @@
    3.72  		/* cleanup */
    3.73  		init_done = 0;
    3.74  		game_shutdown();
    3.75 +		cam_shutdown();
    3.76  		destroy_gl();
    3.77  		break;
    3.78  
    3.79  	case APP_CMD_GAINED_FOCUS:
    3.80  		/* app focused */
    3.81 +		cam_start_video();
    3.82  		break;
    3.83  
    3.84  	case APP_CMD_LOST_FOCUS:
    3.85  		/* app lost focus */
    3.86 +		cam_stop_video();
    3.87  		break;
    3.88  
    3.89  	case APP_CMD_WINDOW_RESIZED:
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/android/camera.c	Thu May 21 19:03:00 2015 +0300
     4.3 @@ -0,0 +1,113 @@
     4.4 +#include <stdio.h>
     4.5 +#include <assert.h>
     4.6 +#include <jni.h>
     4.7 +#include "opengl.h"
     4.8 +#include "camera.h"
     4.9 +
    4.10 +
    4.11 +static JavaVM *jvm;
    4.12 +static JNIEnv *jni;
    4.13 +static jclass activity_class;
    4.14 +static unsigned int tex;
    4.15 +static int capturing;
    4.16 +
    4.17 +int cam_init(void *platform_data)
    4.18 +{
    4.19 +	int glerr;
    4.20 +	struct cam_android_platform_data *pdata = platform_data;
    4.21 +
    4.22 +	jvm = pdata->vm;
    4.23 +	jni = pdata->jni;
    4.24 +	activity_class = pdata->activity_class;
    4.25 +
    4.26 +	// create the camera texture
    4.27 +	assert(glGetError() == GL_NO_ERROR);
    4.28 +	glGenTextures(1, &tex);
    4.29 +	glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
    4.30 +	glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    4.31 +	glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    4.32 +
    4.33 +	glerr = glGetError();
    4.34 +	assert(glerr == GL_NO_ERROR);
    4.35 +	return glerr == GL_NO_ERROR ? 0 : -1;
    4.36 +}
    4.37 +
    4.38 +void cam_shutdown(void)
    4.39 +{
    4.40 +	cam_stop_video();
    4.41 +	glDeleteTextures(1, &tex);
    4.42 +	tex = 0;
    4.43 +}
    4.44 +
    4.45 +unsigned int cam_texture(void)
    4.46 +{
    4.47 +	return tex;
    4.48 +}
    4.49 +
    4.50 +void cam_texture_size(int *w, int *h)
    4.51 +{
    4.52 +	/* TODO */
    4.53 +}
    4.54 +
    4.55 +int cam_start_video(void)
    4.56 +{
    4.57 +	jmethodID method;
    4.58 +
    4.59 +	if(!jvm) {
    4.60 +		fprintf(stderr, "failed to start video, camera not initialized\n");
    4.61 +		return -1;
    4.62 +	}
    4.63 +	if(cam_is_capturing()) {
    4.64 +		return 0;
    4.65 +	}
    4.66 +
    4.67 +	if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "start_video", "(I)I"))) {
    4.68 +		fprintf(stderr, "failed to find static method: start_video\n");
    4.69 +		return -1;
    4.70 +	}
    4.71 +	if((*jni)->CallStaticIntMethod(jni, activity_class, method, tex) == -1) {
    4.72 +		fprintf(stderr, "failed to start video capture\n");
    4.73 +		capturing = 0;
    4.74 +		return -1;
    4.75 +	}
    4.76 +	capturing = 1;
    4.77 +
    4.78 +	printf("video started\n");
    4.79 +
    4.80 +	return 1;
    4.81 +}
    4.82 +
    4.83 +int cam_stop_video(void)
    4.84 +{
    4.85 +	jmethodID method;
    4.86 +
    4.87 +	if(!jvm) {
    4.88 +		fprintf(stderr, "failed to stop video, camera not initialized\n");
    4.89 +		return -1;
    4.90 +	}
    4.91 +
    4.92 +	if(!cam_is_capturing()) {
    4.93 +		return 0;
    4.94 +	}
    4.95 +
    4.96 +	if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "stop_video", "()V"))) {
    4.97 +		fprintf(stderr, "failed to find static method: stop_video\n");
    4.98 +		return -1;
    4.99 +	}
   4.100 +	(*jni)->CallStaticVoidMethod(jni, activity_class, method);
   4.101 +	capturing = 0;
   4.102 +
   4.103 +	printf("video stopped\n");
   4.104 +
   4.105 +	return 0;
   4.106 +}
   4.107 +
   4.108 +int cam_is_capturing(void)
   4.109 +{
   4.110 +	return capturing;	// XXX is it better to do this properly through JNI?
   4.111 +}
   4.112 +
   4.113 +int cam_take_picture(void)
   4.114 +{
   4.115 +	return -1;	// TODO
   4.116 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/camera.h	Thu May 21 19:03:00 2015 +0300
     5.3 @@ -0,0 +1,26 @@
     5.4 +#ifndef CAMERA_H_
     5.5 +#define CAMERA_H_
     5.6 +
     5.7 +#ifdef __ANDROID__
     5.8 +#include <jni.h>
     5.9 +
    5.10 +struct cam_android_platform_data {
    5.11 +	JavaVM *vm;
    5.12 +	JNIEnv *jni;
    5.13 +	jclass activity_class;
    5.14 +};
    5.15 +#endif
    5.16 +
    5.17 +int cam_init(void *platform_data);
    5.18 +void cam_shutdown(void);
    5.19 +
    5.20 +unsigned int cam_texture(void);
    5.21 +void cam_texture_size(int *w, int *h);
    5.22 +
    5.23 +int cam_start_video(void);
    5.24 +int cam_stop_video(void);
    5.25 +int cam_is_capturing(void);
    5.26 +
    5.27 +int cam_take_picture(void);
    5.28 +
    5.29 +#endif	/* CAMERA_H_ */
     6.1 --- a/src/opengl.h	Thu May 21 13:47:04 2015 +0300
     6.2 +++ b/src/opengl.h	Thu May 21 19:03:00 2015 +0300
     6.3 @@ -2,5 +2,6 @@
     6.4  #define OPENGL_H_
     6.5  
     6.6  #include <GLES/gl.h>
     6.7 +#include <GLES/glext.h>
     6.8  
     6.9  #endif	/* OPENGL_H_ */