1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/importers/proto/android_camera_event_module.h"
18
19 #include "perfetto/ext/base/string_utils.h"
20 #include "src/trace_processor/importers/common/async_track_set_tracker.h"
21 #include "src/trace_processor/importers/common/machine_tracker.h"
22 #include "src/trace_processor/importers/common/parser_types.h"
23 #include "src/trace_processor/importers/common/slice_tracker.h"
24 #include "src/trace_processor/importers/common/track_tracker.h"
25 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
26 #include "src/trace_processor/sorter/trace_sorter.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28
29 #include "protos/perfetto/trace/android/camera_event.pbzero.h"
30 #include "protos/perfetto/trace/trace_packet.pbzero.h"
31
32 namespace perfetto {
33 namespace trace_processor {
34
35 using perfetto::protos::pbzero::TracePacket;
36
AndroidCameraEventModule(TraceProcessorContext * context)37 AndroidCameraEventModule::AndroidCameraEventModule(
38 TraceProcessorContext* context)
39 : context_(context) {
40 RegisterForField(TracePacket::kAndroidCameraFrameEventFieldNumber, context);
41 }
42
43 AndroidCameraEventModule::~AndroidCameraEventModule() = default;
44
TokenizePacket(const protos::pbzero::TracePacket::Decoder & decoder,TraceBlobView * packet,int64_t,RefPtr<PacketSequenceStateGeneration> state,uint32_t field_id)45 ModuleResult AndroidCameraEventModule::TokenizePacket(
46 const protos::pbzero::TracePacket::Decoder& decoder,
47 TraceBlobView* packet,
48 int64_t /*packet_timestamp*/,
49 RefPtr<PacketSequenceStateGeneration> state,
50 uint32_t field_id) {
51 if (field_id != TracePacket::kAndroidCameraFrameEventFieldNumber) {
52 return ModuleResult::Ignored();
53 }
54 const auto android_camera_frame_event =
55 protos::pbzero::AndroidCameraFrameEvent::Decoder(
56 decoder.android_camera_frame_event());
57 context_->sorter->PushTracePacket(
58 android_camera_frame_event.request_processing_started_ns(),
59 std::move(state), std::move(*packet), context_->machine_id());
60 return ModuleResult::Handled();
61 }
62
ParseTracePacketData(const TracePacket::Decoder & decoder,int64_t,const TracePacketData &,uint32_t field_id)63 void AndroidCameraEventModule::ParseTracePacketData(
64 const TracePacket::Decoder& decoder,
65 int64_t /*ts*/,
66 const TracePacketData&,
67 uint32_t field_id) {
68 if (field_id != TracePacket::kAndroidCameraFrameEventFieldNumber) {
69 return;
70 }
71 InsertCameraFrameSlice(decoder.android_camera_frame_event());
72 }
73
InsertCameraFrameSlice(protozero::ConstBytes bytes)74 void AndroidCameraEventModule::InsertCameraFrameSlice(
75 protozero::ConstBytes bytes) {
76 const auto android_camera_frame_event =
77 protos::pbzero::AndroidCameraFrameEvent::Decoder(bytes);
78 StringId track_name = context_->storage->InternString(
79 base::StackString<32>("Camera %d Frames",
80 android_camera_frame_event.camera_id())
81 .string_view());
82 StringId slice_name = context_->storage->InternString(
83 base::StackString<32>("Frame %" PRId64,
84 android_camera_frame_event.frame_number())
85 .string_view());
86 int64_t ts = android_camera_frame_event.request_processing_started_ns();
87 int64_t dur = android_camera_frame_event.responses_all_sent_ns() -
88 android_camera_frame_event.request_processing_started_ns();
89 auto track_set_id =
90 context_->async_track_set_tracker->InternGlobalTrackSet(track_name);
91 auto track_id =
92 context_->async_track_set_tracker->Scoped(track_set_id, ts, dur);
93 context_->slice_tracker->Scoped(ts, track_id, /*category=*/kNullStringId,
94 slice_name, dur);
95 }
96
97 } // namespace trace_processor
98 } // namespace perfetto
99