Commit 1817be8a authored by Jeremy Roman's avatar Jeremy Roman Committed by Chromium LUCI CQ

Use a direct buffer when available while accepting Bitmap from Mojo.

This saves a copy.

Bug: None
Change-Id: Id48a8cd618527f640af44dc3f7807877213f5fa5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2597001Reviewed-by: default avatarOksana Zhuravlova <oksamyt@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840802}
parent 15bf3b20
......@@ -18,6 +18,33 @@ import java.nio.ByteBuffer;
public final class BigBufferUtil {
public static final int MAX_INLINE_ARRAY_SIZE = 64 * 1024;
/**
* A mapping to a BigBuffer.
*
* If it is backed by shared memory, this will be a direct mapping which must be closed and will
* be invalid thereafter. The simplest way to do this is by using try-with-resources.
*/
public static class Mapping implements AutoCloseable {
private final SharedBufferHandle mHandle;
private final ByteBuffer mBuffer;
Mapping(SharedBufferHandle handle, ByteBuffer buffer) {
mHandle = handle;
mBuffer = buffer;
}
public ByteBuffer getBuffer() {
return mBuffer;
}
@Override
public void close() {
if (mHandle != null) {
mHandle.unmap(mBuffer);
}
}
}
// Retrives a copy of the buffer's contents regardless of what type was backing it (i.e. array
// or shared memory).
public static byte[] getBytesFromBigBuffer(BigBuffer buffer) {
......@@ -34,6 +61,20 @@ public final class BigBufferUtil {
}
}
// Opens a mapping to an existing buffer for direct reading, without a copy.
// This must be used with a try-with-resources so that close is called to prevent a leak.
// The direct buffer must not be used after close is called.
public static Mapping map(BigBuffer buffer) {
if (buffer.which() == BigBuffer.Tag.Bytes) {
return new Mapping(null, ByteBuffer.wrap(buffer.getBytes()));
} else {
BigBufferSharedMemoryRegion region = buffer.getSharedMemory();
ByteBuffer byteBuffer =
region.bufferHandle.map(0, region.size, SharedBufferHandle.MapFlags.NONE);
return new Mapping(region.bufferHandle, byteBuffer);
}
}
// Creates a new mojom.BigBuffer for IPC from a set of bytes. If the byte array is larger than
// MAX_INLINE_ARRAY_SIZE, shared memory will be used instead of an inline array.
public static BigBuffer createBigBufferFromBytes(byte[] bytes) {
......
......@@ -32,16 +32,16 @@ public class BitmapUtils {
return null;
}
ByteBuffer imageBuffer =
ByteBuffer.wrap(BigBufferUtil.getBytesFromBigBuffer(bitmapData.pixelData));
if (imageBuffer.capacity() <= 0) {
return null;
try (BigBufferUtil.Mapping mapping = BigBufferUtil.map(bitmapData.pixelData)) {
ByteBuffer imageBuffer = mapping.getBuffer();
if (imageBuffer.capacity() <= 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(imageBuffer);
return bitmap;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(imageBuffer);
return bitmap;
}
public static Frame convertToFrame(org.chromium.skia.mojom.BitmapN32 bitmapData) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment