1 /*
2  * Copyright (C) 2021 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 #pragma once
17 
18 #include <gui/fake/BufferData.h>
19 #include <layerproto/TransactionProto.h>
20 #include <utils/RefBase.h>
21 
22 #include "FrontEnd/DisplayInfo.h"
23 #include "FrontEnd/LayerCreationArgs.h"
24 #include "TransactionState.h"
25 
26 namespace android::surfaceflinger {
27 
28 struct TracingLayerState : ResolvedComposerState {
29     bool hasSidebandStream;
30     LayerCreationArgs args;
31 };
32 
33 class TransactionProtoParser {
34 public:
35     // Utility class to map handles to ids and buffers to buffer properties without pulling
36     // in SurfaceFlinger dependencies.
37     class FlingerDataMapper {
38     public:
39         virtual ~FlingerDataMapper() = default;
getDisplayHandle(int32_t)40         virtual sp<IBinder> getDisplayHandle(int32_t /* displayId */) const { return nullptr; }
getDisplayId(const sp<IBinder> &)41         virtual int32_t getDisplayId(const sp<IBinder>& /* displayHandle */) const { return -1; }
42     };
43 
TransactionProtoParser(std::unique_ptr<FlingerDataMapper> provider)44     TransactionProtoParser(std::unique_ptr<FlingerDataMapper> provider)
45           : mMapper(std::move(provider)) {}
46 
47     perfetto::protos::TransactionState toProto(const TransactionState&);
48     perfetto::protos::TransactionState toProto(
49             const std::map<uint32_t /* layerId */, TracingLayerState>&);
50     perfetto::protos::LayerCreationArgs toProto(const LayerCreationArgs& args);
51     perfetto::protos::LayerState toProto(const ResolvedComposerState&);
52     static perfetto::protos::DisplayInfo toProto(const frontend::DisplayInfo&, uint32_t layerStack);
53 
54     TransactionState fromProto(const perfetto::protos::TransactionState&);
55     void mergeFromProto(const perfetto::protos::LayerState&, TracingLayerState& outState);
56     void fromProto(const perfetto::protos::LayerCreationArgs&, LayerCreationArgs& outArgs);
57     std::unique_ptr<FlingerDataMapper> mMapper;
58     static frontend::DisplayInfo fromProto(const perfetto::protos::DisplayInfo&);
59     static void fromProto(const google::protobuf::RepeatedPtrField<perfetto::protos::DisplayInfo>&,
60                           frontend::DisplayInfos& outDisplayInfos);
61 
62 private:
63     perfetto::protos::DisplayState toProto(const DisplayState&);
64     void fromProto(const perfetto::protos::LayerState&, ResolvedComposerState& out);
65     DisplayState fromProto(const perfetto::protos::DisplayState&);
66 };
67 
68 } // namespace android::surfaceflinger
69