#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "gpu/command_buffer/service/shared_image/external_vk_image_backing.h"
#include <utility>
#include <vector>
#include "base/bits.h"
#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/not_fatal_until.h"
#include "build/build_config.h"
#include "components/viz/common/resources/resource_sizes.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/gl_utils.h"
#include "gpu/command_buffer/service/shared_image/external_vk_image_gl_representation.h"
#include "gpu/command_buffer/service/shared_image/external_vk_image_overlay_representation.h"
#include "gpu/command_buffer/service/shared_image/external_vk_image_skia_representation.h"
#include "gpu/command_buffer/service/shared_image/gl_texture_holder.h"
#include "gpu/command_buffer/service/shared_image/shared_image_format_service_utils.h"
#include "gpu/command_buffer/service/shared_image/shared_image_gl_utils.h"
#include "gpu/command_buffer/service/shared_image/skia_gl_image_representation.h"
#include "gpu/command_buffer/service/skia_utils.h"
#include "gpu/config/gpu_finch_features.h"
#include "gpu/ipc/common/vulkan_ycbcr_info.h"
#include "gpu/vulkan/vma_wrapper.h"
#include "gpu/vulkan/vulkan_command_buffer.h"
#include "gpu/vulkan/vulkan_command_pool.h"
#include "gpu/vulkan/vulkan_device_queue.h"
#include "gpu/vulkan/vulkan_fence_helper.h"
#include "gpu/vulkan/vulkan_image.h"
#include "gpu/vulkan/vulkan_implementation.h"
#include "gpu/vulkan/vulkan_util.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#include "third_party/skia/include/core/SkAlphaType.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkPixmap.h"
#include "third_party/skia/include/gpu/GrBackendSemaphore.h"
#include "third_party/skia/include/gpu/GrDirectContext.h"
#include "third_party/skia/include/gpu/GrTypes.h"
#include "third_party/skia/include/gpu/MutableTextureState.h"
#include "third_party/skia/include/gpu/ganesh/vk/GrVkBackendSemaphore.h"
#include "third_party/skia/include/gpu/ganesh/vk/GrVkBackendSurface.h"
#include "third_party/skia/include/gpu/vk/GrVkTypes.h"
#include "third_party/skia/include/gpu/vk/VulkanMutableTextureState.h"
#include "third_party/skia/include/private/chromium/GrPromiseImageTexture.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gl/buildflags.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_utils.h"
#include "ui/gl/gl_version_info.h"
#include "ui/gl/scoped_binders.h"
#if BUILDFLAG(IS_LINUX) && BUILDFLAG(USE_DAWN)
#include "gpu/command_buffer/service/shared_image/external_vk_image_dawn_representation.h"
#if BUILDFLAG(DAWN_ENABLE_BACKEND_OPENGLES)
#include "gpu/command_buffer/service/shared_image/dawn_gl_texture_representation.h"
#endif
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include "gpu/vulkan/fuchsia/vulkan_fuchsia_ext.h"
#endif
#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/surface_factory_ozone.h"
#endif
#define GL_DEDICATED_MEMORY_OBJECT_EXT …
#define GL_TEXTURE_TILING_EXT …
#define GL_TILING_TYPES_EXT …
#define GL_OPTIMAL_TILING_EXT …
#define GL_LINEAR_TILING_EXT …
#define GL_HANDLE_TYPE_OPAQUE_FD_EXT …
#define GL_HANDLE_TYPE_OPAQUE_WIN32_EXT …
#define GL_HANDLE_TYPE_ZIRCON_VMO_ANGLE …
#define GL_HANDLE_TYPE_ZIRCON_EVENT_ANGLE …
namespace gpu {
namespace {
BASE_FEATURE(…);
bool CorrectComputationOfUsagesNeedingColorAttachment() { … }
class ScopedDedicatedMemoryObject { … };
bool UseSeparateGLTexture(SharedContextState* context_state,
viz::SharedImageFormat format) { … }
bool UseMinimalUsageFlags(SharedContextState* context_state) { … }
void WaitSemaphoresOnGrContext(GrDirectContext* gr_context,
std::vector<ExternalSemaphore>* semaphores) { … }
}
std::unique_ptr<ExternalVkImageBacking> ExternalVkImageBacking::Create(
scoped_refptr<SharedContextState> context_state,
VulkanCommandPool* command_pool,
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string debug_label,
const base::flat_map<VkFormat, VkImageUsageFlags>& image_usage_cache,
base::span<const uint8_t> pixel_data) { … }
std::unique_ptr<ExternalVkImageBacking> ExternalVkImageBacking::CreateFromGMB(
scoped_refptr<SharedContextState> context_state,
VulkanCommandPool* command_pool,
const Mailbox& mailbox,
gfx::GpuMemoryBufferHandle handle,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string debug_label,
std::optional<gfx::BufferUsage> buffer_usage) { … }
std::unique_ptr<ExternalVkImageBacking>
ExternalVkImageBacking::CreateWithPixmap(
scoped_refptr<SharedContextState> context_state,
VulkanCommandPool* command_pool,
const Mailbox& mailbox,
viz::SharedImageFormat format,
SurfaceHandle surface_handle,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string debug_label,
gfx::BufferUsage buffer_usage) { … }
ExternalVkImageBacking::ExternalVkImageBacking(
base::PassKey<ExternalVkImageBacking>,
const Mailbox& mailbox,
viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
GrSurfaceOrigin surface_origin,
SkAlphaType alpha_type,
SharedImageUsageSet usage,
std::string debug_label,
size_t estimated_size_bytes,
scoped_refptr<SharedContextState> context_state,
std::vector<TextureHolderVk> vk_textures,
VulkanCommandPool* command_pool,
bool use_separate_gl_texture,
gfx::GpuMemoryBufferHandle handle,
std::optional<gfx::BufferUsage> buffer_usage)
: … { … }
ExternalVkImageBacking::~ExternalVkImageBacking() { … }
std::vector<GLenum> ExternalVkImageBacking::GetVkImageLayoutsForGL() { … }
std::vector<sk_sp<GrPromiseImageTexture>>
ExternalVkImageBacking::GetPromiseTextures() { … }
bool ExternalVkImageBacking::BeginAccess(
bool readonly,
std::vector<ExternalSemaphore>* external_semaphores,
bool is_gl) { … }
void ExternalVkImageBacking::EndAccess(bool readonly,
ExternalSemaphore external_semaphore,
bool is_gl) { … }
SharedImageBackingType ExternalVkImageBacking::GetType() const { … }
void ExternalVkImageBacking::Update(std::unique_ptr<gfx::GpuFence> in_fence) { … }
bool ExternalVkImageBacking::UploadFromMemory(
const std::vector<SkPixmap>& pixmaps) { … }
void ExternalVkImageBacking::AddSemaphoresToPendingListOrRelease(
std::vector<ExternalSemaphore> semaphores) { … }
void ExternalVkImageBacking::ReleaseSemaphoresWithFenceHelper(
std::vector<ExternalSemaphore> semaphores) { … }
scoped_refptr<gfx::NativePixmap> ExternalVkImageBacking::GetNativePixmap() { … }
gfx::GpuMemoryBufferHandle ExternalVkImageBacking::GetGpuMemoryBufferHandle() { … }
void ExternalVkImageBacking::ReturnPendingSemaphoresWithFenceHelper(
std::vector<ExternalSemaphore> semaphores) { … }
std::unique_ptr<DawnImageRepresentation> ExternalVkImageBacking::ProduceDawn(
SharedImageManager* manager,
MemoryTypeTracker* tracker,
const wgpu::Device& wgpuDevice,
wgpu::BackendType backend_type,
std::vector<wgpu::TextureFormat> view_formats,
scoped_refptr<SharedContextState> context_state) { … }
bool ExternalVkImageBacking::MakeGLContextCurrent() { … }
bool ExternalVkImageBacking::ProduceGLTextureInternal(bool is_passthrough) { … }
bool ExternalVkImageBacking::CreateGLTexture(bool is_passthrough,
size_t plane_index) { … }
std::unique_ptr<GLTextureImageRepresentation>
ExternalVkImageBacking::ProduceGLTexture(SharedImageManager* manager,
MemoryTypeTracker* tracker) { … }
std::unique_ptr<GLTexturePassthroughImageRepresentation>
ExternalVkImageBacking::ProduceGLTexturePassthrough(
SharedImageManager* manager,
MemoryTypeTracker* tracker) { … }
std::unique_ptr<SkiaGaneshImageRepresentation>
ExternalVkImageBacking::ProduceSkiaGanesh(
SharedImageManager* manager,
MemoryTypeTracker* tracker,
scoped_refptr<SharedContextState> context_state) { … }
std::unique_ptr<OverlayImageRepresentation>
ExternalVkImageBacking::ProduceOverlay(SharedImageManager* manager,
MemoryTypeTracker* tracker) { … }
void ExternalVkImageBacking::UpdateContent(uint32_t content_flags) { … }
std::pair<std::vector<ExternalVkImageBacking::MapPlaneData>, size_t>
ExternalVkImageBacking::GetMapPlaneData() const { … }
void ExternalVkImageBacking::CopyPixelsFromGLTextureToVkImage() { … }
void ExternalVkImageBacking::CopyPixelsFromVkImageToGLTexture() { … }
bool ExternalVkImageBacking::UploadToVkImage(
const std::vector<SkPixmap>& pixmaps) { … }
bool ExternalVkImageBacking::UploadToGLTexture(
const std::vector<SkPixmap>& pixmaps) { … }
bool ExternalVkImageBacking::BeginAccessInternal(
bool readonly,
std::vector<ExternalSemaphore>* external_semaphores) { … }
void ExternalVkImageBacking::EndAccessInternal(
bool readonly,
ExternalSemaphore external_semaphore) { … }
}