xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/LayerProtoHelper.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <layerproto/LayerProtoHeader.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <renderengine/ExternalTexture.h>
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <Layer.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <gui/WindowInfo.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <math/vec4.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <ui/BlurRegion.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <ui/GraphicBuffer.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <ui/Rect.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <ui/Region.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <ui/Transform.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <cstdint>
29*38e8c45fSAndroid Build Coastguard Worker 
30*38e8c45fSAndroid Build Coastguard Worker #include "FrontEnd/DisplayInfo.h"
31*38e8c45fSAndroid Build Coastguard Worker #include "FrontEnd/LayerHierarchy.h"
32*38e8c45fSAndroid Build Coastguard Worker #include "FrontEnd/LayerSnapshot.h"
33*38e8c45fSAndroid Build Coastguard Worker 
34*38e8c45fSAndroid Build Coastguard Worker namespace android {
35*38e8c45fSAndroid Build Coastguard Worker namespace surfaceflinger {
36*38e8c45fSAndroid Build Coastguard Worker class LayerProtoHelper {
37*38e8c45fSAndroid Build Coastguard Worker public:
38*38e8c45fSAndroid Build Coastguard Worker     static void writePositionToProto(
39*38e8c45fSAndroid Build Coastguard Worker             const float x, const float y,
40*38e8c45fSAndroid Build Coastguard Worker             std::function<perfetto::protos::PositionProto*()> getPositionProto);
41*38e8c45fSAndroid Build Coastguard Worker     static void writeSizeToProto(const uint32_t w, const uint32_t h,
42*38e8c45fSAndroid Build Coastguard Worker                                  std::function<perfetto::protos::SizeProto*()> getSizeProto);
43*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(const Rect& rect,
44*38e8c45fSAndroid Build Coastguard Worker                              std::function<perfetto::protos::RectProto*()> getRectProto);
45*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(const Rect& rect, perfetto::protos::RectProto* rectProto);
46*38e8c45fSAndroid Build Coastguard Worker     static void readFromProto(const perfetto::protos::RectProto& proto, Rect& outRect);
47*38e8c45fSAndroid Build Coastguard Worker     static void readFromProto(const perfetto::protos::RectProto& proto, FloatRect& outRect);
48*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(const FloatRect& rect,
49*38e8c45fSAndroid Build Coastguard Worker                              std::function<perfetto::protos::FloatRectProto*()> getFloatRectProto);
50*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(const Region& region,
51*38e8c45fSAndroid Build Coastguard Worker                              std::function<perfetto::protos::RegionProto*()> getRegionProto);
52*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(const Region& region, perfetto::protos::RegionProto* regionProto);
53*38e8c45fSAndroid Build Coastguard Worker     static void readFromProto(const perfetto::protos::RegionProto& regionProto, Region& outRegion);
54*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(const half4 color,
55*38e8c45fSAndroid Build Coastguard Worker                              std::function<perfetto::protos::ColorProto*()> getColorProto);
56*38e8c45fSAndroid Build Coastguard Worker     // This writeToProto for transform is incorrect, but due to backwards compatibility, we can't
57*38e8c45fSAndroid Build Coastguard Worker     // update Layers to use it. Use writeTransformToProto for any new transform proto data.
58*38e8c45fSAndroid Build Coastguard Worker     static void writeToProtoDeprecated(const ui::Transform& transform,
59*38e8c45fSAndroid Build Coastguard Worker                                        perfetto::protos::TransformProto* transformProto);
60*38e8c45fSAndroid Build Coastguard Worker     static void writeTransformToProto(const ui::Transform& transform,
61*38e8c45fSAndroid Build Coastguard Worker                                       perfetto::protos::TransformProto* transformProto);
62*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(
63*38e8c45fSAndroid Build Coastguard Worker             const renderengine::ExternalTexture& buffer,
64*38e8c45fSAndroid Build Coastguard Worker             std::function<perfetto::protos::ActiveBufferProto*()> getActiveBufferProto);
65*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(
66*38e8c45fSAndroid Build Coastguard Worker             const gui::WindowInfo& inputInfo,
67*38e8c45fSAndroid Build Coastguard Worker             std::function<perfetto::protos::InputWindowInfoProto*()> getInputWindowInfoProto);
68*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(const mat4 matrix,
69*38e8c45fSAndroid Build Coastguard Worker                              perfetto::protos::ColorTransformProto* colorTransformProto);
70*38e8c45fSAndroid Build Coastguard Worker     static void readFromProto(const perfetto::protos::ColorTransformProto& colorTransformProto,
71*38e8c45fSAndroid Build Coastguard Worker                               mat4& matrix);
72*38e8c45fSAndroid Build Coastguard Worker     static void writeToProto(const android::BlurRegion region, perfetto::protos::BlurRegion*);
73*38e8c45fSAndroid Build Coastguard Worker     static void readFromProto(const perfetto::protos::BlurRegion& proto,
74*38e8c45fSAndroid Build Coastguard Worker                               android::BlurRegion& outRegion);
75*38e8c45fSAndroid Build Coastguard Worker     static void writeSnapshotToProto(perfetto::protos::LayerProto* outProto,
76*38e8c45fSAndroid Build Coastguard Worker                                      const frontend::RequestedLayerState& requestedState,
77*38e8c45fSAndroid Build Coastguard Worker                                      const frontend::LayerSnapshot& snapshot, uint32_t traceFlags);
78*38e8c45fSAndroid Build Coastguard Worker     static google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto>
79*38e8c45fSAndroid Build Coastguard Worker     writeDisplayInfoToProto(const frontend::DisplayInfos&);
80*38e8c45fSAndroid Build Coastguard Worker };
81*38e8c45fSAndroid Build Coastguard Worker 
82*38e8c45fSAndroid Build Coastguard Worker class LayerProtoFromSnapshotGenerator {
83*38e8c45fSAndroid Build Coastguard Worker public:
LayerProtoFromSnapshotGenerator(const frontend::LayerSnapshotBuilder & snapshotBuilder,const frontend::DisplayInfos & displayInfos,const std::unordered_map<uint32_t,sp<Layer>> & legacyLayers,uint32_t traceFlags)84*38e8c45fSAndroid Build Coastguard Worker     LayerProtoFromSnapshotGenerator(const frontend::LayerSnapshotBuilder& snapshotBuilder,
85*38e8c45fSAndroid Build Coastguard Worker                                     const frontend::DisplayInfos& displayInfos,
86*38e8c45fSAndroid Build Coastguard Worker                                     const std::unordered_map<uint32_t, sp<Layer>>& legacyLayers,
87*38e8c45fSAndroid Build Coastguard Worker                                     uint32_t traceFlags)
88*38e8c45fSAndroid Build Coastguard Worker           : mSnapshotBuilder(snapshotBuilder),
89*38e8c45fSAndroid Build Coastguard Worker             mLegacyLayers(legacyLayers),
90*38e8c45fSAndroid Build Coastguard Worker             mDisplayInfos(displayInfos),
91*38e8c45fSAndroid Build Coastguard Worker             mTraceFlags(traceFlags) {}
92*38e8c45fSAndroid Build Coastguard Worker     LayerProtoFromSnapshotGenerator& with(const frontend::LayerHierarchy& root);
93*38e8c45fSAndroid Build Coastguard Worker     // Creates a fake root and adds all offscreen layers from the passed in hierarchy to the fake
94*38e8c45fSAndroid Build Coastguard Worker     // root
95*38e8c45fSAndroid Build Coastguard Worker     LayerProtoFromSnapshotGenerator& withOffscreenLayers(
96*38e8c45fSAndroid Build Coastguard Worker             const frontend::LayerHierarchy& offscreenRoot);
generate()97*38e8c45fSAndroid Build Coastguard Worker     perfetto::protos::LayersProto generate() { return mLayersProto; };
98*38e8c45fSAndroid Build Coastguard Worker 
99*38e8c45fSAndroid Build Coastguard Worker private:
100*38e8c45fSAndroid Build Coastguard Worker     void writeHierarchyToProto(const frontend::LayerHierarchy& root,
101*38e8c45fSAndroid Build Coastguard Worker                                frontend::LayerHierarchy::TraversalPath& path);
102*38e8c45fSAndroid Build Coastguard Worker     frontend::LayerSnapshot* getSnapshot(frontend::LayerHierarchy::TraversalPath& path,
103*38e8c45fSAndroid Build Coastguard Worker                                          const frontend::RequestedLayerState& layer);
104*38e8c45fSAndroid Build Coastguard Worker 
105*38e8c45fSAndroid Build Coastguard Worker     const frontend::LayerSnapshotBuilder& mSnapshotBuilder;
106*38e8c45fSAndroid Build Coastguard Worker     const std::unordered_map<uint32_t, sp<Layer>>& mLegacyLayers;
107*38e8c45fSAndroid Build Coastguard Worker     const frontend::DisplayInfos& mDisplayInfos;
108*38e8c45fSAndroid Build Coastguard Worker     uint32_t mTraceFlags;
109*38e8c45fSAndroid Build Coastguard Worker     perfetto::protos::LayersProto mLayersProto;
110*38e8c45fSAndroid Build Coastguard Worker     std::unordered_set<uint32_t> mVisitedLayers;
111*38e8c45fSAndroid Build Coastguard Worker 
112*38e8c45fSAndroid Build Coastguard Worker     // winscope expects all the layers, so provide a snapshot even if it not currently drawing
113*38e8c45fSAndroid Build Coastguard Worker     std::unordered_map<frontend::LayerHierarchy::TraversalPath, frontend::LayerSnapshot,
114*38e8c45fSAndroid Build Coastguard Worker                        frontend::LayerHierarchy::TraversalPathHash>
115*38e8c45fSAndroid Build Coastguard Worker             mDefaultSnapshots;
116*38e8c45fSAndroid Build Coastguard Worker     std::unordered_map<uint32_t /* child unique seq*/, uint32_t /* relative parent unique seq*/>
117*38e8c45fSAndroid Build Coastguard Worker             mChildToRelativeParent;
118*38e8c45fSAndroid Build Coastguard Worker     std::unordered_map<uint32_t /* child unique seq*/, uint32_t /* parent unique seq*/>
119*38e8c45fSAndroid Build Coastguard Worker             mChildToParent;
120*38e8c45fSAndroid Build Coastguard Worker };
121*38e8c45fSAndroid Build Coastguard Worker 
122*38e8c45fSAndroid Build Coastguard Worker } // namespace surfaceflinger
123*38e8c45fSAndroid Build Coastguard Worker } // namespace android
124