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
GetMachPortNameByPid(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 base::Singleton
<
35 BrowserIOSurfaceManager
,
36 base::LeakySingletonTraits
<BrowserIOSurfaceManager
>>::get();
40 base::mac::ScopedMachSendRight
BrowserIOSurfaceManager::LookupServicePort(
42 // Look up the named IOSurfaceManager port that's been registered with
43 // the bootstrap server.
46 bootstrap_look_up(bootstrap_port
,
47 GetMachPortNameByPid(pid
).c_str(),
49 if (kr
!= KERN_SUCCESS
) {
50 BOOTSTRAP_LOG(ERROR
, kr
) << "bootstrap_look_up";
51 return base::mac::ScopedMachSendRight();
54 return base::mac::ScopedMachSendRight(port
);
58 std::string
BrowserIOSurfaceManager::GetMachPortName() {
59 return GetMachPortNameByPid(getpid());
62 bool BrowserIOSurfaceManager::RegisterIOSurface(IOSurfaceId io_surface_id
,
64 IOSurfaceRef io_surface
) {
65 base::AutoLock
lock(lock_
);
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
))));
74 void BrowserIOSurfaceManager::UnregisterIOSurface(IOSurfaceId io_surface_id
,
76 base::AutoLock
lock(lock_
);
78 IOSurfaceMapKey
key(io_surface_id
, client_id
);
79 DCHECK(io_surfaces_
.find(key
) != io_surfaces_
.end());
80 io_surfaces_
.erase(key
);
83 IOSurfaceRef
BrowserIOSurfaceManager::AcquireIOSurface(
84 IOSurfaceId io_surface_id
) {
85 base::AutoLock
lock(lock_
);
89 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId());
90 auto it
= io_surfaces_
.find(key
);
91 if (it
== io_surfaces_
.end()) {
92 LOG(ERROR
) << "Invalid Id for IOSurface " << io_surface_id
.id
;
96 return IOSurfaceLookupFromMachPort(it
->second
->get());
99 void BrowserIOSurfaceManager::EnsureRunning() {
100 base::AutoLock
lock(lock_
);
105 // Do not attempt to reinitialize in the event of failure.
109 LOG(ERROR
) << "Failed to initialize the BrowserIOSurfaceManager";
113 IOSurfaceManagerToken
BrowserIOSurfaceManager::GenerateGpuProcessToken() {
114 base::AutoLock
lock(lock_
);
116 DCHECK(gpu_process_token_
.IsZero());
117 gpu_process_token_
= IOSurfaceManagerToken::Generate();
118 DCHECK(gpu_process_token_
.Verify());
119 return gpu_process_token_
;
122 void BrowserIOSurfaceManager::InvalidateGpuProcessToken() {
123 base::AutoLock
lock(lock_
);
125 DCHECK(!gpu_process_token_
.IsZero());
126 gpu_process_token_
.SetZero();
127 io_surfaces_
.clear();
130 IOSurfaceManagerToken
BrowserIOSurfaceManager::GenerateChildProcessToken(
131 int child_process_id
) {
132 base::AutoLock
lock(lock_
);
134 IOSurfaceManagerToken token
= IOSurfaceManagerToken::Generate();
135 DCHECK(token
.Verify());
136 child_process_ids_
[token
] = child_process_id
;
140 void BrowserIOSurfaceManager::InvalidateChildProcessToken(
141 const IOSurfaceManagerToken
& token
) {
142 base::AutoLock
lock(lock_
);
144 DCHECK(child_process_ids_
.find(token
) != child_process_ids_
.end());
145 child_process_ids_
.erase(token
);
148 BrowserIOSurfaceManager::BrowserIOSurfaceManager() : initialized_(false) {
151 BrowserIOSurfaceManager::~BrowserIOSurfaceManager() {
154 bool BrowserIOSurfaceManager::Initialize() {
155 lock_
.AssertAcquired();
156 DCHECK(!server_port_
.is_valid());
158 // Check in with launchd and publish the service name.
160 kern_return_t kr
= bootstrap_check_in(
161 bootstrap_port
, GetMachPortName().c_str(), &port
);
162 if (kr
!= KERN_SUCCESS
) {
163 BOOTSTRAP_LOG(ERROR
, kr
) << "bootstrap_check_in";
166 server_port_
.reset(port
);
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(), ^{
175 dispatch_source_
->Resume();
180 void BrowserIOSurfaceManager::HandleRequest() {
183 mach_msg_header_t header
;
184 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface
;
185 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface
;
186 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface
;
188 mach_msg_trailer_t trailer
;
190 request
.msg
.header
.msgh_size
= sizeof(request
);
191 request
.msg
.header
.msgh_local_port
= server_port_
.get();
194 mach_msg(&request
.msg
.header
, MACH_RCV_MSG
, 0, sizeof(request
),
195 server_port_
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
196 if (kr
!= KERN_SUCCESS
) {
197 MACH_LOG(ERROR
, kr
) << "mach_msg";
202 mach_msg_header_t header
;
203 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface
;
204 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface
;
207 switch (request
.msg
.header
.msgh_id
) {
208 case IOSurfaceManagerHostMsg_RegisterIOSurface::ID
:
209 HandleRegisterIOSurfaceRequest(request
.msg
.register_io_surface
,
210 &reply
.register_io_surface
);
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
219 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID
:
220 HandleAcquireIOSurfaceRequest(request
.msg
.acquire_io_surface
,
221 &reply
.acquire_io_surface
);
224 LOG(ERROR
) << "Unknown message received!";
228 kr
= mach_msg(&reply
.header
, MACH_SEND_MSG
| MACH_SEND_TIMEOUT
,
229 reply
.header
.msgh_size
, 0, MACH_PORT_NULL
, kSendReplyTimeoutMs
,
231 if (kr
!= KERN_SUCCESS
) {
232 MACH_LOG(ERROR
, kr
) << "mach_msg";
236 void BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest(
237 const IOSurfaceManagerHostMsg_RegisterIOSurface
& request
,
238 IOSurfaceManagerMsg_RegisterIOSurfaceReply
* reply
) {
239 base::AutoLock
lock(lock_
);
241 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;
247 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
);
251 if (token
.IsZero() || token
!= gpu_process_token_
) {
252 LOG(ERROR
) << "Illegal message from non-GPU process!";
256 IOSurfaceMapKey
key(IOSurfaceId(request
.io_surface_id
), request
.client_id
);
257 io_surfaces_
.add(key
, make_scoped_ptr(new base::mac::ScopedMachSendRight(
258 request
.io_surface_port
.name
)));
259 reply
->result
= true;
262 bool BrowserIOSurfaceManager::HandleUnregisterIOSurfaceRequest(
263 const IOSurfaceManagerHostMsg_UnregisterIOSurface
& request
) {
264 base::AutoLock
lock(lock_
);
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
);
270 if (token
.IsZero() || token
!= gpu_process_token_
) {
271 LOG(ERROR
) << "Illegal message from non-GPU process!";
275 IOSurfaceMapKey
key(IOSurfaceId(request
.io_surface_id
), request
.client_id
);
276 io_surfaces_
.erase(key
);
280 void BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest(
281 const IOSurfaceManagerHostMsg_AcquireIOSurface
& request
,
282 IOSurfaceManagerMsg_AcquireIOSurfaceReply
* reply
) {
283 base::AutoLock
lock(lock_
);
285 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;
295 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!";
305 reply
->result
= true;
306 IOSurfaceMapKey
key(IOSurfaceId(request
.io_surface_id
),
307 child_process_id_it
->second
);
308 auto it
= io_surfaces_
.find(key
);
309 if (it
== io_surfaces_
.end()) {
310 LOG(ERROR
) << "Invalid Id for IOSurface " << request
.io_surface_id
;
314 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
;
320 } // namespace content