Chromium Code Reviews| Index: ui/gfx/mojo/buffer_types_traits.cc |
| diff --git a/ui/gfx/mojo/buffer_types_traits.cc b/ui/gfx/mojo/buffer_types_traits.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..badefb88b4bb30f0270488c8b0640640d15cb135 |
| --- /dev/null |
| +++ b/ui/gfx/mojo/buffer_types_traits.cc |
| @@ -0,0 +1,99 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/gfx/mojo/buffer_types_traits.h" |
| + |
| +#include "mojo/public/cpp/system/platform_handle.h" |
| + |
| +namespace mojo { |
| + |
| +std::vector<mojo::ScopedHandle> |
| +StructTraits<gfx::mojom::NativePixmapHandleDataView, |
| + gfx::NativePixmapHandle>::fds(const gfx::NativePixmapHandle& |
| + pixmap_handle) { |
| + std::vector<mojo::ScopedHandle> handles; |
| +#if defined(USE_OZONE) |
| + for (const base::FileDescriptor& fd : pixmap_handle.fds) { |
| + base::PlatformFile platform_file = fd.fd; |
| + handles.push_back(mojo::WrapPlatformFile(platform_file)); |
| + } |
| +#endif // defined(USE_OZONE) |
| + return handles; |
| +} |
| + |
| +bool StructTraits< |
| + gfx::mojom::NativePixmapHandleDataView, |
| + gfx::NativePixmapHandle>::Read(gfx::mojom::NativePixmapHandleDataView data, |
| + gfx::NativePixmapHandle* out) { |
| +#if defined(USE_OZONE) |
| + std::vector<mojo::ScopedHandle> handles; |
| + if (!data.ReadFds(&handles)) |
|
Fady Samuel
2016/08/31 17:06:02
GetFdsDataView and iterate directly inside the buf
sadrul
2016/08/31 19:53:19
Nice! Done. Thanks!
|
| + return false; |
| + for (mojo::ScopedHandle& handle : handles) |
| + out->fds.push_back(base::FileDescriptor(handle.release().value(), true)); |
| + return data.ReadPlanes(&out->planes); |
| +#else |
| + return false; |
| +#endif |
| +} |
| + |
| +mojo::ScopedHandle StructTraits<gfx::mojom::GpuMemoryBufferHandleDataView, |
| + gfx::GpuMemoryBufferHandle>:: |
| + shared_memory_handle(const gfx::GpuMemoryBufferHandle& handle) { |
| + base::PlatformFile platform_file = base::kInvalidPlatformFile; |
| +#if defined(OS_WIN) |
| + platform_file = handle.handle.GetHandle(); |
| +#elif defined(OS_MACOSX) || defined(OS_IOS) |
| + NOTIMPLEMENTED(); |
| +#else |
| + DCHECK(handle.handle.auto_close || handle.handle.fd == -1); |
| + platform_file = handle.handle.fd; |
| +#endif |
| + return mojo::WrapPlatformFile(platform_file); |
| +} |
| + |
| +const gfx::NativePixmapHandle& |
| +StructTraits<gfx::mojom::GpuMemoryBufferHandleDataView, |
| + gfx::GpuMemoryBufferHandle>:: |
| + native_pixmap_handle(const gfx::GpuMemoryBufferHandle& handle) { |
| +#if defined(USE_OZONE) |
| + return handle.native_pixmap_handle; |
| +#else |
| + static gfx::NativePixmapHandle pixmap_handle; |
| + return pixmap_handle; |
| +#endif |
| +} |
| + |
| +bool StructTraits<gfx::mojom::GpuMemoryBufferHandleDataView, |
| + gfx::GpuMemoryBufferHandle>:: |
| + Read(gfx::mojom::GpuMemoryBufferHandleDataView data, |
| + gfx::GpuMemoryBufferHandle* out) { |
| + if (!data.ReadType(&out->type) || !data.ReadId(&out->id)) |
| + return false; |
| + |
| + base::PlatformFile platform_file; |
| + MojoResult unwrap_result = mojo::UnwrapPlatformFile( |
| + data.TakeSharedMemoryHandle(), &platform_file); |
| + if (unwrap_result != MOJO_RESULT_OK) |
| + return false; |
| +#if defined(OS_WIN) |
| + out->handle = |
| + base::SharedMemoryHandle(platform_file, base::GetCurrentProcId()); |
| +#elif defined(OS_MACOSX) || defined(OS_IOS) |
| + // TODO: Add support for mach_port on mac. |
| + out->handle = base::SharedMemoryHandle(); |
| +#else |
| + out->handle = base::SharedMemoryHandle(platform_file, true); |
| +#endif |
| + |
| + out->offset = data.offset(); |
| + out->stride = data.stride(); |
| +#if defined(USE_OZONE) |
| + if (!data.ReadNativePixmapHandle(&out->native_pixmap_handle)) |
| + return false; |
| +#endif |
| + return true; |
| +} |
| + |
| +} // namespace mojo |