Skip to content

Commit

Permalink
media/mojo/mojoVEAService: Add TRACE_EVENTs
Browse files Browse the repository at this point in the history
This adds TRACE_EVENTs to MojoVideoEncodeAcceleratorService.

Bug: b:266774158
Test: ui.perfetto.dev on trogdor
Change-Id: I4e2a0b951db69dc1d92a4861c439f15a5821e8e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4198595
Commit-Queue: Hirokazu Honda <hiroh@chromium.org>
Reviewed-by: Eugene Zemtsov <eugene@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1098459}
  • Loading branch information
Hirokazu Honda authored and Chromium LUCI CQ committed Jan 30, 2023
1 parent 5be106f commit 180ca11
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 17 deletions.
7 changes: 0 additions & 7 deletions media/base/decoder_status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ const std::string GetDecodeStatusString(const DecoderStatus& status) {

} // namespace

// static
bool ScopedDecodeTrace::IsEnabled() {
bool enable_decode_traces = false;
TRACE_EVENT_CATEGORY_GROUP_ENABLED("media", &enable_decode_traces);
return enable_decode_traces;
}

ScopedDecodeTrace::ScopedDecodeTrace(const char* trace_name,
bool is_key_frame,
base::TimeDelta timestamp)
Expand Down
4 changes: 0 additions & 4 deletions media/base/decoder_status.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ using DecoderStatus = TypedStatus<DecoderStatusTraits>;
// owns the class it's guaranteed to be unique.
class MEDIA_EXPORT ScopedDecodeTrace {
public:
// Returns true if tracing is enabled for the media category. If false,
// clients should avoid creating ScopedDecodeTrace objects.
static bool IsEnabled();

// Begins an asynchronous trace with the given name and properties. Providing
// the DecoderBuffer itself yields the most information in the trace.
ScopedDecodeTrace(const char* trace_name, const DecoderBuffer& buffer);
Expand Down
7 changes: 7 additions & 0 deletions media/base/media_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "media/base/media_util.h"

#include "base/trace_event/trace_event.h"

namespace media {

std::vector<uint8_t> EmptyExtraData() {
Expand All @@ -28,4 +30,9 @@ AudioParameters::Format ConvertAudioCodecToBitstreamFormat(AudioCodec codec) {
}
}

bool MediaTraceIsEnabled() {
bool enable_decode_traces = false;
TRACE_EVENT_CATEGORY_GROUP_ENABLED("media", &enable_decode_traces);
return enable_decode_traces;
}
} // namespace media
2 changes: 2 additions & 0 deletions media/base/media_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class MEDIA_EXPORT NullMediaLog : public media::MediaLog {
MEDIA_EXPORT AudioParameters::Format ConvertAudioCodecToBitstreamFormat(
media::AudioCodec codec);

// Returns true if tracing is enabled for the media category.
MEDIA_EXPORT bool MediaTraceIsEnabled();
} // namespace media

#endif // MEDIA_BASE_MEDIA_UTIL_H_
3 changes: 2 additions & 1 deletion media/filters/decoder_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "media/base/limits.h"
#include "media/base/media_log.h"
#include "media/base/media_switches.h"
#include "media/base/media_util.h"
#include "media/base/timestamp_constants.h"
#include "media/base/video_decoder.h"
#include "media/base/video_frame.h"
Expand Down Expand Up @@ -512,7 +513,7 @@ void DecoderStream<StreamType>::DecodeInternal(
DCHECK(buffer);

std::unique_ptr<ScopedDecodeTrace> trace_event;
if (ScopedDecodeTrace::IsEnabled()) {
if (MediaTraceIsEnabled()) {
// Because multiple Decode() calls may be in flight, each call needs a
// unique trace event class to identify it. This scoped event is bound
// into the OnDecodeDone callback to ensure the trace is always closed.
Expand Down
3 changes: 2 additions & 1 deletion media/mojo/services/mojo_video_decoder_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/task/single_thread_task_runner.h"
#include "base/types/optional_util.h"
#include "media/base/decoder_buffer.h"
#include "media/base/media_util.h"
#include "media/base/simple_sync_token_client.h"
#include "media/base/video_decoder.h"
#include "media/base/video_decoder_config.h"
Expand Down Expand Up @@ -282,7 +283,7 @@ void MojoVideoDecoderService::Decode(mojom::DecoderBufferPtr buffer,
DCHECK(callback);

std::unique_ptr<ScopedDecodeTrace> trace_event;
if (ScopedDecodeTrace::IsEnabled()) {
if (MediaTraceIsEnabled()) {
// Because multiple Decode() calls may be in flight, each call needs a
// unique trace event class to identify it. This scoped event is bound
// into the OnDecodeDone callback to ensure the trace is always closed.
Expand Down
54 changes: 51 additions & 3 deletions media/mojo/services/mojo_video_encode_accelerator_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

#include "base/logging.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "media/base/bind_to_current_loop.h"
#include "media/base/bitstream_buffer.h"
#include "media/base/limits.h"
#include "media/base/media_util.h"
#include "media/mojo/mojom/video_encoder_info.mojom.h"
#include "media/mojo/services/mojo_media_log.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
Expand Down Expand Up @@ -42,7 +44,8 @@ MojoVideoEncodeAcceleratorService::MojoVideoEncodeAcceleratorService(
gpu_preferences_(gpu_preferences),
gpu_workarounds_(gpu_workarounds),
gpu_device_(gpu_device),
output_buffer_size_(0) {
output_buffer_size_(0),
timestamps_(128) {
DVLOG(1) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
Expand All @@ -63,6 +66,8 @@ void MojoVideoEncodeAcceleratorService::Initialize(
config.input_format == PIXEL_FORMAT_NV12)
<< "Only I420 or NV12 format supported, got "
<< VideoPixelFormatToString(config.input_format);
TRACE_EVENT1("media", "MojoVideoEncodeAcceleratorService::Initialize",
"config", config.AsHumanReadableString());

scoped_refptr<base::SingleThreadTaskRunner> task_runner =
base::SingleThreadTaskRunner::GetCurrentDefault();
Expand Down Expand Up @@ -140,6 +145,9 @@ void MojoVideoEncodeAcceleratorService::Encode(
EncodeCallback callback) {
DVLOG(2) << __func__ << " tstamp=" << frame->timestamp();
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
TRACE_EVENT2("media", "MojoVideoEncodeAcceleratorService::Encode",
"timestamp", frame->timestamp().InMilliseconds(), "keyframe",
force_keyframe);
if (!encoder_) {
DLOG(ERROR) << __func__ << " Failed to encode, the encoder is invalid";
std::move(callback).Run();
Expand All @@ -156,6 +164,11 @@ void MojoVideoEncodeAcceleratorService::Encode(
return;
}

if (MediaTraceIsEnabled()) {
timestamps_.Put(frame->timestamp().InMilliseconds(),
base::TimeTicks::Now());
}

frame->AddDestructionObserver(media::BindToCurrentLoop(std::move(callback)));
encoder_->Encode(frame, force_keyframe);
}
Expand All @@ -165,7 +178,9 @@ void MojoVideoEncodeAcceleratorService::UseOutputBitstreamBuffer(
base::UnsafeSharedMemoryRegion region) {
DVLOG(2) << __func__ << " bitstream_buffer_id=" << bitstream_buffer_id;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

TRACE_EVENT1("media",
"MojoVideoEncodeAcceleratorService::UseOutputBitstreamBuffer",
"id", bitstream_buffer_id);
if (!encoder_)
return;
if (!region.IsValid()) {
Expand Down Expand Up @@ -198,6 +213,11 @@ void MojoVideoEncodeAcceleratorService::
const media::VideoBitrateAllocation& bitrate_allocation,
uint32_t framerate) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
TRACE_EVENT2("media",
"MojoVideoEncodeAcceleratorService::"
"RequestEncodingParametersChangeWithLayers",
"bitrate_allocation", bitrate_allocation.ToString(), "framerate",
framerate);

if (!encoder_)
return;
Expand All @@ -212,7 +232,10 @@ void MojoVideoEncodeAcceleratorService::
RequestEncodingParametersChangeWithBitrate(const media::Bitrate& bitrate,
uint32_t framerate) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

TRACE_EVENT2("media",
"MojoVideoEncodeAcceleratorService::"
"RequestEncodingParametersChangeWithBitrate",
"bitrate", bitrate.ToString(), "framerate", framerate);
if (!encoder_)
return;

Expand Down Expand Up @@ -259,6 +282,11 @@ void MojoVideoEncodeAcceleratorService::RequireBitstreamBuffers(
<< " input_coded_size=" << input_coded_size.ToString()
<< " output_buffer_size=" << output_buffer_size;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
TRACE_EVENT2("media",
"MojoVideoEncodeAcceleratorService::RequireBitstreamBuffers",
"input_coded_size", input_coded_size.ToString(),
"output_buffer_size", output_buffer_size);

if (!vea_client_)
return;

Expand All @@ -276,6 +304,26 @@ void MojoVideoEncodeAcceleratorService::BitstreamBufferReady(
<< ", payload_size=" << metadata.payload_size_bytes
<< "B, key_frame=" << metadata.key_frame;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
TRACE_EVENT1("media",
"MojoVideoEncodeAcceleratorService::BitstreamBufferReady",
"bitstream_buffer_id", bitstream_buffer_id);
if (MediaTraceIsEnabled() &&
((!metadata.vp9 && !metadata.av1) ||
(metadata.vp9 && metadata.vp9->end_of_picture) ||
(metadata.av1 && metadata.av1->end_of_picture))) {
const auto timestamp_it =
timestamps_.Peek(metadata.timestamp.InMilliseconds());
if (timestamp_it != timestamps_.end()) {
const int64_t timestamp = metadata.timestamp.InMilliseconds();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
"media", "MojoVEAServicee::EncodingFrameDuration",
TRACE_ID_LOCAL(this), timestamp_it->second);
TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP1(
"media", "MojoVEAServicee::EncodingFrameDuration",
TRACE_ID_LOCAL(this), base::TimeTicks::Now(), "timestamp", timestamp);
}
}

if (!vea_client_)
return;

Expand Down
4 changes: 4 additions & 0 deletions media/mojo/services/mojo_video_encode_accelerator_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
#include <vector>

#include "base/compiler_specific.h"
#include "base/containers/lru_cache.h"
#include "base/memory/raw_ref.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "gpu/config/gpu_driver_bug_workarounds.h"
#include "gpu/config/gpu_info.h"
#include "media/mojo/mojom/video_encode_accelerator.mojom.h"
Expand Down Expand Up @@ -119,6 +121,8 @@ class MEDIA_MOJO_EXPORT MojoVideoEncodeAcceleratorService
size_t output_buffer_size_;
gfx::Size input_coded_size_;

base::LRUCache<int64_t, base::TimeTicks> timestamps_;

// Note that this class is already thread hostile when bound.
SEQUENCE_CHECKER(sequence_checker_);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "base/trace_event/common/trace_event_common.h"
#include "base/trace_event/trace_event.h"
#include "media/base/decoder_status.h"
#include "media/base/media_util.h"
#include "media/media_buildflags.h"
#include "media/video/gpu_video_accelerator_factories.h"
#include "third_party/blink/public/common/features.h"
Expand Down Expand Up @@ -421,7 +422,7 @@ bool DecoderTemplate<Traits>::ProcessDecodeRequest(Request* request) {
--num_pending_decodes_;
ScheduleDequeueEvent();

if (media::ScopedDecodeTrace::IsEnabled()) {
if (media::MediaTraceIsEnabled()) {
request->decode_trace = std::make_unique<media::ScopedDecodeTrace>(
GetTraceNames()->decode.c_str(), *request->decoder_buffer);
}
Expand Down

0 comments on commit 180ca11

Please sign in to comment.