forked from Pretz/Emudroid-Common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemumedia.cpp
189 lines (156 loc) · 4.77 KB
/
emumedia.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdlib.h>
#include "emumedia.h"
class EmuMediaImpl : public EmuMedia {
public:
virtual bool init(JNIEnv *env);
virtual void destroy(JNIEnv *env);
virtual void setSurface(JNIEnv *env, jobject holder);
virtual void setSurfaceRegion(JNIEnv *env, int x, int y, int w, int h);
virtual bool lockSurface(JNIEnv *env,
EmuEngine::Surface *info, bool flip);
virtual void unlockSurface(JNIEnv *env);
virtual bool audioCreate(JNIEnv *env,
unsigned int rate, int bits, int channels);
virtual void audioSetVolume(JNIEnv *env, int volume);
virtual void audioStart(JNIEnv *env);
virtual void audioStop(JNIEnv *env);
virtual void audioPause(JNIEnv *env);
virtual void audioPlay(JNIEnv *env, void *data, int size);
private:
void releaseVideoBuffer(JNIEnv *env);
jclass jPeerClass;
jmethodID midDestroy;
jmethodID midSetSurface;
jmethodID midSetSurfaceRegion;
jmethodID midBitBlt;
jmethodID midAudioCreate;
jmethodID midAudioSetVolume;
jmethodID midAudioStart;
jmethodID midAudioStop;
jmethodID midAudioPause;
jmethodID midAudioPlay;
jintArray jVideoBuffer;
jbyteArray jAudioBuffer;
unsigned short *screen;
int screenWidth;
int screenHeight;
bool flipScreen;
};
void EmuMediaImpl::releaseVideoBuffer(JNIEnv *env)
{
if (jVideoBuffer != NULL) {
env->DeleteGlobalRef(jVideoBuffer);
jVideoBuffer = NULL;
}
if (screen != NULL) {
free(screen);
screen = NULL;
}
}
bool EmuMediaImpl::init(JNIEnv *env)
{
jVideoBuffer = NULL;
screen = NULL;
flipScreen = false;
jPeerClass = env->FindClass("com/androidemu/EmuMedia");
jPeerClass = (jclass) env->NewGlobalRef(jPeerClass);
jAudioBuffer = env->NewByteArray(8192);
jAudioBuffer = (jbyteArray) env->NewGlobalRef(jAudioBuffer);
midDestroy = env->GetStaticMethodID(jPeerClass, "destroy", "()V");
midSetSurface = env->GetStaticMethodID(jPeerClass,
"setSurface", "(Landroid/view/SurfaceHolder;)V");
midSetSurfaceRegion = env->GetStaticMethodID(jPeerClass,
"setSurfaceRegion", "(IIII)V");
midBitBlt = env->GetStaticMethodID(jPeerClass,
"bitBlt", "([IZ)V");
midAudioCreate = env->GetStaticMethodID(jPeerClass,
"audioCreate", "(III)Z");
midAudioSetVolume = env->GetStaticMethodID(jPeerClass,
"audioSetVolume", "(I)V");
midAudioStart = env->GetStaticMethodID(jPeerClass, "audioStart", "()V");
midAudioStop = env->GetStaticMethodID(jPeerClass, "audioStop", "()V");
midAudioPause = env->GetStaticMethodID(jPeerClass, "audioPause", "()V");
midAudioPlay = env->GetStaticMethodID(jPeerClass, "audioPlay", "([BI)V");
return true;
}
void EmuMediaImpl::destroy(JNIEnv *env)
{
env->CallStaticVoidMethod(jPeerClass, midDestroy);
env->DeleteGlobalRef(jAudioBuffer);
env->DeleteGlobalRef(jPeerClass);
releaseVideoBuffer(env);
delete this;
}
void EmuMediaImpl::setSurface(JNIEnv *env, jobject holder)
{
env->CallStaticVoidMethod(jPeerClass, midSetSurface, holder);
if (holder == NULL)
releaseVideoBuffer(env);
}
void EmuMediaImpl::setSurfaceRegion(JNIEnv *env,
int x, int y, int w, int h)
{
releaseVideoBuffer(env);
screenWidth = w;
screenHeight = h;
env->CallStaticIntMethod(jPeerClass, midSetSurfaceRegion, x, y, w, h);
screen = new unsigned short[w * h];
jVideoBuffer = env->NewIntArray(w * h);
jVideoBuffer = (jintArray) env->NewGlobalRef(jVideoBuffer);
}
bool EmuMediaImpl::lockSurface(JNIEnv *env,
EmuEngine::Surface *info, bool flip)
{
flipScreen = flip;
info->bits = screen;
info->bpr = screenWidth * 2;
info->w = screenWidth;
info->h = screenHeight;
return true;
}
void EmuMediaImpl::unlockSurface(JNIEnv *env)
{
jsize size = env->GetArrayLength(jVideoBuffer);
jint *image = env->GetIntArrayElements(jVideoBuffer, 0);
for (int i = 0; i < size; i++) {
unsigned short pix = screen[i];
image[i] = (pix & 0xf800) << 8 |
(pix & 0x07e0) << 5 |
(pix & 0x1f) << 3;
}
env->ReleaseIntArrayElements(jVideoBuffer, image, 0);
env->CallStaticVoidMethod(jPeerClass, midBitBlt,
jVideoBuffer, flipScreen);
}
bool EmuMediaImpl::audioCreate(JNIEnv *env,
unsigned int rate, int bits, int channels)
{
jboolean rv = env->CallStaticBooleanMethod(jPeerClass,
midAudioCreate, rate, bits, channels);
return (rv != JNI_FALSE);
}
void EmuMediaImpl::audioSetVolume(JNIEnv *env, int volume)
{
env->CallStaticVoidMethod(jPeerClass, midAudioSetVolume, volume);
}
void EmuMediaImpl::audioStart(JNIEnv *env)
{
env->CallStaticVoidMethod(jPeerClass, midAudioStart);
}
void EmuMediaImpl::audioStop(JNIEnv *env)
{
env->CallStaticVoidMethod(jPeerClass, midAudioStop);
}
void EmuMediaImpl::audioPause(JNIEnv *env)
{
env->CallStaticVoidMethod(jPeerClass, midAudioPause);
}
void EmuMediaImpl::audioPlay(JNIEnv *env, void *src, int size)
{
env->SetByteArrayRegion(jAudioBuffer, 0, size, (jbyte *) src);
env->CallStaticVoidMethod(jPeerClass, midAudioPlay, jAudioBuffer, size);
}
EmuMedia *createEmuMedia()
{
return new EmuMediaImpl;
}