1 **
2 ** Copyright 2006, 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 <GLES/gl.h>
29 #include <GLES/glext.h>
30 
31 // Work around differences between the generated name and the actual name.
32 
33 #define glBlendEquation glBlendEquationOES
34 #define glBlendEquationSeparate glBlendEquationSeparateOES
35 #define glBlendFuncSeparate glBlendFuncSeparateOES
36 #define glGetTexGenfv glGetTexGenfvOES
37 #define glGetTexGeniv glGetTexGenivOES
38 #define glGetTexGenxv glGetTexGenxvOES
39 #define glTexGenf glTexGenfOES
40 #define glTexGenfv glTexGenfvOES
41 #define glTexGeni glTexGeniOES
42 #define glTexGeniv glTexGenivOES
43 #define glTexGenx glTexGenxOES
44 #define glTexGenxv glTexGenxvOES
45 
46 
47 
48 /* special calls implemented in Android's GLES wrapper used to more
49  * efficiently bound-check passed arrays */
50 extern "C" {
51 GL_API void GL_APIENTRY glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
52         const GLvoid *ptr, GLsizei count);
53 GL_API void GL_APIENTRY glNormalPointerBounds(GLenum type, GLsizei stride,
54         const GLvoid *pointer, GLsizei count);
55 GL_API void GL_APIENTRY glTexCoordPointerBounds(GLint size, GLenum type,
56         GLsizei stride, const GLvoid *pointer, GLsizei count);
57 GL_API void GL_APIENTRY glVertexPointerBounds(GLint size, GLenum type,
58         GLsizei stride, const GLvoid *pointer, GLsizei count);
59 GL_API void GL_APIENTRY glPointSizePointerOESBounds(GLenum type,
60         GLsizei stride, const GLvoid *pointer, GLsizei count);
61 GL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type,
62         GLsizei stride, const GLvoid *pointer, GLsizei count);
63 GL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type,
64         GLsizei stride, const GLvoid *pointer, GLsizei count);
65 }
66 
67 static jclass G11ImplClass;
68 static jfieldID haveCheckedExtensionsID;
69 static jfieldID have_OES_blend_equation_separateID;
70 static jfieldID have_OES_blend_subtractID;
71 static jfieldID have_OES_framebuffer_objectID;
72 static jfieldID have_OES_texture_cube_mapID;
73 
74 /* Cache method IDs each time the class is loaded. */
75 
76 static void
77 nativeClassInit(JNIEnv *_env, jclass glImplClass)
78 {
79     jclass g11impClassLocal = _env->FindClass("com/google/android/gles_jni/GLImpl");
80     G11ImplClass = (jclass) _env->NewGlobalRef(g11impClassLocal);
81     haveCheckedExtensionsID =  _env->GetFieldID(G11ImplClass, "haveCheckedExtensions", "Z");
82     have_OES_blend_equation_separateID =  _env->GetFieldID(G11ImplClass, "have_OES_blend_equation_separate", "Z");
83     have_OES_blend_subtractID =  _env->GetFieldID(G11ImplClass, "have_OES_blend_subtract", "Z");
84     have_OES_framebuffer_objectID =  _env->GetFieldID(G11ImplClass, "have_OES_framebuffer_object", "Z");
85     have_OES_texture_cube_mapID =  _env->GetFieldID(G11ImplClass, "have_OES_texture_cube_map", "Z");
86 }
87 
88 static void *
89 getPointer(JNIEnv *_env, jobject buffer, jarray *array, jint *remaining, jint *offset)
90 {
91     jint position;
92     jint limit;
93     jint elementSizeShift;
94     jlong pointer;
95 
96     pointer = jniGetNioBufferFields(_env, buffer, &position, &limit, &elementSizeShift);
97     *remaining = (limit - position) << elementSizeShift;
98     if (pointer != 0L) {
99         *array = nullptr;
100         pointer += position << elementSizeShift;
101         return reinterpret_cast<void*>(pointer);
102     }
103 
104     *array = jniGetNioBufferBaseArray(_env, buffer);
105     *offset = jniGetNioBufferBaseArrayOffset(_env, buffer);
106     return nullptr;
107 }
108 
109 static void
110 releasePointer(JNIEnv *_env, jarray array, void *data, jboolean commit)
111 {
112     _env->ReleasePrimitiveArrayCritical(array, data,
113 					   commit ? 0 : JNI_ABORT);
114 }
115 
116 extern "C" {
117 extern char*  __progname;
118 }
119 
120 static void *
121 getDirectBufferPointer(JNIEnv *_env, jobject buffer) {
122     if (buffer == nullptr) {
123         return nullptr;
124     }
125 
126     jint position;
127     jint limit;
128     jint elementSizeShift;
129     jlong pointer;
130     pointer = jniGetNioBufferFields(_env, buffer, &position, &limit, &elementSizeShift);
131     if (pointer == 0) {
132         jniThrowException(_env, "java/lang/IllegalArgumentException",
133                           "Must use a native order direct Buffer");
134         return nullptr;
135     }
136     pointer += position << elementSizeShift;
137     return reinterpret_cast<void*>(pointer);
138 }
139 
140 static int
141 getNumCompressedTextureFormats() {
142     int numCompressedTextureFormats = 0;
143     glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numCompressedTextureFormats);
144     return numCompressedTextureFormats;
145 }
146 
147 // Check if the extension at the head of pExtensions is pExtension. Note that pExtensions is
148 // terminated by either 0 or space, while pExtension is terminated by 0.
149 
150 static bool
151 extensionEqual(const GLubyte* pExtensions, const GLubyte* pExtension) {
152     while (true) {
153         char a = *pExtensions++;
154         char b = *pExtension++;
155         bool aEnd = a == '\0' || a == ' ';
156         bool bEnd = b == '\0';
157         if ( aEnd || bEnd) {
158             return aEnd == bEnd;
159         }
160         if ( a != b ) {
161             return false;
162         }
163     }
164 }
165 
166 static const GLubyte*
167 nextExtension(const GLubyte* pExtensions) {
168     while (true) {
169         char a = *pExtensions++;
170         if ( a == '\0') {
171             return pExtensions-1;
172         } else if ( a == ' ') {
173             return pExtensions;
174         }
175     }
176 }
177 
178 static bool
179 checkForExtension(const GLubyte* pExtensions, const GLubyte* pExtension) {
180     for (;*pExtensions != '\0'; pExtensions = nextExtension(pExtensions)) {
181         if (extensionEqual(pExtensions, pExtension)) {
182             return true;
183         }
184     }
185     return false;
186 }
187 
188 static bool
189 supportsExtension(JNIEnv *_env, jobject impl, jfieldID fieldId) {
190     if (!_env->GetBooleanField(impl, haveCheckedExtensionsID)) {
191         _env->SetBooleanField(impl, haveCheckedExtensionsID, true);
192         const GLubyte* sExtensions = glGetString(GL_EXTENSIONS);
193         _env->SetBooleanField(impl, have_OES_blend_equation_separateID,
194             checkForExtension(sExtensions, (const GLubyte*) "GL_OES_blend_equation_separate"));
195         _env->SetBooleanField(impl, have_OES_blend_subtractID,
196             checkForExtension(sExtensions, (const GLubyte*) "GL_OES_blend_subtract"));
197         _env->SetBooleanField(impl, have_OES_framebuffer_objectID,
198             checkForExtension(sExtensions, (const GLubyte*) "GL_OES_framebuffer_object"));
199         _env->SetBooleanField(impl, have_OES_texture_cube_mapID,
200             checkForExtension(sExtensions, (const GLubyte*) "GL_OES_texture_cube_map"));
201     }
202     return _env->GetBooleanField(impl, fieldId);
203 }
204 
205 // --------------------------------------------------------------------------
206