Commit 61b71259 authored by sadrul's avatar sadrul Committed by Commit bot

mus: Add support for sending IOSurface mach_ports over mojo.

BUG=643746

Review-Url: https://codereview.chromium.org/2537423004
Cr-Commit-Position: refs/heads/master@{#436161}
parent dfdb0648
......@@ -4,6 +4,11 @@
#include "mojo/public/cpp/system/platform_handle.h"
#if defined(OS_MACOSX) && !defined(OS_IOS)
#include <mach/mach.h>
#include "base/mac/mach_logging.h"
#endif
namespace mojo {
namespace {
......@@ -126,4 +131,39 @@ MojoResult UnwrapSharedMemoryHandle(ScopedSharedBufferHandle handle,
return MOJO_RESULT_OK;
}
#if defined(OS_MACOSX) && !defined(OS_IOS)
ScopedHandle WrapMachPort(mach_port_t port) {
kern_return_t kr =
mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, 1);
MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr)
<< "MachPortAttachmentMac mach_port_mod_refs";
if (kr != KERN_SUCCESS)
return ScopedHandle();
MojoPlatformHandle platform_handle;
platform_handle.struct_size = sizeof(MojoPlatformHandle);
platform_handle.type = MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT;
platform_handle.value = static_cast<uint64_t>(port);
MojoHandle mojo_handle;
MojoResult result = MojoWrapPlatformHandle(&platform_handle, &mojo_handle);
CHECK_EQ(result, MOJO_RESULT_OK);
return ScopedHandle(Handle(mojo_handle));
}
MojoResult UnwrapMachPort(ScopedHandle handle, mach_port_t* port) {
MojoPlatformHandle platform_handle;
platform_handle.struct_size = sizeof(MojoPlatformHandle);
MojoResult result =
MojoUnwrapPlatformHandle(handle.release().value(), &platform_handle);
if (result != MOJO_RESULT_OK)
return result;
CHECK_EQ(platform_handle.type, MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT);
*port = static_cast<mach_port_t>(platform_handle.value);
return MOJO_RESULT_OK;
}
#endif // defined(OS_MACOSX) && !defined(OS_IOS)
} // namespace mojo
......@@ -76,6 +76,17 @@ UnwrapSharedMemoryHandle(ScopedSharedBufferHandle handle,
size_t* size,
bool* read_only);
#if defined(OS_MACOSX) && !defined(OS_IOS)
// Wraps a mach_port_t as a Mojo handle. This takes a reference to the
// Mach port.
MOJO_CPP_SYSTEM_EXPORT ScopedHandle WrapMachPort(mach_port_t port);
// Unwraps a mach_port_t from a Mojo handle. The caller gets ownership of the
// Mach port.
MOJO_CPP_SYSTEM_EXPORT MojoResult UnwrapMachPort(ScopedHandle handle,
mach_port_t* port);
#endif // defined(OS_MACOSX) && !defined(OS_IOS)
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_SYSTEM_PLATFORM_HANDLE_H_
......@@ -74,5 +74,5 @@ struct GpuMemoryBufferHandle {
uint32 offset;
uint32 stride;
NativePixmapHandle? native_pixmap_handle;
// TODO: Add support for mach_port on mac.
handle? mach_port;
};
......@@ -88,6 +88,18 @@ StructTraits<gfx::mojom::GpuMemoryBufferHandleDataView,
#endif
}
mojo::ScopedHandle StructTraits<gfx::mojom::GpuMemoryBufferHandleDataView,
gfx::GpuMemoryBufferHandle>::
mach_port(const gfx::GpuMemoryBufferHandle& handle) {
#if defined(OS_MACOSX) && !defined(OS_IOS)
if (handle.type != gfx::IO_SURFACE_BUFFER)
return mojo::ScopedHandle();
return mojo::WrapMachPort(handle.mach_port.get());
#else
return mojo::ScopedHandle();
#endif
}
bool StructTraits<gfx::mojom::GpuMemoryBufferHandleDataView,
gfx::GpuMemoryBufferHandle>::
Read(gfx::mojom::GpuMemoryBufferHandleDataView data,
......@@ -121,6 +133,16 @@ bool StructTraits<gfx::mojom::GpuMemoryBufferHandleDataView,
if (out->type == gfx::OZONE_NATIVE_PIXMAP &&
!data.ReadNativePixmapHandle(&out->native_pixmap_handle))
return false;
#endif
#if defined(OS_MACOSX) && !defined(OS_IOS)
if (out->type == gfx::IO_SURFACE_BUFFER) {
mach_port_t mach_port;
MojoResult unwrap_result =
mojo::UnwrapMachPort(data.TakeMachPort(), &mach_port);
if (unwrap_result != MOJO_RESULT_OK)
return false;
out->mach_port.reset(mach_port);
}
#endif
return true;
}
......
......@@ -269,6 +269,7 @@ struct StructTraits<gfx::mojom::GpuMemoryBufferHandleDataView,
}
static const gfx::NativePixmapHandle& native_pixmap_handle(
const gfx::GpuMemoryBufferHandle& handle);
static mojo::ScopedHandle mach_port(const gfx::GpuMemoryBufferHandle& handle);
static bool Read(gfx::mojom::GpuMemoryBufferHandleDataView data,
gfx::GpuMemoryBufferHandle* handle);
};
......
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