blob: 9b29a4410920dbd73f03ba9c58f6116e8fe391e5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001**
Jack Palevich27f80022009-04-15 19:13:17 -07002** Copyright 2009, The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003**
Elliott Hughes24ce5fb2011-04-08 20:01:01 -07004** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007**
Elliott Hughes24ce5fb2011-04-08 20:01:01 -07008** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009**
Elliott Hughes24ce5fb2011-04-08 20:01:01 -070010** 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014** limitations under the License.
15*/
16
17// This source file is automatically generated
18
Elliott Hughes24ce5fb2011-04-08 20:01:01 -070019#include "jni.h"
20#include "JNIHelp.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include <android_runtime/AndroidRuntime.h>
22#include <utils/misc.h>
23
24#include <assert.h>
25#include <GLES/gl.h>
Jack Palevichbe509c92009-05-07 09:52:14 -070026#include <GLES/glext.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Jack Palevichbe6eac82009-12-08 15:43:51 +080028/* 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037static int initialized = 0;
38
39static jclass nioAccessClass;
40static jclass bufferClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041static 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
Jack Palevich27f80022009-04-15 19:13:17 -070050static void
Elliott Hughes24ce5fb2011-04-08 20:01:01 -070051nativeClassInit(JNIEnv *_env, jclass glImplClass)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052{
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073static void *
74getPointer(JNIEnv *_env, jobject buffer, jarray *array, jint *remaining)
75{
76 jint position;
77 jint limit;
78 jint elementSizeShift;
79 jlong pointer;
80 jint offset;
81 void *data;
82
83 position = _env->GetIntField(buffer, positionID);
84 limit = _env->GetIntField(buffer, limitID);
85 elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID);
86 *remaining = (limit - position) << elementSizeShift;
87 pointer = _env->CallStaticLongMethod(nioAccessClass,
88 getBasePointerID, buffer);
89 if (pointer != 0L) {
90 *array = NULL;
91 return (void *) (jint) pointer;
92 }
Elliott Hughes24ce5fb2011-04-08 20:01:01 -070093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 *array = (jarray) _env->CallStaticObjectMethod(nioAccessClass,
95 getBaseArrayID, buffer);
96 offset = _env->CallStaticIntMethod(nioAccessClass,
97 getBaseArrayOffsetID, buffer);
98 data = _env->GetPrimitiveArrayCritical(*array, (jboolean *) 0);
Elliott Hughes24ce5fb2011-04-08 20:01:01 -070099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 return (void *) ((char *) data + offset);
101}
102
103
104static void
105releasePointer(JNIEnv *_env, jarray array, void *data, jboolean commit)
106{
107 _env->ReleasePrimitiveArrayCritical(array, data,
108 commit ? 0 : JNI_ABORT);
109}
110
Jack Palevichbe6eac82009-12-08 15:43:51 +0800111static void *
112getDirectBufferPointer(JNIEnv *_env, jobject buffer) {
113 char* buf = (char*) _env->GetDirectBufferAddress(buffer);
114 if (buf) {
115 jint position = _env->GetIntField(buffer, positionID);
116 jint elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID);
117 buf += position << elementSizeShift;
118 } else {
Elliott Hughes24ce5fb2011-04-08 20:01:01 -0700119 jniThrowException(_env, "java/lang/IllegalArgumentException",
120 "Must use a native order direct Buffer");
Jack Palevichbe6eac82009-12-08 15:43:51 +0800121 }
122 return (void*) buf;
123}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124// --------------------------------------------------------------------------