1# Copyright (C) 2020 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from google.protobuf import descriptor_pb2 16from google.protobuf import message_factory 17from google.protobuf.descriptor_pool import DescriptorPool 18 19from .loader import get_loader 20 21 22class ProtoFactory: 23 24 def __init__(self): 25 # Declare descriptor pool 26 self.descriptor_pool = DescriptorPool() 27 28 # Load trace processor descriptor and add to descriptor pool 29 tp_descriptor_bytes = get_loader().read_tp_descriptor() 30 tp_file_desc_set_pb2 = descriptor_pb2.FileDescriptorSet() 31 tp_file_desc_set_pb2.MergeFromString(tp_descriptor_bytes) 32 33 for f_desc_pb2 in tp_file_desc_set_pb2.file: 34 self.descriptor_pool.Add(f_desc_pb2) 35 36 # Load metrics descriptor and add to descriptor pool 37 metrics_descriptor_bytes = get_loader().read_metrics_descriptor() 38 metrics_file_desc_set_pb2 = descriptor_pb2.FileDescriptorSet() 39 metrics_file_desc_set_pb2.MergeFromString(metrics_descriptor_bytes) 40 41 for f_desc_pb2 in metrics_file_desc_set_pb2.file: 42 self.descriptor_pool.Add(f_desc_pb2) 43 44 def create_message_factory(message_type): 45 message_desc = self.descriptor_pool.FindMessageTypeByName(message_type) 46 return message_factory.MessageFactory().GetPrototype(message_desc) 47 48 # Create proto messages to correctly communicate with the RPC API by sending 49 # and receiving data as protos 50 self.StatusResult = create_message_factory('perfetto.protos.StatusResult') 51 self.ComputeMetricArgs = create_message_factory( 52 'perfetto.protos.ComputeMetricArgs') 53 self.ComputeMetricResult = create_message_factory( 54 'perfetto.protos.ComputeMetricResult') 55 self.RawQueryArgs = create_message_factory('perfetto.protos.RawQueryArgs') 56 self.QueryResult = create_message_factory('perfetto.protos.QueryResult') 57 self.TraceMetrics = create_message_factory('perfetto.protos.TraceMetrics') 58 self.DisableAndReadMetatraceResult = create_message_factory( 59 'perfetto.protos.DisableAndReadMetatraceResult') 60 self.CellsBatch = create_message_factory( 61 'perfetto.protos.QueryResult.CellsBatch') 62