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/. */
7 #ifndef _include_mozilla_gfx_ipc_GPUProcessHost_h_
8 #define _include_mozilla_gfx_ipc_GPUProcessHost_h_
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"
18 class SharedPreferenceSerializer
;
20 } // namespace mozilla
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,
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
;
41 virtual void OnProcessLaunchComplete(GPUProcessHost
* aHost
) {}
43 // The GPUProcessHost has unexpectedly shutdown or had its connection
44 // severed. This is not called if an error occurs after calling
46 virtual void OnProcessUnexpectedShutdown(GPUProcessHost
* aHost
) {}
48 virtual void OnRemoteProcessDeviceReset(GPUProcessHost
* aHost
) {}
51 explicit GPUProcessHost(Listener
* listener
);
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.
58 // @param aExtraOpts (StringVector)
59 // Extra options to pass to the subprocess.
60 bool Launch(StringVector aExtraOpts
);
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.
65 // Returns true if the process is successfully connected; false otherwise.
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
73 // After this returns, the attached Listener is no longer used.
76 // Return the actor for the top-level actor of the process. If the process
77 // has not connected yet, this returns null.
78 GPUChild
* GetActor() const { return mGPUChild
.get(); }
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;
84 bool IsConnected() const { return !!mGPUChild
; }
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.
90 TimeStamp
GetLaunchTime() const { return mLaunchTime
; }
92 // Called on the IO thread.
93 void OnChannelConnected(int32_t peer_pid
) override
;
94 void OnChannelError() override
;
96 void SetListener(Listener
* aListener
);
98 // Used for tests and diagnostics
104 // Called on the main thread.
105 void OnChannelConnectedTask();
106 void OnChannelErrorTask();
108 // Called on the main thread after a connection has been established.
109 void InitAfterConnect(bool aSucceeded
);
111 // Called on the main thread when the mGPUChild actor is shutting down.
112 void OnChannelClosed();
114 // Kill the remote process, triggering IPC shutdown.
115 void KillHard(const char* aReason
);
117 void DestroyProcess();
119 DISALLOW_COPY_AND_ASSIGN(GPUProcessHost
);
122 mozilla::ipc::TaskFactory
<GPUProcessHost
> mTaskFactory
;
124 enum class LaunchPhase
{ Unlaunched
, Waiting
, Complete
};
125 LaunchPhase mLaunchPhase
;
127 UniquePtr
<GPUChild
> mGPUChild
;
128 uint64_t mProcessToken
;
130 UniquePtr
<mozilla::ipc::SharedPreferenceSerializer
> mPrefSerializer
;
132 bool mShutdownRequested
;
135 TimeStamp mLaunchTime
;
139 } // namespace mozilla
141 #endif // _include_mozilla_gfx_ipc_GPUProcessHost_h_