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 OnProcessDeclaredStable()50 virtual void OnProcessDeclaredStable() {} 51 }; 52 53 explicit GPUProcessHost(Listener* listener); 54 55 // Launch the subprocess asynchronously. On failure, false is returned. 56 // Otherwise, true is returned, and the OnProcessLaunchComplete listener 57 // callback will be invoked either when a connection has been established, or 58 // if a connection could not be established due to an asynchronous error. 59 // 60 // @param aExtraOpts (StringVector) 61 // Extra options to pass to the subprocess. 62 bool Launch(StringVector aExtraOpts); 63 64 // If the process is being launched, block until it has launched and 65 // connected. If a launch task is pending, it will fire immediately. 66 // 67 // Returns true if the process is successfully connected; false otherwise. 68 bool WaitForLaunch(); 69 70 // Inform the process that it should clean up its resources and shut down. 71 // This initiates an asynchronous shutdown sequence. After this method 72 // returns, it is safe for the caller to forget its pointer to the 73 // GPUProcessHost. 74 // 75 // After this returns, the attached Listener is no longer used. 76 void Shutdown(); 77 78 // Return the actor for the top-level actor of the process. If the process 79 // has not connected yet, this returns null. GetActor()80 GPUChild* GetActor() const { return mGPUChild.get(); } 81 82 // Return a unique id for this process, guaranteed not to be shared with any 83 // past or future instance of GPUProcessHost. 84 uint64_t GetProcessToken() const; 85 IsConnected()86 bool IsConnected() const { return !!mGPUChild; } 87 88 // Return the time stamp for when we tried to launch the GPU process. This is 89 // currently used for Telemetry so that we can determine how long GPU 90 // processes take to spin up. Note this doesn't denote a successful launch, 91 // just when we attempted launch. GetLaunchTime()92 TimeStamp GetLaunchTime() const { return mLaunchTime; } 93 94 // Called on the IO thread. 95 void OnChannelConnected(int32_t peer_pid) override; 96 void OnChannelError() override; 97 98 void SetListener(Listener* aListener); 99 100 // Used for tests and diagnostics 101 void KillProcess(); 102 103 private: 104 ~GPUProcessHost(); 105 106 // Called on the main thread. 107 void OnChannelConnectedTask(); 108 void OnChannelErrorTask(); 109 110 // Called on the main thread after a connection has been established. 111 void InitAfterConnect(bool aSucceeded); 112 113 // Called on the main thread when the mGPUChild actor is shutting down. 114 void OnChannelClosed(); 115 116 // Kill the remote process, triggering IPC shutdown. 117 void KillHard(const char* aReason); 118 119 void DestroyProcess(); 120 121 DISALLOW_COPY_AND_ASSIGN(GPUProcessHost); 122 123 Listener* mListener; 124 mozilla::ipc::TaskFactory<GPUProcessHost> mTaskFactory; 125 126 enum class LaunchPhase { Unlaunched, Waiting, Complete }; 127 LaunchPhase mLaunchPhase; 128 129 UniquePtr<GPUChild> mGPUChild; 130 uint64_t mProcessToken; 131 132 UniquePtr<mozilla::ipc::SharedPreferenceSerializer> mPrefSerializer; 133 134 bool mShutdownRequested; 135 bool mChannelClosed; 136 137 TimeStamp mLaunchTime; 138 }; 139 140 } // namespace gfx 141 } // namespace mozilla 142 143 #endif // _include_mozilla_gfx_ipc_GPUProcessHost_h_ 144