blob: 36f9d52f132719ca6815f026d90ee1691e6769e9 [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
dcastagna5ab612c02015-11-01 20:15:0862bool BrowserIOSurfaceManager::RegisterIOSurface(gfx::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
dcastagna5ab612c02015-11-01 20:15:0874void BrowserIOSurfaceManager::UnregisterIOSurface(
75 gfx::IOSurfaceId io_surface_id,
76 int client_id) {
reveman7c45b3082015-06-04 01:27:0877 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
ericrk368be3b2015-08-20 09:28:2784IOSurfaceRef BrowserIOSurfaceManager::AcquireIOSurface(
dcastagna5ab612c02015-11-01 20:15:0885 gfx::IOSurfaceId io_surface_id) {
reveman7c45b3082015-06-04 01:27:0886 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()) {
ericrk368be3b2015-08-20 09:28:2793 LOG(ERROR) << "Invalid Id for IOSurface " << io_surface_id.id;
reveman7c45b3082015-06-04 01:27:0894 return nullptr;
95 }
96
97 return IOSurfaceLookupFromMachPort(it->second->get());
98}
99
100void 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
reveman459965542015-06-16 18:12:50114IOSurfaceManagerToken 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());
reveman7c45b3082015-06-04 01:27:08120 return gpu_process_token_;
121}
122
reveman459965542015-06-16 18:12:50123void 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
reveman7c45b3082015-06-04 01:27:08131IOSurfaceManagerToken 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
141void 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
reveman459965542015-06-16 18:12:50149BrowserIOSurfaceManager::BrowserIOSurfaceManager() : initialized_(false) {
reveman7c45b3082015-06-04 01:27:08150}
151
152BrowserIOSurfaceManager::~BrowserIOSurfaceManager() {
153}
154
155bool 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(
rsesekdba84112015-09-18 19:22:07162 bootstrap_port, GetMachPortName().c_str(), &port);
reveman7c45b3082015-06-04 01:27:08163 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
181void 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),
markda902e182015-10-20 18:36:13196 server_port_.get(), MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
reveman7c45b3082015-06-04 01:27:08197 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:
ccameron7c766ff42015-09-18 19:43:17210 HandleRegisterIOSurfaceRequest(request.msg.register_io_surface,
211 &reply.register_io_surface);
reveman7c45b3082015-06-04 01:27:08212 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:
ccameron7c766ff42015-09-18 19:43:17221 HandleAcquireIOSurfaceRequest(request.msg.acquire_io_surface,
222 &reply.acquire_io_surface);
reveman7c45b3082015-06-04 01:27:08223 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
ccameron7c766ff42015-09-18 19:43:17237void BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest(
reveman7c45b3082015-06-04 01:27:08238 const IOSurfaceManagerHostMsg_RegisterIOSurface& request,
239 IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply) {
240 base::AutoLock lock(lock_);
241
ccameron7c766ff42015-09-18 19:43:17242 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
reveman7c45b3082015-06-04 01:27:08248 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);
reveman459965542015-06-16 18:12:50252 if (token.IsZero() || token != gpu_process_token_) {
reveman7c45b3082015-06-04 01:27:08253 LOG(ERROR) << "Illegal message from non-GPU process!";
ccameron7c766ff42015-09-18 19:43:17254 return;
reveman7c45b3082015-06-04 01:27:08255 }
256
dcastagna5ab612c02015-11-01 20:15:08257 IOSurfaceMapKey key(gfx::IOSurfaceId(request.io_surface_id),
258 request.client_id);
reveman7c45b3082015-06-04 01:27:08259 io_surfaces_.add(key, make_scoped_ptr(new base::mac::ScopedMachSendRight(
260 request.io_surface_port.name)));
reveman7c45b3082015-06-04 01:27:08261 reply->result = true;
reveman7c45b3082015-06-04 01:27:08262}
263
264bool 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);
reveman459965542015-06-16 18:12:50272 if (token.IsZero() || token != gpu_process_token_) {
reveman7c45b3082015-06-04 01:27:08273 LOG(ERROR) << "Illegal message from non-GPU process!";
274 return false;
275 }
276
dcastagna5ab612c02015-11-01 20:15:08277 IOSurfaceMapKey key(gfx::IOSurfaceId(request.io_surface_id),
278 request.client_id);
reveman7c45b3082015-06-04 01:27:08279 io_surfaces_.erase(key);
280 return true;
281}
282
ccameron7c766ff42015-09-18 19:43:17283void BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest(
reveman7c45b3082015-06-04 01:27:08284 const IOSurfaceManagerHostMsg_AcquireIOSurface& request,
285 IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply) {
286 base::AutoLock lock(lock_);
287
ccameron7c766ff42015-09-18 19:43:17288 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
reveman7c45b3082015-06-04 01:27:08298 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!";
ccameron7c766ff42015-09-18 19:43:17305 return;
reveman7c45b3082015-06-04 01:27:08306 }
307
ccameron7c766ff42015-09-18 19:43:17308 reply->result = true;
dcastagna5ab612c02015-11-01 20:15:08309 IOSurfaceMapKey key(gfx::IOSurfaceId(request.io_surface_id),
ericrk368be3b2015-08-20 09:28:27310 child_process_id_it->second);
reveman459965542015-06-16 18:12:50311 auto it = io_surfaces_.find(key);
312 if (it == io_surfaces_.end()) {
313 LOG(ERROR) << "Invalid Id for IOSurface " << request.io_surface_id;
ccameron7c766ff42015-09-18 19:43:17314 return;
reveman459965542015-06-16 18:12:50315 }
316
reveman7c45b3082015-06-04 01:27:08317 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;
reveman7c45b3082015-06-04 01:27:08321}
322
323} // namespace content