Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(480)

Side by Side Diff: content/browser/browser_io_surface_manager_mac.cc

Issue 1137453002: content: Pass IOSurface references using Mach IPC. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: static_assert Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "content/browser/browser_io_surface_manager_mac.h"
6
7 #include <servers/bootstrap.h>
8
9 #include <string>
10
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"
16
17 namespace content {
18 namespace {
19
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);
25 }
26
27 // Amount of time to wait before giving up when sending a reply message.
28 const int kSendReplyTimeoutMs = 100;
29
30 } // namespace
31
32 // static
33 BrowserIOSurfaceManager* BrowserIOSurfaceManager::GetInstance() {
34 return Singleton<BrowserIOSurfaceManager,
35 LeakySingletonTraits<BrowserIOSurfaceManager>>::get();
36 }
37
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();
49 }
50
51 return base::mac::ScopedMachSendRight(port);
52 }
53
54 bool BrowserIOSurfaceManager::RegisterIOSurface(int io_surface_id,
55 int client_id,
56 IOSurfaceRef io_surface) {
57 base::AutoLock lock(lock_);
58
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;
64 }
65
66 void BrowserIOSurfaceManager::UnregisterIOSurface(int io_surface_id,
67 int client_id) {
68 base::AutoLock lock(lock_);
69
70 IOSurfaceMapKey key(io_surface_id, client_id);
71 DCHECK(io_surfaces_.find(key) != io_surfaces_.end());
72 io_surfaces_.erase(key);
73 }
74
75 IOSurfaceRef BrowserIOSurfaceManager::AcquireIOSurface(int io_surface_id) {
76 base::AutoLock lock(lock_);
77
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;
85 }
86
87 return IOSurfaceLookupFromMachPort(it->second->get());
88 }
89
90 void BrowserIOSurfaceManager::EnsureRunning() {
91 base::AutoLock lock(lock_);
92
93 if (initialized_)
94 return;
95
96 // Do not attempt to reinitialize in the event of failure.
97 initialized_ = true;
98
99 if (!Initialize()) {
100 LOG(ERROR) << "Failed to initialize the BrowserIOSurfaceManager";
101 }
102 }
103
104 IOSurfaceManagerToken BrowserIOSurfaceManager::GetGpuProcessToken() const {
105 return gpu_process_token_;
106 }
107
108 IOSurfaceManagerToken BrowserIOSurfaceManager::GenerateChildProcessToken(
109 int child_process_id) {
110 base::AutoLock lock(lock_);
111
112 IOSurfaceManagerToken token = IOSurfaceManagerToken::Generate();
113 DCHECK(token.Verify());
114 child_process_ids_[token] = child_process_id;
115 return token;
116 }
117
118 void BrowserIOSurfaceManager::InvalidateChildProcessToken(
119 const IOSurfaceManagerToken& token) {
120 base::AutoLock lock(lock_);
121
122 DCHECK(child_process_ids_.find(token) != child_process_ids_.end());
123 child_process_ids_.erase(token);
124 }
125
126 BrowserIOSurfaceManager::BrowserIOSurfaceManager()
127 : initialized_(false),
128 gpu_process_token_(IOSurfaceManagerToken::Generate()) {
129 DCHECK(gpu_process_token_.Verify());
130 }
131
132 BrowserIOSurfaceManager::~BrowserIOSurfaceManager() {
133 }
134
135 bool BrowserIOSurfaceManager::Initialize() {
136 lock_.AssertAcquired();
137 DCHECK(!server_port_.is_valid());
138
139 // Check in with launchd and publish the service name.
140 mach_port_t port;
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";
145 return false;
146 }
147 server_port_.reset(port);
148
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(), ^{
154 HandleRequest();
155 }));
156 dispatch_source_->Resume();
157
158 return true;
159 }
160
161 void BrowserIOSurfaceManager::HandleRequest() {
162 struct {
163 union {
164 mach_msg_header_t header;
165 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface;
166 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface;
167 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface;
168 } msg;
169 mach_msg_trailer_t trailer;
170 } request = {{{0}}};
171 request.msg.header.msgh_size = sizeof(request);
172 request.msg.header.msgh_local_port = server_port_.get();
173
174 kern_return_t kr =
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";
179 return;
180 }
181
182 union {
183 mach_msg_header_t header;
184 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface;
185 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface;
186 } reply = {{0}};
187
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)) {
192 return;
193 }
194 break;
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
200 // process.
201 return;
202 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID:
203 if (!HandleAcquireIOSurfaceRequest(request.msg.acquire_io_surface,
204 &reply.acquire_io_surface)) {
205 return;
206 }
207 break;
208 default:
209 LOG(ERROR) << "Unknown message received!";
210 return;
211 }
212
213 kr = mach_msg(&reply.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT,
214 reply.header.msgh_size, 0, MACH_PORT_NULL, kSendReplyTimeoutMs,
215 MACH_PORT_NULL);
216 if (kr != KERN_SUCCESS) {
217 MACH_LOG(ERROR, kr) << "mach_msg";
218 }
219 }
220
221 bool BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest(
222 const IOSurfaceManagerHostMsg_RegisterIOSurface& request,
223 IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply) {
224 base::AutoLock lock(lock_);
225
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!";
232 return false;
233 }
234
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)));
238
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;
243 return true;
244 }
245
246 bool BrowserIOSurfaceManager::HandleUnregisterIOSurfaceRequest(
247 const IOSurfaceManagerHostMsg_UnregisterIOSurface& request) {
248 base::AutoLock lock(lock_);
249
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!";
256 return false;
257 }
258
259 IOSurfaceMapKey key(request.io_surface_id, request.client_id);
260 io_surfaces_.erase(key);
261 return true;
262 }
263
264 bool BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest(
265 const IOSurfaceManagerHostMsg_AcquireIOSurface& request,
266 IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply) {
267 base::AutoLock lock(lock_);
268
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!";
276 return false;
277 }
278
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;
283 return false;
284 }
285
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;
294 return true;
295 }
296
297 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/browser_io_surface_manager_mac.h ('k') | content/browser/browser_io_surface_manager_mac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698