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 #ifndef CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_
6 #define CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/mac/dispatch_source_mach.h"
15 #include "base/mac/scoped_mach_port.h"
16 #include "base/macros.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/singleton.h"
19 #include "base/synchronization/lock.h"
20 #include "content/common/content_export.h"
21 #include "content/common/mac/io_surface_manager.h"
22 #include "content/common/mac/io_surface_manager_messages.h"
23 #include "content/common/mac/io_surface_manager_token.h"
27 // Implementation of IOSurfaceManager that provides a mechanism for child
28 // processes to register and acquire IOSurfaces through a Mach service.
29 class CONTENT_EXPORT BrowserIOSurfaceManager
: public IOSurfaceManager
{
31 // Returns the global BrowserIOSurfaceManager.
32 static BrowserIOSurfaceManager
* GetInstance();
34 // Look up the IOSurfaceManager service port that's been registered with
35 // the bootstrap server. |pid| is the process ID of the service.
36 static base::mac::ScopedMachSendRight
LookupServicePort(pid_t pid
);
38 // Overridden from IOSurfaceManager:
39 bool RegisterIOSurface(int io_surface_id
,
41 IOSurfaceRef io_surface
) override
;
42 void UnregisterIOSurface(int io_surface_id
, int client_id
) override
;
43 IOSurfaceRef
AcquireIOSurface(int io_surface_id
) override
;
45 // Performs any necessary setup that cannot happen in the constructor.
48 // Generate a unique unguessable token that the GPU process can use to
49 // register/unregister IOSurface for use by clients.
50 IOSurfaceManagerToken
GenerateGpuProcessToken();
52 // Invalidate the previously generated GPU process token.
53 void InvalidateGpuProcessToken();
55 // Generate a unique unguessable token that the child process associated
56 // |child_process_id| can use to acquire IOSurface references.
57 IOSurfaceManagerToken
GenerateChildProcessToken(int child_process_id
);
59 // Invalidate a previously generated child process token.
60 void InvalidateChildProcessToken(const IOSurfaceManagerToken
& token
);
63 friend class BrowserIOSurfaceManagerTest
;
64 friend struct DefaultSingletonTraits
<BrowserIOSurfaceManager
>;
66 BrowserIOSurfaceManager();
67 ~BrowserIOSurfaceManager() override
;
69 // Performs any initialization work.
72 // Message handler that is invoked on |dispatch_source_| when an
73 // incoming message needs to be received.
76 // Message handlers that are invoked from HandleRequest.
77 bool HandleRegisterIOSurfaceRequest(
78 const IOSurfaceManagerHostMsg_RegisterIOSurface
& request
,
79 IOSurfaceManagerMsg_RegisterIOSurfaceReply
* reply
);
80 bool HandleUnregisterIOSurfaceRequest(
81 const IOSurfaceManagerHostMsg_UnregisterIOSurface
& request
);
82 bool HandleAcquireIOSurfaceRequest(
83 const IOSurfaceManagerHostMsg_AcquireIOSurface
& request
,
84 IOSurfaceManagerMsg_AcquireIOSurfaceReply
* reply
);
86 // Whether or not the class has been initialized.
89 // The Mach port on which the server listens.
90 base::mac::ScopedMachReceiveRight server_port_
;
92 // The dispatch source and queue on which Mach messages will be received.
93 scoped_ptr
<base::DispatchSourceMach
> dispatch_source_
;
95 // Stores the IOSurfaces for all GPU clients. The key contains the IOSurface
96 // id and the Child process unique id of the owner.
97 using IOSurfaceMapKey
= std::pair
<int, int>;
99 base::ScopedPtrHashMap
<IOSurfaceMapKey
,
100 scoped_ptr
<base::mac::ScopedMachSendRight
>>;
101 IOSurfaceMap io_surfaces_
;
103 // Stores the Child process unique id (RenderProcessHost ID) for every
105 using ChildProcessIdMap
= std::map
<IOSurfaceManagerToken
, int>;
106 ChildProcessIdMap child_process_ids_
;
108 // Stores the GPU process token.
109 IOSurfaceManagerToken gpu_process_token_
;
111 // Mutex that guards |initialized_|, |io_surfaces_|, |child_process_ids_|
112 // and |gpu_process_token_|.
115 DISALLOW_COPY_AND_ASSIGN(BrowserIOSurfaceManager
);
118 } // namespace content
120 #endif // CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_