OLD | NEW |
(Empty) | |
| 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" |
| 20 #include "content/common/mac/io_surface_manager.h" |
| 21 #include "content/common/mac/io_surface_manager_messages.h" |
| 22 #include "content/common/mac/io_surface_manager_token.h" |
| 23 |
| 24 namespace content { |
| 25 |
| 26 // Implementation of IOSurfaceManager that provides a mechanism for child |
| 27 // processes to register and acquire IOSurfaces through a Mach service. |
| 28 class CONTENT_EXPORT BrowserIOSurfaceManager : public IOSurfaceManager { |
| 29 public: |
| 30 // Returns the global BrowserIOSurfaceManager. |
| 31 static BrowserIOSurfaceManager* GetInstance(); |
| 32 |
| 33 // Look up the IOSurfaceManager service port that's been registered with |
| 34 // the bootstrap server. |pid| is the process ID of the service. |
| 35 static base::mac::ScopedMachSendRight LookupServicePort(pid_t pid); |
| 36 |
| 37 // Overridden from IOSurfaceManager: |
| 38 bool RegisterIOSurface(int io_surface_id, |
| 39 int client_id, |
| 40 IOSurfaceRef io_surface) override; |
| 41 void UnregisterIOSurface(int io_surface_id, int client_id) override; |
| 42 IOSurfaceRef AcquireIOSurface(int io_surface_id) override; |
| 43 |
| 44 // Performs any necessary setup that cannot happen in the constructor. |
| 45 void EnsureRunning(); |
| 46 |
| 47 // Get the unique unguessable token that the GPU process can use to |
| 48 // register/unregister IOSurface for use by clients. |
| 49 IOSurfaceManagerToken GetGpuProcessToken() const; |
| 50 |
| 51 // Generate a unique unguessable token that the child process associated |
| 52 // |child_process_id| can use to acquire IOSurface references. |
| 53 IOSurfaceManagerToken GenerateChildProcessToken(int child_process_id); |
| 54 |
| 55 // Invalidate a previously generated token. |
| 56 void InvalidateChildProcessToken(const IOSurfaceManagerToken& token); |
| 57 |
| 58 private: |
| 59 friend class BrowserIOSurfaceManagerTest; |
| 60 friend struct DefaultSingletonTraits<BrowserIOSurfaceManager>; |
| 61 |
| 62 BrowserIOSurfaceManager(); |
| 63 ~BrowserIOSurfaceManager() override; |
| 64 |
| 65 // Performs any initialization work. |
| 66 bool Initialize(); |
| 67 |
| 68 // Message handler that is invoked on |dispatch_source_| when an |
| 69 // incoming message needs to be received. |
| 70 void HandleRequest(); |
| 71 |
| 72 // Message handlers that are invoked from HandleRequest. |
| 73 bool HandleRegisterIOSurfaceRequest( |
| 74 const IOSurfaceManagerHostMsg_RegisterIOSurface& request, |
| 75 IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply); |
| 76 bool HandleUnregisterIOSurfaceRequest( |
| 77 const IOSurfaceManagerHostMsg_UnregisterIOSurface& request); |
| 78 bool HandleAcquireIOSurfaceRequest( |
| 79 const IOSurfaceManagerHostMsg_AcquireIOSurface& request, |
| 80 IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply); |
| 81 |
| 82 // Whether or not the class has been initialized. |
| 83 bool initialized_; |
| 84 |
| 85 // The Mach port on which the server listens. |
| 86 base::mac::ScopedMachReceiveRight server_port_; |
| 87 |
| 88 // The dispatch source and queue on which Mach messages will be received. |
| 89 scoped_ptr<base::DispatchSourceMach> dispatch_source_; |
| 90 |
| 91 // Stores the IOSurfaces for all GPU clients. The key contains the IOSurface |
| 92 // id and the Child process unique id of the owner. |
| 93 using IOSurfaceMapKey = std::pair<int, int>; |
| 94 using IOSurfaceMap = |
| 95 base::ScopedPtrHashMap<IOSurfaceMapKey, |
| 96 scoped_ptr<base::mac::ScopedMachSendRight>>; |
| 97 IOSurfaceMap io_surfaces_; |
| 98 |
| 99 // Stores the Child process unique id (RenderProcessHost ID) for every |
| 100 // token. |
| 101 using ChildProcessIdMap = std::map<IOSurfaceManagerToken, int>; |
| 102 ChildProcessIdMap child_process_ids_; |
| 103 |
| 104 // Stores the GPU process token. |
| 105 const IOSurfaceManagerToken gpu_process_token_; |
| 106 |
| 107 // Mutex that guards |initialized_|, |io_surfaces_| and |child_process_ids_|. |
| 108 mutable base::Lock lock_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(BrowserIOSurfaceManager); |
| 111 }; |
| 112 |
| 113 } // namespace content |
| 114 |
| 115 #endif // CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_ |
OLD | NEW |