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        if (offset < 0) {
19            _exception = 1;
20            _exceptionType = "java/lang/IllegalArgumentException";
21            _exceptionMessage = "offset < 0";
22            goto exit;
23        }
24        _remaining = _env->GetArrayLength(attrib_list_ref) - offset;
25        attrib_list_base = (EGLint *)
26            _env->GetIntArrayElements(attrib_list_ref, (jboolean *)0);
27        attrib_list = attrib_list_base + offset;
28        attrib_list_sentinel = 0;
29        for (int i = _remaining - 1; i >= 0; i--)  {
30            if (*((EGLint*)(attrib_list + i)) == EGL_NONE){
31                attrib_list_sentinel = 1;
32                break;
33            }
34        }
35        if (attrib_list_sentinel == 0) {
36            _exception = 1;
37            _exceptionType = "java/lang/IllegalArgumentException";
38            _exceptionMessage = "attrib_list must contain EGL_NONE!";
39            goto exit;
40        }
41    }
42
43    if (win == NULL) {
44not_valid_surface:
45        _exception = 1;
46        _exceptionType = "java/lang/IllegalArgumentException";
47        _exceptionMessage = "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface";
48        goto exit;
49    }
50
51    window = android::android_view_Surface_getNativeWindow(_env, win);
52
53    if (window == NULL)
54        goto not_valid_surface;
55
56    _returnValue = eglCreateWindowSurface(
57        (EGLDisplay)dpy_native,
58        (EGLConfig)config_native,
59        (EGLNativeWindowType)window.get(),
60        (EGLint *)attrib_list
61    );
62
63exit:
64    if (attrib_list_base) {
65        _env->ReleaseIntArrayElements(attrib_list_ref, attrib_list_base,
66            JNI_ABORT);
67    }
68    if (_exception) {
69        jniThrowException(_env, _exceptionType, _exceptionMessage);
70    }
71    return toEGLHandle(_env, eglsurfaceClass, eglsurfaceConstructor, _returnValue);
72}
73
74/* EGLSurface eglCreateWindowSurface ( EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list ) */
75static jobject
76android_eglCreateWindowSurfaceTexture
77  (JNIEnv *_env, jobject _this, jobject dpy, jobject config, jobject win, jintArray attrib_list_ref, jint offset) {
78    jint _exception = 0;
79    const char * _exceptionType = "";
80    const char * _exceptionMessage = "";
81    EGLSurface _returnValue = (EGLSurface) 0;
82    EGLDisplay dpy_native = (EGLDisplay) fromEGLHandle(_env, egldisplayGetHandleID, dpy);
83    EGLConfig config_native = (EGLConfig) fromEGLHandle(_env, eglconfigGetHandleID, config);
84    int attrib_list_sentinel = 0;
85    EGLint *attrib_list_base = (EGLint *) 0;
86    jint _remaining;
87    EGLint *attrib_list = (EGLint *) 0;
88    android::sp<ANativeWindow> window;
89    android::sp<android::IGraphicBufferProducer> producer;
90
91    if (!attrib_list_ref) {
92        _exception = 1;
93        _exceptionType = "java/lang/IllegalArgumentException";
94        _exceptionMessage = "attrib_list == null";
95        goto exit;
96    }
97    if (offset < 0) {
98        _exception = 1;
99        _exceptionType = "java/lang/IllegalArgumentException";
100        _exceptionMessage = "offset < 0";
101        goto exit;
102    }
103    if (win == NULL) {
104not_valid_surface:
105        _exception = 1;
106        _exceptionType = "java/lang/IllegalArgumentException";
107        _exceptionMessage = "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface";
108        goto exit;
109    }
110    producer = android::SurfaceTexture_getProducer(_env, win);
111
112    if (producer == NULL)
113        goto not_valid_surface;
114
115    window = new android::Surface(producer, true);
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->GetIntArrayElements(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->ReleaseIntArrayElements(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