Commit bf4d01e1 authored by Zhaoliang Ma's avatar Zhaoliang Ma Committed by Commit Bot

Cleanup the method of mapping GpuMemoryBuffer-backed frame

Multiplace need to map GpuMemoryBuffer-backed frame, so this CL
clean up the existing repeated method.

Bug: 1126811
Test: Manually test webrtc and mediarecorder using libvpx encoder
Change-Id: I893d16449ac63c95ce64c392f5299cce1c400752
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2547845
Auto-Submit: Zhaoliang Ma <zhaoliang.ma@intel.com>
Commit-Queue: Guido Urdaneta <guidou@chromium.org>
Reviewed-by: default avatarMarkus Handell <handellm@google.com>
Reviewed-by: default avatarGuido Urdaneta <guidou@chromium.org>
Reviewed-by: default avatarDale Curtis <dalecurtis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#830931}
parent e023da55
......@@ -453,6 +453,7 @@ scoped_refptr<VideoFrame> ConvertToMemoryMappedFrame(
}
mapped_frame->set_color_space(video_frame->ColorSpace());
mapped_frame->metadata()->MergeMetadataFrom(video_frame->metadata());
// Pass |video_frame| so that it outlives |mapped_frame| and the mapped buffer
// is unmapped on destruction.
......
......@@ -508,50 +508,6 @@ VideoTrackRecorderImpl::Encoder::ConvertToI420ForSoftwareEncoder(
return i420_frame;
}
// static
scoped_refptr<media::VideoFrame>
VideoTrackRecorderImpl::Encoder::WrapMappedGpuMemoryBufferVideoFrame(
scoped_refptr<media::VideoFrame> video_frame) {
DCHECK(video_frame);
DCHECK_EQ(video_frame->storage_type(),
media::VideoFrame::StorageType::STORAGE_GPU_MEMORY_BUFFER);
auto* gmb = video_frame->GetGpuMemoryBuffer();
DCHECK(gmb);
if (!gmb->Map()) {
LOG(WARNING) << "Failed to map GpuMemoryBuffer";
return nullptr;
}
const size_t num_planes = media::VideoFrame::NumPlanes(video_frame->format());
uint8_t* plane_addrs[media::VideoFrame::kMaxPlanes] = {};
for (size_t i = 0; i < num_planes; i++)
plane_addrs[i] = static_cast<uint8_t*>(gmb->memory(i));
auto mapped_frame = media::VideoFrame::WrapExternalYuvDataWithLayout(
video_frame->layout(), video_frame->visible_rect(),
video_frame->natural_size(), plane_addrs[0], plane_addrs[1],
plane_addrs[2], video_frame->timestamp());
if (!mapped_frame) {
gmb->Unmap();
return nullptr;
}
mapped_frame->set_color_space(video_frame->ColorSpace());
// Pass |video_frame| so that it outlives |mapped_frame| and the mapped buffer
// is unmapped on destruction.
mapped_frame->AddDestructionObserver(WTF::Bind(
[](scoped_refptr<media::VideoFrame> frame) {
DCHECK(frame->HasGpuMemoryBuffer());
frame->GetGpuMemoryBuffer()->Unmap();
},
std::move(video_frame)));
return mapped_frame;
}
// static
VideoTrackRecorderImpl::CodecId VideoTrackRecorderImpl::GetPreferredCodecId() {
return GetCodecEnumerator()->GetPreferredCodecId();
......
......@@ -203,12 +203,6 @@ class VideoTrackRecorder : public TrackRecorder<MediaStreamVideoSink> {
scoped_refptr<media::VideoFrame> ConvertToI420ForSoftwareEncoder(
scoped_refptr<media::VideoFrame> frame);
// A helper function to map GpuMemoryBuffer-based VideoFrame. This function
// maps the given GpuMemoryBuffer of |frame| as-is without converting pixel
// format. The returned VideoFrame owns the |frame|.
static scoped_refptr<media::VideoFrame> WrapMappedGpuMemoryBufferVideoFrame(
scoped_refptr<media::VideoFrame> frame);
// Used to shutdown properly on the same thread we were created.
const scoped_refptr<base::SequencedTaskRunner> main_task_runner_;
......
......@@ -8,6 +8,7 @@
#include "base/system/sys_info.h"
#include "media/base/video_frame.h"
#include "media/base/video_util.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
......@@ -74,7 +75,7 @@ void VpxEncoder::EncodeOnEncodingTaskRunner(scoped_refptr<VideoFrame> frame,
if (frame->format() == media::PIXEL_FORMAT_NV12 &&
frame->storage_type() == media::VideoFrame::STORAGE_GPU_MEMORY_BUFFER)
frame = WrapMappedGpuMemoryBufferVideoFrame(frame);
frame = media::ConvertToMemoryMappedFrame(frame);
if (!frame) {
LOG(WARNING) << "Invalid video frame to encode";
return;
......
......@@ -7,6 +7,7 @@
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "media/base/video_util.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/modules/webrtc/webrtc_logging.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
......@@ -195,38 +196,6 @@ void IsValidFrame(const media::VideoFrame& frame) {
}
}
scoped_refptr<media::VideoFrame> WrapGmbVideoFrameForMappedMemoryAccess(
scoped_refptr<media::VideoFrame> source_frame) {
DCHECK_EQ(source_frame->natural_size(), source_frame->visible_rect().size());
gfx::GpuMemoryBuffer* gmb = source_frame->GetGpuMemoryBuffer();
if (!gmb || !gmb->Map()) {
return nullptr;
}
// Y and UV planes from the gmb.
uint8_t* plane_addresses[2] = {static_cast<uint8_t*>(gmb->memory(0)),
static_cast<uint8_t*>(gmb->memory(1))};
scoped_refptr<media::VideoFrame> destination_frame =
media::VideoFrame::WrapExternalYuvData(
media::VideoPixelFormat::PIXEL_FORMAT_NV12,
source_frame->coded_size(), source_frame->visible_rect(),
source_frame->natural_size(), gmb->stride(0), gmb->stride(1),
plane_addresses[0], plane_addresses[1], source_frame->timestamp());
if (!destination_frame) {
gmb->Unmap();
LOG(ERROR) << "Failed to wrap gmb buffer";
return nullptr;
}
destination_frame->set_color_space(source_frame->ColorSpace());
destination_frame->metadata()->MergeMetadataFrom(source_frame->metadata());
destination_frame->AddDestructionObserver(WTF::Bind(
[](scoped_refptr<media::VideoFrame> frame) {
CHECK(frame->HasGpuMemoryBuffer());
frame->GetGpuMemoryBuffer()->Unmap();
},
std::move(source_frame)));
return destination_frame;
}
scoped_refptr<media::VideoFrame> MakeScaledI420VideoFrame(
scoped_refptr<media::VideoFrame> source_frame,
scoped_refptr<blink::WebRtcVideoFrameAdapter::BufferPoolOwner>
......@@ -328,7 +297,7 @@ scoped_refptr<media::VideoFrame> ConstructVideoFrameFromGpu(
std::move(scaled_frame_pool));
} else if (source_frame->natural_size() ==
source_frame->visible_rect().size()) {
return WrapGmbVideoFrameForMappedMemoryAccess(std::move(source_frame));
return media::ConvertToMemoryMappedFrame(std::move(source_frame));
} else {
return MakeScaledNV12VideoFrame(std::move(source_frame),
std::move(scaled_frame_pool));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment