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