• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "protos/perfetto/trace/android/camera_event.pbzero.h"
21 #include "protos/perfetto/trace/trace_packet.pbzero.h"
22 #include "src/trace_processor/importers/common/async_track_set_tracker.h"
23 #include "src/trace_processor/importers/common/parser_types.h"
24 #include "src/trace_processor/importers/common/slice_tracker.h"
25 #include "src/trace_processor/importers/common/track_tracker.h"
26 #include "src/trace_processor/importers/proto/packet_sequence_state.h"
27 #include "src/trace_processor/sorter/trace_sorter.h"
28 #include "src/trace_processor/storage/trace_storage.h"
29 
30 namespace perfetto {
31 namespace trace_processor {
32 
33 using perfetto::protos::pbzero::TracePacket;
34 
AndroidCameraEventModule(TraceProcessorContext * context)35 AndroidCameraEventModule::AndroidCameraEventModule(
36     TraceProcessorContext* context)
37     : context_(context) {
38   RegisterForField(TracePacket::kAndroidCameraFrameEventFieldNumber, context);
39 }
40 
41 AndroidCameraEventModule::~AndroidCameraEventModule() = default;
42 
TokenizePacket(const protos::pbzero::TracePacket::Decoder & decoder,TraceBlobView * packet,int64_t,PacketSequenceState * state,uint32_t field_id)43 ModuleResult AndroidCameraEventModule::TokenizePacket(
44     const protos::pbzero::TracePacket::Decoder& decoder,
45     TraceBlobView* packet,
46     int64_t /*packet_timestamp*/,
47     PacketSequenceState* state,
48     uint32_t field_id) {
49   if (field_id != TracePacket::kAndroidCameraFrameEventFieldNumber) {
50     return ModuleResult::Ignored();
51   }
52   const auto android_camera_frame_event =
53       protos::pbzero::AndroidCameraFrameEvent::Decoder(
54           decoder.android_camera_frame_event());
55   context_->sorter->PushTracePacket(
56       android_camera_frame_event.request_processing_started_ns(),
57       state->current_generation(), std::move(*packet));
58   return ModuleResult::Handled();
59 }
60 
ParseTracePacketData(const TracePacket::Decoder & decoder,int64_t,const TracePacketData &,uint32_t field_id)61 void AndroidCameraEventModule::ParseTracePacketData(
62     const TracePacket::Decoder& decoder,
63     int64_t /*ts*/,
64     const TracePacketData&,
65     uint32_t field_id) {
66   if (field_id != TracePacket::kAndroidCameraFrameEventFieldNumber) {
67     return;
68   }
69   InsertCameraFrameSlice(decoder.android_camera_frame_event());
70 }
71 
InsertCameraFrameSlice(protozero::ConstBytes bytes)72 void AndroidCameraEventModule::InsertCameraFrameSlice(
73     protozero::ConstBytes bytes) {
74   const auto android_camera_frame_event =
75       protos::pbzero::AndroidCameraFrameEvent::Decoder(bytes);
76   StringId track_name = context_->storage->InternString(
77       base::StackString<32>("Camera %d Frames",
78                             android_camera_frame_event.camera_id())
79           .string_view());
80   StringId slice_name = context_->storage->InternString(
81       base::StackString<32>("Frame %" PRId64,
82                             android_camera_frame_event.frame_number())
83           .string_view());
84   int64_t ts = android_camera_frame_event.request_processing_started_ns();
85   int64_t dur = android_camera_frame_event.responses_all_sent_ns() -
86                 android_camera_frame_event.request_processing_started_ns();
87   auto track_set_id =
88       context_->async_track_set_tracker->InternGlobalTrackSet(track_name);
89   auto track_id =
90       context_->async_track_set_tracker->Scoped(track_set_id, ts, dur);
91   context_->slice_tracker->Scoped(ts, track_id, /*category=*/kNullStringId,
92                                   slice_name, dur);
93 }
94 
95 }  // namespace trace_processor
96 }  // namespace perfetto
97