• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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/winscope/shell_transitions_parser.h"
18 #include "src/trace_processor/importers/proto/winscope/shell_transitions_tracker.h"
19 
20 #include "perfetto/ext/base/base64.h"
21 #include "protos/perfetto/trace/android/shell_transition.pbzero.h"
22 #include "src/trace_processor/importers/common/args_tracker.h"
23 #include "src/trace_processor/importers/proto/args_parser.h"
24 #include "src/trace_processor/storage/trace_storage.h"
25 #include "src/trace_processor/types/trace_processor_context.h"
26 #include "src/trace_processor/util/winscope_proto_mapping.h"
27 
28 namespace perfetto {
29 namespace trace_processor {
30 
ShellTransitionsParser(TraceProcessorContext * context)31 ShellTransitionsParser::ShellTransitionsParser(TraceProcessorContext* context)
32     : context_(context), args_parser_{*context->descriptor_pool_} {}
33 
ParseTransition(protozero::ConstBytes blob)34 void ShellTransitionsParser::ParseTransition(protozero::ConstBytes blob) {
35   protos::pbzero::ShellTransition::Decoder transition(blob);
36 
37   auto row_id =
38       ShellTransitionsTracker::GetOrCreate(context_)->InternTransition(
39           transition.id());
40 
41   auto* window_manager_shell_transitions_table =
42       context_->storage->mutable_window_manager_shell_transitions_table();
43   auto row = window_manager_shell_transitions_table->FindById(row_id).value();
44 
45   if (transition.has_dispatch_time_ns()) {
46     row.set_ts(transition.dispatch_time_ns());
47   }
48 
49   tables::WindowManagerShellTransitionProtosTable::Row protos;
50   protos.transition_id = transition.id();
51   protos.base64_proto_id = context_->storage->mutable_string_pool()
52                                ->InternString(base::StringView(
53                                    base::Base64Encode(blob.data, blob.size)))
54                                .raw_id();
55   context_->storage->mutable_window_manager_shell_transition_protos_table()
56       ->Insert(protos);
57 
58   auto inserter = context_->args_tracker->AddArgsTo(row_id);
59   ArgsParser writer(/*timestamp=*/0, inserter, *context_->storage.get());
60   base::Status status = args_parser_.ParseMessage(
61       blob,
62       *util::winscope_proto_mapping::GetProtoName(
63           tables::WindowManagerShellTransitionProtosTable::Name()),
64       nullptr /* parse all fields */, writer);
65 
66   if (!status.ok()) {
67     context_->storage->IncrementStats(
68         stats::winscope_shell_transitions_parse_errors);
69   }
70 }
71 
ParseHandlerMappings(protozero::ConstBytes blob)72 void ShellTransitionsParser::ParseHandlerMappings(protozero::ConstBytes blob) {
73   auto* shell_handlers_table =
74       context_->storage
75           ->mutable_window_manager_shell_transition_handlers_table();
76 
77   protos::pbzero::ShellHandlerMappings::Decoder handler_mappings(blob);
78   for (auto it = handler_mappings.mapping(); it; ++it) {
79     protos::pbzero::ShellHandlerMapping::Decoder mapping(it.field().as_bytes());
80 
81     tables::WindowManagerShellTransitionHandlersTable::Row row;
82     row.handler_id = mapping.id();
83     row.handler_name = context_->storage->InternString(
84         base::StringView(mapping.name().ToStdString()));
85     row.base64_proto_id = context_->storage->mutable_string_pool()
86                               ->InternString(base::StringView(
87                                   base::Base64Encode(blob.data, blob.size)))
88                               .raw_id();
89     shell_handlers_table->Insert(row);
90   }
91 }
92 
93 }  // namespace trace_processor
94 }  // namespace perfetto
95