Merge "Pass models to soundtrigger middleware with shared memory" into rvc-dev am: 2cba965676 am: e76d531782 am: 2df89417f7 am: 46cdc33950
Change-Id: Ie4e341a5b1eba31783e324fb27ac180cb4e54cc7
This commit is contained in:
@@ -32,7 +32,11 @@ import android.media.soundtrigger_middleware.RecognitionMode;
|
||||
import android.media.soundtrigger_middleware.SoundModel;
|
||||
import android.media.soundtrigger_middleware.SoundTriggerModuleDescriptor;
|
||||
import android.media.soundtrigger_middleware.SoundTriggerModuleProperties;
|
||||
import android.os.SharedMemory;
|
||||
import android.system.ErrnoException;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -105,7 +109,8 @@ class ConversionUtil {
|
||||
aidlModel.type = apiModel.type;
|
||||
aidlModel.uuid = api2aidlUuid(apiModel.uuid);
|
||||
aidlModel.vendorUuid = api2aidlUuid(apiModel.vendorUuid);
|
||||
aidlModel.data = Arrays.copyOf(apiModel.data, apiModel.data.length);
|
||||
aidlModel.data = byteArrayToSharedMemory(apiModel.data, "SoundTrigger SoundModel");
|
||||
aidlModel.dataSize = apiModel.data.length;
|
||||
return aidlModel;
|
||||
}
|
||||
|
||||
@@ -352,4 +357,20 @@ class ConversionUtil {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static @Nullable FileDescriptor byteArrayToSharedMemory(byte[] data, String name) {
|
||||
if (data.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
SharedMemory shmem = SharedMemory.create(name != null ? name : "", data.length);
|
||||
ByteBuffer buffer = shmem.mapReadWrite();
|
||||
buffer.put(data);
|
||||
shmem.unmap(buffer);
|
||||
return shmem.getFileDescriptor();
|
||||
} catch (ErrnoException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,14 +21,13 @@ import static android.system.OsConstants.PROT_READ;
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.SystemApi;
|
||||
import android.annotation.TestApi;
|
||||
import android.system.ErrnoException;
|
||||
import android.system.Os;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.util.Preconditions;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.DirectByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
@@ -82,8 +81,7 @@ public final class HidlMemoryUtil {
|
||||
ByteBuffer buffer = shmem.mapReadWrite();
|
||||
buffer.put(input);
|
||||
shmem.unmap(buffer);
|
||||
NativeHandle handle = new NativeHandle(shmem.getFileDescriptor(), true);
|
||||
return new HidlMemory("ashmem", input.length, handle);
|
||||
return sharedMemoryToHidlMemory(shmem);
|
||||
} catch (ErrnoException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -128,8 +126,7 @@ public final class HidlMemoryUtil {
|
||||
buffer.put(b);
|
||||
}
|
||||
shmem.unmap(buffer);
|
||||
NativeHandle handle = new NativeHandle(shmem.getFileDescriptor(), true);
|
||||
return new HidlMemory("ashmem", input.size(), handle);
|
||||
return sharedMemoryToHidlMemory(shmem);
|
||||
} catch (ErrnoException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -189,6 +186,38 @@ public final class HidlMemoryUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a SharedMemory to a HidlMemory without copying.
|
||||
*
|
||||
* @param shmem The shared memory object. Null means "empty" and will still result in a non-null
|
||||
* return value.
|
||||
* @return The HidlMemory instance.
|
||||
*/
|
||||
@NonNull public static HidlMemory sharedMemoryToHidlMemory(@Nullable SharedMemory shmem) {
|
||||
if (shmem == null) {
|
||||
return new HidlMemory("ashmem", 0, null);
|
||||
}
|
||||
return fileDescriptorToHidlMemory(shmem.getFileDescriptor(), shmem.getSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a FileDescriptor to a HidlMemory without copying.
|
||||
*
|
||||
* @param fd The FileDescriptor object. Null is allowed if size is 0 and will still result in
|
||||
* a non-null return value.
|
||||
* @param size The size of the memory buffer.
|
||||
* @return The HidlMemory instance.
|
||||
*/
|
||||
@NonNull public static HidlMemory fileDescriptorToHidlMemory(@Nullable FileDescriptor fd,
|
||||
int size) {
|
||||
Preconditions.checkArgument(fd != null || size == 0);
|
||||
if (fd == null) {
|
||||
return new HidlMemory("ashmem", 0, null);
|
||||
}
|
||||
NativeHandle handle = new NativeHandle(fd, true);
|
||||
return new HidlMemory("ashmem", size, handle);
|
||||
}
|
||||
|
||||
private static ByteBuffer getBuffer(@NonNull HidlMemory mem) {
|
||||
try {
|
||||
final int size = (int) mem.getSize();
|
||||
|
||||
Reference in New Issue
Block a user