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