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#include <android_runtime/AndroidRuntime.h> 20#include <utils/misc.h> 21 22#include <assert.h> 23#include <GLES/gl.h> 24#include <GLES/glext.h> 25 26// Work around differences between the generated name and the actual name. 27 28#define glBlendEquation glBlendEquationOES 29#define glBlendEquationSeparate glBlendEquationSeparateOES 30#define glBlendFuncSeparate glBlendFuncSeparateOES 31#define glGetTexGenfv glGetTexGenfvOES 32#define glGetTexGeniv glGetTexGenivOES 33#define glGetTexGenxv glGetTexGenxvOES 34#define glTexGenf glTexGenfOES 35#define glTexGenfv glTexGenfvOES 36#define glTexGeni glTexGeniOES 37#define glTexGeniv glTexGenivOES 38#define glTexGenx glTexGenxOES 39#define glTexGenxv glTexGenxvOES 40 41 42 43/* special calls implemented in Android's GLES wrapper used to more 44 * efficiently bound-check passed arrays */ 45extern "C" { 46GL_API void GL_APIENTRY glColorPointerBounds(GLint size, GLenum type, GLsizei stride, 47 const GLvoid *ptr, GLsizei count); 48GL_API void GL_APIENTRY glNormalPointerBounds(GLenum type, GLsizei stride, 49 const GLvoid *pointer, GLsizei count); 50GL_API void GL_APIENTRY glTexCoordPointerBounds(GLint size, GLenum type, 51 GLsizei stride, const GLvoid *pointer, GLsizei count); 52GL_API void GL_APIENTRY glVertexPointerBounds(GLint size, GLenum type, 53 GLsizei stride, const GLvoid *pointer, GLsizei count); 54GL_API void GL_APIENTRY glPointSizePointerOESBounds(GLenum type, 55 GLsizei stride, const GLvoid *pointer, GLsizei count); 56GL_API void GL_APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type, 57 GLsizei stride, const GLvoid *pointer, GLsizei count); 58GL_API void GL_APIENTRY glWeightPointerOESBounds(GLint size, GLenum type, 59 GLsizei stride, const GLvoid *pointer, GLsizei count); 60} 61 62static int initialized = 0; 63 64static jclass nioAccessClass; 65static jclass bufferClass; 66static jclass OOMEClass; 67static jclass UOEClass; 68static jclass IAEClass; 69static jclass AIOOBEClass; 70static jclass G11ImplClass; 71static jmethodID getBasePointerID; 72static jmethodID getBaseArrayID; 73static jmethodID getBaseArrayOffsetID; 74static jmethodID allowIndirectBuffersID; 75static jfieldID positionID; 76static jfieldID limitID; 77static jfieldID elementSizeShiftID; 78static jfieldID haveCheckedExtensionsID; 79static jfieldID have_OES_blend_equation_separateID; 80static jfieldID have_OES_blend_subtractID; 81static jfieldID have_OES_framebuffer_objectID; 82static jfieldID have_OES_texture_cube_mapID; 83 84/* Cache method IDs each time the class is loaded. */ 85 86static void 87nativeClassInitBuffer(JNIEnv *_env) 88{ 89 jclass nioAccessClassLocal = _env->FindClass("java/nio/NIOAccess"); 90 nioAccessClass = (jclass) _env->NewGlobalRef(nioAccessClassLocal); 91 92 jclass bufferClassLocal = _env->FindClass("java/nio/Buffer"); 93 bufferClass = (jclass) _env->NewGlobalRef(bufferClassLocal); 94 95 jclass g11impClassLocal = _env->FindClass("com/google/android/gles_jni/GLImpl"); 96 G11ImplClass = (jclass) _env->NewGlobalRef(g11impClassLocal); 97 haveCheckedExtensionsID = _env->GetFieldID(G11ImplClass, "haveCheckedExtensions", "Z"); 98 have_OES_blend_equation_separateID = _env->GetFieldID(G11ImplClass, "have_OES_blend_equation_separate", "Z"); 99 have_OES_blend_subtractID = _env->GetFieldID(G11ImplClass, "have_OES_blend_subtract", "Z"); 100 have_OES_framebuffer_objectID = _env->GetFieldID(G11ImplClass, "have_OES_framebuffer_object", "Z"); 101 have_OES_texture_cube_mapID = _env->GetFieldID(G11ImplClass, "have_OES_texture_cube_map", "Z"); 102 103 getBasePointerID = _env->GetStaticMethodID(nioAccessClass, 104 "getBasePointer", "(Ljava/nio/Buffer;)J"); 105 getBaseArrayID = _env->GetStaticMethodID(nioAccessClass, 106 "getBaseArray", "(Ljava/nio/Buffer;)Ljava/lang/Object;"); 107 getBaseArrayOffsetID = _env->GetStaticMethodID(nioAccessClass, 108 "getBaseArrayOffset", "(Ljava/nio/Buffer;)I"); 109 allowIndirectBuffersID = _env->GetStaticMethodID(g11impClassLocal, 110 "allowIndirectBuffers", "(Ljava/lang/String;)Z"); 111 positionID = _env->GetFieldID(bufferClass, "position", "I"); 112 limitID = _env->GetFieldID(bufferClass, "limit", "I"); 113 elementSizeShiftID = 114 _env->GetFieldID(bufferClass, "_elementSizeShift", "I"); 115} 116 117static void 118nativeClassInit(JNIEnv *_env, jclass glImplClass) 119{ 120 nativeClassInitBuffer(_env); 121 122 jclass IAEClassLocal = 123 _env->FindClass("java/lang/IllegalArgumentException"); 124 jclass OOMEClassLocal = 125 _env->FindClass("java/lang/OutOfMemoryError"); 126 jclass UOEClassLocal = 127 _env->FindClass("java/lang/UnsupportedOperationException"); 128 jclass AIOOBEClassLocal = 129 _env->FindClass("java/lang/ArrayIndexOutOfBoundsException"); 130 131 IAEClass = (jclass) _env->NewGlobalRef(IAEClassLocal); 132 OOMEClass = (jclass) _env->NewGlobalRef(OOMEClassLocal); 133 UOEClass = (jclass) _env->NewGlobalRef(UOEClassLocal); 134 AIOOBEClass = (jclass) _env->NewGlobalRef(AIOOBEClassLocal); 135} 136 137static void * 138getPointer(JNIEnv *_env, jobject buffer, jarray *array, jint *remaining) 139{ 140 jint position; 141 jint limit; 142 jint elementSizeShift; 143 jlong pointer; 144 jint offset; 145 void *data; 146 147 position = _env->GetIntField(buffer, positionID); 148 limit = _env->GetIntField(buffer, limitID); 149 elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID); 150 *remaining = (limit - position) << elementSizeShift; 151 pointer = _env->CallStaticLongMethod(nioAccessClass, 152 getBasePointerID, buffer); 153 if (pointer != 0L) { 154 *array = NULL; 155 return (void *) (jint) pointer; 156 } 157 158 *array = (jarray) _env->CallStaticObjectMethod(nioAccessClass, 159 getBaseArrayID, buffer); 160 if (*array == NULL) { 161 return (void*) NULL; 162 } 163 offset = _env->CallStaticIntMethod(nioAccessClass, 164 getBaseArrayOffsetID, buffer); 165 data = _env->GetPrimitiveArrayCritical(*array, (jboolean *) 0); 166 167 return (void *) ((char *) data + offset); 168} 169 170static void 171releasePointer(JNIEnv *_env, jarray array, void *data, jboolean commit) 172{ 173 _env->ReleasePrimitiveArrayCritical(array, data, 174 commit ? 0 : JNI_ABORT); 175} 176 177extern "C" { 178extern char* __progname; 179} 180 181static bool 182allowIndirectBuffers(JNIEnv *_env) { 183 static jint sIndirectBufferCompatability; 184 if (sIndirectBufferCompatability == 0) { 185 jobject appName = _env->NewStringUTF(::__progname); 186 sIndirectBufferCompatability = _env->CallStaticBooleanMethod(G11ImplClass, allowIndirectBuffersID, appName) ? 2 : 1; 187 } 188 return sIndirectBufferCompatability == 2; 189} 190 191static void * 192getDirectBufferPointer(JNIEnv *_env, jobject buffer) { 193 if (!buffer) { 194 return NULL; 195 } 196 void* buf = _env->GetDirectBufferAddress(buffer); 197 if (buf) { 198 jint position = _env->GetIntField(buffer, positionID); 199 jint elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID); 200 buf = ((char*) buf) + (position << elementSizeShift); 201 } else { 202 if (allowIndirectBuffers(_env)) { 203 jarray array = 0; 204 jint remaining; 205 buf = getPointer(_env, buffer, &array, &remaining); 206 if (array) { 207 releasePointer(_env, array, buf, 0); 208 } 209 } else { 210 _env->ThrowNew(IAEClass, "Must use a native order direct Buffer"); 211 } 212 } 213 return buf; 214} 215 216static int 217getNumCompressedTextureFormats() { 218 int numCompressedTextureFormats = 0; 219 glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numCompressedTextureFormats); 220 return numCompressedTextureFormats; 221} 222 223// Check if the extension at the head of pExtensions is pExtension. Note that pExtensions is 224// terminated by either 0 or space, while pExtension is terminated by 0. 225 226static bool 227extensionEqual(const GLubyte* pExtensions, const GLubyte* pExtension) { 228 while (true) { 229 char a = *pExtensions++; 230 char b = *pExtension++; 231 bool aEnd = a == '\0' || a == ' '; 232 bool bEnd = b == '\0'; 233 if ( aEnd || bEnd) { 234 return aEnd == bEnd; 235 } 236 if ( a != b ) { 237 return false; 238 } 239 } 240} 241 242static const GLubyte* 243nextExtension(const GLubyte* pExtensions) { 244 while (true) { 245 char a = *pExtensions++; 246 if ( a == '\0') { 247 return pExtensions-1; 248 } else if ( a == ' ') { 249 return pExtensions; 250 } 251 } 252} 253 254static bool 255checkForExtension(const GLubyte* pExtensions, const GLubyte* pExtension) { 256 for (;*pExtensions != '\0'; pExtensions = nextExtension(pExtensions)) { 257 if (extensionEqual(pExtensions, pExtension)) { 258 return true; 259 } 260 } 261 return false; 262} 263 264static bool 265supportsExtension(JNIEnv *_env, jobject impl, jfieldID fieldId) { 266 if (!_env->GetBooleanField(impl, haveCheckedExtensionsID)) { 267 _env->SetBooleanField(impl, haveCheckedExtensionsID, true); 268 const GLubyte* sExtensions = glGetString(GL_EXTENSIONS); 269 _env->SetBooleanField(impl, have_OES_blend_equation_separateID, 270 checkForExtension(sExtensions, (const GLubyte*) "GL_OES_blend_equation_separate")); 271 _env->SetBooleanField(impl, have_OES_blend_subtractID, 272 checkForExtension(sExtensions, (const GLubyte*) "GL_OES_blend_subtract")); 273 _env->SetBooleanField(impl, have_OES_framebuffer_objectID, 274 checkForExtension(sExtensions, (const GLubyte*) "GL_OES_framebuffer_object")); 275 _env->SetBooleanField(impl, have_OES_texture_cube_mapID, 276 checkForExtension(sExtensions, (const GLubyte*) "GL_OES_texture_cube_map")); 277 } 278 return _env->GetBooleanField(impl, fieldId); 279} 280 281// -------------------------------------------------------------------------- 282 283