blob: 5aea8485d0c13133c8a9581562d25961cb96a760 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2006, The Android Open Source Project
4**
Jack Palevich1badb712009-03-25 15:12:17 -07005** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Jack Palevich1badb712009-03-25 15:12:17 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Jack Palevich1badb712009-03-25 15:12:17 -070011** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
Steven Moreland2279b252017-07-19 09:50:45 -070018#include <nativehelper/JNIHelp.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <android_runtime/AndroidRuntime.h>
Dianne Hackborn289b9b62010-07-09 11:44:11 -070020#include <android_runtime/android_view_Surface.h>
Romain Guye5e0c502011-06-15 15:18:31 -070021#include <android_runtime/android_graphics_SurfaceTexture.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <utils/misc.h>
23
Mathias Agopian8335f1c2012-02-25 18:48:35 -080024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <EGL/egl.h>
26#include <GLES/gl.h>
Mathias Agopian04d04612017-03-08 23:21:54 -080027#include <private/EGL/display.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Mathias Agopian8335f1c2012-02-25 18:48:35 -080029#include <gui/Surface.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080030#include <gui/GLConsumer.h>
Mathias Agopian52800612013-02-14 17:11:20 -080031#include <gui/Surface.h>
Romain Guy8ff6b9e2011-11-09 20:10:18 -080032
Mathias Agopianaf01feaf2012-02-24 18:25:41 -080033#include <ui/ANativeObjectBase.h>
34
Dianne Hackborn54a181b2010-06-30 18:35:14 -070035namespace android {
Mathias Agopian8b73ae42010-06-10 17:02:51 -070036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037static jclass gConfig_class;
38
39static jmethodID gConfig_ctorID;
40
41static jfieldID gDisplay_EGLDisplayFieldID;
42static jfieldID gContext_EGLContextFieldID;
43static jfieldID gSurface_EGLSurfaceFieldID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044static jfieldID gConfig_EGLConfigFieldID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046static inline EGLDisplay getDisplay(JNIEnv* env, jobject o) {
47 if (!o) return EGL_NO_DISPLAY;
Ashok Bhat863f98b2014-01-27 16:58:46 +000048 return (EGLDisplay)env->GetLongField(o, gDisplay_EGLDisplayFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049}
50static inline EGLSurface getSurface(JNIEnv* env, jobject o) {
51 if (!o) return EGL_NO_SURFACE;
Ashok Bhat863f98b2014-01-27 16:58:46 +000052 return (EGLSurface)env->GetLongField(o, gSurface_EGLSurfaceFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053}
54static inline EGLContext getContext(JNIEnv* env, jobject o) {
55 if (!o) return EGL_NO_CONTEXT;
Ashok Bhat863f98b2014-01-27 16:58:46 +000056 return (EGLContext)env->GetLongField(o, gContext_EGLContextFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057}
58static inline EGLConfig getConfig(JNIEnv* env, jobject o) {
59 if (!o) return 0;
Ashok Bhat863f98b2014-01-27 16:58:46 +000060 return (EGLConfig)env->GetLongField(o, gConfig_EGLConfigFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061}
Ashok Bhat863f98b2014-01-27 16:58:46 +000062
63static inline jboolean EglBoolToJBool(EGLBoolean eglBool) {
64 return eglBool == EGL_TRUE ? JNI_TRUE : JNI_FALSE;
65}
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
68{
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070069 jclass config_class = _env->FindClass("com/google/android/gles_jni/EGLConfigImpl");
70 gConfig_class = (jclass) _env->NewGlobalRef(config_class);
Ashok Bhat863f98b2014-01-27 16:58:46 +000071 gConfig_ctorID = _env->GetMethodID(gConfig_class, "<init>", "(J)V");
72 gConfig_EGLConfigFieldID = _env->GetFieldID(gConfig_class, "mEGLConfig", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070074 jclass display_class = _env->FindClass("com/google/android/gles_jni/EGLDisplayImpl");
Ashok Bhat863f98b2014-01-27 16:58:46 +000075 gDisplay_EGLDisplayFieldID = _env->GetFieldID(display_class, "mEGLDisplay", "J");
Jack Palevich1badb712009-03-25 15:12:17 -070076
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070077 jclass context_class = _env->FindClass("com/google/android/gles_jni/EGLContextImpl");
Ashok Bhat863f98b2014-01-27 16:58:46 +000078 gContext_EGLContextFieldID = _env->GetFieldID(context_class, "mEGLContext", "J");
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070079
80 jclass surface_class = _env->FindClass("com/google/android/gles_jni/EGLSurfaceImpl");
Ashok Bhat863f98b2014-01-27 16:58:46 +000081 gSurface_EGLSurfaceFieldID = _env->GetFieldID(surface_class, "mEGLSurface", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082}
83
Jack Palevich1badb712009-03-25 15:12:17 -070084static const jint gNull_attrib_base[] = {EGL_NONE};
85
86static bool validAttribList(JNIEnv *_env, jintArray attrib_list) {
87 if (attrib_list == NULL) {
88 return true;
89 }
90 jsize len = _env->GetArrayLength(attrib_list);
91 if (len < 1) {
92 return false;
93 }
94 jint item = 0;
95 _env->GetIntArrayRegion(attrib_list, len-1, 1, &item);
96 return item == EGL_NONE;
97}
98
99static jint* beginNativeAttribList(JNIEnv *_env, jintArray attrib_list) {
100 if (attrib_list != NULL) {
Michael Chockb50662452013-08-06 15:21:10 -0700101 return _env->GetIntArrayElements(attrib_list, (jboolean *)0);
Jack Palevich1badb712009-03-25 15:12:17 -0700102 } else {
103 return(jint*) gNull_attrib_base;
104 }
105}
106
107static void endNativeAttributeList(JNIEnv *_env, jintArray attrib_list, jint* attrib_base) {
108 if (attrib_list != NULL) {
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700109 _env->ReleaseIntArrayElements(attrib_list, attrib_base, 0);
Jack Palevich1badb712009-03-25 15:12:17 -0700110 }
111}
112
113static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 jintArray major_minor) {
Jack Palevich1badb712009-03-25 15:12:17 -0700115 if (display == NULL || (major_minor != NULL &&
116 _env->GetArrayLength(major_minor) < 2)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700117 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700118 return JNI_FALSE;
119 }
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000122 EGLBoolean success = eglInitialize(dpy, NULL, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 if (success && major_minor) {
124 int len = _env->GetArrayLength(major_minor);
125 if (len) {
126 // we're exposing only EGL 1.0
127 jint* base = (jint *)_env->GetPrimitiveArrayCritical(major_minor, (jboolean *)0);
128 if (len >= 1) base[0] = 1;
129 if (len >= 2) base[1] = 0;
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700130 _env->ReleasePrimitiveArrayCritical(major_minor, base, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 }
132 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000133 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134}
135
Jack Palevich1badb712009-03-25 15:12:17 -0700136static jboolean jni_eglQueryContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 jobject context, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700138 if (display == NULL || context == NULL || value == NULL
139 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700140 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 return JNI_FALSE;
142 }
Jack Palevich1badb712009-03-25 15:12:17 -0700143 EGLDisplay dpy = getDisplay(_env, display);
144 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000145 EGLBoolean success = EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 int len = _env->GetArrayLength(value);
147 if (len) {
Michael Chockb50662452013-08-06 15:21:10 -0700148 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 success = eglQueryContext(dpy, ctx, attribute, base);
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700150 _env->ReleaseIntArrayElements(value, base, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000152 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153}
Jack Palevich1badb712009-03-25 15:12:17 -0700154
155static jboolean jni_eglQuerySurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 jobject surface, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700157 if (display == NULL || surface == NULL || value == NULL
158 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700159 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 return JNI_FALSE;
161 }
Jack Palevich1badb712009-03-25 15:12:17 -0700162 EGLDisplay dpy = getDisplay(_env, display);
163 EGLContext sur = getSurface(_env, surface);
164
Ashok Bhat863f98b2014-01-27 16:58:46 +0000165 EGLBoolean success = EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 int len = _env->GetArrayLength(value);
167 if (len) {
Michael Chockb50662452013-08-06 15:21:10 -0700168 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 success = eglQuerySurface(dpy, sur, attribute, base);
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700170 _env->ReleaseIntArrayElements(value, base, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000172 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173}
174
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800175static jint jni_getInitCount(JNIEnv *_env, jobject _clazz, jobject display) {
176 EGLDisplay dpy = getDisplay(_env, display);
Mathias Agopian04d04612017-03-08 23:21:54 -0800177 return android::egl_get_init_count(dpy);
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800178}
179
180static jboolean jni_eglReleaseThread(JNIEnv *_env, jobject _this) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000181 return EglBoolToJBool(eglReleaseThread());
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800182}
183
Jack Palevich1badb712009-03-25 15:12:17 -0700184static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
185 jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
186 if (display == NULL
187 || !validAttribList(_env, attrib_list)
188 || (configs != NULL && _env->GetArrayLength(configs) < config_size)
189 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700190 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 return JNI_FALSE;
192 }
Jack Palevich1badb712009-03-25 15:12:17 -0700193 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000194 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700195
196 if (configs == NULL) {
197 config_size = 0;
198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700200
201 int num = 0;
202 jint* attrib_base = beginNativeAttribList(_env, attrib_list);
203 success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
204 endNativeAttributeList(_env, attrib_list, attrib_base);
205
206 if (num_config != NULL) {
207 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
208 }
209
210 if (success && configs!=NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 for (int i=0 ; i<num ; i++) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000212 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 _env->SetObjectArrayElement(configs, i, obj);
214 }
215 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000216 return EglBoolToJBool(success);
Jack Palevich1badb712009-03-25 15:12:17 -0700217}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218
Ashok Bhat863f98b2014-01-27 16:58:46 +0000219static jlong jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 jobject config, jobject share_context, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700221 if (display == NULL || config == NULL || share_context == NULL
222 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700223 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700224 return JNI_FALSE;
225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 EGLDisplay dpy = getDisplay(_env, display);
227 EGLConfig cnf = getConfig(_env, config);
228 EGLContext shr = getContext(_env, share_context);
Jack Palevich1badb712009-03-25 15:12:17 -0700229 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700231 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000232 return reinterpret_cast<jlong>(ctx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233}
234
Ashok Bhat863f98b2014-01-27 16:58:46 +0000235static jlong jni_eglCreatePbufferSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 jobject config, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700237 if (display == NULL || config == NULL
238 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700239 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700240 return JNI_FALSE;
241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 EGLDisplay dpy = getDisplay(_env, display);
243 EGLConfig cnf = getConfig(_env, config);
Jack Palevich1badb712009-03-25 15:12:17 -0700244 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 EGLSurface sur = eglCreatePbufferSurface(dpy, cnf, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700246 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000247 return reinterpret_cast<jlong>(sur);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248}
249
Jack Palevich1badb712009-03-25 15:12:17 -0700250static void jni_eglCreatePixmapSurface(JNIEnv *_env, jobject _this, jobject out_sur,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 jobject display, jobject config, jobject native_pixmap,
Jack Palevich1badb712009-03-25 15:12:17 -0700252 jintArray attrib_list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253{
Romain Guy1f071dd2016-11-30 10:20:29 -0800254 jniThrowException(_env, "java/lang/UnsupportedOperationException", "eglCreatePixmapSurface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255}
256
Ashok Bhat863f98b2014-01-27 16:58:46 +0000257static jlong jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 jobject config, jobject native_window, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700259 if (display == NULL || config == NULL
260 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700261 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700262 return JNI_FALSE;
263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 EGLDisplay dpy = getDisplay(_env, display);
265 EGLContext cnf = getConfig(_env, config);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700266 sp<ANativeWindow> window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 if (native_window == NULL) {
268not_valid_surface:
Elliott Hughes8451b252011-04-07 19:17:57 -0700269 jniThrowException(_env, "java/lang/IllegalArgumentException",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
271 return 0;
272 }
Mathias Agopian8b73ae42010-06-10 17:02:51 -0700273
Jeff Brown64a55af2012-08-26 02:47:39 -0700274 window = android_view_Surface_getNativeWindow(_env, native_window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 if (window == NULL)
276 goto not_valid_surface;
277
Jack Palevich1badb712009-03-25 15:12:17 -0700278 jint* base = beginNativeAttribList(_env, attrib_list);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700279 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
Jack Palevich1badb712009-03-25 15:12:17 -0700280 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000281 return reinterpret_cast<jlong>(sur);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282}
283
Ashok Bhat863f98b2014-01-27 16:58:46 +0000284static jlong jni_eglCreateWindowSurfaceTexture(JNIEnv *_env, jobject _this, jobject display,
Romain Guye5e0c502011-06-15 15:18:31 -0700285 jobject config, jobject native_window, jintArray attrib_list) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700286 if (display == NULL || config == NULL
287 || !validAttribList(_env, attrib_list)) {
288 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000289 return 0;
Romain Guy8f0095c2011-05-02 17:24:22 -0700290 }
291 EGLDisplay dpy = getDisplay(_env, display);
292 EGLContext cnf = getConfig(_env, config);
293 sp<ANativeWindow> window;
294 if (native_window == 0) {
295not_valid_surface:
296 jniThrowException(_env, "java/lang/IllegalArgumentException",
297 "Make sure the SurfaceTexture is valid");
298 return 0;
299 }
Jesse Hall0fa257f2013-08-30 13:49:14 -0700300
Mathias Agopian52a9a102013-08-02 01:38:38 -0700301 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(_env, native_window));
Jesse Hall0fa257f2013-08-30 13:49:14 -0700302 window = new Surface(producer, true);
Romain Guy8f0095c2011-05-02 17:24:22 -0700303 if (window == NULL)
304 goto not_valid_surface;
305
306 jint* base = beginNativeAttribList(_env, attrib_list);
307 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
308 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000309 return reinterpret_cast<jlong>(sur);
Romain Guy8f0095c2011-05-02 17:24:22 -0700310}
311
Jack Palevich1badb712009-03-25 15:12:17 -0700312static jboolean jni_eglGetConfigAttrib(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 jobject config, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700314 if (display == NULL || config == NULL
315 || (value == NULL || _env->GetArrayLength(value) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700316 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 return JNI_FALSE;
318 }
Jack Palevich1badb712009-03-25 15:12:17 -0700319 EGLDisplay dpy = getDisplay(_env, display);
320 EGLContext cnf = getConfig(_env, config);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000321 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700322 jint localValue;
323 success = eglGetConfigAttrib(dpy, cnf, attribute, &localValue);
324 if (success) {
325 _env->SetIntArrayRegion(value, 0, 1, &localValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000327 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328}
329
Jack Palevich1badb712009-03-25 15:12:17 -0700330static jboolean jni_eglGetConfigs(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 jobjectArray configs, jint config_size, jintArray num_config) {
Jack Palevich1badb712009-03-25 15:12:17 -0700332 if (display == NULL || (configs != NULL && _env->GetArrayLength(configs) < config_size)
333 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700334 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 return JNI_FALSE;
336 }
Jack Palevich1badb712009-03-25 15:12:17 -0700337 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000338 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700339 if (configs == NULL) {
340 config_size = 0;
341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700343 int num;
344 success = eglGetConfigs(dpy, configs ? nativeConfigs : 0, config_size, &num);
345 if (num_config != NULL) {
346 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 if (success && configs) {
349 for (int i=0 ; i<num ; i++) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000350 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 _env->SetObjectArrayElement(configs, i, obj);
352 }
353 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000354 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355}
Jack Palevich1badb712009-03-25 15:12:17 -0700356
357static jint jni_eglGetError(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 EGLint error = eglGetError();
359 return error;
360}
361
Ashok Bhat863f98b2014-01-27 16:58:46 +0000362static jlong jni_eglGetCurrentContext(JNIEnv *_env, jobject _this) {
363 return reinterpret_cast<jlong>(eglGetCurrentContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364}
365
Ashok Bhat863f98b2014-01-27 16:58:46 +0000366static jlong jni_eglGetCurrentDisplay(JNIEnv *_env, jobject _this) {
367 return reinterpret_cast<jlong>(eglGetCurrentDisplay());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368}
369
Ashok Bhat863f98b2014-01-27 16:58:46 +0000370static jlong jni_eglGetCurrentSurface(JNIEnv *_env, jobject _this, jint readdraw) {
Romain Guy9b7146d2011-03-07 18:05:04 -0800371 if ((readdraw != EGL_READ) && (readdraw != EGL_DRAW)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700372 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700373 return 0;
374 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000375 return reinterpret_cast<jlong>(eglGetCurrentSurface(readdraw));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376}
377
Jack Palevich1badb712009-03-25 15:12:17 -0700378static jboolean jni_eglDestroyContext(JNIEnv *_env, jobject _this, jobject display, jobject context) {
379 if (display == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700380 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700381 return JNI_FALSE;
382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 EGLDisplay dpy = getDisplay(_env, display);
384 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000385 return EglBoolToJBool(eglDestroyContext(dpy, ctx));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386}
387
Jack Palevich1badb712009-03-25 15:12:17 -0700388static jboolean jni_eglDestroySurface(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
389 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700390 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700391 return JNI_FALSE;
392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 EGLDisplay dpy = getDisplay(_env, display);
394 EGLSurface sur = getSurface(_env, surface);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000395 return EglBoolToJBool(eglDestroySurface(dpy, sur));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396}
397
Ashok Bhat863f98b2014-01-27 16:58:46 +0000398static jlong jni_eglGetDisplay(JNIEnv *_env, jobject _this, jobject native_display) {
399 return reinterpret_cast<jlong>(eglGetDisplay(EGL_DEFAULT_DISPLAY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400}
401
Jack Palevich1badb712009-03-25 15:12:17 -0700402static jboolean jni_eglMakeCurrent(JNIEnv *_env, jobject _this, jobject display, jobject draw, jobject read, jobject context) {
403 if (display == NULL || draw == NULL || read == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700404 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700405 return JNI_FALSE;
406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 EGLDisplay dpy = getDisplay(_env, display);
408 EGLSurface sdr = getSurface(_env, draw);
409 EGLSurface srd = getSurface(_env, read);
410 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000411 return EglBoolToJBool(eglMakeCurrent(dpy, sdr, srd, ctx));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412}
413
Jack Palevich1badb712009-03-25 15:12:17 -0700414static jstring jni_eglQueryString(JNIEnv *_env, jobject _this, jobject display, jint name) {
415 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700416 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700417 return NULL;
418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 EGLDisplay dpy = getDisplay(_env, display);
420 const char* chars = eglQueryString(dpy, name);
421 return _env->NewStringUTF(chars);
422}
423
Jack Palevich1badb712009-03-25 15:12:17 -0700424static jboolean jni_eglSwapBuffers(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
425 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700426 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700427 return JNI_FALSE;
428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 EGLDisplay dpy = getDisplay(_env, display);
430 EGLSurface sur = getSurface(_env, surface);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000431 return EglBoolToJBool(eglSwapBuffers(dpy, sur));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432}
433
Jack Palevich1badb712009-03-25 15:12:17 -0700434static jboolean jni_eglTerminate(JNIEnv *_env, jobject _this, jobject display) {
435 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700436 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700437 return JNI_FALSE;
438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000440 return EglBoolToJBool(eglTerminate(dpy));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441}
442
Jack Palevich1badb712009-03-25 15:12:17 -0700443static jboolean jni_eglCopyBuffers(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 jobject surface, jobject native_pixmap) {
Jack Palevich1badb712009-03-25 15:12:17 -0700445 if (display == NULL || surface == NULL || native_pixmap == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700446 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700447 return JNI_FALSE;
448 }
449 // TODO: Implement this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 return JNI_FALSE;
451}
452
Jack Palevich1badb712009-03-25 15:12:17 -0700453static jboolean jni_eglWaitGL(JNIEnv *_env, jobject _this) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000454 return EglBoolToJBool(eglWaitGL());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455}
456
Jack Palevich1badb712009-03-25 15:12:17 -0700457static jboolean jni_eglWaitNative(JNIEnv *_env, jobject _this, jint engine, jobject bindTarget) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000458 return EglBoolToJBool(eglWaitNative(engine));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459}
460
461
462static const char *classPathName = "com/google/android/gles_jni/EGLImpl";
463
464#define DISPLAY "Ljavax/microedition/khronos/egl/EGLDisplay;"
465#define CONTEXT "Ljavax/microedition/khronos/egl/EGLContext;"
466#define CONFIG "Ljavax/microedition/khronos/egl/EGLConfig;"
467#define SURFACE "Ljavax/microedition/khronos/egl/EGLSurface;"
468#define OBJECT "Ljava/lang/Object;"
469#define STRING "Ljava/lang/String;"
470
Daniel Micay76f6a862015-09-19 17:31:01 -0400471static const JNINativeMethod methods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472{"_nativeClassInit","()V", (void*)nativeClassInit },
473{"eglWaitGL", "()Z", (void*)jni_eglWaitGL },
474{"eglInitialize", "(" DISPLAY "[I)Z", (void*)jni_eglInitialize },
475{"eglQueryContext", "(" DISPLAY CONTEXT "I[I)Z", (void*)jni_eglQueryContext },
476{"eglQuerySurface", "(" DISPLAY SURFACE "I[I)Z", (void*)jni_eglQuerySurface },
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800477{"eglReleaseThread","()Z", (void*)jni_eglReleaseThread },
478{"getInitCount", "(" DISPLAY ")I", (void*)jni_getInitCount },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479{"eglChooseConfig", "(" DISPLAY "[I[" CONFIG "I[I)Z", (void*)jni_eglChooseConfig },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000480{"_eglCreateContext","(" DISPLAY CONFIG CONTEXT "[I)J", (void*)jni_eglCreateContext },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481{"eglGetConfigs", "(" DISPLAY "[" CONFIG "I[I)Z", (void*)jni_eglGetConfigs },
482{"eglTerminate", "(" DISPLAY ")Z", (void*)jni_eglTerminate },
483{"eglCopyBuffers", "(" DISPLAY SURFACE OBJECT ")Z", (void*)jni_eglCopyBuffers },
484{"eglWaitNative", "(I" OBJECT ")Z", (void*)jni_eglWaitNative },
485{"eglGetError", "()I", (void*)jni_eglGetError },
486{"eglGetConfigAttrib", "(" DISPLAY CONFIG "I[I)Z", (void*)jni_eglGetConfigAttrib },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000487{"_eglGetDisplay", "(" OBJECT ")J", (void*)jni_eglGetDisplay },
488{"_eglGetCurrentContext", "()J", (void*)jni_eglGetCurrentContext },
489{"_eglGetCurrentDisplay", "()J", (void*)jni_eglGetCurrentDisplay },
490{"_eglGetCurrentSurface", "(I)J", (void*)jni_eglGetCurrentSurface },
491{"_eglCreatePbufferSurface","(" DISPLAY CONFIG "[I)J", (void*)jni_eglCreatePbufferSurface },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492{"_eglCreatePixmapSurface", "(" SURFACE DISPLAY CONFIG OBJECT "[I)V", (void*)jni_eglCreatePixmapSurface },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000493{"_eglCreateWindowSurface", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurface },
494{"_eglCreateWindowSurfaceTexture", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurfaceTexture },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495{"eglDestroyContext", "(" DISPLAY CONTEXT ")Z", (void*)jni_eglDestroyContext },
496{"eglDestroySurface", "(" DISPLAY SURFACE ")Z", (void*)jni_eglDestroySurface },
497{"eglMakeCurrent", "(" DISPLAY SURFACE SURFACE CONTEXT")Z", (void*)jni_eglMakeCurrent },
498{"eglQueryString", "(" DISPLAY "I)" STRING, (void*)jni_eglQueryString },
499{"eglSwapBuffers", "(" DISPLAY SURFACE ")Z", (void*)jni_eglSwapBuffers },
500};
501
502} // namespace android
503
504int register_com_google_android_gles_jni_EGLImpl(JNIEnv *_env)
505{
506 int err;
507 err = android::AndroidRuntime::registerNativeMethods(_env,
508 android::classPathName, android::methods, NELEM(android::methods));
509 return err;
510}