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"
16 #ifdef MOZ_WIDGET_ANDROID
17 # include "mozilla/java/CompositorSurfaceManagerWrappers.h"
22 class SharedPreferenceSerializer
;
24 } // namespace mozilla
32 // GPUProcessHost is the "parent process" container for a subprocess handle and
33 // IPC connection. It owns the parent process IPDL actor, which in this case,
36 // GPUProcessHosts are allocated and managed by GPUProcessManager. For all
37 // intents and purposes it is a singleton, though more than one may be allocated
38 // at a time due to its shutdown being asynchronous.
39 class GPUProcessHost final
: public mozilla::ipc::GeckoChildProcessHost
{
40 friend class GPUChild
;
45 virtual void OnProcessLaunchComplete(GPUProcessHost
* aHost
) {}
47 // The GPUProcessHost has unexpectedly shutdown or had its connection
48 // severed. This is not called if an error occurs after calling
50 virtual void OnProcessUnexpectedShutdown(GPUProcessHost
* aHost
) {}
52 virtual void OnRemoteProcessDeviceReset(GPUProcessHost
* aHost
) {}
54 virtual void OnProcessDeclaredStable() {}
57 explicit GPUProcessHost(Listener
* listener
);
59 // Launch the subprocess asynchronously. On failure, false is returned.
60 // Otherwise, true is returned, and the OnProcessLaunchComplete listener
61 // callback will be invoked either when a connection has been established, or
62 // if a connection could not be established due to an asynchronous error.
64 // @param aExtraOpts (StringVector)
65 // Extra options to pass to the subprocess.
66 bool Launch(StringVector aExtraOpts
);
68 // If the process is being launched, block until it has launched and
69 // connected. If a launch task is pending, it will fire immediately.
71 // Returns true if the process is successfully connected; false otherwise.
74 // Inform the process that it should clean up its resources and shut down.
75 // This initiates an asynchronous shutdown sequence. After this method
76 // returns, it is safe for the caller to forget its pointer to the
79 // After this returns, the attached Listener is no longer used.
81 // Setting aUnexpectedShutdown = true indicates that this is being called to
82 // clean up resources in response to an unexpected shutdown having been
84 void Shutdown(bool aUnexpectedShutdown
= false);
86 // Return the actor for the top-level actor of the process. If the process
87 // has not connected yet, this returns null.
88 GPUChild
* GetActor() const { return mGPUChild
.get(); }
90 // Return a unique id for this process, guaranteed not to be shared with any
91 // past or future instance of GPUProcessHost.
92 uint64_t GetProcessToken() const;
94 bool IsConnected() const { return !!mGPUChild
; }
96 // Return the time stamp for when we tried to launch the GPU process. This is
97 // currently used for Telemetry so that we can determine how long GPU
98 // processes take to spin up. Note this doesn't denote a successful launch,
99 // just when we attempted launch.
100 TimeStamp
GetLaunchTime() const { return mLaunchTime
; }
102 // Called on the IO thread.
103 void OnChannelConnected(base::ProcessId peer_pid
) override
;
105 void SetListener(Listener
* aListener
);
107 // Kills the GPU process. Used for tests and diagnostics
110 // Causes the GPU process to crash. Used for tests and diagnostics
113 #ifdef MOZ_WIDGET_ANDROID
114 java::CompositorSurfaceManager::Param
GetCompositorSurfaceManager();
120 // Called on the main thread.
121 void OnChannelConnectedTask();
122 void OnChannelErrorTask();
124 // Called on the main thread after a connection has been established.
125 void InitAfterConnect(bool aSucceeded
);
127 // Called on the main thread when the mGPUChild actor is shutting down.
128 void OnChannelClosed();
130 // Kill the remote process, triggering IPC shutdown.
131 void KillHard(const char* aReason
);
133 void DestroyProcess();
135 DISALLOW_COPY_AND_ASSIGN(GPUProcessHost
);
138 mozilla::ipc::TaskFactory
<GPUProcessHost
> mTaskFactory
;
140 enum class LaunchPhase
{ Unlaunched
, Waiting
, Complete
};
141 LaunchPhase mLaunchPhase
;
143 RefPtr
<GPUChild
> mGPUChild
;
144 uint64_t mProcessToken
;
146 UniquePtr
<mozilla::ipc::SharedPreferenceSerializer
> mPrefSerializer
;
148 bool mShutdownRequested
;
151 TimeStamp mLaunchTime
;
153 #ifdef MOZ_WIDGET_ANDROID
154 // Binder interface used to send compositor surfaces to GPU process. There is
155 // one instance per GPU process which gets initialized after launch, then
156 // multiple compositors can take a reference to it.
157 java::CompositorSurfaceManager::GlobalRef mCompositorSurfaceManager
;
162 } // namespace mozilla
164 #endif // _include_mozilla_gfx_ipc_GPUProcessHost_h_