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.data,
36                                                                 blob.size);
37   tables::SurfaceFlingerLayersSnapshotTable::Row snapshot;
38   snapshot.ts = timestamp;
39   snapshot.base64_proto =
40       context_->storage->mutable_string_pool()->InternString(
41           base::StringView(base::Base64Encode(blob.data, blob.size)));
42   snapshot.base64_proto_id = snapshot.base64_proto.raw_id();
43   auto snapshot_id =
44       context_->storage->mutable_surfaceflinger_layers_snapshot_table()
45           ->Insert(snapshot)
46           .id;
47 
48   auto inserter = context_->args_tracker->AddArgsTo(snapshot_id);
49   ArgsParser writer(timestamp, inserter, *context_->storage);
50   const auto table_name = tables::SurfaceFlingerLayersSnapshotTable::Name();
51   auto allowed_fields =
52       util::winscope_proto_mapping::GetAllowedFields(table_name);
53   base::Status status = args_parser_.ParseMessage(
54       blob, *util::winscope_proto_mapping::GetProtoName(table_name),
55       &allowed_fields.value(), writer);
56   if (!status.ok()) {
57     context_->storage->IncrementStats(stats::winscope_sf_layers_parse_errors);
58   }
59 
60   protos::pbzero::LayersProto::Decoder layers_decoder(
61       snapshot_decoder.layers().data, snapshot_decoder.layers().size);
62   for (auto it = layers_decoder.layers(); it; ++it) {
63     ParseLayer(timestamp, *it, snapshot_id);
64   }
65 }
66 
ParseLayer(int64_t timestamp,protozero::ConstBytes blob,tables::SurfaceFlingerLayersSnapshotTable::Id snapshot_id)67 void SurfaceFlingerLayersParser::ParseLayer(
68     int64_t timestamp,
69     protozero::ConstBytes blob,
70     tables::SurfaceFlingerLayersSnapshotTable::Id snapshot_id) {
71   tables::SurfaceFlingerLayerTable::Row layer;
72   layer.snapshot_id = snapshot_id;
73   layer.base64_proto = context_->storage->mutable_string_pool()->InternString(
74       base::StringView(base::Base64Encode(blob.data, blob.size)));
75   layer.base64_proto_id = layer.base64_proto.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