summaryrefslogtreecommitdiff
path: root/firmware/target/hosted
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2011-03-16 14:33:55 +0000
committerThomas Martitz <kugel@rockbox.org>2011-03-16 14:33:55 +0000
commit046cec3aa7fd58a9519cad8d693b47a2400e1742 (patch)
treecb0ba0773a3e8b3cef5d7690e2822f03d1425ddb /firmware/target/hosted
parentefa9f13500d1672d0b631520e92e08f3ff61e45c (diff)
downloadrockbox-046cec3aa7fd58a9519cad8d693b47a2400e1742.tar.gz
rockbox-046cec3aa7fd58a9519cad8d693b47a2400e1742.zip
Android: Partly revert r29569 and only call the new getJavaEnvironment() when needed.
The environment is fine to share in general, just not across OS threads, so it's only needed for functions which are possibly called from multiple OS threads (only 1 currently). git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29601 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/target/hosted')
-rw-r--r--firmware/target/hosted/android/button-android.c1
-rw-r--r--firmware/target/hosted/android/lcd-android.c5
-rw-r--r--firmware/target/hosted/android/pcm-android.c20
-rw-r--r--firmware/target/hosted/android/powermgmt-android.c6
-rw-r--r--firmware/target/hosted/android/system-android.c21
-rw-r--r--firmware/target/hosted/android/system-target.h24
-rw-r--r--firmware/target/hosted/android/telephony-android.c4
7 files changed, 42 insertions, 39 deletions
diff --git a/firmware/target/hosted/android/button-android.c b/firmware/target/hosted/android/button-android.c
index e7a3d00a65..ed1e125223 100644
--- a/firmware/target/hosted/android/button-android.c
+++ b/firmware/target/hosted/android/button-android.c
@@ -29,6 +29,7 @@
29#include "system.h" 29#include "system.h"
30#include "touchscreen.h" 30#include "touchscreen.h"
31 31
32extern JNIEnv *env_ptr;
32static int last_y, last_x; 33static int last_y, last_x;
33static int last_btns; 34static int last_btns;
34 35
diff --git a/firmware/target/hosted/android/lcd-android.c b/firmware/target/hosted/android/lcd-android.c
index 19f077010f..abde72155d 100644
--- a/firmware/target/hosted/android/lcd-android.c
+++ b/firmware/target/hosted/android/lcd-android.c
@@ -28,6 +28,7 @@
28#include "lcd.h" 28#include "lcd.h"
29#include "button.h" 29#include "button.h"
30 30
31extern JNIEnv *env_ptr;
31extern jobject RockboxService_instance; 32extern jobject RockboxService_instance;
32 33
33static jobject RockboxFramebuffer_instance; 34static jobject RockboxFramebuffer_instance;
@@ -90,8 +91,6 @@ void lcd_init_device(void)
90 91
91void lcd_update(void) 92void lcd_update(void)
92{ 93{
93 JNIEnv *env_ptr = getJavaEnvironment();
94
95 if (display_on) 94 if (display_on)
96 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance, 95 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance,
97 java_lcd_update); 96 java_lcd_update);
@@ -99,8 +98,6 @@ void lcd_update(void)
99 98
100void lcd_update_rect(int x, int y, int width, int height) 99void lcd_update_rect(int x, int y, int width, int height)
101{ 100{
102 JNIEnv *env_ptr = getJavaEnvironment();
103
104 if (display_on) 101 if (display_on)
105 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance, 102 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance,
106 java_lcd_update_rect, x, y, width, height); 103 java_lcd_update_rect, x, y, width, height);
diff --git a/firmware/target/hosted/android/pcm-android.c b/firmware/target/hosted/android/pcm-android.c
index 24881bd3df..edb3503262 100644
--- a/firmware/target/hosted/android/pcm-android.c
+++ b/firmware/target/hosted/android/pcm-android.c
@@ -21,10 +21,13 @@
21 21
22#include <jni.h> 22#include <jni.h>
23#include <stdbool.h> 23#include <stdbool.h>
24#define _SYSTEM_WITH_JNI /* for getJavaEnvironment */
24#include <system.h> 25#include <system.h>
25#include "debug.h" 26#include "debug.h"
26#include "pcm.h" 27#include "pcm.h"
27 28
29extern JNIEnv *env_ptr;
30
28/* infos about our pcm chunks */ 31/* infos about our pcm chunks */
29static size_t pcm_data_size; 32static size_t pcm_data_size;
30static char *pcm_data_start; 33static char *pcm_data_start;
@@ -115,17 +118,17 @@ void pcm_play_dma_start(const void *addr, size_t size)
115 118
116void pcm_play_dma_stop(void) 119void pcm_play_dma_stop(void)
117{ 120{
118 JNIEnv *env_ptr = getJavaEnvironment(); 121 /* NOTE: due to how pcm_play_get_more_callback() works, this is
119 122 * possibly called from pcmSamplesToByteArray(), i.e. another thread.
120 (*env_ptr)->CallVoidMethod(env_ptr, 123 * => We need to discover the env_ptr */
121 RockboxPCM_instance, 124 JNIEnv* env = getJavaEnvironment();
122 stop_method); 125 (*env)->CallVoidMethod(env,
126 RockboxPCM_instance,
127 stop_method);
123} 128}
124 129
125void pcm_play_dma_pause(bool pause) 130void pcm_play_dma_pause(bool pause)
126{ 131{
127 JNIEnv *env_ptr = getJavaEnvironment();
128
129 (*env_ptr)->CallVoidMethod(env_ptr, 132 (*env_ptr)->CallVoidMethod(env_ptr,
130 RockboxPCM_instance, 133 RockboxPCM_instance,
131 play_pause_method, 134 play_pause_method,
@@ -154,7 +157,6 @@ void pcm_play_dma_init(void)
154 * Luckily we only reference the PCM object from here, so it's safe (and 157 * Luckily we only reference the PCM object from here, so it's safe (and
155 * clean) to allocate it here 158 * clean) to allocate it here
156 **/ 159 **/
157 JNIEnv *env_ptr = getJavaEnvironment();
158 JNIEnv e = *env_ptr; 160 JNIEnv e = *env_ptr;
159 /* get the class and its constructor */ 161 /* get the class and its constructor */
160 jclass RockboxPCM_class = e->FindClass(env_ptr, "org/rockbox/RockboxPCM"); 162 jclass RockboxPCM_class = e->FindClass(env_ptr, "org/rockbox/RockboxPCM");
@@ -175,8 +177,6 @@ void pcm_postinit(void)
175 177
176void pcm_set_mixer_volume(int volume) 178void pcm_set_mixer_volume(int volume)
177{ 179{
178 JNIEnv *env_ptr = getJavaEnvironment();
179
180 (*env_ptr)->CallVoidMethod(env_ptr, RockboxPCM_instance, set_volume_method, volume); 180 (*env_ptr)->CallVoidMethod(env_ptr, RockboxPCM_instance, set_volume_method, volume);
181} 181}
182 182
diff --git a/firmware/target/hosted/android/powermgmt-android.c b/firmware/target/hosted/android/powermgmt-android.c
index 222212f9c8..d23fece39a 100644
--- a/firmware/target/hosted/android/powermgmt-android.c
+++ b/firmware/target/hosted/android/powermgmt-android.c
@@ -23,8 +23,8 @@
23#include <jni.h> 23#include <jni.h>
24#include <stdbool.h> 24#include <stdbool.h>
25#include "config.h" 25#include "config.h"
26#include "system.h"
27 26
27extern JNIEnv *env_ptr;
28extern jclass RockboxService_class; 28extern jclass RockboxService_class;
29extern jobject RockboxService_instance; 29extern jobject RockboxService_instance;
30 30
@@ -32,8 +32,6 @@ static jfieldID _battery_level;
32 32
33void powermgmt_init_target(void) 33void powermgmt_init_target(void)
34{ 34{
35 JNIEnv *env_ptr = getJavaEnvironment();
36
37 jmethodID initBatteryMonitor = (*env_ptr)->GetMethodID(env_ptr, 35 jmethodID initBatteryMonitor = (*env_ptr)->GetMethodID(env_ptr,
38 RockboxService_class, 36 RockboxService_class,
39 "initBatteryMonitor", 37 "initBatteryMonitor",
@@ -52,8 +50,6 @@ void powermgmt_init_target(void)
52 50
53int battery_level(void) 51int battery_level(void)
54{ 52{
55 JNIEnv *env_ptr = getJavaEnvironment();
56
57 return (*env_ptr)->GetIntField(env_ptr, RockboxService_instance, _battery_level); 53 return (*env_ptr)->GetIntField(env_ptr, RockboxService_instance, _battery_level);
58} 54}
59 55
diff --git a/firmware/target/hosted/android/system-android.c b/firmware/target/hosted/android/system-android.c
index 8d36198c61..686453cfbb 100644
--- a/firmware/target/hosted/android/system-android.c
+++ b/firmware/target/hosted/android/system-android.c
@@ -27,7 +27,8 @@
27 27
28 28
29/* global fields for use with various JNI calls */ 29/* global fields for use with various JNI calls */
30JavaVM *vm_ptr; 30static JavaVM *vm_ptr;
31JNIEnv *env_ptr;
31jobject RockboxService_instance; 32jobject RockboxService_instance;
32jclass RockboxService_class; 33jclass RockboxService_class;
33 34
@@ -47,6 +48,22 @@ void system_init(void)
47 telephony_init_device(); 48 telephony_init_device();
48} 49}
49 50
51JNIEXPORT jint JNICALL
52JNI_OnLoad(JavaVM *vm, void* reserved)
53{
54 (void)reserved;
55 vm_ptr = vm;
56
57 return JNI_VERSION_1_2;
58}
59
60JNIEnv* getJavaEnvironment(void)
61{
62 JNIEnv* env;
63 (*vm_ptr)->GetEnv(vm_ptr, (void**) &env, JNI_VERSION_1_2);
64 return env;
65}
66
50/* this is the entry point of the android app initially called by jni */ 67/* this is the entry point of the android app initially called by jni */
51JNIEXPORT void JNICALL 68JNIEXPORT void JNICALL
52Java_org_rockbox_RockboxService_main(JNIEnv *env, jobject this) 69Java_org_rockbox_RockboxService_main(JNIEnv *env, jobject this)
@@ -58,8 +75,8 @@ Java_org_rockbox_RockboxService_main(JNIEnv *env, jobject this)
58 75
59 volatile uintptr_t stack = 0; 76 volatile uintptr_t stack = 0;
60 stackbegin = stackend = (uintptr_t*) &stack; 77 stackbegin = stackend = (uintptr_t*) &stack;
78 env_ptr = env;
61 79
62 (*env)->GetJavaVM(env, &vm_ptr);
63 RockboxService_instance = this; 80 RockboxService_instance = this;
64 RockboxService_class = (*env)->GetObjectClass(env, this); 81 RockboxService_class = (*env)->GetObjectClass(env, this);
65 82
diff --git a/firmware/target/hosted/android/system-target.h b/firmware/target/hosted/android/system-target.h
index 9145ab1e84..325c1012af 100644
--- a/firmware/target/hosted/android/system-target.h
+++ b/firmware/target/hosted/android/system-target.h
@@ -21,8 +21,6 @@
21#ifndef __SYSTEM_TARGET_H__ 21#ifndef __SYSTEM_TARGET_H__
22#define __SYSTEM_TARGET_H__ 22#define __SYSTEM_TARGET_H__
23 23
24#include <jni.h>
25
26#define disable_irq() 24#define disable_irq()
27#define enable_irq() 25#define enable_irq()
28#define disable_irq_save() 0 26#define disable_irq_save() 0
@@ -32,19 +30,15 @@ void power_off(void);
32void wait_for_interrupt(void); 30void wait_for_interrupt(void);
33void interrupt(void); 31void interrupt(void);
34 32
35/* A JNI environment is specific to its thread, so use the correct way to 33 /* don't pull in jni.h for every user of this file, it should be only needed
36 * obtain it: share a pointer to the JavaVM structure and ask that the JNI 34 * within the target tree (if at all)
37 * environment attached to the current thread. */ 35 * define this before #including system.h or system-target.h */
38static inline JNIEnv* getJavaEnvironment(void) 36#ifdef _SYSTEM_WITH_JNI
39{ 37#include <jni.h>
40 extern JavaVM *vm_ptr; 38/*
41 JNIEnv *env = NULL; 39 * discover the JNIEnv for this the calling thread in case it's not known */
42 40extern JNIEnv* getJavaEnvironment(void);
43 if (vm_ptr) 41#endif /* _SYSTEM_WITH_JNI */
44 (*vm_ptr)->GetEnv(vm_ptr, (void**) &env, JNI_VERSION_1_2);
45
46 return env;
47}
48 42
49#endif /* __SYSTEM_TARGET_H__ */ 43#endif /* __SYSTEM_TARGET_H__ */
50 44
diff --git a/firmware/target/hosted/android/telephony-android.c b/firmware/target/hosted/android/telephony-android.c
index fb2dc37623..64ad436ca7 100644
--- a/firmware/target/hosted/android/telephony-android.c
+++ b/firmware/target/hosted/android/telephony-android.c
@@ -22,16 +22,14 @@
22 22
23#include <jni.h> 23#include <jni.h>
24#include "kernel.h" 24#include "kernel.h"
25#include "system.h"
26 25
26extern JNIEnv *env_ptr;
27extern jobject RockboxService_instance; 27extern jobject RockboxService_instance;
28 28
29 29
30void telephony_init_device(void) 30void telephony_init_device(void)
31{ 31{
32 JNIEnv *env_ptr = getJavaEnvironment();
33 JNIEnv e = *env_ptr; 32 JNIEnv e = *env_ptr;
34
35 jclass class = e->FindClass(env_ptr, "org/rockbox/RockboxTelephony"); 33 jclass class = e->FindClass(env_ptr, "org/rockbox/RockboxTelephony");
36 jmethodID constructor = e->GetMethodID(env_ptr, class, "<init>", "(Landroid/content/Context;)V"); 34 jmethodID constructor = e->GetMethodID(env_ptr, class, "<init>", "(Landroid/content/Context;)V");
37 35