blob: 3d421d5e47f7081e5f1633c5e5f14a62be7d3643 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033#include <SkBitmap.h>
34#include <SkPixelRef.h>
35
Mathias Agopianaf01feaf2012-02-24 18:25:41 -080036#include <ui/ANativeObjectBase.h>
37
Dianne Hackborn54a181b2010-06-30 18:35:14 -070038namespace android {
Mathias Agopian8b73ae42010-06-10 17:02:51 -070039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040static jclass gConfig_class;
41
42static jmethodID gConfig_ctorID;
43
44static jfieldID gDisplay_EGLDisplayFieldID;
45static jfieldID gContext_EGLContextFieldID;
46static jfieldID gSurface_EGLSurfaceFieldID;
47static jfieldID gSurface_NativePixelRefFieldID;
48static jfieldID gConfig_EGLConfigFieldID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049static jfieldID gBitmap_NativeBitmapFieldID;
50
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 jclass bitmap_class = _env->FindClass("android/graphics/Bitmap");
Ashok Bhata0398432014-01-20 20:08:01 +000090 gBitmap_NativeBitmapFieldID = _env->GetFieldID(bitmap_class, "mNativeBitmap", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091}
92
Jack Palevich1badb712009-03-25 15:12:17 -070093static const jint gNull_attrib_base[] = {EGL_NONE};
94
95static bool validAttribList(JNIEnv *_env, jintArray attrib_list) {
96 if (attrib_list == NULL) {
97 return true;
98 }
99 jsize len = _env->GetArrayLength(attrib_list);
100 if (len < 1) {
101 return false;
102 }
103 jint item = 0;
104 _env->GetIntArrayRegion(attrib_list, len-1, 1, &item);
105 return item == EGL_NONE;
106}
107
108static jint* beginNativeAttribList(JNIEnv *_env, jintArray attrib_list) {
109 if (attrib_list != NULL) {
Michael Chockb50662452013-08-06 15:21:10 -0700110 return _env->GetIntArrayElements(attrib_list, (jboolean *)0);
Jack Palevich1badb712009-03-25 15:12:17 -0700111 } else {
112 return(jint*) gNull_attrib_base;
113 }
114}
115
116static void endNativeAttributeList(JNIEnv *_env, jintArray attrib_list, jint* attrib_base) {
117 if (attrib_list != NULL) {
Michael Chockb50662452013-08-06 15:21:10 -0700118 _env->ReleaseIntArrayElements(attrib_list, attrib_base, JNI_ABORT);
Jack Palevich1badb712009-03-25 15:12:17 -0700119 }
120}
121
122static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 jintArray major_minor) {
Jack Palevich1badb712009-03-25 15:12:17 -0700124 if (display == NULL || (major_minor != NULL &&
125 _env->GetArrayLength(major_minor) < 2)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700126 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700127 return JNI_FALSE;
128 }
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000131 EGLBoolean success = eglInitialize(dpy, NULL, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 if (success && major_minor) {
133 int len = _env->GetArrayLength(major_minor);
134 if (len) {
135 // we're exposing only EGL 1.0
136 jint* base = (jint *)_env->GetPrimitiveArrayCritical(major_minor, (jboolean *)0);
137 if (len >= 1) base[0] = 1;
138 if (len >= 2) base[1] = 0;
139 _env->ReleasePrimitiveArrayCritical(major_minor, base, JNI_ABORT);
140 }
141 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000142 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143}
144
Jack Palevich1badb712009-03-25 15:12:17 -0700145static jboolean jni_eglQueryContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 jobject context, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700147 if (display == NULL || context == NULL || value == NULL
148 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700149 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 return JNI_FALSE;
151 }
Jack Palevich1badb712009-03-25 15:12:17 -0700152 EGLDisplay dpy = getDisplay(_env, display);
153 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000154 EGLBoolean success = EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 int len = _env->GetArrayLength(value);
156 if (len) {
Michael Chockb50662452013-08-06 15:21:10 -0700157 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 success = eglQueryContext(dpy, ctx, attribute, base);
Michael Chockb50662452013-08-06 15:21:10 -0700159 _env->ReleaseIntArrayElements(value, base, JNI_ABORT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000161 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162}
Jack Palevich1badb712009-03-25 15:12:17 -0700163
164static jboolean jni_eglQuerySurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 jobject surface, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700166 if (display == NULL || surface == NULL || value == NULL
167 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700168 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 return JNI_FALSE;
170 }
Jack Palevich1badb712009-03-25 15:12:17 -0700171 EGLDisplay dpy = getDisplay(_env, display);
172 EGLContext sur = getSurface(_env, surface);
173
Ashok Bhat863f98b2014-01-27 16:58:46 +0000174 EGLBoolean success = EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 int len = _env->GetArrayLength(value);
176 if (len) {
Michael Chockb50662452013-08-06 15:21:10 -0700177 jint* base = _env->GetIntArrayElements(value, (jboolean *)0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 success = eglQuerySurface(dpy, sur, attribute, base);
Michael Chockb50662452013-08-06 15:21:10 -0700179 _env->ReleaseIntArrayElements(value, base, JNI_ABORT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000181 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182}
183
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800184static jint jni_getInitCount(JNIEnv *_env, jobject _clazz, jobject display) {
185 EGLDisplay dpy = getDisplay(_env, display);
Jesse Hall9847f312012-04-05 10:37:13 -0700186 egl_display_t* eglDisplay = get_display_nowake(dpy);
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800187 return eglDisplay ? eglDisplay->getRefsCount() : 0;
188}
189
190static jboolean jni_eglReleaseThread(JNIEnv *_env, jobject _this) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000191 return EglBoolToJBool(eglReleaseThread());
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800192}
193
Jack Palevich1badb712009-03-25 15:12:17 -0700194static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
195 jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
196 if (display == NULL
197 || !validAttribList(_env, attrib_list)
198 || (configs != NULL && _env->GetArrayLength(configs) < config_size)
199 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700200 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 return JNI_FALSE;
202 }
Jack Palevich1badb712009-03-25 15:12:17 -0700203 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000204 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700205
206 if (configs == NULL) {
207 config_size = 0;
208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700210
211 int num = 0;
212 jint* attrib_base = beginNativeAttribList(_env, attrib_list);
213 success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
214 endNativeAttributeList(_env, attrib_list, attrib_base);
215
216 if (num_config != NULL) {
217 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
218 }
219
220 if (success && configs!=NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 for (int i=0 ; i<num ; i++) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000222 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 _env->SetObjectArrayElement(configs, i, obj);
224 }
225 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000226 return EglBoolToJBool(success);
Jack Palevich1badb712009-03-25 15:12:17 -0700227}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Ashok Bhat863f98b2014-01-27 16:58:46 +0000229static jlong jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 jobject config, jobject share_context, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700231 if (display == NULL || config == NULL || share_context == NULL
232 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700233 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700234 return JNI_FALSE;
235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 EGLDisplay dpy = getDisplay(_env, display);
237 EGLConfig cnf = getConfig(_env, config);
238 EGLContext shr = getContext(_env, share_context);
Jack Palevich1badb712009-03-25 15:12:17 -0700239 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700241 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000242 return reinterpret_cast<jlong>(ctx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243}
244
Ashok Bhat863f98b2014-01-27 16:58:46 +0000245static jlong jni_eglCreatePbufferSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 jobject config, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700247 if (display == NULL || config == NULL
248 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700249 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700250 return JNI_FALSE;
251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 EGLDisplay dpy = getDisplay(_env, display);
253 EGLConfig cnf = getConfig(_env, config);
Jack Palevich1badb712009-03-25 15:12:17 -0700254 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 EGLSurface sur = eglCreatePbufferSurface(dpy, cnf, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700256 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000257 return reinterpret_cast<jlong>(sur);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258}
259
260static PixelFormat convertPixelFormat(SkBitmap::Config format)
261{
262 switch (format) {
263 case SkBitmap::kARGB_8888_Config: return PIXEL_FORMAT_RGBA_8888;
264 case SkBitmap::kARGB_4444_Config: return PIXEL_FORMAT_RGBA_4444;
265 case SkBitmap::kRGB_565_Config: return PIXEL_FORMAT_RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 default: return PIXEL_FORMAT_NONE;
267 }
268}
269
Jack Palevich1badb712009-03-25 15:12:17 -0700270static void jni_eglCreatePixmapSurface(JNIEnv *_env, jobject _this, jobject out_sur,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 jobject display, jobject config, jobject native_pixmap,
Jack Palevich1badb712009-03-25 15:12:17 -0700272 jintArray attrib_list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273{
Jack Palevich1badb712009-03-25 15:12:17 -0700274 if (display == NULL || config == NULL || native_pixmap == NULL
275 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700276 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700277 return;
278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 EGLDisplay dpy = getDisplay(_env, display);
280 EGLConfig cnf = getConfig(_env, config);
281 jint* base = 0;
282
283 SkBitmap const * nativeBitmap =
Ashok Bhata0398432014-01-20 20:08:01 +0000284 (SkBitmap const *)_env->GetLongField(native_pixmap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 gBitmap_NativeBitmapFieldID);
286 SkPixelRef* ref = nativeBitmap ? nativeBitmap->pixelRef() : 0;
287 if (ref == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700288 jniThrowException(_env, "java/lang/IllegalArgumentException", "Bitmap has no PixelRef");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 return;
290 }
Jack Palevich1badb712009-03-25 15:12:17 -0700291
Derek Sollenberger6062c592011-02-22 13:55:04 -0500292 SkSafeRef(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 ref->lockPixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 egl_native_pixmap_t pixmap;
296 pixmap.version = sizeof(pixmap);
297 pixmap.width = nativeBitmap->width();
298 pixmap.height = nativeBitmap->height();
299 pixmap.stride = nativeBitmap->rowBytes() / nativeBitmap->bytesPerPixel();
300 pixmap.format = convertPixelFormat(nativeBitmap->config());
301 pixmap.data = (uint8_t*)ref->pixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700302
303 base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 EGLSurface sur = eglCreatePixmapSurface(dpy, cnf, &pixmap, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700305 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
307 if (sur != EGL_NO_SURFACE) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000308 _env->SetLongField(out_sur, gSurface_EGLSurfaceFieldID, reinterpret_cast<jlong>(sur));
309 _env->SetLongField(out_sur, gSurface_NativePixelRefFieldID, reinterpret_cast<jlong>(ref));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 } else {
311 ref->unlockPixels();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500312 SkSafeUnref(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
314}
315
Ashok Bhat863f98b2014-01-27 16:58:46 +0000316static jlong jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 jobject config, jobject native_window, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700318 if (display == NULL || config == NULL
319 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700320 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700321 return JNI_FALSE;
322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 EGLDisplay dpy = getDisplay(_env, display);
324 EGLContext cnf = getConfig(_env, config);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700325 sp<ANativeWindow> window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 if (native_window == NULL) {
327not_valid_surface:
Elliott Hughes8451b252011-04-07 19:17:57 -0700328 jniThrowException(_env, "java/lang/IllegalArgumentException",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
330 return 0;
331 }
Mathias Agopian8b73ae42010-06-10 17:02:51 -0700332
Jeff Brown64a55af2012-08-26 02:47:39 -0700333 window = android_view_Surface_getNativeWindow(_env, native_window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 if (window == NULL)
335 goto not_valid_surface;
336
Jack Palevich1badb712009-03-25 15:12:17 -0700337 jint* base = beginNativeAttribList(_env, attrib_list);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700338 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
Jack Palevich1badb712009-03-25 15:12:17 -0700339 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000340 return reinterpret_cast<jlong>(sur);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341}
342
Ashok Bhat863f98b2014-01-27 16:58:46 +0000343static jlong jni_eglCreateWindowSurfaceTexture(JNIEnv *_env, jobject _this, jobject display,
Romain Guye5e0c502011-06-15 15:18:31 -0700344 jobject config, jobject native_window, jintArray attrib_list) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700345 if (display == NULL || config == NULL
346 || !validAttribList(_env, attrib_list)) {
347 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000348 return 0;
Romain Guy8f0095c2011-05-02 17:24:22 -0700349 }
350 EGLDisplay dpy = getDisplay(_env, display);
351 EGLContext cnf = getConfig(_env, config);
352 sp<ANativeWindow> window;
353 if (native_window == 0) {
354not_valid_surface:
355 jniThrowException(_env, "java/lang/IllegalArgumentException",
356 "Make sure the SurfaceTexture is valid");
357 return 0;
358 }
Jesse Hall0fa257f2013-08-30 13:49:14 -0700359
Mathias Agopian52a9a102013-08-02 01:38:38 -0700360 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(_env, native_window));
Jesse Hall0fa257f2013-08-30 13:49:14 -0700361 window = new Surface(producer, true);
Romain Guy8f0095c2011-05-02 17:24:22 -0700362 if (window == NULL)
363 goto not_valid_surface;
364
365 jint* base = beginNativeAttribList(_env, attrib_list);
366 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
367 endNativeAttributeList(_env, attrib_list, base);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000368 return reinterpret_cast<jlong>(sur);
Romain Guy8f0095c2011-05-02 17:24:22 -0700369}
370
Jack Palevich1badb712009-03-25 15:12:17 -0700371static jboolean jni_eglGetConfigAttrib(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 jobject config, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700373 if (display == NULL || config == NULL
374 || (value == NULL || _env->GetArrayLength(value) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700375 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 return JNI_FALSE;
377 }
Jack Palevich1badb712009-03-25 15:12:17 -0700378 EGLDisplay dpy = getDisplay(_env, display);
379 EGLContext cnf = getConfig(_env, config);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000380 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700381 jint localValue;
382 success = eglGetConfigAttrib(dpy, cnf, attribute, &localValue);
383 if (success) {
384 _env->SetIntArrayRegion(value, 0, 1, &localValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000386 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387}
388
Jack Palevich1badb712009-03-25 15:12:17 -0700389static jboolean jni_eglGetConfigs(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 jobjectArray configs, jint config_size, jintArray num_config) {
Jack Palevich1badb712009-03-25 15:12:17 -0700391 if (display == NULL || (configs != NULL && _env->GetArrayLength(configs) < config_size)
392 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700393 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 return JNI_FALSE;
395 }
Jack Palevich1badb712009-03-25 15:12:17 -0700396 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000397 EGLBoolean success = EGL_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700398 if (configs == NULL) {
399 config_size = 0;
400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700402 int num;
403 success = eglGetConfigs(dpy, configs ? nativeConfigs : 0, config_size, &num);
404 if (num_config != NULL) {
405 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 if (success && configs) {
408 for (int i=0 ; i<num ; i++) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000409 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, reinterpret_cast<jlong>(nativeConfigs[i]));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 _env->SetObjectArrayElement(configs, i, obj);
411 }
412 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000413 return EglBoolToJBool(success);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414}
Jack Palevich1badb712009-03-25 15:12:17 -0700415
416static jint jni_eglGetError(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 EGLint error = eglGetError();
418 return error;
419}
420
Ashok Bhat863f98b2014-01-27 16:58:46 +0000421static jlong jni_eglGetCurrentContext(JNIEnv *_env, jobject _this) {
422 return reinterpret_cast<jlong>(eglGetCurrentContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423}
424
Ashok Bhat863f98b2014-01-27 16:58:46 +0000425static jlong jni_eglGetCurrentDisplay(JNIEnv *_env, jobject _this) {
426 return reinterpret_cast<jlong>(eglGetCurrentDisplay());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427}
428
Ashok Bhat863f98b2014-01-27 16:58:46 +0000429static jlong jni_eglGetCurrentSurface(JNIEnv *_env, jobject _this, jint readdraw) {
Romain Guy9b7146d2011-03-07 18:05:04 -0800430 if ((readdraw != EGL_READ) && (readdraw != EGL_DRAW)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700431 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700432 return 0;
433 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000434 return reinterpret_cast<jlong>(eglGetCurrentSurface(readdraw));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435}
436
Jack Palevich1badb712009-03-25 15:12:17 -0700437static jboolean jni_eglDestroyContext(JNIEnv *_env, jobject _this, jobject display, jobject context) {
438 if (display == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700439 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700440 return JNI_FALSE;
441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 EGLDisplay dpy = getDisplay(_env, display);
443 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000444 return EglBoolToJBool(eglDestroyContext(dpy, ctx));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445}
446
Jack Palevich1badb712009-03-25 15:12:17 -0700447static jboolean jni_eglDestroySurface(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
448 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700449 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700450 return JNI_FALSE;
451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 EGLDisplay dpy = getDisplay(_env, display);
453 EGLSurface sur = getSurface(_env, surface);
454
455 if (sur) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000456 SkPixelRef* ref = (SkPixelRef*)(_env->GetLongField(surface,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 gSurface_NativePixelRefFieldID));
458 if (ref) {
459 ref->unlockPixels();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500460 SkSafeUnref(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462 }
Ashok Bhat863f98b2014-01-27 16:58:46 +0000463 return EglBoolToJBool(eglDestroySurface(dpy, sur));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464}
465
Ashok Bhat863f98b2014-01-27 16:58:46 +0000466static jlong jni_eglGetDisplay(JNIEnv *_env, jobject _this, jobject native_display) {
467 return reinterpret_cast<jlong>(eglGetDisplay(EGL_DEFAULT_DISPLAY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468}
469
Jack Palevich1badb712009-03-25 15:12:17 -0700470static jboolean jni_eglMakeCurrent(JNIEnv *_env, jobject _this, jobject display, jobject draw, jobject read, jobject context) {
471 if (display == NULL || draw == NULL || read == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700472 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700473 return JNI_FALSE;
474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 EGLDisplay dpy = getDisplay(_env, display);
476 EGLSurface sdr = getSurface(_env, draw);
477 EGLSurface srd = getSurface(_env, read);
478 EGLContext ctx = getContext(_env, context);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000479 return EglBoolToJBool(eglMakeCurrent(dpy, sdr, srd, ctx));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480}
481
Jack Palevich1badb712009-03-25 15:12:17 -0700482static jstring jni_eglQueryString(JNIEnv *_env, jobject _this, jobject display, jint name) {
483 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700484 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700485 return NULL;
486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 EGLDisplay dpy = getDisplay(_env, display);
488 const char* chars = eglQueryString(dpy, name);
489 return _env->NewStringUTF(chars);
490}
491
Jack Palevich1badb712009-03-25 15:12:17 -0700492static jboolean jni_eglSwapBuffers(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
493 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700494 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700495 return JNI_FALSE;
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 EGLDisplay dpy = getDisplay(_env, display);
498 EGLSurface sur = getSurface(_env, surface);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000499 return EglBoolToJBool(eglSwapBuffers(dpy, sur));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500}
501
Jack Palevich1badb712009-03-25 15:12:17 -0700502static jboolean jni_eglTerminate(JNIEnv *_env, jobject _this, jobject display) {
503 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700504 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700505 return JNI_FALSE;
506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 EGLDisplay dpy = getDisplay(_env, display);
Ashok Bhat863f98b2014-01-27 16:58:46 +0000508 return EglBoolToJBool(eglTerminate(dpy));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509}
510
Jack Palevich1badb712009-03-25 15:12:17 -0700511static jboolean jni_eglCopyBuffers(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 jobject surface, jobject native_pixmap) {
Jack Palevich1badb712009-03-25 15:12:17 -0700513 if (display == NULL || surface == NULL || native_pixmap == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700514 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700515 return JNI_FALSE;
516 }
517 // TODO: Implement this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 return JNI_FALSE;
519}
520
Jack Palevich1badb712009-03-25 15:12:17 -0700521static jboolean jni_eglWaitGL(JNIEnv *_env, jobject _this) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000522 return EglBoolToJBool(eglWaitGL());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523}
524
Jack Palevich1badb712009-03-25 15:12:17 -0700525static jboolean jni_eglWaitNative(JNIEnv *_env, jobject _this, jint engine, jobject bindTarget) {
Ashok Bhat863f98b2014-01-27 16:58:46 +0000526 return EglBoolToJBool(eglWaitNative(engine));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527}
528
529
530static const char *classPathName = "com/google/android/gles_jni/EGLImpl";
531
532#define DISPLAY "Ljavax/microedition/khronos/egl/EGLDisplay;"
533#define CONTEXT "Ljavax/microedition/khronos/egl/EGLContext;"
534#define CONFIG "Ljavax/microedition/khronos/egl/EGLConfig;"
535#define SURFACE "Ljavax/microedition/khronos/egl/EGLSurface;"
536#define OBJECT "Ljava/lang/Object;"
537#define STRING "Ljava/lang/String;"
538
539static JNINativeMethod methods[] = {
540{"_nativeClassInit","()V", (void*)nativeClassInit },
541{"eglWaitGL", "()Z", (void*)jni_eglWaitGL },
542{"eglInitialize", "(" DISPLAY "[I)Z", (void*)jni_eglInitialize },
543{"eglQueryContext", "(" DISPLAY CONTEXT "I[I)Z", (void*)jni_eglQueryContext },
544{"eglQuerySurface", "(" DISPLAY SURFACE "I[I)Z", (void*)jni_eglQuerySurface },
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800545{"eglReleaseThread","()Z", (void*)jni_eglReleaseThread },
546{"getInitCount", "(" DISPLAY ")I", (void*)jni_getInitCount },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547{"eglChooseConfig", "(" DISPLAY "[I[" CONFIG "I[I)Z", (void*)jni_eglChooseConfig },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000548{"_eglCreateContext","(" DISPLAY CONFIG CONTEXT "[I)J", (void*)jni_eglCreateContext },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549{"eglGetConfigs", "(" DISPLAY "[" CONFIG "I[I)Z", (void*)jni_eglGetConfigs },
550{"eglTerminate", "(" DISPLAY ")Z", (void*)jni_eglTerminate },
551{"eglCopyBuffers", "(" DISPLAY SURFACE OBJECT ")Z", (void*)jni_eglCopyBuffers },
552{"eglWaitNative", "(I" OBJECT ")Z", (void*)jni_eglWaitNative },
553{"eglGetError", "()I", (void*)jni_eglGetError },
554{"eglGetConfigAttrib", "(" DISPLAY CONFIG "I[I)Z", (void*)jni_eglGetConfigAttrib },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000555{"_eglGetDisplay", "(" OBJECT ")J", (void*)jni_eglGetDisplay },
556{"_eglGetCurrentContext", "()J", (void*)jni_eglGetCurrentContext },
557{"_eglGetCurrentDisplay", "()J", (void*)jni_eglGetCurrentDisplay },
558{"_eglGetCurrentSurface", "(I)J", (void*)jni_eglGetCurrentSurface },
559{"_eglCreatePbufferSurface","(" DISPLAY CONFIG "[I)J", (void*)jni_eglCreatePbufferSurface },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560{"_eglCreatePixmapSurface", "(" SURFACE DISPLAY CONFIG OBJECT "[I)V", (void*)jni_eglCreatePixmapSurface },
Ashok Bhat863f98b2014-01-27 16:58:46 +0000561{"_eglCreateWindowSurface", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurface },
562{"_eglCreateWindowSurfaceTexture", "(" DISPLAY CONFIG OBJECT "[I)J", (void*)jni_eglCreateWindowSurfaceTexture },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563{"eglDestroyContext", "(" DISPLAY CONTEXT ")Z", (void*)jni_eglDestroyContext },
564{"eglDestroySurface", "(" DISPLAY SURFACE ")Z", (void*)jni_eglDestroySurface },
565{"eglMakeCurrent", "(" DISPLAY SURFACE SURFACE CONTEXT")Z", (void*)jni_eglMakeCurrent },
566{"eglQueryString", "(" DISPLAY "I)" STRING, (void*)jni_eglQueryString },
567{"eglSwapBuffers", "(" DISPLAY SURFACE ")Z", (void*)jni_eglSwapBuffers },
568};
569
570} // namespace android
571
572int register_com_google_android_gles_jni_EGLImpl(JNIEnv *_env)
573{
574 int err;
575 err = android::AndroidRuntime::registerNativeMethods(_env,
576 android::classPathName, android::methods, NELEM(android::methods));
577 return err;
578}