#include "gpu/command_buffer/service/shared_image/shared_image_manager.h"
#include <inttypes.h>
#include <memory>
#include <utility>
#include "base/containers/contains.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/logging.h"
#include "base/memory/stack_allocated.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "gpu/command_buffer/common/shared_image_trace_utils.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/shared_context_state.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "ui/gl/trace_util.h"
#if BUILDFLAG(IS_WIN)
#include "gpu/command_buffer/service/dxgi_shared_handle_manager.h"
#include "ui/gfx/win/d3d_shared_fence.h"
#include "ui/gl/direct_composition_support.h"
#include "ui/gl/gl_angle_util_win.h"
#endif
#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/android_hardware_buffer_compat.h"
#endif
#if DCHECK_IS_ON()
#define CALLED_ON_VALID_THREAD() …
#else
#define CALLED_ON_VALID_THREAD …
#endif
namespace gpu {
namespace {
void EnforceSharedImageUsage(const SharedImageBacking& backing,
SharedImageUsageSet usage) { … }
}
bool operator<(const std::unique_ptr<SharedImageBacking>& lhs,
const std::unique_ptr<SharedImageBacking>& rhs) { … }
bool operator<(const Mailbox& lhs,
const std::unique_ptr<SharedImageBacking>& rhs) { … }
bool operator<(const std::unique_ptr<SharedImageBacking>& lhs,
const Mailbox& rhs) { … }
class SCOPED_LOCKABLE SharedImageManager::AutoLock { … };
SharedImageManager::SharedImageManager(bool thread_safe,
bool display_context_on_another_thread)
: … { … }
SharedImageManager::~SharedImageManager() { … }
std::unique_ptr<SharedImageRepresentationFactoryRef>
SharedImageManager::Register(std::unique_ptr<SharedImageBacking> backing,
MemoryTypeTracker* tracker) { … }
std::unique_ptr<SharedImageRepresentationFactoryRef>
SharedImageManager::AddSecondaryReference(const Mailbox& mailbox,
MemoryTypeTracker* tracker) { … }
std::unique_ptr<GLTextureImageRepresentation>
SharedImageManager::ProduceGLTexture(const Mailbox& mailbox,
MemoryTypeTracker* tracker) { … }
std::unique_ptr<GLTexturePassthroughImageRepresentation>
SharedImageManager::ProduceGLTexturePassthrough(const Mailbox& mailbox,
MemoryTypeTracker* tracker) { … }
std::unique_ptr<SkiaImageRepresentation> SharedImageManager::ProduceSkia(
const Mailbox& mailbox,
MemoryTypeTracker* tracker,
scoped_refptr<SharedContextState> context_state) { … }
std::unique_ptr<DawnImageRepresentation> SharedImageManager::ProduceDawn(
const Mailbox& mailbox,
MemoryTypeTracker* tracker,
const wgpu::Device& device,
wgpu::BackendType backend_type,
std::vector<wgpu::TextureFormat> view_formats,
scoped_refptr<SharedContextState> context_state) { … }
std::unique_ptr<OverlayImageRepresentation> SharedImageManager::ProduceOverlay(
const gpu::Mailbox& mailbox,
gpu::MemoryTypeTracker* tracker) { … }
std::unique_ptr<MemoryImageRepresentation> SharedImageManager::ProduceMemory(
const Mailbox& mailbox,
MemoryTypeTracker* tracker) { … }
std::unique_ptr<RasterImageRepresentation> SharedImageManager::ProduceRaster(
const Mailbox& mailbox,
MemoryTypeTracker* tracker) { … }
std::unique_ptr<VideoImageRepresentation> SharedImageManager::ProduceVideo(
VideoDevice device,
const Mailbox& mailbox,
MemoryTypeTracker* tracker) { … }
#if BUILDFLAG(ENABLE_VULKAN)
std::unique_ptr<VulkanImageRepresentation> SharedImageManager::ProduceVulkan(
const Mailbox& mailbox,
MemoryTypeTracker* tracker,
gpu::VulkanDeviceQueue* vulkan_device_queue,
gpu::VulkanImplementation& vulkan_impl,
bool needs_detiling) { … }
#endif
#if BUILDFLAG(IS_ANDROID)
std::unique_ptr<LegacyOverlayImageRepresentation>
SharedImageManager::ProduceLegacyOverlay(const Mailbox& mailbox,
MemoryTypeTracker* tracker) {
CALLED_ON_VALID_THREAD();
AutoLock autolock(this);
auto found = images_.find(mailbox);
if (found == images_.end()) {
LOG(ERROR)
<< "SharedImageManager::ProduceLegacyOverlay: Trying to Produce a "
"Legacy Overlay representation from a non-existent mailbox.";
return nullptr;
}
EnforceSharedImageUsage(**found, {SHARED_IMAGE_USAGE_SCANOUT});
auto representation = (*found)->ProduceLegacyOverlay(this, tracker);
if (!representation) {
LOG(ERROR)
<< "SharedImageManager::ProduceLegacyOverlay: Trying to produce a "
"Legacy Overlay representation from an incompatible backing: "
<< (*found)->GetName();
return nullptr;
}
return representation;
}
#endif
#if BUILDFLAG(IS_WIN)
void SharedImageManager::UpdateExternalFence(
const Mailbox& mailbox,
scoped_refptr<gfx::D3DSharedFence> external_fence) {
CALLED_ON_VALID_THREAD();
AutoLock autolock(this);
auto found = images_.find(mailbox);
if (found == images_.end()) {
LOG(ERROR)
<< "SharedImageManager::ProduceVideoDecode: Trying to Produce a D3D"
"representation from a non-existent mailbox.";
return;
}
(*found)->UpdateExternalFence(std::move(external_fence));
}
#endif
std::optional<SharedImageUsageSet> SharedImageManager::GetUsageForMailbox(
const Mailbox& mailbox) { … }
void SharedImageManager::OnRepresentationDestroyed(
const Mailbox& mailbox,
SharedImageRepresentation* representation) { … }
void SharedImageManager::SetPurgeable(const Mailbox& mailbox, bool purgeable) { … }
bool SharedImageManager::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) { … }
scoped_refptr<gfx::NativePixmap> SharedImageManager::GetNativePixmap(
const gpu::Mailbox& mailbox) { … }
bool SharedImageManager::SupportsScanoutImages() { … }
}