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.
5 #include "content/browser/browser_io_surface_manager_mac.h"
7 #include <servers/bootstrap.h>
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"
20 // Returns the Mach port name to use when sending or receiving messages. |pid|
21 // is the process ID of the service.
22 std::string
GetMachPortName(pid_t pid
) {
23 return base::StringPrintf("%s.iosurfacemgr.%d", base::mac::BaseBundleID(),
27 // Amount of time to wait before giving up when sending a reply message.
28 const int kSendReplyTimeoutMs
= 100;
33 BrowserIOSurfaceManager
* BrowserIOSurfaceManager::GetInstance() {
34 return Singleton
<BrowserIOSurfaceManager
,
35 LeakySingletonTraits
<BrowserIOSurfaceManager
>>::get();
39 base::mac::ScopedMachSendRight
BrowserIOSurfaceManager::LookupServicePort(
41 // Look up the named IOSurfaceManager port that's been registered with
42 // the bootstrap server.
45 bootstrap_look_up(bootstrap_port
, GetMachPortName(pid
).c_str(), &port
);
46 if (kr
!= KERN_SUCCESS
) {
47 BOOTSTRAP_LOG(ERROR
, kr
) << "bootstrap_look_up";
48 return base::mac::ScopedMachSendRight();
51 return base::mac::ScopedMachSendRight(port
);
54 bool BrowserIOSurfaceManager::RegisterIOSurface(IOSurfaceId io_surface_id
,
56 IOSurfaceRef io_surface
) {
57 base::AutoLock
lock(lock_
);
59 IOSurfaceMapKey
key(io_surface_id
, client_id
);
60 DCHECK(io_surfaces_
.find(key
) == io_surfaces_
.end());
61 io_surfaces_
.add(key
, make_scoped_ptr(new base::mac::ScopedMachSendRight(
62 IOSurfaceCreateMachPort(io_surface
))));
66 void BrowserIOSurfaceManager::UnregisterIOSurface(IOSurfaceId io_surface_id
,
68 base::AutoLock
lock(lock_
);
70 IOSurfaceMapKey
key(io_surface_id
, client_id
);
71 DCHECK(io_surfaces_
.find(key
) != io_surfaces_
.end());
72 io_surfaces_
.erase(key
);
75 IOSurfaceRef
BrowserIOSurfaceManager::AcquireIOSurface(
76 IOSurfaceId io_surface_id
) {
77 base::AutoLock
lock(lock_
);
81 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId());
82 auto it
= io_surfaces_
.find(key
);
83 if (it
== io_surfaces_
.end()) {
84 LOG(ERROR
) << "Invalid Id for IOSurface " << io_surface_id
.id
;
88 return IOSurfaceLookupFromMachPort(it
->second
->get());
91 void BrowserIOSurfaceManager::EnsureRunning() {
92 base::AutoLock
lock(lock_
);
97 // Do not attempt to reinitialize in the event of failure.
101 LOG(ERROR
) << "Failed to initialize the BrowserIOSurfaceManager";
105 IOSurfaceManagerToken
BrowserIOSurfaceManager::GenerateGpuProcessToken() {
106 base::AutoLock
lock(lock_
);
108 DCHECK(gpu_process_token_
.IsZero());
109 gpu_process_token_
= IOSurfaceManagerToken::Generate();
110 DCHECK(gpu_process_token_
.Verify());
111 return gpu_process_token_
;
114 void BrowserIOSurfaceManager::InvalidateGpuProcessToken() {
115 base::AutoLock
lock(lock_
);
117 DCHECK(!gpu_process_token_
.IsZero());
118 gpu_process_token_
.SetZero();
119 io_surfaces_
.clear();
122 IOSurfaceManagerToken
BrowserIOSurfaceManager::GenerateChildProcessToken(
123 int child_process_id
) {
124 base::AutoLock
lock(lock_
);
126 IOSurfaceManagerToken token
= IOSurfaceManagerToken::Generate();
127 DCHECK(token
.Verify());
128 child_process_ids_
[token
] = child_process_id
;
132 void BrowserIOSurfaceManager::InvalidateChildProcessToken(
133 const IOSurfaceManagerToken
& token
) {
134 base::AutoLock
lock(lock_
);
136 DCHECK(child_process_ids_
.find(token
) != child_process_ids_
.end());
137 child_process_ids_
.erase(token
);
140 BrowserIOSurfaceManager::BrowserIOSurfaceManager() : initialized_(false) {
143 BrowserIOSurfaceManager::~BrowserIOSurfaceManager() {
146 bool BrowserIOSurfaceManager::Initialize() {
147 lock_
.AssertAcquired();
148 DCHECK(!server_port_
.is_valid());
150 // Check in with launchd and publish the service name.
152 kern_return_t kr
= bootstrap_check_in(
153 bootstrap_port
, GetMachPortName(getpid()).c_str(), &port
);
154 if (kr
!= KERN_SUCCESS
) {
155 BOOTSTRAP_LOG(ERROR
, kr
) << "bootstrap_check_in";
158 server_port_
.reset(port
);
160 // Start the dispatch source.
161 std::string queue_name
=
162 base::StringPrintf("%s.IOSurfaceManager", base::mac::BaseBundleID());
163 dispatch_source_
.reset(
164 new base::DispatchSourceMach(queue_name
.c_str(), server_port_
.get(), ^{
167 dispatch_source_
->Resume();
172 void BrowserIOSurfaceManager::HandleRequest() {
175 mach_msg_header_t header
;
176 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface
;
177 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface
;
178 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface
;
180 mach_msg_trailer_t trailer
;
182 request
.msg
.header
.msgh_size
= sizeof(request
);
183 request
.msg
.header
.msgh_local_port
= server_port_
.get();
186 mach_msg(&request
.msg
.header
, MACH_RCV_MSG
, 0, sizeof(request
),
187 server_port_
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
188 if (kr
!= KERN_SUCCESS
) {
189 MACH_LOG(ERROR
, kr
) << "mach_msg";
194 mach_msg_header_t header
;
195 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface
;
196 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface
;
199 switch (request
.msg
.header
.msgh_id
) {
200 case IOSurfaceManagerHostMsg_RegisterIOSurface::ID
:
201 if (!HandleRegisterIOSurfaceRequest(request
.msg
.register_io_surface
,
202 &reply
.register_io_surface
)) {
206 case IOSurfaceManagerHostMsg_UnregisterIOSurface::ID
:
207 HandleUnregisterIOSurfaceRequest(request
.msg
.unregister_io_surface
);
208 // Unregister requests are asynchronous and do not require a reply as
209 // there is no guarantee for how quickly an IO surface is removed from
210 // the IOSurfaceManager instance after it has been deleted by a child
213 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID
:
214 if (!HandleAcquireIOSurfaceRequest(request
.msg
.acquire_io_surface
,
215 &reply
.acquire_io_surface
)) {
220 LOG(ERROR
) << "Unknown message received!";
224 kr
= mach_msg(&reply
.header
, MACH_SEND_MSG
| MACH_SEND_TIMEOUT
,
225 reply
.header
.msgh_size
, 0, MACH_PORT_NULL
, kSendReplyTimeoutMs
,
227 if (kr
!= KERN_SUCCESS
) {
228 MACH_LOG(ERROR
, kr
) << "mach_msg";
232 bool BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest(
233 const IOSurfaceManagerHostMsg_RegisterIOSurface
& request
,
234 IOSurfaceManagerMsg_RegisterIOSurfaceReply
* reply
) {
235 base::AutoLock
lock(lock_
);
237 IOSurfaceManagerToken token
;
238 static_assert(sizeof(request
.token_name
) == sizeof(token
.name
),
239 "Mach message token size doesn't match expectation.");
240 token
.SetName(request
.token_name
);
241 if (token
.IsZero() || token
!= gpu_process_token_
) {
242 LOG(ERROR
) << "Illegal message from non-GPU process!";
246 IOSurfaceMapKey
key(IOSurfaceId(request
.io_surface_id
), request
.client_id
);
247 io_surfaces_
.add(key
, make_scoped_ptr(new base::mac::ScopedMachSendRight(
248 request
.io_surface_port
.name
)));
250 reply
->header
.msgh_bits
= MACH_MSGH_BITS_REMOTE(request
.header
.msgh_bits
);
251 reply
->header
.msgh_remote_port
= request
.header
.msgh_remote_port
;
252 reply
->header
.msgh_size
= sizeof(*reply
);
253 reply
->result
= true;
257 bool BrowserIOSurfaceManager::HandleUnregisterIOSurfaceRequest(
258 const IOSurfaceManagerHostMsg_UnregisterIOSurface
& request
) {
259 base::AutoLock
lock(lock_
);
261 IOSurfaceManagerToken token
;
262 static_assert(sizeof(request
.token_name
) == sizeof(token
.name
),
263 "Mach message token size doesn't match expectation.");
264 token
.SetName(request
.token_name
);
265 if (token
.IsZero() || token
!= gpu_process_token_
) {
266 LOG(ERROR
) << "Illegal message from non-GPU process!";
270 IOSurfaceMapKey
key(IOSurfaceId(request
.io_surface_id
), request
.client_id
);
271 io_surfaces_
.erase(key
);
275 bool BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest(
276 const IOSurfaceManagerHostMsg_AcquireIOSurface
& request
,
277 IOSurfaceManagerMsg_AcquireIOSurfaceReply
* reply
) {
278 base::AutoLock
lock(lock_
);
280 IOSurfaceManagerToken token
;
281 static_assert(sizeof(request
.token_name
) == sizeof(token
.name
),
282 "Mach message token size doesn't match expectation.");
283 token
.SetName(request
.token_name
);
284 auto child_process_id_it
= child_process_ids_
.find(token
);
285 if (child_process_id_it
== child_process_ids_
.end()) {
286 LOG(ERROR
) << "Illegal message from non-child process!";
290 reply
->header
.msgh_bits
=
291 MACH_MSGH_BITS_REMOTE(request
.header
.msgh_bits
) | MACH_MSGH_BITS_COMPLEX
;
292 reply
->header
.msgh_remote_port
= request
.header
.msgh_remote_port
;
293 reply
->header
.msgh_size
= sizeof(*reply
);
295 IOSurfaceMapKey
key(IOSurfaceId(request
.io_surface_id
),
296 child_process_id_it
->second
);
297 auto it
= io_surfaces_
.find(key
);
298 if (it
== io_surfaces_
.end()) {
299 LOG(ERROR
) << "Invalid Id for IOSurface " << request
.io_surface_id
;
303 reply
->body
.msgh_descriptor_count
= 1;
304 reply
->io_surface_port
.name
= it
->second
->get();
305 reply
->io_surface_port
.disposition
= MACH_MSG_TYPE_COPY_SEND
;
306 reply
->io_surface_port
.type
= MACH_MSG_PORT_DESCRIPTOR
;
310 } // namespace content