blob: b1895989ec4137ad2208eef4a91dbb46efde9ac0 [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) {
Yi Kong01b81262024-08-12 07:30:15 +080065 proto.mutable_layer_changes()->Add(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) {
Yi Kong01b81262024-08-12 07:30:15 +080070 proto.mutable_display_changes()->Add(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) {
Vishnu Nair9e0017e2024-05-22 19:02:44 +0000250 proto.set_is_trusted_overlay(layer.trustedOverlay == gui::TrustedOverlay::ENABLED);
251 // TODO(b/339701674) update protos
Vishnu Nair6b591152021-10-08 11:45:14 -0700252 }
253 if (layer.what & layer_state_t::eBufferCropChanged) {
254 LayerProtoHelper::writeToProto(layer.bufferCrop, proto.mutable_buffer_crop());
255 }
256 if (layer.what & layer_state_t::eDestinationFrameChanged) {
257 LayerProtoHelper::writeToProto(layer.destinationFrame, proto.mutable_destination_frame());
258 }
259 if (layer.what & layer_state_t::eDropInputModeChanged) {
260 proto.set_drop_input_mode(
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000261 static_cast<perfetto::protos::LayerState_DropInputMode>(layer.dropInputMode));
Vishnu Nair6b591152021-10-08 11:45:14 -0700262 }
263 return proto;
264}
265
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000266perfetto::protos::DisplayState TransactionProtoParser::toProto(const DisplayState& display) {
267 perfetto::protos::DisplayState proto;
Vishnu Nair6b591152021-10-08 11:45:14 -0700268 proto.set_what(display.what);
Vishnu Nair685cfef2022-02-02 10:01:25 -0800269 proto.set_id(mMapper->getDisplayId(display.token));
Vishnu Nair6b591152021-10-08 11:45:14 -0700270
271 if (display.what & DisplayState::eLayerStackChanged) {
272 proto.set_layer_stack(display.layerStack.id);
273 }
274 if (display.what & DisplayState::eDisplayProjectionChanged) {
275 proto.set_orientation(static_cast<uint32_t>(display.orientation));
276 LayerProtoHelper::writeToProto(display.orientedDisplaySpaceRect,
277 proto.mutable_oriented_display_space_rect());
278 LayerProtoHelper::writeToProto(display.layerStackSpaceRect,
279 proto.mutable_layer_stack_space_rect());
280 }
281 if (display.what & DisplayState::eDisplaySizeChanged) {
282 proto.set_width(display.width);
283 proto.set_height(display.height);
284 }
285 if (display.what & DisplayState::eFlagsChanged) {
286 proto.set_flags(display.flags);
287 }
288 return proto;
289}
290
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000291perfetto::protos::LayerCreationArgs TransactionProtoParser::toProto(const LayerCreationArgs& args) {
292 perfetto::protos::LayerCreationArgs proto;
Vishnu Nair81750622023-03-08 15:02:06 -0800293 proto.set_layer_id(args.sequence);
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800294 proto.set_name(args.name);
295 proto.set_flags(args.flags);
296 proto.set_parent_id(args.parentId);
Vishnu Nair81750622023-03-08 15:02:06 -0800297 proto.set_mirror_from_id(args.layerIdToMirror);
298 proto.set_add_to_root(args.addToRoot);
299 proto.set_layer_stack_to_mirror(args.layerStackToMirror.id);
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800300 return proto;
301}
302
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000303TransactionState TransactionProtoParser::fromProto(
304 const perfetto::protos::TransactionState& proto) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700305 TransactionState t;
306 t.originPid = proto.pid();
307 t.originUid = proto.uid();
308 t.frameTimelineInfo.vsyncId = proto.vsync_id();
309 t.frameTimelineInfo.inputEventId = proto.input_event_id();
310 t.postTime = proto.post_time();
Vishnu Naird37343b2022-01-12 16:18:56 -0800311 t.id = proto.transaction_id();
312
Vishnu Nair6b591152021-10-08 11:45:14 -0700313 int32_t layerCount = proto.layer_changes_size();
314 t.states.reserve(static_cast<size_t>(layerCount));
315 for (int i = 0; i < layerCount; i++) {
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000316 ResolvedComposerState s;
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800317 s.state.what = 0;
Vishnu Nair81750622023-03-08 15:02:06 -0800318 fromProto(proto.layer_changes(i), s);
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000319 t.states.emplace_back(s);
Vishnu Nair6b591152021-10-08 11:45:14 -0700320 }
321
322 int32_t displayCount = proto.display_changes_size();
323 t.displays.reserve(static_cast<size_t>(displayCount));
324 for (int i = 0; i < displayCount; i++) {
Vishnu Nair685cfef2022-02-02 10:01:25 -0800325 t.displays.add(fromProto(proto.display_changes(i)));
Vishnu Nair6b591152021-10-08 11:45:14 -0700326 }
327 return t;
328}
329
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000330void TransactionProtoParser::fromProto(const perfetto::protos::LayerCreationArgs& proto,
Vishnu Nair81750622023-03-08 15:02:06 -0800331 LayerCreationArgs& outArgs) {
332 outArgs.sequence = proto.layer_id();
333
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800334 outArgs.name = proto.name();
335 outArgs.flags = proto.flags();
336 outArgs.parentId = proto.parent_id();
Vishnu Nair81750622023-03-08 15:02:06 -0800337 outArgs.layerIdToMirror = proto.mirror_from_id();
338 outArgs.addToRoot = proto.add_to_root();
339 outArgs.layerStackToMirror.id = proto.layer_stack_to_mirror();
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800340}
Vishnu Nair6b591152021-10-08 11:45:14 -0700341
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000342void TransactionProtoParser::mergeFromProto(const perfetto::protos::LayerState& proto,
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800343 TracingLayerState& outState) {
Vishnu Nair81750622023-03-08 15:02:06 -0800344 ResolvedComposerState resolvedComposerState;
345 fromProto(proto, resolvedComposerState);
346 layer_state_t& state = resolvedComposerState.state;
347 outState.state.merge(state);
348 outState.layerId = resolvedComposerState.layerId;
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800349
350 if (state.what & layer_state_t::eReparent) {
Vishnu Nair81750622023-03-08 15:02:06 -0800351 outState.parentId = resolvedComposerState.parentId;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800352 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800353 if (state.what & layer_state_t::eRelativeLayerChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800354 outState.relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800355 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800356 if (state.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800357 outState.touchCropId = resolvedComposerState.touchCropId;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800358 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800359 if (state.what & layer_state_t::eBufferChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800360 outState.externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800361 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800362 if (state.what & layer_state_t::eSidebandStreamChanged) {
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800363 outState.hasSidebandStream = proto.has_sideband_stream();
364 }
365}
366
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000367void TransactionProtoParser::fromProto(const perfetto::protos::LayerState& proto,
Vishnu Nair81750622023-03-08 15:02:06 -0800368 ResolvedComposerState& resolvedComposerState) {
369 auto& layer = resolvedComposerState.state;
370 resolvedComposerState.layerId = proto.layer_id();
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800371 layer.what |= proto.what();
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800372
373 if (proto.what() & layer_state_t::ePositionChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700374 layer.x = proto.x();
375 layer.y = proto.y();
376 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800377 if (proto.what() & layer_state_t::eLayerChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700378 layer.z = proto.z();
379 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800380 if (proto.what() & layer_state_t::eLayerStackChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700381 layer.layerStack.id = proto.layer_stack();
382 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800383 if (proto.what() & layer_state_t::eFlagsChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700384 layer.flags = proto.flags();
385 layer.mask = proto.mask();
386 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800387 if (proto.what() & layer_state_t::eMatrixChanged) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000388 const perfetto::protos::LayerState_Matrix22& matrixProto = proto.matrix();
Vishnu Nair6b591152021-10-08 11:45:14 -0700389 layer.matrix.dsdx = matrixProto.dsdx();
390 layer.matrix.dsdy = matrixProto.dsdy();
391 layer.matrix.dtdx = matrixProto.dtdx();
392 layer.matrix.dtdy = matrixProto.dtdy();
393 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800394 if (proto.what() & layer_state_t::eCornerRadiusChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700395 layer.cornerRadius = proto.corner_radius();
396 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800397 if (proto.what() & layer_state_t::eBackgroundBlurRadiusChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700398 layer.backgroundBlurRadius = proto.background_blur_radius();
399 }
400
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800401 if (proto.what() & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +0000402 layer.color.a = proto.alpha();
Vishnu Nair6b591152021-10-08 11:45:14 -0700403 }
404
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800405 if (proto.what() & layer_state_t::eColorChanged) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000406 const perfetto::protos::LayerState_Color3& colorProto = proto.color();
Vishnu Nair6b591152021-10-08 11:45:14 -0700407 layer.color.r = colorProto.r();
408 layer.color.g = colorProto.g();
409 layer.color.b = colorProto.b();
410 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800411 if (proto.what() & layer_state_t::eTransparentRegionChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700412 LayerProtoHelper::readFromProto(proto.transparent_region(), layer.transparentRegion);
413 }
Vishnu Nairbbceb462022-10-10 04:52:13 +0000414 if (proto.what() & layer_state_t::eBufferTransformChanged) {
415 layer.bufferTransform = proto.transform();
Vishnu Nair6b591152021-10-08 11:45:14 -0700416 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800417 if (proto.what() & layer_state_t::eTransformToDisplayInverseChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700418 layer.transformToDisplayInverse = proto.transform_to_display_inverse();
419 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800420 if (proto.what() & layer_state_t::eCropChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700421 LayerProtoHelper::readFromProto(proto.crop(), layer.crop);
422 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800423 if (proto.what() & layer_state_t::eBufferChanged) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000424 const perfetto::protos::LayerState_BufferData& bufferProto = proto.buffer_data();
Vishnu Nair685cfef2022-02-02 10:01:25 -0800425 layer.bufferData =
Vishnu Nair81750622023-03-08 15:02:06 -0800426 std::make_shared<fake::BufferData>(bufferProto.buffer_id(), bufferProto.width(),
427 bufferProto.height(), bufferProto.pixel_format(),
428 bufferProto.usage());
429 resolvedComposerState.externalTexture =
430 std::make_shared<FakeExternalTexture>(layer.bufferData->getWidth(),
431 layer.bufferData->getHeight(),
432 layer.bufferData->getId(),
433 layer.bufferData->getPixelFormat(),
434 layer.bufferData->getUsage());
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800435 layer.bufferData->frameNumber = bufferProto.frame_number();
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700436 layer.bufferData->flags = ftl::Flags<BufferData::BufferDataChange>(bufferProto.flags());
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800437 layer.bufferData->cachedBuffer.id = bufferProto.cached_buffer_id();
Vishnu Nair8eebba42022-02-25 07:57:15 -0800438 layer.bufferData->acquireFence = Fence::NO_FENCE;
Nergi Rahardi39f510f2024-05-23 15:16:54 +0900439 layer.bufferData->dequeueTime = -1;
Vishnu Nair6b591152021-10-08 11:45:14 -0700440 }
Vishnu Nair6b591152021-10-08 11:45:14 -0700441
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800442 if (proto.what() & layer_state_t::eApiChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700443 layer.api = proto.api();
444 }
445
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800446 if (proto.what() & layer_state_t::eColorTransformChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700447 LayerProtoHelper::readFromProto(proto.color_transform(), layer.colorTransform);
448 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800449 if (proto.what() & layer_state_t::eBlurRegionsChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700450 layer.blurRegions.reserve(static_cast<size_t>(proto.blur_regions_size()));
451 for (int i = 0; i < proto.blur_regions_size(); i++) {
452 android::BlurRegion region;
453 LayerProtoHelper::readFromProto(proto.blur_regions(i), region);
454 layer.blurRegions.push_back(region);
455 }
456 }
457
Vishnu Nair685cfef2022-02-02 10:01:25 -0800458 if (proto.what() & layer_state_t::eReparent) {
Vishnu Nair81750622023-03-08 15:02:06 -0800459 resolvedComposerState.parentId = proto.parent_id();
Vishnu Nair6b591152021-10-08 11:45:14 -0700460 }
Vishnu Naird37343b2022-01-12 16:18:56 -0800461 if (proto.what() & layer_state_t::eRelativeLayerChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800462 resolvedComposerState.relativeParentId = proto.relative_parent_id();
Vishnu Naird37343b2022-01-12 16:18:56 -0800463 layer.z = proto.z();
Vishnu Nair6b591152021-10-08 11:45:14 -0700464 }
465
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800466 if ((proto.what() & layer_state_t::eInputInfoChanged) && proto.has_window_info_handle()) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700467 gui::WindowInfo inputInfo;
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000468 const perfetto::protos::LayerState_WindowInfo& windowInfoProto = proto.window_info_handle();
Vishnu Nair6b591152021-10-08 11:45:14 -0700469
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800470 inputInfo.layoutParamsFlags =
471 static_cast<gui::WindowInfo::Flag>(windowInfoProto.layout_params_flags());
472 inputInfo.layoutParamsType =
473 static_cast<gui::WindowInfo::Type>(windowInfoProto.layout_params_type());
Vishnu Nair6b591152021-10-08 11:45:14 -0700474 LayerProtoHelper::readFromProto(windowInfoProto.touchable_region(),
475 inputInfo.touchableRegion);
Siarhei Vishniakoue2eff112023-06-21 14:20:39 -0700476 inputInfo.inputConfig =
477 ftl::Flags<gui::WindowInfo::InputConfig>(windowInfoProto.input_config());
Vishnu Nair6b591152021-10-08 11:45:14 -0700478 inputInfo.surfaceInset = windowInfoProto.surface_inset();
Vishnu Nair6b591152021-10-08 11:45:14 -0700479 inputInfo.globalScaleFactor = windowInfoProto.global_scale_factor();
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000480 const perfetto::protos::Transform& transformProto = windowInfoProto.transform();
Vishnu Nair6b591152021-10-08 11:45:14 -0700481 inputInfo.transform.set(transformProto.dsdx(), transformProto.dtdx(), transformProto.dtdy(),
482 transformProto.dsdy());
483 inputInfo.transform.set(transformProto.tx(), transformProto.ty());
484 inputInfo.replaceTouchableRegionWithCrop =
485 windowInfoProto.replace_touchable_region_with_crop();
Vishnu Nair81750622023-03-08 15:02:06 -0800486 resolvedComposerState.touchCropId = windowInfoProto.crop_layer_id();
Vishnu Nair286f4f92022-06-08 16:37:39 -0700487
Vishnu Nair6b591152021-10-08 11:45:14 -0700488 layer.windowInfoHandle = sp<gui::WindowInfoHandle>::make(inputInfo);
489 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800490 if (proto.what() & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000491 layer.bgColor.a = proto.bg_color_alpha();
Vishnu Nair6b591152021-10-08 11:45:14 -0700492 layer.bgColorDataspace = static_cast<ui::Dataspace>(proto.bg_color_dataspace());
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000493 const perfetto::protos::LayerState_Color3& colorProto = proto.color();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000494 layer.bgColor.r = colorProto.r();
495 layer.bgColor.g = colorProto.g();
496 layer.bgColor.b = colorProto.b();
Vishnu Nair6b591152021-10-08 11:45:14 -0700497 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800498 if (proto.what() & layer_state_t::eColorSpaceAgnosticChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700499 layer.colorSpaceAgnostic = proto.color_space_agnostic();
500 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800501 if (proto.what() & layer_state_t::eShadowRadiusChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700502 layer.shadowRadius = proto.shadow_radius();
503 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800504 if (proto.what() & layer_state_t::eFrameRateSelectionPriority) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700505 layer.frameRateSelectionPriority = proto.frame_rate_selection_priority();
506 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800507 if (proto.what() & layer_state_t::eFrameRateChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700508 layer.frameRate = proto.frame_rate();
509 layer.frameRateCompatibility = static_cast<int8_t>(proto.frame_rate_compatibility());
510 layer.changeFrameRateStrategy = static_cast<int8_t>(proto.change_frame_rate_strategy());
511 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800512 if (proto.what() & layer_state_t::eFixedTransformHintChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700513 layer.fixedTransformHint =
514 static_cast<ui::Transform::RotationFlags>(proto.fixed_transform_hint());
515 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800516 if (proto.what() & layer_state_t::eAutoRefreshChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700517 layer.autoRefresh = proto.auto_refresh();
518 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800519 if (proto.what() & layer_state_t::eTrustedOverlayChanged) {
Vishnu Nair9e0017e2024-05-22 19:02:44 +0000520 layer.trustedOverlay = proto.is_trusted_overlay() ? gui::TrustedOverlay::ENABLED
521 : gui::TrustedOverlay::UNSET;
Vishnu Nair6b591152021-10-08 11:45:14 -0700522 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800523 if (proto.what() & layer_state_t::eBufferCropChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700524 LayerProtoHelper::readFromProto(proto.buffer_crop(), layer.bufferCrop);
525 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800526 if (proto.what() & layer_state_t::eDestinationFrameChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700527 LayerProtoHelper::readFromProto(proto.destination_frame(), layer.destinationFrame);
528 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800529 if (proto.what() & layer_state_t::eDropInputModeChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700530 layer.dropInputMode = static_cast<gui::DropInputMode>(proto.drop_input_mode());
531 }
Vishnu Nair6b591152021-10-08 11:45:14 -0700532}
533
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000534DisplayState TransactionProtoParser::fromProto(const perfetto::protos::DisplayState& proto) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700535 DisplayState display;
536 display.what = proto.what();
Vishnu Nair685cfef2022-02-02 10:01:25 -0800537 display.token = mMapper->getDisplayHandle(proto.id());
Vishnu Nair6b591152021-10-08 11:45:14 -0700538
539 if (display.what & DisplayState::eLayerStackChanged) {
540 display.layerStack.id = proto.layer_stack();
541 }
542 if (display.what & DisplayState::eDisplayProjectionChanged) {
543 display.orientation = static_cast<ui::Rotation>(proto.orientation());
544 LayerProtoHelper::readFromProto(proto.oriented_display_space_rect(),
545 display.orientedDisplaySpaceRect);
546 LayerProtoHelper::readFromProto(proto.layer_stack_space_rect(),
547 display.layerStackSpaceRect);
548 }
549 if (display.what & DisplayState::eDisplaySizeChanged) {
550 display.width = proto.width();
551 display.height = proto.height();
552 }
553 if (display.what & DisplayState::eFlagsChanged) {
554 display.flags = proto.flags();
555 }
556 return display;
557}
558
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000559void asProto(perfetto::protos::Transform* proto, const ui::Transform& transform) {
Vishnu Nair81750622023-03-08 15:02:06 -0800560 proto->set_dsdx(transform.dsdx());
561 proto->set_dtdx(transform.dtdx());
562 proto->set_dtdy(transform.dtdy());
563 proto->set_dsdy(transform.dsdy());
564 proto->set_tx(transform.tx());
565 proto->set_ty(transform.ty());
566}
567
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000568perfetto::protos::DisplayInfo TransactionProtoParser::toProto(
569 const frontend::DisplayInfo& displayInfo, uint32_t layerStack) {
570 perfetto::protos::DisplayInfo proto;
Vishnu Nair81750622023-03-08 15:02:06 -0800571 proto.set_layer_stack(layerStack);
Linnan Li13bf76a2024-05-05 19:18:02 +0800572 proto.set_display_id(displayInfo.info.displayId.val());
Vishnu Nair81750622023-03-08 15:02:06 -0800573 proto.set_logical_width(displayInfo.info.logicalWidth);
574 proto.set_logical_height(displayInfo.info.logicalHeight);
575 asProto(proto.mutable_transform_inverse(), displayInfo.info.transform);
576 asProto(proto.mutable_transform(), displayInfo.transform);
577 proto.set_receives_input(displayInfo.receivesInput);
578 proto.set_is_secure(displayInfo.isSecure);
579 proto.set_is_primary(displayInfo.isPrimary);
580 proto.set_is_virtual(displayInfo.isVirtual);
581 proto.set_rotation_flags((int)displayInfo.rotationFlags);
582 proto.set_transform_hint((int)displayInfo.transformHint);
583 return proto;
584}
585
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000586void fromProto2(ui::Transform& outTransform, const perfetto::protos::Transform& proto) {
Vishnu Nair81750622023-03-08 15:02:06 -0800587 outTransform.set(proto.dsdx(), proto.dtdx(), proto.dtdy(), proto.dsdy());
588 outTransform.set(proto.tx(), proto.ty());
589}
590
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000591frontend::DisplayInfo TransactionProtoParser::fromProto(
592 const perfetto::protos::DisplayInfo& proto) {
Vishnu Nair81750622023-03-08 15:02:06 -0800593 frontend::DisplayInfo displayInfo;
Linnan Li13bf76a2024-05-05 19:18:02 +0800594 displayInfo.info.displayId = ui::LogicalDisplayId{proto.display_id()};
Vishnu Nair81750622023-03-08 15:02:06 -0800595 displayInfo.info.logicalWidth = proto.logical_width();
596 displayInfo.info.logicalHeight = proto.logical_height();
597 fromProto2(displayInfo.info.transform, proto.transform_inverse());
598 fromProto2(displayInfo.transform, proto.transform());
599 displayInfo.receivesInput = proto.receives_input();
600 displayInfo.isSecure = proto.is_secure();
601 displayInfo.isPrimary = proto.is_primary();
Vishnu Nair0d13b912023-03-27 22:06:38 +0000602 displayInfo.isVirtual = proto.is_virtual();
Vishnu Nair81750622023-03-08 15:02:06 -0800603 displayInfo.rotationFlags = (ui::Transform::RotationFlags)proto.rotation_flags();
604 displayInfo.transformHint = (ui::Transform::RotationFlags)proto.transform_hint();
605 return displayInfo;
606}
607
608void TransactionProtoParser::fromProto(
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000609 const google::protobuf::RepeatedPtrField<perfetto::protos::DisplayInfo>& proto,
Dominik Laskowski6b049ff2023-01-29 15:46:45 -0500610 frontend::DisplayInfos& outDisplayInfos) {
Vishnu Nair81750622023-03-08 15:02:06 -0800611 outDisplayInfos.clear();
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000612 for (const perfetto::protos::DisplayInfo& displayInfo : proto) {
Vishnu Nair81750622023-03-08 15:02:06 -0800613 outDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(displayInfo.layer_stack()),
614 fromProto(displayInfo));
615 }
616}
617
Vishnu Nair6b591152021-10-08 11:45:14 -0700618} // namespace android::surfaceflinger