blob: 2dc89b55ba8ecac18ba05cab08caf7348f76b858 [file] [log] [blame]
Vishnu Nair6b591152021-10-08 11:45:14 -07001/*
2 * Copyright (C) 2021 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 <gui/SurfaceComposerClient.h>
Vishnu Nair8eebba42022-02-25 07:57:15 -080018#include <ui/Fence.h>
Vishnu Nair6b591152021-10-08 11:45:14 -070019#include <ui/Rect.h>
20
Vishnu Nair81750622023-03-08 15:02:06 -080021#include "FrontEnd/LayerCreationArgs.h"
Vishnu Nair6b591152021-10-08 11:45:14 -070022#include "LayerProtoHelper.h"
23#include "TransactionProtoParser.h"
Vishnu Nair81750622023-03-08 15:02:06 -080024#include "TransactionState.h"
25#include "gui/LayerState.h"
Vishnu Nair6b591152021-10-08 11:45:14 -070026
27namespace android::surfaceflinger {
28
Vishnu Nair81750622023-03-08 15:02:06 -080029class FakeExternalTexture : public renderengine::ExternalTexture {
Vishnu Nair32ebda42023-03-16 10:04:29 -070030 const sp<GraphicBuffer> mEmptyBuffer = nullptr;
Vishnu Nair81750622023-03-08 15:02:06 -080031 uint32_t mWidth;
32 uint32_t mHeight;
33 uint64_t mId;
34 PixelFormat mPixelFormat;
35 uint64_t mUsage;
36
37public:
38 FakeExternalTexture(uint32_t width, uint32_t height, uint64_t id, PixelFormat pixelFormat,
39 uint64_t usage)
40 : mWidth(width), mHeight(height), mId(id), mPixelFormat(pixelFormat), mUsage(usage) {}
41 const sp<GraphicBuffer>& getBuffer() const { return mEmptyBuffer; }
42 bool hasSameBuffer(const renderengine::ExternalTexture& other) const override {
43 return getId() == other.getId();
44 }
45 uint32_t getWidth() const override { return mWidth; }
46 uint32_t getHeight() const override { return mHeight; }
47 uint64_t getId() const override { return mId; }
48 PixelFormat getPixelFormat() const override { return mPixelFormat; }
49 uint64_t getUsage() const override { return mUsage; }
Alec Mouri74c7ae12023-03-26 02:57:47 +000050 void remapBuffer() override {}
Vishnu Nair81750622023-03-08 15:02:06 -080051 ~FakeExternalTexture() = default;
52};
53
Kean Mariotti4ba343c2023-04-19 13:31:02 +000054perfetto::protos::TransactionState TransactionProtoParser::toProto(const TransactionState& t) {
55 perfetto::protos::TransactionState proto;
Vishnu Nair6b591152021-10-08 11:45:14 -070056 proto.set_pid(t.originPid);
57 proto.set_uid(t.originUid);
58 proto.set_vsync_id(t.frameTimelineInfo.vsyncId);
59 proto.set_input_event_id(t.frameTimelineInfo.inputEventId);
60 proto.set_post_time(t.postTime);
Vishnu Naird37343b2022-01-12 16:18:56 -080061 proto.set_transaction_id(t.id);
Vishnu Nair6b591152021-10-08 11:45:14 -070062
Vishnu Nair685cfef2022-02-02 10:01:25 -080063 proto.mutable_layer_changes()->Reserve(static_cast<int32_t>(t.states.size()));
Vishnu Nair6b591152021-10-08 11:45:14 -070064 for (auto& layerState : t.states) {
Vishnu Nair81750622023-03-08 15:02:06 -080065 proto.mutable_layer_changes()->Add(std::move(toProto(layerState)));
Vishnu Nair6b591152021-10-08 11:45:14 -070066 }
67
Vishnu Nair685cfef2022-02-02 10:01:25 -080068 proto.mutable_display_changes()->Reserve(static_cast<int32_t>(t.displays.size()));
Vishnu Nair6b591152021-10-08 11:45:14 -070069 for (auto& displayState : t.displays) {
Vishnu Nair685cfef2022-02-02 10:01:25 -080070 proto.mutable_display_changes()->Add(std::move(toProto(displayState)));
Vishnu Nair6b591152021-10-08 11:45:14 -070071 }
Pablo Gamito23780be2023-04-18 08:30:00 +000072
73 proto.mutable_merged_transaction_ids()->Reserve(
74 static_cast<int32_t>(t.mergedTransactionIds.size()));
75 for (auto& mergedTransactionId : t.mergedTransactionIds) {
76 proto.mutable_merged_transaction_ids()->Add(mergedTransactionId);
77 }
78
Vishnu Nair6b591152021-10-08 11:45:14 -070079 return proto;
80}
81
Kean Mariotti4ba343c2023-04-19 13:31:02 +000082perfetto::protos::TransactionState TransactionProtoParser::toProto(
Vishnu Nair81750622023-03-08 15:02:06 -080083 const std::map<uint32_t /* layerId */, TracingLayerState>& states) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +000084 perfetto::protos::TransactionState proto;
Vishnu Nair685cfef2022-02-02 10:01:25 -080085 proto.mutable_layer_changes()->Reserve(static_cast<int32_t>(states.size()));
Vishnu Nair68dee2b2021-11-08 18:52:12 -080086 for (auto& [layerId, state] : states) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +000087 perfetto::protos::LayerState layerProto = toProto(state);
Vishnu Nair68dee2b2021-11-08 18:52:12 -080088 layerProto.set_has_sideband_stream(state.hasSidebandStream);
Vishnu Nair68dee2b2021-11-08 18:52:12 -080089 proto.mutable_layer_changes()->Add(std::move(layerProto));
90 }
91 return proto;
92}
93
Kean Mariotti4ba343c2023-04-19 13:31:02 +000094perfetto::protos::LayerState TransactionProtoParser::toProto(
Vishnu Nair81750622023-03-08 15:02:06 -080095 const ResolvedComposerState& resolvedComposerState) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +000096 perfetto::protos::LayerState proto;
Vishnu Nair81750622023-03-08 15:02:06 -080097 auto& layer = resolvedComposerState.state;
98 proto.set_layer_id(resolvedComposerState.layerId);
Vishnu Nair6b591152021-10-08 11:45:14 -070099 proto.set_what(layer.what);
100
101 if (layer.what & layer_state_t::ePositionChanged) {
102 proto.set_x(layer.x);
103 proto.set_y(layer.y);
104 }
105 if (layer.what & layer_state_t::eLayerChanged) {
106 proto.set_z(layer.z);
107 }
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000108
Vishnu Nair6b591152021-10-08 11:45:14 -0700109 if (layer.what & layer_state_t::eLayerStackChanged) {
110 proto.set_layer_stack(layer.layerStack.id);
111 }
112 if (layer.what & layer_state_t::eFlagsChanged) {
113 proto.set_flags(layer.flags);
114 proto.set_mask(layer.mask);
115 }
116 if (layer.what & layer_state_t::eMatrixChanged) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000117 perfetto::protos::LayerState_Matrix22* matrixProto = proto.mutable_matrix();
Vishnu Nair6b591152021-10-08 11:45:14 -0700118 matrixProto->set_dsdx(layer.matrix.dsdx);
119 matrixProto->set_dsdy(layer.matrix.dsdy);
120 matrixProto->set_dtdx(layer.matrix.dtdx);
121 matrixProto->set_dtdy(layer.matrix.dtdy);
122 }
123 if (layer.what & layer_state_t::eCornerRadiusChanged) {
124 proto.set_corner_radius(layer.cornerRadius);
125 }
126 if (layer.what & layer_state_t::eBackgroundBlurRadiusChanged) {
127 proto.set_background_blur_radius(layer.backgroundBlurRadius);
128 }
129
130 if (layer.what & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +0000131 proto.set_alpha(layer.color.a);
Vishnu Nair6b591152021-10-08 11:45:14 -0700132 }
133
134 if (layer.what & layer_state_t::eColorChanged) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000135 perfetto::protos::LayerState_Color3* colorProto = proto.mutable_color();
Vishnu Nair6b591152021-10-08 11:45:14 -0700136 colorProto->set_r(layer.color.r);
137 colorProto->set_g(layer.color.g);
138 colorProto->set_b(layer.color.b);
139 }
140 if (layer.what & layer_state_t::eTransparentRegionChanged) {
141 LayerProtoHelper::writeToProto(layer.transparentRegion, proto.mutable_transparent_region());
142 }
Vishnu Nairbbceb462022-10-10 04:52:13 +0000143 if (layer.what & layer_state_t::eBufferTransformChanged) {
144 proto.set_transform(layer.bufferTransform);
Vishnu Nair6b591152021-10-08 11:45:14 -0700145 }
146 if (layer.what & layer_state_t::eTransformToDisplayInverseChanged) {
147 proto.set_transform_to_display_inverse(layer.transformToDisplayInverse);
148 }
149 if (layer.what & layer_state_t::eCropChanged) {
150 LayerProtoHelper::writeToProto(layer.crop, proto.mutable_crop());
151 }
152 if (layer.what & layer_state_t::eBufferChanged) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000153 perfetto::protos::LayerState_BufferData* bufferProto = proto.mutable_buffer_data();
Vishnu Nair81750622023-03-08 15:02:06 -0800154 if (resolvedComposerState.externalTexture) {
155 bufferProto->set_buffer_id(resolvedComposerState.externalTexture->getId());
156 bufferProto->set_width(resolvedComposerState.externalTexture->getWidth());
157 bufferProto->set_height(resolvedComposerState.externalTexture->getHeight());
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000158 bufferProto->set_pixel_format(
159 static_cast<perfetto::protos::LayerState_BufferData_PixelFormat>(
160 resolvedComposerState.externalTexture->getPixelFormat()));
Vishnu Nair81750622023-03-08 15:02:06 -0800161 bufferProto->set_usage(resolvedComposerState.externalTexture->getUsage());
Vishnu Nair6b591152021-10-08 11:45:14 -0700162 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800163 bufferProto->set_frame_number(layer.bufferData->frameNumber);
164 bufferProto->set_flags(layer.bufferData->flags.get());
165 bufferProto->set_cached_buffer_id(layer.bufferData->cachedBuffer.id);
Vishnu Nair6b591152021-10-08 11:45:14 -0700166 }
167 if (layer.what & layer_state_t::eSidebandStreamChanged) {
168 proto.set_has_sideband_stream(layer.sidebandStream != nullptr);
169 }
170
171 if (layer.what & layer_state_t::eApiChanged) {
172 proto.set_api(layer.api);
173 }
174
175 if (layer.what & layer_state_t::eColorTransformChanged) {
176 LayerProtoHelper::writeToProto(layer.colorTransform, proto.mutable_color_transform());
177 }
178 if (layer.what & layer_state_t::eBlurRegionsChanged) {
179 for (auto& region : layer.blurRegions) {
180 LayerProtoHelper::writeToProto(region, proto.add_blur_regions());
181 }
182 }
183
Vishnu Nair685cfef2022-02-02 10:01:25 -0800184 if (layer.what & layer_state_t::eReparent) {
Vishnu Nair81750622023-03-08 15:02:06 -0800185 proto.set_parent_id(resolvedComposerState.parentId);
Vishnu Nair6b591152021-10-08 11:45:14 -0700186 }
Vishnu Nair685cfef2022-02-02 10:01:25 -0800187 if (layer.what & layer_state_t::eRelativeLayerChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800188 proto.set_relative_parent_id(resolvedComposerState.relativeParentId);
Vishnu Naird37343b2022-01-12 16:18:56 -0800189 proto.set_z(layer.z);
Vishnu Nair6b591152021-10-08 11:45:14 -0700190 }
191
192 if (layer.what & layer_state_t::eInputInfoChanged) {
193 if (layer.windowInfoHandle) {
194 const gui::WindowInfo* inputInfo = layer.windowInfoHandle->getInfo();
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000195 perfetto::protos::LayerState_WindowInfo* windowInfoProto =
196 proto.mutable_window_info_handle();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800197 windowInfoProto->set_layout_params_flags(inputInfo->layoutParamsFlags.get());
198 windowInfoProto->set_layout_params_type(
199 static_cast<int32_t>(inputInfo->layoutParamsType));
Siarhei Vishniakoue2eff112023-06-21 14:20:39 -0700200 windowInfoProto->set_input_config(inputInfo->inputConfig.get());
Vishnu Nair6b591152021-10-08 11:45:14 -0700201 LayerProtoHelper::writeToProto(inputInfo->touchableRegion,
202 windowInfoProto->mutable_touchable_region());
203 windowInfoProto->set_surface_inset(inputInfo->surfaceInset);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800204 windowInfoProto->set_focusable(
205 !inputInfo->inputConfig.test(gui::WindowInfo::InputConfig::NOT_FOCUSABLE));
206 windowInfoProto->set_has_wallpaper(inputInfo->inputConfig.test(
207 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER));
Vishnu Nair6b591152021-10-08 11:45:14 -0700208 windowInfoProto->set_global_scale_factor(inputInfo->globalScaleFactor);
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000209 perfetto::protos::Transform* transformProto = windowInfoProto->mutable_transform();
Vishnu Nair6b591152021-10-08 11:45:14 -0700210 transformProto->set_dsdx(inputInfo->transform.dsdx());
211 transformProto->set_dtdx(inputInfo->transform.dtdx());
212 transformProto->set_dtdy(inputInfo->transform.dtdy());
213 transformProto->set_dsdy(inputInfo->transform.dsdy());
214 transformProto->set_tx(inputInfo->transform.tx());
215 transformProto->set_ty(inputInfo->transform.ty());
216 windowInfoProto->set_replace_touchable_region_with_crop(
217 inputInfo->replaceTouchableRegionWithCrop);
Vishnu Nair81750622023-03-08 15:02:06 -0800218 windowInfoProto->set_crop_layer_id(resolvedComposerState.touchCropId);
Vishnu Nair6b591152021-10-08 11:45:14 -0700219 }
220 }
221 if (layer.what & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000222 proto.set_bg_color_alpha(layer.bgColor.a);
Vishnu Nair6b591152021-10-08 11:45:14 -0700223 proto.set_bg_color_dataspace(static_cast<int32_t>(layer.bgColorDataspace));
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000224 perfetto::protos::LayerState_Color3* colorProto = proto.mutable_color();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000225 colorProto->set_r(layer.bgColor.r);
226 colorProto->set_g(layer.bgColor.g);
227 colorProto->set_b(layer.bgColor.b);
Vishnu Nair6b591152021-10-08 11:45:14 -0700228 }
229 if (layer.what & layer_state_t::eColorSpaceAgnosticChanged) {
230 proto.set_color_space_agnostic(layer.colorSpaceAgnostic);
231 }
232 if (layer.what & layer_state_t::eShadowRadiusChanged) {
233 proto.set_shadow_radius(layer.shadowRadius);
234 }
235 if (layer.what & layer_state_t::eFrameRateSelectionPriority) {
236 proto.set_frame_rate_selection_priority(layer.frameRateSelectionPriority);
237 }
238 if (layer.what & layer_state_t::eFrameRateChanged) {
239 proto.set_frame_rate(layer.frameRate);
240 proto.set_frame_rate_compatibility(layer.frameRateCompatibility);
241 proto.set_change_frame_rate_strategy(layer.changeFrameRateStrategy);
242 }
243 if (layer.what & layer_state_t::eFixedTransformHintChanged) {
244 proto.set_fixed_transform_hint(layer.fixedTransformHint);
245 }
246 if (layer.what & layer_state_t::eAutoRefreshChanged) {
247 proto.set_auto_refresh(layer.autoRefresh);
248 }
249 if (layer.what & layer_state_t::eTrustedOverlayChanged) {
250 proto.set_is_trusted_overlay(layer.isTrustedOverlay);
251 }
252 if (layer.what & layer_state_t::eBufferCropChanged) {
253 LayerProtoHelper::writeToProto(layer.bufferCrop, proto.mutable_buffer_crop());
254 }
255 if (layer.what & layer_state_t::eDestinationFrameChanged) {
256 LayerProtoHelper::writeToProto(layer.destinationFrame, proto.mutable_destination_frame());
257 }
258 if (layer.what & layer_state_t::eDropInputModeChanged) {
259 proto.set_drop_input_mode(
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000260 static_cast<perfetto::protos::LayerState_DropInputMode>(layer.dropInputMode));
Vishnu Nair6b591152021-10-08 11:45:14 -0700261 }
262 return proto;
263}
264
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000265perfetto::protos::DisplayState TransactionProtoParser::toProto(const DisplayState& display) {
266 perfetto::protos::DisplayState proto;
Vishnu Nair6b591152021-10-08 11:45:14 -0700267 proto.set_what(display.what);
Vishnu Nair685cfef2022-02-02 10:01:25 -0800268 proto.set_id(mMapper->getDisplayId(display.token));
Vishnu Nair6b591152021-10-08 11:45:14 -0700269
270 if (display.what & DisplayState::eLayerStackChanged) {
271 proto.set_layer_stack(display.layerStack.id);
272 }
273 if (display.what & DisplayState::eDisplayProjectionChanged) {
274 proto.set_orientation(static_cast<uint32_t>(display.orientation));
275 LayerProtoHelper::writeToProto(display.orientedDisplaySpaceRect,
276 proto.mutable_oriented_display_space_rect());
277 LayerProtoHelper::writeToProto(display.layerStackSpaceRect,
278 proto.mutable_layer_stack_space_rect());
279 }
280 if (display.what & DisplayState::eDisplaySizeChanged) {
281 proto.set_width(display.width);
282 proto.set_height(display.height);
283 }
284 if (display.what & DisplayState::eFlagsChanged) {
285 proto.set_flags(display.flags);
286 }
287 return proto;
288}
289
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000290perfetto::protos::LayerCreationArgs TransactionProtoParser::toProto(const LayerCreationArgs& args) {
291 perfetto::protos::LayerCreationArgs proto;
Vishnu Nair81750622023-03-08 15:02:06 -0800292 proto.set_layer_id(args.sequence);
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800293 proto.set_name(args.name);
294 proto.set_flags(args.flags);
295 proto.set_parent_id(args.parentId);
Vishnu Nair81750622023-03-08 15:02:06 -0800296 proto.set_mirror_from_id(args.layerIdToMirror);
297 proto.set_add_to_root(args.addToRoot);
298 proto.set_layer_stack_to_mirror(args.layerStackToMirror.id);
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800299 return proto;
300}
301
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000302TransactionState TransactionProtoParser::fromProto(
303 const perfetto::protos::TransactionState& proto) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700304 TransactionState t;
305 t.originPid = proto.pid();
306 t.originUid = proto.uid();
307 t.frameTimelineInfo.vsyncId = proto.vsync_id();
308 t.frameTimelineInfo.inputEventId = proto.input_event_id();
309 t.postTime = proto.post_time();
Vishnu Naird37343b2022-01-12 16:18:56 -0800310 t.id = proto.transaction_id();
311
Vishnu Nair6b591152021-10-08 11:45:14 -0700312 int32_t layerCount = proto.layer_changes_size();
313 t.states.reserve(static_cast<size_t>(layerCount));
314 for (int i = 0; i < layerCount; i++) {
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000315 ResolvedComposerState s;
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800316 s.state.what = 0;
Vishnu Nair81750622023-03-08 15:02:06 -0800317 fromProto(proto.layer_changes(i), s);
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000318 t.states.emplace_back(s);
Vishnu Nair6b591152021-10-08 11:45:14 -0700319 }
320
321 int32_t displayCount = proto.display_changes_size();
322 t.displays.reserve(static_cast<size_t>(displayCount));
323 for (int i = 0; i < displayCount; i++) {
Vishnu Nair685cfef2022-02-02 10:01:25 -0800324 t.displays.add(fromProto(proto.display_changes(i)));
Vishnu Nair6b591152021-10-08 11:45:14 -0700325 }
326 return t;
327}
328
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000329void TransactionProtoParser::fromProto(const perfetto::protos::LayerCreationArgs& proto,
Vishnu Nair81750622023-03-08 15:02:06 -0800330 LayerCreationArgs& outArgs) {
331 outArgs.sequence = proto.layer_id();
332
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800333 outArgs.name = proto.name();
334 outArgs.flags = proto.flags();
335 outArgs.parentId = proto.parent_id();
Vishnu Nair81750622023-03-08 15:02:06 -0800336 outArgs.layerIdToMirror = proto.mirror_from_id();
337 outArgs.addToRoot = proto.add_to_root();
338 outArgs.layerStackToMirror.id = proto.layer_stack_to_mirror();
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800339}
Vishnu Nair6b591152021-10-08 11:45:14 -0700340
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000341void TransactionProtoParser::mergeFromProto(const perfetto::protos::LayerState& proto,
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800342 TracingLayerState& outState) {
Vishnu Nair81750622023-03-08 15:02:06 -0800343 ResolvedComposerState resolvedComposerState;
344 fromProto(proto, resolvedComposerState);
345 layer_state_t& state = resolvedComposerState.state;
346 outState.state.merge(state);
347 outState.layerId = resolvedComposerState.layerId;
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800348
349 if (state.what & layer_state_t::eReparent) {
Vishnu Nair81750622023-03-08 15:02:06 -0800350 outState.parentId = resolvedComposerState.parentId;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800351 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800352 if (state.what & layer_state_t::eRelativeLayerChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800353 outState.relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800354 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800355 if (state.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800356 outState.touchCropId = resolvedComposerState.touchCropId;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800357 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800358 if (state.what & layer_state_t::eBufferChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800359 outState.externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800360 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800361 if (state.what & layer_state_t::eSidebandStreamChanged) {
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800362 outState.hasSidebandStream = proto.has_sideband_stream();
363 }
364}
365
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000366void TransactionProtoParser::fromProto(const perfetto::protos::LayerState& proto,
Vishnu Nair81750622023-03-08 15:02:06 -0800367 ResolvedComposerState& resolvedComposerState) {
368 auto& layer = resolvedComposerState.state;
369 resolvedComposerState.layerId = proto.layer_id();
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800370 layer.what |= proto.what();
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800371
372 if (proto.what() & layer_state_t::ePositionChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700373 layer.x = proto.x();
374 layer.y = proto.y();
375 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800376 if (proto.what() & layer_state_t::eLayerChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700377 layer.z = proto.z();
378 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800379 if (proto.what() & layer_state_t::eLayerStackChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700380 layer.layerStack.id = proto.layer_stack();
381 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800382 if (proto.what() & layer_state_t::eFlagsChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700383 layer.flags = proto.flags();
384 layer.mask = proto.mask();
385 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800386 if (proto.what() & layer_state_t::eMatrixChanged) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000387 const perfetto::protos::LayerState_Matrix22& matrixProto = proto.matrix();
Vishnu Nair6b591152021-10-08 11:45:14 -0700388 layer.matrix.dsdx = matrixProto.dsdx();
389 layer.matrix.dsdy = matrixProto.dsdy();
390 layer.matrix.dtdx = matrixProto.dtdx();
391 layer.matrix.dtdy = matrixProto.dtdy();
392 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800393 if (proto.what() & layer_state_t::eCornerRadiusChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700394 layer.cornerRadius = proto.corner_radius();
395 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800396 if (proto.what() & layer_state_t::eBackgroundBlurRadiusChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700397 layer.backgroundBlurRadius = proto.background_blur_radius();
398 }
399
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800400 if (proto.what() & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +0000401 layer.color.a = proto.alpha();
Vishnu Nair6b591152021-10-08 11:45:14 -0700402 }
403
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800404 if (proto.what() & layer_state_t::eColorChanged) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000405 const perfetto::protos::LayerState_Color3& colorProto = proto.color();
Vishnu Nair6b591152021-10-08 11:45:14 -0700406 layer.color.r = colorProto.r();
407 layer.color.g = colorProto.g();
408 layer.color.b = colorProto.b();
409 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800410 if (proto.what() & layer_state_t::eTransparentRegionChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700411 LayerProtoHelper::readFromProto(proto.transparent_region(), layer.transparentRegion);
412 }
Vishnu Nairbbceb462022-10-10 04:52:13 +0000413 if (proto.what() & layer_state_t::eBufferTransformChanged) {
414 layer.bufferTransform = proto.transform();
Vishnu Nair6b591152021-10-08 11:45:14 -0700415 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800416 if (proto.what() & layer_state_t::eTransformToDisplayInverseChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700417 layer.transformToDisplayInverse = proto.transform_to_display_inverse();
418 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800419 if (proto.what() & layer_state_t::eCropChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700420 LayerProtoHelper::readFromProto(proto.crop(), layer.crop);
421 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800422 if (proto.what() & layer_state_t::eBufferChanged) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000423 const perfetto::protos::LayerState_BufferData& bufferProto = proto.buffer_data();
Vishnu Nair685cfef2022-02-02 10:01:25 -0800424 layer.bufferData =
Vishnu Nair81750622023-03-08 15:02:06 -0800425 std::make_shared<fake::BufferData>(bufferProto.buffer_id(), bufferProto.width(),
426 bufferProto.height(), bufferProto.pixel_format(),
427 bufferProto.usage());
428 resolvedComposerState.externalTexture =
429 std::make_shared<FakeExternalTexture>(layer.bufferData->getWidth(),
430 layer.bufferData->getHeight(),
431 layer.bufferData->getId(),
432 layer.bufferData->getPixelFormat(),
433 layer.bufferData->getUsage());
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800434 layer.bufferData->frameNumber = bufferProto.frame_number();
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700435 layer.bufferData->flags = ftl::Flags<BufferData::BufferDataChange>(bufferProto.flags());
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800436 layer.bufferData->cachedBuffer.id = bufferProto.cached_buffer_id();
Vishnu Nair8eebba42022-02-25 07:57:15 -0800437 layer.bufferData->acquireFence = Fence::NO_FENCE;
Vishnu Nair6b591152021-10-08 11:45:14 -0700438 }
Vishnu Nair6b591152021-10-08 11:45:14 -0700439
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800440 if (proto.what() & layer_state_t::eApiChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700441 layer.api = proto.api();
442 }
443
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800444 if (proto.what() & layer_state_t::eColorTransformChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700445 LayerProtoHelper::readFromProto(proto.color_transform(), layer.colorTransform);
446 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800447 if (proto.what() & layer_state_t::eBlurRegionsChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700448 layer.blurRegions.reserve(static_cast<size_t>(proto.blur_regions_size()));
449 for (int i = 0; i < proto.blur_regions_size(); i++) {
450 android::BlurRegion region;
451 LayerProtoHelper::readFromProto(proto.blur_regions(i), region);
452 layer.blurRegions.push_back(region);
453 }
454 }
455
Vishnu Nair685cfef2022-02-02 10:01:25 -0800456 if (proto.what() & layer_state_t::eReparent) {
Vishnu Nair81750622023-03-08 15:02:06 -0800457 resolvedComposerState.parentId = proto.parent_id();
Vishnu Nair6b591152021-10-08 11:45:14 -0700458 }
Vishnu Naird37343b2022-01-12 16:18:56 -0800459 if (proto.what() & layer_state_t::eRelativeLayerChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800460 resolvedComposerState.relativeParentId = proto.relative_parent_id();
Vishnu Naird37343b2022-01-12 16:18:56 -0800461 layer.z = proto.z();
Vishnu Nair6b591152021-10-08 11:45:14 -0700462 }
463
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800464 if ((proto.what() & layer_state_t::eInputInfoChanged) && proto.has_window_info_handle()) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700465 gui::WindowInfo inputInfo;
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000466 const perfetto::protos::LayerState_WindowInfo& windowInfoProto = proto.window_info_handle();
Vishnu Nair6b591152021-10-08 11:45:14 -0700467
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800468 inputInfo.layoutParamsFlags =
469 static_cast<gui::WindowInfo::Flag>(windowInfoProto.layout_params_flags());
470 inputInfo.layoutParamsType =
471 static_cast<gui::WindowInfo::Type>(windowInfoProto.layout_params_type());
Vishnu Nair6b591152021-10-08 11:45:14 -0700472 LayerProtoHelper::readFromProto(windowInfoProto.touchable_region(),
473 inputInfo.touchableRegion);
Siarhei Vishniakoue2eff112023-06-21 14:20:39 -0700474 inputInfo.inputConfig =
475 ftl::Flags<gui::WindowInfo::InputConfig>(windowInfoProto.input_config());
Vishnu Nair6b591152021-10-08 11:45:14 -0700476 inputInfo.surfaceInset = windowInfoProto.surface_inset();
Vishnu Nair6b591152021-10-08 11:45:14 -0700477 inputInfo.globalScaleFactor = windowInfoProto.global_scale_factor();
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000478 const perfetto::protos::Transform& transformProto = windowInfoProto.transform();
Vishnu Nair6b591152021-10-08 11:45:14 -0700479 inputInfo.transform.set(transformProto.dsdx(), transformProto.dtdx(), transformProto.dtdy(),
480 transformProto.dsdy());
481 inputInfo.transform.set(transformProto.tx(), transformProto.ty());
482 inputInfo.replaceTouchableRegionWithCrop =
483 windowInfoProto.replace_touchable_region_with_crop();
Vishnu Nair81750622023-03-08 15:02:06 -0800484 resolvedComposerState.touchCropId = windowInfoProto.crop_layer_id();
Vishnu Nair286f4f92022-06-08 16:37:39 -0700485
Vishnu Nair6b591152021-10-08 11:45:14 -0700486 layer.windowInfoHandle = sp<gui::WindowInfoHandle>::make(inputInfo);
487 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800488 if (proto.what() & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000489 layer.bgColor.a = proto.bg_color_alpha();
Vishnu Nair6b591152021-10-08 11:45:14 -0700490 layer.bgColorDataspace = static_cast<ui::Dataspace>(proto.bg_color_dataspace());
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000491 const perfetto::protos::LayerState_Color3& colorProto = proto.color();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000492 layer.bgColor.r = colorProto.r();
493 layer.bgColor.g = colorProto.g();
494 layer.bgColor.b = colorProto.b();
Vishnu Nair6b591152021-10-08 11:45:14 -0700495 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800496 if (proto.what() & layer_state_t::eColorSpaceAgnosticChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700497 layer.colorSpaceAgnostic = proto.color_space_agnostic();
498 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800499 if (proto.what() & layer_state_t::eShadowRadiusChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700500 layer.shadowRadius = proto.shadow_radius();
501 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800502 if (proto.what() & layer_state_t::eFrameRateSelectionPriority) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700503 layer.frameRateSelectionPriority = proto.frame_rate_selection_priority();
504 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800505 if (proto.what() & layer_state_t::eFrameRateChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700506 layer.frameRate = proto.frame_rate();
507 layer.frameRateCompatibility = static_cast<int8_t>(proto.frame_rate_compatibility());
508 layer.changeFrameRateStrategy = static_cast<int8_t>(proto.change_frame_rate_strategy());
509 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800510 if (proto.what() & layer_state_t::eFixedTransformHintChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700511 layer.fixedTransformHint =
512 static_cast<ui::Transform::RotationFlags>(proto.fixed_transform_hint());
513 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800514 if (proto.what() & layer_state_t::eAutoRefreshChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700515 layer.autoRefresh = proto.auto_refresh();
516 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800517 if (proto.what() & layer_state_t::eTrustedOverlayChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700518 layer.isTrustedOverlay = proto.is_trusted_overlay();
519 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800520 if (proto.what() & layer_state_t::eBufferCropChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700521 LayerProtoHelper::readFromProto(proto.buffer_crop(), layer.bufferCrop);
522 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800523 if (proto.what() & layer_state_t::eDestinationFrameChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700524 LayerProtoHelper::readFromProto(proto.destination_frame(), layer.destinationFrame);
525 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800526 if (proto.what() & layer_state_t::eDropInputModeChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700527 layer.dropInputMode = static_cast<gui::DropInputMode>(proto.drop_input_mode());
528 }
Vishnu Nair6b591152021-10-08 11:45:14 -0700529}
530
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000531DisplayState TransactionProtoParser::fromProto(const perfetto::protos::DisplayState& proto) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700532 DisplayState display;
533 display.what = proto.what();
Vishnu Nair685cfef2022-02-02 10:01:25 -0800534 display.token = mMapper->getDisplayHandle(proto.id());
Vishnu Nair6b591152021-10-08 11:45:14 -0700535
536 if (display.what & DisplayState::eLayerStackChanged) {
537 display.layerStack.id = proto.layer_stack();
538 }
539 if (display.what & DisplayState::eDisplayProjectionChanged) {
540 display.orientation = static_cast<ui::Rotation>(proto.orientation());
541 LayerProtoHelper::readFromProto(proto.oriented_display_space_rect(),
542 display.orientedDisplaySpaceRect);
543 LayerProtoHelper::readFromProto(proto.layer_stack_space_rect(),
544 display.layerStackSpaceRect);
545 }
546 if (display.what & DisplayState::eDisplaySizeChanged) {
547 display.width = proto.width();
548 display.height = proto.height();
549 }
550 if (display.what & DisplayState::eFlagsChanged) {
551 display.flags = proto.flags();
552 }
553 return display;
554}
555
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000556void asProto(perfetto::protos::Transform* proto, const ui::Transform& transform) {
Vishnu Nair81750622023-03-08 15:02:06 -0800557 proto->set_dsdx(transform.dsdx());
558 proto->set_dtdx(transform.dtdx());
559 proto->set_dtdy(transform.dtdy());
560 proto->set_dsdy(transform.dsdy());
561 proto->set_tx(transform.tx());
562 proto->set_ty(transform.ty());
563}
564
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000565perfetto::protos::DisplayInfo TransactionProtoParser::toProto(
566 const frontend::DisplayInfo& displayInfo, uint32_t layerStack) {
567 perfetto::protos::DisplayInfo proto;
Vishnu Nair81750622023-03-08 15:02:06 -0800568 proto.set_layer_stack(layerStack);
569 proto.set_display_id(displayInfo.info.displayId);
570 proto.set_logical_width(displayInfo.info.logicalWidth);
571 proto.set_logical_height(displayInfo.info.logicalHeight);
572 asProto(proto.mutable_transform_inverse(), displayInfo.info.transform);
573 asProto(proto.mutable_transform(), displayInfo.transform);
574 proto.set_receives_input(displayInfo.receivesInput);
575 proto.set_is_secure(displayInfo.isSecure);
576 proto.set_is_primary(displayInfo.isPrimary);
577 proto.set_is_virtual(displayInfo.isVirtual);
578 proto.set_rotation_flags((int)displayInfo.rotationFlags);
579 proto.set_transform_hint((int)displayInfo.transformHint);
580 return proto;
581}
582
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000583void fromProto2(ui::Transform& outTransform, const perfetto::protos::Transform& proto) {
Vishnu Nair81750622023-03-08 15:02:06 -0800584 outTransform.set(proto.dsdx(), proto.dtdx(), proto.dtdy(), proto.dsdy());
585 outTransform.set(proto.tx(), proto.ty());
586}
587
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000588frontend::DisplayInfo TransactionProtoParser::fromProto(
589 const perfetto::protos::DisplayInfo& proto) {
Vishnu Nair81750622023-03-08 15:02:06 -0800590 frontend::DisplayInfo displayInfo;
591 displayInfo.info.displayId = proto.display_id();
592 displayInfo.info.logicalWidth = proto.logical_width();
593 displayInfo.info.logicalHeight = proto.logical_height();
594 fromProto2(displayInfo.info.transform, proto.transform_inverse());
595 fromProto2(displayInfo.transform, proto.transform());
596 displayInfo.receivesInput = proto.receives_input();
597 displayInfo.isSecure = proto.is_secure();
598 displayInfo.isPrimary = proto.is_primary();
Vishnu Nair0d13b912023-03-27 22:06:38 +0000599 displayInfo.isVirtual = proto.is_virtual();
Vishnu Nair81750622023-03-08 15:02:06 -0800600 displayInfo.rotationFlags = (ui::Transform::RotationFlags)proto.rotation_flags();
601 displayInfo.transformHint = (ui::Transform::RotationFlags)proto.transform_hint();
602 return displayInfo;
603}
604
605void TransactionProtoParser::fromProto(
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000606 const google::protobuf::RepeatedPtrField<perfetto::protos::DisplayInfo>& proto,
Dominik Laskowski6b049ff2023-01-29 15:46:45 -0500607 frontend::DisplayInfos& outDisplayInfos) {
Vishnu Nair81750622023-03-08 15:02:06 -0800608 outDisplayInfos.clear();
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000609 for (const perfetto::protos::DisplayInfo& displayInfo : proto) {
Vishnu Nair81750622023-03-08 15:02:06 -0800610 outDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(displayInfo.layer_stack()),
611 fromProto(displayInfo));
612 }
613}
614
Vishnu Nair6b591152021-10-08 11:45:14 -0700615} // namespace android::surfaceflinger