2022-06-29 12:32:29 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "mozilla/layers/RemoteTextureMap.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
#include "mozilla/gfx/gfxVars.h"
|
2022-06-29 12:32:29 +03:00
|
|
|
#include "mozilla/layers/AsyncImagePipelineManager.h"
|
|
|
|
#include "mozilla/layers/BufferTexture.h"
|
|
|
|
#include "mozilla/layers/CompositorThread.h"
|
2022-09-07 03:58:34 +03:00
|
|
|
#include "mozilla/layers/RemoteTextureHostWrapper.h"
|
2022-06-29 12:32:29 +03:00
|
|
|
#include "mozilla/layers/WebRenderTextureHost.h"
|
2022-09-07 03:58:34 +03:00
|
|
|
#include "mozilla/StaticPrefs_webgl.h"
|
2022-12-07 05:24:57 +03:00
|
|
|
#include "mozilla/webrender/RenderThread.h"
|
2022-06-29 12:32:29 +03:00
|
|
|
#include "SharedSurface.h"
|
|
|
|
|
|
|
|
namespace mozilla::layers {
|
|
|
|
|
|
|
|
RemoteTextureOwnerClient::RemoteTextureOwnerClient(
|
|
|
|
const base::ProcessId aForPid)
|
|
|
|
: mForPid(aForPid) {}
|
|
|
|
|
|
|
|
RemoteTextureOwnerClient::~RemoteTextureOwnerClient() = default;
|
|
|
|
|
|
|
|
bool RemoteTextureOwnerClient::IsRegistered(
|
|
|
|
const RemoteTextureOwnerId aOwnerId) {
|
|
|
|
auto it = mOwnerIds.find(aOwnerId);
|
|
|
|
if (it == mOwnerIds.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteTextureOwnerClient::RegisterTextureOwner(
|
|
|
|
const RemoteTextureOwnerId aOwnerId) {
|
|
|
|
MOZ_ASSERT(mOwnerIds.find(aOwnerId) == mOwnerIds.end());
|
|
|
|
mOwnerIds.emplace(aOwnerId);
|
|
|
|
RemoteTextureMap::Get()->RegisterTextureOwner(aOwnerId, mForPid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteTextureOwnerClient::UnregisterTextureOwner(
|
|
|
|
const RemoteTextureOwnerId aOwnerId) {
|
|
|
|
auto it = mOwnerIds.find(aOwnerId);
|
|
|
|
if (it == mOwnerIds.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mOwnerIds.erase(it);
|
|
|
|
RemoteTextureMap::Get()->UnregisterTextureOwner(aOwnerId, mForPid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteTextureOwnerClient::UnregisterAllTextureOwners() {
|
|
|
|
if (!mOwnerIds.empty()) {
|
|
|
|
RemoteTextureMap::Get()->UnregisterTextureOwners(mOwnerIds, mForPid);
|
|
|
|
mOwnerIds.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 09:56:19 +03:00
|
|
|
void RemoteTextureOwnerClient::PushTexture(
|
2022-06-29 12:32:29 +03:00
|
|
|
const RemoteTextureId aTextureId, const RemoteTextureOwnerId aOwnerId,
|
|
|
|
UniquePtr<TextureData>&& aTextureData,
|
|
|
|
const std::shared_ptr<gl::SharedSurface>& aSharedSurface) {
|
|
|
|
MOZ_ASSERT(IsRegistered(aOwnerId));
|
2022-07-12 09:56:19 +03:00
|
|
|
RemoteTextureMap::Get()->PushTexture(aTextureId, aOwnerId, mForPid,
|
|
|
|
std::move(aTextureData), aSharedSurface);
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<TextureData>
|
|
|
|
RemoteTextureOwnerClient::CreateOrRecycleBufferTextureData(
|
|
|
|
const RemoteTextureOwnerId aOwnerId, gfx::IntSize aSize,
|
|
|
|
gfx::SurfaceFormat aFormat) {
|
2022-09-07 03:58:34 +03:00
|
|
|
auto texture = RemoteTextureMap::Get()->GetRecycledBufferTextureData(
|
|
|
|
aOwnerId, mForPid, aSize, aFormat);
|
2022-06-29 12:32:29 +03:00
|
|
|
if (texture) {
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2022-07-12 09:56:21 +03:00
|
|
|
auto flags = TextureFlags::DEALLOCATE_CLIENT | TextureFlags::REMOTE_TEXTURE;
|
|
|
|
auto* data = BufferTextureData::Create(aSize, aFormat, gfx::BackendType::SKIA,
|
|
|
|
LayersBackend::LAYERS_WR, flags,
|
|
|
|
ALLOC_DEFAULT, nullptr);
|
2022-06-29 12:32:29 +03:00
|
|
|
return UniquePtr<TextureData>(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<gl::SharedSurface>
|
|
|
|
RemoteTextureOwnerClient::GetRecycledSharedSurface(
|
|
|
|
const RemoteTextureOwnerId aOwnerId) {
|
|
|
|
return RemoteTextureMap::Get()->RemoteTextureMap::GetRecycledSharedSurface(
|
|
|
|
aOwnerId, mForPid);
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticAutoPtr<RemoteTextureMap> RemoteTextureMap::sInstance;
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void RemoteTextureMap::Init() {
|
|
|
|
MOZ_ASSERT(!sInstance);
|
|
|
|
sInstance = new RemoteTextureMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void RemoteTextureMap::Shutdown() {
|
|
|
|
if (sInstance) {
|
|
|
|
sInstance = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
RemoteTextureMap::RemoteTextureMap() : mMonitor("D3D11TextureMap::mMonitor") {}
|
2022-06-29 12:32:29 +03:00
|
|
|
|
|
|
|
RemoteTextureMap::~RemoteTextureMap() = default;
|
|
|
|
|
2022-07-12 09:56:19 +03:00
|
|
|
void RemoteTextureMap::PushTexture(
|
2022-06-29 12:32:29 +03:00
|
|
|
const RemoteTextureId aTextureId, const RemoteTextureOwnerId aOwnerId,
|
|
|
|
const base::ProcessId aForPid, UniquePtr<TextureData>&& aTextureData,
|
|
|
|
const std::shared_ptr<gl::SharedSurface>& aSharedSurface) {
|
|
|
|
MOZ_RELEASE_ASSERT(aTextureData);
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
RefPtr<TextureHost> textureHost =
|
|
|
|
RemoteTextureMap::CreateRemoteTexture(aTextureData.get());
|
|
|
|
if (!textureHost) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-29 12:32:29 +03:00
|
|
|
{
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
|
|
|
auto* owner = GetTextureOwner(lock, aOwnerId, aForPid);
|
|
|
|
if (!owner) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
auto textureData = MakeUnique<TextureDataHolder>(
|
|
|
|
aTextureId, textureHost, std::move(aTextureData), aSharedSurface);
|
|
|
|
|
|
|
|
MOZ_ASSERT(owner->mLatestTextureId < aTextureId);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
owner->mWaitingTextureDataHolders.push_back(std::move(textureData));
|
|
|
|
|
|
|
|
if (!gfx::gfxVars::WebglOopAsyncPresentForceSync()) {
|
|
|
|
// Update mAsyncRemoteTextureHost for async mode.
|
|
|
|
// This happens when PushTexture() with RemoteTextureId is called after
|
|
|
|
// GetRemoteTextureForDisplayList() with the RemoteTextureId.
|
|
|
|
const auto key = std::pair(aForPid, aTextureId);
|
|
|
|
auto it = mRemoteTextureHostWrapperHolders.find(key);
|
|
|
|
if (it != mRemoteTextureHostWrapperHolders.end()) {
|
|
|
|
MOZ_ASSERT(!it->second->mAsyncRemoteTextureHost);
|
|
|
|
it->second->mAsyncRemoteTextureHost = textureHost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mMonitor.Notify();
|
2022-09-07 03:58:34 +03:00
|
|
|
|
|
|
|
// Drop obsoleted remote textures.
|
|
|
|
while (!owner->mUsingTextureDataHolders.empty()) {
|
|
|
|
auto& front = owner->mUsingTextureDataHolders.front();
|
|
|
|
// When compositable ref of TextureHost becomes 0, the TextureHost is not
|
|
|
|
// used by WebRender anymore.
|
|
|
|
if (front->mTextureHost &&
|
|
|
|
front->mTextureHost->NumCompositableRefs() == 0) {
|
|
|
|
// Recycle gl::SharedSurface
|
|
|
|
if (front->mSharedSurface) {
|
|
|
|
owner->mRecycledSharedSurfaces.push(front->mSharedSurface);
|
|
|
|
front->mSharedSurface = nullptr;
|
|
|
|
}
|
|
|
|
// Recycle BufferTextureData
|
|
|
|
if (front->mTextureData && front->mTextureData->AsBufferTextureData()) {
|
|
|
|
owner->mRecycledTextures.push(std::move(front->mTextureData));
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
2022-09-08 17:27:44 +03:00
|
|
|
owner->mUsingTextureDataHolders.pop_front();
|
2022-09-07 03:58:34 +03:00
|
|
|
} else if (front->mTextureHost &&
|
|
|
|
front->mTextureHost->NumCompositableRefs() >= 0) {
|
|
|
|
// Remote texture is still in use by WebRender.
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
2022-09-08 17:27:44 +03:00
|
|
|
owner->mUsingTextureDataHolders.pop_front();
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteTextureMap::RegisterTextureOwner(const RemoteTextureOwnerId aOwnerId,
|
|
|
|
const base::ProcessId aForPid) {
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
|
|
|
const auto key = std::pair(aForPid, aOwnerId);
|
|
|
|
auto it = mTextureOwners.find(key);
|
|
|
|
if (it != mTextureOwners.end()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto owner = MakeUnique<TextureOwner>();
|
|
|
|
mTextureOwners.emplace(key, std::move(owner));
|
|
|
|
}
|
|
|
|
|
2022-09-08 17:27:44 +03:00
|
|
|
void RemoteTextureMap::KeepTextureDataAliveForTextureHostIfNecessary(
|
2022-12-07 05:24:57 +03:00
|
|
|
const MonitorAutoLock& aProofOfLock,
|
2022-09-08 17:27:44 +03:00
|
|
|
std::deque<UniquePtr<TextureDataHolder>>& aHolders) {
|
|
|
|
for (auto& holder : aHolders) {
|
|
|
|
// If remote texture of TextureHost still exist, keep
|
|
|
|
// gl::SharedSurface/TextureData alive while the TextureHost is alive.
|
|
|
|
if (holder->mTextureHost &&
|
|
|
|
holder->mTextureHost->NumCompositableRefs() >= 0) {
|
|
|
|
RefPtr<nsISerialEventTarget> eventTarget =
|
|
|
|
MessageLoop::current()->SerialEventTarget();
|
|
|
|
RefPtr<Runnable> runnable = NS_NewRunnableFunction(
|
|
|
|
"RemoteTextureMap::UnregisterTextureOwner::Runnable",
|
|
|
|
[data = std::move(holder->mTextureData),
|
|
|
|
surface = std::move(holder->mSharedSurface)]() {});
|
|
|
|
|
|
|
|
auto destroyedCallback = [eventTarget = std::move(eventTarget),
|
|
|
|
runnable = std::move(runnable)]() mutable {
|
|
|
|
eventTarget->Dispatch(runnable.forget());
|
|
|
|
};
|
|
|
|
|
|
|
|
holder->mTextureHost->SetDestroyedCallback(destroyedCallback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 12:32:29 +03:00
|
|
|
void RemoteTextureMap::UnregisterTextureOwner(
|
|
|
|
const RemoteTextureOwnerId aOwnerId, const base::ProcessId aForPid) {
|
2022-12-07 05:24:57 +03:00
|
|
|
UniquePtr<TextureOwner> releasingOwner; // Release outside the monitor
|
|
|
|
RefPtr<TextureHost> releasingTexture; // Release outside the monitor
|
2022-06-29 12:32:29 +03:00
|
|
|
{
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
|
|
|
const auto key = std::pair(aForPid, aOwnerId);
|
|
|
|
auto it = mTextureOwners.find(key);
|
|
|
|
if (it == mTextureOwners.end()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
return;
|
|
|
|
}
|
2022-09-08 17:27:44 +03:00
|
|
|
|
2022-09-12 06:49:02 +03:00
|
|
|
if (it->second->mLatestTextureHost) {
|
2022-12-07 05:24:57 +03:00
|
|
|
// Release CompositableRef in mMonitor
|
2022-09-12 06:49:02 +03:00
|
|
|
releasingTexture = it->second->mLatestTextureHost;
|
|
|
|
it->second->mLatestTextureHost = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
KeepTextureDataAliveForTextureHostIfNecessary(
|
|
|
|
lock, it->second->mWaitingTextureDataHolders);
|
|
|
|
|
2022-09-08 17:27:44 +03:00
|
|
|
KeepTextureDataAliveForTextureHostIfNecessary(
|
|
|
|
lock, it->second->mUsingTextureDataHolders);
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
releasingOwner = std::move(it->second);
|
2022-06-29 12:32:29 +03:00
|
|
|
mTextureOwners.erase(it);
|
2022-12-07 05:24:57 +03:00
|
|
|
|
|
|
|
mMonitor.Notify();
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteTextureMap::UnregisterTextureOwners(
|
|
|
|
const std::unordered_set<RemoteTextureOwnerId,
|
|
|
|
RemoteTextureOwnerId::HashFn>& aOwnerIds,
|
|
|
|
const base::ProcessId aForPid) {
|
2022-09-07 03:58:34 +03:00
|
|
|
std::vector<UniquePtr<TextureOwner>>
|
2022-12-07 05:24:57 +03:00
|
|
|
releasingOwners; // Release outside the monitor
|
2022-09-12 06:49:02 +03:00
|
|
|
std::vector<RefPtr<TextureHost>>
|
2022-12-07 05:24:57 +03:00
|
|
|
releasingTextures; // Release outside the monitor
|
2022-06-29 12:32:29 +03:00
|
|
|
{
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
|
|
|
for (auto id : aOwnerIds) {
|
|
|
|
const auto key = std::pair(aForPid, id);
|
|
|
|
auto it = mTextureOwners.find(key);
|
|
|
|
if (it == mTextureOwners.end()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-08 17:27:44 +03:00
|
|
|
|
2022-09-12 06:49:02 +03:00
|
|
|
if (it->second->mLatestTextureHost) {
|
2022-12-07 05:24:57 +03:00
|
|
|
// Release CompositableRef in mMonitor
|
2022-09-12 06:49:02 +03:00
|
|
|
releasingTextures.emplace_back(it->second->mLatestTextureHost);
|
|
|
|
it->second->mLatestTextureHost = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
KeepTextureDataAliveForTextureHostIfNecessary(
|
|
|
|
lock, it->second->mWaitingTextureDataHolders);
|
|
|
|
|
2022-09-08 17:27:44 +03:00
|
|
|
KeepTextureDataAliveForTextureHostIfNecessary(
|
|
|
|
lock, it->second->mUsingTextureDataHolders);
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
releasingOwners.push_back(std::move(it->second));
|
2022-06-29 12:32:29 +03:00
|
|
|
mTextureOwners.erase(it);
|
|
|
|
}
|
2022-12-07 05:24:57 +03:00
|
|
|
|
|
|
|
mMonitor.Notify();
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
/* static */
|
2022-06-29 12:32:29 +03:00
|
|
|
RefPtr<TextureHost> RemoteTextureMap::CreateRemoteTexture(
|
2022-09-07 03:58:34 +03:00
|
|
|
TextureData* aTextureData) {
|
2022-06-29 12:32:29 +03:00
|
|
|
SurfaceDescriptor desc;
|
2022-09-07 03:58:34 +03:00
|
|
|
DebugOnly<bool> ret = aTextureData->Serialize(desc);
|
2022-06-29 12:32:29 +03:00
|
|
|
MOZ_ASSERT(ret);
|
2022-09-07 03:58:34 +03:00
|
|
|
TextureFlags flags =
|
|
|
|
TextureFlags::REMOTE_TEXTURE | TextureFlags::DEALLOCATE_CLIENT;
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
Maybe<wr::ExternalImageId> externalImageId = Nothing();
|
2022-06-29 12:32:29 +03:00
|
|
|
RefPtr<TextureHost> textureHost =
|
|
|
|
TextureHost::Create(desc, null_t(), nullptr, LayersBackend::LAYERS_WR,
|
|
|
|
flags, externalImageId);
|
|
|
|
MOZ_ASSERT(textureHost);
|
|
|
|
if (!textureHost) {
|
|
|
|
gfxCriticalNoteOnce << "Failed to create remote texture";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
textureHost->EnsureRenderTexture(Nothing());
|
|
|
|
|
2022-06-29 12:32:29 +03:00
|
|
|
return textureHost;
|
|
|
|
}
|
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
void RemoteTextureMap::UpdateTexture(const MonitorAutoLock& aProofOfLock,
|
2022-09-07 03:58:34 +03:00
|
|
|
RemoteTextureMap::TextureOwner* aOwner,
|
|
|
|
const RemoteTextureId aTextureId) {
|
2022-06-29 12:32:29 +03:00
|
|
|
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
|
2022-09-07 03:58:34 +03:00
|
|
|
MOZ_ASSERT(aOwner);
|
|
|
|
MOZ_ASSERT(aTextureId >= aOwner->mLatestTextureId);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
if (aTextureId == aOwner->mLatestTextureId) {
|
|
|
|
// No need to update texture.
|
|
|
|
return;
|
|
|
|
}
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
// Move remote textures to mUsingTextureDataHolders.
|
|
|
|
while (!aOwner->mWaitingTextureDataHolders.empty()) {
|
|
|
|
auto& front = aOwner->mWaitingTextureDataHolders.front();
|
|
|
|
if (aTextureId < front->mTextureId) {
|
|
|
|
break;
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
2022-09-07 03:58:34 +03:00
|
|
|
MOZ_RELEASE_ASSERT(front->mTextureHost);
|
|
|
|
aOwner->mLatestTextureHost = front->mTextureHost;
|
|
|
|
aOwner->mLatestTextureId = front->mTextureId;
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
UniquePtr<TextureDataHolder> holder = std::move(front);
|
2022-12-07 05:24:57 +03:00
|
|
|
aOwner->mWaitingTextureDataHolders.pop_front();
|
2022-09-08 17:27:44 +03:00
|
|
|
aOwner->mUsingTextureDataHolders.push_back(std::move(holder));
|
2022-09-07 03:58:34 +03:00
|
|
|
}
|
|
|
|
}
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
void RemoteTextureMap::GetRemoteTextureForDisplayList(
|
2022-09-07 03:58:34 +03:00
|
|
|
RemoteTextureHostWrapper* aTextureHostWrapper) {
|
|
|
|
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
|
|
|
|
MOZ_ASSERT(aTextureHostWrapper);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
const auto& textureId = aTextureHostWrapper->mTextureId;
|
|
|
|
const auto& ownerId = aTextureHostWrapper->mOwnerId;
|
|
|
|
const auto& forPid = aTextureHostWrapper->mForPid;
|
|
|
|
const auto& size = aTextureHostWrapper->mSize;
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
RefPtr<TextureHost> textureHost;
|
|
|
|
{
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2022-09-07 03:58:34 +03:00
|
|
|
|
|
|
|
auto* owner = GetTextureOwner(lock, ownerId, forPid);
|
|
|
|
if (!owner) {
|
|
|
|
return;
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
UpdateTexture(lock, owner, textureId);
|
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
if (gfx::gfxVars::WebglOopAsyncPresentForceSync()) {
|
2022-09-07 03:58:34 +03:00
|
|
|
// remote texture sync ipc
|
|
|
|
if (textureId == owner->mLatestTextureId) {
|
|
|
|
MOZ_ASSERT(owner->mLatestTextureHost);
|
|
|
|
MOZ_ASSERT(owner->mLatestTextureHost->GetSize() == size);
|
2022-11-05 07:14:31 +03:00
|
|
|
if (owner->mLatestTextureHost->GetSize() != size) {
|
|
|
|
gfxCriticalNoteOnce << "unexpected remote texture size: "
|
|
|
|
<< owner->mLatestTextureHost->GetSize()
|
|
|
|
<< " expected: " << size;
|
|
|
|
}
|
2022-09-07 03:58:34 +03:00
|
|
|
textureHost = owner->mLatestTextureHost;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// remote texture async ipc
|
2022-12-07 05:24:57 +03:00
|
|
|
if (owner->mLatestTextureHost) {
|
|
|
|
if (owner->mLatestTextureHost->GetSize() == size) {
|
|
|
|
textureHost = owner->mLatestTextureHost;
|
|
|
|
} else {
|
|
|
|
gfxCriticalNoteOnce << "unexpected remote texture size: "
|
|
|
|
<< owner->mLatestTextureHost->GetSize()
|
|
|
|
<< " expected: " << size;
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gfxCriticalNoteOnce << "remote texture does not exist";
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update mAsyncRemoteTextureHost for async mode
|
|
|
|
if (textureId == owner->mLatestTextureId) {
|
|
|
|
const auto key = std::pair(forPid, textureId);
|
|
|
|
auto it = mRemoteTextureHostWrapperHolders.find(key);
|
|
|
|
if (it != mRemoteTextureHostWrapperHolders.end() &&
|
|
|
|
!it->second->mAsyncRemoteTextureHost) {
|
|
|
|
it->second->mAsyncRemoteTextureHost = owner->mLatestTextureHost;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(it->second->mAsyncRemoteTextureHost ==
|
|
|
|
owner->mLatestTextureHost);
|
|
|
|
}
|
2022-09-07 03:58:34 +03:00
|
|
|
}
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
if (textureHost) {
|
2022-12-07 05:24:57 +03:00
|
|
|
aTextureHostWrapper->SetRemoteTextureHostForDisplayList(lock,
|
|
|
|
textureHost);
|
2022-09-07 03:58:34 +03:00
|
|
|
aTextureHostWrapper->ApplyTextureFlagsToRemoteTexture();
|
|
|
|
}
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
2022-09-07 03:58:34 +03:00
|
|
|
}
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
wr::MaybeExternalImageId
|
|
|
|
RemoteTextureMap::GetExternalImageIdOfRemoteTextureSync(
|
|
|
|
const RemoteTextureId aTextureId, const RemoteTextureOwnerId aOwnerId,
|
|
|
|
const base::ProcessId aForPid) {
|
|
|
|
MOZ_ASSERT(wr::RenderThread::IsInRenderThread());
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
const TimeDuration timeout = TimeDuration::FromMilliseconds(10000);
|
|
|
|
|
|
|
|
const auto key = std::pair(aForPid, aTextureId);
|
|
|
|
auto it = mRemoteTextureHostWrapperHolders.find(key);
|
|
|
|
if (it == mRemoteTextureHostWrapperHolders.end()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
TextureHost* remoteTexture = it->second->mAsyncRemoteTextureHost;
|
|
|
|
|
|
|
|
auto* owner = GetTextureOwner(lock, aOwnerId, aForPid);
|
|
|
|
// When owner is alreay unregistered, remote texture will not be pushed.
|
|
|
|
if (!owner) {
|
|
|
|
if (!remoteTexture) {
|
|
|
|
// This could happen with IPC abnormal shutdown
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
return remoteTexture->GetMaybeExternalImageId();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!remoteTexture) {
|
|
|
|
CVStatus status = mMonitor.Wait(timeout);
|
|
|
|
if (status == CVStatus::Timeout) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = mRemoteTextureHostWrapperHolders.find(key);
|
|
|
|
if (it == mRemoteTextureHostWrapperHolders.end()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteTexture = it->second->mAsyncRemoteTextureHost;
|
|
|
|
if (!remoteTexture) {
|
|
|
|
auto* owner = GetTextureOwner(lock, aOwnerId, aForPid);
|
|
|
|
// When owner is alreay unregistered, remote texture will not be pushed.
|
|
|
|
if (!owner) {
|
|
|
|
// This could happen with IPC abnormal shutdown
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return remoteTexture->GetMaybeExternalImageId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteTextureMap::ReleaseRemoteTextureHostForDisplayList(
|
2022-09-07 03:58:34 +03:00
|
|
|
RemoteTextureHostWrapper* aTextureHostWrapper) {
|
|
|
|
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
|
|
|
|
MOZ_ASSERT(aTextureHostWrapper);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
|
|
|
RefPtr<TextureHost> releasingTexture; // Release outside the mutex
|
|
|
|
{
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
releasingTexture =
|
|
|
|
aTextureHostWrapper->GetRemoteTextureHostForDisplayList(lock);
|
|
|
|
aTextureHostWrapper->SetRemoteTextureHostForDisplayList(lock, nullptr);
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
RefPtr<TextureHost> RemoteTextureMap::GetOrCreateRemoteTextureHostWrapper(
|
|
|
|
const RemoteTextureId aTextureId, const RemoteTextureOwnerId aOwnerId,
|
|
|
|
const base::ProcessId aForPid, const gfx::IntSize aSize,
|
|
|
|
const TextureFlags aFlags) {
|
|
|
|
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
|
|
|
const auto key = std::pair(aForPid, aTextureId);
|
2022-12-07 05:24:57 +03:00
|
|
|
auto it = mRemoteTextureHostWrapperHolders.find(key);
|
|
|
|
if (it != mRemoteTextureHostWrapperHolders.end()) {
|
|
|
|
return it->second->mRemoteTextureHostWrapper;
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
auto wrapper = RemoteTextureHostWrapper::Create(aTextureId, aOwnerId, aForPid,
|
|
|
|
aSize, aFlags);
|
|
|
|
auto wrapperHolder = MakeUnique<RemoteTextureHostWrapperHolder>(wrapper);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
mRemoteTextureHostWrapperHolders.emplace(key, std::move(wrapperHolder));
|
|
|
|
|
|
|
|
return wrapper;
|
2022-09-07 03:58:34 +03:00
|
|
|
}
|
2022-06-29 12:32:29 +03:00
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
void RemoteTextureMap::UnregisterRemoteTextureHostWrapper(
|
|
|
|
const RemoteTextureId aTextureId, const RemoteTextureOwnerId aOwnerId,
|
|
|
|
const base::ProcessId aForPid) {
|
|
|
|
MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
|
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
std::vector<RefPtr<TextureHost>>
|
|
|
|
releasingTextures; // Release outside the monitor
|
2022-09-07 03:58:34 +03:00
|
|
|
{
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2022-09-07 03:58:34 +03:00
|
|
|
|
|
|
|
const auto key = std::pair(aForPid, aTextureId);
|
2022-12-07 05:24:57 +03:00
|
|
|
auto it = mRemoteTextureHostWrapperHolders.find(key);
|
|
|
|
if (it == mRemoteTextureHostWrapperHolders.end()) {
|
2022-09-07 03:58:34 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
|
|
|
|
return;
|
|
|
|
}
|
2022-12-07 05:24:57 +03:00
|
|
|
releasingTextures.emplace_back(it->second->mRemoteTextureHostWrapper);
|
|
|
|
if (it->second->mAsyncRemoteTextureHost) {
|
|
|
|
releasingTextures.emplace_back(it->second->mAsyncRemoteTextureHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
mRemoteTextureHostWrapperHolders.erase(it);
|
|
|
|
mMonitor.Notify();
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<TextureData> RemoteTextureMap::GetRecycledBufferTextureData(
|
|
|
|
const RemoteTextureOwnerId aOwnerId, const base::ProcessId aForPid,
|
|
|
|
gfx::IntSize aSize, gfx::SurfaceFormat aFormat) {
|
2022-09-07 03:58:34 +03:00
|
|
|
std::stack<UniquePtr<TextureData>>
|
2022-12-07 05:24:57 +03:00
|
|
|
releasingTextures; // Release outside the monitor
|
2022-06-29 12:32:29 +03:00
|
|
|
UniquePtr<TextureData> texture;
|
|
|
|
{
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
|
|
|
auto* owner = GetTextureOwner(lock, aOwnerId, aForPid);
|
|
|
|
if (!owner) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (owner->mRecycledTextures.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!owner->mRecycledTextures.empty()) {
|
|
|
|
auto& top = owner->mRecycledTextures.top();
|
|
|
|
auto* bufferTexture = top->AsBufferTextureData();
|
|
|
|
if (bufferTexture && bufferTexture->GetSize() == aSize &&
|
|
|
|
bufferTexture->GetFormat() == aFormat) {
|
|
|
|
texture = std::move(top);
|
|
|
|
owner->mRecycledTextures.pop();
|
|
|
|
} else {
|
2022-09-07 03:58:34 +03:00
|
|
|
// If size or format are different, release all textures.
|
|
|
|
owner->mRecycledTextures.swap(releasingTextures);
|
2022-06-29 12:32:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<gl::SharedSurface> RemoteTextureMap::GetRecycledSharedSurface(
|
|
|
|
const RemoteTextureOwnerId aOwnerId, const base::ProcessId aForPid) {
|
|
|
|
std::shared_ptr<gl::SharedSurface> sharedSurface;
|
|
|
|
{
|
2022-12-07 05:24:57 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2022-06-29 12:32:29 +03:00
|
|
|
|
|
|
|
auto* owner = GetTextureOwner(lock, aOwnerId, aForPid);
|
|
|
|
if (!owner) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (owner->mRecycledSharedSurfaces.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!owner->mRecycledSharedSurfaces.empty()) {
|
2022-09-07 03:58:34 +03:00
|
|
|
sharedSurface = owner->mRecycledSharedSurfaces.top();
|
2022-06-29 12:32:29 +03:00
|
|
|
owner->mRecycledSharedSurfaces.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sharedSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteTextureMap::TextureOwner* RemoteTextureMap::GetTextureOwner(
|
2022-12-07 05:24:57 +03:00
|
|
|
const MonitorAutoLock& aProofOfLock, const RemoteTextureOwnerId aOwnerId,
|
2022-06-29 12:32:29 +03:00
|
|
|
const base::ProcessId aForPid) {
|
|
|
|
const auto key = std::pair(aForPid, aOwnerId);
|
|
|
|
auto it = mTextureOwners.find(key);
|
|
|
|
if (it == mTextureOwners.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
2022-09-07 03:58:34 +03:00
|
|
|
RemoteTextureMap::TextureDataHolder::TextureDataHolder(
|
|
|
|
const RemoteTextureId aTextureId, RefPtr<TextureHost> aTextureHost,
|
|
|
|
UniquePtr<TextureData>&& aTextureData,
|
|
|
|
const std::shared_ptr<gl::SharedSurface>& aSharedSurface)
|
2022-06-29 12:32:29 +03:00
|
|
|
: mTextureId(aTextureId),
|
2022-09-07 03:58:34 +03:00
|
|
|
mTextureHost(aTextureHost),
|
2022-06-29 12:32:29 +03:00
|
|
|
mTextureData(std::move(aTextureData)),
|
|
|
|
mSharedSurface(aSharedSurface) {}
|
|
|
|
|
2022-12-07 05:24:57 +03:00
|
|
|
RemoteTextureMap::RemoteTextureHostWrapperHolder::
|
|
|
|
RemoteTextureHostWrapperHolder(
|
|
|
|
RefPtr<TextureHost> aRemoteTextureHostWrapper)
|
|
|
|
: mRemoteTextureHostWrapper(aRemoteTextureHostWrapper) {}
|
|
|
|
|
2022-06-29 12:32:29 +03:00
|
|
|
} // namespace mozilla::layers
|