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 // TODO(ericrk): Use gfx::GenericSharedMemoryId as the |io_surface_id| in
28 // this file. Allows for more type-safe usage of GpuMemoryBufferIds as the
29 // type of the |io_surface_id|, as it is a typedef of
30 // gfx::GenericSharedMemoryId.
32 // Implementation of IOSurfaceManager that provides a mechanism for child
33 // processes to register and acquire IOSurfaces through a Mach service.
34 class CONTENT_EXPORT BrowserIOSurfaceManager
: public IOSurfaceManager
{
36 // Returns the global BrowserIOSurfaceManager.
37 static BrowserIOSurfaceManager
* GetInstance();
39 // Look up the IOSurfaceManager service port that's been registered with
40 // the bootstrap server. |pid| is the process ID of the service.
41 static base::mac::ScopedMachSendRight
LookupServicePort(pid_t pid
);
43 // Returns the name of the service registered with the bootstrap server.
44 static std::string
GetMachPortName();
46 // Overridden from IOSurfaceManager:
47 bool RegisterIOSurface(IOSurfaceId io_surface_id
,
49 IOSurfaceRef io_surface
) override
;
50 void UnregisterIOSurface(IOSurfaceId io_surface_id
, int client_id
) override
;
51 IOSurfaceRef
AcquireIOSurface(IOSurfaceId io_surface_id
) override
;
53 // Performs any necessary setup that cannot happen in the constructor.
56 // Generate a unique unguessable token that the GPU process can use to
57 // register/unregister IOSurface for use by clients.
58 IOSurfaceManagerToken
GenerateGpuProcessToken();
60 // Invalidate the previously generated GPU process token.
61 void InvalidateGpuProcessToken();
63 // Generate a unique unguessable token that the child process associated
64 // |child_process_id| can use to acquire IOSurface references.
65 IOSurfaceManagerToken
GenerateChildProcessToken(int child_process_id
);
67 // Invalidate a previously generated child process token.
68 void InvalidateChildProcessToken(const IOSurfaceManagerToken
& token
);
71 friend class BrowserIOSurfaceManagerTest
;
72 friend struct base::DefaultSingletonTraits
<BrowserIOSurfaceManager
>;
74 BrowserIOSurfaceManager();
75 ~BrowserIOSurfaceManager() override
;
77 // Performs any initialization work.
80 // Message handler that is invoked on |dispatch_source_| when an
81 // incoming message needs to be received.
84 // Message handlers that are invoked from HandleRequest.
85 void HandleRegisterIOSurfaceRequest(
86 const IOSurfaceManagerHostMsg_RegisterIOSurface
& request
,
87 IOSurfaceManagerMsg_RegisterIOSurfaceReply
* reply
);
88 bool HandleUnregisterIOSurfaceRequest(
89 const IOSurfaceManagerHostMsg_UnregisterIOSurface
& request
);
90 void HandleAcquireIOSurfaceRequest(
91 const IOSurfaceManagerHostMsg_AcquireIOSurface
& request
,
92 IOSurfaceManagerMsg_AcquireIOSurfaceReply
* reply
);
94 // Whether or not the class has been initialized.
97 // The Mach port on which the server listens.
98 base::mac::ScopedMachReceiveRight server_port_
;
100 // The dispatch source and queue on which Mach messages will be received.
101 scoped_ptr
<base::DispatchSourceMach
> dispatch_source_
;
103 // Stores the IOSurfaces for all GPU clients. The key contains the IOSurface
104 // id and the Child process unique id of the owner.
105 using IOSurfaceMapKey
= std::pair
<IOSurfaceId
, int>;
107 base::ScopedPtrHashMap
<IOSurfaceMapKey
,
108 scoped_ptr
<base::mac::ScopedMachSendRight
>>;
109 IOSurfaceMap io_surfaces_
;
111 // Stores the Child process unique id (RenderProcessHost ID) for every
113 using ChildProcessIdMap
= std::map
<IOSurfaceManagerToken
, int>;
114 ChildProcessIdMap child_process_ids_
;
116 // Stores the GPU process token.
117 IOSurfaceManagerToken gpu_process_token_
;
119 // Mutex that guards |initialized_|, |io_surfaces_|, |child_process_ids_|
120 // and |gpu_process_token_|.
123 DISALLOW_COPY_AND_ASSIGN(BrowserIOSurfaceManager
);
126 } // namespace content
128 #endif // CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_