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 writeToProto(const FloatRect& rect,
48                              std::function<perfetto::protos::FloatRectProto*()> getFloatRectProto);
49     static void writeToProto(const Region& region,
50                              std::function<perfetto::protos::RegionProto*()> getRegionProto);
51     static void writeToProto(const Region& region, perfetto::protos::RegionProto* regionProto);
52     static void readFromProto(const perfetto::protos::RegionProto& regionProto, Region& outRegion);
53     static void writeToProto(const half4 color,
54                              std::function<perfetto::protos::ColorProto*()> getColorProto);
55     // This writeToProto for transform is incorrect, but due to backwards compatibility, we can't
56     // update Layers to use it. Use writeTransformToProto for any new transform proto data.
57     static void writeToProtoDeprecated(const ui::Transform& transform,
58                                        perfetto::protos::TransformProto* transformProto);
59     static void writeTransformToProto(const ui::Transform& transform,
60                                       perfetto::protos::TransformProto* transformProto);
61     static void writeToProto(
62             const renderengine::ExternalTexture& buffer,
63             std::function<perfetto::protos::ActiveBufferProto*()> getActiveBufferProto);
64     static void writeToProto(
65             const gui::WindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds,
66             std::function<perfetto::protos::InputWindowInfoProto*()> getInputWindowInfoProto);
67     static void writeToProto(const mat4 matrix,
68                              perfetto::protos::ColorTransformProto* colorTransformProto);
69     static void readFromProto(const perfetto::protos::ColorTransformProto& colorTransformProto,
70                               mat4& matrix);
71     static void writeToProto(const android::BlurRegion region, perfetto::protos::BlurRegion*);
72     static void readFromProto(const perfetto::protos::BlurRegion& proto,
73                               android::BlurRegion& outRegion);
74     static void writeSnapshotToProto(perfetto::protos::LayerProto* outProto,
75                                      const frontend::RequestedLayerState& requestedState,
76                                      const frontend::LayerSnapshot& snapshot, uint32_t traceFlags);
77     static google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto>
78     writeDisplayInfoToProto(const frontend::DisplayInfos&);
79 };
80 
81 class LayerProtoFromSnapshotGenerator {
82 public:
LayerProtoFromSnapshotGenerator(const frontend::LayerSnapshotBuilder & snapshotBuilder,const frontend::DisplayInfos & displayInfos,const std::unordered_map<uint32_t,sp<Layer>> & legacyLayers,uint32_t traceFlags)83     LayerProtoFromSnapshotGenerator(const frontend::LayerSnapshotBuilder& snapshotBuilder,
84                                     const frontend::DisplayInfos& displayInfos,
85                                     const std::unordered_map<uint32_t, sp<Layer>>& legacyLayers,
86                                     uint32_t traceFlags)
87           : mSnapshotBuilder(snapshotBuilder),
88             mLegacyLayers(legacyLayers),
89             mDisplayInfos(displayInfos),
90             mTraceFlags(traceFlags) {}
91     perfetto::protos::LayersProto generate(const frontend::LayerHierarchy& root);
92 
93 private:
94     void writeHierarchyToProto(const frontend::LayerHierarchy& root,
95                                frontend::LayerHierarchy::TraversalPath& path);
96     frontend::LayerSnapshot* getSnapshot(frontend::LayerHierarchy::TraversalPath& path,
97                                          const frontend::RequestedLayerState& layer);
98 
99     const frontend::LayerSnapshotBuilder& mSnapshotBuilder;
100     const std::unordered_map<uint32_t, sp<Layer>>& mLegacyLayers;
101     const frontend::DisplayInfos& mDisplayInfos;
102     uint32_t mTraceFlags;
103     perfetto::protos::LayersProto mLayersProto;
104     // winscope expects all the layers, so provide a snapshot even if it not currently drawing
105     std::unordered_map<frontend::LayerHierarchy::TraversalPath, frontend::LayerSnapshot,
106                        frontend::LayerHierarchy::TraversalPathHash>
107             mDefaultSnapshots;
108     std::unordered_map<uint32_t /* child unique seq*/, uint32_t /* relative parent unique seq*/>
109             mChildToRelativeParent;
110     std::unordered_map<uint32_t /* child unique seq*/, uint32_t /* parent unique seq*/>
111             mChildToParent;
112 };
113 
114 } // namespace surfaceflinger
115 } // namespace android
116