Search
lxdream.org :: lxdream/src/gui_android.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui_android.c
changeset 1282:9f445c5e252b
prev1278:2f0de47738d0
author nkeynes
date Tue Mar 27 08:23:52 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Android: Preserve render buffers when switching away from app.
- fix gl_read_render_buffer + gl_load_frame_buffer to work in GLES
a) glReadPixels only (guaranteed to) work for GL_RGBA,GL_UNSIGNED_BYTE
b) glTexSubImage2D can only load GL_RGBA into a GL_RGBA render buffer.
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Native shims for the Android front-end (implemented in Java).
     5  *
     6  * This is complicated by the fact that all the emulation runs in a
     7  * separate thread.
     8  *
     9  * Copyright (c) 2012 Nathan Keynes.
    10  *
    11  * This program is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU General Public License as published by
    13  * the Free Software Foundation; either version 2 of the License, or
    14  * (at your option) any later version.
    15  *
    16  * This program is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU General Public License for more details.
    20  */
    22 #include <jni.h>
    23 #include <pthread.h>
    24 #include <android/log.h>
    25 #include <android/native_window_jni.h>
    26 #include <libisofs.h>
    27 #include "dream.h"
    28 #include "dreamcast.h"
    29 #include "gui.h"
    30 #include "config.h"
    31 #include "lxpaths.h"
    32 #include "tqueue.h"
    33 #include "display.h"
    34 #include "gdlist.h"
    35 #include "gdrom/gdrom.h"
    36 #include "hotkeys.h"
    37 #include "serial.h"
    38 #include "aica/audio.h"
    39 #include "drivers/video_egl.h"
    40 #include "maple/maple.h"
    41 #include "vmu/vmulist.h"
    42 #include "pvr2/pvr2.h"
    44 struct surface_info {
    45     ANativeWindow *win;
    46     int width, height, format;
    47 };
    49 static struct surface_info current_surface = { NULL, 0, 0, 0 };
    50 static const char *appHome = NULL;
    52 /**
    53  * Count of running nanoseconds - used to cut back on the GUI runtime
    54  */
    55 static uint32_t android_gui_nanos = 0;
    56 static uint32_t android_gui_ticks = 0;
    57 static struct timeval android_gui_lasttv;
    60 void android_gui_start( void )
    61 {
    62     /* Dreamcast starting up hook */
    63 }
    65 void android_gui_stop( void )
    66 {
    67     /* Dreamcast stopping hook */
    68 }
    70 void android_gui_reset( void )
    71 {
    72     /* Dreamcast reset hook */
    73 }
    75 /**
    76  * The main emulation thread. (as opposed to the UI thread).
    77  */
    78 void *android_thread_main(void *data)
    79 {
    80     while(1) {
    81         tqueue_process_wait();
    82     }
    83     return NULL;
    84 }
    86 int android_set_surface(void *data)
    87 {
    88     struct surface_info *surface = (struct surface_info *)data;
    89     video_egl_set_window(surface->win, surface->width, surface->height, surface->format);
    90     pvr2_restore_render_buffers();
    91     pvr2_draw_frame();
    92     INFO( "set_surface" );
    93     return 0;
    94 }
    96 int android_clear_surface(void *data)
    97 {
    98     struct surface_info *surface = (struct surface_info *)data;
   100     if( dreamcast_is_running() ) {
   101         dreamcast_stop(); /* Should already be stopped, but just in case */
   102     }
   103     pvr2_preserve_render_buffers();
   104     video_egl_clear_window();
   105     ANativeWindow_release(surface->win);
   106     surface->win = NULL;
   107     INFO( "clear_surface" );
   108     return 0;
   109 }
   111 static pthread_t dreamcast_thread;
   113 void android_start_thread()
   114 {
   115     pthread_attr_t attr;
   117     pthread_attr_init(&attr);
   118     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
   119     pthread_attr_setstacksize(&attr,2048*1024);
   120     int status = pthread_create(&dreamcast_thread, &attr, android_thread_main, NULL);
   121     if( status != 0 ) {
   122         /* Handle errors */
   123     }
   124 }
   126 /** tqueue callback wrapper to get the right call type for simple events */
   127 int android_callback_wrapper( void *fn )
   128 {
   129     void (*cast_fn)(void) = fn;
   130     cast_fn();
   131 }
   133 int android_mount_disc( void *data )
   134 {
   135     char *s = (char *)data;
   136     ERROR err;
   137     gboolean result = gdrom_mount_image( s, &err ); /* TODO: Report error */
   138     return result;
   139 }
   141 int android_toggle_run( void *data )
   142 {
   143     if( dreamcast_is_running() ) {
   144         dreamcast_stop();
   145     } else {
   146         dreamcast_run();
   147     }
   148 }
   150 uint32_t android_gui_run_slice( uint32_t nanosecs )
   151 {
   152     android_gui_nanos += nanosecs;
   153     if( android_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
   154         android_gui_nanos -= GUI_TICK_PERIOD;
   155         android_gui_ticks ++;
   156         uint32_t current_period = android_gui_ticks * GUI_TICK_PERIOD;
   158         // Run the event loop
   159         tqueue_process_all();
   161         struct timeval tv;
   162         gettimeofday(&tv,NULL);
   163         uint32_t ns = ((tv.tv_sec - android_gui_lasttv.tv_sec) * 1000000000) +
   164         (tv.tv_usec - android_gui_lasttv.tv_usec)*1000;
   165         if( (ns * 1.05) < current_period ) {
   166             // We've gotten ahead - sleep for a little bit
   167             struct timespec tv;
   168             tv.tv_sec = 0;
   169             tv.tv_nsec = current_period - ns;
   170             nanosleep(&tv, &tv);
   171         }
   173 #if 0
   174         /* Update the display every 10 ticks (ie 10 times a second) and
   175          * save the current tv value */
   176         if( android_gui_ticks > 10 ) {
   177             android_gui_ticks -= 10;
   179             double speed = (float)( (double)current_period * 100.0 / ns );
   180             android_gui_lasttv.tv_sec = tv.tv_sec;
   181             android_gui_lasttv.tv_usec = tv.tv_usec;
   182             main_window_set_speed( main_win, speed );
   183         }
   184 #endif
   185     }
   186     return nanosecs;
   189 }
   191 struct dreamcast_module android_gui_module = { "gui", NULL,
   192         android_gui_reset,
   193         android_gui_start,
   194         android_gui_run_slice,
   195         android_gui_stop,
   196         NULL, NULL };
   198 gboolean gui_error_dialog( const char *fmt, ... )
   199 {
   200     return FALSE; /* TODO */
   201 }
   203 void gui_update_state()
   204 {
   205     /* TODO */
   206 }
   208 void gui_set_use_grab( gboolean grab )
   209 {
   210     /* No implementation - mouse grab doesn't exist */
   211 }
   213 void gui_update_io_activity( io_activity_type activity, gboolean active )
   214 {
   215     /* No implementation */
   216 }
   218 void gui_do_later( do_later_callback_t func )
   219 {
   220     func(); /* TODO */
   221 }
   223 static void android_init( const char *appHomeDir )
   224 {
   225     set_global_log_level("info");
   226     appHome = appHomeDir;
   227     const char *confFile = g_strdup_printf("%s/lxdreamrc", appHome);
   228     set_user_data_path(appHome);
   229     lxdream_set_config_filename( confFile );
   230     lxdream_make_config_dir( );
   231     lxdream_load_config( );
   232     iso_init();
   233     gdrom_list_init();
   234     vmulist_init();
   235     dreamcast_init(1);
   237     dreamcast_register_module( &android_gui_module );
   238     audio_init_driver(NULL);
   239     display_driver_t display_driver = get_display_driver_by_name(NULL);
   240     display_set_driver(display_driver);
   242     hotkeys_init();
   243     serial_init();
   244     maple_reattach_all();
   246     ERROR err;
   247     gchar *disc_file = lxdream_get_global_config_path_value( CONFIG_GDROM );
   248     if( disc_file != NULL ) {
   249         gboolean ok = gdrom_mount_image( disc_file, &err );
   250         g_free(disc_file);
   251         if( !ok ) {
   252             WARN( err.msg );
   253         } else {
   254             INFO( "Mounted %s", disc_file );
   255         }
   256     }
   258     INFO( "%s! ready...", APP_NAME );
   259     android_start_thread();
   260 }
   263 /************************* Dreamcast native entry points **************************/
   265 static char *getStringChars( JNIEnv *env, jstring str );
   267 /**
   268  * Main initialization entry point. We need to do all the setup that main()
   269  * would normally do, as well as any UI specific setup.
   270  */
   271 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_init(JNIEnv * env, jclass obj,  jstring homeDir )
   272 {
   273     android_init( getStringChars(env, homeDir) );
   274 }
   276 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_run(JNIEnv * env, jclass obj)
   277 {
   278     tqueue_post_message( android_callback_wrapper, dreamcast_run );
   279 }
   281 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_toggleRun(JNIEnv * env, jclass obj)
   282 {
   283     tqueue_post_message( android_toggle_run, NULL );
   284 }
   286 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_reset(JNIEnv * env, jclass obj)
   287 {
   288     tqueue_post_message( android_callback_wrapper, dreamcast_reset );
   289 }
   291 JNIEXPORT void JNICALL Java_org_lxdream_Dreamcast_stop(JNIEnv * env, jclass obj)
   292 {
   293     /* Need to make sure this completely shuts down before we return */
   294     tqueue_send_message( android_callback_wrapper, dreamcast_stop );
   295 }
   297 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_isRunning(JNIEnv *env, jclass obj)
   298 {
   299     return dreamcast_is_running();
   300 }
   302 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_isRunnable(JNIEnv *env, jclass obj)
   303 {
   304     return dreamcast_can_run();
   305 }
   307 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_mount(JNIEnv *env, jclass obj, jstring str)
   308 {
   309     char *s = getStringChars(env, str);
   310     return tqueue_send_message( android_mount_disc, s );
   311 }
   313 JNIEXPORT jboolean JNICALL Java_org_lxdream_Dreamcast_unmount(JNIEnv *env, jclass obj)
   314 {
   315     tqueue_post_message( android_callback_wrapper, gdrom_unmount_disc );
   316 }
   319 /************************* LxdreamView native entry points **************************/
   321 JNIEXPORT void JNICALL Java_org_lxdream_LxdreamView_setSurface(JNIEnv * env, jobject view, jobject surface, jint width, jint height)
   322 {
   323     current_surface.win = ANativeWindow_fromSurface(env, surface);
   324     current_surface.width = width;
   325     current_surface.height = height;
   326     int fmt = ANativeWindow_getFormat(current_surface.win);
   327     if( fmt == WINDOW_FORMAT_RGB_565 ) {
   328         current_surface.format = COLFMT_RGB565;
   329     } else {
   330         current_surface.format = COLFMT_RGB888;
   331     }
   332     INFO( "Setting surface" );
   333     tqueue_post_message( android_set_surface, &current_surface );
   334 }
   336 JNIEXPORT void JNICALL Java_org_lxdream_LxdreamView_clearSurface(JNIEnv * env, jobject view, jobject surface)
   337 {
   338     /* Need to make sure this completely shuts down before we return */
   339     tqueue_send_message( android_clear_surface, &current_surface );
   340 }
   343 /************************* JNI Support functions **************************/
   345 static char *getStringChars( JNIEnv *env, jstring str )
   346 {
   347     jboolean iscopy;
   348     const char *p = (*env)->GetStringUTFChars(env, str, &iscopy);
   349     char *result = strdup(p);
   350     (*env)->ReleaseStringUTFChars(env,str,p);
   351     return result;
   352 }
.