/src/mozilla-central/gfx/ipc/GPUProcessHost.h
Line | Count | Source (jump to first uncovered line) |
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 | | 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 | | class Listener { |
36 | | public: |
37 | | 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 | | virtual void OnProcessUnexpectedShutdown(GPUProcessHost* aHost) |
44 | 0 | {} |
45 | | |
46 | | 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 OnProcessLaunchComplete listener |
56 | | // callback will be invoked either when a connection has been established, or |
57 | | // if a connection could not be established due to an asynchronous error. |
58 | | // |
59 | | // @param aExtraOpts (StringVector) |
60 | | // Extra options to pass to the subprocess. |
61 | | bool Launch(StringVector aExtraOpts); |
62 | | |
63 | | // If the process is being launched, block until it has launched and |
64 | | // connected. If a launch task is pending, it will fire immediately. |
65 | | // |
66 | | // Returns true if the process is successfully connected; false otherwise. |
67 | | bool WaitForLaunch(); |
68 | | |
69 | | // Inform the process that it should clean up its resources and shut down. |
70 | | // This initiates an asynchronous shutdown sequence. After this method returns, |
71 | | // it is safe for the caller to forget its pointer to the 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. |
78 | 0 | GPUChild* GetActor() const { |
79 | 0 | return mGPUChild.get(); |
80 | 0 | } |
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 | | |
86 | 0 | bool IsConnected() const { |
87 | 0 | return !!mGPUChild; |
88 | 0 | } |
89 | | |
90 | | // Return the time stamp for when we tried to launch the GPU process. This is |
91 | | // currently used for Telemetry so that we can determine how long GPU processes |
92 | | // take to spin up. Note this doesn't denote a successful launch, just when we |
93 | | // attempted launch. |
94 | 0 | TimeStamp GetLaunchTime() const { |
95 | 0 | return mLaunchTime; |
96 | 0 | } |
97 | | |
98 | | // Called on the IO thread. |
99 | | void OnChannelConnected(int32_t peer_pid) override; |
100 | | void OnChannelError() override; |
101 | | |
102 | | void SetListener(Listener* aListener); |
103 | | |
104 | | // Used for tests and diagnostics |
105 | | void KillProcess(); |
106 | | |
107 | | private: |
108 | | // Called on the main thread. |
109 | | void OnChannelConnectedTask(); |
110 | | void OnChannelErrorTask(); |
111 | | |
112 | | // Called on the main thread after a connection has been established. |
113 | | void InitAfterConnect(bool aSucceeded); |
114 | | |
115 | | // Called on the main thread when the mGPUChild actor is shutting down. |
116 | | void OnChannelClosed(); |
117 | | |
118 | | // Kill the remote process, triggering IPC shutdown. |
119 | | void KillHard(const char* aReason); |
120 | | |
121 | | void DestroyProcess(); |
122 | | |
123 | | private: |
124 | | DISALLOW_COPY_AND_ASSIGN(GPUProcessHost); |
125 | | |
126 | | Listener* mListener; |
127 | | mozilla::ipc::TaskFactory<GPUProcessHost> mTaskFactory; |
128 | | |
129 | | enum class LaunchPhase { |
130 | | Unlaunched, |
131 | | Waiting, |
132 | | Complete |
133 | | }; |
134 | | LaunchPhase mLaunchPhase; |
135 | | |
136 | | UniquePtr<GPUChild> mGPUChild; |
137 | | uint64_t mProcessToken; |
138 | | |
139 | | bool mShutdownRequested; |
140 | | bool mChannelClosed; |
141 | | |
142 | | TimeStamp mLaunchTime; |
143 | | }; |
144 | | |
145 | | } // namespace gfx |
146 | | } // namespace mozilla |
147 | | |
148 | | #endif // _include_mozilla_gfx_ipc_GPUProcessHost_h_ |