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