reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 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/browser/browser_io_surface_manager_mac.h" |
| 6 | |
| 7 | #include <servers/bootstrap.h> |
| 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/logging.h" |
| 12 | #include "base/mac/foundation_util.h" |
| 13 | #include "base/mac/mach_logging.h" |
| 14 | #include "base/strings/stringprintf.h" |
| 15 | #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
| 16 | |
| 17 | namespace content { |
| 18 | namespace { |
| 19 | |
| 20 | // Returns the Mach port name to use when sending or receiving messages. |pid| |
| 21 | // is the process ID of the service. |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 22 | std::string GetMachPortNameByPid(pid_t pid) { |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 23 | return base::StringPrintf("%s.iosurfacemgr.%d", base::mac::BaseBundleID(), |
| 24 | pid); |
| 25 | } |
| 26 | |
| 27 | // Amount of time to wait before giving up when sending a reply message. |
| 28 | const int kSendReplyTimeoutMs = 100; |
| 29 | |
| 30 | } // namespace |
| 31 | |
| 32 | // static |
| 33 | BrowserIOSurfaceManager* BrowserIOSurfaceManager::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 34 | return base::Singleton< |
| 35 | BrowserIOSurfaceManager, |
| 36 | base::LeakySingletonTraits<BrowserIOSurfaceManager>>::get(); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // static |
| 40 | base::mac::ScopedMachSendRight BrowserIOSurfaceManager::LookupServicePort( |
| 41 | pid_t pid) { |
| 42 | // Look up the named IOSurfaceManager port that's been registered with |
| 43 | // the bootstrap server. |
| 44 | mach_port_t port; |
| 45 | kern_return_t kr = |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 46 | bootstrap_look_up(bootstrap_port, |
| 47 | GetMachPortNameByPid(pid).c_str(), |
| 48 | &port); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 49 | if (kr != KERN_SUCCESS) { |
| 50 | BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up"; |
| 51 | return base::mac::ScopedMachSendRight(); |
| 52 | } |
| 53 | |
| 54 | return base::mac::ScopedMachSendRight(port); |
| 55 | } |
| 56 | |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 57 | // static |
| 58 | std::string BrowserIOSurfaceManager::GetMachPortName() { |
| 59 | return GetMachPortNameByPid(getpid()); |
| 60 | } |
| 61 | |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 62 | bool BrowserIOSurfaceManager::RegisterIOSurface(gfx::IOSurfaceId io_surface_id, |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 63 | int client_id, |
| 64 | IOSurfaceRef io_surface) { |
| 65 | base::AutoLock lock(lock_); |
| 66 | |
| 67 | IOSurfaceMapKey key(io_surface_id, client_id); |
| 68 | DCHECK(io_surfaces_.find(key) == io_surfaces_.end()); |
| 69 | io_surfaces_.add(key, make_scoped_ptr(new base::mac::ScopedMachSendRight( |
| 70 | IOSurfaceCreateMachPort(io_surface)))); |
| 71 | return true; |
| 72 | } |
| 73 | |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 74 | void BrowserIOSurfaceManager::UnregisterIOSurface( |
| 75 | gfx::IOSurfaceId io_surface_id, |
| 76 | int client_id) { |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 77 | base::AutoLock lock(lock_); |
| 78 | |
| 79 | IOSurfaceMapKey key(io_surface_id, client_id); |
| 80 | DCHECK(io_surfaces_.find(key) != io_surfaces_.end()); |
| 81 | io_surfaces_.erase(key); |
| 82 | } |
| 83 | |
ericrk | 368be3b | 2015-08-20 09:28:27 | [diff] [blame] | 84 | IOSurfaceRef BrowserIOSurfaceManager::AcquireIOSurface( |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 85 | gfx::IOSurfaceId io_surface_id) { |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 86 | base::AutoLock lock(lock_); |
| 87 | |
| 88 | IOSurfaceMapKey key( |
| 89 | io_surface_id, |
| 90 | BrowserGpuChannelHostFactory::instance()->GetGpuChannelId()); |
| 91 | auto it = io_surfaces_.find(key); |
| 92 | if (it == io_surfaces_.end()) { |
ericrk | 368be3b | 2015-08-20 09:28:27 | [diff] [blame] | 93 | LOG(ERROR) << "Invalid Id for IOSurface " << io_surface_id.id; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 94 | return nullptr; |
| 95 | } |
| 96 | |
| 97 | return IOSurfaceLookupFromMachPort(it->second->get()); |
| 98 | } |
| 99 | |
| 100 | void BrowserIOSurfaceManager::EnsureRunning() { |
| 101 | base::AutoLock lock(lock_); |
| 102 | |
| 103 | if (initialized_) |
| 104 | return; |
| 105 | |
| 106 | // Do not attempt to reinitialize in the event of failure. |
| 107 | initialized_ = true; |
| 108 | |
| 109 | if (!Initialize()) { |
| 110 | LOG(ERROR) << "Failed to initialize the BrowserIOSurfaceManager"; |
| 111 | } |
| 112 | } |
| 113 | |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 114 | IOSurfaceManagerToken BrowserIOSurfaceManager::GenerateGpuProcessToken() { |
| 115 | base::AutoLock lock(lock_); |
| 116 | |
| 117 | DCHECK(gpu_process_token_.IsZero()); |
| 118 | gpu_process_token_ = IOSurfaceManagerToken::Generate(); |
| 119 | DCHECK(gpu_process_token_.Verify()); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 120 | return gpu_process_token_; |
| 121 | } |
| 122 | |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 123 | void BrowserIOSurfaceManager::InvalidateGpuProcessToken() { |
| 124 | base::AutoLock lock(lock_); |
| 125 | |
| 126 | DCHECK(!gpu_process_token_.IsZero()); |
| 127 | gpu_process_token_.SetZero(); |
| 128 | io_surfaces_.clear(); |
| 129 | } |
| 130 | |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 131 | IOSurfaceManagerToken BrowserIOSurfaceManager::GenerateChildProcessToken( |
| 132 | int child_process_id) { |
| 133 | base::AutoLock lock(lock_); |
| 134 | |
| 135 | IOSurfaceManagerToken token = IOSurfaceManagerToken::Generate(); |
| 136 | DCHECK(token.Verify()); |
| 137 | child_process_ids_[token] = child_process_id; |
| 138 | return token; |
| 139 | } |
| 140 | |
| 141 | void BrowserIOSurfaceManager::InvalidateChildProcessToken( |
| 142 | const IOSurfaceManagerToken& token) { |
| 143 | base::AutoLock lock(lock_); |
| 144 | |
| 145 | DCHECK(child_process_ids_.find(token) != child_process_ids_.end()); |
| 146 | child_process_ids_.erase(token); |
| 147 | } |
| 148 | |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 149 | BrowserIOSurfaceManager::BrowserIOSurfaceManager() : initialized_(false) { |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | BrowserIOSurfaceManager::~BrowserIOSurfaceManager() { |
| 153 | } |
| 154 | |
| 155 | bool BrowserIOSurfaceManager::Initialize() { |
| 156 | lock_.AssertAcquired(); |
| 157 | DCHECK(!server_port_.is_valid()); |
| 158 | |
| 159 | // Check in with launchd and publish the service name. |
| 160 | mach_port_t port; |
| 161 | kern_return_t kr = bootstrap_check_in( |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 162 | bootstrap_port, GetMachPortName().c_str(), &port); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 163 | if (kr != KERN_SUCCESS) { |
| 164 | BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_check_in"; |
| 165 | return false; |
| 166 | } |
| 167 | server_port_.reset(port); |
| 168 | |
| 169 | // Start the dispatch source. |
| 170 | std::string queue_name = |
| 171 | base::StringPrintf("%s.IOSurfaceManager", base::mac::BaseBundleID()); |
| 172 | dispatch_source_.reset( |
| 173 | new base::DispatchSourceMach(queue_name.c_str(), server_port_.get(), ^{ |
| 174 | HandleRequest(); |
| 175 | })); |
| 176 | dispatch_source_->Resume(); |
| 177 | |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | void BrowserIOSurfaceManager::HandleRequest() { |
| 182 | struct { |
| 183 | union { |
| 184 | mach_msg_header_t header; |
| 185 | IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface; |
| 186 | IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface; |
| 187 | IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface; |
| 188 | } msg; |
| 189 | mach_msg_trailer_t trailer; |
| 190 | } request = {{{0}}}; |
| 191 | request.msg.header.msgh_size = sizeof(request); |
| 192 | request.msg.header.msgh_local_port = server_port_.get(); |
| 193 | |
| 194 | kern_return_t kr = |
| 195 | mach_msg(&request.msg.header, MACH_RCV_MSG, 0, sizeof(request), |
mark | da902e18 | 2015-10-20 18:36:13 | [diff] [blame] | 196 | server_port_.get(), MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 197 | if (kr != KERN_SUCCESS) { |
| 198 | MACH_LOG(ERROR, kr) << "mach_msg"; |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | union { |
| 203 | mach_msg_header_t header; |
| 204 | IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface; |
| 205 | IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface; |
| 206 | } reply = {{0}}; |
| 207 | |
| 208 | switch (request.msg.header.msgh_id) { |
| 209 | case IOSurfaceManagerHostMsg_RegisterIOSurface::ID: |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 210 | HandleRegisterIOSurfaceRequest(request.msg.register_io_surface, |
| 211 | &reply.register_io_surface); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 212 | break; |
| 213 | case IOSurfaceManagerHostMsg_UnregisterIOSurface::ID: |
| 214 | HandleUnregisterIOSurfaceRequest(request.msg.unregister_io_surface); |
| 215 | // Unregister requests are asynchronous and do not require a reply as |
| 216 | // there is no guarantee for how quickly an IO surface is removed from |
| 217 | // the IOSurfaceManager instance after it has been deleted by a child |
| 218 | // process. |
| 219 | return; |
| 220 | case IOSurfaceManagerHostMsg_AcquireIOSurface::ID: |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 221 | HandleAcquireIOSurfaceRequest(request.msg.acquire_io_surface, |
| 222 | &reply.acquire_io_surface); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 223 | break; |
| 224 | default: |
| 225 | LOG(ERROR) << "Unknown message received!"; |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | kr = mach_msg(&reply.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, |
| 230 | reply.header.msgh_size, 0, MACH_PORT_NULL, kSendReplyTimeoutMs, |
| 231 | MACH_PORT_NULL); |
| 232 | if (kr != KERN_SUCCESS) { |
| 233 | MACH_LOG(ERROR, kr) << "mach_msg"; |
| 234 | } |
| 235 | } |
| 236 | |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 237 | void BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest( |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 238 | const IOSurfaceManagerHostMsg_RegisterIOSurface& request, |
| 239 | IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply) { |
| 240 | base::AutoLock lock(lock_); |
| 241 | |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 242 | reply->header.msgh_bits = MACH_MSGH_BITS_REMOTE(request.header.msgh_bits); |
| 243 | reply->header.msgh_remote_port = request.header.msgh_remote_port; |
| 244 | reply->header.msgh_size = sizeof(*reply); |
| 245 | reply->body.msgh_descriptor_count = 0; |
| 246 | reply->result = false; |
| 247 | |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 248 | IOSurfaceManagerToken token; |
| 249 | static_assert(sizeof(request.token_name) == sizeof(token.name), |
| 250 | "Mach message token size doesn't match expectation."); |
| 251 | token.SetName(request.token_name); |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 252 | if (token.IsZero() || token != gpu_process_token_) { |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 253 | LOG(ERROR) << "Illegal message from non-GPU process!"; |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 254 | return; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 255 | } |
| 256 | |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 257 | IOSurfaceMapKey key(gfx::IOSurfaceId(request.io_surface_id), |
| 258 | request.client_id); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 259 | io_surfaces_.add(key, make_scoped_ptr(new base::mac::ScopedMachSendRight( |
| 260 | request.io_surface_port.name))); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 261 | reply->result = true; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | bool BrowserIOSurfaceManager::HandleUnregisterIOSurfaceRequest( |
| 265 | const IOSurfaceManagerHostMsg_UnregisterIOSurface& request) { |
| 266 | base::AutoLock lock(lock_); |
| 267 | |
| 268 | IOSurfaceManagerToken token; |
| 269 | static_assert(sizeof(request.token_name) == sizeof(token.name), |
| 270 | "Mach message token size doesn't match expectation."); |
| 271 | token.SetName(request.token_name); |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 272 | if (token.IsZero() || token != gpu_process_token_) { |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 273 | LOG(ERROR) << "Illegal message from non-GPU process!"; |
| 274 | return false; |
| 275 | } |
| 276 | |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 277 | IOSurfaceMapKey key(gfx::IOSurfaceId(request.io_surface_id), |
| 278 | request.client_id); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 279 | io_surfaces_.erase(key); |
| 280 | return true; |
| 281 | } |
| 282 | |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 283 | void BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest( |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 284 | const IOSurfaceManagerHostMsg_AcquireIOSurface& request, |
| 285 | IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply) { |
| 286 | base::AutoLock lock(lock_); |
| 287 | |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 288 | reply->header.msgh_bits = |
| 289 | MACH_MSGH_BITS_REMOTE(request.header.msgh_bits) | MACH_MSGH_BITS_COMPLEX; |
| 290 | reply->header.msgh_remote_port = request.header.msgh_remote_port; |
| 291 | reply->header.msgh_size = sizeof(*reply); |
| 292 | reply->body.msgh_descriptor_count = 0; |
| 293 | reply->result = false; |
| 294 | reply->io_surface_port.name = MACH_PORT_NULL; |
| 295 | reply->io_surface_port.disposition = 0; |
| 296 | reply->io_surface_port.type = 0; |
| 297 | |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 298 | IOSurfaceManagerToken token; |
| 299 | static_assert(sizeof(request.token_name) == sizeof(token.name), |
| 300 | "Mach message token size doesn't match expectation."); |
| 301 | token.SetName(request.token_name); |
| 302 | auto child_process_id_it = child_process_ids_.find(token); |
| 303 | if (child_process_id_it == child_process_ids_.end()) { |
| 304 | LOG(ERROR) << "Illegal message from non-child process!"; |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 305 | return; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 306 | } |
| 307 | |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 308 | reply->result = true; |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 309 | IOSurfaceMapKey key(gfx::IOSurfaceId(request.io_surface_id), |
ericrk | 368be3b | 2015-08-20 09:28:27 | [diff] [blame] | 310 | child_process_id_it->second); |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 311 | auto it = io_surfaces_.find(key); |
| 312 | if (it == io_surfaces_.end()) { |
| 313 | LOG(ERROR) << "Invalid Id for IOSurface " << request.io_surface_id; |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 314 | return; |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 315 | } |
| 316 | |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 317 | reply->body.msgh_descriptor_count = 1; |
| 318 | reply->io_surface_port.name = it->second->get(); |
| 319 | reply->io_surface_port.disposition = MACH_MSG_TYPE_COPY_SEND; |
| 320 | reply->io_surface_port.type = MACH_MSG_PORT_DESCRIPTOR; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | } // namespace content |