2016-06-11 05:27:24 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sts=8 sw=2 ts=2 tw=99 et :
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "GPUProcessHost.h"
|
2016-06-11 05:37:03 +03:00
|
|
|
#include "chrome/common/process_watcher.h"
|
|
|
|
#include "gfxPrefs.h"
|
2016-06-11 05:27:24 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
2016-06-11 05:37:03 +03:00
|
|
|
#include "nsITimer.h"
|
2016-06-11 05:27:24 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2016-09-21 12:24:44 +03:00
|
|
|
using namespace ipc;
|
|
|
|
|
2016-06-11 05:27:24 +03:00
|
|
|
GPUProcessHost::GPUProcessHost(Listener* aListener)
|
|
|
|
: GeckoChildProcessHost(GeckoProcessType_GPU),
|
|
|
|
mListener(aListener),
|
|
|
|
mTaskFactory(this),
|
2016-06-11 05:37:03 +03:00
|
|
|
mLaunchPhase(LaunchPhase::Unlaunched),
|
2016-07-18 07:24:28 +03:00
|
|
|
mProcessToken(0),
|
2016-07-18 07:24:28 +03:00
|
|
|
mShutdownRequested(false),
|
|
|
|
mChannelClosed(false)
|
2016-06-11 05:27:24 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(GPUProcessHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
GPUProcessHost::~GPUProcessHost()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(GPUProcessHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GPUProcessHost::Launch()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mLaunchPhase == LaunchPhase::Unlaunched);
|
|
|
|
MOZ_ASSERT(!mGPUChild);
|
|
|
|
|
|
|
|
mLaunchPhase = LaunchPhase::Waiting;
|
2016-10-20 18:33:40 +03:00
|
|
|
mLaunchTime = TimeStamp::Now();
|
|
|
|
|
2016-06-11 05:27:24 +03:00
|
|
|
if (!GeckoChildProcessHost::AsyncLaunch()) {
|
|
|
|
mLaunchPhase = LaunchPhase::Complete;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GPUProcessHost::WaitForLaunch()
|
|
|
|
{
|
|
|
|
if (mLaunchPhase == LaunchPhase::Complete) {
|
|
|
|
return !!mGPUChild;
|
|
|
|
}
|
|
|
|
|
2017-01-17 10:51:24 +03:00
|
|
|
int32_t timeoutMs = gfxPrefs::GPUProcessTimeoutMs();
|
2016-06-11 05:27:24 +03:00
|
|
|
|
|
|
|
// Our caller expects the connection to be finished after we return, so we
|
|
|
|
// immediately set up the IPDL actor and fire callbacks. The IO thread will
|
|
|
|
// still dispatch a notification to the main thread - we'll just ignore it.
|
|
|
|
bool result = GeckoChildProcessHost::WaitUntilConnected(timeoutMs);
|
|
|
|
InitAfterConnect(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GPUProcessHost::OnChannelConnected(int32_t peer_pid)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
GeckoChildProcessHost::OnChannelConnected(peer_pid);
|
|
|
|
|
|
|
|
// Post a task to the main thread. Take the lock because mTaskFactory is not
|
|
|
|
// thread-safe.
|
|
|
|
RefPtr<Runnable> runnable;
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
runnable = mTaskFactory.NewRunnableMethod(&GPUProcessHost::OnChannelConnectedTask);
|
|
|
|
}
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GPUProcessHost::OnChannelError()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
GeckoChildProcessHost::OnChannelError();
|
|
|
|
|
|
|
|
// Post a task to the main thread. Take the lock because mTaskFactory is not
|
|
|
|
// thread-safe.
|
|
|
|
RefPtr<Runnable> runnable;
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
runnable = mTaskFactory.NewRunnableMethod(&GPUProcessHost::OnChannelErrorTask);
|
|
|
|
}
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GPUProcessHost::OnChannelConnectedTask()
|
|
|
|
{
|
|
|
|
if (mLaunchPhase == LaunchPhase::Waiting) {
|
|
|
|
InitAfterConnect(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GPUProcessHost::OnChannelErrorTask()
|
|
|
|
{
|
|
|
|
if (mLaunchPhase == LaunchPhase::Waiting) {
|
|
|
|
InitAfterConnect(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 07:24:28 +03:00
|
|
|
static uint64_t sProcessTokenCounter = 0;
|
|
|
|
|
2016-06-11 05:27:24 +03:00
|
|
|
void
|
|
|
|
GPUProcessHost::InitAfterConnect(bool aSucceeded)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mLaunchPhase == LaunchPhase::Waiting);
|
|
|
|
MOZ_ASSERT(!mGPUChild);
|
|
|
|
|
|
|
|
mLaunchPhase = LaunchPhase::Complete;
|
|
|
|
|
|
|
|
if (aSucceeded) {
|
2016-07-18 07:24:28 +03:00
|
|
|
mProcessToken = ++sProcessTokenCounter;
|
2016-06-11 05:37:03 +03:00
|
|
|
mGPUChild = MakeUnique<GPUChild>(this);
|
2016-06-11 05:27:24 +03:00
|
|
|
DebugOnly<bool> rv =
|
|
|
|
mGPUChild->Open(GetChannel(), base::GetProcId(GetChildProcessHandle()));
|
|
|
|
MOZ_ASSERT(rv);
|
2016-06-27 09:33:20 +03:00
|
|
|
|
|
|
|
mGPUChild->Init();
|
2016-06-11 05:27:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mListener->OnProcessLaunchComplete(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GPUProcessHost::Shutdown()
|
|
|
|
{
|
2016-06-11 05:37:03 +03:00
|
|
|
MOZ_ASSERT(!mShutdownRequested);
|
|
|
|
|
2016-06-11 05:27:24 +03:00
|
|
|
mListener = nullptr;
|
2016-06-11 05:37:03 +03:00
|
|
|
|
|
|
|
if (mGPUChild) {
|
|
|
|
// OnChannelClosed uses this to check if the shutdown was expected or
|
|
|
|
// unexpected.
|
|
|
|
mShutdownRequested = true;
|
|
|
|
|
|
|
|
#ifdef NS_FREE_PERMANENT_DATA
|
2016-07-18 07:24:28 +03:00
|
|
|
// The channel might already be closed if we got here unexpectedly.
|
|
|
|
if (!mChannelClosed) {
|
|
|
|
mGPUChild->Close();
|
|
|
|
}
|
2016-06-11 05:37:03 +03:00
|
|
|
#else
|
|
|
|
// No need to communicate shutdown, the GPU process doesn't need to
|
|
|
|
// communicate anything back.
|
|
|
|
KillHard("NormalShutdown");
|
|
|
|
#endif
|
|
|
|
|
2016-07-18 07:24:28 +03:00
|
|
|
// If we're shutting down unexpectedly, we're in the middle of handling an
|
|
|
|
// ActorDestroy for PGPUChild, which is still on the stack. We'll return
|
|
|
|
// back to OnChannelClosed.
|
|
|
|
//
|
|
|
|
// Otherwise, we'll wait for OnChannelClose to be called whenever PGPUChild
|
|
|
|
// acknowledges shutdown.
|
2016-06-11 05:37:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-11 05:27:24 +03:00
|
|
|
DestroyProcess();
|
|
|
|
}
|
|
|
|
|
2016-06-11 05:37:03 +03:00
|
|
|
void
|
|
|
|
GPUProcessHost::OnChannelClosed()
|
|
|
|
{
|
|
|
|
if (!mShutdownRequested) {
|
|
|
|
// This is an unclean shutdown. Notify our listener that we're going away.
|
2016-07-18 07:24:28 +03:00
|
|
|
mChannelClosed = true;
|
2016-06-11 05:37:03 +03:00
|
|
|
if (mListener) {
|
|
|
|
mListener->OnProcessUnexpectedShutdown(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the actor.
|
|
|
|
GPUChild::Destroy(Move(mGPUChild));
|
|
|
|
MOZ_ASSERT(!mGPUChild);
|
|
|
|
|
|
|
|
// If the owner of GPUProcessHost already requested shutdown, we can now
|
|
|
|
// schedule destruction. Otherwise we must wait for someone to call
|
|
|
|
// Shutdown. Note that GPUProcessManager calls Shutdown within
|
|
|
|
// OnProcessUnexpectedShutdown.
|
|
|
|
if (mShutdownRequested) {
|
|
|
|
DestroyProcess();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GPUProcessHost::KillHard(const char* aReason)
|
|
|
|
{
|
|
|
|
ProcessHandle handle = GetChildProcessHandle();
|
|
|
|
if (!base::KillProcess(handle, base::PROCESS_END_KILLED_BY_USER, false)) {
|
|
|
|
NS_WARNING("failed to kill subprocess!");
|
|
|
|
}
|
|
|
|
|
|
|
|
SetAlreadyDead();
|
|
|
|
}
|
|
|
|
|
2016-07-18 07:24:28 +03:00
|
|
|
uint64_t
|
|
|
|
GPUProcessHost::GetProcessToken() const
|
|
|
|
{
|
|
|
|
return mProcessToken;
|
|
|
|
}
|
|
|
|
|
2016-05-28 04:03:12 +03:00
|
|
|
static void
|
|
|
|
DelayedDeleteSubprocess(GeckoChildProcessHost* aSubprocess)
|
|
|
|
{
|
|
|
|
XRE_GetIOMessageLoop()->
|
|
|
|
PostTask(mozilla::MakeAndAddRef<DeleteTask<GeckoChildProcessHost>>(aSubprocess));
|
|
|
|
}
|
|
|
|
|
2016-10-31 22:44:40 +03:00
|
|
|
void
|
|
|
|
GPUProcessHost::KillProcess()
|
|
|
|
{
|
|
|
|
KillHard("DiagnosticKill");
|
|
|
|
}
|
|
|
|
|
2016-06-11 05:27:24 +03:00
|
|
|
void
|
|
|
|
GPUProcessHost::DestroyProcess()
|
|
|
|
{
|
|
|
|
// Cancel all tasks. We don't want anything triggering after our caller
|
|
|
|
// expects this to go away.
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mTaskFactory.RevokeAll();
|
|
|
|
}
|
|
|
|
|
2016-05-28 04:03:12 +03:00
|
|
|
MessageLoop::current()->
|
|
|
|
PostTask(NewRunnableFunction(DelayedDeleteSubprocess, this));
|
2016-06-11 05:27:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|