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