Index: content/child/child_io_surface_manager_mac.cc |
diff --git a/content/child/child_io_surface_manager_mac.cc b/content/child/child_io_surface_manager_mac.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6d0257b88b08c2fe266ab6a0cf4ec583a269b601 |
--- /dev/null |
+++ b/content/child/child_io_surface_manager_mac.cc |
@@ -0,0 +1,170 @@ |
+// Copyright 2015 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 "content/child/child_io_surface_manager_mac.h" |
+ |
+#include <bsm/libbsm.h> |
Robert Sesek
2015/05/15 21:45:05
Unused.
reveman
2015/05/18 18:15:38
Done.
|
+#include <servers/bootstrap.h> |
+ |
+#include <string> |
+ |
+#include "base/logging.h" |
+#include "base/mac/foundation_util.h" |
+#include "base/mac/mach_logging.h" |
+#include "base/strings/stringprintf.h" |
+#include "content/common/mac/io_surface_manager_messages.h" |
+ |
+namespace content { |
+ |
+// static |
+ChildIOSurfaceManager* ChildIOSurfaceManager::GetInstance() { |
+ return Singleton<ChildIOSurfaceManager, |
+ LeakySingletonTraits<ChildIOSurfaceManager>>::get(); |
+} |
+ |
+bool ChildIOSurfaceManager::RegisterIOSurface(int io_surface_id, |
+ int client_id, |
+ IOSurfaceRef io_surface) { |
+ // Deallocate the right after sending a copy to the parent. |
+ base::mac::ScopedMachSendRight scoped_io_surface_right( |
Robert Sesek
2015/05/15 21:45:05
Do this after allocating the reply, since that can
reveman
2015/05/18 18:15:38
Good call. Done.
|
+ IOSurfaceCreateMachPort(io_surface)); |
+ |
+ mach_port_t receive_port; |
Robert Sesek
2015/05/15 21:45:05
naming: reply_port
reveman
2015/05/18 18:15:39
Done.
|
+ kern_return_t kr = mach_port_allocate(mach_task_self(), |
+ MACH_PORT_RIGHT_RECEIVE, &receive_port); |
+ if (kr != KERN_SUCCESS) { |
+ MACH_LOG(ERROR, kr) << "mach_port_allocate"; |
+ return false; |
+ } |
+ base::mac::ScopedMachReceiveRight scoped_receive_right(receive_port); |
+ |
+ union { |
Robert Sesek
2015/05/15 21:45:05
I'd avoid a union here.
reveman
2015/05/18 18:15:39
hm, how? by using mach_msg_overwrite?
Robert Sesek
2015/05/19 23:26:23
No, just declare two variables not in a union.
|
+ struct { |
+ mach_msg_header_t header; |
+ mach_msg_body_t body; |
+ IOSurfaceManagerHostMsg_RegisterIOSurface msg; |
+ gpu::Mailbox mailbox; |
+ } request; |
+ struct { |
+ mach_msg_header_t header; |
+ mach_msg_body_t body; |
+ IOSurfaceManagerMsg_RegisterIOSurfaceReply msg; |
+ mach_msg_trailer_t trailer; |
+ } reply; |
+ } data = {{{0}}}; |
+ data.request.header.msgh_bits = |
+ MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE) | |
+ MACH_MSGH_BITS_COMPLEX; |
+ data.request.header.msgh_remote_port = send_port_; |
+ data.request.header.msgh_local_port = receive_port; |
+ data.request.header.msgh_size = sizeof(data.request); |
+ data.request.header.msgh_id = IOSurfaceManagerHostMsg_RegisterIOSurface::ID; |
+ data.request.body.msgh_descriptor_count = 1; |
+ data.request.msg.io_surface_port.name = scoped_io_surface_right; |
+ data.request.msg.io_surface_port.disposition = MACH_MSG_TYPE_COPY_SEND; |
+ data.request.msg.io_surface_port.type = MACH_MSG_PORT_DESCRIPTOR; |
+ data.request.msg.io_surface_id = io_surface_id; |
+ data.request.msg.client_id = client_id; |
+ data.request.mailbox = mailbox_; |
+ |
+ kr = mach_msg(&data.request.header, MACH_SEND_MSG | MACH_RCV_MSG, |
+ sizeof(data.request), sizeof(data.reply), receive_port, |
+ MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); |
+ if (kr != KERN_SUCCESS) { |
+ MACH_LOG(ERROR, kr) << "mach_msg"; |
+ return false; |
+ } |
+ |
+ return data.reply.msg.result; |
+} |
+ |
+void ChildIOSurfaceManager::UnregisterIOSurface(int io_surface_id, |
+ int client_id) { |
+ struct { |
+ mach_msg_header_t header; |
+ mach_msg_body_t body; |
+ IOSurfaceManagerHostMsg_UnregisterIOSurface msg; |
+ gpu::Mailbox mailbox; |
+ } request = {{0}}; |
+ |
+ request.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0); |
+ request.header.msgh_remote_port = send_port_; |
+ request.header.msgh_local_port = MACH_PORT_NULL; |
+ request.header.msgh_size = sizeof(request); |
+ request.header.msgh_id = IOSurfaceManagerHostMsg_UnregisterIOSurface::ID; |
+ request.msg.io_surface_id = io_surface_id; |
+ request.msg.client_id = client_id; |
+ request.mailbox = mailbox_; |
+ |
+ kern_return_t kr = mach_msg( |
+ &request.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, sizeof(request), 0, |
+ MACH_PORT_NULL, 100 /*milliseconds*/, MACH_PORT_NULL); |
+ if (kr != KERN_SUCCESS) { |
+ MACH_LOG(ERROR, kr) << "mach_msg"; |
+ } |
+} |
+ |
+IOSurfaceRef ChildIOSurfaceManager::AcquireIOSurface(int io_surface_id) { |
+ mach_port_t receive_port; |
+ kern_return_t kr = mach_port_allocate(mach_task_self(), |
+ MACH_PORT_RIGHT_RECEIVE, &receive_port); |
+ if (kr != KERN_SUCCESS) { |
+ MACH_LOG(ERROR, kr) << "mach_port_allocate"; |
+ return nullptr; |
+ } |
+ base::mac::ScopedMachReceiveRight scoped_receive_right(receive_port); |
+ |
+ union { |
+ struct { |
+ mach_msg_header_t header; |
+ mach_msg_body_t body; |
+ IOSurfaceManagerHostMsg_AcquireIOSurface msg; |
+ gpu::Mailbox mailbox; |
+ } request; |
+ struct { |
+ mach_msg_header_t header; |
+ mach_msg_body_t body; |
+ IOSurfaceManagerMsg_AcquireIOSurfaceReply msg; |
+ mach_msg_trailer_t trailer; |
+ } reply; |
+ } data = {{{0}}}; |
+ data.request.header.msgh_bits = |
+ MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE); |
+ data.request.header.msgh_remote_port = send_port_; |
+ data.request.header.msgh_local_port = receive_port; |
+ data.request.header.msgh_size = sizeof(data.request); |
+ data.request.header.msgh_id = IOSurfaceManagerHostMsg_AcquireIOSurface::ID; |
+ data.request.msg.io_surface_id = io_surface_id; |
+ data.request.mailbox = mailbox_; |
+ |
+ kr = mach_msg(&data.request.header, MACH_SEND_MSG | MACH_RCV_MSG, |
+ sizeof(data.request), sizeof(data.reply), receive_port, |
+ MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); |
+ if (kr != KERN_SUCCESS) { |
+ MACH_LOG(ERROR, kr) << "mach_msg"; |
+ return nullptr; |
+ } |
+ |
+ return IOSurfaceLookupFromMachPort(data.reply.msg.io_surface_port.name); |
+} |
+ |
+ChildIOSurfaceManager::ChildIOSurfaceManager() : send_port_(MACH_PORT_NULL) { |
+ // Look up the named IOSurfaceManager port that's been registered with |
+ // the bootstrap server. |
+ std::string mach_port_name = base::StringPrintf( |
+ "%s.iosurfacemgr.%d", base::mac::BaseBundleID(), getppid()); |
Robert Sesek
2015/05/15 21:45:05
This could be centralized to IOSurfaceManager as a
reveman
2015/05/18 18:15:38
Done. See LookupServicePort in latest patch.
|
+ mach_port_t port; |
+ kern_return_t kr = bootstrap_look_up( |
+ bootstrap_port, const_cast<char*>(mach_port_name.c_str()), &port); |
+ if (kr != KERN_SUCCESS) { |
+ BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up"; |
+ return; |
Robert Sesek
2015/05/15 21:45:05
If this fails, then an instance of this class will
reveman
2015/05/18 18:15:38
I changed how this is initialized in latest patch.
|
+ } |
+ send_port_.reset(port); |
+} |
+ |
+ChildIOSurfaceManager::~ChildIOSurfaceManager() { |
+} |
+ |
+} // namespace content |