blob: 940ac22193b3ad0a6735c2509c99c9e33f86ba55 [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
John Reckf4faeac2015-03-05 13:50:31 -080033#include <GraphicsJNI.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include <SkBitmap.h>
35#include <SkPixelRef.h>
36
Mathias Agopianaf01feaf2012-02-24 18:25:41 -080037#include <ui/ANativeObjectBase.h>
38
Dianne Hackborn54a181b2010-06-30 18:35:14 -070039namespace android {
Mathias Agopian8b73ae42010-06-10 17:02:51 -070040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041static jclass gConfig_class;
42
43static jmethodID gConfig_ctorID;
44
45static jfieldID gDisplay_EGLDisplayFieldID;
46static jfieldID gContext_EGLContextFieldID;
47static jfieldID gSurface_EGLSurfaceFieldID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048static jfieldID gConfig_EGLConfigFieldID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050static inline EGLDisplay getDisplay(JNIEnv* env, jobject o) {
51 if (!o) return EGL_NO_DISPLAY;
Ashok Bhat863f98b2014-01-27 16:58:46 +000052 return (EGLDisplay)env->GetLongField(o, gDisplay_EGLDisplayFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053}
54static inline EGLSurface getSurface(JNIEnv* env, jobject o) {
55 if (!o) return EGL_NO_SURFACE;
Ashok Bhat863f98b2014-01-27 16:58:46 +000056 return (EGLSurface)env->GetLongField(o, gSurface_EGLSurfaceFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057}
58static inline EGLContext getContext(JNIEnv* env, jobject o) {
59 if (!o) return EGL_NO_CONTEXT;
Ashok Bhat863f98b2014-01-27 16:58:46 +000060 return (EGLContext)env->GetLongField(o, gContext_EGLContextFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061}
62static inline EGLConfig getConfig(JNIEnv* env, jobject o) {
63 if (!o) return 0;
Ashok Bhat863f98b2014-01-27 16:58:46 +000064 return (EGLConfig)env->GetLongField(o, gConfig_EGLConfigFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065}
Ashok Bhat863f98b2014-01-27 16:58:46 +000066
67static inline jboolean EglBoolToJBool(EGLBoolean eglBool) {
68 return eglBool == EGL_TRUE ? JNI_TRUE : JNI_FALSE;
69}
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
72{
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070073 jclass config_class = _env->FindClass("com/google/android/gles_jni/EGLConfigImpl");
74 gConfig_class = (jclass) _env->NewGlobalRef(config_class);
Ashok Bhat863f98b2014-01-27 16:58:46 +000075 gConfig_ctorID = _env->GetMethodID(gConfig_class, "<init>", "(J)V");
76 gConfig_EGLConfigFieldID = _env->GetFieldID(gConfig_class, "mEGLConfig", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070078 jclass display_class = _env->FindClass("com/google/android/gles_jni/EGLDisplayImpl");
Ashok Bhat863f98b2014-01-27 16:58:46 +000079 gDisplay_EGLDisplayFieldID = _env->GetFieldID(display_class, "mEGLDisplay", "J");
Jack Palevich1badb712009-03-25 15:12:17 -070080
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070081 jclass context_class = _env->FindClass("com/google/android/gles_jni/EGLContextImpl");
Ashok Bhat863f98b2014-01-27 16:58:46 +000082 gContext_EGLContextFieldID = _env->GetFieldID(context_class, "mEGLContext", "J");
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070083
84 jclass surface_class = _env->FindClass("com/google/android/gles_jni/EGLSurfaceImpl");
Ashok Bhat863f98b2014-01-27 16:58:46 +000085 gSurface_EGLSurfaceFieldID = _env->GetFieldID(surface_class, "mEGLSurface", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086}
87
Jack Palevich1badb712009-03-25 15:12:17 -070088static const jint gNull_attrib_base[] = {EGL_NONE};
89
90static bool validAttribList(JNIEnv *_env, jintArray attrib_list) {
91 if (attrib_list == NULL) {
92 return true;
93 }
94 jsize len = _env->GetArrayLength(attrib_list);
95 if (len < 1) {
96 return false;
97 }
98 jint item = 0;
99 _env->GetIntArrayRegion(attrib_list, len-1, 1, &item);
100 return item == EGL_NONE;
101}
102
103static jint* beginNativeAttribList(JNIEnv *_env, jintArray attrib_list) {
104 if (attrib_list != NULL) {
Michael Chockb50662452013-08-06 15:21:10 -0700105 return _env->GetIntArrayElements(attrib_list, (jboolean *)0);
Jack Palevich1badb712009-03-25 15:12:17 -0700106 } else {
107 return(jint*) gNull_attrib_base;
108 }
109}
110
111static void endNativeAttributeList(JNIEnv *_env, jintArray attrib_list, jint* attrib_base) {
112 if (attrib_list != NULL) {
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700113 _env->ReleaseIntArrayElements(attrib_list, attrib_base, 0);
Jack Palevich1badb712009-03-25 15:12:17 -0700114 }
115}
116
117static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 jintArray major_minor) {
Jack Palevich1badb712009-03-25 15:12:17 -0700119 if (display == NULL || (major_minor != NULL &&
120 _env->GetArrayLength(major_minor) < 2)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700121 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700122 return JNI_FALSE;
123 }
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000126 EGLBoolean success = eglInitialize(dpy, NULL, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 if (success && major_minor) {
128 int len = _env->GetArrayLength(major_minor);
129 if (len) {
130 // we're exposing only EGL 1.0
131 jint* base = (jint *)_env->GetPrimitiveArrayCritical(major_minor, (jboolean *)0);
132 if (len >= 1) base[0] = 1;
133 if (len >= 2) base[1] = 0;
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700134 _env->ReleasePrimitiveArrayCritical(major_minor, base, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000137 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138}
139
Jack Palevich1badb712009-03-25 15:12:17 -0700140static jboolean jni_eglQueryContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 jobject context, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700142 if (display == NULL || context == NULL || value == NULL
143 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700144 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 return JNI_FALSE;
146 }
Jack Palevich1badb712009-03-25 15:12:17 -0700147 EGLDisplay dpy = getDisplay(_env, display);
148 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000149 EGLBoolean success = EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 int len = _env->GetArrayLength(value);
151 if (len) {
Michael Chockb50662452013-08-06 15:21:10 -0700152 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 success = eglQueryContext(dpy, ctx, attribute, base);
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700154 _env->ReleaseIntArrayElements(value, base, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000156 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157}
Jack Palevich1badb712009-03-25 15:12:17 -0700158
159static jboolean jni_eglQuerySurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 jobject surface, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700161 if (display == NULL || surface == NULL || value == NULL
162 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700163 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 return JNI_FALSE;
165 }
Jack Palevich1badb712009-03-25 15:12:17 -0700166 EGLDisplay dpy = getDisplay(_env, display);
167 EGLContext sur = getSurface(_env, surface);
168
Ashok Bhat863f98b2014-01-27 16:58:46 +0000169 EGLBoolean success = EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 int len = _env->GetArrayLength(value);
171 if (len) {
Michael Chockb50662452013-08-06 15:21:10 -0700172 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 success = eglQuerySurface(dpy, sur, attribute, base);
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700174 _env->ReleaseIntArrayElements(value, base, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000176 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177}
178
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800179static jint jni_getInitCount(JNIEnv *_env, jobject _clazz, jobject display) {
180 EGLDisplay dpy = getDisplay(_env, display);
Mathias Agopian04d04612017-03-08 23:21:54 -0800181 return android::egl_get_init_count(dpy);
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800182}
183
184static jboolean jni_eglReleaseThread(JNIEnv *_env, jobject _this) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000185 return EglBoolToJBool(eglReleaseThread());
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800186}
187
Jack Palevich1badb712009-03-25 15:12:17 -0700188static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
189 jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
190 if (display == NULL
191 || !validAttribList(_env, attrib_list)
192 || (configs != NULL && _env->GetArrayLength(configs) < config_size)
193 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700194 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 return JNI_FALSE;
196 }
Jack Palevich1badb712009-03-25 15:12:17 -0700197 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000198 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700199
200 if (configs == NULL) {
201 config_size = 0;
202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700204
205 int num = 0;
206 jint* attrib_base = beginNativeAttribList(_env, attrib_list);
207 success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
208 endNativeAttributeList(_env, attrib_list, attrib_base);
209
210 if (num_config != NULL) {
211 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
212 }
213
214 if (success && configs!=NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 for (int i=0 ; i<num ; i++) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000216 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 _env->SetObjectArrayElement(configs, i, obj);
218 }
219 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000220 return EglBoolToJBool(success);
Jack Palevich1badb712009-03-25 15:12:17 -0700221}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
Ashok Bhat863f98b2014-01-27 16:58:46 +0000223static jlong jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 jobject config, jobject share_context, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700225 if (display == NULL || config == NULL || share_context == NULL
226 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700227 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700228 return JNI_FALSE;
229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 EGLDisplay dpy = getDisplay(_env, display);
231 EGLConfig cnf = getConfig(_env, config);
232 EGLContext shr = getContext(_env, share_context);
Jack Palevich1badb712009-03-25 15:12:17 -0700233 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700235 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000236 return reinterpret_cast<jlong>(ctx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237}
238
Ashok Bhat863f98b2014-01-27 16:58:46 +0000239static jlong jni_eglCreatePbufferSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 jobject config, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700241 if (display == NULL || config == NULL
242 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700243 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700244 return JNI_FALSE;
245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 EGLDisplay dpy = getDisplay(_env, display);
247 EGLConfig cnf = getConfig(_env, config);
Jack Palevich1badb712009-03-25 15:12:17 -0700248 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 EGLSurface sur = eglCreatePbufferSurface(dpy, cnf, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700250 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000251 return reinterpret_cast<jlong>(sur);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252}
253
Jack Palevich1badb712009-03-25 15:12:17 -0700254static void jni_eglCreatePixmapSurface(JNIEnv *_env, jobject _this, jobject out_sur,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 jobject display, jobject config, jobject native_pixmap,
Jack Palevich1badb712009-03-25 15:12:17 -0700256 jintArray attrib_list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257{
Romain Guy1f071dd2016-11-30 10:20:29 -0800258 jniThrowException(_env, "java/lang/UnsupportedOperationException", "eglCreatePixmapSurface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259}
260
Ashok Bhat863f98b2014-01-27 16:58:46 +0000261static jlong jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 jobject config, jobject native_window, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700263 if (display == NULL || config == NULL
264 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700265 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700266 return JNI_FALSE;
267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 EGLDisplay dpy = getDisplay(_env, display);
269 EGLContext cnf = getConfig(_env, config);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700270 sp<ANativeWindow> window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 if (native_window == NULL) {
272not_valid_surface:
Elliott Hughes8451b252011-04-07 19:17:57 -0700273 jniThrowException(_env, "java/lang/IllegalArgumentException",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
275 return 0;
276 }
Mathias Agopian8b73ae42010-06-10 17:02:51 -0700277
Jeff Brown64a55af2012-08-26 02:47:39 -0700278 window = android_view_Surface_getNativeWindow(_env, native_window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 if (window == NULL)
280 goto not_valid_surface;
281
Jack Palevich1badb712009-03-25 15:12:17 -0700282 jint* base = beginNativeAttribList(_env, attrib_list);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700283 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
Jack Palevich1badb712009-03-25 15:12:17 -0700284 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000285 return reinterpret_cast<jlong>(sur);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286}
287
Ashok Bhat863f98b2014-01-27 16:58:46 +0000288static jlong jni_eglCreateWindowSurfaceTexture(JNIEnv *_env, jobject _this, jobject display,
Romain Guye5e0c502011-06-15 15:18:31 -0700289 jobject config, jobject native_window, jintArray attrib_list) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700290 if (display == NULL || config == NULL
291 || !validAttribList(_env, attrib_list)) {
292 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000293 return 0;
Romain Guy8f0095c2011-05-02 17:24:22 -0700294 }
295 EGLDisplay dpy = getDisplay(_env, display);
296 EGLContext cnf = getConfig(_env, config);
297 sp<ANativeWindow> window;
298 if (native_window == 0) {
299not_valid_surface:
300 jniThrowException(_env, "java/lang/IllegalArgumentException",
301 "Make sure the SurfaceTexture is valid");
302 return 0;
303 }
Jesse Hall0fa257f2013-08-30 13:49:14 -0700304
Mathias Agopian52a9a102013-08-02 01:38:38 -0700305 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(_env, native_window));
Jesse Hall0fa257f2013-08-30 13:49:14 -0700306 window = new Surface(producer, true);
Romain Guy8f0095c2011-05-02 17:24:22 -0700307 if (window == NULL)
308 goto not_valid_surface;
309
310 jint* base = beginNativeAttribList(_env, attrib_list);
311 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
312 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000313 return reinterpret_cast<jlong>(sur);
Romain Guy8f0095c2011-05-02 17:24:22 -0700314}
315
Jack Palevich1badb712009-03-25 15:12:17 -0700316static jboolean jni_eglGetConfigAttrib(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 jobject config, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700318 if (display == NULL || config == NULL
319 || (value == NULL || _env->GetArrayLength(value) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700320 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 return JNI_FALSE;
322 }
Jack Palevich1badb712009-03-25 15:12:17 -0700323 EGLDisplay dpy = getDisplay(_env, display);
324 EGLContext cnf = getConfig(_env, config);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000325 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700326 jint localValue;
327 success = eglGetConfigAttrib(dpy, cnf, attribute, &localValue);
328 if (success) {
329 _env->SetIntArrayRegion(value, 0, 1, &localValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000331 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332}
333
Jack Palevich1badb712009-03-25 15:12:17 -0700334static jboolean jni_eglGetConfigs(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 jobjectArray configs, jint config_size, jintArray num_config) {
Jack Palevich1badb712009-03-25 15:12:17 -0700336 if (display == NULL || (configs != NULL && _env->GetArrayLength(configs) < config_size)
337 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700338 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 return JNI_FALSE;
340 }
Jack Palevich1badb712009-03-25 15:12:17 -0700341 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000342 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700343 if (configs == NULL) {
344 config_size = 0;
345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700347 int num;
348 success = eglGetConfigs(dpy, configs ? nativeConfigs : 0, config_size, &num);
349 if (num_config != NULL) {
350 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 if (success && configs) {
353 for (int i=0 ; i<num ; i++) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000354 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 _env->SetObjectArrayElement(configs, i, obj);
356 }
357 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000358 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359}
Jack Palevich1badb712009-03-25 15:12:17 -0700360
361static jint jni_eglGetError(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 EGLint error = eglGetError();
363 return error;
364}
365
Ashok Bhat863f98b2014-01-27 16:58:46 +0000366static jlong jni_eglGetCurrentContext(JNIEnv *_env, jobject _this) {
367 return reinterpret_cast<jlong>(eglGetCurrentContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368}
369
Ashok Bhat863f98b2014-01-27 16:58:46 +0000370static jlong jni_eglGetCurrentDisplay(JNIEnv *_env, jobject _this) {
371 return reinterpret_cast<jlong>(eglGetCurrentDisplay());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372}
373
Ashok Bhat863f98b2014-01-27 16:58:46 +0000374static jlong jni_eglGetCurrentSurface(JNIEnv *_env, jobject _this, jint readdraw) {
Romain Guy9b7146d2011-03-07 18:05:04 -0800375 if ((readdraw != EGL_READ) && (readdraw != EGL_DRAW)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700376 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700377 return 0;
378 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000379 return reinterpret_cast<jlong>(eglGetCurrentSurface(readdraw));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380}
381
Jack Palevich1badb712009-03-25 15:12:17 -0700382static jboolean jni_eglDestroyContext(JNIEnv *_env, jobject _this, jobject display, jobject context) {
383 if (display == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700384 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700385 return JNI_FALSE;
386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 EGLDisplay dpy = getDisplay(_env, display);
388 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000389 return EglBoolToJBool(eglDestroyContext(dpy, ctx));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390}
391
Jack Palevich1badb712009-03-25 15:12:17 -0700392static jboolean jni_eglDestroySurface(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
393 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700394 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700395 return JNI_FALSE;
396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 EGLDisplay dpy = getDisplay(_env, display);
398 EGLSurface sur = getSurface(_env, surface);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000399 return EglBoolToJBool(eglDestroySurface(dpy, sur));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400}
401
Ashok Bhat863f98b2014-01-27 16:58:46 +0000402static jlong jni_eglGetDisplay(JNIEnv *_env, jobject _this, jobject native_display) {
403 return reinterpret_cast<jlong>(eglGetDisplay(EGL_DEFAULT_DISPLAY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404}
405
Jack Palevich1badb712009-03-25 15:12:17 -0700406static jboolean jni_eglMakeCurrent(JNIEnv *_env, jobject _this, jobject display, jobject draw, jobject read, jobject context) {
407 if (display == NULL || draw == NULL || read == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700408 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700409 return JNI_FALSE;
410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 EGLDisplay dpy = getDisplay(_env, display);
412 EGLSurface sdr = getSurface(_env, draw);
413 EGLSurface srd = getSurface(_env, read);
414 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000415 return EglBoolToJBool(eglMakeCurrent(dpy, sdr, srd, ctx));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416}
417
Jack Palevich1badb712009-03-25 15:12:17 -0700418static jstring jni_eglQueryString(JNIEnv *_env, jobject _this, jobject display, jint name) {
419 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700420 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700421 return NULL;
422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 EGLDisplay dpy = getDisplay(_env, display);
424 const char* chars = eglQueryString(dpy, name);
425 return _env->NewStringUTF(chars);
426}
427
Jack Palevich1badb712009-03-25 15:12:17 -0700428static jboolean jni_eglSwapBuffers(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
429 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700430 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700431 return JNI_FALSE;
432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 EGLDisplay dpy = getDisplay(_env, display);
434 EGLSurface sur = getSurface(_env, surface);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000435 return EglBoolToJBool(eglSwapBuffers(dpy, sur));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436}
437
Jack Palevich1badb712009-03-25 15:12:17 -0700438static jboolean jni_eglTerminate(JNIEnv *_env, jobject _this, jobject display) {
439 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700440 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700441 return JNI_FALSE;
442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000444 return EglBoolToJBool(eglTerminate(dpy));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445}
446
Jack Palevich1badb712009-03-25 15:12:17 -0700447static jboolean jni_eglCopyBuffers(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 jobject surface, jobject native_pixmap) {
Jack Palevich1badb712009-03-25 15:12:17 -0700449 if (display == NULL || surface == NULL || native_pixmap == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700450 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700451 return JNI_FALSE;
452 }
453 // TODO: Implement this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 return JNI_FALSE;
455}
456
Jack Palevich1badb712009-03-25 15:12:17 -0700457static jboolean jni_eglWaitGL(JNIEnv *_env, jobject _this) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000458 return EglBoolToJBool(eglWaitGL());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459}
460
Jack Palevich1badb712009-03-25 15:12:17 -0700461static jboolean jni_eglWaitNative(JNIEnv *_env, jobject _this, jint engine, jobject bindTarget) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000462 return EglBoolToJBool(eglWaitNative(engine));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463}
464
465
466static const char *classPathName = "com/google/android/gles_jni/EGLImpl";
467
468#define DISPLAY "Ljavax/microedition/khronos/egl/EGLDisplay;"
469#define CONTEXT "Ljavax/microedition/khronos/egl/EGLContext;"
470#define CONFIG "Ljavax/microedition/khronos/egl/EGLConfig;"
471#define SURFACE "Ljavax/microedition/khronos/egl/EGLSurface;"
472#define OBJECT "Ljava/lang/Object;"
473#define STRING "Ljava/lang/String;"
474
Daniel Micay76f6a862015-09-19 17:31:01 -0400475static const JNINativeMethod methods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476{"_nativeClassInit","()V", (void*)nativeClassInit },
477{"eglWaitGL", "()Z", (void*)jni_eglWaitGL },
478{"eglInitialize", "(" DISPLAY "[I)Z", (void*)jni_eglInitialize },
479{"eglQueryContext", "(" DISPLAY CONTEXT "I[I)Z", (void*)jni_eglQueryContext },
480{"eglQuerySurface", "(" DISPLAY SURFACE "I[I)Z", (void*)jni_eglQuerySurface },
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800481{"eglReleaseThread","()Z", (void*)jni_eglReleaseThread },
482{"getInitCount", "(" DISPLAY ")I", (void*)jni_getInitCount },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483{"eglChooseConfig", "(" DISPLAY "[I[" CONFIG "I[I)Z", (void*)jni_eglChooseConfig },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000484{"_eglCreateContext","(" DISPLAY CONFIG CONTEXT "[I)J", (void*)jni_eglCreateContext },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485{"eglGetConfigs", "(" DISPLAY "[" CONFIG "I[I)Z", (void*)jni_eglGetConfigs },
486{"eglTerminate", "(" DISPLAY ")Z", (void*)jni_eglTerminate },
487{"eglCopyBuffers", "(" DISPLAY SURFACE OBJECT ")Z", (void*)jni_eglCopyBuffers },
488{"eglWaitNative", "(I" OBJECT ")Z", (void*)jni_eglWaitNative },
489{"eglGetError", "()I", (void*)jni_eglGetError },
490{"eglGetConfigAttrib", "(" DISPLAY CONFIG "I[I)Z", (void*)jni_eglGetConfigAttrib },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000491{"_eglGetDisplay", "(" OBJECT ")J", (void*)jni_eglGetDisplay },
492{"_eglGetCurrentContext", "()J", (void*)jni_eglGetCurrentContext },
493{"_eglGetCurrentDisplay", "()J", (void*)jni_eglGetCurrentDisplay },
494{"_eglGetCurrentSurface", "(I)J", (void*)jni_eglGetCurrentSurface },
495{"_eglCreatePbufferSurface","(" DISPLAY CONFIG "[I)J", (void*)jni_eglCreatePbufferSurface },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496{"_eglCreatePixmapSurface", "(" SURFACE DISPLAY CONFIG OBJECT "[I)V", (void*)jni_eglCreatePixmapSurface },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000497{"_eglCreateWindowSurface", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurface },
498{"_eglCreateWindowSurfaceTexture", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurfaceTexture },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499{"eglDestroyContext", "(" DISPLAY CONTEXT ")Z", (void*)jni_eglDestroyContext },
500{"eglDestroySurface", "(" DISPLAY SURFACE ")Z", (void*)jni_eglDestroySurface },
501{"eglMakeCurrent", "(" DISPLAY SURFACE SURFACE CONTEXT")Z", (void*)jni_eglMakeCurrent },
502{"eglQueryString", "(" DISPLAY "I)" STRING, (void*)jni_eglQueryString },
503{"eglSwapBuffers", "(" DISPLAY SURFACE ")Z", (void*)jni_eglSwapBuffers },
504};
505
506} // namespace android
507
508int register_com_google_android_gles_jni_EGLImpl(JNIEnv *_env)
509{
510 int err;
511 err = android::AndroidRuntime::registerNativeMethods(_env,
512 android::classPathName, android::methods, NELEM(android::methods));
513 return err;
514}