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(int 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(int 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(int io_surface_id
) {
76 base::AutoLock
lock(lock_
);
80 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId());
81 auto it
= io_surfaces_
.find(key
);
82 if (it
== io_surfaces_
.end()) {
83 LOG(ERROR
) << "Invalid Id for IOSurface " << io_surface_id
;
87 return IOSurfaceLookupFromMachPort(it
->second
->get());
90 void BrowserIOSurfaceManager::EnsureRunning() {
91 base::AutoLock
lock(lock_
);
96 // Do not attempt to reinitialize in the event of failure.
100 LOG(ERROR
) << "Failed to initialize the BrowserIOSurfaceManager";
104 IOSurfaceManagerToken
BrowserIOSurfaceManager::GenerateGpuProcessToken() {
105 base::AutoLock
lock(lock_
);
107 DCHECK(gpu_process_token_
.IsZero());
108 gpu_process_token_
= IOSurfaceManagerToken::Generate();
109 DCHECK(gpu_process_token_
.Verify());
110 return gpu_process_token_
;
113 void BrowserIOSurfaceManager::InvalidateGpuProcessToken() {
114 base::AutoLock
lock(lock_
);
116 DCHECK(!gpu_process_token_
.IsZero());
117 gpu_process_token_
.SetZero();
118 io_surfaces_
.clear();
121 IOSurfaceManagerToken
BrowserIOSurfaceManager::GenerateChildProcessToken(
122 int child_process_id
) {
123 base::AutoLock
lock(lock_
);
125 IOSurfaceManagerToken token
= IOSurfaceManagerToken::Generate();
126 DCHECK(token
.Verify());
127 child_process_ids_
[token
] = child_process_id
;
131 void BrowserIOSurfaceManager::InvalidateChildProcessToken(
132 const IOSurfaceManagerToken
& token
) {
133 base::AutoLock
lock(lock_
);
135 DCHECK(child_process_ids_
.find(token
) != child_process_ids_
.end());
136 child_process_ids_
.erase(token
);
139 BrowserIOSurfaceManager::BrowserIOSurfaceManager() : initialized_(false) {
142 BrowserIOSurfaceManager::~BrowserIOSurfaceManager() {
145 bool BrowserIOSurfaceManager::Initialize() {
146 lock_
.AssertAcquired();
147 DCHECK(!server_port_
.is_valid());
149 // Check in with launchd and publish the service name.
151 kern_return_t kr
= bootstrap_check_in(
152 bootstrap_port
, GetMachPortName(getpid()).c_str(), &port
);
153 if (kr
!= KERN_SUCCESS
) {
154 BOOTSTRAP_LOG(ERROR
, kr
) << "bootstrap_check_in";
157 server_port_
.reset(port
);
159 // Start the dispatch source.
160 std::string queue_name
=
161 base::StringPrintf("%s.IOSurfaceManager", base::mac::BaseBundleID());
162 dispatch_source_
.reset(
163 new base::DispatchSourceMach(queue_name
.c_str(), server_port_
.get(), ^{
166 dispatch_source_
->Resume();
171 void BrowserIOSurfaceManager::HandleRequest() {
174 mach_msg_header_t header
;
175 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface
;
176 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface
;
177 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface
;
179 mach_msg_trailer_t trailer
;
181 request
.msg
.header
.msgh_size
= sizeof(request
);
182 request
.msg
.header
.msgh_local_port
= server_port_
.get();
185 mach_msg(&request
.msg
.header
, MACH_RCV_MSG
, 0, sizeof(request
),
186 server_port_
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
187 if (kr
!= KERN_SUCCESS
) {
188 MACH_LOG(ERROR
, kr
) << "mach_msg";
193 mach_msg_header_t header
;
194 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface
;
195 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface
;
198 switch (request
.msg
.header
.msgh_id
) {
199 case IOSurfaceManagerHostMsg_RegisterIOSurface::ID
:
200 if (!HandleRegisterIOSurfaceRequest(request
.msg
.register_io_surface
,
201 &reply
.register_io_surface
)) {
205 case IOSurfaceManagerHostMsg_UnregisterIOSurface::ID
:
206 HandleUnregisterIOSurfaceRequest(request
.msg
.unregister_io_surface
);
207 // Unregister requests are asynchronous and do not require a reply as
208 // there is no guarantee for how quickly an IO surface is removed from
209 // the IOSurfaceManager instance after it has been deleted by a child
212 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID
:
213 if (!HandleAcquireIOSurfaceRequest(request
.msg
.acquire_io_surface
,
214 &reply
.acquire_io_surface
)) {
219 LOG(ERROR
) << "Unknown message received!";
223 kr
= mach_msg(&reply
.header
, MACH_SEND_MSG
| MACH_SEND_TIMEOUT
,
224 reply
.header
.msgh_size
, 0, MACH_PORT_NULL
, kSendReplyTimeoutMs
,
226 if (kr
!= KERN_SUCCESS
) {
227 MACH_LOG(ERROR
, kr
) << "mach_msg";
231 bool BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest(
232 const IOSurfaceManagerHostMsg_RegisterIOSurface
& request
,
233 IOSurfaceManagerMsg_RegisterIOSurfaceReply
* reply
) {
234 base::AutoLock
lock(lock_
);
236 IOSurfaceManagerToken token
;
237 static_assert(sizeof(request
.token_name
) == sizeof(token
.name
),
238 "Mach message token size doesn't match expectation.");
239 token
.SetName(request
.token_name
);
240 if (token
.IsZero() || token
!= gpu_process_token_
) {
241 LOG(ERROR
) << "Illegal message from non-GPU process!";
245 IOSurfaceMapKey
key(request
.io_surface_id
, request
.client_id
);
246 io_surfaces_
.add(key
, make_scoped_ptr(new base::mac::ScopedMachSendRight(
247 request
.io_surface_port
.name
)));
249 reply
->header
.msgh_bits
= MACH_MSGH_BITS_REMOTE(request
.header
.msgh_bits
);
250 reply
->header
.msgh_remote_port
= request
.header
.msgh_remote_port
;
251 reply
->header
.msgh_size
= sizeof(*reply
);
252 reply
->result
= true;
256 bool BrowserIOSurfaceManager::HandleUnregisterIOSurfaceRequest(
257 const IOSurfaceManagerHostMsg_UnregisterIOSurface
& request
) {
258 base::AutoLock
lock(lock_
);
260 IOSurfaceManagerToken token
;
261 static_assert(sizeof(request
.token_name
) == sizeof(token
.name
),
262 "Mach message token size doesn't match expectation.");
263 token
.SetName(request
.token_name
);
264 if (token
.IsZero() || token
!= gpu_process_token_
) {
265 LOG(ERROR
) << "Illegal message from non-GPU process!";
269 IOSurfaceMapKey
key(request
.io_surface_id
, request
.client_id
);
270 io_surfaces_
.erase(key
);
274 bool BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest(
275 const IOSurfaceManagerHostMsg_AcquireIOSurface
& request
,
276 IOSurfaceManagerMsg_AcquireIOSurfaceReply
* reply
) {
277 base::AutoLock
lock(lock_
);
279 IOSurfaceManagerToken token
;
280 static_assert(sizeof(request
.token_name
) == sizeof(token
.name
),
281 "Mach message token size doesn't match expectation.");
282 token
.SetName(request
.token_name
);
283 auto child_process_id_it
= child_process_ids_
.find(token
);
284 if (child_process_id_it
== child_process_ids_
.end()) {
285 LOG(ERROR
) << "Illegal message from non-child process!";
289 reply
->header
.msgh_bits
=
290 MACH_MSGH_BITS_REMOTE(request
.header
.msgh_bits
) | MACH_MSGH_BITS_COMPLEX
;
291 reply
->header
.msgh_remote_port
= request
.header
.msgh_remote_port
;
292 reply
->header
.msgh_size
= sizeof(*reply
);
294 IOSurfaceMapKey
key(request
.io_surface_id
, child_process_id_it
->second
);
295 auto it
= io_surfaces_
.find(key
);
296 if (it
== io_surfaces_
.end()) {
297 LOG(ERROR
) << "Invalid Id for IOSurface " << request
.io_surface_id
;
301 reply
->body
.msgh_descriptor_count
= 1;
302 reply
->io_surface_port
.name
= it
->second
->get();
303 reply
->io_surface_port
.disposition
= MACH_MSG_TYPE_COPY_SEND
;
304 reply
->io_surface_port
.type
= MACH_MSG_PORT_DESCRIPTOR
;
308 } // namespace content