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_transactions_parser.h"
18 
19 #include "perfetto/ext/base/base64.h"
20 #include "protos/perfetto/trace/android/surfaceflinger_transactions.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/storage/trace_storage.h"
24 #include "src/trace_processor/types/trace_processor_context.h"
25 #include "src/trace_processor/util/winscope_proto_mapping.h"
26 
27 namespace perfetto {
28 namespace trace_processor {
29 
SurfaceFlingerTransactionsParser(TraceProcessorContext * context)30 SurfaceFlingerTransactionsParser::SurfaceFlingerTransactionsParser(
31     TraceProcessorContext* context)
32     : context_{context}, args_parser_{*context->descriptor_pool_} {}
33 
Parse(int64_t timestamp,protozero::ConstBytes blob)34 void SurfaceFlingerTransactionsParser::Parse(int64_t timestamp,
35                                              protozero::ConstBytes blob) {
36   tables::SurfaceFlingerTransactionsTable::Row row;
37   row.ts = timestamp;
38   row.base64_proto = context_->storage->mutable_string_pool()->InternString(
39       base::StringView(base::Base64Encode(blob.data, blob.size)));
40   row.base64_proto_id = row.base64_proto.raw_id();
41   auto rowId = context_->storage->mutable_surfaceflinger_transactions_table()
42                    ->Insert(row)
43                    .id;
44 
45   ArgsTracker tracker(context_);
46   auto inserter = tracker.AddArgsTo(rowId);
47   ArgsParser writer(timestamp, inserter, *context_->storage.get());
48   base::Status status = args_parser_.ParseMessage(
49       blob,
50       *util::winscope_proto_mapping::GetProtoName(
51           tables::SurfaceFlingerTransactionsTable::Name()),
52       nullptr /* parse all fields */, writer);
53   if (!status.ok()) {
54     context_->storage->IncrementStats(
55         stats::winscope_sf_transactions_parse_errors);
56   }
57 }
58 
59 }  // namespace trace_processor
60 }  // namespace perfetto
61