#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "media/renderers/video_resource_updater.h"
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <vector>
#include "base/atomic_sequence_num.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/ranges/algorithm.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 "cc/base/math_util.h"
#include "cc/paint/skia_paint_canvas.h"
#include "components/viz/client/client_resource_provider.h"
#include "components/viz/client/shared_bitmap_reporter.h"
#include "components/viz/common/features.h"
#include "components/viz/common/gpu/raster_context_provider.h"
#include "components/viz/common/quads/compositor_render_pass.h"
#include "components/viz/common/quads/texture_draw_quad.h"
#include "components/viz/common/quads/video_hole_draw_quad.h"
#include "components/viz/common/resources/bitmap_allocation.h"
#include "components/viz/common/resources/resource_sizes.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/client/shared_image_interface.h"
#include "gpu/command_buffer/common/shared_image_capabilities.h"
#include "gpu/command_buffer/common/shared_image_trace_utils.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/ipc/client/client_shared_image_interface.h"
#include "media/base/media_switches.h"
#include "media/base/wait_and_replace_sync_token_client.h"
#include "media/renderers/paint_canvas_video_renderer.h"
#include "media/renderers/resource_sync_token_client.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "third_party/khronos/GLES3/gl3.h"
#include "third_party/libyuv/include/libyuv.h"
#include "third_party/skia/include/core/SkYUVAInfo.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/video_types.h"
#include "ui/gl/gl_enums.h"
#include "ui/gl/trace_util.h"
namespace media {
namespace {
BASE_FEATURE(…);
bool MediaSharedBitmapConversionEnabled() { … }
base::AtomicSequenceNumber g_next_video_resource_updater_id;
gfx::ProtectedVideoType ProtectedVideoTypeFromMetadata(
const VideoFrameMetadata& metadata) { … }
VideoFrameResourceType ExternalResourceTypeForHardwarePlanes(
const VideoFrame& frame,
GLuint target,
viz::SharedImageFormat& si_format,
bool use_stream_video_draw_quad) { … }
gfx::Size SoftwareFirstPlaneDimension(VideoFrame* input_frame,
bool software_compositor) { … }
SkYUVAInfo::PlaneConfig ToSkYUVAPlaneConfig(viz::SharedImageFormat format) { … }
SkYUVAInfo::Subsampling ToSkYUVASubsampling(viz::SharedImageFormat format) { … }
viz::SharedImageFormat GetRGBSharedImageFormat(VideoPixelFormat format) { … }
viz::SharedImageFormat GetSingleChannel8BitFormat(
const gpu::Capabilities& caps,
const gpu::SharedImageCapabilities& shared_image_caps) { … }
bool HasCompatibleRGBFormat(VideoPixelFormat input_format,
viz::SharedImageFormat output_format) { … }
bool IsFrameFormat32BitRGB(VideoPixelFormat frame_format) { … }
bool IsFormat16BitFloat(viz::SharedImageFormat format) { … }
viz::SharedImageFormat::ChannelFormat SupportedMultiPlaneChannelFormat(
viz::SharedImageFormat format) { … }
viz::SharedImageFormat VideoPixelFormatToMultiPlanarSharedImageFormat(
VideoPixelFormat input_format) { … }
std::vector<VideoFrame::Plane> GetVideoFramePlanes(
viz::SharedImageFormat format) { … }
class CopyingSyncTokenClient : public VideoFrame::SyncTokenClient { … };
}
VideoFrameExternalResources::VideoFrameExternalResources() = default;
VideoFrameExternalResources::~VideoFrameExternalResources() = default;
VideoFrameExternalResources::VideoFrameExternalResources(
VideoFrameExternalResources&& other) = default;
VideoFrameExternalResources& VideoFrameExternalResources::operator=(
VideoFrameExternalResources&& other) = default;
class VideoResourceUpdater::PlaneResource { … };
class VideoResourceUpdater::SoftwarePlaneResource
: public VideoResourceUpdater::PlaneResource { … };
class VideoResourceUpdater::HardwarePlaneResource
: public VideoResourceUpdater::PlaneResource { … };
VideoResourceUpdater::SoftwarePlaneResource*
VideoResourceUpdater::PlaneResource::AsSoftware() { … }
VideoResourceUpdater::HardwarePlaneResource*
VideoResourceUpdater::PlaneResource::AsHardware() { … }
VideoResourceUpdater::VideoResourceUpdater(
viz::RasterContextProvider* context_provider,
viz::SharedBitmapReporter* shared_bitmap_reporter,
viz::ClientResourceProvider* resource_provider,
scoped_refptr<gpu::ClientSharedImageInterface> shared_image_interface,
bool use_stream_video_draw_quad,
bool use_gpu_memory_buffer_resources,
int max_resource_size)
: … { … }
VideoResourceUpdater::~VideoResourceUpdater() { … }
void VideoResourceUpdater::ObtainFrameResources(
scoped_refptr<VideoFrame> video_frame) { … }
void VideoResourceUpdater::ReleaseFrameResources() { … }
void VideoResourceUpdater::AppendQuads(
viz::CompositorRenderPass* render_pass,
scoped_refptr<VideoFrame> frame,
gfx::Transform transform,
gfx::Rect quad_rect,
gfx::Rect visible_quad_rect,
const gfx::MaskFilterInfo& mask_filter_info,
std::optional<gfx::Rect> clip_rect,
bool contents_opaque,
float draw_opacity,
int sorting_context_id) { … }
VideoFrameExternalResources
VideoResourceUpdater::CreateExternalResourcesFromVideoFrame(
scoped_refptr<VideoFrame> video_frame) { … }
bool VideoResourceUpdater::ReallocateUploadPixels(size_t needed_size,
size_t plane) { … }
VideoResourceUpdater::PlaneResource*
VideoResourceUpdater::RecycleOrAllocateResource(
const gfx::Size& resource_size,
viz::SharedImageFormat si_format,
const gfx::ColorSpace& color_space,
VideoFrame::ID unique_id) { … }
VideoResourceUpdater::PlaneResource* VideoResourceUpdater::AllocateResource(
const gfx::Size& plane_size,
viz::SharedImageFormat format,
const gfx::ColorSpace& color_space) { … }
void VideoResourceUpdater::CopyHardwarePlane(
VideoFrame* video_frame,
const gpu::MailboxHolder& mailbox_holder,
VideoFrameExternalResources* external_resources) { … }
VideoFrameExternalResources VideoResourceUpdater::CreateForHardwarePlanes(
scoped_refptr<VideoFrame> video_frame) { … }
viz::SharedImageFormat VideoResourceUpdater::YuvSharedImageFormat(
int bits_per_channel) { … }
viz::SharedImageFormat VideoResourceUpdater::GetSoftwareOutputFormat(
VideoPixelFormat input_frame_format,
int bits_per_channel,
const gfx::ColorSpace& input_frame_color_space,
bool& texture_needs_rgb_conversion_out) { … }
void VideoResourceUpdater::TransferRGBPixelsToPaintCanvas(
scoped_refptr<VideoFrame> video_frame,
PlaneResource* plane_resource) { … }
bool VideoResourceUpdater::WriteRGBPixelsToTexture(
scoped_refptr<VideoFrame> video_frame,
PlaneResource* plane_resource,
viz::SharedImageFormat output_si_format) { … }
bool VideoResourceUpdater::WriteYUVPixelsForAllPlanesToTexture(
scoped_refptr<VideoFrame> video_frame,
HardwarePlaneResource* resource,
size_t bits_per_channel) { … }
VideoFrameExternalResources VideoResourceUpdater::CreateForSoftwarePlanes(
scoped_refptr<VideoFrame> video_frame) { … }
gpu::raster::RasterInterface* VideoResourceUpdater::RasterInterface() { … }
void VideoResourceUpdater::ReturnTexture(
scoped_refptr<VideoFrame> video_frame,
const gpu::SyncToken& original_release_token,
const gpu::SyncToken& new_release_token,
bool lost_resource) { … }
void VideoResourceUpdater::RecycleResource(uint32_t plane_resource_id,
const gpu::SyncToken& sync_token,
bool lost_resource) { … }
bool VideoResourceUpdater::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) { … }
scoped_refptr<gpu::ClientSharedImageInterface>
VideoResourceUpdater::shared_image_interface() const { … }
VideoResourceUpdater::FrameResource::FrameResource() = default;
VideoResourceUpdater::FrameResource::FrameResource(viz::ResourceId id,
const gfx::Size& size)
: … { … }
}