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 // Overridden from IOSurfaceManager:
44 bool RegisterIOSurface(IOSurfaceId io_surface_id
,
46 IOSurfaceRef io_surface
) override
;
47 void UnregisterIOSurface(IOSurfaceId io_surface_id
, int client_id
) override
;
48 IOSurfaceRef
AcquireIOSurface(IOSurfaceId io_surface_id
) override
;
50 // Performs any necessary setup that cannot happen in the constructor.
53 // Generate a unique unguessable token that the GPU process can use to
54 // register/unregister IOSurface for use by clients.
55 IOSurfaceManagerToken
GenerateGpuProcessToken();
57 // Invalidate the previously generated GPU process token.
58 void InvalidateGpuProcessToken();
60 // Generate a unique unguessable token that the child process associated
61 // |child_process_id| can use to acquire IOSurface references.
62 IOSurfaceManagerToken
GenerateChildProcessToken(int child_process_id
);
64 // Invalidate a previously generated child process token.
65 void InvalidateChildProcessToken(const IOSurfaceManagerToken
& token
);
68 friend class BrowserIOSurfaceManagerTest
;
69 friend struct DefaultSingletonTraits
<BrowserIOSurfaceManager
>;
71 BrowserIOSurfaceManager();
72 ~BrowserIOSurfaceManager() override
;
74 // Performs any initialization work.
77 // Message handler that is invoked on |dispatch_source_| when an
78 // incoming message needs to be received.
81 // Message handlers that are invoked from HandleRequest.
82 bool HandleRegisterIOSurfaceRequest(
83 const IOSurfaceManagerHostMsg_RegisterIOSurface
& request
,
84 IOSurfaceManagerMsg_RegisterIOSurfaceReply
* reply
);
85 bool HandleUnregisterIOSurfaceRequest(
86 const IOSurfaceManagerHostMsg_UnregisterIOSurface
& request
);
87 bool HandleAcquireIOSurfaceRequest(
88 const IOSurfaceManagerHostMsg_AcquireIOSurface
& request
,
89 IOSurfaceManagerMsg_AcquireIOSurfaceReply
* reply
);
91 // Whether or not the class has been initialized.
94 // The Mach port on which the server listens.
95 base::mac::ScopedMachReceiveRight server_port_
;
97 // The dispatch source and queue on which Mach messages will be received.
98 scoped_ptr
<base::DispatchSourceMach
> dispatch_source_
;
100 // Stores the IOSurfaces for all GPU clients. The key contains the IOSurface
101 // id and the Child process unique id of the owner.
102 using IOSurfaceMapKey
= std::pair
<IOSurfaceId
, int>;
104 base::ScopedPtrHashMap
<IOSurfaceMapKey
,
105 scoped_ptr
<base::mac::ScopedMachSendRight
>>;
106 IOSurfaceMap io_surfaces_
;
108 // Stores the Child process unique id (RenderProcessHost ID) for every
110 using ChildProcessIdMap
= std::map
<IOSurfaceManagerToken
, int>;
111 ChildProcessIdMap child_process_ids_
;
113 // Stores the GPU process token.
114 IOSurfaceManagerToken gpu_process_token_
;
116 // Mutex that guards |initialized_|, |io_surfaces_|, |child_process_ids_|
117 // and |gpu_process_token_|.
120 DISALLOW_COPY_AND_ASSIGN(BrowserIOSurfaceManager
);
123 } // namespace content
125 #endif // CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_