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