blob: 41ea68420f498782c3e8968f8c5edfc9a088e877 [file] [log] [blame]
chaviw1d044282017-09-27 12:19:28 -07001/*
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>
Vishnu Naird37343b2022-01-12 16:18:56 -080018#include <renderengine/ExternalTexture.h>
chaviw1d044282017-09-27 12:19:28 -070019
Vishnu Nair9245d3b2019-03-22 13:38:56 -070020#include <Layer.h>
chaviw98318de2021-05-19 16:45:23 -050021#include <gui/WindowInfo.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070022#include <math/vec4.h>
Vishnu Nair6b591152021-10-08 11:45:14 -070023#include <ui/BlurRegion.h>
chaviw1d044282017-09-27 12:19:28 -070024#include <ui/GraphicBuffer.h>
25#include <ui/Rect.h>
26#include <ui/Region.h>
Peiyong Linefefaac2018-08-17 12:27:51 -070027#include <ui/Transform.h>
Vishnu Naird0183602023-03-16 18:52:15 +000028#include <cstdint>
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050029
30#include "FrontEnd/DisplayInfo.h"
Vishnu Naird0183602023-03-16 18:52:15 +000031#include "FrontEnd/LayerHierarchy.h"
32#include "FrontEnd/LayerSnapshot.h"
chaviw1d044282017-09-27 12:19:28 -070033
34namespace android {
35namespace surfaceflinger {
36class LayerProtoHelper {
37public:
Kean Mariotti4ba343c2023-04-19 13:31:02 +000038 static void writePositionToProto(
39 const float x, const float y,
40 std::function<perfetto::protos::PositionProto*()> getPositionProto);
Nataniel Borges797b0e42019-02-15 14:11:58 -080041 static void writeSizeToProto(const uint32_t w, const uint32_t h,
Kean Mariotti4ba343c2023-04-19 13:31:02 +000042 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);
Nataniel Borges797b0e42019-02-15 14:11:58 -080047 static void writeToProto(const FloatRect& rect,
Kean Mariotti4ba343c2023-04-19 13:31:02 +000048 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);
chaviw0a398992021-08-13 10:13:01 -050055 // 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,
Kean Mariotti4ba343c2023-04-19 13:31:02 +000058 perfetto::protos::TransformProto* transformProto);
chaviw0a398992021-08-13 10:13:01 -050059 static void writeTransformToProto(const ui::Transform& transform,
Kean Mariotti4ba343c2023-04-19 13:31:02 +000060 perfetto::protos::TransformProto* transformProto);
61 static void writeToProto(
62 const renderengine::ExternalTexture& buffer,
63 std::function<perfetto::protos::ActiveBufferProto*()> getActiveBufferProto);
64 static void writeToProto(
Vishnu Nairdc83d4b2024-08-15 16:29:20 +000065 const gui::WindowInfo& inputInfo,
Kean Mariotti4ba343c2023-04-19 13:31:02 +000066 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,
Vishnu Nairea6ff812023-02-27 17:41:39 +000075 const frontend::RequestedLayerState& requestedState,
76 const frontend::LayerSnapshot& snapshot, uint32_t traceFlags);
Kean Mariotti4ba343c2023-04-19 13:31:02 +000077 static google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto>
78 writeDisplayInfoToProto(const frontend::DisplayInfos&);
chaviw1d044282017-09-27 12:19:28 -070079};
80
Vishnu Naird0183602023-03-16 18:52:15 +000081class LayerProtoFromSnapshotGenerator {
82public:
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050083 LayerProtoFromSnapshotGenerator(const frontend::LayerSnapshotBuilder& snapshotBuilder,
84 const frontend::DisplayInfos& displayInfos,
85 const std::unordered_map<uint32_t, sp<Layer>>& legacyLayers,
86 uint32_t traceFlags)
Vishnu Naird0183602023-03-16 18:52:15 +000087 : mSnapshotBuilder(snapshotBuilder),
88 mLegacyLayers(legacyLayers),
89 mDisplayInfos(displayInfos),
90 mTraceFlags(traceFlags) {}
Vishnu Nairb46e37c2024-08-16 14:30:28 +000091 LayerProtoFromSnapshotGenerator& with(const frontend::LayerHierarchy& root);
92 // Creates a fake root and adds all offscreen layers from the passed in hierarchy to the fake
93 // root
94 LayerProtoFromSnapshotGenerator& withOffscreenLayers(
95 const frontend::LayerHierarchy& offscreenRoot);
96 perfetto::protos::LayersProto generate() { return mLayersProto; };
Vishnu Naird0183602023-03-16 18:52:15 +000097
98private:
99 void writeHierarchyToProto(const frontend::LayerHierarchy& root,
100 frontend::LayerHierarchy::TraversalPath& path);
101 frontend::LayerSnapshot* getSnapshot(frontend::LayerHierarchy::TraversalPath& path,
102 const frontend::RequestedLayerState& layer);
103
104 const frontend::LayerSnapshotBuilder& mSnapshotBuilder;
105 const std::unordered_map<uint32_t, sp<Layer>>& mLegacyLayers;
Dominik Laskowski6b049ff2023-01-29 15:46:45 -0500106 const frontend::DisplayInfos& mDisplayInfos;
Vishnu Naird0183602023-03-16 18:52:15 +0000107 uint32_t mTraceFlags;
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000108 perfetto::protos::LayersProto mLayersProto;
Vishnu Nair516228f2024-07-11 23:03:02 +0000109 std::unordered_set<uint32_t> mVisitedLayers;
110
Vishnu Naird0183602023-03-16 18:52:15 +0000111 // winscope expects all the layers, so provide a snapshot even if it not currently drawing
112 std::unordered_map<frontend::LayerHierarchy::TraversalPath, frontend::LayerSnapshot,
113 frontend::LayerHierarchy::TraversalPathHash>
114 mDefaultSnapshots;
115 std::unordered_map<uint32_t /* child unique seq*/, uint32_t /* relative parent unique seq*/>
116 mChildToRelativeParent;
117 std::unordered_map<uint32_t /* child unique seq*/, uint32_t /* parent unique seq*/>
118 mChildToParent;
119};
120
chaviw1d044282017-09-27 12:19:28 -0700121} // namespace surfaceflinger
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800122} // namespace android