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::GetGpuProcessToken() const {
105 return gpu_process_token_
;
108 IOSurfaceManagerToken
BrowserIOSurfaceManager::GenerateChildProcessToken(
109 int child_process_id
) {
110 base::AutoLock
lock(lock_
);
112 IOSurfaceManagerToken token
= IOSurfaceManagerToken::Generate();
113 DCHECK(token
.Verify());
114 child_process_ids_
[token
] = child_process_id
;
118 void BrowserIOSurfaceManager::InvalidateChildProcessToken(
119 const IOSurfaceManagerToken
& token
) {
120 base::AutoLock
lock(lock_
);
122 DCHECK(child_process_ids_
.find(token
) != child_process_ids_
.end());
123 child_process_ids_
.erase(token
);
126 BrowserIOSurfaceManager::BrowserIOSurfaceManager()
127 : initialized_(false),
128 gpu_process_token_(IOSurfaceManagerToken::Generate()) {
129 DCHECK(gpu_process_token_
.Verify());
132 BrowserIOSurfaceManager::~BrowserIOSurfaceManager() {
135 bool BrowserIOSurfaceManager::Initialize() {
136 lock_
.AssertAcquired();
137 DCHECK(!server_port_
.is_valid());
139 // Check in with launchd and publish the service name.
141 kern_return_t kr
= bootstrap_check_in(
142 bootstrap_port
, GetMachPortName(getpid()).c_str(), &port
);
143 if (kr
!= KERN_SUCCESS
) {
144 BOOTSTRAP_LOG(ERROR
, kr
) << "bootstrap_check_in";
147 server_port_
.reset(port
);
149 // Start the dispatch source.
150 std::string queue_name
=
151 base::StringPrintf("%s.IOSurfaceManager", base::mac::BaseBundleID());
152 dispatch_source_
.reset(
153 new base::DispatchSourceMach(queue_name
.c_str(), server_port_
.get(), ^{
156 dispatch_source_
->Resume();
161 void BrowserIOSurfaceManager::HandleRequest() {
164 mach_msg_header_t header
;
165 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface
;
166 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface
;
167 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface
;
169 mach_msg_trailer_t trailer
;
171 request
.msg
.header
.msgh_size
= sizeof(request
);
172 request
.msg
.header
.msgh_local_port
= server_port_
.get();
175 mach_msg(&request
.msg
.header
, MACH_RCV_MSG
, 0, sizeof(request
),
176 server_port_
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
177 if (kr
!= KERN_SUCCESS
) {
178 MACH_LOG(ERROR
, kr
) << "mach_msg";
183 mach_msg_header_t header
;
184 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface
;
185 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface
;
188 switch (request
.msg
.header
.msgh_id
) {
189 case IOSurfaceManagerHostMsg_RegisterIOSurface::ID
:
190 if (!HandleRegisterIOSurfaceRequest(request
.msg
.register_io_surface
,
191 &reply
.register_io_surface
)) {
195 case IOSurfaceManagerHostMsg_UnregisterIOSurface::ID
:
196 HandleUnregisterIOSurfaceRequest(request
.msg
.unregister_io_surface
);
197 // Unregister requests are asynchronous and do not require a reply as
198 // there is no guarantee for how quickly an IO surface is removed from
199 // the IOSurfaceManager instance after it has been deleted by a child
202 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID
:
203 if (!HandleAcquireIOSurfaceRequest(request
.msg
.acquire_io_surface
,
204 &reply
.acquire_io_surface
)) {
209 LOG(ERROR
) << "Unknown message received!";
213 kr
= mach_msg(&reply
.header
, MACH_SEND_MSG
| MACH_SEND_TIMEOUT
,
214 reply
.header
.msgh_size
, 0, MACH_PORT_NULL
, kSendReplyTimeoutMs
,
216 if (kr
!= KERN_SUCCESS
) {
217 MACH_LOG(ERROR
, kr
) << "mach_msg";
221 bool BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest(
222 const IOSurfaceManagerHostMsg_RegisterIOSurface
& request
,
223 IOSurfaceManagerMsg_RegisterIOSurfaceReply
* reply
) {
224 base::AutoLock
lock(lock_
);
226 IOSurfaceManagerToken token
;
227 static_assert(sizeof(request
.token_name
) == sizeof(token
.name
),
228 "Mach message token size doesn't match expectation.");
229 token
.SetName(request
.token_name
);
230 if (token
!= gpu_process_token_
) {
231 LOG(ERROR
) << "Illegal message from non-GPU process!";
235 IOSurfaceMapKey
key(request
.io_surface_id
, request
.client_id
);
236 io_surfaces_
.add(key
, make_scoped_ptr(new base::mac::ScopedMachSendRight(
237 request
.io_surface_port
.name
)));
239 reply
->header
.msgh_bits
= MACH_MSGH_BITS_REMOTE(request
.header
.msgh_bits
);
240 reply
->header
.msgh_remote_port
= request
.header
.msgh_remote_port
;
241 reply
->header
.msgh_size
= sizeof(*reply
);
242 reply
->result
= true;
246 bool BrowserIOSurfaceManager::HandleUnregisterIOSurfaceRequest(
247 const IOSurfaceManagerHostMsg_UnregisterIOSurface
& request
) {
248 base::AutoLock
lock(lock_
);
250 IOSurfaceManagerToken token
;
251 static_assert(sizeof(request
.token_name
) == sizeof(token
.name
),
252 "Mach message token size doesn't match expectation.");
253 token
.SetName(request
.token_name
);
254 if (token
!= gpu_process_token_
) {
255 LOG(ERROR
) << "Illegal message from non-GPU process!";
259 IOSurfaceMapKey
key(request
.io_surface_id
, request
.client_id
);
260 io_surfaces_
.erase(key
);
264 bool BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest(
265 const IOSurfaceManagerHostMsg_AcquireIOSurface
& request
,
266 IOSurfaceManagerMsg_AcquireIOSurfaceReply
* reply
) {
267 base::AutoLock
lock(lock_
);
269 IOSurfaceManagerToken token
;
270 static_assert(sizeof(request
.token_name
) == sizeof(token
.name
),
271 "Mach message token size doesn't match expectation.");
272 token
.SetName(request
.token_name
);
273 auto child_process_id_it
= child_process_ids_
.find(token
);
274 if (child_process_id_it
== child_process_ids_
.end()) {
275 LOG(ERROR
) << "Illegal message from non-child process!";
279 IOSurfaceMapKey
key(request
.io_surface_id
, child_process_id_it
->second
);
280 auto it
= io_surfaces_
.find(key
);
281 if (it
== io_surfaces_
.end()) {
282 LOG(ERROR
) << "Invalid Id for IOSurface " << request
.io_surface_id
;
286 reply
->header
.msgh_bits
=
287 MACH_MSGH_BITS_REMOTE(request
.header
.msgh_bits
) | MACH_MSGH_BITS_COMPLEX
;
288 reply
->header
.msgh_remote_port
= request
.header
.msgh_remote_port
;
289 reply
->header
.msgh_size
= sizeof(*reply
);
290 reply
->body
.msgh_descriptor_count
= 1;
291 reply
->io_surface_port
.name
= it
->second
->get();
292 reply
->io_surface_port
.disposition
= MACH_MSG_TYPE_COPY_SEND
;
293 reply
->io_surface_port
.type
= MACH_MSG_PORT_DESCRIPTOR
;
297 } // namespace content