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