Skip to content

Commit

Permalink
VideoFrameYUVConverter: Clearly separate non-OOP-R path
Browse files Browse the repository at this point in the history
VideoFrameYUVConverter will either use the GrContext (non-OOP-R) or the
RasterInterface (OOP-R). Many parameters are only used in the GrContext
path. Make this distinction clear.

In a future patch, we will need to specify the destination texture's
color space (but only in the non-OOP-R path).

Bug: 1348566
Change-Id: Ib2f8a765583ef67c08f2084ae8ce78cd1e798bbd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3804799
Reviewed-by: Dale Curtis <dalecurtis@chromium.org>
Commit-Queue: ccameron chromium <ccameron@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1030823}
  • Loading branch information
ccameron-chromium authored and Chromium LUCI CQ committed Aug 3, 2022
1 parent 2a3dd75 commit 1a4be8f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 102 deletions.
9 changes: 7 additions & 2 deletions media/renderers/paint_canvas_video_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1509,9 +1509,14 @@ bool PaintCanvasVideoRenderer::UploadVideoFrameToGLTexture(
destination_gl->GenUnverifiedSyncTokenCHROMIUM(
mailbox_holder.sync_token.GetData());

if (!VideoFrameYUVConverter::ConvertYUVVideoFrameToDstTextureNoCaching(
VideoFrameYUVConverter::GrParams yuv_gr_params;
yuv_gr_params.internal_format = internal_format;
yuv_gr_params.type = type;
yuv_gr_params.flip_y = flip_y;
yuv_gr_params.use_visible_rect = true;
if (!VideoFrameYUVConverter::ConvertYUVVideoFrameNoCaching(
video_frame.get(), raster_context_provider, mailbox_holder,
internal_format, type, flip_y, true /* use visible_rect */)) {
yuv_gr_params)) {
return false;
}

Expand Down
106 changes: 40 additions & 66 deletions media/renderers/video_frame_yuv_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,18 @@ bool VideoFrameYUVConverter::IsVideoFrameFormatSupported(
bool VideoFrameYUVConverter::ConvertYUVVideoFrameNoCaching(
const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
const gpu::MailboxHolder& dest_mailbox_holder) {
const gpu::MailboxHolder& dest_mailbox_holder,
absl::optional<GrParams> gr_params) {
VideoFrameYUVConverter converter;
return converter.ConvertYUVVideoFrame(video_frame, raster_context_provider,
dest_mailbox_holder);
dest_mailbox_holder, gr_params);
}

bool VideoFrameYUVConverter::ConvertYUVVideoFrame(
const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
const gpu::MailboxHolder& dest_mailbox_holder,
unsigned int internal_format,
unsigned int type,
bool flip_y,
bool use_visible_rect) {
absl::optional<GrParams> gr_params) {
DCHECK(video_frame);
DCHECK(IsVideoFrameFormatSupported(*video_frame))
<< "VideoFrame has an unsupported YUV format " << video_frame->format();
Expand All @@ -130,10 +128,15 @@ bool VideoFrameYUVConverter::ConvertYUVVideoFrame(
holder_ = std::make_unique<VideoFrameYUVMailboxesHolder>();

if (raster_context_provider->GrContext()) {
// Only SW VideoFrame direct uploading path use SkPixmap.
return ConvertFromVideoFrameYUVWithGrContext(
video_frame, raster_context_provider, dest_mailbox_holder,
internal_format, type, flip_y, use_visible_rect);
gr_params.value_or(GrParams()));
}

// The RasterInterface path does not support flip_y or use_visible_rect.
if (gr_params) {
DCHECK(!gr_params->flip_y);
DCHECK(!gr_params->use_visible_rect);
}

auto* ri = raster_context_provider->RasterInterface();
Expand All @@ -150,20 +153,6 @@ bool VideoFrameYUVConverter::ConvertYUVVideoFrame(
return true;
}

bool VideoFrameYUVConverter::ConvertYUVVideoFrameToDstTextureNoCaching(
const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
const gpu::MailboxHolder& dest_mailbox_holder,
unsigned int internal_format,
unsigned int type,
bool flip_y,
bool use_visible_rect) {
VideoFrameYUVConverter converter;
return converter.ConvertYUVVideoFrame(video_frame, raster_context_provider,
dest_mailbox_holder, internal_format,
type, flip_y, use_visible_rect);
}

void VideoFrameYUVConverter::ReleaseCachedData() {
holder_.reset();
}
Expand All @@ -172,10 +161,7 @@ bool VideoFrameYUVConverter::ConvertFromVideoFrameYUVWithGrContext(
const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
const gpu::MailboxHolder& dest_mailbox_holder,
unsigned int internal_format,
unsigned int type,
bool flip_y,
bool use_visible_rect) {
const GrParams& gr_params) {
gpu::raster::RasterInterface* ri = raster_context_provider->RasterInterface();
DCHECK(ri);
ri->WaitSyncTokenCHROMIUM(dest_mailbox_holder.sync_token.GetConstData());
Expand All @@ -186,26 +172,6 @@ bool VideoFrameYUVConverter::ConvertFromVideoFrameYUVWithGrContext(
dest_tex_id, GL_SHARED_IMAGE_ACCESS_MODE_READWRITE_CHROMIUM);
}

bool result = ConvertFromVideoFrameYUVSkia(
video_frame, raster_context_provider, dest_mailbox_holder.texture_target,
dest_tex_id, internal_format, type, flip_y, use_visible_rect);

if (dest_mailbox_holder.mailbox.IsSharedImage())
ri->EndSharedImageAccessDirectCHROMIUM(dest_tex_id);
ri->DeleteGpuRasterTexture(dest_tex_id);

return result;
}

bool VideoFrameYUVConverter::ConvertFromVideoFrameYUVSkia(
const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
unsigned int texture_target,
unsigned int texture_id,
unsigned int internal_format,
unsigned int type,
bool flip_y,
bool use_visible_rect) {
// Rendering YUV textures to SkSurface by dst texture
GrDirectContext* gr_context = raster_context_provider->GrContext();
DCHECK(gr_context);
Expand All @@ -215,40 +181,48 @@ bool VideoFrameYUVConverter::ConvertFromVideoFrameYUVSkia(

// Create SkSurface with dst texture.
GrGLTextureInfo result_gl_texture_info{};
result_gl_texture_info.fID = texture_id;
result_gl_texture_info.fTarget = texture_target;
result_gl_texture_info.fFormat = GetSurfaceColorFormat(internal_format, type);

int result_width = use_visible_rect ? video_frame->visible_rect().width()
: video_frame->coded_size().width();
int result_height = use_visible_rect ? video_frame->visible_rect().height()
: video_frame->coded_size().height();
result_gl_texture_info.fID = dest_tex_id;
result_gl_texture_info.fTarget = dest_mailbox_holder.texture_target;
result_gl_texture_info.fFormat =
GetSurfaceColorFormat(gr_params.internal_format, gr_params.type);

int result_width = gr_params.use_visible_rect
? video_frame->visible_rect().width()
: video_frame->coded_size().width();
int result_height = gr_params.use_visible_rect
? video_frame->visible_rect().height()
: video_frame->coded_size().height();

GrBackendTexture result_texture(result_width, result_height, GrMipMapped::kNo,
result_gl_texture_info);

// Use dst texture as SkSurface back resource.
auto surface = SkSurface::MakeFromBackendTexture(
gr_context, result_texture,
flip_y ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin, 1,
GetCompatibleSurfaceColorType(result_gl_texture_info.fFormat),
gr_params.flip_y ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin,
1, GetCompatibleSurfaceColorType(result_gl_texture_info.fFormat),
SkColorSpace::MakeSRGB(), nullptr);

// Terminate if surface cannot be created.
if (!surface) {
return false;
}

bool result = true;
if (surface) {
auto image =
holder_->VideoFrameToSkImage(video_frame, raster_context_provider);
bool result =
DrawYUVImageToSkSurface(video_frame, image, surface, use_visible_rect);
result = DrawYUVImageToSkSurface(video_frame, image, surface,
gr_params.use_visible_rect);
} else {
result = false;
}

// Release textures to guarantee |holder_| doesn't hold read access on
// textures it doesn't own.
holder_->ReleaseTextures();
// Release textures to guarantee |holder_| doesn't hold read access on
// textures it doesn't own.
holder_->ReleaseTextures();

return result;
if (dest_mailbox_holder.mailbox.IsSharedImage())
ri->EndSharedImageAccessDirectCHROMIUM(dest_tex_id);
ri->DeleteGpuRasterTexture(dest_tex_id);

return result;
}

} // namespace media
50 changes: 16 additions & 34 deletions media/renderers/video_frame_yuv_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,55 +34,37 @@ class VideoFrameYUVMailboxesHolder;
// images.
class MEDIA_EXPORT VideoFrameYUVConverter {
public:
static bool IsVideoFrameFormatSupported(const VideoFrame& video_frame);
// These parameters are only supported by ConvertYUVVideoFrame et al when the
// specified RasterContextProvider also has a GrContext (equivalently, when
// OOP-R is disabled). Isolate them in their own structure, so they can
// eventually be removed once OOP-R is universal.
struct GrParams {
unsigned int internal_format = GL_RGBA;
unsigned int type = GL_UNSIGNED_BYTE;
bool flip_y = false;
bool use_visible_rect = false;
};

VideoFrameYUVConverter();
~VideoFrameYUVConverter();
static bool IsVideoFrameFormatSupported(const VideoFrame& video_frame);
static bool ConvertYUVVideoFrameNoCaching(
const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
const gpu::MailboxHolder& dest_mailbox_holder);

// TODO(crbug.com/1108154): Will merge this uploading path
// with ConvertYUVVideoFrameYUVWithGrContext after solving
// issue 1120911, 1120912
static bool ConvertYUVVideoFrameToDstTextureNoCaching(
const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
const gpu::MailboxHolder& dest_mailbox_holder,
unsigned int internal_format,
unsigned int type,
bool flip_y,
bool use_visible_rect);

VideoFrameYUVConverter();
~VideoFrameYUVConverter();

absl::optional<GrParams> gr_params = absl::nullopt);
bool ConvertYUVVideoFrame(const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
const gpu::MailboxHolder& dest_mailbox_holder,
unsigned int internal_format = GL_RGBA,
unsigned int type = GL_UNSIGNED_BYTE,
bool flip_y = false,
bool use_visible_rect = false);
absl::optional<GrParams> gr_params = absl::nullopt);
void ReleaseCachedData();

private:
bool ConvertFromVideoFrameYUVWithGrContext(
const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
const gpu::MailboxHolder& dest_mailbox_holder,
unsigned int internal_format,
unsigned int type,
bool flip_y,
bool use_visible_rect);
bool ConvertFromVideoFrameYUVSkia(
const VideoFrame* video_frame,
viz::RasterContextProvider* raster_context_provider,
unsigned int texture_target,
unsigned int texture_id,
unsigned int internal_format,
unsigned int type,
bool flip_y,
bool use_visible_rect);
const GrParams& gr_params);

std::unique_ptr<VideoFrameYUVMailboxesHolder> holder_;
};
Expand Down

0 comments on commit 1a4be8f

Please sign in to comment.