blob: f77752759dc3469543914a0130272a28f982a4ce [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
Romain Guy8f0095c2011-05-02 17:24:22 -070030#include <gui/SurfaceTexture.h>
31#include <gui/SurfaceTextureClient.h>
32
Dianne Hackborn54a181b2010-06-30 18:35:14 -070033namespace android {
Mathias Agopian8b73ae42010-06-10 17:02:51 -070034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035static 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047static inline EGLDisplay getDisplay(JNIEnv* env, jobject o) {
48 if (!o) return EGL_NO_DISPLAY;
49 return (EGLDisplay)env->GetIntField(o, gDisplay_EGLDisplayFieldID);
50}
51static inline EGLSurface getSurface(JNIEnv* env, jobject o) {
52 if (!o) return EGL_NO_SURFACE;
53 return (EGLSurface)env->GetIntField(o, gSurface_EGLSurfaceFieldID);
54}
55static inline EGLContext getContext(JNIEnv* env, jobject o) {
56 if (!o) return EGL_NO_CONTEXT;
57 return (EGLContext)env->GetIntField(o, gContext_EGLContextFieldID);
58}
59static inline EGLConfig getConfig(JNIEnv* env, jobject o) {
60 if (!o) return 0;
61 return (EGLConfig)env->GetIntField(o, gConfig_EGLConfigFieldID);
62}
63static void nativeClassInit(JNIEnv *_env, jclass eglImplClass)
64{
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070065 jclass config_class = _env->FindClass("com/google/android/gles_jni/EGLConfigImpl");
66 gConfig_class = (jclass) _env->NewGlobalRef(config_class);
67 gConfig_ctorID = _env->GetMethodID(gConfig_class, "<init>", "(I)V");
68 gConfig_EGLConfigFieldID = _env->GetFieldID(gConfig_class, "mEGLConfig", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070070 jclass display_class = _env->FindClass("com/google/android/gles_jni/EGLDisplayImpl");
71 gDisplay_EGLDisplayFieldID = _env->GetFieldID(display_class, "mEGLDisplay", "I");
Jack Palevich1badb712009-03-25 15:12:17 -070072
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070073 jclass context_class = _env->FindClass("com/google/android/gles_jni/EGLContextImpl");
74 gContext_EGLContextFieldID = _env->GetFieldID(context_class, "mEGLContext", "I");
75
76 jclass surface_class = _env->FindClass("com/google/android/gles_jni/EGLSurfaceImpl");
77 gSurface_EGLSurfaceFieldID = _env->GetFieldID(surface_class, "mEGLSurface", "I");
78 gSurface_NativePixelRefFieldID = _env->GetFieldID(surface_class, "mNativePixelRef", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 jclass bitmap_class = _env->FindClass("android/graphics/Bitmap");
Jack Palevich1badb712009-03-25 15:12:17 -070081 gBitmap_NativeBitmapFieldID = _env->GetFieldID(bitmap_class, "mNativeBitmap", "I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082}
83
Jack Palevich1badb712009-03-25 15:12:17 -070084static const jint gNull_attrib_base[] = {EGL_NONE};
85
86static bool validAttribList(JNIEnv *_env, jintArray attrib_list) {
87 if (attrib_list == NULL) {
88 return true;
89 }
90 jsize len = _env->GetArrayLength(attrib_list);
91 if (len < 1) {
92 return false;
93 }
94 jint item = 0;
95 _env->GetIntArrayRegion(attrib_list, len-1, 1, &item);
96 return item == EGL_NONE;
97}
98
99static jint* beginNativeAttribList(JNIEnv *_env, jintArray attrib_list) {
100 if (attrib_list != NULL) {
101 return (jint *)_env->GetPrimitiveArrayCritical(attrib_list, (jboolean *)0);
102 } else {
103 return(jint*) gNull_attrib_base;
104 }
105}
106
107static void endNativeAttributeList(JNIEnv *_env, jintArray attrib_list, jint* attrib_base) {
108 if (attrib_list != NULL) {
109 _env->ReleasePrimitiveArrayCritical(attrib_list, attrib_base, JNI_ABORT);
110 }
111}
112
113static jboolean jni_eglInitialize(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 jintArray major_minor) {
Jack Palevich1badb712009-03-25 15:12:17 -0700115 if (display == NULL || (major_minor != NULL &&
116 _env->GetArrayLength(major_minor) < 2)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700117 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700118 return JNI_FALSE;
119 }
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 EGLDisplay dpy = getDisplay(_env, display);
122 jboolean success = eglInitialize(dpy, NULL, NULL);
123 if (success && major_minor) {
124 int len = _env->GetArrayLength(major_minor);
125 if (len) {
126 // we're exposing only EGL 1.0
127 jint* base = (jint *)_env->GetPrimitiveArrayCritical(major_minor, (jboolean *)0);
128 if (len >= 1) base[0] = 1;
129 if (len >= 2) base[1] = 0;
130 _env->ReleasePrimitiveArrayCritical(major_minor, base, JNI_ABORT);
131 }
132 }
133 return success;
134}
135
Jack Palevich1badb712009-03-25 15:12:17 -0700136static jboolean jni_eglQueryContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 jobject context, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700138 if (display == NULL || context == NULL || value == NULL
139 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700140 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 return JNI_FALSE;
142 }
Jack Palevich1badb712009-03-25 15:12:17 -0700143 EGLDisplay dpy = getDisplay(_env, display);
144 EGLContext ctx = getContext(_env, context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 jboolean success = JNI_FALSE;
146 int len = _env->GetArrayLength(value);
147 if (len) {
148 jint* base = (jint *)_env->GetPrimitiveArrayCritical(value, (jboolean *)0);
149 success = eglQueryContext(dpy, ctx, attribute, base);
150 _env->ReleasePrimitiveArrayCritical(value, base, JNI_ABORT);
151 }
152 return success;
153}
Jack Palevich1badb712009-03-25 15:12:17 -0700154
155static jboolean jni_eglQuerySurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 jobject surface, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700157 if (display == NULL || surface == NULL || value == NULL
158 || _env->GetArrayLength(value) < 1) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700159 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
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 sur = getSurface(_env, surface);
164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 jboolean success = JNI_FALSE;
166 int len = _env->GetArrayLength(value);
167 if (len) {
168 jint* base = (jint *)_env->GetPrimitiveArrayCritical(value, (jboolean *)0);
169 success = eglQuerySurface(dpy, sur, attribute, base);
170 _env->ReleasePrimitiveArrayCritical(value, base, JNI_ABORT);
171 }
172 return success;
173}
174
Jack Palevich1badb712009-03-25 15:12:17 -0700175static jboolean jni_eglChooseConfig(JNIEnv *_env, jobject _this, jobject display,
176 jintArray attrib_list, jobjectArray configs, jint config_size, jintArray num_config) {
177 if (display == NULL
178 || !validAttribList(_env, attrib_list)
179 || (configs != NULL && _env->GetArrayLength(configs) < config_size)
180 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700181 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 return JNI_FALSE;
183 }
Jack Palevich1badb712009-03-25 15:12:17 -0700184 EGLDisplay dpy = getDisplay(_env, display);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 jboolean success = JNI_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700186
187 if (configs == NULL) {
188 config_size = 0;
189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700191
192 int num = 0;
193 jint* attrib_base = beginNativeAttribList(_env, attrib_list);
194 success = eglChooseConfig(dpy, attrib_base, configs ? nativeConfigs : 0, config_size, &num);
195 endNativeAttributeList(_env, attrib_list, attrib_base);
196
197 if (num_config != NULL) {
198 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
199 }
200
201 if (success && configs!=NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 for (int i=0 ; i<num ; i++) {
203 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, (jint)nativeConfigs[i]);
204 _env->SetObjectArrayElement(configs, i, obj);
205 }
206 }
207 return success;
Jack Palevich1badb712009-03-25 15:12:17 -0700208}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
Jack Palevich1badb712009-03-25 15:12:17 -0700210static jint jni_eglCreateContext(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 jobject config, jobject share_context, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700212 if (display == NULL || config == NULL || share_context == NULL
213 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700214 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700215 return JNI_FALSE;
216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 EGLDisplay dpy = getDisplay(_env, display);
218 EGLConfig cnf = getConfig(_env, config);
219 EGLContext shr = getContext(_env, share_context);
Jack Palevich1badb712009-03-25 15:12:17 -0700220 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 EGLContext ctx = eglCreateContext(dpy, cnf, shr, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700222 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 return (jint)ctx;
224}
225
Jack Palevich1badb712009-03-25 15:12:17 -0700226static jint jni_eglCreatePbufferSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 jobject config, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700228 if (display == NULL || config == NULL
229 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700230 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700231 return JNI_FALSE;
232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 EGLDisplay dpy = getDisplay(_env, display);
234 EGLConfig cnf = getConfig(_env, config);
Jack Palevich1badb712009-03-25 15:12:17 -0700235 jint* base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 EGLSurface sur = eglCreatePbufferSurface(dpy, cnf, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700237 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 return (jint)sur;
239}
240
241static PixelFormat convertPixelFormat(SkBitmap::Config format)
242{
243 switch (format) {
244 case SkBitmap::kARGB_8888_Config: return PIXEL_FORMAT_RGBA_8888;
245 case SkBitmap::kARGB_4444_Config: return PIXEL_FORMAT_RGBA_4444;
246 case SkBitmap::kRGB_565_Config: return PIXEL_FORMAT_RGB_565;
247 case SkBitmap::kA8_Config: return PIXEL_FORMAT_A_8;
248 default: return PIXEL_FORMAT_NONE;
249 }
250}
251
Jack Palevich1badb712009-03-25 15:12:17 -0700252static void jni_eglCreatePixmapSurface(JNIEnv *_env, jobject _this, jobject out_sur,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 jobject display, jobject config, jobject native_pixmap,
Jack Palevich1badb712009-03-25 15:12:17 -0700254 jintArray attrib_list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255{
Jack Palevich1badb712009-03-25 15:12:17 -0700256 if (display == NULL || config == NULL || native_pixmap == NULL
257 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700258 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700259 return;
260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 EGLDisplay dpy = getDisplay(_env, display);
262 EGLConfig cnf = getConfig(_env, config);
263 jint* base = 0;
264
265 SkBitmap const * nativeBitmap =
266 (SkBitmap const *)_env->GetIntField(native_pixmap,
267 gBitmap_NativeBitmapFieldID);
268 SkPixelRef* ref = nativeBitmap ? nativeBitmap->pixelRef() : 0;
269 if (ref == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700270 jniThrowException(_env, "java/lang/IllegalArgumentException", "Bitmap has no PixelRef");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 return;
272 }
Jack Palevich1badb712009-03-25 15:12:17 -0700273
Derek Sollenberger6062c592011-02-22 13:55:04 -0500274 SkSafeRef(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 ref->lockPixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 egl_native_pixmap_t pixmap;
278 pixmap.version = sizeof(pixmap);
279 pixmap.width = nativeBitmap->width();
280 pixmap.height = nativeBitmap->height();
281 pixmap.stride = nativeBitmap->rowBytes() / nativeBitmap->bytesPerPixel();
282 pixmap.format = convertPixelFormat(nativeBitmap->config());
283 pixmap.data = (uint8_t*)ref->pixels();
Jack Palevich1badb712009-03-25 15:12:17 -0700284
285 base = beginNativeAttribList(_env, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 EGLSurface sur = eglCreatePixmapSurface(dpy, cnf, &pixmap, base);
Jack Palevich1badb712009-03-25 15:12:17 -0700287 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288
289 if (sur != EGL_NO_SURFACE) {
290 _env->SetIntField(out_sur, gSurface_EGLSurfaceFieldID, (int)sur);
291 _env->SetIntField(out_sur, gSurface_NativePixelRefFieldID, (int)ref);
292 } else {
293 ref->unlockPixels();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500294 SkSafeUnref(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296}
297
Jack Palevich1badb712009-03-25 15:12:17 -0700298static jint jni_eglCreateWindowSurface(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 jobject config, jobject native_window, jintArray attrib_list) {
Jack Palevich1badb712009-03-25 15:12:17 -0700300 if (display == NULL || config == NULL
301 || !validAttribList(_env, attrib_list)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700302 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700303 return JNI_FALSE;
304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 EGLDisplay dpy = getDisplay(_env, display);
306 EGLContext cnf = getConfig(_env, config);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700307 sp<ANativeWindow> window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 if (native_window == NULL) {
309not_valid_surface:
Elliott Hughes8451b252011-04-07 19:17:57 -0700310 jniThrowException(_env, "java/lang/IllegalArgumentException",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface");
312 return 0;
313 }
Mathias Agopian8b73ae42010-06-10 17:02:51 -0700314
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700315 window = android_Surface_getNativeWindow(_env, native_window);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 if (window == NULL)
317 goto not_valid_surface;
318
Jack Palevich1badb712009-03-25 15:12:17 -0700319 jint* base = beginNativeAttribList(_env, attrib_list);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700320 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
Jack Palevich1badb712009-03-25 15:12:17 -0700321 endNativeAttributeList(_env, attrib_list, base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 return (jint)sur;
323}
324
Romain Guy8f0095c2011-05-02 17:24:22 -0700325static jint jni_eglCreateWindowSurfaceTexture(JNIEnv *_env, jobject _this, jobject display,
326 jobject config, jint native_window, jintArray attrib_list) {
327 if (display == NULL || config == NULL
328 || !validAttribList(_env, attrib_list)) {
329 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
330 return JNI_FALSE;
331 }
332 EGLDisplay dpy = getDisplay(_env, display);
333 EGLContext cnf = getConfig(_env, config);
334 sp<ANativeWindow> window;
335 if (native_window == 0) {
336not_valid_surface:
337 jniThrowException(_env, "java/lang/IllegalArgumentException",
338 "Make sure the SurfaceTexture is valid");
339 return 0;
340 }
341
342 sp<SurfaceTexture> surfaceTexture = reinterpret_cast<SurfaceTexture*>(native_window);
343
344 window = new SurfaceTextureClient(surfaceTexture);
345 if (window == NULL)
346 goto not_valid_surface;
347
348 jint* base = beginNativeAttribList(_env, attrib_list);
349 EGLSurface sur = eglCreateWindowSurface(dpy, cnf, window.get(), base);
350 endNativeAttributeList(_env, attrib_list, base);
351 return (jint)sur;
352}
353
Jack Palevich1badb712009-03-25 15:12:17 -0700354static jboolean jni_eglGetConfigAttrib(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 jobject config, jint attribute, jintArray value) {
Jack Palevich1badb712009-03-25 15:12:17 -0700356 if (display == NULL || config == NULL
357 || (value == NULL || _env->GetArrayLength(value) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700358 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 return JNI_FALSE;
360 }
Jack Palevich1badb712009-03-25 15:12:17 -0700361 EGLDisplay dpy = getDisplay(_env, display);
362 EGLContext cnf = getConfig(_env, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 jboolean success = JNI_FALSE;
Jack Palevich1badb712009-03-25 15:12:17 -0700364 jint localValue;
365 success = eglGetConfigAttrib(dpy, cnf, attribute, &localValue);
366 if (success) {
367 _env->SetIntArrayRegion(value, 0, 1, &localValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
369 return success;
370}
371
Jack Palevich1badb712009-03-25 15:12:17 -0700372static jboolean jni_eglGetConfigs(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 jobjectArray configs, jint config_size, jintArray num_config) {
Jack Palevich1badb712009-03-25 15:12:17 -0700374 if (display == NULL || (configs != NULL && _env->GetArrayLength(configs) < config_size)
375 || (num_config != NULL && _env->GetArrayLength(num_config) < 1)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700376 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 return JNI_FALSE;
378 }
Jack Palevich1badb712009-03-25 15:12:17 -0700379 EGLDisplay dpy = getDisplay(_env, display);
380 jboolean success = JNI_FALSE;
381 if (configs == NULL) {
382 config_size = 0;
383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 EGLConfig nativeConfigs[config_size];
Jack Palevich1badb712009-03-25 15:12:17 -0700385 int num;
386 success = eglGetConfigs(dpy, configs ? nativeConfigs : 0, config_size, &num);
387 if (num_config != NULL) {
388 _env->SetIntArrayRegion(num_config, 0, 1, (jint*) &num);
389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 if (success && configs) {
391 for (int i=0 ; i<num ; i++) {
392 jobject obj = _env->NewObject(gConfig_class, gConfig_ctorID, (jint)nativeConfigs[i]);
393 _env->SetObjectArrayElement(configs, i, obj);
394 }
395 }
396 return success;
397}
Jack Palevich1badb712009-03-25 15:12:17 -0700398
399static jint jni_eglGetError(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 EGLint error = eglGetError();
401 return error;
402}
403
Jack Palevich1badb712009-03-25 15:12:17 -0700404static jint jni_eglGetCurrentContext(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 return (jint)eglGetCurrentContext();
406}
407
Jack Palevich1badb712009-03-25 15:12:17 -0700408static jint jni_eglGetCurrentDisplay(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 return (jint)eglGetCurrentDisplay();
410}
411
Jack Palevich1badb712009-03-25 15:12:17 -0700412static jint jni_eglGetCurrentSurface(JNIEnv *_env, jobject _this, jint readdraw) {
Romain Guy9b7146d2011-03-07 18:05:04 -0800413 if ((readdraw != EGL_READ) && (readdraw != EGL_DRAW)) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700414 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700415 return 0;
416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 return (jint)eglGetCurrentSurface(readdraw);
418}
419
Jack Palevich1badb712009-03-25 15:12:17 -0700420static jboolean jni_eglDestroyContext(JNIEnv *_env, jobject _this, jobject display, jobject context) {
421 if (display == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700422 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700423 return JNI_FALSE;
424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 EGLDisplay dpy = getDisplay(_env, display);
426 EGLContext ctx = getContext(_env, context);
427 return eglDestroyContext(dpy, ctx);
428}
429
Jack Palevich1badb712009-03-25 15:12:17 -0700430static jboolean jni_eglDestroySurface(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
431 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700432 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700433 return JNI_FALSE;
434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 EGLDisplay dpy = getDisplay(_env, display);
436 EGLSurface sur = getSurface(_env, surface);
437
438 if (sur) {
439 SkPixelRef* ref = (SkPixelRef*)(_env->GetIntField(surface,
440 gSurface_NativePixelRefFieldID));
441 if (ref) {
442 ref->unlockPixels();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500443 SkSafeUnref(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 }
446 return eglDestroySurface(dpy, sur);
447}
448
Jack Palevich1badb712009-03-25 15:12:17 -0700449static jint jni_eglGetDisplay(JNIEnv *_env, jobject _this, jobject native_display) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 return (jint)eglGetDisplay(EGL_DEFAULT_DISPLAY);
451}
452
Jack Palevich1badb712009-03-25 15:12:17 -0700453static jboolean jni_eglMakeCurrent(JNIEnv *_env, jobject _this, jobject display, jobject draw, jobject read, jobject context) {
454 if (display == NULL || draw == NULL || read == NULL || context == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700455 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700456 return JNI_FALSE;
457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 EGLDisplay dpy = getDisplay(_env, display);
459 EGLSurface sdr = getSurface(_env, draw);
460 EGLSurface srd = getSurface(_env, read);
461 EGLContext ctx = getContext(_env, context);
462 return eglMakeCurrent(dpy, sdr, srd, ctx);
463}
464
Jack Palevich1badb712009-03-25 15:12:17 -0700465static jstring jni_eglQueryString(JNIEnv *_env, jobject _this, jobject display, jint name) {
466 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700467 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700468 return NULL;
469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 EGLDisplay dpy = getDisplay(_env, display);
471 const char* chars = eglQueryString(dpy, name);
472 return _env->NewStringUTF(chars);
473}
474
Jack Palevich1badb712009-03-25 15:12:17 -0700475static jboolean jni_eglSwapBuffers(JNIEnv *_env, jobject _this, jobject display, jobject surface) {
476 if (display == NULL || surface == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700477 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700478 return JNI_FALSE;
479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 EGLDisplay dpy = getDisplay(_env, display);
481 EGLSurface sur = getSurface(_env, surface);
482 return eglSwapBuffers(dpy, sur);
483}
484
Jack Palevich1badb712009-03-25 15:12:17 -0700485static jboolean jni_eglTerminate(JNIEnv *_env, jobject _this, jobject display) {
486 if (display == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700487 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700488 return JNI_FALSE;
489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 EGLDisplay dpy = getDisplay(_env, display);
491 return eglTerminate(dpy);
492}
493
Jack Palevich1badb712009-03-25 15:12:17 -0700494static jboolean jni_eglCopyBuffers(JNIEnv *_env, jobject _this, jobject display,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 jobject surface, jobject native_pixmap) {
Jack Palevich1badb712009-03-25 15:12:17 -0700496 if (display == NULL || surface == NULL || native_pixmap == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700497 jniThrowException(_env, "java/lang/IllegalArgumentException", NULL);
Jack Palevich1badb712009-03-25 15:12:17 -0700498 return JNI_FALSE;
499 }
500 // TODO: Implement this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 return JNI_FALSE;
502}
503
Jack Palevich1badb712009-03-25 15:12:17 -0700504static jboolean jni_eglWaitGL(JNIEnv *_env, jobject _this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 return eglWaitGL();
506}
507
Jack Palevich1badb712009-03-25 15:12:17 -0700508static jboolean jni_eglWaitNative(JNIEnv *_env, jobject _this, jint engine, jobject bindTarget) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 return eglWaitNative(engine);
510}
511
512
513static const char *classPathName = "com/google/android/gles_jni/EGLImpl";
514
515#define DISPLAY "Ljavax/microedition/khronos/egl/EGLDisplay;"
516#define CONTEXT "Ljavax/microedition/khronos/egl/EGLContext;"
517#define CONFIG "Ljavax/microedition/khronos/egl/EGLConfig;"
518#define SURFACE "Ljavax/microedition/khronos/egl/EGLSurface;"
519#define OBJECT "Ljava/lang/Object;"
520#define STRING "Ljava/lang/String;"
521
522static JNINativeMethod methods[] = {
523{"_nativeClassInit","()V", (void*)nativeClassInit },
524{"eglWaitGL", "()Z", (void*)jni_eglWaitGL },
525{"eglInitialize", "(" DISPLAY "[I)Z", (void*)jni_eglInitialize },
526{"eglQueryContext", "(" DISPLAY CONTEXT "I[I)Z", (void*)jni_eglQueryContext },
527{"eglQuerySurface", "(" DISPLAY SURFACE "I[I)Z", (void*)jni_eglQuerySurface },
528{"eglChooseConfig", "(" DISPLAY "[I[" CONFIG "I[I)Z", (void*)jni_eglChooseConfig },
529{"_eglCreateContext","(" DISPLAY CONFIG CONTEXT "[I)I", (void*)jni_eglCreateContext },
530{"eglGetConfigs", "(" DISPLAY "[" CONFIG "I[I)Z", (void*)jni_eglGetConfigs },
531{"eglTerminate", "(" DISPLAY ")Z", (void*)jni_eglTerminate },
532{"eglCopyBuffers", "(" DISPLAY SURFACE OBJECT ")Z", (void*)jni_eglCopyBuffers },
533{"eglWaitNative", "(I" OBJECT ")Z", (void*)jni_eglWaitNative },
534{"eglGetError", "()I", (void*)jni_eglGetError },
535{"eglGetConfigAttrib", "(" DISPLAY CONFIG "I[I)Z", (void*)jni_eglGetConfigAttrib },
536{"_eglGetDisplay", "(" OBJECT ")I", (void*)jni_eglGetDisplay },
537{"_eglGetCurrentContext", "()I", (void*)jni_eglGetCurrentContext },
538{"_eglGetCurrentDisplay", "()I", (void*)jni_eglGetCurrentDisplay },
539{"_eglGetCurrentSurface", "(I)I", (void*)jni_eglGetCurrentSurface },
540{"_eglCreatePbufferSurface","(" DISPLAY CONFIG "[I)I", (void*)jni_eglCreatePbufferSurface },
541{"_eglCreatePixmapSurface", "(" SURFACE DISPLAY CONFIG OBJECT "[I)V", (void*)jni_eglCreatePixmapSurface },
542{"_eglCreateWindowSurface", "(" DISPLAY CONFIG OBJECT "[I)I", (void*)jni_eglCreateWindowSurface },
Romain Guy8f0095c2011-05-02 17:24:22 -0700543{"_eglCreateWindowSurfaceTexture", "(" DISPLAY CONFIG "I[I)I", (void*)jni_eglCreateWindowSurfaceTexture },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544{"eglDestroyContext", "(" DISPLAY CONTEXT ")Z", (void*)jni_eglDestroyContext },
545{"eglDestroySurface", "(" DISPLAY SURFACE ")Z", (void*)jni_eglDestroySurface },
546{"eglMakeCurrent", "(" DISPLAY SURFACE SURFACE CONTEXT")Z", (void*)jni_eglMakeCurrent },
547{"eglQueryString", "(" DISPLAY "I)" STRING, (void*)jni_eglQueryString },
548{"eglSwapBuffers", "(" DISPLAY SURFACE ")Z", (void*)jni_eglSwapBuffers },
549};
550
551} // namespace android
552
553int register_com_google_android_gles_jni_EGLImpl(JNIEnv *_env)
554{
555 int err;
556 err = android::AndroidRuntime::registerNativeMethods(_env,
557 android::classPathName, android::methods, NELEM(android::methods));
558 return err;
559}