blob: 7080e2ab5a783d4a0d036205e2aa995767c4670a [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
Elliott Hughes8451b252011-04-07 19:17:57 -070018#include "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
25#include <EGL/egl_display.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <EGL/egl.h>
27#include <GLES/gl.h>
28
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;
48static jfieldID gSurface_NativePixelRefFieldID;
49static jfieldID gConfig_EGLConfigFieldID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051static inline EGLDisplay getDisplay(JNIEnv* env, jobject o) {
52 if (!o) return EGL_NO_DISPLAY;
Ashok Bhat863f98b2014-01-27 16:58:46 +000053 return (EGLDisplay)env->GetLongField(o, gDisplay_EGLDisplayFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054}
55static inline EGLSurface getSurface(JNIEnv* env, jobject o) {
56 if (!o) return EGL_NO_SURFACE;
Ashok Bhat863f98b2014-01-27 16:58:46 +000057 return (EGLSurface)env->GetLongField(o, gSurface_EGLSurfaceFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058}
59static inline EGLContext getContext(JNIEnv* env, jobject o) {
60 if (!o) return EGL_NO_CONTEXT;
Ashok Bhat863f98b2014-01-27 16:58:46 +000061 return (EGLContext)env->GetLongField(o, gContext_EGLContextFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062}
63static inline EGLConfig getConfig(JNIEnv* env, jobject o) {
64 if (!o) return 0;
Ashok Bhat863f98b2014-01-27 16:58:46 +000065 return (EGLConfig)env->GetLongField(o, gConfig_EGLConfigFieldID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066}
Ashok Bhat863f98b2014-01-27 16:58:46 +000067
68static inline jboolean EglBoolToJBool(EGLBoolean eglBool) {
69 return eglBool == EGL_TRUE ? JNI_TRUE : JNI_FALSE;
70}
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
73{
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070074 jclass config_class = _env->FindClass("com/google/android/gles_jni/EGLConfigImpl");
75 gConfig_class = (jclass) _env->NewGlobalRef(config_class);
Ashok Bhat863f98b2014-01-27 16:58:46 +000076 gConfig_ctorID = _env->GetMethodID(gConfig_class, "<init>", "(J)V");
77 gConfig_EGLConfigFieldID = _env->GetFieldID(gConfig_class, "mEGLConfig", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070079 jclass display_class = _env->FindClass("com/google/android/gles_jni/EGLDisplayImpl");
Ashok Bhat863f98b2014-01-27 16:58:46 +000080 gDisplay_EGLDisplayFieldID = _env->GetFieldID(display_class, "mEGLDisplay", "J");
Jack Palevich1badb712009-03-25 15:12:17 -070081
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070082 jclass context_class = _env->FindClass("com/google/android/gles_jni/EGLContextImpl");
Ashok Bhat863f98b2014-01-27 16:58:46 +000083 gContext_EGLContextFieldID = _env->GetFieldID(context_class, "mEGLContext", "J");
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070084
85 jclass surface_class = _env->FindClass("com/google/android/gles_jni/EGLSurfaceImpl");
Ashok Bhat863f98b2014-01-27 16:58:46 +000086 gSurface_EGLSurfaceFieldID = _env->GetFieldID(surface_class, "mEGLSurface", "J");
87 gSurface_NativePixelRefFieldID = _env->GetFieldID(surface_class, "mNativePixelRef", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088}
89
Jack Palevich1badb712009-03-25 15:12:17 -070090static const jint gNull_attrib_base[] = {EGL_NONE};
91
92static bool validAttribList(JNIEnv *_env, jintArray attrib_list) {
93 if (attrib_list == NULL) {
94 return true;
95 }
96 jsize len = _env->GetArrayLength(attrib_list);
97 if (len < 1) {
98 return false;
99 }
100 jint item = 0;
101 _env->GetIntArrayRegion(attrib_list, len-1, 1, &item);
102 return item == EGL_NONE;
103}
104
105static jint* beginNativeAttribList(JNIEnv *_env, jintArray attrib_list) {
106 if (attrib_list != NULL) {
Michael Chockb50662452013-08-06 15:21:10 -0700107 return _env->GetIntArrayElements(attrib_list, (jboolean *)0);
Jack Palevich1badb712009-03-25 15:12:17 -0700108 } else {
109 return(jint*) gNull_attrib_base;
110 }
111}
112
113static void endNativeAttributeList(JNIEnv *_env, jintArray attrib_list, jint* attrib_base) {
114 if (attrib_list != NULL) {
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700115 _env->ReleaseIntArrayElements(attrib_list, attrib_base, 0);
Jack Palevich1badb712009-03-25 15:12:17 -0700116 }
117}
118
119static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 jintArray major_minor) {
Jack Palevich1badb712009-03-25 15:12:17 -0700121 if (display == NULL || (major_minor != NULL &&
122 _env->GetArrayLength(major_minor) < 2)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700123 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700124 return JNI_FALSE;
125 }
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000128 EGLBoolean success = eglInitialize(dpy, NULL, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 if (success && major_minor) {
130 int len = _env->GetArrayLength(major_minor);
131 if (len) {
132 // we're exposing only EGL 1.0
133 jint* base = (jint *)_env->GetPrimitiveArrayCritical(major_minor, (jboolean *)0);
134 if (len >= 1) base[0] = 1;
135 if (len >= 2) base[1] = 0;
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700136 _env->ReleasePrimitiveArrayCritical(major_minor, base, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
138 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000139 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140}
141
Jack Palevich1badb712009-03-25 15:12:17 -0700142static jboolean jni_eglQueryContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 jobject context, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700144 if (display == NULL || context == NULL || value == NULL
145 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700146 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 return JNI_FALSE;
148 }
Jack Palevich1badb712009-03-25 15:12:17 -0700149 EGLDisplay dpy = getDisplay(_env, display);
150 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000151 EGLBoolean success = EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 int len = _env->GetArrayLength(value);
153 if (len) {
Michael Chockb50662452013-08-06 15:21:10 -0700154 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 success = eglQueryContext(dpy, ctx, attribute, base);
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700156 _env->ReleaseIntArrayElements(value, base, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000158 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159}
Jack Palevich1badb712009-03-25 15:12:17 -0700160
161static jboolean jni_eglQuerySurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 jobject surface, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700163 if (display == NULL || surface == NULL || value == NULL
164 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700165 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 return JNI_FALSE;
167 }
Jack Palevich1badb712009-03-25 15:12:17 -0700168 EGLDisplay dpy = getDisplay(_env, display);
169 EGLContext sur = getSurface(_env, surface);
170
Ashok Bhat863f98b2014-01-27 16:58:46 +0000171 EGLBoolean success = EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 int len = _env->GetArrayLength(value);
173 if (len) {
Michael Chockb50662452013-08-06 15:21:10 -0700174 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 success = eglQuerySurface(dpy, sur, attribute, base);
Mathieu Chartier6a5e6df2014-10-06 12:42:00 -0700176 _env->ReleaseIntArrayElements(value, base, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000178 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179}
180
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800181static jint jni_getInitCount(JNIEnv *_env, jobject _clazz, jobject display) {
182 EGLDisplay dpy = getDisplay(_env, display);
Jesse Hall9847f312012-04-05 10:37:13 -0700183 egl_display_t* eglDisplay = get_display_nowake(dpy);
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800184 return eglDisplay ? eglDisplay->getRefsCount() : 0;
185}
186
187static jboolean jni_eglReleaseThread(JNIEnv *_env, jobject _this) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000188 return EglBoolToJBool(eglReleaseThread());
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800189}
190
Jack Palevich1badb712009-03-25 15:12:17 -0700191static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
192 jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
193 if (display == NULL
194 || !validAttribList(_env, attrib_list)
195 || (configs != NULL && _env->GetArrayLength(configs) < config_size)
196 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700197 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 return JNI_FALSE;
199 }
Jack Palevich1badb712009-03-25 15:12:17 -0700200 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000201 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700202
203 if (configs == NULL) {
204 config_size = 0;
205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700207
208 int num = 0;
209 jint* attrib_base = beginNativeAttribList(_env, attrib_list);
210 success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
211 endNativeAttributeList(_env, attrib_list, attrib_base);
212
213 if (num_config != NULL) {
214 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
215 }
216
217 if (success && configs!=NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 for (int i=0 ; i<num ; i++) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000219 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 _env->SetObjectArrayElement(configs, i, obj);
221 }
222 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000223 return EglBoolToJBool(success);
Jack Palevich1badb712009-03-25 15:12:17 -0700224}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
Ashok Bhat863f98b2014-01-27 16:58:46 +0000226static jlong jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 jobject config, jobject share_context, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700228 if (display == NULL || config == NULL || share_context == NULL
229 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700230 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700231 return JNI_FALSE;
232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 EGLDisplay dpy = getDisplay(_env, display);
234 EGLConfig cnf = getConfig(_env, config);
235 EGLContext shr = getContext(_env, share_context);
Jack Palevich1badb712009-03-25 15:12:17 -0700236 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700238 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000239 return reinterpret_cast<jlong>(ctx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240}
241
Ashok Bhat863f98b2014-01-27 16:58:46 +0000242static jlong jni_eglCreatePbufferSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 jobject config, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700244 if (display == NULL || config == NULL
245 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700246 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700247 return JNI_FALSE;
248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 EGLDisplay dpy = getDisplay(_env, display);
250 EGLConfig cnf = getConfig(_env, config);
Jack Palevich1badb712009-03-25 15:12:17 -0700251 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 EGLSurface sur = eglCreatePbufferSurface(dpy, cnf, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700253 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000254 return reinterpret_cast<jlong>(sur);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255}
256
Mike Reed1103b322014-07-08 12:36:44 -0400257static PixelFormat convertPixelFormat(SkColorType format)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258{
259 switch (format) {
Mike Reed1103b322014-07-08 12:36:44 -0400260 case kN32_SkColorType: return PIXEL_FORMAT_RGBA_8888;
261 case kARGB_4444_SkColorType: return PIXEL_FORMAT_RGBA_4444;
262 case kRGB_565_SkColorType: return PIXEL_FORMAT_RGB_565;
263 default: return PIXEL_FORMAT_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265}
266
Jack Palevich1badb712009-03-25 15:12:17 -0700267static void jni_eglCreatePixmapSurface(JNIEnv *_env, jobject _this, jobject out_sur,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 jobject display, jobject config, jobject native_pixmap,
Jack Palevich1badb712009-03-25 15:12:17 -0700269 jintArray attrib_list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270{
Jack Palevich1badb712009-03-25 15:12:17 -0700271 if (display == NULL || config == NULL || native_pixmap == NULL
272 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700273 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700274 return;
275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 EGLDisplay dpy = getDisplay(_env, display);
277 EGLConfig cnf = getConfig(_env, config);
278 jint* base = 0;
279
John Reckedc22fb2015-04-20 22:06:31 +0000280 SkBitmap const * nativeBitmap = GraphicsJNI::getSkBitmap(_env, native_pixmap);
281 SkPixelRef* ref = nativeBitmap ? nativeBitmap->pixelRef() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 if (ref == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700283 jniThrowException(_env, "java/lang/IllegalArgumentException", "Bitmap has no PixelRef");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 return;
285 }
Jack Palevich1badb712009-03-25 15:12:17 -0700286
Derek Sollenberger6062c592011-02-22 13:55:04 -0500287 SkSafeRef(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 ref->lockPixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 egl_native_pixmap_t pixmap;
291 pixmap.version = sizeof(pixmap);
John Reckedc22fb2015-04-20 22:06:31 +0000292 pixmap.width = nativeBitmap->width();
293 pixmap.height = nativeBitmap->height();
294 pixmap.stride = nativeBitmap->rowBytes() / nativeBitmap->bytesPerPixel();
295 pixmap.format = convertPixelFormat(nativeBitmap->colorType());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 pixmap.data = (uint8_t*)ref->pixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700297
298 base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 EGLSurface sur = eglCreatePixmapSurface(dpy, cnf, &pixmap, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700300 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301
302 if (sur != EGL_NO_SURFACE) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000303 _env->SetLongField(out_sur, gSurface_EGLSurfaceFieldID, reinterpret_cast<jlong>(sur));
304 _env->SetLongField(out_sur, gSurface_NativePixelRefFieldID, reinterpret_cast<jlong>(ref));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 } else {
306 ref->unlockPixels();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500307 SkSafeUnref(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 }
309}
310
Ashok Bhat863f98b2014-01-27 16:58:46 +0000311static jlong jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 jobject config, jobject native_window, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700313 if (display == NULL || config == NULL
314 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700315 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700316 return JNI_FALSE;
317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 EGLDisplay dpy = getDisplay(_env, display);
319 EGLContext cnf = getConfig(_env, config);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700320 sp<ANativeWindow> window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 if (native_window == NULL) {
322not_valid_surface:
Elliott Hughes8451b252011-04-07 19:17:57 -0700323 jniThrowException(_env, "java/lang/IllegalArgumentException",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
325 return 0;
326 }
Mathias Agopian8b73ae42010-06-10 17:02:51 -0700327
Jeff Brown64a55af2012-08-26 02:47:39 -0700328 window = android_view_Surface_getNativeWindow(_env, native_window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 if (window == NULL)
330 goto not_valid_surface;
331
Jack Palevich1badb712009-03-25 15:12:17 -0700332 jint* base = beginNativeAttribList(_env, attrib_list);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700333 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
Jack Palevich1badb712009-03-25 15:12:17 -0700334 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000335 return reinterpret_cast<jlong>(sur);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336}
337
Ashok Bhat863f98b2014-01-27 16:58:46 +0000338static jlong jni_eglCreateWindowSurfaceTexture(JNIEnv *_env, jobject _this, jobject display,
Romain Guye5e0c502011-06-15 15:18:31 -0700339 jobject config, jobject native_window, jintArray attrib_list) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700340 if (display == NULL || config == NULL
341 || !validAttribList(_env, attrib_list)) {
342 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000343 return 0;
Romain Guy8f0095c2011-05-02 17:24:22 -0700344 }
345 EGLDisplay dpy = getDisplay(_env, display);
346 EGLContext cnf = getConfig(_env, config);
347 sp<ANativeWindow> window;
348 if (native_window == 0) {
349not_valid_surface:
350 jniThrowException(_env, "java/lang/IllegalArgumentException",
351 "Make sure the SurfaceTexture is valid");
352 return 0;
353 }
Jesse Hall0fa257f2013-08-30 13:49:14 -0700354
Mathias Agopian52a9a102013-08-02 01:38:38 -0700355 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(_env, native_window));
Jesse Hall0fa257f2013-08-30 13:49:14 -0700356 window = new Surface(producer, true);
Romain Guy8f0095c2011-05-02 17:24:22 -0700357 if (window == NULL)
358 goto not_valid_surface;
359
360 jint* base = beginNativeAttribList(_env, attrib_list);
361 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
362 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000363 return reinterpret_cast<jlong>(sur);
Romain Guy8f0095c2011-05-02 17:24:22 -0700364}
365
Jack Palevich1badb712009-03-25 15:12:17 -0700366static jboolean jni_eglGetConfigAttrib(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 jobject config, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700368 if (display == NULL || config == NULL
369 || (value == NULL || _env->GetArrayLength(value) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700370 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 return JNI_FALSE;
372 }
Jack Palevich1badb712009-03-25 15:12:17 -0700373 EGLDisplay dpy = getDisplay(_env, display);
374 EGLContext cnf = getConfig(_env, config);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000375 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700376 jint localValue;
377 success = eglGetConfigAttrib(dpy, cnf, attribute, &localValue);
378 if (success) {
379 _env->SetIntArrayRegion(value, 0, 1, &localValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000381 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382}
383
Jack Palevich1badb712009-03-25 15:12:17 -0700384static jboolean jni_eglGetConfigs(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 jobjectArray configs, jint config_size, jintArray num_config) {
Jack Palevich1badb712009-03-25 15:12:17 -0700386 if (display == NULL || (configs != NULL && _env->GetArrayLength(configs) < config_size)
387 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700388 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 return JNI_FALSE;
390 }
Jack Palevich1badb712009-03-25 15:12:17 -0700391 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000392 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700393 if (configs == NULL) {
394 config_size = 0;
395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700397 int num;
398 success = eglGetConfigs(dpy, configs ? nativeConfigs : 0, config_size, &num);
399 if (num_config != NULL) {
400 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 if (success && configs) {
403 for (int i=0 ; i<num ; i++) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000404 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 _env->SetObjectArrayElement(configs, i, obj);
406 }
407 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000408 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409}
Jack Palevich1badb712009-03-25 15:12:17 -0700410
411static jint jni_eglGetError(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 EGLint error = eglGetError();
413 return error;
414}
415
Ashok Bhat863f98b2014-01-27 16:58:46 +0000416static jlong jni_eglGetCurrentContext(JNIEnv *_env, jobject _this) {
417 return reinterpret_cast<jlong>(eglGetCurrentContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418}
419
Ashok Bhat863f98b2014-01-27 16:58:46 +0000420static jlong jni_eglGetCurrentDisplay(JNIEnv *_env, jobject _this) {
421 return reinterpret_cast<jlong>(eglGetCurrentDisplay());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422}
423
Ashok Bhat863f98b2014-01-27 16:58:46 +0000424static jlong jni_eglGetCurrentSurface(JNIEnv *_env, jobject _this, jint readdraw) {
Romain Guy9b7146d2011-03-07 18:05:04 -0800425 if ((readdraw != EGL_READ) && (readdraw != EGL_DRAW)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700426 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700427 return 0;
428 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000429 return reinterpret_cast<jlong>(eglGetCurrentSurface(readdraw));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430}
431
Jack Palevich1badb712009-03-25 15:12:17 -0700432static jboolean jni_eglDestroyContext(JNIEnv *_env, jobject _this, jobject display, jobject context) {
433 if (display == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700434 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700435 return JNI_FALSE;
436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 EGLDisplay dpy = getDisplay(_env, display);
438 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000439 return EglBoolToJBool(eglDestroyContext(dpy, ctx));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440}
441
Jack Palevich1badb712009-03-25 15:12:17 -0700442static jboolean jni_eglDestroySurface(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
443 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700444 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700445 return JNI_FALSE;
446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 EGLDisplay dpy = getDisplay(_env, display);
448 EGLSurface sur = getSurface(_env, surface);
449
450 if (sur) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000451 SkPixelRef* ref = (SkPixelRef*)(_env->GetLongField(surface,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 gSurface_NativePixelRefFieldID));
453 if (ref) {
454 ref->unlockPixels();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500455 SkSafeUnref(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
457 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000458 return EglBoolToJBool(eglDestroySurface(dpy, sur));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459}
460
Ashok Bhat863f98b2014-01-27 16:58:46 +0000461static jlong jni_eglGetDisplay(JNIEnv *_env, jobject _this, jobject native_display) {
462 return reinterpret_cast<jlong>(eglGetDisplay(EGL_DEFAULT_DISPLAY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463}
464
Jack Palevich1badb712009-03-25 15:12:17 -0700465static jboolean jni_eglMakeCurrent(JNIEnv *_env, jobject _this, jobject display, jobject draw, jobject read, jobject context) {
466 if (display == NULL || draw == NULL || read == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700467 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700468 return JNI_FALSE;
469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 EGLDisplay dpy = getDisplay(_env, display);
471 EGLSurface sdr = getSurface(_env, draw);
472 EGLSurface srd = getSurface(_env, read);
473 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000474 return EglBoolToJBool(eglMakeCurrent(dpy, sdr, srd, ctx));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475}
476
Jack Palevich1badb712009-03-25 15:12:17 -0700477static jstring jni_eglQueryString(JNIEnv *_env, jobject _this, jobject display, jint name) {
478 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700479 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700480 return NULL;
481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 EGLDisplay dpy = getDisplay(_env, display);
483 const char* chars = eglQueryString(dpy, name);
484 return _env->NewStringUTF(chars);
485}
486
Jack Palevich1badb712009-03-25 15:12:17 -0700487static jboolean jni_eglSwapBuffers(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
488 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700489 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700490 return JNI_FALSE;
491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 EGLDisplay dpy = getDisplay(_env, display);
493 EGLSurface sur = getSurface(_env, surface);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000494 return EglBoolToJBool(eglSwapBuffers(dpy, sur));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495}
496
Jack Palevich1badb712009-03-25 15:12:17 -0700497static jboolean jni_eglTerminate(JNIEnv *_env, jobject _this, jobject display) {
498 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700499 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700500 return JNI_FALSE;
501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000503 return EglBoolToJBool(eglTerminate(dpy));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504}
505
Jack Palevich1badb712009-03-25 15:12:17 -0700506static jboolean jni_eglCopyBuffers(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 jobject surface, jobject native_pixmap) {
Jack Palevich1badb712009-03-25 15:12:17 -0700508 if (display == NULL || surface == NULL || native_pixmap == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700509 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700510 return JNI_FALSE;
511 }
512 // TODO: Implement this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 return JNI_FALSE;
514}
515
Jack Palevich1badb712009-03-25 15:12:17 -0700516static jboolean jni_eglWaitGL(JNIEnv *_env, jobject _this) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000517 return EglBoolToJBool(eglWaitGL());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518}
519
Jack Palevich1badb712009-03-25 15:12:17 -0700520static jboolean jni_eglWaitNative(JNIEnv *_env, jobject _this, jint engine, jobject bindTarget) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000521 return EglBoolToJBool(eglWaitNative(engine));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522}
523
524
525static const char *classPathName = "com/google/android/gles_jni/EGLImpl";
526
527#define DISPLAY "Ljavax/microedition/khronos/egl/EGLDisplay;"
528#define CONTEXT "Ljavax/microedition/khronos/egl/EGLContext;"
529#define CONFIG "Ljavax/microedition/khronos/egl/EGLConfig;"
530#define SURFACE "Ljavax/microedition/khronos/egl/EGLSurface;"
531#define OBJECT "Ljava/lang/Object;"
532#define STRING "Ljava/lang/String;"
533
534static JNINativeMethod methods[] = {
535{"_nativeClassInit","()V", (void*)nativeClassInit },
536{"eglWaitGL", "()Z", (void*)jni_eglWaitGL },
537{"eglInitialize", "(" DISPLAY "[I)Z", (void*)jni_eglInitialize },
538{"eglQueryContext", "(" DISPLAY CONTEXT "I[I)Z", (void*)jni_eglQueryContext },
539{"eglQuerySurface", "(" DISPLAY SURFACE "I[I)Z", (void*)jni_eglQuerySurface },
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800540{"eglReleaseThread","()Z", (void*)jni_eglReleaseThread },
541{"getInitCount", "(" DISPLAY ")I", (void*)jni_getInitCount },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542{"eglChooseConfig", "(" DISPLAY "[I[" CONFIG "I[I)Z", (void*)jni_eglChooseConfig },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000543{"_eglCreateContext","(" DISPLAY CONFIG CONTEXT "[I)J", (void*)jni_eglCreateContext },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544{"eglGetConfigs", "(" DISPLAY "[" CONFIG "I[I)Z", (void*)jni_eglGetConfigs },
545{"eglTerminate", "(" DISPLAY ")Z", (void*)jni_eglTerminate },
546{"eglCopyBuffers", "(" DISPLAY SURFACE OBJECT ")Z", (void*)jni_eglCopyBuffers },
547{"eglWaitNative", "(I" OBJECT ")Z", (void*)jni_eglWaitNative },
548{"eglGetError", "()I", (void*)jni_eglGetError },
549{"eglGetConfigAttrib", "(" DISPLAY CONFIG "I[I)Z", (void*)jni_eglGetConfigAttrib },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000550{"_eglGetDisplay", "(" OBJECT ")J", (void*)jni_eglGetDisplay },
551{"_eglGetCurrentContext", "()J", (void*)jni_eglGetCurrentContext },
552{"_eglGetCurrentDisplay", "()J", (void*)jni_eglGetCurrentDisplay },
553{"_eglGetCurrentSurface", "(I)J", (void*)jni_eglGetCurrentSurface },
554{"_eglCreatePbufferSurface","(" DISPLAY CONFIG "[I)J", (void*)jni_eglCreatePbufferSurface },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555{"_eglCreatePixmapSurface", "(" SURFACE DISPLAY CONFIG OBJECT "[I)V", (void*)jni_eglCreatePixmapSurface },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000556{"_eglCreateWindowSurface", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurface },
557{"_eglCreateWindowSurfaceTexture", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurfaceTexture },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558{"eglDestroyContext", "(" DISPLAY CONTEXT ")Z", (void*)jni_eglDestroyContext },
559{"eglDestroySurface", "(" DISPLAY SURFACE ")Z", (void*)jni_eglDestroySurface },
560{"eglMakeCurrent", "(" DISPLAY SURFACE SURFACE CONTEXT")Z", (void*)jni_eglMakeCurrent },
561{"eglQueryString", "(" DISPLAY "I)" STRING, (void*)jni_eglQueryString },
562{"eglSwapBuffers", "(" DISPLAY SURFACE ")Z", (void*)jni_eglSwapBuffers },
563};
564
565} // namespace android
566
567int register_com_google_android_gles_jni_EGLImpl(JNIEnv *_env)
568{
569 int err;
570 err = android::AndroidRuntime::registerNativeMethods(_env,
571 android::classPathName, android::methods, NELEM(android::methods));
572 return err;
573}