2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2018-11-14 21:06:11 +03:00
|
|
|
/* 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 "RemoteDecoderManagerChild.h"
|
|
|
|
|
|
|
|
#include "base/task.h"
|
|
|
|
|
2019-02-14 22:08:21 +03:00
|
|
|
#include "RemoteDecoderChild.h"
|
2019-06-11 05:01:34 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "mozilla/ipc/ProtocolUtils.h"
|
|
|
|
#include "mozilla/layers/SynchronousTask.h"
|
|
|
|
#include "mozilla/gfx/DataSurfaceHelpers.h"
|
|
|
|
#include "mozilla/layers/ISurfaceAllocator.h"
|
2018-11-14 21:06:11 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2019-06-11 05:01:34 +03:00
|
|
|
using namespace layers;
|
|
|
|
using namespace gfx;
|
|
|
|
|
2018-11-14 21:06:11 +03:00
|
|
|
// Only modified on the main-thread
|
|
|
|
StaticRefPtr<nsIThread> sRemoteDecoderManagerChildThread;
|
|
|
|
StaticRefPtr<AbstractThread> sRemoteDecoderManagerChildAbstractThread;
|
|
|
|
|
|
|
|
// Only accessed from sRemoteDecoderManagerChildThread
|
2019-06-11 05:01:34 +03:00
|
|
|
static StaticRefPtr<RemoteDecoderManagerChild>
|
|
|
|
sRemoteDecoderManagerChildForRDDProcess;
|
|
|
|
|
|
|
|
static StaticRefPtr<RemoteDecoderManagerChild>
|
|
|
|
sRemoteDecoderManagerChildForGPUProcess;
|
|
|
|
static UniquePtr<nsTArray<RefPtr<Runnable>>> sRecreateTasks;
|
2018-11-14 21:06:11 +03:00
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void RemoteDecoderManagerChild::InitializeThread() {
|
2018-11-14 21:06:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!sRemoteDecoderManagerChildThread) {
|
|
|
|
RefPtr<nsIThread> childThread;
|
|
|
|
nsresult rv = NS_NewNamedThread("RemVidChild", getter_AddRefs(childThread));
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
sRemoteDecoderManagerChildThread = childThread;
|
|
|
|
|
|
|
|
sRemoteDecoderManagerChildAbstractThread =
|
|
|
|
AbstractThread::CreateXPCOMThreadWrapper(childThread, false);
|
2019-06-11 05:01:34 +03:00
|
|
|
|
|
|
|
sRecreateTasks = MakeUnique<nsTArray<RefPtr<Runnable>>>();
|
2018-11-14 21:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
2019-06-11 05:01:34 +03:00
|
|
|
void RemoteDecoderManagerChild::InitForRDDProcess(
|
|
|
|
Endpoint<PRemoteDecoderManagerChild>&& aVideoManager) {
|
|
|
|
InitializeThread();
|
|
|
|
sRemoteDecoderManagerChildThread->Dispatch(
|
|
|
|
NewRunnableFunction("InitForContentRunnable", &OpenForRDDProcess,
|
|
|
|
std::move(aVideoManager)),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void RemoteDecoderManagerChild::InitForGPUProcess(
|
2018-11-14 21:06:11 +03:00
|
|
|
Endpoint<PRemoteDecoderManagerChild>&& aVideoManager) {
|
|
|
|
InitializeThread();
|
|
|
|
sRemoteDecoderManagerChildThread->Dispatch(
|
2019-06-11 05:01:34 +03:00
|
|
|
NewRunnableFunction("InitForContentRunnable", &OpenForGPUProcess,
|
2018-11-14 21:06:11 +03:00
|
|
|
std::move(aVideoManager)),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void RemoteDecoderManagerChild::Shutdown() {
|
2018-11-14 21:06:11 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (sRemoteDecoderManagerChildThread) {
|
|
|
|
sRemoteDecoderManagerChildThread->Dispatch(
|
2019-06-11 05:01:34 +03:00
|
|
|
NS_NewRunnableFunction(
|
|
|
|
"dom::RemoteDecoderManagerChild::Shutdown",
|
|
|
|
[]() {
|
|
|
|
if (sRemoteDecoderManagerChildForRDDProcess &&
|
|
|
|
sRemoteDecoderManagerChildForRDDProcess->CanSend()) {
|
|
|
|
sRemoteDecoderManagerChildForRDDProcess->Close();
|
|
|
|
sRemoteDecoderManagerChildForRDDProcess = nullptr;
|
|
|
|
}
|
|
|
|
if (sRemoteDecoderManagerChildForGPUProcess &&
|
|
|
|
sRemoteDecoderManagerChildForGPUProcess->CanSend()) {
|
|
|
|
sRemoteDecoderManagerChildForGPUProcess->Close();
|
|
|
|
sRemoteDecoderManagerChildForGPUProcess = nullptr;
|
|
|
|
}
|
|
|
|
}),
|
2018-11-14 21:06:11 +03:00
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
sRemoteDecoderManagerChildAbstractThread = nullptr;
|
|
|
|
sRemoteDecoderManagerChildThread->Shutdown();
|
|
|
|
sRemoteDecoderManagerChildThread = nullptr;
|
2019-06-11 05:01:34 +03:00
|
|
|
|
|
|
|
sRecreateTasks = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDecoderManagerChild::RunWhenGPUProcessRecreated(
|
|
|
|
already_AddRefed<Runnable> aTask) {
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == GetManagerThread());
|
|
|
|
|
|
|
|
// If we've already been recreated, then run the task immediately.
|
|
|
|
if (GetGPUProcessSingleton() && GetGPUProcessSingleton() != this &&
|
|
|
|
GetGPUProcessSingleton()->CanSend()) {
|
|
|
|
RefPtr<Runnable> task = aTask;
|
|
|
|
task->Run();
|
|
|
|
} else {
|
|
|
|
sRecreateTasks->AppendElement(aTask);
|
2018-11-14 21:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
2019-06-11 05:01:34 +03:00
|
|
|
RemoteDecoderManagerChild* RemoteDecoderManagerChild::GetRDDProcessSingleton() {
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == GetManagerThread());
|
|
|
|
return sRemoteDecoderManagerChildForRDDProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
RemoteDecoderManagerChild* RemoteDecoderManagerChild::GetGPUProcessSingleton() {
|
2018-11-14 21:06:11 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == GetManagerThread());
|
2019-06-11 05:01:34 +03:00
|
|
|
return sRemoteDecoderManagerChildForGPUProcess;
|
2018-11-14 21:06:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
nsIThread* RemoteDecoderManagerChild::GetManagerThread() {
|
2018-11-14 21:06:11 +03:00
|
|
|
return sRemoteDecoderManagerChildThread;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
AbstractThread* RemoteDecoderManagerChild::GetManagerAbstractThread() {
|
2018-11-14 21:06:11 +03:00
|
|
|
return sRemoteDecoderManagerChildAbstractThread;
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:08:21 +03:00
|
|
|
PRemoteDecoderChild* RemoteDecoderManagerChild::AllocPRemoteDecoderChild(
|
|
|
|
const RemoteDecoderInfoIPDL& /* not used */,
|
2019-06-11 05:01:51 +03:00
|
|
|
const CreateDecoderParams::OptionSet& aOptions,
|
2019-06-28 09:32:16 +03:00
|
|
|
const Maybe<layers::TextureFactoryIdentifier>& aIdentifier, bool* aSuccess,
|
2018-11-14 21:06:11 +03:00
|
|
|
nsCString* /* not used */) {
|
2019-02-14 22:08:21 +03:00
|
|
|
// RemoteDecoderModule is responsible for creating RemoteDecoderChild
|
|
|
|
// classes.
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"RemoteDecoderManagerChild cannot create "
|
|
|
|
"RemoteDecoderChild classes");
|
|
|
|
return nullptr;
|
2018-11-14 21:06:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-14 22:08:21 +03:00
|
|
|
bool RemoteDecoderManagerChild::DeallocPRemoteDecoderChild(
|
|
|
|
PRemoteDecoderChild* actor) {
|
|
|
|
RemoteDecoderChild* child = static_cast<RemoteDecoderChild*>(actor);
|
2018-11-14 21:06:11 +03:00
|
|
|
child->IPDLActorDestroyed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-04 06:41:45 +03:00
|
|
|
RemoteDecoderManagerChild::RemoteDecoderManagerChild(
|
|
|
|
layers::VideoBridgeSource aSource)
|
|
|
|
: mSource(aSource) {}
|
|
|
|
|
2019-06-11 05:01:34 +03:00
|
|
|
void RemoteDecoderManagerChild::OpenForRDDProcess(
|
2018-11-14 21:06:11 +03:00
|
|
|
Endpoint<PRemoteDecoderManagerChild>&& aEndpoint) {
|
2019-01-18 06:53:51 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == GetManagerThread());
|
|
|
|
// Only create RemoteDecoderManagerChild, bind new endpoint and init
|
|
|
|
// ipdl if:
|
|
|
|
// 1) haven't init'd sRemoteDecoderManagerChild
|
|
|
|
// or
|
|
|
|
// 2) if ActorDestroy was called (mCanSend is false) meaning the other
|
|
|
|
// end of the ipc channel was torn down
|
2019-06-11 05:01:34 +03:00
|
|
|
if (sRemoteDecoderManagerChildForRDDProcess &&
|
|
|
|
sRemoteDecoderManagerChildForRDDProcess->mCanSend) {
|
2019-01-18 06:53:51 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-06-11 05:01:34 +03:00
|
|
|
sRemoteDecoderManagerChildForRDDProcess = nullptr;
|
2018-11-14 21:06:11 +03:00
|
|
|
if (aEndpoint.IsValid()) {
|
2019-11-04 06:41:45 +03:00
|
|
|
RefPtr<RemoteDecoderManagerChild> manager = new RemoteDecoderManagerChild(VideoBridgeSource::RddProcess);
|
2018-11-14 21:06:11 +03:00
|
|
|
if (aEndpoint.Bind(manager)) {
|
2019-06-11 05:01:34 +03:00
|
|
|
sRemoteDecoderManagerChildForRDDProcess = manager;
|
2018-11-14 21:06:11 +03:00
|
|
|
manager->InitIPDL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 05:01:34 +03:00
|
|
|
void RemoteDecoderManagerChild::OpenForGPUProcess(
|
|
|
|
Endpoint<PRemoteDecoderManagerChild>&& aEndpoint) {
|
|
|
|
// Make sure we always dispatch everything in sRecreateTasks, even if we
|
|
|
|
// fail since this is as close to being recreated as we will ever be.
|
|
|
|
sRemoteDecoderManagerChildForGPUProcess = nullptr;
|
|
|
|
if (aEndpoint.IsValid()) {
|
2019-11-04 06:41:45 +03:00
|
|
|
RefPtr<RemoteDecoderManagerChild> manager = new RemoteDecoderManagerChild(VideoBridgeSource::GpuProcess);
|
2019-06-11 05:01:34 +03:00
|
|
|
if (aEndpoint.Bind(manager)) {
|
|
|
|
sRemoteDecoderManagerChildForGPUProcess = manager;
|
|
|
|
manager->InitIPDL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Runnable* task : *sRecreateTasks) {
|
|
|
|
task->Run();
|
|
|
|
}
|
|
|
|
sRecreateTasks->Clear();
|
|
|
|
}
|
|
|
|
|
2018-11-14 21:06:11 +03:00
|
|
|
void RemoteDecoderManagerChild::InitIPDL() {
|
|
|
|
mCanSend = true;
|
|
|
|
mIPDLSelfRef = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDecoderManagerChild::ActorDestroy(ActorDestroyReason aWhy) {
|
|
|
|
mCanSend = false;
|
|
|
|
}
|
|
|
|
|
2019-05-21 20:04:21 +03:00
|
|
|
void RemoteDecoderManagerChild::ActorDealloc() { mIPDLSelfRef = nullptr; }
|
2018-11-14 21:06:11 +03:00
|
|
|
|
|
|
|
bool RemoteDecoderManagerChild::CanSend() {
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == GetManagerThread());
|
|
|
|
return mCanSend;
|
|
|
|
}
|
|
|
|
|
2019-06-11 05:01:34 +03:00
|
|
|
bool RemoteDecoderManagerChild::DeallocShmem(mozilla::ipc::Shmem& aShmem) {
|
|
|
|
if (NS_GetCurrentThread() != sRemoteDecoderManagerChildThread) {
|
|
|
|
RefPtr<RemoteDecoderManagerChild> self = this;
|
|
|
|
mozilla::ipc::Shmem shmem = aShmem;
|
|
|
|
sRemoteDecoderManagerChildThread->Dispatch(
|
|
|
|
NS_NewRunnableFunction("RemoteDecoderManagerChild::DeallocShmem",
|
|
|
|
[self, shmem]() {
|
|
|
|
if (self->CanSend()) {
|
|
|
|
mozilla::ipc::Shmem shmemCopy = shmem;
|
|
|
|
self->DeallocShmem(shmemCopy);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return PRemoteDecoderManagerChild::DeallocShmem(aShmem);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SurfaceDescriptorUserData {
|
|
|
|
SurfaceDescriptorUserData(RemoteDecoderManagerChild* aAllocator,
|
|
|
|
SurfaceDescriptor& aSD)
|
|
|
|
: mAllocator(aAllocator), mSD(aSD) {}
|
|
|
|
~SurfaceDescriptorUserData() { DestroySurfaceDescriptor(mAllocator, &mSD); }
|
|
|
|
|
|
|
|
RefPtr<RemoteDecoderManagerChild> mAllocator;
|
|
|
|
SurfaceDescriptor mSD;
|
|
|
|
};
|
|
|
|
|
|
|
|
void DeleteSurfaceDescriptorUserData(void* aClosure) {
|
|
|
|
SurfaceDescriptorUserData* sd =
|
|
|
|
reinterpret_cast<SurfaceDescriptorUserData*>(aClosure);
|
|
|
|
delete sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<SourceSurface> RemoteDecoderManagerChild::Readback(
|
|
|
|
const SurfaceDescriptorGPUVideo& aSD) {
|
|
|
|
// We can't use NS_DISPATCH_SYNC here since that can spin the event
|
|
|
|
// loop while it waits. This function can be called from JS and we
|
|
|
|
// don't want that to happen.
|
|
|
|
SynchronousTask task("Readback sync");
|
|
|
|
|
|
|
|
RefPtr<RemoteDecoderManagerChild> ref = this;
|
|
|
|
SurfaceDescriptor sd;
|
|
|
|
if (NS_FAILED(sRemoteDecoderManagerChildThread->Dispatch(
|
|
|
|
NS_NewRunnableFunction("RemoteDecoderManagerChild::Readback",
|
|
|
|
[&]() {
|
|
|
|
AutoCompleteTask complete(&task);
|
|
|
|
if (ref->CanSend()) {
|
|
|
|
ref->SendReadback(aSD, &sd);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
NS_DISPATCH_NORMAL))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
task.Wait();
|
|
|
|
|
|
|
|
if (!IsSurfaceDescriptorValid(sd)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DataSourceSurface> source = GetSurfaceForDescriptor(sd);
|
|
|
|
if (!source) {
|
|
|
|
DestroySurfaceDescriptor(this, &sd);
|
|
|
|
NS_WARNING("Failed to map SurfaceDescriptor in Readback");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UserDataKey sSurfaceDescriptor;
|
|
|
|
source->AddUserData(&sSurfaceDescriptor,
|
|
|
|
new SurfaceDescriptorUserData(this, sd),
|
|
|
|
DeleteSurfaceDescriptorUserData);
|
|
|
|
|
|
|
|
return source.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDecoderManagerChild::DeallocateSurfaceDescriptorGPUVideo(
|
|
|
|
const SurfaceDescriptorGPUVideo& aSD) {
|
|
|
|
RefPtr<RemoteDecoderManagerChild> ref = this;
|
|
|
|
SurfaceDescriptorGPUVideo sd = std::move(aSD);
|
|
|
|
sRemoteDecoderManagerChildThread->Dispatch(
|
|
|
|
NS_NewRunnableFunction(
|
|
|
|
"RemoteDecoderManagerChild::DeallocateSurfaceDescriptorGPUVideo",
|
|
|
|
[ref, sd]() {
|
|
|
|
if (ref->CanSend()) {
|
|
|
|
ref->SendDeallocateSurfaceDescriptorGPUVideo(sd);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteDecoderManagerChild::HandleFatalError(const char* aMsg) const {
|
|
|
|
dom::ContentChild::FatalErrorIfNotUsingGPUProcess(aMsg, OtherPid());
|
|
|
|
}
|
|
|
|
|
2018-11-14 21:06:11 +03:00
|
|
|
} // namespace mozilla
|