Don't trigger HW acceleration from Toasts on low-end devices.
[chromium-blink-merge.git] / content / browser / browser_io_surface_manager_mac.cc
blob6446d67722e901733df529d807eccbf6c5697a06
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 GetMachPortName(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 Singleton<BrowserIOSurfaceManager,
35 LeakySingletonTraits<BrowserIOSurfaceManager>>::get();
38 // static
39 base::mac::ScopedMachSendRight BrowserIOSurfaceManager::LookupServicePort(
40 pid_t pid) {
41 // Look up the named IOSurfaceManager port that's been registered with
42 // the bootstrap server.
43 mach_port_t port;
44 kern_return_t kr =
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,
55 int client_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))));
63 return true;
66 void BrowserIOSurfaceManager::UnregisterIOSurface(int io_surface_id,
67 int client_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_);
78 IOSurfaceMapKey key(
79 io_surface_id,
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;
84 return nullptr;
87 return IOSurfaceLookupFromMachPort(it->second->get());
90 void BrowserIOSurfaceManager::EnsureRunning() {
91 base::AutoLock lock(lock_);
93 if (initialized_)
94 return;
96 // Do not attempt to reinitialize in the event of failure.
97 initialized_ = true;
99 if (!Initialize()) {
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;
128 return token;
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.
150 mach_port_t port;
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";
155 return false;
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(), ^{
164 HandleRequest();
165 }));
166 dispatch_source_->Resume();
168 return true;
171 void BrowserIOSurfaceManager::HandleRequest() {
172 struct {
173 union {
174 mach_msg_header_t header;
175 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface;
176 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface;
177 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface;
178 } msg;
179 mach_msg_trailer_t trailer;
180 } request = {{{0}}};
181 request.msg.header.msgh_size = sizeof(request);
182 request.msg.header.msgh_local_port = server_port_.get();
184 kern_return_t kr =
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";
189 return;
192 union {
193 mach_msg_header_t header;
194 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface;
195 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface;
196 } reply = {{0}};
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)) {
202 return;
204 break;
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
210 // process.
211 return;
212 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID:
213 if (!HandleAcquireIOSurfaceRequest(request.msg.acquire_io_surface,
214 &reply.acquire_io_surface)) {
215 return;
217 break;
218 default:
219 LOG(ERROR) << "Unknown message received!";
220 return;
223 kr = mach_msg(&reply.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT,
224 reply.header.msgh_size, 0, MACH_PORT_NULL, kSendReplyTimeoutMs,
225 MACH_PORT_NULL);
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!";
242 return false;
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;
253 return 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!";
266 return false;
269 IOSurfaceMapKey key(request.io_surface_id, request.client_id);
270 io_surfaces_.erase(key);
271 return true;
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!";
286 return false;
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;
298 return true;
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;
305 return true;
308 } // namespace content