Skip to content

Commit 1ba2071

Browse files
authored
Android: Implemented AndroidNativeBufferAllocator - Deprecated AndroidBufferAllocator (#1821)
1 parent 9d52503 commit 1ba2071

File tree

8 files changed

+327
-3
lines changed

8 files changed

+327
-3
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// build file for native buffer allocator, created by pavl_g on 5/17/22.
2+
3+
// directories for native source
4+
String bufferAllocatorAndroidPath = 'src/native/jme_bufferallocator'
5+
String bufferAllocatorHeaders = 'src/native/headers'
6+
7+
//Pre-compiled libs directory
8+
def rootPath = rootProject.projectDir.absolutePath
9+
String bufferAllocatorPreCompiledLibsDir =
10+
rootPath + File.separator + "build" + File.separator + 'native' + File.separator + 'android' + File.separator + 'allocator'
11+
12+
// directories for build
13+
String bufferAllocatorBuildDir = "$buildDir" + File.separator + "bufferallocator"
14+
String bufferAllocatorJniDir = bufferAllocatorBuildDir + File.separator + "jni"
15+
String bufferAllocatorHeadersBuildDir = bufferAllocatorJniDir + File.separator + "headers"
16+
String bufferAllocatorBuildLibsDir = bufferAllocatorBuildDir + File.separator + "libs"
17+
18+
// copy native src to build dir
19+
task copyJmeBufferAllocator(type: Copy) {
20+
from file(bufferAllocatorAndroidPath)
21+
into file(bufferAllocatorJniDir)
22+
}
23+
24+
// copy native headers to build dir
25+
task copyJmeHeadersBufferAllocator(type: Copy, dependsOn: copyJmeBufferAllocator) {
26+
from file(bufferAllocatorHeaders)
27+
into file(bufferAllocatorHeadersBuildDir)
28+
}
29+
30+
// compile and build copied natives in build dir
31+
task buildBufferAllocatorNativeLib(type: Exec, dependsOn: [copyJmeBufferAllocator, copyJmeHeadersBufferAllocator]) {
32+
workingDir bufferAllocatorBuildDir
33+
executable rootProject.ndkCommandPath
34+
args "-j" + Runtime.runtime.availableProcessors()
35+
}
36+
37+
task updatePreCompiledLibsBufferAllocator(type: Copy, dependsOn: buildBufferAllocatorNativeLib) {
38+
from file(bufferAllocatorBuildLibsDir)
39+
into file(bufferAllocatorPreCompiledLibsDir)
40+
}
41+
42+
// Copy pre-compiled libs to build directory (when not building new libs)
43+
task copyPreCompiledLibsBufferAllocator(type: Copy) {
44+
from file(bufferAllocatorPreCompiledLibsDir)
45+
into file(bufferAllocatorBuildLibsDir)
46+
}
47+
48+
// ndkExists is a boolean from the build.gradle in the root project
49+
// buildNativeProjects is a string set to "true"
50+
if (ndkExists && buildNativeProjects == "true") {
51+
// build native libs and update stored pre-compiled libs to commit
52+
compileJava.dependsOn { updatePreCompiledLibsBufferAllocator }
53+
} else {
54+
// use pre-compiled native libs (not building new ones)
55+
compileJava.dependsOn { copyPreCompiledLibsBufferAllocator }
56+
}
57+
58+
// package the native object files inside the lib folder in a production jar
59+
jar.into("lib") { from bufferAllocatorBuildLibsDir }

jme3-android-native/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ apply from: file('openalsoft.gradle')
3535
// apply from: file('stb_image.gradle')
3636
// apply from: file('tremor.gradle')
3737
apply from: file('decode.gradle')
38+
apply from: file('bufferallocator.gradle')
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#
2+
# Copyright (c) 2009-2022 jMonkeyEngine
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are
7+
# met:
8+
#
9+
# * Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
#
12+
# * Redistributions in binary form must reproduce the above copyright
13+
# notice, this list of conditions and the following disclaimer in the
14+
# documentation and/or other materials provided with the distribution.
15+
#
16+
# * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
# may be used to endorse or promote products derived from this software
18+
# without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
##
33+
# Created by pavl_g on 5/17/22.
34+
# For more : https://developer.android.com/ndk/guides/android_mk.
35+
##
36+
TARGET_PLATFORM := android-19
37+
38+
LOCAL_PATH := $(call my-dir)
39+
40+
include $(CLEAR_VARS)
41+
42+
LOCAL_LDLIBS := -llog -Wl,-s
43+
44+
LOCAL_MODULE := bufferallocatorjme
45+
46+
LOCAL_C_INCLUDES := $(LOCAL_PATH)
47+
48+
LOCAL_SRC_FILES := com_jme3_util_AndroidNativeBufferAllocator.c
49+
50+
include $(BUILD_SHARED_LIBRARY)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#
2+
# Copyright (c) 2009-2022 jMonkeyEngine
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are
7+
# met:
8+
#
9+
# * Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
#
12+
# * Redistributions in binary form must reproduce the above copyright
13+
# notice, this list of conditions and the following disclaimer in the
14+
# documentation and/or other materials provided with the distribution.
15+
#
16+
# * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
# may be used to endorse or promote products derived from this software
18+
# without specific prior written permission.
19+
#
20+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
##
33+
# Created by pavl_g on 5/17/22.
34+
# For more : https://developer.android.com/ndk/guides/application_mk.
35+
##
36+
APP_PLATFORM := android-19
37+
# change this to 'debug' to see android logs
38+
APP_OPTIM := release
39+
APP_ABI := all
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2009-2022 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
/**
34+
* @file com_jme3_util_AndroidNativeBufferAllocator.c
35+
* @author pavl_g.
36+
* @brief Creates and releases direct byte buffers for {com.jme3.util.AndroidNativeBufferAllocator}.
37+
* @date 2022-05-17.
38+
* @note
39+
* Find more at :
40+
* - JNI Direct byte buffers : https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#NewDirectByteBuffer.
41+
* - JNI Get Direct byte buffer : https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#GetDirectBufferAddress.
42+
* - GNU Basic allocation : https://www.gnu.org/software/libc/manual/html_node/Basic-Allocation.html.
43+
* - GNU Allocating Cleared Space : https://www.gnu.org/software/libc/manual/html_node/Allocating-Cleared-Space.html.
44+
* - GNU No Memory error : https://www.gnu.org/software/libc/manual/html_node/Error-Codes.html#index-ENOMEM.
45+
* - GNU Freeing memory : https://www.gnu.org/software/libc/manual/html_node/Freeing-after-Malloc.html.
46+
* - Android logging : https://developer.android.com/ndk/reference/group/logging.
47+
* - Android logging example : https://github.com/android/ndk-samples/blob/7a8ff4c5529fce6ec4c5796efbe773f5d0e569cc/hello-libs/app/src/main/cpp/hello-libs.cpp#L25-L26.
48+
*/
49+
50+
#include "headers/com_jme3_util_AndroidNativeBufferAllocator.h"
51+
#include <stdlib.h>
52+
#include <stdbool.h>
53+
#include <errno.h>
54+
55+
#ifndef NDEBUG
56+
#include <android/log.h>
57+
#define LOG(LOG_ID, ...) __android_log_print(LOG_ID, \
58+
"AndroidNativeBufferAllocator", ##__VA_ARGS__);
59+
#else
60+
#define LOG(...)
61+
#endif
62+
63+
bool isDeviceOutOfMemory(void*);
64+
65+
/**
66+
* @brief Tests if the device is out of memory.
67+
*
68+
* @return true if the buffer to allocate is a NULL pointer and the errno is ENOMEM (Error-no-memory).
69+
* @return false otherwise.
70+
*/
71+
bool isDeviceOutOfMemory(void* buffer) {
72+
return buffer == NULL && errno == ENOMEM;
73+
}
74+
75+
JNIEXPORT void JNICALL Java_com_jme3_util_AndroidNativeBufferAllocator_releaseDirectByteBuffer
76+
(JNIEnv * env, jobject object, jobject bufferObject)
77+
{
78+
void* buffer = (*env)->GetDirectBufferAddress(env, bufferObject);
79+
// deallocates the buffer pointer
80+
free(buffer);
81+
// log the destruction by mem address
82+
LOG(ANDROID_LOG_INFO, "Buffer released (mem_address, size) -> (%p, %lu)", buffer, sizeof(buffer));
83+
// avoid accessing this memory space by resetting the memory address
84+
buffer = NULL;
85+
LOG(ANDROID_LOG_INFO, "Buffer mem_address formatted (mem_address, size) -> (%p, %u)", buffer, sizeof(buffer));
86+
}
87+
88+
JNIEXPORT jobject JNICALL Java_com_jme3_util_AndroidNativeBufferAllocator_createDirectByteBuffer
89+
(JNIEnv * env, jobject object, jlong size)
90+
{
91+
void* buffer = calloc(1, size);
92+
if (isDeviceOutOfMemory(buffer)) {
93+
LOG(ANDROID_LOG_FATAL, "Device is out of memory exiting with %u", errno);
94+
exit(errno);
95+
} else {
96+
LOG(ANDROID_LOG_INFO, "Buffer created successfully (mem_address, size) -> (%p %lli)", buffer, size);
97+
}
98+
return (*env)->NewDirectByteBuffer(env, buffer, size);
99+
}

jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
import com.jme3.renderer.android.AndroidGL;
5555
import com.jme3.renderer.opengl.*;
5656
import com.jme3.system.*;
57-
import com.jme3.util.AndroidBufferAllocator;
5857
import com.jme3.util.BufferAllocatorFactory;
58+
import com.jme3.util.AndroidNativeBufferAllocator;
5959
import java.util.concurrent.atomic.AtomicBoolean;
6060
import java.util.logging.Level;
6161
import java.util.logging.Logger;
@@ -82,7 +82,7 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex
8282
final String implementation = BufferAllocatorFactory.PROPERTY_BUFFER_ALLOCATOR_IMPLEMENTATION;
8383

8484
if (System.getProperty(implementation) == null) {
85-
System.setProperty(implementation, AndroidBufferAllocator.class.getName());
85+
System.setProperty(implementation, AndroidNativeBufferAllocator.class.getName());
8686
}
8787
}
8888

