xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/LayerProtoHelper.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 
17 #include <layerproto/LayerProtoHeader.h>
18 #include <renderengine/ExternalTexture.h>
19 
20 #include <Layer.h>
21 #include <gui/WindowInfo.h>
22 #include <math/vec4.h>
23 #include <ui/BlurRegion.h>
24 #include <ui/GraphicBuffer.h>
25 #include <ui/Rect.h>
26 #include <ui/Region.h>
27 #include <ui/Transform.h>
28 #include <cstdint>
29 
30 #include "FrontEnd/DisplayInfo.h"
31 #include "FrontEnd/LayerHierarchy.h"
32 #include "FrontEnd/LayerSnapshot.h"
33 
34 namespace android {
35 namespace surfaceflinger {
36 class LayerProtoHelper {
37 public:
38     static void writePositionToProto(
39             const float x, const float y,
40             std::function<perfetto::protos::PositionProto*()> getPositionProto);
41     static void writeSizeToProto(const uint32_t w, const uint32_t h,
42                                  std::function<perfetto::protos::SizeProto*()> getSizeProto);
43     static void writeToProto(const Rect& rect,
44                              std::function<perfetto::protos::RectProto*()> getRectProto);
45     static void writeToProto(const Rect& rect, perfetto::protos::RectProto* rectProto);
46     static void readFromProto(const perfetto::protos::RectProto& proto, Rect& outRect);
47     static void readFromProto(const perfetto::protos::RectProto& proto, FloatRect& outRect);
48     static void writeToProto(const FloatRect& rect,
49                              std::function<perfetto::protos::FloatRectProto*()> getFloatRectProto);
50     static void writeToProto(const Region& region,
51                              std::function<perfetto::protos::RegionProto*()> getRegionProto);
52     static void writeToProto(const Region& region, perfetto::protos::RegionProto* regionProto);
53     static void readFromProto(const perfetto::protos::RegionProto& regionProto, Region& outRegion);
54     static void writeToProto(const half4 color,
55                              std::function<perfetto::protos::ColorProto*()> getColorProto);
56     // This writeToProto for transform is incorrect, but due to backwards compatibility, we can't
57     // update Layers to use it. Use writeTransformToProto for any new transform proto data.
58     static void writeToProtoDeprecated(const ui::Transform& transform,
59                                        perfetto::protos::TransformProto* transformProto);
60     static void writeTransformToProto(const ui::Transform& transform,
61                                       perfetto::protos::TransformProto* transformProto);
62     static void writeToProto(
63             const renderengine::ExternalTexture& buffer,
64             std::function<perfetto::protos::ActiveBufferProto*()> getActiveBufferProto);
65     static void writeToProto(
66             const gui::WindowInfo& inputInfo,
67             std::function<perfetto::protos::InputWindowInfoProto*()> getInputWindowInfoProto);
68     static void writeToProto(const mat4 matrix,
69                              perfetto::protos::ColorTransformProto* colorTransformProto);
70     static void readFromProto(const perfetto::protos::ColorTransformProto& colorTransformProto,
71                               mat4& matrix);
72     static void writeToProto(const android::BlurRegion region, perfetto::protos::BlurRegion*);
73     static void readFromProto(const perfetto::protos::BlurRegion& proto,
74                               android::BlurRegion& outRegion);
75     static void writeSnapshotToProto(perfetto::protos::LayerProto* outProto,
76                                      const frontend::RequestedLayerState& requestedState,
77                                      const frontend::LayerSnapshot& snapshot, uint32_t traceFlags);
78     static google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto>
79     writeDisplayInfoToProto(const frontend::DisplayInfos&);
80 };
81 
82 class LayerProtoFromSnapshotGenerator {
83 public:
LayerProtoFromSnapshotGenerator(const frontend::LayerSnapshotBuilder & snapshotBuilder,const frontend::DisplayInfos & displayInfos,const std::unordered_map<uint32_t,sp<Layer>> & legacyLayers,uint32_t traceFlags)84     LayerProtoFromSnapshotGenerator(const frontend::LayerSnapshotBuilder& snapshotBuilder,
85                                     const frontend::DisplayInfos& displayInfos,
86                                     const std::unordered_map<uint32_t, sp<Layer>>& legacyLayers,
87                                     uint32_t traceFlags)
88           : mSnapshotBuilder(snapshotBuilder),
89             mLegacyLayers(legacyLayers),
90             mDisplayInfos(displayInfos),
91             mTraceFlags(traceFlags) {}
92     LayerProtoFromSnapshotGenerator& with(const frontend::LayerHierarchy& root);
93     // Creates a fake root and adds all offscreen layers from the passed in hierarchy to the fake
94     // root
95     LayerProtoFromSnapshotGenerator& withOffscreenLayers(
96             const frontend::LayerHierarchy& offscreenRoot);
generate()97     perfetto::protos::LayersProto generate() { return mLayersProto; };
98 
99 private:
100     void writeHierarchyToProto(const frontend::LayerHierarchy& root,
101                                frontend::LayerHierarchy::TraversalPath& path);
102     frontend::LayerSnapshot* getSnapshot(frontend::LayerHierarchy::TraversalPath& path,
103                                          const frontend::RequestedLayerState& layer);
104 
105     const frontend::LayerSnapshotBuilder& mSnapshotBuilder;
106     const std::unordered_map<uint32_t, sp<Layer>>& mLegacyLayers;
107     const frontend::DisplayInfos& mDisplayInfos;
108     uint32_t mTraceFlags;
109     perfetto::protos::LayersProto mLayersProto;
110     std::unordered_set<uint32_t> mVisitedLayers;
111 
112     // winscope expects all the layers, so provide a snapshot even if it not currently drawing
113     std::unordered_map<frontend::LayerHierarchy::TraversalPath, frontend::LayerSnapshot,
114                        frontend::LayerHierarchy::TraversalPathHash>
115             mDefaultSnapshots;
116     std::unordered_map<uint32_t /* child unique seq*/, uint32_t /* relative parent unique seq*/>
117             mChildToRelativeParent;
118     std::unordered_map<uint32_t /* child unique seq*/, uint32_t /* parent unique seq*/>
119             mChildToParent;
120 };
121 
122 } // namespace surfaceflinger
123 } // namespace android
124