1**
2** Copyright 2009, 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 "jni.h"
20#include "JNIHelp.h"
21#include <android_runtime/AndroidRuntime.h>
22#include <utils/misc.h>
23
24#include <assert.h>
25#include <GLES/gl.h>
26#include <GLES/glext.h>
27
28/* special calls implemented in Android's GLES wrapper used to more
29 * efficiently bound-check passed arrays */
30extern "C" {
31GL_API void GL_APIENTRY glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
32        const GLvoid *ptr, GLsizei count);
33GL_API void GL_APIENTRY glNormalPointerBounds(GLenum type, GLsizei stride,
34        const GLvoid *pointer, GLsizei count);
35GL_API void GL_APIENTRY glTexCoordPointerBounds(GLint size, GLenum type,
36        GLsizei stride, const GLvoid *pointer, GLsizei count);
37GL_API void GL_APIENTRY glVertexPointerBounds(GLint size, GLenum type,
38        GLsizei stride, const GLvoid *pointer, GLsizei count);
39}
40
41static int initialized = 0;
42
43static jclass nioAccessClass;
44static jclass bufferClass;
45static jmethodID getBasePointerID;
46static jmethodID getBaseArrayID;
47static jmethodID getBaseArrayOffsetID;
48static jfieldID positionID;
49static jfieldID limitID;
50static jfieldID elementSizeShiftID;
51
52/* Cache method IDs each time the class is loaded. */
53
54static void
55nativeClassInit(JNIEnv *_env, jclass glImplClass)
56{
57    jclass nioAccessClassLocal = _env->FindClass("java/nio/NIOAccess");
58    nioAccessClass = (jclass) _env->NewGlobalRef(nioAccessClassLocal);
59
60    jclass bufferClassLocal = _env->FindClass("java/nio/Buffer");
61    bufferClass = (jclass) _env->NewGlobalRef(bufferClassLocal);
62
63    getBasePointerID = _env->GetStaticMethodID(nioAccessClass,
64            "getBasePointer", "(Ljava/nio/Buffer;)J");
65    getBaseArrayID = _env->GetStaticMethodID(nioAccessClass,
66            "getBaseArray", "(Ljava/nio/Buffer;)Ljava/lang/Object;");
67    getBaseArrayOffsetID = _env->GetStaticMethodID(nioAccessClass,
68            "getBaseArrayOffset", "(Ljava/nio/Buffer;)I");
69
70    positionID = _env->GetFieldID(bufferClass, "position", "I");
71    limitID = _env->GetFieldID(bufferClass, "limit", "I");
72    elementSizeShiftID =
73        _env->GetFieldID(bufferClass, "_elementSizeShift", "I");
74}
75
76static void *
77getPointer(JNIEnv *_env, jobject buffer, jarray *array, jint *remaining)
78{
79    jint position;
80    jint limit;
81    jint elementSizeShift;
82    jlong pointer;
83    jint offset;
84    void *data;
85
86    position = _env->GetIntField(buffer, positionID);
87    limit = _env->GetIntField(buffer, limitID);
88    elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID);
89    *remaining = (limit - position) << elementSizeShift;
90    pointer = _env->CallStaticLongMethod(nioAccessClass,
91            getBasePointerID, buffer);
92    if (pointer != 0L) {
93        *array = NULL;
94        return (void *) (jint) pointer;
95    }
96
97    *array = (jarray) _env->CallStaticObjectMethod(nioAccessClass,
98            getBaseArrayID, buffer);
99    offset = _env->CallStaticIntMethod(nioAccessClass,
100            getBaseArrayOffsetID, buffer);
101    data = _env->GetPrimitiveArrayCritical(*array, (jboolean *) 0);
102
103    return (void *) ((char *) data + offset);
104}
105
106static void
107releasePointer(JNIEnv *_env, jarray array, void *data, jboolean commit)
108{
109    _env->ReleasePrimitiveArrayCritical(array, data,
110					   commit ? 0 : JNI_ABORT);
111}
112
113static void *
114getDirectBufferPointer(JNIEnv *_env, jobject buffer) {
115    char* buf = (char*) _env->GetDirectBufferAddress(buffer);
116    if (buf) {
117        jint position = _env->GetIntField(buffer, positionID);
118        jint elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID);
119        buf += position << elementSizeShift;
120    } else {
121        jniThrowException(_env, "java/lang/IllegalArgumentException",
122                          "Must use a native order direct Buffer");
123    }
124    return (void*) buf;
125}
126
127static int
128getNumCompressedTextureFormats() {
129    int numCompressedTextureFormats = 0;
130    glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numCompressedTextureFormats);
131    return numCompressedTextureFormats;
132}
133
134// --------------------------------------------------------------------------
135