1 /*
2 ** Copyright 2018, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 
17 // This source file is automatically generated
18 
19 #pragma GCC diagnostic ignored "-Wunused-variable"
20 #pragma GCC diagnostic ignored "-Wunused-function"
21 
22 #include "jni.h"
23 #include <nativehelper/JNIPlatformHelp.h>
24 #include <android_runtime/AndroidRuntime.h>
25 #include <utils/misc.h>
26 
27 #include <assert.h>
28 #include <vector>
29 #include <EGL/egl.h>
30 
31 #include <ui/ANativeObjectBase.h>
32 
33 // classes from EGL 1.4
34 static jclass egldisplayClass;
35 static jclass eglsurfaceClass;
36 static jclass eglconfigClass;
37 static jclass eglcontextClass;
38 
39 static jmethodID egldisplayGetHandleID;
40 static jmethodID eglconfigGetHandleID;
41 static jmethodID eglcontextGetHandleID;
42 static jmethodID eglsurfaceGetHandleID;
43 
44 static jmethodID egldisplayConstructor;
45 static jmethodID eglcontextConstructor;
46 static jmethodID eglsurfaceConstructor;
47 static jmethodID eglconfigConstructor;
48 
49 static jobject eglNoContextObject;
50 static jobject eglNoDisplayObject;
51 static jobject eglNoSurfaceObject;
52 
53 // classes from EGL 1.5
54 static jclass eglimageClass;
55 static jclass eglsyncClass;
56 
57 static jmethodID eglimageGetHandleID;
58 static jmethodID eglsyncGetHandleID;
59 
60 static jmethodID eglimageConstructor;
61 static jmethodID eglsyncConstructor;
62 
63 static jobject eglNoImageObject;
64 static jobject eglNoSyncObject;
65 
66 /* Cache method IDs each time the class is loaded. */
67 
68 static void
nativeClassInit(JNIEnv * _env,jclass glImplClass)69 nativeClassInit(JNIEnv *_env, jclass glImplClass)
70 {
71     // EGL 1.4 Init
72     jclass eglconfigClassLocal = _env->FindClass("android/opengl/EGLConfig");
73     eglconfigClass = (jclass) _env->NewGlobalRef(eglconfigClassLocal);
74     jclass eglcontextClassLocal = _env->FindClass("android/opengl/EGLContext");
75     eglcontextClass = (jclass) _env->NewGlobalRef(eglcontextClassLocal);
76     jclass egldisplayClassLocal = _env->FindClass("android/opengl/EGLDisplay");
77     egldisplayClass = (jclass) _env->NewGlobalRef(egldisplayClassLocal);
78     jclass eglsurfaceClassLocal = _env->FindClass("android/opengl/EGLSurface");
79     eglsurfaceClass = (jclass) _env->NewGlobalRef(eglsurfaceClassLocal);
80 
81     eglconfigGetHandleID = _env->GetMethodID(eglconfigClass, "getNativeHandle", "()J");
82     eglcontextGetHandleID = _env->GetMethodID(eglcontextClass, "getNativeHandle", "()J");
83     egldisplayGetHandleID = _env->GetMethodID(egldisplayClass, "getNativeHandle", "()J");
84     eglsurfaceGetHandleID = _env->GetMethodID(eglsurfaceClass, "getNativeHandle", "()J");
85 
86     eglconfigConstructor = _env->GetMethodID(eglconfigClass, "<init>", "(J)V");
87     eglcontextConstructor = _env->GetMethodID(eglcontextClass, "<init>", "(J)V");
88     egldisplayConstructor = _env->GetMethodID(egldisplayClass, "<init>", "(J)V");
89     eglsurfaceConstructor = _env->GetMethodID(eglsurfaceClass, "<init>", "(J)V");
90 
91     jobject localeglNoContextObject = _env->NewObject(eglcontextClass, eglcontextConstructor, reinterpret_cast<jlong>(EGL_NO_CONTEXT));
92     eglNoContextObject = _env->NewGlobalRef(localeglNoContextObject);
93     jobject localeglNoDisplayObject = _env->NewObject(egldisplayClass, egldisplayConstructor, reinterpret_cast<jlong>(EGL_NO_DISPLAY));
94     eglNoDisplayObject = _env->NewGlobalRef(localeglNoDisplayObject);
95     jobject localeglNoSurfaceObject = _env->NewObject(eglsurfaceClass, eglsurfaceConstructor, reinterpret_cast<jlong>(EGL_NO_SURFACE));
96     eglNoSurfaceObject = _env->NewGlobalRef(localeglNoSurfaceObject);
97 
98     jclass eglClass = _env->FindClass("android/opengl/EGL15");
99     jfieldID noContextFieldID = _env->GetStaticFieldID(eglClass, "EGL_NO_CONTEXT", "Landroid/opengl/EGLContext;");
100     _env->SetStaticObjectField(eglClass, noContextFieldID, eglNoContextObject);
101 
102     jfieldID noDisplayFieldID = _env->GetStaticFieldID(eglClass, "EGL_NO_DISPLAY", "Landroid/opengl/EGLDisplay;");
103     _env->SetStaticObjectField(eglClass, noDisplayFieldID, eglNoDisplayObject);
104 
105     jfieldID noSurfaceFieldID = _env->GetStaticFieldID(eglClass, "EGL_NO_SURFACE", "Landroid/opengl/EGLSurface;");
106     _env->SetStaticObjectField(eglClass, noSurfaceFieldID, eglNoSurfaceObject);
107 
108     // EGL 1.5 init
109     jclass eglimageClassLocal = _env->FindClass("android/opengl/EGLImage");
110     eglimageClass = (jclass) _env->NewGlobalRef(eglimageClassLocal);
111     jclass eglsyncClassLocal = _env->FindClass("android/opengl/EGLSync");
112     eglsyncClass = (jclass) _env->NewGlobalRef(eglsyncClassLocal);
113 
114     eglimageGetHandleID = _env->GetMethodID(eglimageClass, "getNativeHandle", "()J");
115     eglsyncGetHandleID = _env->GetMethodID(eglsyncClass, "getNativeHandle", "()J");
116 
117     eglimageConstructor = _env->GetMethodID(eglimageClass, "<init>", "(J)V");
118     eglsyncConstructor = _env->GetMethodID(eglsyncClass, "<init>", "(J)V");
119 
120     jfieldID noImageFieldID = _env->GetStaticFieldID(eglClass, "EGL_NO_IMAGE", "Landroid/opengl/EGLImage;");
121     _env->SetStaticObjectField(eglClass, noImageFieldID, eglNoImageObject);
122 
123     jfieldID noSyncFieldID = _env->GetStaticFieldID(eglClass, "EGL_NO_SYNC", "Landroid/opengl/EGLSync;");
124     _env->SetStaticObjectField(eglClass, noSyncFieldID, eglNoSyncObject);
125 }
126 
127 static void *
getPointer(JNIEnv * _env,jobject buffer,jarray * array,jint * remaining,jint * offset)128 getPointer(JNIEnv *_env, jobject buffer, jarray *array, jint *remaining, jint *offset)
129 {
130     jint position;
131     jint limit;
132     jint elementSizeShift;
133     jlong pointer;
134 
135     pointer = jniGetNioBufferFields(_env, buffer, &position, &limit, &elementSizeShift);
136     *remaining = (limit - position) << elementSizeShift;
137     if (pointer != 0L) {
138         *array = nullptr;
139         pointer += position << elementSizeShift;
140         return reinterpret_cast<void*>(pointer);
141     }
142 
143     *array = jniGetNioBufferBaseArray(_env, buffer);
144     *offset = jniGetNioBufferBaseArrayOffset(_env, buffer);
145     return nullptr;
146 }
147 
148 static void
releasePointer(JNIEnv * _env,jarray array,void * data,jboolean commit)149 releasePointer(JNIEnv *_env, jarray array, void *data, jboolean commit)
150 {
151     _env->ReleasePrimitiveArrayCritical(array, data,
152                        commit ? 0 : JNI_ABORT);
153 }
154 
155 static void *
fromEGLHandle(JNIEnv * _env,jmethodID mid,jobject obj)156 fromEGLHandle(JNIEnv *_env, jmethodID mid, jobject obj) {
157     if (obj == NULL) {
158         jniThrowException(_env, "java/lang/IllegalArgumentException",
159                           "Object is set to null.");
160         return nullptr;
161     }
162 
163     jlong handle = _env->CallLongMethod(obj, mid);
164     return reinterpret_cast<void*>(handle);
165 }
166 
167 static jobject
toEGLHandle(JNIEnv * _env,jclass cls,jmethodID con,void * handle)168 toEGLHandle(JNIEnv *_env, jclass cls, jmethodID con, void *handle) {
169     if (cls == eglimageClass && (EGLImage)handle == EGL_NO_IMAGE) {
170         return eglNoImageObject;
171     }
172 
173     return _env->NewObject(cls, con, reinterpret_cast<jlong>(handle));
174 }
175 
176 struct WrappedEGLAttribs {
177 private:
178     std::vector<EGLAttrib> backing; // only for 32-bit
179 public:
180     EGLAttrib *attribs;
WrappedEGLAttribsWrappedEGLAttribs181     WrappedEGLAttribs(): attribs(nullptr) { };
initWrappedEGLAttribs182     void init(jlong *array, jint size) {
183         if (sizeof(EGLAttrib) != sizeof(jlong)) {
184             for (jint i = 0; i < size; ++i) {
185                 backing.push_back(array[i]);
186             }
187             attribs = backing.data();
188         } else {
189             attribs = (EGLAttrib*)array;
190         }
191     }
192 };
193 
194 // --------------------------------------------------------------------------
195