1/* EGLSurface eglCreateWindowSurface ( EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list ) */
2static jobject
3android_eglCreateWindowSurface
4  (JNIEnv *_env, jobject _this, jobject dpy, jobject config, jobject win, jintArray attrib_list_ref, jint offset) {
5    jint _exception = 0;
6    const char * _exceptionType = "";
7    const char * _exceptionMessage = "";
8    EGLSurface _returnValue = (EGLSurface) 0;
9    EGLDisplay dpy_native = (EGLDisplay) fromEGLHandle(_env, egldisplayGetHandleID, dpy);
10    EGLConfig config_native = (EGLConfig) fromEGLHandle(_env, eglconfigGetHandleID, config);
11    int attrib_list_sentinel = 0;
12    EGLint *attrib_list_base = (EGLint *) 0;
13    jint _remaining;
14    EGLint *attrib_list = (EGLint *) 0;
15    android::sp<ANativeWindow> window;
16
17    if (!attrib_list_ref) {
18        _exception = 1;
19        _exceptionType = "java/lang/IllegalArgumentException";
20        _exceptionMessage = "attrib_list == null";
21        goto exit;
22    }
23    if (offset < 0) {
24        _exception = 1;
25        _exceptionType = "java/lang/IllegalArgumentException";
26        _exceptionMessage = "offset < 0";
27        goto exit;
28    }
29    if (win == NULL) {
30not_valid_surface:
31        _exception = 1;
32        _exceptionType = "java/lang/IllegalArgumentException";
33        _exceptionMessage = "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface";
34        goto exit;
35    }
36
37    window = android::android_Surface_getNativeWindow(_env, win);
38
39    if (window == NULL)
40        goto not_valid_surface;
41
42    _remaining = _env->GetArrayLength(attrib_list_ref) - offset;
43    attrib_list_base = (EGLint *)
44        _env->GetPrimitiveArrayCritical(attrib_list_ref, (jboolean *)0);
45    attrib_list = attrib_list_base + offset;
46    attrib_list_sentinel = 0;
47    for (int i = _remaining - 1; i >= 0; i--)  {
48        if (*((EGLint*)(attrib_list + i)) == EGL_NONE){
49            attrib_list_sentinel = 1;
50            break;
51        }
52    }
53    if (attrib_list_sentinel == 0) {
54        _exception = 1;
55        _exceptionType = "java/lang/IllegalArgumentException";
56        _exceptionMessage = "attrib_list must contain EGL_NONE!";
57        goto exit;
58    }
59
60    _returnValue = eglCreateWindowSurface(
61        (EGLDisplay)dpy_native,
62        (EGLConfig)config_native,
63        (EGLNativeWindowType)window.get(),
64        (EGLint *)attrib_list
65    );
66
67exit:
68    if (attrib_list_base) {
69        _env->ReleasePrimitiveArrayCritical(attrib_list_ref, attrib_list_base,
70            JNI_ABORT);
71    }
72    if (_exception) {
73        jniThrowException(_env, _exceptionType, _exceptionMessage);
74    }
75    return toEGLHandle(_env, eglsurfaceClass, eglsurfaceConstructor, _returnValue);
76}
77
78/* EGLSurface eglCreateWindowSurface ( EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list ) */
79static jobject
80android_eglCreateWindowSurfaceTexture
81  (JNIEnv *_env, jobject _this, jobject dpy, jobject config, jobject win, jintArray attrib_list_ref, jint offset) {
82    jint _exception = 0;
83    const char * _exceptionType = "";
84    const char * _exceptionMessage = "";
85    EGLSurface _returnValue = (EGLSurface) 0;
86    EGLDisplay dpy_native = (EGLDisplay) fromEGLHandle(_env, egldisplayGetHandleID, dpy);
87    EGLConfig config_native = (EGLConfig) fromEGLHandle(_env, eglconfigGetHandleID, config);
88    int attrib_list_sentinel = 0;
89    EGLint *attrib_list_base = (EGLint *) 0;
90    jint _remaining;
91    EGLint *attrib_list = (EGLint *) 0;
92    android::sp<ANativeWindow> window;
93    android::sp<android::SurfaceTexture> surfaceTexture;
94
95    if (!attrib_list_ref) {
96        _exception = 1;
97        _exceptionType = "java/lang/IllegalArgumentException";
98        _exceptionMessage = "attrib_list == null";
99        goto exit;
100    }
101    if (offset < 0) {
102        _exception = 1;
103        _exceptionType = "java/lang/IllegalArgumentException";
104        _exceptionMessage = "offset < 0";
105        goto exit;
106    }
107    if (win == NULL) {
108not_valid_surface:
109        _exception = 1;
110        _exceptionType = "java/lang/IllegalArgumentException";
111        _exceptionMessage = "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface";
112        goto exit;
113    }
114    surfaceTexture = android::SurfaceTexture_getSurfaceTexture(_env, win);
115    window = new android::SurfaceTextureClient(surfaceTexture);
116
117    if (window == NULL)
118        goto not_valid_surface;
119
120    _remaining = _env->GetArrayLength(attrib_list_ref) - offset;
121    attrib_list_base = (EGLint *)
122        _env->GetPrimitiveArrayCritical(attrib_list_ref, (jboolean *)0);
123    attrib_list = attrib_list_base + offset;
124    attrib_list_sentinel = 0;
125    for (int i = _remaining - 1; i >= 0; i--)  {
126        if (*((EGLint*)(attrib_list + i)) == EGL_NONE){
127            attrib_list_sentinel = 1;
128            break;
129        }
130    }
131    if (attrib_list_sentinel == 0) {
132        _exception = 1;
133        _exceptionType = "java/lang/IllegalArgumentException";
134        _exceptionMessage = "attrib_list must contain EGL_NONE!";
135        goto exit;
136    }
137
138    _returnValue = eglCreateWindowSurface(
139        (EGLDisplay)dpy_native,
140        (EGLConfig)config_native,
141        (EGLNativeWindowType)window.get(),
142        (EGLint *)attrib_list
143    );
144
145exit:
146    if (attrib_list_base) {
147        _env->ReleasePrimitiveArrayCritical(attrib_list_ref, attrib_list_base,
148            JNI_ABORT);
149    }
150    if (_exception) {
151        jniThrowException(_env, _exceptionType, _exceptionMessage);
152    }
153    return toEGLHandle(_env, eglsurfaceClass, eglsurfaceConstructor, _returnValue);
154}
155