1 /*
2  * Copyright (C) 2017 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 <layerproto/LayerProtoHeader.h>
19 
20 #include <math/vec4.h>
21 
22 #include <memory>
23 #include <unordered_map>
24 #include <vector>
25 
26 namespace android {
27 namespace surfaceflinger {
28 
29 class LayerProtoParser {
30 public:
31     class ActiveBuffer {
32     public:
33         uint32_t width;
34         uint32_t height;
35         uint32_t stride;
36         int32_t format;
37 
38         std::string to_string() const;
39     };
40 
41     class Transform {
42     public:
43         float dsdx;
44         float dtdx;
45         float dsdy;
46         float dtdy;
47 
48         std::string to_string() const;
49     };
50 
51     class Rect {
52     public:
53         int32_t left;
54         int32_t top;
55         int32_t right;
56         int32_t bottom;
57 
58         std::string to_string() const;
59     };
60 
61     class FloatRect {
62     public:
63         float left;
64         float top;
65         float right;
66         float bottom;
67 
68         std::string to_string() const;
69     };
70 
71     class Region {
72     public:
73         uint64_t id;
74         std::vector<Rect> rects;
75 
76         std::string to_string(const char* what) const;
77     };
78 
79     class Layer {
80     public:
81         int32_t id;
82         std::string name;
83         std::vector<std::unique_ptr<Layer>> children;
84         std::vector<Layer*> relatives;
85         std::string type;
86         LayerProtoParser::Region transparentRegion;
87         LayerProtoParser::Region visibleRegion;
88         LayerProtoParser::Region damageRegion;
89         uint32_t layerStack;
90         int32_t z;
91         float2 position;
92         float2 requestedPosition;
93         int2 size;
94         LayerProtoParser::Rect crop;
95         LayerProtoParser::Rect finalCrop;
96         bool isOpaque;
97         bool invalidate;
98         std::string dataspace;
99         std::string pixelFormat;
100         half4 color;
101         half4 requestedColor;
102         uint32_t flags;
103         Transform transform;
104         Transform requestedTransform;
105         Layer* parent = 0;
106         Layer* zOrderRelativeOf = 0;
107         LayerProtoParser::ActiveBuffer activeBuffer;
108         int32_t queuedFrames;
109         bool refreshPending;
110         LayerProtoParser::Rect hwcFrame;
111         LayerProtoParser::FloatRect hwcCrop;
112         int32_t hwcTransform;
113         int32_t windowType;
114         int32_t appId;
115         int32_t hwcCompositionType;
116         bool isProtected;
117 
118         std::string to_string() const;
119     };
120 
121     class LayerGlobal {
122     public:
123         int2 resolution;
124         std::string colorMode;
125         std::string colorTransform;
126         int32_t globalTransform;
127     };
128 
129     static const LayerGlobal generateLayerGlobalInfo(const LayersProto& layersProto);
130     static std::vector<std::unique_ptr<Layer>> generateLayerTree(const LayersProto& layersProto);
131     static std::string layersToString(std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers);
132 
133 private:
134     static std::unordered_map<int32_t, Layer*> generateMap(const LayersProto& layersProto);
135     static LayerProtoParser::Layer* generateLayer(const LayerProto& layerProto);
136     static LayerProtoParser::Region generateRegion(const RegionProto& regionProto);
137     static LayerProtoParser::Rect generateRect(const RectProto& rectProto);
138     static LayerProtoParser::FloatRect generateFloatRect(const FloatRectProto& rectProto);
139     static LayerProtoParser::Transform generateTransform(const TransformProto& transformProto);
140     static LayerProtoParser::ActiveBuffer generateActiveBuffer(
141             const ActiveBufferProto& activeBufferProto);
142     static void updateChildrenAndRelative(const LayerProto& layerProto,
143                                           std::unordered_map<int32_t, Layer*>& layerMap);
144 
145     static std::string layerToString(LayerProtoParser::Layer* layer);
146 };
147 
148 } // namespace surfaceflinger
149 } // namespace android
150