Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / content / browser / browser_io_surface_manager_mac.h
blob66a1c7b002425ea838722993999bf622c52a6dc2
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_
8 #include <mach/mach.h>
10 #include <map>
11 #include <utility>
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"
25 namespace content {
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 {
35 public:
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,
45 int client_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.
51 void EnsureRunning();
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);
67 private:
68 friend class BrowserIOSurfaceManagerTest;
69 friend struct DefaultSingletonTraits<BrowserIOSurfaceManager>;
71 BrowserIOSurfaceManager();
72 ~BrowserIOSurfaceManager() override;
74 // Performs any initialization work.
75 bool Initialize();
77 // Message handler that is invoked on |dispatch_source_| when an
78 // incoming message needs to be received.
79 void HandleRequest();
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.
92 bool 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>;
103 using IOSurfaceMap =
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
109 // token.
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_|.
118 base::Lock lock_;
120 DISALLOW_COPY_AND_ASSIGN(BrowserIOSurfaceManager);
123 } // namespace content
125 #endif // CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_