blob: d5cde48b853828be6bb39da6d1be9d2ec91dab76 [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>
19#include <utils/misc.h>
20
21#include <EGL/egl.h>
22#include <GLES/gl.h>
23
Mathias Agopian000479f2010-02-09 17:46:37 -080024#include <surfaceflinger/Surface.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <SkBitmap.h>
26#include <SkPixelRef.h>
27
28namespace android {
29
Mathias Agopian8b73ae42010-06-10 17:02:51 -070030extern EGLNativeWindowType android_Surface_getEGLNativeWindow(
31 JNIEnv* env, jobject clazz);
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033static jclass gDisplay_class;
34static jclass gContext_class;
35static jclass gSurface_class;
36static jclass gConfig_class;
37
38static jmethodID gConfig_ctorID;
39
40static jfieldID gDisplay_EGLDisplayFieldID;
41static jfieldID gContext_EGLContextFieldID;
42static jfieldID gSurface_EGLSurfaceFieldID;
43static jfieldID gSurface_NativePixelRefFieldID;
44static jfieldID gConfig_EGLConfigFieldID;
45static jfieldID gSurface_SurfaceFieldID;
46static jfieldID gBitmap_NativeBitmapFieldID;
47
48static __attribute__((noinline))
49void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
50{
51 jclass npeClazz = env->FindClass(exc);
52 env->ThrowNew(npeClazz, msg);
53}
54
55static __attribute__((noinline))
56bool hasException(JNIEnv *env) {
57 if (env->ExceptionCheck() != 0) {
58 env->ExceptionDescribe();
59 return true;
60 }
61 return false;
62}
63
64static __attribute__((noinline))
65jclass make_globalref(JNIEnv* env, const char classname[]) {
66 jclass c = env->FindClass(classname);
67 return (jclass)env->NewGlobalRef(c);
68}
69
70static inline EGLDisplay getDisplay(JNIEnv* env, jobject o) {
71 if (!o) return EGL_NO_DISPLAY;
72 return (EGLDisplay)env->GetIntField(o, gDisplay_EGLDisplayFieldID);
73}
74static inline EGLSurface getSurface(JNIEnv* env, jobject o) {
75 if (!o) return EGL_NO_SURFACE;
76 return (EGLSurface)env->GetIntField(o, gSurface_EGLSurfaceFieldID);
77}
78static inline EGLContext getContext(JNIEnv* env, jobject o) {
79 if (!o) return EGL_NO_CONTEXT;
80 return (EGLContext)env->GetIntField(o, gContext_EGLContextFieldID);
81}
82static inline EGLConfig getConfig(JNIEnv* env, jobject o) {
83 if (!o) return 0;
84 return (EGLConfig)env->GetIntField(o, gConfig_EGLConfigFieldID);
85}
86static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
87{
88 gDisplay_class = make_globalref(_env, "com/google/android/gles_jni/EGLDisplayImpl");
89 gContext_class = make_globalref(_env, "com/google/android/gles_jni/EGLContextImpl");
90 gSurface_class = make_globalref(_env, "com/google/android/gles_jni/EGLSurfaceImpl");
91 gConfig_class = make_globalref(_env, "com/google/android/gles_jni/EGLConfigImpl");
92
93 gConfig_ctorID = _env->GetMethodID(gConfig_class, "<init>", "(I)V");
Jack Palevich1badb712009-03-25 15:12:17 -070094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 gDisplay_EGLDisplayFieldID = _env->GetFieldID(gDisplay_class, "mEGLDisplay", "I");
96 gContext_EGLContextFieldID = _env->GetFieldID(gContext_class, "mEGLContext", "I");
97 gSurface_EGLSurfaceFieldID = _env->GetFieldID(gSurface_class, "mEGLSurface", "I");
98 gSurface_NativePixelRefFieldID = _env->GetFieldID(gSurface_class, "mNativePixelRef", "I");
99 gConfig_EGLConfigFieldID = _env->GetFieldID(gConfig_class, "mEGLConfig", "I");
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 jclass bitmap_class = _env->FindClass("android/graphics/Bitmap");
Jack Palevich1badb712009-03-25 15:12:17 -0700102 gBitmap_NativeBitmapFieldID = _env->GetFieldID(bitmap_class, "mNativeBitmap", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103}
104
Jack Palevich1badb712009-03-25 15:12:17 -0700105static const jint gNull_attrib_base[] = {EGL_NONE};
106
107static bool validAttribList(JNIEnv *_env, jintArray attrib_list) {
108 if (attrib_list == NULL) {
109 return true;
110 }
111 jsize len = _env->GetArrayLength(attrib_list);
112 if (len < 1) {
113 return false;
114 }
115 jint item = 0;
116 _env->GetIntArrayRegion(attrib_list, len-1, 1, &item);
117 return item == EGL_NONE;
118}
119
120static jint* beginNativeAttribList(JNIEnv *_env, jintArray attrib_list) {
121 if (attrib_list != NULL) {
122 return (jint *)_env->GetPrimitiveArrayCritical(attrib_list, (jboolean *)0);
123 } else {
124 return(jint*) gNull_attrib_base;
125 }
126}
127
128static void endNativeAttributeList(JNIEnv *_env, jintArray attrib_list, jint* attrib_base) {
129 if (attrib_list != NULL) {
130 _env->ReleasePrimitiveArrayCritical(attrib_list, attrib_base, JNI_ABORT);
131 }
132}
133
134static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 jintArray major_minor) {
Jack Palevich1badb712009-03-25 15:12:17 -0700136 if (display == NULL || (major_minor != NULL &&
137 _env->GetArrayLength(major_minor) < 2)) {
138 doThrow(_env, "java/lang/IllegalArgumentException");
139 return JNI_FALSE;
140 }
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 EGLDisplay dpy = getDisplay(_env, display);
143 jboolean success = eglInitialize(dpy, NULL, NULL);
144 if (success && major_minor) {
145 int len = _env->GetArrayLength(major_minor);
146 if (len) {
147 // we're exposing only EGL 1.0
148 jint* base = (jint *)_env->GetPrimitiveArrayCritical(major_minor, (jboolean *)0);
149 if (len >= 1) base[0] = 1;
150 if (len >= 2) base[1] = 0;
151 _env->ReleasePrimitiveArrayCritical(major_minor, base, JNI_ABORT);
152 }
153 }
154 return success;
155}
156
Jack Palevich1badb712009-03-25 15:12:17 -0700157static jboolean jni_eglQueryContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 jobject context, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700159 if (display == NULL || context == NULL || value == NULL
160 || _env->GetArrayLength(value) < 1) {
161 doThrow(_env, "java/lang/IllegalArgumentException");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 return JNI_FALSE;
163 }
Jack Palevich1badb712009-03-25 15:12:17 -0700164 EGLDisplay dpy = getDisplay(_env, display);
165 EGLContext ctx = getContext(_env, context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 jboolean success = JNI_FALSE;
167 int len = _env->GetArrayLength(value);
168 if (len) {
169 jint* base = (jint *)_env->GetPrimitiveArrayCritical(value, (jboolean *)0);
170 success = eglQueryContext(dpy, ctx, attribute, base);
171 _env->ReleasePrimitiveArrayCritical(value, base, JNI_ABORT);
172 }
173 return success;
174}
Jack Palevich1badb712009-03-25 15:12:17 -0700175
176static jboolean jni_eglQuerySurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 jobject surface, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700178 if (display == NULL || surface == NULL || value == NULL
179 || _env->GetArrayLength(value) < 1) {
180 doThrow(_env, "java/lang/IllegalArgumentException");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 return JNI_FALSE;
182 }
Jack Palevich1badb712009-03-25 15:12:17 -0700183 EGLDisplay dpy = getDisplay(_env, display);
184 EGLContext sur = getSurface(_env, surface);
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 jboolean success = JNI_FALSE;
187 int len = _env->GetArrayLength(value);
188 if (len) {
189 jint* base = (jint *)_env->GetPrimitiveArrayCritical(value, (jboolean *)0);
190 success = eglQuerySurface(dpy, sur, attribute, base);
191 _env->ReleasePrimitiveArrayCritical(value, base, JNI_ABORT);
192 }
193 return success;
194}
195
Jack Palevich1badb712009-03-25 15:12:17 -0700196static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
197 jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
198 if (display == NULL
199 || !validAttribList(_env, attrib_list)
200 || (configs != NULL && _env->GetArrayLength(configs) < config_size)
201 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
202 doThrow(_env, "java/lang/IllegalArgumentException");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 return JNI_FALSE;
204 }
Jack Palevich1badb712009-03-25 15:12:17 -0700205 EGLDisplay dpy = getDisplay(_env, display);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 jboolean success = JNI_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700207
208 if (configs == NULL) {
209 config_size = 0;
210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700212
213 int num = 0;
214 jint* attrib_base = beginNativeAttribList(_env, attrib_list);
215 success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
216 endNativeAttributeList(_env, attrib_list, attrib_base);
217
218 if (num_config != NULL) {
219 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
220 }
221
222 if (success && configs!=NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 for (int i=0 ; i<num ; i++) {
224 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, (jint)nativeConfigs[i]);
225 _env->SetObjectArrayElement(configs, i, obj);
226 }
227 }
228 return success;
Jack Palevich1badb712009-03-25 15:12:17 -0700229}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
Jack Palevich1badb712009-03-25 15:12:17 -0700231static jint jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 jobject config, jobject share_context, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700233 if (display == NULL || config == NULL || share_context == NULL
234 || !validAttribList(_env, attrib_list)) {
235 doThrow(_env, "java/lang/IllegalArgumentException");
236 return JNI_FALSE;
237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 EGLDisplay dpy = getDisplay(_env, display);
239 EGLConfig cnf = getConfig(_env, config);
240 EGLContext shr = getContext(_env, share_context);
Jack Palevich1badb712009-03-25 15:12:17 -0700241 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700243 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 return (jint)ctx;
245}
246
Jack Palevich1badb712009-03-25 15:12:17 -0700247static jint jni_eglCreatePbufferSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 jobject config, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700249 if (display == NULL || config == NULL
250 || !validAttribList(_env, attrib_list)) {
251 doThrow(_env, "java/lang/IllegalArgumentException");
252 return JNI_FALSE;
253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 EGLDisplay dpy = getDisplay(_env, display);
255 EGLConfig cnf = getConfig(_env, config);
Jack Palevich1badb712009-03-25 15:12:17 -0700256 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 EGLSurface sur = eglCreatePbufferSurface(dpy, cnf, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700258 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 return (jint)sur;
260}
261
262static PixelFormat convertPixelFormat(SkBitmap::Config format)
263{
264 switch (format) {
265 case SkBitmap::kARGB_8888_Config: return PIXEL_FORMAT_RGBA_8888;
266 case SkBitmap::kARGB_4444_Config: return PIXEL_FORMAT_RGBA_4444;
267 case SkBitmap::kRGB_565_Config: return PIXEL_FORMAT_RGB_565;
268 case SkBitmap::kA8_Config: return PIXEL_FORMAT_A_8;
269 default: return PIXEL_FORMAT_NONE;
270 }
271}
272
Jack Palevich1badb712009-03-25 15:12:17 -0700273static void jni_eglCreatePixmapSurface(JNIEnv *_env, jobject _this, jobject out_sur,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 jobject display, jobject config, jobject native_pixmap,
Jack Palevich1badb712009-03-25 15:12:17 -0700275 jintArray attrib_list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276{
Jack Palevich1badb712009-03-25 15:12:17 -0700277 if (display == NULL || config == NULL || native_pixmap == NULL
278 || !validAttribList(_env, attrib_list)) {
279 doThrow(_env, "java/lang/IllegalArgumentException");
280 return;
281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 EGLDisplay dpy = getDisplay(_env, display);
283 EGLConfig cnf = getConfig(_env, config);
284 jint* base = 0;
285
286 SkBitmap const * nativeBitmap =
287 (SkBitmap const *)_env->GetIntField(native_pixmap,
288 gBitmap_NativeBitmapFieldID);
289 SkPixelRef* ref = nativeBitmap ? nativeBitmap->pixelRef() : 0;
290 if (ref == NULL) {
Jack Palevich1badb712009-03-25 15:12:17 -0700291 doThrow(_env, "java/lang/IllegalArgumentException", "Bitmap has no PixelRef");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 return;
293 }
Jack Palevich1badb712009-03-25 15:12:17 -0700294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 ref->safeRef();
296 ref->lockPixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 egl_native_pixmap_t pixmap;
299 pixmap.version = sizeof(pixmap);
300 pixmap.width = nativeBitmap->width();
301 pixmap.height = nativeBitmap->height();
302 pixmap.stride = nativeBitmap->rowBytes() / nativeBitmap->bytesPerPixel();
303 pixmap.format = convertPixelFormat(nativeBitmap->config());
304 pixmap.data = (uint8_t*)ref->pixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700305
306 base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 EGLSurface sur = eglCreatePixmapSurface(dpy, cnf, &pixmap, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700308 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309
310 if (sur != EGL_NO_SURFACE) {
311 _env->SetIntField(out_sur, gSurface_EGLSurfaceFieldID, (int)sur);
312 _env->SetIntField(out_sur, gSurface_NativePixelRefFieldID, (int)ref);
313 } else {
314 ref->unlockPixels();
315 ref->safeUnref();
316 }
317}
318
Jack Palevich1badb712009-03-25 15:12:17 -0700319static jint jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 jobject config, jobject native_window, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700321 if (display == NULL || config == NULL
322 || !validAttribList(_env, attrib_list)) {
323 doThrow(_env, "java/lang/IllegalArgumentException");
324 return JNI_FALSE;
325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 EGLDisplay dpy = getDisplay(_env, display);
327 EGLContext cnf = getConfig(_env, config);
Mathias Agopian8b73ae42010-06-10 17:02:51 -0700328 EGLNativeWindowType window = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 if (native_window == NULL) {
330not_valid_surface:
Jack Palevich1badb712009-03-25 15:12:17 -0700331 doThrow(_env, "java/lang/IllegalArgumentException",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
333 return 0;
334 }
Mathias Agopian8b73ae42010-06-10 17:02:51 -0700335
336 window = android_Surface_getEGLNativeWindow(_env, native_window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 if (window == NULL)
338 goto not_valid_surface;
339
Jack Palevich1badb712009-03-25 15:12:17 -0700340 jint* base = beginNativeAttribList(_env, attrib_list);
Mathias Agopian1473f462009-04-10 14:24:30 -0700341 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700342 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 return (jint)sur;
344}
345
Jack Palevich1badb712009-03-25 15:12:17 -0700346static jboolean jni_eglGetConfigAttrib(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 jobject config, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700348 if (display == NULL || config == NULL
349 || (value == NULL || _env->GetArrayLength(value) < 1)) {
350 doThrow(_env, "java/lang/IllegalArgumentException");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 return JNI_FALSE;
352 }
Jack Palevich1badb712009-03-25 15:12:17 -0700353 EGLDisplay dpy = getDisplay(_env, display);
354 EGLContext cnf = getConfig(_env, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 jboolean success = JNI_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700356 jint localValue;
357 success = eglGetConfigAttrib(dpy, cnf, attribute, &localValue);
358 if (success) {
359 _env->SetIntArrayRegion(value, 0, 1, &localValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
361 return success;
362}
363
Jack Palevich1badb712009-03-25 15:12:17 -0700364static jboolean jni_eglGetConfigs(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 jobjectArray configs, jint config_size, jintArray num_config) {
Jack Palevich1badb712009-03-25 15:12:17 -0700366 if (display == NULL || (configs != NULL && _env->GetArrayLength(configs) < config_size)
367 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
368 doThrow(_env, "java/lang/IllegalArgumentException");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 return JNI_FALSE;
370 }
Jack Palevich1badb712009-03-25 15:12:17 -0700371 EGLDisplay dpy = getDisplay(_env, display);
372 jboolean success = JNI_FALSE;
373 if (configs == NULL) {
374 config_size = 0;
375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700377 int num;
378 success = eglGetConfigs(dpy, configs ? nativeConfigs : 0, config_size, &num);
379 if (num_config != NULL) {
380 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 if (success && configs) {
383 for (int i=0 ; i<num ; i++) {
384 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, (jint)nativeConfigs[i]);
385 _env->SetObjectArrayElement(configs, i, obj);
386 }
387 }
388 return success;
389}
Jack Palevich1badb712009-03-25 15:12:17 -0700390
391static jint jni_eglGetError(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 EGLint error = eglGetError();
393 return error;
394}
395
Jack Palevich1badb712009-03-25 15:12:17 -0700396static jint jni_eglGetCurrentContext(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 return (jint)eglGetCurrentContext();
398}
399
Jack Palevich1badb712009-03-25 15:12:17 -0700400static jint jni_eglGetCurrentDisplay(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 return (jint)eglGetCurrentDisplay();
402}
403
Jack Palevich1badb712009-03-25 15:12:17 -0700404static jint jni_eglGetCurrentSurface(JNIEnv *_env, jobject _this, jint readdraw) {
405 if (!(readdraw == EGL_READ) || (readdraw == EGL_DRAW)) {
406 doThrow(_env, "java/lang/IllegalArgumentException");
407 return 0;
408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 return (jint)eglGetCurrentSurface(readdraw);
410}
411
Jack Palevich1badb712009-03-25 15:12:17 -0700412static jboolean jni_eglDestroyContext(JNIEnv *_env, jobject _this, jobject display, jobject context) {
413 if (display == NULL || context == NULL) {
414 doThrow(_env, "java/lang/IllegalArgumentException");
415 return JNI_FALSE;
416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 EGLDisplay dpy = getDisplay(_env, display);
418 EGLContext ctx = getContext(_env, context);
419 return eglDestroyContext(dpy, ctx);
420}
421
Jack Palevich1badb712009-03-25 15:12:17 -0700422static jboolean jni_eglDestroySurface(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
423 if (display == NULL || surface == NULL) {
424 doThrow(_env, "java/lang/IllegalArgumentException");
425 return JNI_FALSE;
426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 EGLDisplay dpy = getDisplay(_env, display);
428 EGLSurface sur = getSurface(_env, surface);
429
430 if (sur) {
431 SkPixelRef* ref = (SkPixelRef*)(_env->GetIntField(surface,
432 gSurface_NativePixelRefFieldID));
433 if (ref) {
434 ref->unlockPixels();
435 ref->safeUnref();
436 }
437 }
438 return eglDestroySurface(dpy, sur);
439}
440
Jack Palevich1badb712009-03-25 15:12:17 -0700441static jint jni_eglGetDisplay(JNIEnv *_env, jobject _this, jobject native_display) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 return (jint)eglGetDisplay(EGL_DEFAULT_DISPLAY);
443}
444
Jack Palevich1badb712009-03-25 15:12:17 -0700445static jboolean jni_eglMakeCurrent(JNIEnv *_env, jobject _this, jobject display, jobject draw, jobject read, jobject context) {
446 if (display == NULL || draw == NULL || read == NULL || context == NULL) {
447 doThrow(_env, "java/lang/IllegalArgumentException");
448 return JNI_FALSE;
449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 EGLDisplay dpy = getDisplay(_env, display);
451 EGLSurface sdr = getSurface(_env, draw);
452 EGLSurface srd = getSurface(_env, read);
453 EGLContext ctx = getContext(_env, context);
454 return eglMakeCurrent(dpy, sdr, srd, ctx);
455}
456
Jack Palevich1badb712009-03-25 15:12:17 -0700457static jstring jni_eglQueryString(JNIEnv *_env, jobject _this, jobject display, jint name) {
458 if (display == NULL) {
459 doThrow(_env, "java/lang/IllegalArgumentException");
460 return NULL;
461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 EGLDisplay dpy = getDisplay(_env, display);
463 const char* chars = eglQueryString(dpy, name);
464 return _env->NewStringUTF(chars);
465}
466
Jack Palevich1badb712009-03-25 15:12:17 -0700467static jboolean jni_eglSwapBuffers(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
468 if (display == NULL || surface == NULL) {
469 doThrow(_env, "java/lang/IllegalArgumentException");
470 return JNI_FALSE;
471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 EGLDisplay dpy = getDisplay(_env, display);
473 EGLSurface sur = getSurface(_env, surface);
474 return eglSwapBuffers(dpy, sur);
475}
476
Jack Palevich1badb712009-03-25 15:12:17 -0700477static jboolean jni_eglTerminate(JNIEnv *_env, jobject _this, jobject display) {
478 if (display == NULL) {
479 doThrow(_env, "java/lang/IllegalArgumentException");
480 return JNI_FALSE;
481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 EGLDisplay dpy = getDisplay(_env, display);
483 return eglTerminate(dpy);
484}
485
Jack Palevich1badb712009-03-25 15:12:17 -0700486static jboolean jni_eglCopyBuffers(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 jobject surface, jobject native_pixmap) {
Jack Palevich1badb712009-03-25 15:12:17 -0700488 if (display == NULL || surface == NULL || native_pixmap == NULL) {
489 doThrow(_env, "java/lang/IllegalArgumentException");
490 return JNI_FALSE;
491 }
492 // TODO: Implement this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 return JNI_FALSE;
494}
495
Jack Palevich1badb712009-03-25 15:12:17 -0700496static jboolean jni_eglWaitGL(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 return eglWaitGL();
498}
499
Jack Palevich1badb712009-03-25 15:12:17 -0700500static jboolean jni_eglWaitNative(JNIEnv *_env, jobject _this, jint engine, jobject bindTarget) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 return eglWaitNative(engine);
502}
503
504
505static const char *classPathName = "com/google/android/gles_jni/EGLImpl";
506
507#define DISPLAY "Ljavax/microedition/khronos/egl/EGLDisplay;"
508#define CONTEXT "Ljavax/microedition/khronos/egl/EGLContext;"
509#define CONFIG "Ljavax/microedition/khronos/egl/EGLConfig;"
510#define SURFACE "Ljavax/microedition/khronos/egl/EGLSurface;"
511#define OBJECT "Ljava/lang/Object;"
512#define STRING "Ljava/lang/String;"
513
514static JNINativeMethod methods[] = {
515{"_nativeClassInit","()V", (void*)nativeClassInit },
516{"eglWaitGL", "()Z", (void*)jni_eglWaitGL },
517{"eglInitialize", "(" DISPLAY "[I)Z", (void*)jni_eglInitialize },
518{"eglQueryContext", "(" DISPLAY CONTEXT "I[I)Z", (void*)jni_eglQueryContext },
519{"eglQuerySurface", "(" DISPLAY SURFACE "I[I)Z", (void*)jni_eglQuerySurface },
520{"eglChooseConfig", "(" DISPLAY "[I[" CONFIG "I[I)Z", (void*)jni_eglChooseConfig },
521{"_eglCreateContext","(" DISPLAY CONFIG CONTEXT "[I)I", (void*)jni_eglCreateContext },
522{"eglGetConfigs", "(" DISPLAY "[" CONFIG "I[I)Z", (void*)jni_eglGetConfigs },
523{"eglTerminate", "(" DISPLAY ")Z", (void*)jni_eglTerminate },
524{"eglCopyBuffers", "(" DISPLAY SURFACE OBJECT ")Z", (void*)jni_eglCopyBuffers },
525{"eglWaitNative", "(I" OBJECT ")Z", (void*)jni_eglWaitNative },
526{"eglGetError", "()I", (void*)jni_eglGetError },
527{"eglGetConfigAttrib", "(" DISPLAY CONFIG "I[I)Z", (void*)jni_eglGetConfigAttrib },
528{"_eglGetDisplay", "(" OBJECT ")I", (void*)jni_eglGetDisplay },
529{"_eglGetCurrentContext", "()I", (void*)jni_eglGetCurrentContext },
530{"_eglGetCurrentDisplay", "()I", (void*)jni_eglGetCurrentDisplay },
531{"_eglGetCurrentSurface", "(I)I", (void*)jni_eglGetCurrentSurface },
532{"_eglCreatePbufferSurface","(" DISPLAY CONFIG "[I)I", (void*)jni_eglCreatePbufferSurface },
533{"_eglCreatePixmapSurface", "(" SURFACE DISPLAY CONFIG OBJECT "[I)V", (void*)jni_eglCreatePixmapSurface },
534{"_eglCreateWindowSurface", "(" DISPLAY CONFIG OBJECT "[I)I", (void*)jni_eglCreateWindowSurface },
535{"eglDestroyContext", "(" DISPLAY CONTEXT ")Z", (void*)jni_eglDestroyContext },
536{"eglDestroySurface", "(" DISPLAY SURFACE ")Z", (void*)jni_eglDestroySurface },
537{"eglMakeCurrent", "(" DISPLAY SURFACE SURFACE CONTEXT")Z", (void*)jni_eglMakeCurrent },
538{"eglQueryString", "(" DISPLAY "I)" STRING, (void*)jni_eglQueryString },
539{"eglSwapBuffers", "(" DISPLAY SURFACE ")Z", (void*)jni_eglSwapBuffers },
540};
541
542} // namespace android
543
544int register_com_google_android_gles_jni_EGLImpl(JNIEnv *_env)
545{
546 int err;
547 err = android::AndroidRuntime::registerNativeMethods(_env,
548 android::classPathName, android::methods, NELEM(android::methods));
549 return err;
550}
551