Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / browser / browser_io_surface_manager_mac.cc
blob898fa4b24f6bd6de054498f2b04b792f44a06fd4
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>
9 #include <string>
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"
17 namespace content {
18 namespace {
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(),
24 pid);
27 // Amount of time to wait before giving up when sending a reply message.
28 const int kSendReplyTimeoutMs = 100;
30 } // namespace
32 // static
33 BrowserIOSurfaceManager* BrowserIOSurfaceManager::GetInstance() {
34 return base::Singleton<
35 BrowserIOSurfaceManager,
36 base::LeakySingletonTraits<BrowserIOSurfaceManager>>::get();
39 // static
40 base::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 =
46 bootstrap_look_up(bootstrap_port,
47 GetMachPortNameByPid(pid).c_str(),
48 &port);
49 if (kr != KERN_SUCCESS) {
50 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up";
51 return base::mac::ScopedMachSendRight();
54 return base::mac::ScopedMachSendRight(port);
57 // static
58 std::string BrowserIOSurfaceManager::GetMachPortName() {
59 return GetMachPortNameByPid(getpid());
62 bool BrowserIOSurfaceManager::RegisterIOSurface(IOSurfaceId io_surface_id,
63 int client_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))));
71 return true;
74 void BrowserIOSurfaceManager::UnregisterIOSurface(IOSurfaceId io_surface_id,
75 int client_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_);
87 IOSurfaceMapKey key(
88 io_surface_id,
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;
93 return nullptr;
96 return IOSurfaceLookupFromMachPort(it->second->get());
99 void BrowserIOSurfaceManager::EnsureRunning() {
100 base::AutoLock lock(lock_);
102 if (initialized_)
103 return;
105 // Do not attempt to reinitialize in the event of failure.
106 initialized_ = true;
108 if (!Initialize()) {
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;
137 return token;
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.
159 mach_port_t port;
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";
164 return false;
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(), ^{
173 HandleRequest();
174 }));
175 dispatch_source_->Resume();
177 return true;
180 void BrowserIOSurfaceManager::HandleRequest() {
181 struct {
182 union {
183 mach_msg_header_t header;
184 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface;
185 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface;
186 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface;
187 } msg;
188 mach_msg_trailer_t trailer;
189 } request = {{{0}}};
190 request.msg.header.msgh_size = sizeof(request);
191 request.msg.header.msgh_local_port = server_port_.get();
193 kern_return_t kr =
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";
198 return;
201 union {
202 mach_msg_header_t header;
203 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface;
204 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface;
205 } reply = {{0}};
207 switch (request.msg.header.msgh_id) {
208 case IOSurfaceManagerHostMsg_RegisterIOSurface::ID:
209 HandleRegisterIOSurfaceRequest(request.msg.register_io_surface,
210 &reply.register_io_surface);
211 break;
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
217 // process.
218 return;
219 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID:
220 HandleAcquireIOSurfaceRequest(request.msg.acquire_io_surface,
221 &reply.acquire_io_surface);
222 break;
223 default:
224 LOG(ERROR) << "Unknown message received!";
225 return;
228 kr = mach_msg(&reply.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT,
229 reply.header.msgh_size, 0, MACH_PORT_NULL, kSendReplyTimeoutMs,
230 MACH_PORT_NULL);
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!";
253 return;
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!";
272 return false;
275 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id), request.client_id);
276 io_surfaces_.erase(key);
277 return true;
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!";
302 return;
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;
311 return;
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