1 /*
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include "JNIHelp.h"
19 #include <android_runtime/AndroidRuntime.h>
20 #include <android_runtime/android_view_Surface.h>
21 #include <android_runtime/android_graphics_SurfaceTexture.h>
22 #include <utils/misc.h>
23
24
25 #include <EGL/egl_display.h>
26 #include <EGL/egl.h>
27 #include <GLES/gl.h>
28
29 #include <gui/Surface.h>
30 #include <gui/GLConsumer.h>
31 #include <gui/Surface.h>
32
33 #include <SkBitmap.h>
34 #include <SkPixelRef.h>
35
36 #include <ui/ANativeObjectBase.h>
37
38 namespace android {
39
40 static jclass gConfig_class;
41
42 static jmethodID gConfig_ctorID;
43
44 static jfieldID gDisplay_EGLDisplayFieldID;
45 static jfieldID gContext_EGLContextFieldID;
46 static jfieldID gSurface_EGLSurfaceFieldID;
47 static jfieldID gSurface_NativePixelRefFieldID;
48 static jfieldID gConfig_EGLConfigFieldID;
49 static jfieldID gBitmap_NativeBitmapFieldID;
50
getDisplay(JNIEnv * env,jobject o)51 static inline EGLDisplay getDisplay(JNIEnv* env, jobject o) {
52 if (!o) return EGL_NO_DISPLAY;
53 return (EGLDisplay)env->GetLongField(o, gDisplay_EGLDisplayFieldID);
54 }
getSurface(JNIEnv * env,jobject o)55 static inline EGLSurface getSurface(JNIEnv* env, jobject o) {
56 if (!o) return EGL_NO_SURFACE;
57 return (EGLSurface)env->GetLongField(o, gSurface_EGLSurfaceFieldID);
58 }
getContext(JNIEnv * env,jobject o)59 static inline EGLContext getContext(JNIEnv* env, jobject o) {
60 if (!o) return EGL_NO_CONTEXT;
61 return (EGLContext)env->GetLongField(o, gContext_EGLContextFieldID);
62 }
getConfig(JNIEnv * env,jobject o)63 static inline EGLConfig getConfig(JNIEnv* env, jobject o) {
64 if (!o) return 0;
65 return (EGLConfig)env->GetLongField(o, gConfig_EGLConfigFieldID);
66 }
67
EglBoolToJBool(EGLBoolean eglBool)68 static inline jboolean EglBoolToJBool(EGLBoolean eglBool) {
69 return eglBool == EGL_TRUE ? JNI_TRUE : JNI_FALSE;
70 }
71
nativeClassInit(JNIEnv * _env,jclass eglImplClass)72 static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
73 {
74 jclass config_class = _env->FindClass("com/google/android/gles_jni/EGLConfigImpl");
75 gConfig_class = (jclass) _env->NewGlobalRef(config_class);
76 gConfig_ctorID = _env->GetMethodID(gConfig_class, "<init>", "(J)V");
77 gConfig_EGLConfigFieldID = _env->GetFieldID(gConfig_class, "mEGLConfig", "J");
78
79 jclass display_class = _env->FindClass("com/google/android/gles_jni/EGLDisplayImpl");
80 gDisplay_EGLDisplayFieldID = _env->GetFieldID(display_class, "mEGLDisplay", "J");
81
82 jclass context_class = _env->FindClass("com/google/android/gles_jni/EGLContextImpl");
83 gContext_EGLContextFieldID = _env->GetFieldID(context_class, "mEGLContext", "J");
84
85 jclass surface_class = _env->FindClass("com/google/android/gles_jni/EGLSurfaceImpl");
86 gSurface_EGLSurfaceFieldID = _env->GetFieldID(surface_class, "mEGLSurface", "J");
87 gSurface_NativePixelRefFieldID = _env->GetFieldID(surface_class, "mNativePixelRef", "J");
88
89 jclass bitmap_class = _env->FindClass("android/graphics/Bitmap");
90 gBitmap_NativeBitmapFieldID = _env->GetFieldID(bitmap_class, "mNativeBitmap", "J");
91 }
92
93 static const jint gNull_attrib_base[] = {EGL_NONE};
94
validAttribList(JNIEnv * _env,jintArray attrib_list)95 static bool validAttribList(JNIEnv *_env, jintArray attrib_list) {
96 if (attrib_list == NULL) {
97 return true;
98 }
99 jsize len = _env->GetArrayLength(attrib_list);
100 if (len < 1) {
101 return false;
102 }
103 jint item = 0;
104 _env->GetIntArrayRegion(attrib_list, len-1, 1, &item);
105 return item == EGL_NONE;
106 }
107
beginNativeAttribList(JNIEnv * _env,jintArray attrib_list)108 static jint* beginNativeAttribList(JNIEnv *_env, jintArray attrib_list) {
109 if (attrib_list != NULL) {
110 return _env->GetIntArrayElements(attrib_list, (jboolean *)0);
111 } else {
112 return(jint*) gNull_attrib_base;
113 }
114 }
115
endNativeAttributeList(JNIEnv * _env,jintArray attrib_list,jint * attrib_base)116 static void endNativeAttributeList(JNIEnv *_env, jintArray attrib_list, jint* attrib_base) {
117 if (attrib_list != NULL) {
118 _env->ReleaseIntArrayElements(attrib_list, attrib_base, 0);
119 }
120 }
121
jni_eglInitialize(JNIEnv * _env,jobject _this,jobject display,jintArray major_minor)122 static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
123 jintArray major_minor) {
124 if (display == NULL || (major_minor != NULL &&
125 _env->GetArrayLength(major_minor) < 2)) {
126 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
127 return JNI_FALSE;
128 }
129
130 EGLDisplay dpy = getDisplay(_env, display);
131 EGLBoolean success = eglInitialize(dpy, NULL, NULL);
132 if (success && major_minor) {
133 int len = _env->GetArrayLength(major_minor);
134 if (len) {
135 // we're exposing only EGL 1.0
136 jint* base = (jint *)_env->GetPrimitiveArrayCritical(major_minor, (jboolean *)0);
137 if (len >= 1) base[0] = 1;
138 if (len >= 2) base[1] = 0;
139 _env->ReleasePrimitiveArrayCritical(major_minor, base, 0);
140 }
141 }
142 return EglBoolToJBool(success);
143 }
144
jni_eglQueryContext(JNIEnv * _env,jobject _this,jobject display,jobject context,jint attribute,jintArray value)145 static jboolean jni_eglQueryContext(JNIEnv *_env, jobject _this, jobject display,
146 jobject context, jint attribute, jintArray value) {
147 if (display == NULL || context == NULL || value == NULL
148 || _env->GetArrayLength(value) < 1) {
149 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
150 return JNI_FALSE;
151 }
152 EGLDisplay dpy = getDisplay(_env, display);
153 EGLContext ctx = getContext(_env, context);
154 EGLBoolean success = EGL_FALSE;
155 int len = _env->GetArrayLength(value);
156 if (len) {
157 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
158 success = eglQueryContext(dpy, ctx, attribute, base);
159 _env->ReleaseIntArrayElements(value, base, 0);
160 }
161 return EglBoolToJBool(success);
162 }
163
jni_eglQuerySurface(JNIEnv * _env,jobject _this,jobject display,jobject surface,jint attribute,jintArray value)164 static jboolean jni_eglQuerySurface(JNIEnv *_env, jobject _this, jobject display,
165 jobject surface, jint attribute, jintArray value) {
166 if (display == NULL || surface == NULL || value == NULL
167 || _env->GetArrayLength(value) < 1) {
168 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
169 return JNI_FALSE;
170 }
171 EGLDisplay dpy = getDisplay(_env, display);
172 EGLContext sur = getSurface(_env, surface);
173
174 EGLBoolean success = EGL_FALSE;
175 int len = _env->GetArrayLength(value);
176 if (len) {
177 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
178 success = eglQuerySurface(dpy, sur, attribute, base);
179 _env->ReleaseIntArrayElements(value, base, 0);
180 }
181 return EglBoolToJBool(success);
182 }
183
jni_getInitCount(JNIEnv * _env,jobject _clazz,jobject display)184 static jint jni_getInitCount(JNIEnv *_env, jobject _clazz, jobject display) {
185 EGLDisplay dpy = getDisplay(_env, display);
186 egl_display_t* eglDisplay = get_display_nowake(dpy);
187 return eglDisplay ? eglDisplay->getRefsCount() : 0;
188 }
189
jni_eglReleaseThread(JNIEnv * _env,jobject _this)190 static jboolean jni_eglReleaseThread(JNIEnv *_env, jobject _this) {
191 return EglBoolToJBool(eglReleaseThread());
192 }
193
jni_eglChooseConfig(JNIEnv * _env,jobject _this,jobject display,jintArray attrib_list,jobjectArray configs,jint config_size,jintArray num_config)194 static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
195 jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
196 if (display == NULL
197 || !validAttribList(_env, attrib_list)
198 || (configs != NULL && _env->GetArrayLength(configs) < config_size)
199 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
200 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
201 return JNI_FALSE;
202 }
203 EGLDisplay dpy = getDisplay(_env, display);
204 EGLBoolean success = EGL_FALSE;
205
206 if (configs == NULL) {
207 config_size = 0;
208 }
209 EGLConfig nativeConfigs[config_size];
210
211 int num = 0;
212 jint* attrib_base = beginNativeAttribList(_env, attrib_list);
213 success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
214 endNativeAttributeList(_env, attrib_list, attrib_base);
215
216 if (num_config != NULL) {
217 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
218 }
219
220 if (success && configs!=NULL) {
221 for (int i=0 ; i<num ; i++) {
222 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
223 _env->SetObjectArrayElement(configs, i, obj);
224 }
225 }
226 return EglBoolToJBool(success);
227 }
228
jni_eglCreateContext(JNIEnv * _env,jobject _this,jobject display,jobject config,jobject share_context,jintArray attrib_list)229 static jlong jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
230 jobject config, jobject share_context, jintArray attrib_list) {
231 if (display == NULL || config == NULL || share_context == NULL
232 || !validAttribList(_env, attrib_list)) {
233 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
234 return JNI_FALSE;
235 }
236 EGLDisplay dpy = getDisplay(_env, display);
237 EGLConfig cnf = getConfig(_env, config);
238 EGLContext shr = getContext(_env, share_context);
239 jint* base = beginNativeAttribList(_env, attrib_list);
240 EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
241 endNativeAttributeList(_env, attrib_list, base);
242 return reinterpret_cast<jlong>(ctx);
243 }
244
jni_eglCreatePbufferSurface(JNIEnv * _env,jobject _this,jobject display,jobject config,jintArray attrib_list)245 static jlong jni_eglCreatePbufferSurface(JNIEnv *_env, jobject _this, jobject display,
246 jobject config, jintArray attrib_list) {
247 if (display == NULL || config == NULL
248 || !validAttribList(_env, attrib_list)) {
249 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
250 return JNI_FALSE;
251 }
252 EGLDisplay dpy = getDisplay(_env, display);
253 EGLConfig cnf = getConfig(_env, config);
254 jint* base = beginNativeAttribList(_env, attrib_list);
255 EGLSurface sur = eglCreatePbufferSurface(dpy, cnf, base);
256 endNativeAttributeList(_env, attrib_list, base);
257 return reinterpret_cast<jlong>(sur);
258 }
259
convertPixelFormat(SkColorType format)260 static PixelFormat convertPixelFormat(SkColorType format)
261 {
262 switch (format) {
263 case kN32_SkColorType: return PIXEL_FORMAT_RGBA_8888;
264 case kARGB_4444_SkColorType: return PIXEL_FORMAT_RGBA_4444;
265 case kRGB_565_SkColorType: return PIXEL_FORMAT_RGB_565;
266 default: return PIXEL_FORMAT_NONE;
267 }
268 }
269
jni_eglCreatePixmapSurface(JNIEnv * _env,jobject _this,jobject out_sur,jobject display,jobject config,jobject native_pixmap,jintArray attrib_list)270 static void jni_eglCreatePixmapSurface(JNIEnv *_env, jobject _this, jobject out_sur,
271 jobject display, jobject config, jobject native_pixmap,
272 jintArray attrib_list)
273 {
274 if (display == NULL || config == NULL || native_pixmap == NULL
275 || !validAttribList(_env, attrib_list)) {
276 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
277 return;
278 }
279 EGLDisplay dpy = getDisplay(_env, display);
280 EGLConfig cnf = getConfig(_env, config);
281 jint* base = 0;
282
283 SkBitmap const * nativeBitmap =
284 (SkBitmap const *)_env->GetLongField(native_pixmap,
285 gBitmap_NativeBitmapFieldID);
286 SkPixelRef* ref = nativeBitmap ? nativeBitmap->pixelRef() : 0;
287 if (ref == NULL) {
288 jniThrowException(_env, "java/lang/IllegalArgumentException", "Bitmap has no PixelRef");
289 return;
290 }
291
292 SkSafeRef(ref);
293 ref->lockPixels();
294
295 egl_native_pixmap_t pixmap;
296 pixmap.version = sizeof(pixmap);
297 pixmap.width = nativeBitmap->width();
298 pixmap.height = nativeBitmap->height();
299 pixmap.stride = nativeBitmap->rowBytes() / nativeBitmap->bytesPerPixel();
300 pixmap.format = convertPixelFormat(nativeBitmap->colorType());
301 pixmap.data = (uint8_t*)ref->pixels();
302
303 base = beginNativeAttribList(_env, attrib_list);
304 EGLSurface sur = eglCreatePixmapSurface(dpy, cnf, &pixmap, base);
305 endNativeAttributeList(_env, attrib_list, base);
306
307 if (sur != EGL_NO_SURFACE) {
308 _env->SetLongField(out_sur, gSurface_EGLSurfaceFieldID, reinterpret_cast<jlong>(sur));
309 _env->SetLongField(out_sur, gSurface_NativePixelRefFieldID, reinterpret_cast<jlong>(ref));
310 } else {
311 ref->unlockPixels();
312 SkSafeUnref(ref);
313 }
314 }
315
jni_eglCreateWindowSurface(JNIEnv * _env,jobject _this,jobject display,jobject config,jobject native_window,jintArray attrib_list)316 static jlong jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
317 jobject config, jobject native_window, jintArray attrib_list) {
318 if (display == NULL || config == NULL
319 || !validAttribList(_env, attrib_list)) {
320 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
321 return JNI_FALSE;
322 }
323 EGLDisplay dpy = getDisplay(_env, display);
324 EGLContext cnf = getConfig(_env, config);
325 sp<ANativeWindow> window;
326 if (native_window == NULL) {
327 not_valid_surface:
328 jniThrowException(_env, "java/lang/IllegalArgumentException",
329 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
330 return 0;
331 }
332
333 window = android_view_Surface_getNativeWindow(_env, native_window);
334 if (window == NULL)
335 goto not_valid_surface;
336
337 jint* base = beginNativeAttribList(_env, attrib_list);
338 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
339 endNativeAttributeList(_env, attrib_list, base);
340 return reinterpret_cast<jlong>(sur);
341 }
342
jni_eglCreateWindowSurfaceTexture(JNIEnv * _env,jobject _this,jobject display,jobject config,jobject native_window,jintArray attrib_list)343 static jlong jni_eglCreateWindowSurfaceTexture(JNIEnv *_env, jobject _this, jobject display,
344 jobject config, jobject native_window, jintArray attrib_list) {
345 if (display == NULL || config == NULL
346 || !validAttribList(_env, attrib_list)) {
347 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
348 return 0;
349 }
350 EGLDisplay dpy = getDisplay(_env, display);
351 EGLContext cnf = getConfig(_env, config);
352 sp<ANativeWindow> window;
353 if (native_window == 0) {
354 not_valid_surface:
355 jniThrowException(_env, "java/lang/IllegalArgumentException",
356 "Make sure the SurfaceTexture is valid");
357 return 0;
358 }
359
360 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(_env, native_window));
361 window = new Surface(producer, true);
362 if (window == NULL)
363 goto not_valid_surface;
364
365 jint* base = beginNativeAttribList(_env, attrib_list);
366 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
367 endNativeAttributeList(_env, attrib_list, base);
368 return reinterpret_cast<jlong>(sur);
369 }
370
jni_eglGetConfigAttrib(JNIEnv * _env,jobject _this,jobject display,jobject config,jint attribute,jintArray value)371 static jboolean jni_eglGetConfigAttrib(JNIEnv *_env, jobject _this, jobject display,
372 jobject config, jint attribute, jintArray value) {
373 if (display == NULL || config == NULL
374 || (value == NULL || _env->GetArrayLength(value) < 1)) {
375 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
376 return JNI_FALSE;
377 }
378 EGLDisplay dpy = getDisplay(_env, display);
379 EGLContext cnf = getConfig(_env, config);
380 EGLBoolean success = EGL_FALSE;
381 jint localValue;
382 success = eglGetConfigAttrib(dpy, cnf, attribute, &localValue);
383 if (success) {
384 _env->SetIntArrayRegion(value, 0, 1, &localValue);
385 }
386 return EglBoolToJBool(success);
387 }
388
jni_eglGetConfigs(JNIEnv * _env,jobject _this,jobject display,jobjectArray configs,jint config_size,jintArray num_config)389 static jboolean jni_eglGetConfigs(JNIEnv *_env, jobject _this, jobject display,
390 jobjectArray configs, jint config_size, jintArray num_config) {
391 if (display == NULL || (configs != NULL && _env->GetArrayLength(configs) < config_size)
392 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
393 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
394 return JNI_FALSE;
395 }
396 EGLDisplay dpy = getDisplay(_env, display);
397 EGLBoolean success = EGL_FALSE;
398 if (configs == NULL) {
399 config_size = 0;
400 }
401 EGLConfig nativeConfigs[config_size];
402 int num;
403 success = eglGetConfigs(dpy, configs ? nativeConfigs : 0, config_size, &num);
404 if (num_config != NULL) {
405 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
406 }
407 if (success && configs) {
408 for (int i=0 ; i<num ; i++) {
409 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
410 _env->SetObjectArrayElement(configs, i, obj);
411 }
412 }
413 return EglBoolToJBool(success);
414 }
415
jni_eglGetError(JNIEnv * _env,jobject _this)416 static jint jni_eglGetError(JNIEnv *_env, jobject _this) {
417 EGLint error = eglGetError();
418 return error;
419 }
420
jni_eglGetCurrentContext(JNIEnv * _env,jobject _this)421 static jlong jni_eglGetCurrentContext(JNIEnv *_env, jobject _this) {
422 return reinterpret_cast<jlong>(eglGetCurrentContext());
423 }
424
jni_eglGetCurrentDisplay(JNIEnv * _env,jobject _this)425 static jlong jni_eglGetCurrentDisplay(JNIEnv *_env, jobject _this) {
426 return reinterpret_cast<jlong>(eglGetCurrentDisplay());
427 }
428
jni_eglGetCurrentSurface(JNIEnv * _env,jobject _this,jint readdraw)429 static jlong jni_eglGetCurrentSurface(JNIEnv *_env, jobject _this, jint readdraw) {
430 if ((readdraw != EGL_READ) && (readdraw != EGL_DRAW)) {
431 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
432 return 0;
433 }
434 return reinterpret_cast<jlong>(eglGetCurrentSurface(readdraw));
435 }
436
jni_eglDestroyContext(JNIEnv * _env,jobject _this,jobject display,jobject context)437 static jboolean jni_eglDestroyContext(JNIEnv *_env, jobject _this, jobject display, jobject context) {
438 if (display == NULL || context == NULL) {
439 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
440 return JNI_FALSE;
441 }
442 EGLDisplay dpy = getDisplay(_env, display);
443 EGLContext ctx = getContext(_env, context);
444 return EglBoolToJBool(eglDestroyContext(dpy, ctx));
445 }
446
jni_eglDestroySurface(JNIEnv * _env,jobject _this,jobject display,jobject surface)447 static jboolean jni_eglDestroySurface(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
448 if (display == NULL || surface == NULL) {
449 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
450 return JNI_FALSE;
451 }
452 EGLDisplay dpy = getDisplay(_env, display);
453 EGLSurface sur = getSurface(_env, surface);
454
455 if (sur) {
456 SkPixelRef* ref = (SkPixelRef*)(_env->GetLongField(surface,
457 gSurface_NativePixelRefFieldID));
458 if (ref) {
459 ref->unlockPixels();
460 SkSafeUnref(ref);
461 }
462 }
463 return EglBoolToJBool(eglDestroySurface(dpy, sur));
464 }
465
jni_eglGetDisplay(JNIEnv * _env,jobject _this,jobject native_display)466 static jlong jni_eglGetDisplay(JNIEnv *_env, jobject _this, jobject native_display) {
467 return reinterpret_cast<jlong>(eglGetDisplay(EGL_DEFAULT_DISPLAY));
468 }
469
jni_eglMakeCurrent(JNIEnv * _env,jobject _this,jobject display,jobject draw,jobject read,jobject context)470 static jboolean jni_eglMakeCurrent(JNIEnv *_env, jobject _this, jobject display, jobject draw, jobject read, jobject context) {
471 if (display == NULL || draw == NULL || read == NULL || context == NULL) {
472 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
473 return JNI_FALSE;
474 }
475 EGLDisplay dpy = getDisplay(_env, display);
476 EGLSurface sdr = getSurface(_env, draw);
477 EGLSurface srd = getSurface(_env, read);
478 EGLContext ctx = getContext(_env, context);
479 return EglBoolToJBool(eglMakeCurrent(dpy, sdr, srd, ctx));
480 }
481
jni_eglQueryString(JNIEnv * _env,jobject _this,jobject display,jint name)482 static jstring jni_eglQueryString(JNIEnv *_env, jobject _this, jobject display, jint name) {
483 if (display == NULL) {
484 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
485 return NULL;
486 }
487 EGLDisplay dpy = getDisplay(_env, display);
488 const char* chars = eglQueryString(dpy, name);
489 return _env->NewStringUTF(chars);
490 }
491
jni_eglSwapBuffers(JNIEnv * _env,jobject _this,jobject display,jobject surface)492 static jboolean jni_eglSwapBuffers(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
493 if (display == NULL || surface == NULL) {
494 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
495 return JNI_FALSE;
496 }
497 EGLDisplay dpy = getDisplay(_env, display);
498 EGLSurface sur = getSurface(_env, surface);
499 return EglBoolToJBool(eglSwapBuffers(dpy, sur));
500 }
501
jni_eglTerminate(JNIEnv * _env,jobject _this,jobject display)502 static jboolean jni_eglTerminate(JNIEnv *_env, jobject _this, jobject display) {
503 if (display == NULL) {
504 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
505 return JNI_FALSE;
506 }
507 EGLDisplay dpy = getDisplay(_env, display);
508 return EglBoolToJBool(eglTerminate(dpy));
509 }
510
jni_eglCopyBuffers(JNIEnv * _env,jobject _this,jobject display,jobject surface,jobject native_pixmap)511 static jboolean jni_eglCopyBuffers(JNIEnv *_env, jobject _this, jobject display,
512 jobject surface, jobject native_pixmap) {
513 if (display == NULL || surface == NULL || native_pixmap == NULL) {
514 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
515 return JNI_FALSE;
516 }
517 // TODO: Implement this
518 return JNI_FALSE;
519 }
520
jni_eglWaitGL(JNIEnv * _env,jobject _this)521 static jboolean jni_eglWaitGL(JNIEnv *_env, jobject _this) {
522 return EglBoolToJBool(eglWaitGL());
523 }
524
jni_eglWaitNative(JNIEnv * _env,jobject _this,jint engine,jobject bindTarget)525 static jboolean jni_eglWaitNative(JNIEnv *_env, jobject _this, jint engine, jobject bindTarget) {
526 return EglBoolToJBool(eglWaitNative(engine));
527 }
528
529
530 static const char *classPathName = "com/google/android/gles_jni/EGLImpl";
531
532 #define DISPLAY "Ljavax/microedition/khronos/egl/EGLDisplay;"
533 #define CONTEXT "Ljavax/microedition/khronos/egl/EGLContext;"
534 #define CONFIG "Ljavax/microedition/khronos/egl/EGLConfig;"
535 #define SURFACE "Ljavax/microedition/khronos/egl/EGLSurface;"
536 #define OBJECT "Ljava/lang/Object;"
537 #define STRING "Ljava/lang/String;"
538
539 static JNINativeMethod methods[] = {
540 {"_nativeClassInit","()V", (void*)nativeClassInit },
541 {"eglWaitGL", "()Z", (void*)jni_eglWaitGL },
542 {"eglInitialize", "(" DISPLAY "[I)Z", (void*)jni_eglInitialize },
543 {"eglQueryContext", "(" DISPLAY CONTEXT "I[I)Z", (void*)jni_eglQueryContext },
544 {"eglQuerySurface", "(" DISPLAY SURFACE "I[I)Z", (void*)jni_eglQuerySurface },
545 {"eglReleaseThread","()Z", (void*)jni_eglReleaseThread },
546 {"getInitCount", "(" DISPLAY ")I", (void*)jni_getInitCount },
547 {"eglChooseConfig", "(" DISPLAY "[I[" CONFIG "I[I)Z", (void*)jni_eglChooseConfig },
548 {"_eglCreateContext","(" DISPLAY CONFIG CONTEXT "[I)J", (void*)jni_eglCreateContext },
549 {"eglGetConfigs", "(" DISPLAY "[" CONFIG "I[I)Z", (void*)jni_eglGetConfigs },
550 {"eglTerminate", "(" DISPLAY ")Z", (void*)jni_eglTerminate },
551 {"eglCopyBuffers", "(" DISPLAY SURFACE OBJECT ")Z", (void*)jni_eglCopyBuffers },
552 {"eglWaitNative", "(I" OBJECT ")Z", (void*)jni_eglWaitNative },
553 {"eglGetError", "()I", (void*)jni_eglGetError },
554 {"eglGetConfigAttrib", "(" DISPLAY CONFIG "I[I)Z", (void*)jni_eglGetConfigAttrib },
555 {"_eglGetDisplay", "(" OBJECT ")J", (void*)jni_eglGetDisplay },
556 {"_eglGetCurrentContext", "()J", (void*)jni_eglGetCurrentContext },
557 {"_eglGetCurrentDisplay", "()J", (void*)jni_eglGetCurrentDisplay },
558 {"_eglGetCurrentSurface", "(I)J", (void*)jni_eglGetCurrentSurface },
559 {"_eglCreatePbufferSurface","(" DISPLAY CONFIG "[I)J", (void*)jni_eglCreatePbufferSurface },
560 {"_eglCreatePixmapSurface", "(" SURFACE DISPLAY CONFIG OBJECT "[I)V", (void*)jni_eglCreatePixmapSurface },
561 {"_eglCreateWindowSurface", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurface },
562 {"_eglCreateWindowSurfaceTexture", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurfaceTexture },
563 {"eglDestroyContext", "(" DISPLAY CONTEXT ")Z", (void*)jni_eglDestroyContext },
564 {"eglDestroySurface", "(" DISPLAY SURFACE ")Z", (void*)jni_eglDestroySurface },
565 {"eglMakeCurrent", "(" DISPLAY SURFACE SURFACE CONTEXT")Z", (void*)jni_eglMakeCurrent },
566 {"eglQueryString", "(" DISPLAY "I)" STRING, (void*)jni_eglQueryString },
567 {"eglSwapBuffers", "(" DISPLAY SURFACE ")Z", (void*)jni_eglSwapBuffers },
568 };
569
570 } // namespace android
571
register_com_google_android_gles_jni_EGLImpl(JNIEnv * _env)572 int register_com_google_android_gles_jni_EGLImpl(JNIEnv *_env)
573 {
574 int err;
575 err = android::AndroidRuntime::registerNativeMethods(_env,
576 android::classPathName, android::methods, NELEM(android::methods));
577 return err;
578 }
579