#include "media/renderers/video_frame_rgba_to_yuva_converter.h"
#include "base/check.h"
#include "base/logging.h"
#include "components/viz/common/gpu/raster_context_provider.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/client/raster_interface.h"
#include "media/base/simple_sync_token_client.h"
namespace media {
std::optional<gpu::SyncToken> CopyRGBATextureToVideoFrame(
viz::RasterContextProvider* provider,
const gfx::Size& src_size,
scoped_refptr<gpu::ClientSharedImage> src_shared_image,
const gpu::SyncToken& acquire_sync_token,
VideoFrame* dst_video_frame) {
DCHECK_EQ(dst_video_frame->format(), PIXEL_FORMAT_NV12);
CHECK(dst_video_frame->HasSharedImage());
auto* ri = provider->RasterInterface();
DCHECK(ri);
if (ri->GetGraphicsResetStatusKHR() != GL_NO_ERROR) {
DLOG(ERROR) << "Raster context lost.";
return std::nullopt;
}
if (!provider->ContextCapabilities().supports_rgb_to_yuv_conversion) {
DVLOG(1) << "RGB->YUV conversion not supported";
return std::nullopt;
}
std::unique_ptr<gpu::RasterScopedAccess> src_ri_access =
src_shared_image->BeginRasterAccess(ri, acquire_sync_token,
true);
auto dst_sync_token = dst_video_frame->acquire_sync_token();
auto dst_shared_image = dst_video_frame->shared_image();
std::unique_ptr<gpu::RasterScopedAccess> dst_ri_access =
dst_shared_image->BeginRasterAccess(ri, dst_sync_token,
false);
ri->CopySharedImage(src_shared_image->mailbox(), dst_shared_image->mailbox(),
0, 0, 0, 0, src_size.width(), src_size.height());
ri->Flush();
gpu::RasterScopedAccess::EndAccess(std::move(dst_ri_access));
gpu::SyncToken completion_sync_token =
gpu::RasterScopedAccess::EndAccess(std::move(src_ri_access));
SimpleSyncTokenClient simple_client(completion_sync_token);
dst_video_frame->UpdateAcquireSyncToken(completion_sync_token);
dst_video_frame->UpdateReleaseSyncToken(&simple_client);
return completion_sync_token;
}
}