• 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 <cinttypes>
20 #include <cstdint>
21 #include <utility>
22 
23 #include "perfetto/ext/base/string_utils.h"
24 #include "perfetto/protozero/field.h"
25 #include "perfetto/trace_processor/ref_counted.h"
26 #include "src/trace_processor/importers/common/parser_types.h"
27 #include "src/trace_processor/importers/common/slice_tracker.h"
28 #include "src/trace_processor/importers/common/track_compressor.h"
29 #include "src/trace_processor/importers/common/tracks.h"
30 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
31 #include "src/trace_processor/importers/proto/proto_importer_module.h"
32 #include "src/trace_processor/sorter/trace_sorter.h"
33 #include "src/trace_processor/storage/trace_storage.h"
34 
35 #include "protos/perfetto/trace/android/camera_event.pbzero.h"
36 #include "protos/perfetto/trace/trace_packet.pbzero.h"
37 
38 namespace perfetto::trace_processor {
39 
40 using perfetto::protos::pbzero::TracePacket;
41 
AndroidCameraEventModule(TraceProcessorContext * context)42 AndroidCameraEventModule::AndroidCameraEventModule(
43     TraceProcessorContext* context)
44     : context_(context) {
45   RegisterForField(TracePacket::kAndroidCameraFrameEventFieldNumber, context);
46 }
47 
48 AndroidCameraEventModule::~AndroidCameraEventModule() = default;
49 
TokenizePacket(const protos::pbzero::TracePacket::Decoder & decoder,TraceBlobView * packet,int64_t,RefPtr<PacketSequenceStateGeneration> state,uint32_t field_id)50 ModuleResult AndroidCameraEventModule::TokenizePacket(
51     const protos::pbzero::TracePacket::Decoder& decoder,
52     TraceBlobView* packet,
53     int64_t /*packet_timestamp*/,
54     RefPtr<PacketSequenceStateGeneration> state,
55     uint32_t field_id) {
56   if (field_id != TracePacket::kAndroidCameraFrameEventFieldNumber) {
57     return ModuleResult::Ignored();
58   }
59   const auto android_camera_frame_event =
60       protos::pbzero::AndroidCameraFrameEvent::Decoder(
61           decoder.android_camera_frame_event());
62   context_->sorter->PushTracePacket(
63       android_camera_frame_event.request_processing_started_ns(),
64       std::move(state), std::move(*packet), context_->machine_id());
65   return ModuleResult::Handled();
66 }
67 
ParseTracePacketData(const TracePacket::Decoder & decoder,int64_t,const TracePacketData &,uint32_t field_id)68 void AndroidCameraEventModule::ParseTracePacketData(
69     const TracePacket::Decoder& decoder,
70     int64_t /*ts*/,
71     const TracePacketData&,
72     uint32_t field_id) {
73   if (field_id != TracePacket::kAndroidCameraFrameEventFieldNumber) {
74     return;
75   }
76   InsertCameraFrameSlice(decoder.android_camera_frame_event());
77 }
78 
InsertCameraFrameSlice(protozero::ConstBytes bytes)79 void AndroidCameraEventModule::InsertCameraFrameSlice(
80     protozero::ConstBytes bytes) {
81   protos::pbzero::AndroidCameraFrameEvent::Decoder evt(bytes);
82   StringId slice_name = context_->storage->InternString(
83       base::StackString<32>("Frame %" PRId64, evt.frame_number())
84           .string_view());
85   int64_t ts = evt.request_processing_started_ns();
86   int64_t dur =
87       evt.responses_all_sent_ns() - evt.request_processing_started_ns();
88 
89   constexpr auto kBlueprint = TrackCompressor::SliceBlueprint(
90       "android_camera_event",
91       tracks::Dimensions(tracks::UintDimensionBlueprint("android_camera_id")),
92       tracks::FnNameBlueprint([](uint32_t camera_id) {
93         return base::StackString<32>("Camera %u Frames", camera_id);
94       }));
95 
96   TrackId track_id = context_->track_compressor->InternScoped(
97       kBlueprint, tracks::Dimensions(evt.camera_id()), ts, dur);
98   context_->slice_tracker->Scoped(ts, track_id, /*category=*/kNullStringId,
99                                   slice_name, dur);
100 }
101 
102 }  // namespace perfetto::trace_processor
103