GPUProcessHost.h (6944B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef _include_mozilla_gfx_ipc_GPUProcessHost_h_ 8 #define _include_mozilla_gfx_ipc_GPUProcessHost_h_ 9 10 #include "mozilla/UniquePtr.h" 11 #include "mozilla/gfx/Types.h" 12 #include "mozilla/ipc/GeckoChildProcessHost.h" 13 #include "mozilla/ipc/ProtocolUtils.h" 14 #include "mozilla/media/MediaUtils.h" 15 16 #ifdef MOZ_WIDGET_ANDROID 17 # include "mozilla/java/CompositorSurfaceManagerWrappers.h" 18 #endif 19 20 namespace mozilla { 21 namespace ipc { 22 class SharedPreferenceSerializer; 23 } 24 } // namespace mozilla 25 class nsITimer; 26 27 namespace mozilla { 28 namespace gfx { 29 30 class GPUChild; 31 32 // GPUProcessHost is the "parent process" container for a subprocess handle and 33 // IPC connection. It owns the parent process IPDL actor, which in this case, 34 // is a GPUChild. 35 // 36 // GPUProcessHosts are allocated and managed by GPUProcessManager. For all 37 // intents and purposes it is a singleton, though more than one may be allocated 38 // at a time due to its shutdown being asynchronous. 39 class GPUProcessHost final : public mozilla::ipc::GeckoChildProcessHost { 40 friend class GPUChild; 41 42 public: 43 class Listener { 44 public: 45 virtual void OnProcessLaunchComplete(GPUProcessHost* aHost) {} 46 47 // The GPUProcessHost has unexpectedly shutdown or had its connection 48 // severed. This is not called if an error occurs after calling 49 // Shutdown(). 50 virtual void OnProcessUnexpectedShutdown(GPUProcessHost* aHost) {} 51 52 virtual void OnRemoteProcessDeviceReset( 53 GPUProcessHost* aHost, const DeviceResetReason& aReason, 54 const DeviceResetDetectPlace& aPlace) {} 55 56 virtual void OnProcessDeclaredStable() {} 57 }; 58 59 explicit GPUProcessHost(Listener* listener); 60 61 // Launch the subprocess asynchronously. On failure, false is returned. 62 // Otherwise, true is returned, and the OnProcessLaunchComplete listener 63 // callback will be invoked either when a connection has been established and 64 // process initialization is complete, or if a connection could not be 65 // established due to an asynchronous error. 66 // 67 // @param aExtraOpts (geckoargs::ChildProcessArgs) 68 // Extra options to pass to the subprocess. 69 bool Launch(geckoargs::ChildProcessArgs aExtraOpts); 70 71 // If the process is being launched, block until it has launched and 72 // connected, and any initialization has completed. If a launch task is 73 // pending, it will fire immediately. 74 // 75 // Returns true if the process is successfully initialized; false otherwise. 76 bool WaitForLaunch(); 77 78 // Inform the process that it should clean up its resources and shut down. 79 // This initiates an asynchronous shutdown sequence. After this method 80 // returns, it is safe for the caller to forget its pointer to the 81 // GPUProcessHost. 82 // 83 // After this returns, the attached Listener is no longer used. 84 // 85 // Setting aUnexpectedShutdown = true indicates that this is being called to 86 // clean up resources in response to an unexpected shutdown having been 87 // detected. 88 void Shutdown(bool aUnexpectedShutdown = false); 89 90 // Return the actor for the top-level actor of the process. If the process 91 // has not connected yet, this returns null. 92 GPUChild* GetActor() const { return mGPUChild.get(); } 93 94 // Return a unique id for this process, guaranteed not to be shared with any 95 // past or future instance of GPUProcessHost. 96 uint64_t GetProcessToken() const; 97 98 bool IsConnected() const { return !!mGPUChild; } 99 100 // Return the time stamp for when we tried to launch the GPU process. This is 101 // currently used for Telemetry so that we can determine how long GPU 102 // processes take to spin up. Note this doesn't denote a successful launch, 103 // just when we attempted launch. 104 TimeStamp GetLaunchTime() const { return mLaunchTime; } 105 106 // Called on the IO thread. 107 void OnChannelConnected(base::ProcessId peer_pid) override; 108 109 void SetListener(Listener* aListener); 110 111 // Kills the GPU process. Used in normal operation to recover from an error, 112 // as well as for tests and diagnostics. 113 void KillProcess(bool aGenerateMinidump); 114 115 // Causes the GPU process to crash. Used for tests and diagnostics 116 void CrashProcess(); 117 118 #ifdef MOZ_WIDGET_ANDROID 119 java::CompositorSurfaceManager::Param GetCompositorSurfaceManager(); 120 #endif 121 122 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX) 123 static MacSandboxType GetMacSandboxType() { return MacSandboxType_GPU; }; 124 #endif 125 126 private: 127 ~GPUProcessHost(); 128 129 // Called on the main thread after a connection has been established. 130 // Creates the PGPU endpoints and begins asynchronous initialization. 131 void InitAfterConnect(bool aSucceeded); 132 // Called on the main thread after post-connection initialization tasks have 133 // completed asynchronously. 134 void OnAsyncInitComplete(); 135 // Synchronously completes any outstanding post-connection initialization 136 // tasks which have not yet completed asynchronously. 137 bool CompleteInitSynchronously(); 138 139 // Called on the main thread when the mGPUChild actor is shutting down. 140 void OnChannelClosed(); 141 142 // Kill the remote process, triggering IPC shutdown. 143 void KillHard(bool aGenerateMinidump); 144 145 void DestroyProcess(); 146 147 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX) 148 static bool sLaunchWithMacSandbox; 149 bool IsMacSandboxLaunchEnabled() override { return sLaunchWithMacSandbox; } 150 151 // Override so we can turn on GPU process-specific sandbox logging 152 bool FillMacSandboxInfo(MacSandboxInfo& aInfo) override; 153 #endif 154 155 DISALLOW_COPY_AND_ASSIGN(GPUProcessHost); 156 157 Listener* mListener; 158 159 enum class LaunchPhase { Unlaunched, Waiting, Connected, Complete }; 160 LaunchPhase mLaunchPhase; 161 162 RefPtr<GPUChild> mGPUChild; 163 uint64_t mProcessToken; 164 165 UniquePtr<mozilla::ipc::SharedPreferenceSerializer> mPrefSerializer; 166 167 bool mShutdownRequested; 168 bool mChannelClosed; 169 170 TimeStamp mLaunchTime; 171 172 // Set to true on construction and to false just prior deletion. 173 // The GPUProcessHost isn't refcounted; so we can capture this by value in 174 // lambdas along with a strong reference to mLiveToken and check if that value 175 // is true before accessing "this". 176 // While a reference to mLiveToken can be taken on any thread; its value can 177 // only be read on the main thread. 178 const RefPtr<media::Refcountable<bool>> mLiveToken; 179 180 #ifdef MOZ_WIDGET_ANDROID 181 // Binder interface used to send compositor surfaces to GPU process. There is 182 // one instance per GPU process which gets initialized after launch, then 183 // multiple compositors can take a reference to it. 184 java::CompositorSurfaceManager::GlobalRef mCompositorSurfaceManager; 185 #endif 186 }; 187 188 } // namespace gfx 189 } // namespace mozilla 190 191 #endif // _include_mozilla_gfx_ipc_GPUProcessHost_h_