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