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