• 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/surfaceflinger_layers_parser.h"
18 
19 #include "perfetto/ext/base/base64.h"
20 #include "protos/perfetto/trace/android/surfaceflinger_layers.pbzero.h"
21 #include "src/trace_processor/importers/common/args_tracker.h"
22 #include "src/trace_processor/importers/proto/args_parser.h"
23 #include "src/trace_processor/types/trace_processor_context.h"
24 #include "src/trace_processor/util/winscope_proto_mapping.h"
25 
26 namespace perfetto {
27 namespace trace_processor {
28 
SurfaceFlingerLayersParser(TraceProcessorContext * context)29 SurfaceFlingerLayersParser::SurfaceFlingerLayersParser(
30     TraceProcessorContext* context)
31     : context_{context}, args_parser_{*context->descriptor_pool_} {}
32 
Parse(int64_t timestamp,protozero::ConstBytes blob)33 void SurfaceFlingerLayersParser::Parse(int64_t timestamp,
34                                        protozero::ConstBytes blob) {
35   protos::pbzero::LayersSnapshotProto::Decoder snapshot_decoder(blob);
36   tables::SurfaceFlingerLayersSnapshotTable::Row snapshot;
37   snapshot.ts = timestamp;
38   snapshot.base64_proto_id = context_->storage->mutable_string_pool()
39                                  ->InternString(base::StringView(
40                                      base::Base64Encode(blob.data, blob.size)))
41                                  .raw_id();
42   auto snapshot_id =
43       context_->storage->mutable_surfaceflinger_layers_snapshot_table()
44           ->Insert(snapshot)
45           .id;
46 
47   auto inserter = context_->args_tracker->AddArgsTo(snapshot_id);
48   ArgsParser writer(timestamp, inserter, *context_->storage);
49   const auto table_name = tables::SurfaceFlingerLayersSnapshotTable::Name();
50   auto allowed_fields =
51       util::winscope_proto_mapping::GetAllowedFields(table_name);
52   base::Status status = args_parser_.ParseMessage(
53       blob, *util::winscope_proto_mapping::GetProtoName(table_name),
54       &allowed_fields.value(), writer);
55   if (!status.ok()) {
56     context_->storage->IncrementStats(stats::winscope_sf_layers_parse_errors);
57   }
58 
59   protos::pbzero::LayersProto::Decoder layers_decoder(
60       snapshot_decoder.layers());
61   for (auto it = layers_decoder.layers(); it; ++it) {
62     ParseLayer(timestamp, *it, snapshot_id);
63   }
64 }
65 
ParseLayer(int64_t timestamp,protozero::ConstBytes blob,tables::SurfaceFlingerLayersSnapshotTable::Id snapshot_id)66 void SurfaceFlingerLayersParser::ParseLayer(
67     int64_t timestamp,
68     protozero::ConstBytes blob,
69     tables::SurfaceFlingerLayersSnapshotTable::Id snapshot_id) {
70   tables::SurfaceFlingerLayerTable::Row layer;
71   layer.snapshot_id = snapshot_id;
72   layer.base64_proto_id = context_->storage->mutable_string_pool()
73                               ->InternString(base::StringView(
74                                   base::Base64Encode(blob.data, blob.size)))
75                               .raw_id();
76   auto layerId =
77       context_->storage->mutable_surfaceflinger_layer_table()->Insert(layer).id;
78 
79   ArgsTracker tracker(context_);
80   auto inserter = tracker.AddArgsTo(layerId);
81   ArgsParser writer(timestamp, inserter, *context_->storage);
82   base::Status status =
83       args_parser_.ParseMessage(blob,
84                                 *util::winscope_proto_mapping::GetProtoName(
85                                     tables::SurfaceFlingerLayerTable::Name()),
86                                 nullptr /* parse all fields */, writer);
87   if (!status.ok()) {
88     context_->storage->IncrementStats(stats::winscope_sf_layers_parse_errors);
89   }
90 }
91 
92 }  // namespace trace_processor
93 }  // namespace perfetto
94