blob: 941ed6394714e41cedef95f767fc427bf5edafea [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
18#include <android_runtime/AndroidRuntime.h>
Dianne Hackborn289b9b62010-07-09 11:44:11 -070019#include <android_runtime/android_view_Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <utils/misc.h>
21
22#include <EGL/egl.h>
23#include <GLES/gl.h>
24
Mathias Agopian000479f2010-02-09 17:46:37 -080025#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <SkBitmap.h>
27#include <SkPixelRef.h>
28
Dianne Hackborn54a181b2010-06-30 18:35:14 -070029namespace android {
Mathias Agopian8b73ae42010-06-10 17:02:51 -070030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031static jclass gDisplay_class;
32static jclass gContext_class;
33static jclass gSurface_class;
34static jclass gConfig_class;
35
36static jmethodID gConfig_ctorID;
37
38static jfieldID gDisplay_EGLDisplayFieldID;
39static jfieldID gContext_EGLContextFieldID;
40static jfieldID gSurface_EGLSurfaceFieldID;
41static jfieldID gSurface_NativePixelRefFieldID;
42static jfieldID gConfig_EGLConfigFieldID;
43static jfieldID gSurface_SurfaceFieldID;
44static jfieldID gBitmap_NativeBitmapFieldID;
45
46static __attribute__((noinline))
47void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
48{
49 jclass npeClazz = env->FindClass(exc);
50 env->ThrowNew(npeClazz, msg);
51}
52
53static __attribute__((noinline))
54bool hasException(JNIEnv *env) {
55 if (env->ExceptionCheck() != 0) {
56 env->ExceptionDescribe();
57 return true;
58 }
59 return false;
60}
61
62static __attribute__((noinline))
63jclass make_globalref(JNIEnv* env, const char classname[]) {
64 jclass c = env->FindClass(classname);
65 return (jclass)env->NewGlobalRef(c);
66}
67
68static inline EGLDisplay getDisplay(JNIEnv* env, jobject o) {
69 if (!o) return EGL_NO_DISPLAY;
70 return (EGLDisplay)env->GetIntField(o, gDisplay_EGLDisplayFieldID);
71}
72static inline EGLSurface getSurface(JNIEnv* env, jobject o) {
73 if (!o) return EGL_NO_SURFACE;
74 return (EGLSurface)env->GetIntField(o, gSurface_EGLSurfaceFieldID);
75}
76static inline EGLContext getContext(JNIEnv* env, jobject o) {
77 if (!o) return EGL_NO_CONTEXT;
78 return (EGLContext)env->GetIntField(o, gContext_EGLContextFieldID);
79}
80static inline EGLConfig getConfig(JNIEnv* env, jobject o) {
81 if (!o) return 0;
82 return (EGLConfig)env->GetIntField(o, gConfig_EGLConfigFieldID);
83}
84static 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 Palevich1badb712009-03-25 15:12:17 -070092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 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 Project9066cfe2009-03-03 19:31:44 -080099 jclass bitmap_class = _env->FindClass("android/graphics/Bitmap");
Jack Palevich1badb712009-03-25 15:12:17 -0700100 gBitmap_NativeBitmapFieldID = _env->GetFieldID(bitmap_class, "mNativeBitmap", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101}
102
Jack Palevich1badb712009-03-25 15:12:17 -0700103static const jint gNull_attrib_base[] = {EGL_NONE};
104
105static 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
118static 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
126static 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
132static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 jintArray major_minor) {
Jack Palevich1badb712009-03-25 15:12:17 -0700134 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 Project9066cfe2009-03-03 19:31:44 -0800140 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 Palevich1badb712009-03-25 15:12:17 -0700155static jboolean jni_eglQueryContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 jobject context, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700157 if (display == NULL || context == NULL || value == NULL
158 || _env->GetArrayLength(value) < 1) {
159 doThrow(_env, "java/lang/IllegalArgumentException");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 return JNI_FALSE;
161 }
Jack Palevich1badb712009-03-25 15:12:17 -0700162 EGLDisplay dpy = getDisplay(_env, display);
163 EGLContext ctx = getContext(_env, context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 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 Palevich1badb712009-03-25 15:12:17 -0700173
174static jboolean jni_eglQuerySurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 jobject surface, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700176 if (display == NULL || surface == NULL || value == NULL
177 || _env->GetArrayLength(value) < 1) {
178 doThrow(_env, "java/lang/IllegalArgumentException");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 return JNI_FALSE;
180 }
Jack Palevich1badb712009-03-25 15:12:17 -0700181 EGLDisplay dpy = getDisplay(_env, display);
182 EGLContext sur = getSurface(_env, surface);
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 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 Palevich1badb712009-03-25 15:12:17 -0700194static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
195 jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
196 if (display == NULL
197 || !validAttribList(_env, attrib_list)
198 || (configs != NULL && _env->GetArrayLength(configs) < config_size)
199 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
200 doThrow(_env, "java/lang/IllegalArgumentException");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 return JNI_FALSE;
202 }
Jack Palevich1badb712009-03-25 15:12:17 -0700203 EGLDisplay dpy = getDisplay(_env, display);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 jboolean success = JNI_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700205
206 if (configs == NULL) {
207 config_size = 0;
208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700210
211 int num = 0;
212 jint* attrib_base = beginNativeAttribList(_env, attrib_list);
213 success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
214 endNativeAttributeList(_env, attrib_list, attrib_base);
215
216 if (num_config != NULL) {
217 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
218 }
219
220 if (success && configs!=NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 for (int i=0 ; i<num ; i++) {
222 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, (jint)nativeConfigs[i]);
223 _env->SetObjectArrayElement(configs, i, obj);
224 }
225 }
226 return success;
Jack Palevich1badb712009-03-25 15:12:17 -0700227}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Jack Palevich1badb712009-03-25 15:12:17 -0700229static jint jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 jobject config, jobject share_context, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700231 if (display == NULL || config == NULL || share_context == NULL
232 || !validAttribList(_env, attrib_list)) {
233 doThrow(_env, "java/lang/IllegalArgumentException");
234 return JNI_FALSE;
235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 EGLDisplay dpy = getDisplay(_env, display);
237 EGLConfig cnf = getConfig(_env, config);
238 EGLContext shr = getContext(_env, share_context);
Jack Palevich1badb712009-03-25 15:12:17 -0700239 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700241 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 return (jint)ctx;
243}
244
Jack Palevich1badb712009-03-25 15:12:17 -0700245static jint jni_eglCreatePbufferSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 jobject config, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700247 if (display == NULL || config == NULL
248 || !validAttribList(_env, attrib_list)) {
249 doThrow(_env, "java/lang/IllegalArgumentException");
250 return JNI_FALSE;
251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 EGLDisplay dpy = getDisplay(_env, display);
253 EGLConfig cnf = getConfig(_env, config);
Jack Palevich1badb712009-03-25 15:12:17 -0700254 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 EGLSurface sur = eglCreatePbufferSurface(dpy, cnf, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700256 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 return (jint)sur;
258}
259
260static PixelFormat convertPixelFormat(SkBitmap::Config format)
261{
262 switch (format) {
263 case SkBitmap::kARGB_8888_Config: return PIXEL_FORMAT_RGBA_8888;
264 case SkBitmap::kARGB_4444_Config: return PIXEL_FORMAT_RGBA_4444;
265 case SkBitmap::kRGB_565_Config: return PIXEL_FORMAT_RGB_565;
266 case SkBitmap::kA8_Config: return PIXEL_FORMAT_A_8;
267 default: return PIXEL_FORMAT_NONE;
268 }
269}
270
Jack Palevich1badb712009-03-25 15:12:17 -0700271static void jni_eglCreatePixmapSurface(JNIEnv *_env, jobject _this, jobject out_sur,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 jobject display, jobject config, jobject native_pixmap,
Jack Palevich1badb712009-03-25 15:12:17 -0700273 jintArray attrib_list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274{
Jack Palevich1badb712009-03-25 15:12:17 -0700275 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 Project9066cfe2009-03-03 19:31:44 -0800280 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 Palevich1badb712009-03-25 15:12:17 -0700289 doThrow(_env, "java/lang/IllegalArgumentException", "Bitmap has no PixelRef");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 return;
291 }
Jack Palevich1badb712009-03-25 15:12:17 -0700292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 ref->safeRef();
294 ref->lockPixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 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 Palevich1badb712009-03-25 15:12:17 -0700303
304 base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 EGLSurface sur = eglCreatePixmapSurface(dpy, cnf, &pixmap, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700306 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307
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();
313 ref->safeUnref();
314 }
315}
316
Jack Palevich1badb712009-03-25 15:12:17 -0700317static jint jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 jobject config, jobject native_window, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700319 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 Project9066cfe2009-03-03 19:31:44 -0800324 EGLDisplay dpy = getDisplay(_env, display);
325 EGLContext cnf = getConfig(_env, config);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700326 sp<ANativeWindow> window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 if (native_window == NULL) {
328not_valid_surface:
Jack Palevich1badb712009-03-25 15:12:17 -0700329 doThrow(_env, "java/lang/IllegalArgumentException",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
331 return 0;
332 }
Mathias Agopian8b73ae42010-06-10 17:02:51 -0700333
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700334 window = android_Surface_getNativeWindow(_env, native_window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 if (window == NULL)
336 goto not_valid_surface;
337
Jack Palevich1badb712009-03-25 15:12:17 -0700338 jint* base = beginNativeAttribList(_env, attrib_list);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700339 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
Jack Palevich1badb712009-03-25 15:12:17 -0700340 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 return (jint)sur;
342}
343
Jack Palevich1badb712009-03-25 15:12:17 -0700344static jboolean jni_eglGetConfigAttrib(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 jobject config, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700346 if (display == NULL || config == NULL
347 || (value == NULL || _env->GetArrayLength(value) < 1)) {
348 doThrow(_env, "java/lang/IllegalArgumentException");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 return JNI_FALSE;
350 }
Jack Palevich1badb712009-03-25 15:12:17 -0700351 EGLDisplay dpy = getDisplay(_env, display);
352 EGLContext cnf = getConfig(_env, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 jboolean success = JNI_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700354 jint localValue;
355 success = eglGetConfigAttrib(dpy, cnf, attribute, &localValue);
356 if (success) {
357 _env->SetIntArrayRegion(value, 0, 1, &localValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359 return success;
360}
361
Jack Palevich1badb712009-03-25 15:12:17 -0700362static jboolean jni_eglGetConfigs(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 jobjectArray configs, jint config_size, jintArray num_config) {
Jack Palevich1badb712009-03-25 15:12:17 -0700364 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 Project9066cfe2009-03-03 19:31:44 -0800367 return JNI_FALSE;
368 }
Jack Palevich1badb712009-03-25 15:12:17 -0700369 EGLDisplay dpy = getDisplay(_env, display);
370 jboolean success = JNI_FALSE;
371 if (configs == NULL) {
372 config_size = 0;
373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700375 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 Project9066cfe2009-03-03 19:31:44 -0800380 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 Palevich1badb712009-03-25 15:12:17 -0700388
389static jint jni_eglGetError(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 EGLint error = eglGetError();
391 return error;
392}
393
Jack Palevich1badb712009-03-25 15:12:17 -0700394static jint jni_eglGetCurrentContext(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 return (jint)eglGetCurrentContext();
396}
397
Jack Palevich1badb712009-03-25 15:12:17 -0700398static jint jni_eglGetCurrentDisplay(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 return (jint)eglGetCurrentDisplay();
400}
401
Jack Palevich1badb712009-03-25 15:12:17 -0700402static jint jni_eglGetCurrentSurface(JNIEnv *_env, jobject _this, jint readdraw) {
403 if (!(readdraw == EGL_READ) || (readdraw == EGL_DRAW)) {
404 doThrow(_env, "java/lang/IllegalArgumentException");
405 return 0;
406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 return (jint)eglGetCurrentSurface(readdraw);
408}
409
Jack Palevich1badb712009-03-25 15:12:17 -0700410static 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 Project9066cfe2009-03-03 19:31:44 -0800415 EGLDisplay dpy = getDisplay(_env, display);
416 EGLContext ctx = getContext(_env, context);
417 return eglDestroyContext(dpy, ctx);
418}
419
Jack Palevich1badb712009-03-25 15:12:17 -0700420static 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 Project9066cfe2009-03-03 19:31:44 -0800425 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();
433 ref->safeUnref();
434 }
435 }
436 return eglDestroySurface(dpy, sur);
437}
438
Jack Palevich1badb712009-03-25 15:12:17 -0700439static jint jni_eglGetDisplay(JNIEnv *_env, jobject _this, jobject native_display) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 return (jint)eglGetDisplay(EGL_DEFAULT_DISPLAY);
441}
442
Jack Palevich1badb712009-03-25 15:12:17 -0700443static 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 Project9066cfe2009-03-03 19:31:44 -0800448 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 Palevich1badb712009-03-25 15:12:17 -0700455static 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 Project9066cfe2009-03-03 19:31:44 -0800460 EGLDisplay dpy = getDisplay(_env, display);
461 const char* chars = eglQueryString(dpy, name);
462 return _env->NewStringUTF(chars);
463}
464
Jack Palevich1badb712009-03-25 15:12:17 -0700465static 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 Project9066cfe2009-03-03 19:31:44 -0800470 EGLDisplay dpy = getDisplay(_env, display);
471 EGLSurface sur = getSurface(_env, surface);
472 return eglSwapBuffers(dpy, sur);
473}
474
Jack Palevich1badb712009-03-25 15:12:17 -0700475static 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 Project9066cfe2009-03-03 19:31:44 -0800480 EGLDisplay dpy = getDisplay(_env, display);
481 return eglTerminate(dpy);
482}
483
Jack Palevich1badb712009-03-25 15:12:17 -0700484static jboolean jni_eglCopyBuffers(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 jobject surface, jobject native_pixmap) {
Jack Palevich1badb712009-03-25 15:12:17 -0700486 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 Project9066cfe2009-03-03 19:31:44 -0800491 return JNI_FALSE;
492}
493
Jack Palevich1badb712009-03-25 15:12:17 -0700494static jboolean jni_eglWaitGL(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 return eglWaitGL();
496}
497
Jack Palevich1badb712009-03-25 15:12:17 -0700498static jboolean jni_eglWaitNative(JNIEnv *_env, jobject _this, jint engine, jobject bindTarget) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 return eglWaitNative(engine);
500}
501
502
503static 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
512static 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
542int 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