jme3-android/src/main/java/com/jme3/util/AndroidBufferAllocator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2019 jMonkeyEngine
2+
* Copyright (c) 2009-2022 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -40,7 +40,9 @@
4040

4141
/**
4242
* @author Jesus Oliver
43+
* @deprecated implemented {@link AndroidNativeBufferAllocator} instead.
4344
*/
45+
@Deprecated
4446
public class AndroidBufferAllocator implements BufferAllocator {
4547

4648
// We make use of the ReflectionAllocator to remove the inner buffer
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2009-2022 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package com.jme3.util;
33+
34+
import java.nio.Buffer;
35+
import java.nio.ByteBuffer;
36+
37+
/**
38+
* Allocates and destroys direct byte buffers using native code.
39+
*
40+
* @author pavl_g.
41+
*/
42+
public final class AndroidNativeBufferAllocator implements BufferAllocator {
43+
44+
static {
45+
System.loadLibrary("bufferallocatorjme");
46+
}
47+
48+
@Override
49+
public void destroyDirectBuffer(Buffer toBeDestroyed) {
50+
releaseDirectByteBuffer(toBeDestroyed);
51+
}
52+
53+
@Override
54+
public ByteBuffer allocate(int size) {
55+
return createDirectByteBuffer(size);
56+
}
57+
58+
/**
59+
* Releases the memory of a direct buffer using a buffer object reference.
60+
*
61+
* @param buffer the buffer reference to release its memory.
62+
* @see AndroidNativeBufferAllocator#destroyDirectBuffer(Buffer)
63+
*/
64+
private native void releaseDirectByteBuffer(Buffer buffer);
65+
66+
/**
67+
* Creates a new direct byte buffer explicitly with a specific size.
68+
*
69+
* @param size the byte buffer size used for allocating the buffer.
70+
* @return a new direct byte buffer object.
71+
* @see AndroidNativeBufferAllocator#allocate(int)
72+
*/
73+
private native ByteBuffer createDirectByteBuffer(long size);
74+
}

0 commit comments

Comments
 (0)