OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/child/child_io_surface_manager_mac.h" |
| 6 |
| 7 #include "base/mac/mach_logging.h" |
| 8 #include "content/common/mac/io_surface_manager_messages.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 // static |
| 13 ChildIOSurfaceManager* ChildIOSurfaceManager::GetInstance() { |
| 14 return Singleton<ChildIOSurfaceManager, |
| 15 LeakySingletonTraits<ChildIOSurfaceManager>>::get(); |
| 16 } |
| 17 |
| 18 bool ChildIOSurfaceManager::RegisterIOSurface(int io_surface_id, |
| 19 int client_id, |
| 20 IOSurfaceRef io_surface) { |
| 21 DCHECK(service_port_.is_valid()); |
| 22 DCHECK(!token_.IsZero()); |
| 23 |
| 24 mach_port_t reply_port; |
| 25 kern_return_t kr = mach_port_allocate(mach_task_self(), |
| 26 MACH_PORT_RIGHT_RECEIVE, &reply_port); |
| 27 if (kr != KERN_SUCCESS) { |
| 28 MACH_LOG(ERROR, kr) << "mach_port_allocate"; |
| 29 return false; |
| 30 } |
| 31 base::mac::ScopedMachReceiveRight scoped_receive_right(reply_port); |
| 32 |
| 33 // Deallocate the right after sending a copy to the parent. |
| 34 base::mac::ScopedMachSendRight scoped_io_surface_right( |
| 35 IOSurfaceCreateMachPort(io_surface)); |
| 36 |
| 37 union { |
| 38 IOSurfaceManagerHostMsg_RegisterIOSurface request; |
| 39 struct { |
| 40 IOSurfaceManagerMsg_RegisterIOSurfaceReply msg; |
| 41 mach_msg_trailer_t trailer; |
| 42 } reply; |
| 43 } data = {{{0}}}; |
| 44 data.request.header.msgh_bits = |
| 45 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE) | |
| 46 MACH_MSGH_BITS_COMPLEX; |
| 47 data.request.header.msgh_remote_port = service_port_; |
| 48 data.request.header.msgh_local_port = reply_port; |
| 49 data.request.header.msgh_size = sizeof(data.request); |
| 50 data.request.header.msgh_id = IOSurfaceManagerHostMsg_RegisterIOSurface::ID; |
| 51 data.request.body.msgh_descriptor_count = 1; |
| 52 data.request.io_surface_port.name = scoped_io_surface_right; |
| 53 data.request.io_surface_port.disposition = MACH_MSG_TYPE_COPY_SEND; |
| 54 data.request.io_surface_port.type = MACH_MSG_PORT_DESCRIPTOR; |
| 55 data.request.io_surface_id = io_surface_id; |
| 56 data.request.client_id = client_id; |
| 57 memcpy(data.request.token_name, token_.name, sizeof(token_.name)); |
| 58 |
| 59 kr = mach_msg(&data.request.header, MACH_SEND_MSG | MACH_RCV_MSG, |
| 60 sizeof(data.request), sizeof(data.reply), reply_port, |
| 61 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); |
| 62 if (kr != KERN_SUCCESS) { |
| 63 MACH_LOG(ERROR, kr) << "mach_msg"; |
| 64 return false; |
| 65 } |
| 66 |
| 67 return data.reply.msg.result; |
| 68 } |
| 69 |
| 70 void ChildIOSurfaceManager::UnregisterIOSurface(int io_surface_id, |
| 71 int client_id) { |
| 72 DCHECK(service_port_.is_valid()); |
| 73 DCHECK(!token_.IsZero()); |
| 74 |
| 75 IOSurfaceManagerHostMsg_UnregisterIOSurface request = {{0}}; |
| 76 request.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0); |
| 77 request.header.msgh_remote_port = service_port_; |
| 78 request.header.msgh_local_port = MACH_PORT_NULL; |
| 79 request.header.msgh_size = sizeof(request); |
| 80 request.header.msgh_id = IOSurfaceManagerHostMsg_UnregisterIOSurface::ID; |
| 81 request.io_surface_id = io_surface_id; |
| 82 request.client_id = client_id; |
| 83 memcpy(request.token_name, token_.name, sizeof(token_.name)); |
| 84 |
| 85 kern_return_t kr = |
| 86 mach_msg(&request.header, MACH_SEND_MSG, sizeof(request), 0, |
| 87 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); |
| 88 if (kr != KERN_SUCCESS) { |
| 89 MACH_LOG(ERROR, kr) << "mach_msg"; |
| 90 } |
| 91 } |
| 92 |
| 93 IOSurfaceRef ChildIOSurfaceManager::AcquireIOSurface(int io_surface_id) { |
| 94 DCHECK(service_port_.is_valid()); |
| 95 DCHECK(!token_.IsZero()); |
| 96 |
| 97 mach_port_t reply_port; |
| 98 kern_return_t kr = mach_port_allocate(mach_task_self(), |
| 99 MACH_PORT_RIGHT_RECEIVE, &reply_port); |
| 100 if (kr != KERN_SUCCESS) { |
| 101 MACH_LOG(ERROR, kr) << "mach_port_allocate"; |
| 102 return nullptr; |
| 103 } |
| 104 base::mac::ScopedMachReceiveRight scoped_receive_right(reply_port); |
| 105 |
| 106 union { |
| 107 IOSurfaceManagerHostMsg_AcquireIOSurface request; |
| 108 struct { |
| 109 IOSurfaceManagerMsg_AcquireIOSurfaceReply msg; |
| 110 mach_msg_trailer_t trailer; |
| 111 } reply; |
| 112 } data = {{{0}}}; |
| 113 data.request.header.msgh_bits = |
| 114 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE); |
| 115 data.request.header.msgh_remote_port = service_port_; |
| 116 data.request.header.msgh_local_port = reply_port; |
| 117 data.request.header.msgh_size = sizeof(data.request); |
| 118 data.request.header.msgh_id = IOSurfaceManagerHostMsg_AcquireIOSurface::ID; |
| 119 data.request.io_surface_id = io_surface_id; |
| 120 memcpy(data.request.token_name, token_.name, sizeof(token_.name)); |
| 121 |
| 122 kr = mach_msg(&data.request.header, MACH_SEND_MSG | MACH_RCV_MSG, |
| 123 sizeof(data.request), sizeof(data.reply), reply_port, |
| 124 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); |
| 125 if (kr != KERN_SUCCESS) { |
| 126 MACH_LOG(ERROR, kr) << "mach_msg"; |
| 127 return nullptr; |
| 128 } |
| 129 |
| 130 // Deallocate the right after creating an IOSurface reference. |
| 131 base::mac::ScopedMachSendRight scoped_io_surface_right( |
| 132 data.reply.msg.io_surface_port.name); |
| 133 |
| 134 return IOSurfaceLookupFromMachPort(scoped_io_surface_right); |
| 135 } |
| 136 |
| 137 ChildIOSurfaceManager::ChildIOSurfaceManager() { |
| 138 } |
| 139 |
| 140 ChildIOSurfaceManager::~ChildIOSurfaceManager() { |
| 141 } |
| 142 |
| 143 } // namespace content |
OLD | NEW |