reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 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 | #ifndef CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_ |
| 6 | #define CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_ |
| 7 | |
| 8 | #include <mach/mach.h> |
| 9 | |
| 10 | #include <map> |
| 11 | #include <utility> |
| 12 | |
| 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" |
reveman | ac364edf4 | 2015-06-10 22:41:55 | [diff] [blame] | 20 | #include "content/common/content_export.h" |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 21 | #include "content/common/mac/io_surface_manager_messages.h" |
| 22 | #include "content/common/mac/io_surface_manager_token.h" |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 23 | #include "ui/gfx/mac/io_surface_manager.h" |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 24 | |
| 25 | namespace content { |
| 26 | |
ericrk | c9984ebe | 2015-08-17 14:22:37 | [diff] [blame] | 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. |
| 31 | |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 32 | // Implementation of IOSurfaceManager that provides a mechanism for child |
| 33 | // processes to register and acquire IOSurfaces through a Mach service. |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 34 | class CONTENT_EXPORT BrowserIOSurfaceManager : public gfx::IOSurfaceManager { |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 35 | public: |
| 36 | // Returns the global BrowserIOSurfaceManager. |
| 37 | static BrowserIOSurfaceManager* GetInstance(); |
| 38 | |
| 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); |
| 42 | |
rsesek | dba8411 | 2015-09-18 19:22:07 | [diff] [blame] | 43 | // Returns the name of the service registered with the bootstrap server. |
| 44 | static std::string GetMachPortName(); |
| 45 | |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 46 | // Overridden from IOSurfaceManager: |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 47 | bool RegisterIOSurface(gfx::IOSurfaceId io_surface_id, |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 48 | int client_id, |
| 49 | IOSurfaceRef io_surface) override; |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 50 | void UnregisterIOSurface(gfx::IOSurfaceId io_surface_id, |
| 51 | int client_id) override; |
| 52 | IOSurfaceRef AcquireIOSurface(gfx::IOSurfaceId io_surface_id) override; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 53 | |
| 54 | // Performs any necessary setup that cannot happen in the constructor. |
| 55 | void EnsureRunning(); |
| 56 | |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 57 | // Generate a unique unguessable token that the GPU process can use to |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 58 | // register/unregister IOSurface for use by clients. |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 59 | IOSurfaceManagerToken GenerateGpuProcessToken(); |
| 60 | |
| 61 | // Invalidate the previously generated GPU process token. |
| 62 | void InvalidateGpuProcessToken(); |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 63 | |
| 64 | // Generate a unique unguessable token that the child process associated |
| 65 | // |child_process_id| can use to acquire IOSurface references. |
| 66 | IOSurfaceManagerToken GenerateChildProcessToken(int child_process_id); |
| 67 | |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 68 | // Invalidate a previously generated child process token. |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 69 | void InvalidateChildProcessToken(const IOSurfaceManagerToken& token); |
| 70 | |
| 71 | private: |
| 72 | friend class BrowserIOSurfaceManagerTest; |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 73 | friend struct base::DefaultSingletonTraits<BrowserIOSurfaceManager>; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 74 | |
| 75 | BrowserIOSurfaceManager(); |
| 76 | ~BrowserIOSurfaceManager() override; |
| 77 | |
| 78 | // Performs any initialization work. |
| 79 | bool Initialize(); |
| 80 | |
| 81 | // Message handler that is invoked on |dispatch_source_| when an |
| 82 | // incoming message needs to be received. |
| 83 | void HandleRequest(); |
| 84 | |
| 85 | // Message handlers that are invoked from HandleRequest. |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 86 | void HandleRegisterIOSurfaceRequest( |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 87 | const IOSurfaceManagerHostMsg_RegisterIOSurface& request, |
| 88 | IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply); |
| 89 | bool HandleUnregisterIOSurfaceRequest( |
| 90 | const IOSurfaceManagerHostMsg_UnregisterIOSurface& request); |
ccameron | 7c766ff4 | 2015-09-18 19:43:17 | [diff] [blame] | 91 | void HandleAcquireIOSurfaceRequest( |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 92 | const IOSurfaceManagerHostMsg_AcquireIOSurface& request, |
| 93 | IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply); |
| 94 | |
| 95 | // Whether or not the class has been initialized. |
| 96 | bool initialized_; |
| 97 | |
| 98 | // The Mach port on which the server listens. |
| 99 | base::mac::ScopedMachReceiveRight server_port_; |
| 100 | |
| 101 | // The dispatch source and queue on which Mach messages will be received. |
| 102 | scoped_ptr<base::DispatchSourceMach> dispatch_source_; |
| 103 | |
| 104 | // Stores the IOSurfaces for all GPU clients. The key contains the IOSurface |
| 105 | // id and the Child process unique id of the owner. |
dcastagna | 5ab612c0 | 2015-11-01 20:15:08 | [diff] [blame] | 106 | using IOSurfaceMapKey = std::pair<gfx::IOSurfaceId, int>; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 107 | using IOSurfaceMap = |
| 108 | base::ScopedPtrHashMap<IOSurfaceMapKey, |
| 109 | scoped_ptr<base::mac::ScopedMachSendRight>>; |
| 110 | IOSurfaceMap io_surfaces_; |
| 111 | |
| 112 | // Stores the Child process unique id (RenderProcessHost ID) for every |
| 113 | // token. |
| 114 | using ChildProcessIdMap = std::map<IOSurfaceManagerToken, int>; |
| 115 | ChildProcessIdMap child_process_ids_; |
| 116 | |
| 117 | // Stores the GPU process token. |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 118 | IOSurfaceManagerToken gpu_process_token_; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 119 | |
reveman | 45996554 | 2015-06-16 18:12:50 | [diff] [blame] | 120 | // Mutex that guards |initialized_|, |io_surfaces_|, |child_process_ids_| |
| 121 | // and |gpu_process_token_|. |
| 122 | base::Lock lock_; |
reveman | 7c45b308 | 2015-06-04 01:27:08 | [diff] [blame] | 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(BrowserIOSurfaceManager); |
| 125 | }; |
| 126 | |
| 127 | } // namespace content |
| 128 | |
| 129 | #endif // CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_ |