blob: 61efcf26d73d181ea234731e6a7c483eb6bd8d39 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2006, The Android Open Source Project
4**
Jack Palevich1badb712009-03-25 15:12:17 -07005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Jack Palevich1badb712009-03-25 15:12:17 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Jack Palevich1badb712009-03-25 15:12:17 -070011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
Elliott Hughes8451b252011-04-07 19:17:57 -070018#include "JNIHelp.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <android_runtime/AndroidRuntime.h>
Dianne Hackborn289b9b62010-07-09 11:44:11 -070020#include <android_runtime/android_view_Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include <utils/misc.h>
22
23#include <EGL/egl.h>
24#include <GLES/gl.h>
25
Mathias Agopian000479f2010-02-09 17:46:37 -080026#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <SkBitmap.h>
28#include <SkPixelRef.h>
29
Dianne Hackborn54a181b2010-06-30 18:35:14 -070030namespace android {
Mathias Agopian8b73ae42010-06-10 17:02:51 -070031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032static jclass gDisplay_class;
33static jclass gContext_class;
34static jclass gSurface_class;
35static jclass gConfig_class;
36
37static jmethodID gConfig_ctorID;
38
39static jfieldID gDisplay_EGLDisplayFieldID;
40static jfieldID gContext_EGLContextFieldID;
41static jfieldID gSurface_EGLSurfaceFieldID;
42static jfieldID gSurface_NativePixelRefFieldID;
43static jfieldID gConfig_EGLConfigFieldID;
44static jfieldID gSurface_SurfaceFieldID;
45static jfieldID gBitmap_NativeBitmapFieldID;
46
47static __attribute__((noinline))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048bool hasException(JNIEnv *env) {
49 if (env->ExceptionCheck() != 0) {
50 env->ExceptionDescribe();
51 return true;
52 }
53 return false;
54}
55
56static __attribute__((noinline))
57jclass make_globalref(JNIEnv* env, const char classname[]) {
58 jclass c = env->FindClass(classname);
59 return (jclass)env->NewGlobalRef(c);
60}
61
62static inline EGLDisplay getDisplay(JNIEnv* env, jobject o) {
63 if (!o) return EGL_NO_DISPLAY;
64 return (EGLDisplay)env->GetIntField(o, gDisplay_EGLDisplayFieldID);
65}
66static inline EGLSurface getSurface(JNIEnv* env, jobject o) {
67 if (!o) return EGL_NO_SURFACE;
68 return (EGLSurface)env->GetIntField(o, gSurface_EGLSurfaceFieldID);
69}
70static inline EGLContext getContext(JNIEnv* env, jobject o) {
71 if (!o) return EGL_NO_CONTEXT;
72 return (EGLContext)env->GetIntField(o, gContext_EGLContextFieldID);
73}
74static inline EGLConfig getConfig(JNIEnv* env, jobject o) {
75 if (!o) return 0;
76 return (EGLConfig)env->GetIntField(o, gConfig_EGLConfigFieldID);
77}
78static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
79{
80 gDisplay_class = make_globalref(_env, "com/google/android/gles_jni/EGLDisplayImpl");
81 gContext_class = make_globalref(_env, "com/google/android/gles_jni/EGLContextImpl");
82 gSurface_class = make_globalref(_env, "com/google/android/gles_jni/EGLSurfaceImpl");
83 gConfig_class = make_globalref(_env, "com/google/android/gles_jni/EGLConfigImpl");
84
85 gConfig_ctorID = _env->GetMethodID(gConfig_class, "<init>", "(I)V");
Jack Palevich1badb712009-03-25 15:12:17 -070086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 gDisplay_EGLDisplayFieldID = _env->GetFieldID(gDisplay_class, "mEGLDisplay", "I");
88 gContext_EGLContextFieldID = _env->GetFieldID(gContext_class, "mEGLContext", "I");
89 gSurface_EGLSurfaceFieldID = _env->GetFieldID(gSurface_class, "mEGLSurface", "I");
90 gSurface_NativePixelRefFieldID = _env->GetFieldID(gSurface_class, "mNativePixelRef", "I");
91 gConfig_EGLConfigFieldID = _env->GetFieldID(gConfig_class, "mEGLConfig", "I");
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 jclass bitmap_class = _env->FindClass("android/graphics/Bitmap");
Jack Palevich1badb712009-03-25 15:12:17 -070094 gBitmap_NativeBitmapFieldID = _env->GetFieldID(bitmap_class, "mNativeBitmap", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095}
96
Jack Palevich1badb712009-03-25 15:12:17 -070097static const jint gNull_attrib_base[] = {EGL_NONE};
98
99static bool validAttribList(JNIEnv *_env, jintArray attrib_list) {
100 if (attrib_list == NULL) {
101 return true;
102 }
103 jsize len = _env->GetArrayLength(attrib_list);
104 if (len < 1) {
105 return false;
106 }
107 jint item = 0;
108 _env->GetIntArrayRegion(attrib_list, len-1, 1, &item);
109 return item == EGL_NONE;
110}
111
112static jint* beginNativeAttribList(JNIEnv *_env, jintArray attrib_list) {
113 if (attrib_list != NULL) {
114 return (jint *)_env->GetPrimitiveArrayCritical(attrib_list, (jboolean *)0);
115 } else {
116 return(jint*) gNull_attrib_base;
117 }
118}
119
120static void endNativeAttributeList(JNIEnv *_env, jintArray attrib_list, jint* attrib_base) {
121 if (attrib_list != NULL) {
122 _env->ReleasePrimitiveArrayCritical(attrib_list, attrib_base, JNI_ABORT);
123 }
124}
125
126static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 jintArray major_minor) {
Jack Palevich1badb712009-03-25 15:12:17 -0700128 if (display == NULL || (major_minor != NULL &&
129 _env->GetArrayLength(major_minor) < 2)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700130 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700131 return JNI_FALSE;
132 }
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 EGLDisplay dpy = getDisplay(_env, display);
135 jboolean success = eglInitialize(dpy, NULL, NULL);
136 if (success && major_minor) {
137 int len = _env->GetArrayLength(major_minor);
138 if (len) {
139 // we're exposing only EGL 1.0
140 jint* base = (jint *)_env->GetPrimitiveArrayCritical(major_minor, (jboolean *)0);
141 if (len >= 1) base[0] = 1;
142 if (len >= 2) base[1] = 0;
143 _env->ReleasePrimitiveArrayCritical(major_minor, base, JNI_ABORT);
144 }
145 }
146 return success;
147}
148
Jack Palevich1badb712009-03-25 15:12:17 -0700149static jboolean jni_eglQueryContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 jobject context, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700151 if (display == NULL || context == NULL || value == NULL
152 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700153 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 return JNI_FALSE;
155 }
Jack Palevich1badb712009-03-25 15:12:17 -0700156 EGLDisplay dpy = getDisplay(_env, display);
157 EGLContext ctx = getContext(_env, context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 jboolean success = JNI_FALSE;
159 int len = _env->GetArrayLength(value);
160 if (len) {
161 jint* base = (jint *)_env->GetPrimitiveArrayCritical(value, (jboolean *)0);
162 success = eglQueryContext(dpy, ctx, attribute, base);
163 _env->ReleasePrimitiveArrayCritical(value, base, JNI_ABORT);
164 }
165 return success;
166}
Jack Palevich1badb712009-03-25 15:12:17 -0700167
168static jboolean jni_eglQuerySurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 jobject surface, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700170 if (display == NULL || surface == NULL || value == NULL
171 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700172 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 return JNI_FALSE;
174 }
Jack Palevich1badb712009-03-25 15:12:17 -0700175 EGLDisplay dpy = getDisplay(_env, display);
176 EGLContext sur = getSurface(_env, surface);
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 jboolean success = JNI_FALSE;
179 int len = _env->GetArrayLength(value);
180 if (len) {
181 jint* base = (jint *)_env->GetPrimitiveArrayCritical(value, (jboolean *)0);
182 success = eglQuerySurface(dpy, sur, attribute, base);
183 _env->ReleasePrimitiveArrayCritical(value, base, JNI_ABORT);
184 }
185 return success;
186}
187
Jack Palevich1badb712009-03-25 15:12:17 -0700188static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
189 jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
190 if (display == NULL
191 || !validAttribList(_env, attrib_list)
192 || (configs != NULL && _env->GetArrayLength(configs) < config_size)
193 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700194 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 return JNI_FALSE;
196 }
Jack Palevich1badb712009-03-25 15:12:17 -0700197 EGLDisplay dpy = getDisplay(_env, display);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 jboolean success = JNI_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700199
200 if (configs == NULL) {
201 config_size = 0;
202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700204
205 int num = 0;
206 jint* attrib_base = beginNativeAttribList(_env, attrib_list);
207 success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
208 endNativeAttributeList(_env, attrib_list, attrib_base);
209
210 if (num_config != NULL) {
211 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
212 }
213
214 if (success && configs!=NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 for (int i=0 ; i<num ; i++) {
216 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, (jint)nativeConfigs[i]);
217 _env->SetObjectArrayElement(configs, i, obj);
218 }
219 }
220 return success;
Jack Palevich1badb712009-03-25 15:12:17 -0700221}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
Jack Palevich1badb712009-03-25 15:12:17 -0700223static jint jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 jobject config, jobject share_context, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700225 if (display == NULL || config == NULL || share_context == NULL
226 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700227 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700228 return JNI_FALSE;
229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 EGLDisplay dpy = getDisplay(_env, display);
231 EGLConfig cnf = getConfig(_env, config);
232 EGLContext shr = getContext(_env, share_context);
Jack Palevich1badb712009-03-25 15:12:17 -0700233 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700235 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 return (jint)ctx;
237}
238
Jack Palevich1badb712009-03-25 15:12:17 -0700239static jint jni_eglCreatePbufferSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 jobject config, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700241 if (display == NULL || config == NULL
242 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700243 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700244 return JNI_FALSE;
245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 EGLDisplay dpy = getDisplay(_env, display);
247 EGLConfig cnf = getConfig(_env, config);
Jack Palevich1badb712009-03-25 15:12:17 -0700248 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 EGLSurface sur = eglCreatePbufferSurface(dpy, cnf, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700250 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 return (jint)sur;
252}
253
254static PixelFormat convertPixelFormat(SkBitmap::Config format)
255{
256 switch (format) {
257 case SkBitmap::kARGB_8888_Config: return PIXEL_FORMAT_RGBA_8888;
258 case SkBitmap::kARGB_4444_Config: return PIXEL_FORMAT_RGBA_4444;
259 case SkBitmap::kRGB_565_Config: return PIXEL_FORMAT_RGB_565;
260 case SkBitmap::kA8_Config: return PIXEL_FORMAT_A_8;
261 default: return PIXEL_FORMAT_NONE;
262 }
263}
264
Jack Palevich1badb712009-03-25 15:12:17 -0700265static void jni_eglCreatePixmapSurface(JNIEnv *_env, jobject _this, jobject out_sur,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 jobject display, jobject config, jobject native_pixmap,
Jack Palevich1badb712009-03-25 15:12:17 -0700267 jintArray attrib_list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268{
Jack Palevich1badb712009-03-25 15:12:17 -0700269 if (display == NULL || config == NULL || native_pixmap == NULL
270 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700271 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700272 return;
273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 EGLDisplay dpy = getDisplay(_env, display);
275 EGLConfig cnf = getConfig(_env, config);
276 jint* base = 0;
277
278 SkBitmap const * nativeBitmap =
279 (SkBitmap const *)_env->GetIntField(native_pixmap,
280 gBitmap_NativeBitmapFieldID);
281 SkPixelRef* ref = nativeBitmap ? nativeBitmap->pixelRef() : 0;
282 if (ref == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700283 jniThrowException(_env, "java/lang/IllegalArgumentException", "Bitmap has no PixelRef");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 return;
285 }
Jack Palevich1badb712009-03-25 15:12:17 -0700286
Derek Sollenberger6062c592011-02-22 13:55:04 -0500287 SkSafeRef(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 ref->lockPixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 egl_native_pixmap_t pixmap;
291 pixmap.version = sizeof(pixmap);
292 pixmap.width = nativeBitmap->width();
293 pixmap.height = nativeBitmap->height();
294 pixmap.stride = nativeBitmap->rowBytes() / nativeBitmap->bytesPerPixel();
295 pixmap.format = convertPixelFormat(nativeBitmap->config());
296 pixmap.data = (uint8_t*)ref->pixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700297
298 base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 EGLSurface sur = eglCreatePixmapSurface(dpy, cnf, &pixmap, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700300 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301
302 if (sur != EGL_NO_SURFACE) {
303 _env->SetIntField(out_sur, gSurface_EGLSurfaceFieldID, (int)sur);
304 _env->SetIntField(out_sur, gSurface_NativePixelRefFieldID, (int)ref);
305 } else {
306 ref->unlockPixels();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500307 SkSafeUnref(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 }
309}
310
Jack Palevich1badb712009-03-25 15:12:17 -0700311static jint jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 jobject config, jobject native_window, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700313 if (display == NULL || config == NULL
314 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700315 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700316 return JNI_FALSE;
317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 EGLDisplay dpy = getDisplay(_env, display);
319 EGLContext cnf = getConfig(_env, config);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700320 sp<ANativeWindow> window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 if (native_window == NULL) {
322not_valid_surface:
Elliott Hughes8451b252011-04-07 19:17:57 -0700323 jniThrowException(_env, "java/lang/IllegalArgumentException",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
325 return 0;
326 }
Mathias Agopian8b73ae42010-06-10 17:02:51 -0700327
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700328 window = android_Surface_getNativeWindow(_env, native_window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 if (window == NULL)
330 goto not_valid_surface;
331
Jack Palevich1badb712009-03-25 15:12:17 -0700332 jint* base = beginNativeAttribList(_env, attrib_list);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700333 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
Jack Palevich1badb712009-03-25 15:12:17 -0700334 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 return (jint)sur;
336}
337
Jack Palevich1badb712009-03-25 15:12:17 -0700338static jboolean jni_eglGetConfigAttrib(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 jobject config, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700340 if (display == NULL || config == NULL
341 || (value == NULL || _env->GetArrayLength(value) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700342 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 return JNI_FALSE;
344 }
Jack Palevich1badb712009-03-25 15:12:17 -0700345 EGLDisplay dpy = getDisplay(_env, display);
346 EGLContext cnf = getConfig(_env, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 jboolean success = JNI_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700348 jint localValue;
349 success = eglGetConfigAttrib(dpy, cnf, attribute, &localValue);
350 if (success) {
351 _env->SetIntArrayRegion(value, 0, 1, &localValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353 return success;
354}
355
Jack Palevich1badb712009-03-25 15:12:17 -0700356static jboolean jni_eglGetConfigs(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 jobjectArray configs, jint config_size, jintArray num_config) {
Jack Palevich1badb712009-03-25 15:12:17 -0700358 if (display == NULL || (configs != NULL && _env->GetArrayLength(configs) < config_size)
359 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700360 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 return JNI_FALSE;
362 }
Jack Palevich1badb712009-03-25 15:12:17 -0700363 EGLDisplay dpy = getDisplay(_env, display);
364 jboolean success = JNI_FALSE;
365 if (configs == NULL) {
366 config_size = 0;
367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700369 int num;
370 success = eglGetConfigs(dpy, configs ? nativeConfigs : 0, config_size, &num);
371 if (num_config != NULL) {
372 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 if (success && configs) {
375 for (int i=0 ; i<num ; i++) {
376 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, (jint)nativeConfigs[i]);
377 _env->SetObjectArrayElement(configs, i, obj);
378 }
379 }
380 return success;
381}
Jack Palevich1badb712009-03-25 15:12:17 -0700382
383static jint jni_eglGetError(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 EGLint error = eglGetError();
385 return error;
386}
387
Jack Palevich1badb712009-03-25 15:12:17 -0700388static jint jni_eglGetCurrentContext(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 return (jint)eglGetCurrentContext();
390}
391
Jack Palevich1badb712009-03-25 15:12:17 -0700392static jint jni_eglGetCurrentDisplay(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 return (jint)eglGetCurrentDisplay();
394}
395
Jack Palevich1badb712009-03-25 15:12:17 -0700396static jint jni_eglGetCurrentSurface(JNIEnv *_env, jobject _this, jint readdraw) {
Romain Guy9b7146d2011-03-07 18:05:04 -0800397 if ((readdraw != EGL_READ) && (readdraw != EGL_DRAW)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700398 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700399 return 0;
400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 return (jint)eglGetCurrentSurface(readdraw);
402}
403
Jack Palevich1badb712009-03-25 15:12:17 -0700404static jboolean jni_eglDestroyContext(JNIEnv *_env, jobject _this, jobject display, jobject context) {
405 if (display == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700406 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700407 return JNI_FALSE;
408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 EGLDisplay dpy = getDisplay(_env, display);
410 EGLContext ctx = getContext(_env, context);
411 return eglDestroyContext(dpy, ctx);
412}
413
Jack Palevich1badb712009-03-25 15:12:17 -0700414static jboolean jni_eglDestroySurface(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
415 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700416 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700417 return JNI_FALSE;
418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 EGLDisplay dpy = getDisplay(_env, display);
420 EGLSurface sur = getSurface(_env, surface);
421
422 if (sur) {
423 SkPixelRef* ref = (SkPixelRef*)(_env->GetIntField(surface,
424 gSurface_NativePixelRefFieldID));
425 if (ref) {
426 ref->unlockPixels();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500427 SkSafeUnref(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429 }
430 return eglDestroySurface(dpy, sur);
431}
432
Jack Palevich1badb712009-03-25 15:12:17 -0700433static jint jni_eglGetDisplay(JNIEnv *_env, jobject _this, jobject native_display) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 return (jint)eglGetDisplay(EGL_DEFAULT_DISPLAY);
435}
436
Jack Palevich1badb712009-03-25 15:12:17 -0700437static jboolean jni_eglMakeCurrent(JNIEnv *_env, jobject _this, jobject display, jobject draw, jobject read, jobject context) {
438 if (display == NULL || draw == NULL || read == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700439 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700440 return JNI_FALSE;
441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 EGLDisplay dpy = getDisplay(_env, display);
443 EGLSurface sdr = getSurface(_env, draw);
444 EGLSurface srd = getSurface(_env, read);
445 EGLContext ctx = getContext(_env, context);
446 return eglMakeCurrent(dpy, sdr, srd, ctx);
447}
448
Jack Palevich1badb712009-03-25 15:12:17 -0700449static jstring jni_eglQueryString(JNIEnv *_env, jobject _this, jobject display, jint name) {
450 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700451 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700452 return NULL;
453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 EGLDisplay dpy = getDisplay(_env, display);
455 const char* chars = eglQueryString(dpy, name);
456 return _env->NewStringUTF(chars);
457}
458
Jack Palevich1badb712009-03-25 15:12:17 -0700459static jboolean jni_eglSwapBuffers(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
460 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700461 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700462 return JNI_FALSE;
463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 EGLDisplay dpy = getDisplay(_env, display);
465 EGLSurface sur = getSurface(_env, surface);
466 return eglSwapBuffers(dpy, sur);
467}
468
Jack Palevich1badb712009-03-25 15:12:17 -0700469static jboolean jni_eglTerminate(JNIEnv *_env, jobject _this, jobject display) {
470 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700471 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700472 return JNI_FALSE;
473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 EGLDisplay dpy = getDisplay(_env, display);
475 return eglTerminate(dpy);
476}
477
Jack Palevich1badb712009-03-25 15:12:17 -0700478static jboolean jni_eglCopyBuffers(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 jobject surface, jobject native_pixmap) {
Jack Palevich1badb712009-03-25 15:12:17 -0700480 if (display == NULL || surface == NULL || native_pixmap == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700481 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700482 return JNI_FALSE;
483 }
484 // TODO: Implement this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 return JNI_FALSE;
486}
487
Jack Palevich1badb712009-03-25 15:12:17 -0700488static jboolean jni_eglWaitGL(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 return eglWaitGL();
490}
491
Jack Palevich1badb712009-03-25 15:12:17 -0700492static jboolean jni_eglWaitNative(JNIEnv *_env, jobject _this, jint engine, jobject bindTarget) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 return eglWaitNative(engine);
494}
495
496
497static const char *classPathName = "com/google/android/gles_jni/EGLImpl";
498
499#define DISPLAY "Ljavax/microedition/khronos/egl/EGLDisplay;"
500#define CONTEXT "Ljavax/microedition/khronos/egl/EGLContext;"
501#define CONFIG "Ljavax/microedition/khronos/egl/EGLConfig;"
502#define SURFACE "Ljavax/microedition/khronos/egl/EGLSurface;"
503#define OBJECT "Ljava/lang/Object;"
504#define STRING "Ljava/lang/String;"
505
506static JNINativeMethod methods[] = {
507{"_nativeClassInit","()V", (void*)nativeClassInit },
508{"eglWaitGL", "()Z", (void*)jni_eglWaitGL },
509{"eglInitialize", "(" DISPLAY "[I)Z", (void*)jni_eglInitialize },
510{"eglQueryContext", "(" DISPLAY CONTEXT "I[I)Z", (void*)jni_eglQueryContext },
511{"eglQuerySurface", "(" DISPLAY SURFACE "I[I)Z", (void*)jni_eglQuerySurface },
512{"eglChooseConfig", "(" DISPLAY "[I[" CONFIG "I[I)Z", (void*)jni_eglChooseConfig },
513{"_eglCreateContext","(" DISPLAY CONFIG CONTEXT "[I)I", (void*)jni_eglCreateContext },
514{"eglGetConfigs", "(" DISPLAY "[" CONFIG "I[I)Z", (void*)jni_eglGetConfigs },
515{"eglTerminate", "(" DISPLAY ")Z", (void*)jni_eglTerminate },
516{"eglCopyBuffers", "(" DISPLAY SURFACE OBJECT ")Z", (void*)jni_eglCopyBuffers },
517{"eglWaitNative", "(I" OBJECT ")Z", (void*)jni_eglWaitNative },
518{"eglGetError", "()I", (void*)jni_eglGetError },
519{"eglGetConfigAttrib", "(" DISPLAY CONFIG "I[I)Z", (void*)jni_eglGetConfigAttrib },
520{"_eglGetDisplay", "(" OBJECT ")I", (void*)jni_eglGetDisplay },
521{"_eglGetCurrentContext", "()I", (void*)jni_eglGetCurrentContext },
522{"_eglGetCurrentDisplay", "()I", (void*)jni_eglGetCurrentDisplay },
523{"_eglGetCurrentSurface", "(I)I", (void*)jni_eglGetCurrentSurface },
524{"_eglCreatePbufferSurface","(" DISPLAY CONFIG "[I)I", (void*)jni_eglCreatePbufferSurface },
525{"_eglCreatePixmapSurface", "(" SURFACE DISPLAY CONFIG OBJECT "[I)V", (void*)jni_eglCreatePixmapSurface },
526{"_eglCreateWindowSurface", "(" DISPLAY CONFIG OBJECT "[I)I", (void*)jni_eglCreateWindowSurface },
527{"eglDestroyContext", "(" DISPLAY CONTEXT ")Z", (void*)jni_eglDestroyContext },
528{"eglDestroySurface", "(" DISPLAY SURFACE ")Z", (void*)jni_eglDestroySurface },
529{"eglMakeCurrent", "(" DISPLAY SURFACE SURFACE CONTEXT")Z", (void*)jni_eglMakeCurrent },
530{"eglQueryString", "(" DISPLAY "I)" STRING, (void*)jni_eglQueryString },
531{"eglSwapBuffers", "(" DISPLAY SURFACE ")Z", (void*)jni_eglSwapBuffers },
532};
533
534} // namespace android
535
536int register_com_google_android_gles_jni_EGLImpl(JNIEnv *_env)
537{
538 int err;
539 err = android::AndroidRuntime::registerNativeMethods(_env,
540 android::classPathName, android::methods, NELEM(android::methods));
541 return err;
542}