• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 #include "jni/jni_native_buffer.h"
18 #include "jni/jni_util.h"
19 
GetJBufferData(JNIEnv * env,jobject buffer,int * size)20 char* GetJBufferData(JNIEnv* env, jobject buffer, int* size) {
21   jclass base_class = env->FindClass("android/filterfw/core/NativeBuffer");
22 
23   // Get fields
24   jfieldID ptr_field = env->GetFieldID(base_class, "mDataPointer", "J");
25   jfieldID size_field = env->GetFieldID(base_class, "mSize", "I");
26 
27   // Get their values
28   char* data = reinterpret_cast<char*>(env->GetLongField(buffer, ptr_field));
29   if (size) {
30     *size = env->GetIntField(buffer, size_field);
31   }
32 
33   // Clean-up
34   env->DeleteLocalRef(base_class);
35 
36   return data;
37 }
38 
AttachDataToJBuffer(JNIEnv * env,jobject buffer,char * data,int size)39 bool AttachDataToJBuffer(JNIEnv* env, jobject buffer, char* data, int size) {
40   jclass base_class = env->FindClass("android/filterfw/core/NativeBuffer");
41 
42   // Get fields
43   jfieldID ptr_field = env->GetFieldID(base_class, "mDataPointer", "J");
44   jfieldID size_field = env->GetFieldID(base_class, "mSize", "I");
45 
46   // Set their values
47   env->SetLongField(buffer, ptr_field, reinterpret_cast<jlong>(data));
48   env->SetIntField(buffer, size_field, size);
49 
50   return true;
51 }
52 
Java_android_filterfw_core_NativeBuffer_allocate(JNIEnv * env,jobject thiz,jint size)53 jboolean Java_android_filterfw_core_NativeBuffer_allocate(JNIEnv* env, jobject thiz, jint size) {
54   char* data = new char[size];
55   return ToJBool(AttachDataToJBuffer(env, thiz, data, size));
56 }
57 
Java_android_filterfw_core_NativeBuffer_deallocate(JNIEnv * env,jobject thiz,jboolean owns_data)58 jboolean Java_android_filterfw_core_NativeBuffer_deallocate(JNIEnv* env,
59                                                             jobject thiz,
60                                                             jboolean owns_data) {
61   if (ToCppBool(owns_data)) {
62     char* data = GetJBufferData(env, thiz, NULL);
63     delete[] data;
64   }
65   return JNI_TRUE;
66 }
67 
Java_android_filterfw_core_NativeBuffer_nativeCopyTo(JNIEnv * env,jobject thiz,jobject new_buffer)68 jboolean Java_android_filterfw_core_NativeBuffer_nativeCopyTo(JNIEnv* env,
69                                                               jobject thiz,
70                                                               jobject new_buffer) {
71   // Get source buffer
72   int size;
73   char* source_data = GetJBufferData(env, thiz, &size);
74 
75   // Make copy
76   char* target_data = new char[size];
77   memcpy(target_data, source_data, size);
78 
79   // Attach it to new buffer
80   AttachDataToJBuffer(env, new_buffer, target_data, size);
81 
82   return JNI_TRUE;
83 }
84 
85