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 <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28static int initialized = 0;
29
30static jclass nioAccessClass;
31static jclass bufferClass;
32static jmethodID getBasePointerID;
33static jmethodID getBaseArrayID;
34static jmethodID getBaseArrayOffsetID;
35static jfieldID positionID;
36static jfieldID limitID;
37static jfieldID elementSizeShiftID;
38
39/* Cache method IDs each time the class is loaded. */
40
41static void
42nativeClassInit(JNIEnv *_env, jclass glImplClass)
43{
44    jclass nioAccessClassLocal = _env->FindClass("java/nio/NIOAccess");
45    nioAccessClass = (jclass) _env->NewGlobalRef(nioAccessClassLocal);
46
47    jclass bufferClassLocal = _env->FindClass("java/nio/Buffer");
48    bufferClass = (jclass) _env->NewGlobalRef(bufferClassLocal);
49
50    getBasePointerID = _env->GetStaticMethodID(nioAccessClass,
51            "getBasePointer", "(Ljava/nio/Buffer;)J");
52    getBaseArrayID = _env->GetStaticMethodID(nioAccessClass,
53            "getBaseArray", "(Ljava/nio/Buffer;)Ljava/lang/Object;");
54    getBaseArrayOffsetID = _env->GetStaticMethodID(nioAccessClass,
55            "getBaseArrayOffset", "(Ljava/nio/Buffer;)I");
56
57    positionID = _env->GetFieldID(bufferClass, "position", "I");
58    limitID = _env->GetFieldID(bufferClass, "limit", "I");
59    elementSizeShiftID =
60        _env->GetFieldID(bufferClass, "_elementSizeShift", "I");
61}
62
63
64static void *
65getPointer(JNIEnv *_env, jobject buffer, jarray *array, jint *remaining, jint *offset)
66{
67    jint position;
68    jint limit;
69    jint elementSizeShift;
70    jlong pointer;
71
72    position = _env->GetIntField(buffer, positionID);
73    limit = _env->GetIntField(buffer, limitID);
74    elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID);
75    *remaining = (limit - position) << elementSizeShift;
76    pointer = _env->CallStaticLongMethod(nioAccessClass,
77            getBasePointerID, buffer);
78    if (pointer != 0L) {
79        *array = NULL;
80        return (void *) (jint) pointer;
81    }
82
83    *array = (jarray) _env->CallStaticObjectMethod(nioAccessClass,
84            getBaseArrayID, buffer);
85    *offset = _env->CallStaticIntMethod(nioAccessClass,
86            getBaseArrayOffsetID, buffer);
87
88    return NULL;
89}
90
91
92static void
93releasePointer(JNIEnv *_env, jarray array, void *data, jboolean commit)
94{
95    _env->ReleasePrimitiveArrayCritical(array, data,
96					   commit ? 0 : JNI_ABORT);
97}
98
99static void *
100getDirectBufferPointer(JNIEnv *_env, jobject buffer) {
101    char* buf = (char*) _env->GetDirectBufferAddress(buffer);
102    if (buf) {
103        jint position = _env->GetIntField(buffer, positionID);
104        jint elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID);
105        buf += position << elementSizeShift;
106    } else {
107        jniThrowException(_env, "java/lang/IllegalArgumentException",
108                          "Must use a native order direct Buffer");
109    }
110    return (void*) buf;
111}
112
113static int
114getNumCompressedTextureFormats() {
115    int numCompressedTextureFormats = 0;
116    glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numCompressedTextureFormats);
117    return numCompressedTextureFormats;
118}
119
120static void glVertexAttribPointerBounds(GLuint indx, GLint size, GLenum type,
121        GLboolean normalized, GLsizei stride, const GLvoid *pointer, GLsizei count) {
122    glVertexAttribPointer(indx, size, type, normalized, stride, pointer);
123}
124
125// --------------------------------------------------------------------------
126