blob: 3d42d003923ddd5dbbe1cd61652e67e14dc24ad9 [file] [log] [blame]
reveman7c45b3082015-06-04 01:27:081// 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
17namespace content {
18namespace {
19
20// Returns the Mach port name to use when sending or receiving messages. |pid|
21// is the process ID of the service.
rsesekdba84112015-09-18 19:22:0722std::string GetMachPortNameByPid(pid_t pid) {
reveman7c45b3082015-06-04 01:27:0823 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.
28const int kSendReplyTimeoutMs = 100;
29
30} // namespace
31
32// static
33BrowserIOSurfaceManager* BrowserIOSurfaceManager::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:2234 return base::Singleton<
35 BrowserIOSurfaceManager,
36 base::LeakySingletonTraits<BrowserIOSurfaceManager>>::get();
reveman7c45b3082015-06-04 01:27:0837}
38
39// static
40base::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 =
rsesekdba84112015-09-18 19:22:0746 bootstrap_look_up(bootstrap_port,
47 GetMachPortNameByPid(pid).c_str(),
48 &port);
reveman7c45b3082015-06-04 01:27:0849 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
rsesekdba84112015-09-18 19:22:0757// static
58std::string BrowserIOSurfaceManager::GetMachPortName() {
59 return GetMachPortNameByPid(getpid());
60}
61
ericrk368be3b2015-08-20 09:28:2762bool BrowserIOSurfaceManager::RegisterIOSurface(IOSurfaceId io_surface_id,
reveman7c45b3082015-06-04 01:27:0863 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
ericrk368be3b2015-08-20 09:28:2774void BrowserIOSurfaceManager::UnregisterIOSurface(IOSurfaceId io_surface_id,
reveman7c45b3082015-06-04 01:27:0875 int client_id) {
76 base::AutoLock lock(lock_);
77
78 IOSurfaceMapKey key(io_surface_id, client_id);
79 DCHECK(io_surfaces_.find(key) != io_surfaces_.end());
80 io_surfaces_.erase(key);
81}
82
ericrk368be3b2015-08-20 09:28:2783IOSurfaceRef BrowserIOSurfaceManager::AcquireIOSurface(
84 IOSurfaceId io_surface_id) {
reveman7c45b3082015-06-04 01:27:0885 base::AutoLock lock(lock_);
86
87 IOSurfaceMapKey key(
88 io_surface_id,
89 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId());
90 auto it = io_surfaces_.find(key);
91 if (it == io_surfaces_.end()) {
ericrk368be3b2015-08-20 09:28:2792 LOG(ERROR) << "Invalid Id for IOSurface " << io_surface_id.id;
reveman7c45b3082015-06-04 01:27:0893 return nullptr;
94 }
95
96 return IOSurfaceLookupFromMachPort(it->second->get());
97}
98
99void BrowserIOSurfaceManager::EnsureRunning() {
100 base::AutoLock lock(lock_);
101
102 if (initialized_)
103 return;
104
105 // Do not attempt to reinitialize in the event of failure.
106 initialized_ = true;
107
108 if (!Initialize()) {
109 LOG(ERROR) << "Failed to initialize the BrowserIOSurfaceManager";
110 }
111}
112
reveman459965542015-06-16 18:12:50113IOSurfaceManagerToken BrowserIOSurfaceManager::GenerateGpuProcessToken() {
114 base::AutoLock lock(lock_);
115
116 DCHECK(gpu_process_token_.IsZero());
117 gpu_process_token_ = IOSurfaceManagerToken::Generate();
118 DCHECK(gpu_process_token_.Verify());
reveman7c45b3082015-06-04 01:27:08119 return gpu_process_token_;
120}
121
reveman459965542015-06-16 18:12:50122void BrowserIOSurfaceManager::InvalidateGpuProcessToken() {
123 base::AutoLock lock(lock_);
124
125 DCHECK(!gpu_process_token_.IsZero());
126 gpu_process_token_.SetZero();
127 io_surfaces_.clear();
128}
129
reveman7c45b3082015-06-04 01:27:08130IOSurfaceManagerToken BrowserIOSurfaceManager::GenerateChildProcessToken(
131 int child_process_id) {
132 base::AutoLock lock(lock_);
133
134 IOSurfaceManagerToken token = IOSurfaceManagerToken::Generate();
135 DCHECK(token.Verify());
136 child_process_ids_[token] = child_process_id;
137 return token;
138}
139
140void BrowserIOSurfaceManager::InvalidateChildProcessToken(
141 const IOSurfaceManagerToken& token) {
142 base::AutoLock lock(lock_);
143
144 DCHECK(child_process_ids_.find(token) != child_process_ids_.end());
145 child_process_ids_.erase(token);
146}
147
reveman459965542015-06-16 18:12:50148BrowserIOSurfaceManager::BrowserIOSurfaceManager() : initialized_(false) {
reveman7c45b3082015-06-04 01:27:08149}
150
151BrowserIOSurfaceManager::~BrowserIOSurfaceManager() {
152}
153
154bool BrowserIOSurfaceManager::Initialize() {
155 lock_.AssertAcquired();
156 DCHECK(!server_port_.is_valid());
157
158 // Check in with launchd and publish the service name.
159 mach_port_t port;
160 kern_return_t kr = bootstrap_check_in(
rsesekdba84112015-09-18 19:22:07161 bootstrap_port, GetMachPortName().c_str(), &port);
reveman7c45b3082015-06-04 01:27:08162 if (kr != KERN_SUCCESS) {
163 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_check_in";
164 return false;
165 }
166 server_port_.reset(port);
167
168 // Start the dispatch source.
169 std::string queue_name =
170 base::StringPrintf("%s.IOSurfaceManager", base::mac::BaseBundleID());
171 dispatch_source_.reset(
172 new base::DispatchSourceMach(queue_name.c_str(), server_port_.get(), ^{
173 HandleRequest();
174 }));
175 dispatch_source_->Resume();
176
177 return true;
178}
179
180void BrowserIOSurfaceManager::HandleRequest() {
181 struct {
182 union {
183 mach_msg_header_t header;
184 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface;
185 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface;
186 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface;
187 } msg;
188 mach_msg_trailer_t trailer;
189 } request = {{{0}}};
190 request.msg.header.msgh_size = sizeof(request);
191 request.msg.header.msgh_local_port = server_port_.get();
192
193 kern_return_t kr =
194 mach_msg(&request.msg.header, MACH_RCV_MSG, 0, sizeof(request),
markda902e182015-10-20 18:36:13195 server_port_.get(), MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
reveman7c45b3082015-06-04 01:27:08196 if (kr != KERN_SUCCESS) {
197 MACH_LOG(ERROR, kr) << "mach_msg";
198 return;
199 }
200
201 union {
202 mach_msg_header_t header;
203 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface;
204 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface;
205 } reply = {{0}};
206
207 switch (request.msg.header.msgh_id) {
208 case IOSurfaceManagerHostMsg_RegisterIOSurface::ID:
ccameron7c766ff42015-09-18 19:43:17209 HandleRegisterIOSurfaceRequest(request.msg.register_io_surface,
210 &reply.register_io_surface);
reveman7c45b3082015-06-04 01:27:08211 break;
212 case IOSurfaceManagerHostMsg_UnregisterIOSurface::ID:
213 HandleUnregisterIOSurfaceRequest(request.msg.unregister_io_surface);
214 // Unregister requests are asynchronous and do not require a reply as
215 // there is no guarantee for how quickly an IO surface is removed from
216 // the IOSurfaceManager instance after it has been deleted by a child
217 // process.
218 return;
219 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID:
ccameron7c766ff42015-09-18 19:43:17220 HandleAcquireIOSurfaceRequest(request.msg.acquire_io_surface,
221 &reply.acquire_io_surface);
reveman7c45b3082015-06-04 01:27:08222 break;
223 default:
224 LOG(ERROR) << "Unknown message received!";
225 return;
226 }
227
228 kr = mach_msg(&reply.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT,
229 reply.header.msgh_size, 0, MACH_PORT_NULL, kSendReplyTimeoutMs,
230 MACH_PORT_NULL);
231 if (kr != KERN_SUCCESS) {
232 MACH_LOG(ERROR, kr) << "mach_msg";
233 }
234}
235
ccameron7c766ff42015-09-18 19:43:17236void BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest(
reveman7c45b3082015-06-04 01:27:08237 const IOSurfaceManagerHostMsg_RegisterIOSurface& request,
238 IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply) {
239 base::AutoLock lock(lock_);
240
ccameron7c766ff42015-09-18 19:43:17241 reply->header.msgh_bits = MACH_MSGH_BITS_REMOTE(request.header.msgh_bits);
242 reply->header.msgh_remote_port = request.header.msgh_remote_port;
243 reply->header.msgh_size = sizeof(*reply);
244 reply->body.msgh_descriptor_count = 0;
245 reply->result = false;
246
reveman7c45b3082015-06-04 01:27:08247 IOSurfaceManagerToken token;
248 static_assert(sizeof(request.token_name) == sizeof(token.name),
249 "Mach message token size doesn't match expectation.");
250 token.SetName(request.token_name);
reveman459965542015-06-16 18:12:50251 if (token.IsZero() || token != gpu_process_token_) {
reveman7c45b3082015-06-04 01:27:08252 LOG(ERROR) << "Illegal message from non-GPU process!";
ccameron7c766ff42015-09-18 19:43:17253 return;
reveman7c45b3082015-06-04 01:27:08254 }
255
ericrk368be3b2015-08-20 09:28:27256 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id), request.client_id);
reveman7c45b3082015-06-04 01:27:08257 io_surfaces_.add(key, make_scoped_ptr(new base::mac::ScopedMachSendRight(
258 request.io_surface_port.name)));
reveman7c45b3082015-06-04 01:27:08259 reply->result = true;
reveman7c45b3082015-06-04 01:27:08260}
261
262bool BrowserIOSurfaceManager::HandleUnregisterIOSurfaceRequest(
263 const IOSurfaceManagerHostMsg_UnregisterIOSurface& request) {
264 base::AutoLock lock(lock_);
265
266 IOSurfaceManagerToken token;
267 static_assert(sizeof(request.token_name) == sizeof(token.name),
268 "Mach message token size doesn't match expectation.");
269 token.SetName(request.token_name);
reveman459965542015-06-16 18:12:50270 if (token.IsZero() || token != gpu_process_token_) {
reveman7c45b3082015-06-04 01:27:08271 LOG(ERROR) << "Illegal message from non-GPU process!";
272 return false;
273 }
274
ericrk368be3b2015-08-20 09:28:27275 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id), request.client_id);
reveman7c45b3082015-06-04 01:27:08276 io_surfaces_.erase(key);
277 return true;
278}
279
ccameron7c766ff42015-09-18 19:43:17280void BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest(
reveman7c45b3082015-06-04 01:27:08281 const IOSurfaceManagerHostMsg_AcquireIOSurface& request,
282 IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply) {
283 base::AutoLock lock(lock_);
284
ccameron7c766ff42015-09-18 19:43:17285 reply->header.msgh_bits =
286 MACH_MSGH_BITS_REMOTE(request.header.msgh_bits) | MACH_MSGH_BITS_COMPLEX;
287 reply->header.msgh_remote_port = request.header.msgh_remote_port;
288 reply->header.msgh_size = sizeof(*reply);
289 reply->body.msgh_descriptor_count = 0;
290 reply->result = false;
291 reply->io_surface_port.name = MACH_PORT_NULL;
292 reply->io_surface_port.disposition = 0;
293 reply->io_surface_port.type = 0;
294
reveman7c45b3082015-06-04 01:27:08295 IOSurfaceManagerToken token;
296 static_assert(sizeof(request.token_name) == sizeof(token.name),
297 "Mach message token size doesn't match expectation.");
298 token.SetName(request.token_name);
299 auto child_process_id_it = child_process_ids_.find(token);
300 if (child_process_id_it == child_process_ids_.end()) {
301 LOG(ERROR) << "Illegal message from non-child process!";
ccameron7c766ff42015-09-18 19:43:17302 return;
reveman7c45b3082015-06-04 01:27:08303 }
304
ccameron7c766ff42015-09-18 19:43:17305 reply->result = true;
ericrk368be3b2015-08-20 09:28:27306 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id),
307 child_process_id_it->second);
reveman459965542015-06-16 18:12:50308 auto it = io_surfaces_.find(key);
309 if (it == io_surfaces_.end()) {
310 LOG(ERROR) << "Invalid Id for IOSurface " << request.io_surface_id;
ccameron7c766ff42015-09-18 19:43:17311 return;
reveman459965542015-06-16 18:12:50312 }
313
reveman7c45b3082015-06-04 01:27:08314 reply->body.msgh_descriptor_count = 1;
315 reply->io_surface_port.name = it->second->get();
316 reply->io_surface_port.disposition = MACH_MSG_TYPE_COPY_SEND;
317 reply->io_surface_port.type = MACH_MSG_PORT_DESCRIPTOR;
reveman7c45b3082015-06-04 01:27:08318}
319
320} // namespace content