blob: 8fd653880c41f55d45b08c0f16bff24b580bac1c [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 {
30 const sp<GraphicBuffer> mEmptyBuffer =
31 sp<GraphicBuffer>::make(1u, 1u, PIXEL_FORMAT_RGBA_8888,
32 GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_OFTEN);
33 uint32_t mWidth;
34 uint32_t mHeight;
35 uint64_t mId;
36 PixelFormat mPixelFormat;
37 uint64_t mUsage;
38
39public:
40 FakeExternalTexture(uint32_t width, uint32_t height, uint64_t id, PixelFormat pixelFormat,
41 uint64_t usage)
42 : mWidth(width), mHeight(height), mId(id), mPixelFormat(pixelFormat), mUsage(usage) {}
43 const sp<GraphicBuffer>& getBuffer() const { return mEmptyBuffer; }
44 bool hasSameBuffer(const renderengine::ExternalTexture& other) const override {
45 return getId() == other.getId();
46 }
47 uint32_t getWidth() const override { return mWidth; }
48 uint32_t getHeight() const override { return mHeight; }
49 uint64_t getId() const override { return mId; }
50 PixelFormat getPixelFormat() const override { return mPixelFormat; }
51 uint64_t getUsage() const override { return mUsage; }
52 ~FakeExternalTexture() = default;
53};
54
Vishnu Nair685cfef2022-02-02 10:01:25 -080055proto::TransactionState TransactionProtoParser::toProto(const TransactionState& t) {
Vishnu Nair6b591152021-10-08 11:45:14 -070056 proto::TransactionState proto;
57 proto.set_pid(t.originPid);
58 proto.set_uid(t.originUid);
59 proto.set_vsync_id(t.frameTimelineInfo.vsyncId);
60 proto.set_input_event_id(t.frameTimelineInfo.inputEventId);
61 proto.set_post_time(t.postTime);
Vishnu Naird37343b2022-01-12 16:18:56 -080062 proto.set_transaction_id(t.id);
Vishnu Nair6b591152021-10-08 11:45:14 -070063
Vishnu Nair685cfef2022-02-02 10:01:25 -080064 proto.mutable_layer_changes()->Reserve(static_cast<int32_t>(t.states.size()));
Vishnu Nair6b591152021-10-08 11:45:14 -070065 for (auto& layerState : t.states) {
Vishnu Nair81750622023-03-08 15:02:06 -080066 proto.mutable_layer_changes()->Add(std::move(toProto(layerState)));
Vishnu Nair6b591152021-10-08 11:45:14 -070067 }
68
Vishnu Nair685cfef2022-02-02 10:01:25 -080069 proto.mutable_display_changes()->Reserve(static_cast<int32_t>(t.displays.size()));
Vishnu Nair6b591152021-10-08 11:45:14 -070070 for (auto& displayState : t.displays) {
Vishnu Nair685cfef2022-02-02 10:01:25 -080071 proto.mutable_display_changes()->Add(std::move(toProto(displayState)));
Vishnu Nair6b591152021-10-08 11:45:14 -070072 }
73 return proto;
74}
75
Vishnu Nair68dee2b2021-11-08 18:52:12 -080076proto::TransactionState TransactionProtoParser::toProto(
Vishnu Nair81750622023-03-08 15:02:06 -080077 const std::map<uint32_t /* layerId */, TracingLayerState>& states) {
Vishnu Nair68dee2b2021-11-08 18:52:12 -080078 proto::TransactionState proto;
Vishnu Nair685cfef2022-02-02 10:01:25 -080079 proto.mutable_layer_changes()->Reserve(static_cast<int32_t>(states.size()));
Vishnu Nair68dee2b2021-11-08 18:52:12 -080080 for (auto& [layerId, state] : states) {
Vishnu Nair685cfef2022-02-02 10:01:25 -080081 proto::LayerState layerProto = toProto(state);
Vishnu Nair68dee2b2021-11-08 18:52:12 -080082 layerProto.set_has_sideband_stream(state.hasSidebandStream);
Vishnu Nair68dee2b2021-11-08 18:52:12 -080083 proto.mutable_layer_changes()->Add(std::move(layerProto));
84 }
85 return proto;
86}
87
Vishnu Nair81750622023-03-08 15:02:06 -080088proto::LayerState TransactionProtoParser::toProto(
89 const ResolvedComposerState& resolvedComposerState) {
Vishnu Nair6b591152021-10-08 11:45:14 -070090 proto::LayerState proto;
Vishnu Nair81750622023-03-08 15:02:06 -080091 auto& layer = resolvedComposerState.state;
92 proto.set_layer_id(resolvedComposerState.layerId);
Vishnu Nair6b591152021-10-08 11:45:14 -070093 proto.set_what(layer.what);
94
95 if (layer.what & layer_state_t::ePositionChanged) {
96 proto.set_x(layer.x);
97 proto.set_y(layer.y);
98 }
99 if (layer.what & layer_state_t::eLayerChanged) {
100 proto.set_z(layer.z);
101 }
Vishnu Nairea04b6f2022-08-19 21:28:17 +0000102
Vishnu Nair6b591152021-10-08 11:45:14 -0700103 if (layer.what & layer_state_t::eLayerStackChanged) {
104 proto.set_layer_stack(layer.layerStack.id);
105 }
106 if (layer.what & layer_state_t::eFlagsChanged) {
107 proto.set_flags(layer.flags);
108 proto.set_mask(layer.mask);
109 }
110 if (layer.what & layer_state_t::eMatrixChanged) {
111 proto::LayerState_Matrix22* matrixProto = proto.mutable_matrix();
112 matrixProto->set_dsdx(layer.matrix.dsdx);
113 matrixProto->set_dsdy(layer.matrix.dsdy);
114 matrixProto->set_dtdx(layer.matrix.dtdx);
115 matrixProto->set_dtdy(layer.matrix.dtdy);
116 }
117 if (layer.what & layer_state_t::eCornerRadiusChanged) {
118 proto.set_corner_radius(layer.cornerRadius);
119 }
120 if (layer.what & layer_state_t::eBackgroundBlurRadiusChanged) {
121 proto.set_background_blur_radius(layer.backgroundBlurRadius);
122 }
123
124 if (layer.what & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +0000125 proto.set_alpha(layer.color.a);
Vishnu Nair6b591152021-10-08 11:45:14 -0700126 }
127
128 if (layer.what & layer_state_t::eColorChanged) {
129 proto::LayerState_Color3* colorProto = proto.mutable_color();
130 colorProto->set_r(layer.color.r);
131 colorProto->set_g(layer.color.g);
132 colorProto->set_b(layer.color.b);
133 }
134 if (layer.what & layer_state_t::eTransparentRegionChanged) {
135 LayerProtoHelper::writeToProto(layer.transparentRegion, proto.mutable_transparent_region());
136 }
Vishnu Nairbbceb462022-10-10 04:52:13 +0000137 if (layer.what & layer_state_t::eBufferTransformChanged) {
138 proto.set_transform(layer.bufferTransform);
Vishnu Nair6b591152021-10-08 11:45:14 -0700139 }
140 if (layer.what & layer_state_t::eTransformToDisplayInverseChanged) {
141 proto.set_transform_to_display_inverse(layer.transformToDisplayInverse);
142 }
143 if (layer.what & layer_state_t::eCropChanged) {
144 LayerProtoHelper::writeToProto(layer.crop, proto.mutable_crop());
145 }
146 if (layer.what & layer_state_t::eBufferChanged) {
147 proto::LayerState_BufferData* bufferProto = proto.mutable_buffer_data();
Vishnu Nair81750622023-03-08 15:02:06 -0800148 if (resolvedComposerState.externalTexture) {
149 bufferProto->set_buffer_id(resolvedComposerState.externalTexture->getId());
150 bufferProto->set_width(resolvedComposerState.externalTexture->getWidth());
151 bufferProto->set_height(resolvedComposerState.externalTexture->getHeight());
Vishnu Naird37343b2022-01-12 16:18:56 -0800152 bufferProto->set_pixel_format(static_cast<proto::LayerState_BufferData_PixelFormat>(
Vishnu Nair81750622023-03-08 15:02:06 -0800153 resolvedComposerState.externalTexture->getPixelFormat()));
154 bufferProto->set_usage(resolvedComposerState.externalTexture->getUsage());
Vishnu Nair6b591152021-10-08 11:45:14 -0700155 }
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800156 bufferProto->set_frame_number(layer.bufferData->frameNumber);
157 bufferProto->set_flags(layer.bufferData->flags.get());
158 bufferProto->set_cached_buffer_id(layer.bufferData->cachedBuffer.id);
Vishnu Nair6b591152021-10-08 11:45:14 -0700159 }
160 if (layer.what & layer_state_t::eSidebandStreamChanged) {
161 proto.set_has_sideband_stream(layer.sidebandStream != nullptr);
162 }
163
164 if (layer.what & layer_state_t::eApiChanged) {
165 proto.set_api(layer.api);
166 }
167
168 if (layer.what & layer_state_t::eColorTransformChanged) {
169 LayerProtoHelper::writeToProto(layer.colorTransform, proto.mutable_color_transform());
170 }
171 if (layer.what & layer_state_t::eBlurRegionsChanged) {
172 for (auto& region : layer.blurRegions) {
173 LayerProtoHelper::writeToProto(region, proto.add_blur_regions());
174 }
175 }
176
Vishnu Nair685cfef2022-02-02 10:01:25 -0800177 if (layer.what & layer_state_t::eReparent) {
Vishnu Nair81750622023-03-08 15:02:06 -0800178 proto.set_parent_id(resolvedComposerState.parentId);
Vishnu Nair6b591152021-10-08 11:45:14 -0700179 }
Vishnu Nair685cfef2022-02-02 10:01:25 -0800180 if (layer.what & layer_state_t::eRelativeLayerChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800181 proto.set_relative_parent_id(resolvedComposerState.relativeParentId);
Vishnu Naird37343b2022-01-12 16:18:56 -0800182 proto.set_z(layer.z);
Vishnu Nair6b591152021-10-08 11:45:14 -0700183 }
184
185 if (layer.what & layer_state_t::eInputInfoChanged) {
186 if (layer.windowInfoHandle) {
187 const gui::WindowInfo* inputInfo = layer.windowInfoHandle->getInfo();
188 proto::LayerState_WindowInfo* windowInfoProto = proto.mutable_window_info_handle();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800189 windowInfoProto->set_layout_params_flags(inputInfo->layoutParamsFlags.get());
190 windowInfoProto->set_layout_params_type(
191 static_cast<int32_t>(inputInfo->layoutParamsType));
Vishnu Nair6b591152021-10-08 11:45:14 -0700192 LayerProtoHelper::writeToProto(inputInfo->touchableRegion,
193 windowInfoProto->mutable_touchable_region());
194 windowInfoProto->set_surface_inset(inputInfo->surfaceInset);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800195 windowInfoProto->set_focusable(
196 !inputInfo->inputConfig.test(gui::WindowInfo::InputConfig::NOT_FOCUSABLE));
197 windowInfoProto->set_has_wallpaper(inputInfo->inputConfig.test(
198 gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER));
Vishnu Nair6b591152021-10-08 11:45:14 -0700199 windowInfoProto->set_global_scale_factor(inputInfo->globalScaleFactor);
Vishnu Nair81750622023-03-08 15:02:06 -0800200 proto::Transform* transformProto = windowInfoProto->mutable_transform();
Vishnu Nair6b591152021-10-08 11:45:14 -0700201 transformProto->set_dsdx(inputInfo->transform.dsdx());
202 transformProto->set_dtdx(inputInfo->transform.dtdx());
203 transformProto->set_dtdy(inputInfo->transform.dtdy());
204 transformProto->set_dsdy(inputInfo->transform.dsdy());
205 transformProto->set_tx(inputInfo->transform.tx());
206 transformProto->set_ty(inputInfo->transform.ty());
207 windowInfoProto->set_replace_touchable_region_with_crop(
208 inputInfo->replaceTouchableRegionWithCrop);
Vishnu Nair81750622023-03-08 15:02:06 -0800209 windowInfoProto->set_crop_layer_id(resolvedComposerState.touchCropId);
Vishnu Nair6b591152021-10-08 11:45:14 -0700210 }
211 }
212 if (layer.what & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000213 proto.set_bg_color_alpha(layer.bgColor.a);
Vishnu Nair6b591152021-10-08 11:45:14 -0700214 proto.set_bg_color_dataspace(static_cast<int32_t>(layer.bgColorDataspace));
215 proto::LayerState_Color3* colorProto = proto.mutable_color();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000216 colorProto->set_r(layer.bgColor.r);
217 colorProto->set_g(layer.bgColor.g);
218 colorProto->set_b(layer.bgColor.b);
Vishnu Nair6b591152021-10-08 11:45:14 -0700219 }
220 if (layer.what & layer_state_t::eColorSpaceAgnosticChanged) {
221 proto.set_color_space_agnostic(layer.colorSpaceAgnostic);
222 }
223 if (layer.what & layer_state_t::eShadowRadiusChanged) {
224 proto.set_shadow_radius(layer.shadowRadius);
225 }
226 if (layer.what & layer_state_t::eFrameRateSelectionPriority) {
227 proto.set_frame_rate_selection_priority(layer.frameRateSelectionPriority);
228 }
229 if (layer.what & layer_state_t::eFrameRateChanged) {
230 proto.set_frame_rate(layer.frameRate);
231 proto.set_frame_rate_compatibility(layer.frameRateCompatibility);
232 proto.set_change_frame_rate_strategy(layer.changeFrameRateStrategy);
233 }
234 if (layer.what & layer_state_t::eFixedTransformHintChanged) {
235 proto.set_fixed_transform_hint(layer.fixedTransformHint);
236 }
237 if (layer.what & layer_state_t::eAutoRefreshChanged) {
238 proto.set_auto_refresh(layer.autoRefresh);
239 }
240 if (layer.what & layer_state_t::eTrustedOverlayChanged) {
241 proto.set_is_trusted_overlay(layer.isTrustedOverlay);
242 }
243 if (layer.what & layer_state_t::eBufferCropChanged) {
244 LayerProtoHelper::writeToProto(layer.bufferCrop, proto.mutable_buffer_crop());
245 }
246 if (layer.what & layer_state_t::eDestinationFrameChanged) {
247 LayerProtoHelper::writeToProto(layer.destinationFrame, proto.mutable_destination_frame());
248 }
249 if (layer.what & layer_state_t::eDropInputModeChanged) {
250 proto.set_drop_input_mode(
251 static_cast<proto::LayerState_DropInputMode>(layer.dropInputMode));
252 }
253 return proto;
254}
255
Vishnu Nair685cfef2022-02-02 10:01:25 -0800256proto::DisplayState TransactionProtoParser::toProto(const DisplayState& display) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700257 proto::DisplayState proto;
258 proto.set_what(display.what);
Vishnu Nair685cfef2022-02-02 10:01:25 -0800259 proto.set_id(mMapper->getDisplayId(display.token));
Vishnu Nair6b591152021-10-08 11:45:14 -0700260
261 if (display.what & DisplayState::eLayerStackChanged) {
262 proto.set_layer_stack(display.layerStack.id);
263 }
264 if (display.what & DisplayState::eDisplayProjectionChanged) {
265 proto.set_orientation(static_cast<uint32_t>(display.orientation));
266 LayerProtoHelper::writeToProto(display.orientedDisplaySpaceRect,
267 proto.mutable_oriented_display_space_rect());
268 LayerProtoHelper::writeToProto(display.layerStackSpaceRect,
269 proto.mutable_layer_stack_space_rect());
270 }
271 if (display.what & DisplayState::eDisplaySizeChanged) {
272 proto.set_width(display.width);
273 proto.set_height(display.height);
274 }
275 if (display.what & DisplayState::eFlagsChanged) {
276 proto.set_flags(display.flags);
277 }
278 return proto;
279}
280
Vishnu Nair81750622023-03-08 15:02:06 -0800281proto::LayerCreationArgs TransactionProtoParser::toProto(const LayerCreationArgs& args) {
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800282 proto::LayerCreationArgs proto;
Vishnu Nair81750622023-03-08 15:02:06 -0800283 proto.set_layer_id(args.sequence);
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800284 proto.set_name(args.name);
285 proto.set_flags(args.flags);
286 proto.set_parent_id(args.parentId);
Vishnu Nair81750622023-03-08 15:02:06 -0800287 proto.set_mirror_from_id(args.layerIdToMirror);
288 proto.set_add_to_root(args.addToRoot);
289 proto.set_layer_stack_to_mirror(args.layerStackToMirror.id);
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800290 return proto;
291}
292
Vishnu Nair685cfef2022-02-02 10:01:25 -0800293TransactionState TransactionProtoParser::fromProto(const proto::TransactionState& proto) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700294 TransactionState t;
295 t.originPid = proto.pid();
296 t.originUid = proto.uid();
297 t.frameTimelineInfo.vsyncId = proto.vsync_id();
298 t.frameTimelineInfo.inputEventId = proto.input_event_id();
299 t.postTime = proto.post_time();
Vishnu Naird37343b2022-01-12 16:18:56 -0800300 t.id = proto.transaction_id();
301
Vishnu Nair6b591152021-10-08 11:45:14 -0700302 int32_t layerCount = proto.layer_changes_size();
303 t.states.reserve(static_cast<size_t>(layerCount));
304 for (int i = 0; i < layerCount; i++) {
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000305 ResolvedComposerState s;
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800306 s.state.what = 0;
Vishnu Nair81750622023-03-08 15:02:06 -0800307 fromProto(proto.layer_changes(i), s);
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000308 t.states.emplace_back(s);
Vishnu Nair6b591152021-10-08 11:45:14 -0700309 }
310
311 int32_t displayCount = proto.display_changes_size();
312 t.displays.reserve(static_cast<size_t>(displayCount));
313 for (int i = 0; i < displayCount; i++) {
Vishnu Nair685cfef2022-02-02 10:01:25 -0800314 t.displays.add(fromProto(proto.display_changes(i)));
Vishnu Nair6b591152021-10-08 11:45:14 -0700315 }
316 return t;
317}
318
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800319void TransactionProtoParser::fromProto(const proto::LayerCreationArgs& proto,
Vishnu Nair81750622023-03-08 15:02:06 -0800320 LayerCreationArgs& outArgs) {
321 outArgs.sequence = proto.layer_id();
322
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800323 outArgs.name = proto.name();
324 outArgs.flags = proto.flags();
325 outArgs.parentId = proto.parent_id();
Vishnu Nair81750622023-03-08 15:02:06 -0800326 outArgs.layerIdToMirror = proto.mirror_from_id();
327 outArgs.addToRoot = proto.add_to_root();
328 outArgs.layerStackToMirror.id = proto.layer_stack_to_mirror();
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800329}
Vishnu Nair6b591152021-10-08 11:45:14 -0700330
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800331void TransactionProtoParser::mergeFromProto(const proto::LayerState& proto,
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800332 TracingLayerState& outState) {
Vishnu Nair81750622023-03-08 15:02:06 -0800333 ResolvedComposerState resolvedComposerState;
334 fromProto(proto, resolvedComposerState);
335 layer_state_t& state = resolvedComposerState.state;
336 outState.state.merge(state);
337 outState.layerId = resolvedComposerState.layerId;
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800338
339 if (state.what & layer_state_t::eReparent) {
Vishnu Nair81750622023-03-08 15:02:06 -0800340 outState.parentId = resolvedComposerState.parentId;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800341 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800342 if (state.what & layer_state_t::eRelativeLayerChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800343 outState.relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800344 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800345 if (state.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800346 outState.touchCropId = resolvedComposerState.touchCropId;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800347 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800348 if (state.what & layer_state_t::eBufferChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800349 outState.externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800350 }
Vishnu Nair7b0f18b2022-01-12 16:39:08 -0800351 if (state.what & layer_state_t::eSidebandStreamChanged) {
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800352 outState.hasSidebandStream = proto.has_sideband_stream();
353 }
354}
355
Vishnu Nair81750622023-03-08 15:02:06 -0800356void TransactionProtoParser::fromProto(const proto::LayerState& proto,
357 ResolvedComposerState& resolvedComposerState) {
358 auto& layer = resolvedComposerState.state;
359 resolvedComposerState.layerId = proto.layer_id();
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800360 layer.what |= proto.what();
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800361
362 if (proto.what() & layer_state_t::ePositionChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700363 layer.x = proto.x();
364 layer.y = proto.y();
365 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800366 if (proto.what() & layer_state_t::eLayerChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700367 layer.z = proto.z();
368 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800369 if (proto.what() & layer_state_t::eLayerStackChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700370 layer.layerStack.id = proto.layer_stack();
371 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800372 if (proto.what() & layer_state_t::eFlagsChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700373 layer.flags = proto.flags();
374 layer.mask = proto.mask();
375 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800376 if (proto.what() & layer_state_t::eMatrixChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700377 const proto::LayerState_Matrix22& matrixProto = proto.matrix();
378 layer.matrix.dsdx = matrixProto.dsdx();
379 layer.matrix.dsdy = matrixProto.dsdy();
380 layer.matrix.dtdx = matrixProto.dtdx();
381 layer.matrix.dtdy = matrixProto.dtdy();
382 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800383 if (proto.what() & layer_state_t::eCornerRadiusChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700384 layer.cornerRadius = proto.corner_radius();
385 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800386 if (proto.what() & layer_state_t::eBackgroundBlurRadiusChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700387 layer.backgroundBlurRadius = proto.background_blur_radius();
388 }
389
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800390 if (proto.what() & layer_state_t::eAlphaChanged) {
Vishnu Nairbbceb462022-10-10 04:52:13 +0000391 layer.color.a = proto.alpha();
Vishnu Nair6b591152021-10-08 11:45:14 -0700392 }
393
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800394 if (proto.what() & layer_state_t::eColorChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700395 const proto::LayerState_Color3& colorProto = proto.color();
396 layer.color.r = colorProto.r();
397 layer.color.g = colorProto.g();
398 layer.color.b = colorProto.b();
399 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800400 if (proto.what() & layer_state_t::eTransparentRegionChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700401 LayerProtoHelper::readFromProto(proto.transparent_region(), layer.transparentRegion);
402 }
Vishnu Nairbbceb462022-10-10 04:52:13 +0000403 if (proto.what() & layer_state_t::eBufferTransformChanged) {
404 layer.bufferTransform = proto.transform();
Vishnu Nair6b591152021-10-08 11:45:14 -0700405 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800406 if (proto.what() & layer_state_t::eTransformToDisplayInverseChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700407 layer.transformToDisplayInverse = proto.transform_to_display_inverse();
408 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800409 if (proto.what() & layer_state_t::eCropChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700410 LayerProtoHelper::readFromProto(proto.crop(), layer.crop);
411 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800412 if (proto.what() & layer_state_t::eBufferChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700413 const proto::LayerState_BufferData& bufferProto = proto.buffer_data();
Vishnu Nair685cfef2022-02-02 10:01:25 -0800414 layer.bufferData =
Vishnu Nair81750622023-03-08 15:02:06 -0800415 std::make_shared<fake::BufferData>(bufferProto.buffer_id(), bufferProto.width(),
416 bufferProto.height(), bufferProto.pixel_format(),
417 bufferProto.usage());
418 resolvedComposerState.externalTexture =
419 std::make_shared<FakeExternalTexture>(layer.bufferData->getWidth(),
420 layer.bufferData->getHeight(),
421 layer.bufferData->getId(),
422 layer.bufferData->getPixelFormat(),
423 layer.bufferData->getUsage());
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800424 layer.bufferData->frameNumber = bufferProto.frame_number();
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700425 layer.bufferData->flags = ftl::Flags<BufferData::BufferDataChange>(bufferProto.flags());
Vishnu Nair9f0835e2022-01-07 09:33:19 -0800426 layer.bufferData->cachedBuffer.id = bufferProto.cached_buffer_id();
Vishnu Nair8eebba42022-02-25 07:57:15 -0800427 layer.bufferData->acquireFence = Fence::NO_FENCE;
Vishnu Nair6b591152021-10-08 11:45:14 -0700428 }
Vishnu Nair6b591152021-10-08 11:45:14 -0700429
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800430 if (proto.what() & layer_state_t::eApiChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700431 layer.api = proto.api();
432 }
433
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800434 if (proto.what() & layer_state_t::eColorTransformChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700435 LayerProtoHelper::readFromProto(proto.color_transform(), layer.colorTransform);
436 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800437 if (proto.what() & layer_state_t::eBlurRegionsChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700438 layer.blurRegions.reserve(static_cast<size_t>(proto.blur_regions_size()));
439 for (int i = 0; i < proto.blur_regions_size(); i++) {
440 android::BlurRegion region;
441 LayerProtoHelper::readFromProto(proto.blur_regions(i), region);
442 layer.blurRegions.push_back(region);
443 }
444 }
445
Vishnu Nair685cfef2022-02-02 10:01:25 -0800446 if (proto.what() & layer_state_t::eReparent) {
Vishnu Nair81750622023-03-08 15:02:06 -0800447 resolvedComposerState.parentId = proto.parent_id();
Vishnu Nair6b591152021-10-08 11:45:14 -0700448 }
Vishnu Naird37343b2022-01-12 16:18:56 -0800449 if (proto.what() & layer_state_t::eRelativeLayerChanged) {
Vishnu Nair81750622023-03-08 15:02:06 -0800450 resolvedComposerState.relativeParentId = proto.relative_parent_id();
Vishnu Naird37343b2022-01-12 16:18:56 -0800451 layer.z = proto.z();
Vishnu Nair6b591152021-10-08 11:45:14 -0700452 }
453
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800454 if ((proto.what() & layer_state_t::eInputInfoChanged) && proto.has_window_info_handle()) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700455 gui::WindowInfo inputInfo;
456 const proto::LayerState_WindowInfo& windowInfoProto = proto.window_info_handle();
457
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800458 inputInfo.layoutParamsFlags =
459 static_cast<gui::WindowInfo::Flag>(windowInfoProto.layout_params_flags());
460 inputInfo.layoutParamsType =
461 static_cast<gui::WindowInfo::Type>(windowInfoProto.layout_params_type());
Vishnu Nair6b591152021-10-08 11:45:14 -0700462 LayerProtoHelper::readFromProto(windowInfoProto.touchable_region(),
463 inputInfo.touchableRegion);
464 inputInfo.surfaceInset = windowInfoProto.surface_inset();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800465 inputInfo.setInputConfig(gui::WindowInfo::InputConfig::NOT_FOCUSABLE,
466 !windowInfoProto.focusable());
467 inputInfo.setInputConfig(gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER,
468 windowInfoProto.has_wallpaper());
Vishnu Nair6b591152021-10-08 11:45:14 -0700469 inputInfo.globalScaleFactor = windowInfoProto.global_scale_factor();
Vishnu Nair81750622023-03-08 15:02:06 -0800470 const proto::Transform& transformProto = windowInfoProto.transform();
Vishnu Nair6b591152021-10-08 11:45:14 -0700471 inputInfo.transform.set(transformProto.dsdx(), transformProto.dtdx(), transformProto.dtdy(),
472 transformProto.dsdy());
473 inputInfo.transform.set(transformProto.tx(), transformProto.ty());
474 inputInfo.replaceTouchableRegionWithCrop =
475 windowInfoProto.replace_touchable_region_with_crop();
Vishnu Nair81750622023-03-08 15:02:06 -0800476 resolvedComposerState.touchCropId = windowInfoProto.crop_layer_id();
Vishnu Nair286f4f92022-06-08 16:37:39 -0700477
Vishnu Nair6b591152021-10-08 11:45:14 -0700478 layer.windowInfoHandle = sp<gui::WindowInfoHandle>::make(inputInfo);
479 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800480 if (proto.what() & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000481 layer.bgColor.a = proto.bg_color_alpha();
Vishnu Nair6b591152021-10-08 11:45:14 -0700482 layer.bgColorDataspace = static_cast<ui::Dataspace>(proto.bg_color_dataspace());
483 const proto::LayerState_Color3& colorProto = proto.color();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000484 layer.bgColor.r = colorProto.r();
485 layer.bgColor.g = colorProto.g();
486 layer.bgColor.b = colorProto.b();
Vishnu Nair6b591152021-10-08 11:45:14 -0700487 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800488 if (proto.what() & layer_state_t::eColorSpaceAgnosticChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700489 layer.colorSpaceAgnostic = proto.color_space_agnostic();
490 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800491 if (proto.what() & layer_state_t::eShadowRadiusChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700492 layer.shadowRadius = proto.shadow_radius();
493 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800494 if (proto.what() & layer_state_t::eFrameRateSelectionPriority) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700495 layer.frameRateSelectionPriority = proto.frame_rate_selection_priority();
496 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800497 if (proto.what() & layer_state_t::eFrameRateChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700498 layer.frameRate = proto.frame_rate();
499 layer.frameRateCompatibility = static_cast<int8_t>(proto.frame_rate_compatibility());
500 layer.changeFrameRateStrategy = static_cast<int8_t>(proto.change_frame_rate_strategy());
501 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800502 if (proto.what() & layer_state_t::eFixedTransformHintChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700503 layer.fixedTransformHint =
504 static_cast<ui::Transform::RotationFlags>(proto.fixed_transform_hint());
505 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800506 if (proto.what() & layer_state_t::eAutoRefreshChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700507 layer.autoRefresh = proto.auto_refresh();
508 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800509 if (proto.what() & layer_state_t::eTrustedOverlayChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700510 layer.isTrustedOverlay = proto.is_trusted_overlay();
511 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800512 if (proto.what() & layer_state_t::eBufferCropChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700513 LayerProtoHelper::readFromProto(proto.buffer_crop(), layer.bufferCrop);
514 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800515 if (proto.what() & layer_state_t::eDestinationFrameChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700516 LayerProtoHelper::readFromProto(proto.destination_frame(), layer.destinationFrame);
517 }
Vishnu Nair68dee2b2021-11-08 18:52:12 -0800518 if (proto.what() & layer_state_t::eDropInputModeChanged) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700519 layer.dropInputMode = static_cast<gui::DropInputMode>(proto.drop_input_mode());
520 }
Vishnu Nair6b591152021-10-08 11:45:14 -0700521}
522
Vishnu Nair685cfef2022-02-02 10:01:25 -0800523DisplayState TransactionProtoParser::fromProto(const proto::DisplayState& proto) {
Vishnu Nair6b591152021-10-08 11:45:14 -0700524 DisplayState display;
525 display.what = proto.what();
Vishnu Nair685cfef2022-02-02 10:01:25 -0800526 display.token = mMapper->getDisplayHandle(proto.id());
Vishnu Nair6b591152021-10-08 11:45:14 -0700527
528 if (display.what & DisplayState::eLayerStackChanged) {
529 display.layerStack.id = proto.layer_stack();
530 }
531 if (display.what & DisplayState::eDisplayProjectionChanged) {
532 display.orientation = static_cast<ui::Rotation>(proto.orientation());
533 LayerProtoHelper::readFromProto(proto.oriented_display_space_rect(),
534 display.orientedDisplaySpaceRect);
535 LayerProtoHelper::readFromProto(proto.layer_stack_space_rect(),
536 display.layerStackSpaceRect);
537 }
538 if (display.what & DisplayState::eDisplaySizeChanged) {
539 display.width = proto.width();
540 display.height = proto.height();
541 }
542 if (display.what & DisplayState::eFlagsChanged) {
543 display.flags = proto.flags();
544 }
545 return display;
546}
547
Vishnu Nair81750622023-03-08 15:02:06 -0800548void asProto(proto::Transform* proto, const ui::Transform& transform) {
549 proto->set_dsdx(transform.dsdx());
550 proto->set_dtdx(transform.dtdx());
551 proto->set_dtdy(transform.dtdy());
552 proto->set_dsdy(transform.dsdy());
553 proto->set_tx(transform.tx());
554 proto->set_ty(transform.ty());
555}
556
557proto::DisplayInfo TransactionProtoParser::toProto(const frontend::DisplayInfo& displayInfo,
558 uint32_t layerStack) {
559 proto::DisplayInfo proto;
560 proto.set_layer_stack(layerStack);
561 proto.set_display_id(displayInfo.info.displayId);
562 proto.set_logical_width(displayInfo.info.logicalWidth);
563 proto.set_logical_height(displayInfo.info.logicalHeight);
564 asProto(proto.mutable_transform_inverse(), displayInfo.info.transform);
565 asProto(proto.mutable_transform(), displayInfo.transform);
566 proto.set_receives_input(displayInfo.receivesInput);
567 proto.set_is_secure(displayInfo.isSecure);
568 proto.set_is_primary(displayInfo.isPrimary);
569 proto.set_is_virtual(displayInfo.isVirtual);
570 proto.set_rotation_flags((int)displayInfo.rotationFlags);
571 proto.set_transform_hint((int)displayInfo.transformHint);
572 return proto;
573}
574
575void fromProto2(ui::Transform& outTransform, const proto::Transform& proto) {
576 outTransform.set(proto.dsdx(), proto.dtdx(), proto.dtdy(), proto.dsdy());
577 outTransform.set(proto.tx(), proto.ty());
578}
579
580frontend::DisplayInfo TransactionProtoParser::fromProto(const proto::DisplayInfo& proto) {
581 frontend::DisplayInfo displayInfo;
582 displayInfo.info.displayId = proto.display_id();
583 displayInfo.info.logicalWidth = proto.logical_width();
584 displayInfo.info.logicalHeight = proto.logical_height();
585 fromProto2(displayInfo.info.transform, proto.transform_inverse());
586 fromProto2(displayInfo.transform, proto.transform());
587 displayInfo.receivesInput = proto.receives_input();
588 displayInfo.isSecure = proto.is_secure();
589 displayInfo.isPrimary = proto.is_primary();
590 displayInfo.isPrimary = proto.is_virtual();
591 displayInfo.rotationFlags = (ui::Transform::RotationFlags)proto.rotation_flags();
592 displayInfo.transformHint = (ui::Transform::RotationFlags)proto.transform_hint();
593 return displayInfo;
594}
595
596void TransactionProtoParser::fromProto(
597 const google::protobuf::RepeatedPtrField<proto::DisplayInfo>& proto,
598 display::DisplayMap<ui::LayerStack, frontend::DisplayInfo> outDisplayInfos) {
599 outDisplayInfos.clear();
600 for (const proto::DisplayInfo& displayInfo : proto) {
601 outDisplayInfos.emplace_or_replace(ui::LayerStack::fromValue(displayInfo.layer_stack()),
602 fromProto(displayInfo));
603 }
604}
605
Vishnu Nair6b591152021-10-08 11:45:14 -0700606} // namespace android::surfaceflinger