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