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/Maybe.h" 11 #include "mozilla/UniquePtr.h" 12 #include "mozilla/ipc/GeckoChildProcessHost.h" 13 #include "mozilla/ipc/ProtocolUtils.h" 14 #include "mozilla/ipc/TaskFactory.h" 15 16 namespace mozilla { 17 namespace ipc { 18 class SharedPreferenceSerializer; 19 } 20 } // namespace mozilla 21 class nsITimer; 22 23 namespace mozilla { 24 namespace gfx { 25 26 class GPUChild; 27 28 // GPUProcessHost is the "parent process" container for a subprocess handle and 29 // IPC connection. It owns the parent process IPDL actor, which in this case, 30 // is a GPUChild. 31 // 32 // GPUProcessHosts are allocated and managed by GPUProcessManager. For all 33 // intents and purposes it is a singleton, though more than one may be allocated 34 // at a time due to its shutdown being asynchronous. 35 class GPUProcessHost final : public mozilla::ipc::GeckoChildProcessHost { 36 friend class GPUChild; 37 38 public: 39 class Listener { 40 public: OnProcessLaunchComplete(GPUProcessHost * aHost)41 virtual void OnProcessLaunchComplete(GPUProcessHost* aHost) {} 42 43 // The GPUProcessHost has unexpectedly shutdown or had its connection 44 // severed. This is not called if an error occurs after calling 45 // Shutdown(). OnProcessUnexpectedShutdown(GPUProcessHost * aHost)46 virtual void OnProcessUnexpectedShutdown(GPUProcessHost* aHost) {} 47 OnRemoteProcessDeviceReset(GPUProcessHost * aHost)48 virtual void OnRemoteProcessDeviceReset(GPUProcessHost* aHost) {} 49 }; 50 51 explicit GPUProcessHost(Listener* listener); 52 53 // Launch the subprocess asynchronously. On failure, false is returned. 54 // Otherwise, true is returned, and the OnProcessLaunchComplete listener 55 // callback will be invoked either when a connection has been established, or 56 // if a connection could not be established due to an asynchronous error. 57 // 58 // @param aExtraOpts (StringVector) 59 // Extra options to pass to the subprocess. 60 bool Launch(StringVector aExtraOpts); 61 62 // If the process is being launched, block until it has launched and 63 // connected. If a launch task is pending, it will fire immediately. 64 // 65 // Returns true if the process is successfully connected; false otherwise. 66 bool WaitForLaunch(); 67 68 // Inform the process that it should clean up its resources and shut down. 69 // This initiates an asynchronous shutdown sequence. After this method 70 // returns, it is safe for the caller to forget its pointer to the 71 // GPUProcessHost. 72 // 73 // After this returns, the attached Listener is no longer used. 74 void Shutdown(); 75 76 // Return the actor for the top-level actor of the process. If the process 77 // has not connected yet, this returns null. GetActor()78 GPUChild* GetActor() const { return mGPUChild.get(); } 79 80 // Return a unique id for this process, guaranteed not to be shared with any 81 // past or future instance of GPUProcessHost. 82 uint64_t GetProcessToken() const; 83 IsConnected()84 bool IsConnected() const { return !!mGPUChild; } 85 86 // Return the time stamp for when we tried to launch the GPU process. This is 87 // currently used for Telemetry so that we can determine how long GPU 88 // processes take to spin up. Note this doesn't denote a successful launch, 89 // just when we attempted launch. GetLaunchTime()90 TimeStamp GetLaunchTime() const { return mLaunchTime; } 91 92 // Called on the IO thread. 93 void OnChannelConnected(int32_t peer_pid) override; 94 void OnChannelError() override; 95 96 void SetListener(Listener* aListener); 97 98 // Used for tests and diagnostics 99 void KillProcess(); 100 101 private: 102 ~GPUProcessHost(); 103 104 // Called on the main thread. 105 void OnChannelConnectedTask(); 106 void OnChannelErrorTask(); 107 108 // Called on the main thread after a connection has been established. 109 void InitAfterConnect(bool aSucceeded); 110 111 // Called on the main thread when the mGPUChild actor is shutting down. 112 void OnChannelClosed(); 113 114 // Kill the remote process, triggering IPC shutdown. 115 void KillHard(const char* aReason); 116 117 void DestroyProcess(); 118 119 DISALLOW_COPY_AND_ASSIGN(GPUProcessHost); 120 121 Listener* mListener; 122 mozilla::ipc::TaskFactory<GPUProcessHost> mTaskFactory; 123 124 enum class LaunchPhase { Unlaunched, Waiting, Complete }; 125 LaunchPhase mLaunchPhase; 126 127 UniquePtr<GPUChild> mGPUChild; 128 uint64_t mProcessToken; 129 130 UniquePtr<mozilla::ipc::SharedPreferenceSerializer> mPrefSerializer; 131 132 bool mShutdownRequested; 133 bool mChannelClosed; 134 135 TimeStamp mLaunchTime; 136 }; 137 138 } // namespace gfx 139 } // namespace mozilla 140 141 #endif // _include_mozilla_gfx_ipc_GPUProcessHost_h_ 142