blob: fb1d43bf6c6b32bc9e279325940bf7f2d2ce453e [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>
18#include <ui/Rect.h>
19
20#include "LayerProtoHelper.h"
21#include "TransactionProtoParser.h"
22
23namespace android::surfaceflinger {
24
25proto::TransactionState TransactionProtoParser::toProto(
26 const TransactionState& t, std::function<int32_t(const sp<IBinder>&)> getLayerId,
27 std::function<int32_t(const sp<IBinder>&)> getDisplayId) {
28 proto::TransactionState proto;
29 proto.set_pid(t.originPid);
30 proto.set_uid(t.originUid);
31 proto.set_vsync_id(t.frameTimelineInfo.vsyncId);
32 proto.set_input_event_id(t.frameTimelineInfo.inputEventId);
33 proto.set_post_time(t.postTime);
34
35 for (auto& layerState : t.states) {
36 proto.mutable_layer_changes()->Add(std::move(toProto(layerState.state, getLayerId)));
37 }
38
39 for (auto& displayState : t.displays) {
40 proto.mutable_display_changes()->Add(std::move(toProto(displayState, getDisplayId)));
41 }
42 return proto;
43}
44
45proto::LayerState TransactionProtoParser::toProto(
46 const layer_state_t& layer, std::function<int32_t(const sp<IBinder>&)> getLayerId) {
47 proto::LayerState proto;
48 proto.set_layer_id(layer.layerId);
49 proto.set_what(layer.what);
50
51 if (layer.what & layer_state_t::ePositionChanged) {
52 proto.set_x(layer.x);
53 proto.set_y(layer.y);
54 }
55 if (layer.what & layer_state_t::eLayerChanged) {
56 proto.set_z(layer.z);
57 }
58 if (layer.what & layer_state_t::eSizeChanged) {
59 proto.set_w(layer.w);
60 proto.set_h(layer.h);
61 }
62 if (layer.what & layer_state_t::eLayerStackChanged) {
63 proto.set_layer_stack(layer.layerStack.id);
64 }
65 if (layer.what & layer_state_t::eFlagsChanged) {
66 proto.set_flags(layer.flags);
67 proto.set_mask(layer.mask);
68 }
69 if (layer.what & layer_state_t::eMatrixChanged) {
70 proto::LayerState_Matrix22* matrixProto = proto.mutable_matrix();
71 matrixProto->set_dsdx(layer.matrix.dsdx);
72 matrixProto->set_dsdy(layer.matrix.dsdy);
73 matrixProto->set_dtdx(layer.matrix.dtdx);
74 matrixProto->set_dtdy(layer.matrix.dtdy);
75 }
76 if (layer.what & layer_state_t::eCornerRadiusChanged) {
77 proto.set_corner_radius(layer.cornerRadius);
78 }
79 if (layer.what & layer_state_t::eBackgroundBlurRadiusChanged) {
80 proto.set_background_blur_radius(layer.backgroundBlurRadius);
81 }
82
83 if (layer.what & layer_state_t::eAlphaChanged) {
84 proto.set_alpha(layer.alpha);
85 }
86
87 if (layer.what & layer_state_t::eColorChanged) {
88 proto::LayerState_Color3* colorProto = proto.mutable_color();
89 colorProto->set_r(layer.color.r);
90 colorProto->set_g(layer.color.g);
91 colorProto->set_b(layer.color.b);
92 }
93 if (layer.what & layer_state_t::eTransparentRegionChanged) {
94 LayerProtoHelper::writeToProto(layer.transparentRegion, proto.mutable_transparent_region());
95 }
96 if (layer.what & layer_state_t::eTransformChanged) {
97 proto.set_transform(layer.transform);
98 }
99 if (layer.what & layer_state_t::eTransformToDisplayInverseChanged) {
100 proto.set_transform_to_display_inverse(layer.transformToDisplayInverse);
101 }
102 if (layer.what & layer_state_t::eCropChanged) {
103 LayerProtoHelper::writeToProto(layer.crop, proto.mutable_crop());
104 }
105 if (layer.what & layer_state_t::eBufferChanged) {
106 proto::LayerState_BufferData* bufferProto = proto.mutable_buffer_data();
107 if (layer.bufferData.buffer) {
108 bufferProto->set_buffer_id(layer.bufferData.buffer->getId());
109 bufferProto->set_width(layer.bufferData.buffer->getWidth());
110 bufferProto->set_height(layer.bufferData.buffer->getHeight());
111 }
112 bufferProto->set_frame_number(layer.bufferData.frameNumber);
113 bufferProto->set_flags(layer.bufferData.flags.get());
114 bufferProto->set_cached_buffer_id(layer.bufferData.cachedBuffer.id);
115 }
116 if (layer.what & layer_state_t::eSidebandStreamChanged) {
117 proto.set_has_sideband_stream(layer.sidebandStream != nullptr);
118 }
119
120 if (layer.what & layer_state_t::eApiChanged) {
121 proto.set_api(layer.api);
122 }
123
124 if (layer.what & layer_state_t::eColorTransformChanged) {
125 LayerProtoHelper::writeToProto(layer.colorTransform, proto.mutable_color_transform());
126 }
127 if (layer.what & layer_state_t::eBlurRegionsChanged) {
128 for (auto& region : layer.blurRegions) {
129 LayerProtoHelper::writeToProto(region, proto.add_blur_regions());
130 }
131 }
132
133 if (layer.what & layer_state_t::eReparent) {
134 int32_t layerId = layer.parentSurfaceControlForChild
135 ? getLayerId(layer.parentSurfaceControlForChild->getHandle())
136 : -1;
137 proto.set_parent_id(layerId);
138 }
139 if (layer.what & layer_state_t::eRelativeLayerChanged) {
140 int32_t layerId = layer.relativeLayerSurfaceControl
141 ? getLayerId(layer.relativeLayerSurfaceControl->getHandle())
142 : -1;
143 proto.set_relative_parent_id(layerId);
144 }
145
146 if (layer.what & layer_state_t::eInputInfoChanged) {
147 if (layer.windowInfoHandle) {
148 const gui::WindowInfo* inputInfo = layer.windowInfoHandle->getInfo();
149 proto::LayerState_WindowInfo* windowInfoProto = proto.mutable_window_info_handle();
150 windowInfoProto->set_layout_params_flags(inputInfo->flags.get());
151 windowInfoProto->set_layout_params_type(static_cast<int32_t>(inputInfo->type));
152 LayerProtoHelper::writeToProto(inputInfo->touchableRegion,
153 windowInfoProto->mutable_touchable_region());
154 windowInfoProto->set_surface_inset(inputInfo->surfaceInset);
155 windowInfoProto->set_focusable(inputInfo->focusable);
156 windowInfoProto->set_has_wallpaper(inputInfo->hasWallpaper);
157 windowInfoProto->set_global_scale_factor(inputInfo->globalScaleFactor);
158 proto::LayerState_Transform* transformProto = windowInfoProto->mutable_transform();
159 transformProto->set_dsdx(inputInfo->transform.dsdx());
160 transformProto->set_dtdx(inputInfo->transform.dtdx());
161 transformProto->set_dtdy(inputInfo->transform.dtdy());
162 transformProto->set_dsdy(inputInfo->transform.dsdy());
163 transformProto->set_tx(inputInfo->transform.tx());
164 transformProto->set_ty(inputInfo->transform.ty());
165 windowInfoProto->set_replace_touchable_region_with_crop(
166 inputInfo->replaceTouchableRegionWithCrop);
167 windowInfoProto->set_crop_layer_id(
168 getLayerId(inputInfo->touchableRegionCropHandle.promote()));
169 }
170 }
171 if (layer.what & layer_state_t::eBackgroundColorChanged) {
172 proto.set_bg_color_alpha(layer.bgColorAlpha);
173 proto.set_bg_color_dataspace(static_cast<int32_t>(layer.bgColorDataspace));
174 proto::LayerState_Color3* colorProto = proto.mutable_color();
175 colorProto->set_r(layer.color.r);
176 colorProto->set_g(layer.color.g);
177 colorProto->set_b(layer.color.b);
178 }
179 if (layer.what & layer_state_t::eColorSpaceAgnosticChanged) {
180 proto.set_color_space_agnostic(layer.colorSpaceAgnostic);
181 }
182 if (layer.what & layer_state_t::eShadowRadiusChanged) {
183 proto.set_shadow_radius(layer.shadowRadius);
184 }
185 if (layer.what & layer_state_t::eFrameRateSelectionPriority) {
186 proto.set_frame_rate_selection_priority(layer.frameRateSelectionPriority);
187 }
188 if (layer.what & layer_state_t::eFrameRateChanged) {
189 proto.set_frame_rate(layer.frameRate);
190 proto.set_frame_rate_compatibility(layer.frameRateCompatibility);
191 proto.set_change_frame_rate_strategy(layer.changeFrameRateStrategy);
192 }
193 if (layer.what & layer_state_t::eFixedTransformHintChanged) {
194 proto.set_fixed_transform_hint(layer.fixedTransformHint);
195 }
196 if (layer.what & layer_state_t::eAutoRefreshChanged) {
197 proto.set_auto_refresh(layer.autoRefresh);
198 }
199 if (layer.what & layer_state_t::eTrustedOverlayChanged) {
200 proto.set_is_trusted_overlay(layer.isTrustedOverlay);
201 }
202 if (layer.what & layer_state_t::eBufferCropChanged) {
203 LayerProtoHelper::writeToProto(layer.bufferCrop, proto.mutable_buffer_crop());
204 }
205 if (layer.what & layer_state_t::eDestinationFrameChanged) {
206 LayerProtoHelper::writeToProto(layer.destinationFrame, proto.mutable_destination_frame());
207 }
208 if (layer.what & layer_state_t::eDropInputModeChanged) {
209 proto.set_drop_input_mode(
210 static_cast<proto::LayerState_DropInputMode>(layer.dropInputMode));
211 }
212 return proto;
213}
214
215proto::DisplayState TransactionProtoParser::toProto(
216 const DisplayState& display, std::function<int32_t(const sp<IBinder>&)> getDisplayId) {
217 proto::DisplayState proto;
218 proto.set_what(display.what);
219 proto.set_id(getDisplayId(display.token));
220
221 if (display.what & DisplayState::eLayerStackChanged) {
222 proto.set_layer_stack(display.layerStack.id);
223 }
224 if (display.what & DisplayState::eDisplayProjectionChanged) {
225 proto.set_orientation(static_cast<uint32_t>(display.orientation));
226 LayerProtoHelper::writeToProto(display.orientedDisplaySpaceRect,
227 proto.mutable_oriented_display_space_rect());
228 LayerProtoHelper::writeToProto(display.layerStackSpaceRect,
229 proto.mutable_layer_stack_space_rect());
230 }
231 if (display.what & DisplayState::eDisplaySizeChanged) {
232 proto.set_width(display.width);
233 proto.set_height(display.height);
234 }
235 if (display.what & DisplayState::eFlagsChanged) {
236 proto.set_flags(display.flags);
237 }
238 return proto;
239}
240
241TransactionState TransactionProtoParser::fromProto(
242 const proto::TransactionState& proto, std::function<sp<IBinder>(int32_t)> getLayerHandle,
243 std::function<sp<IBinder>(int32_t)> getDisplayHandle) {
244 TransactionState t;
245 t.originPid = proto.pid();
246 t.originUid = proto.uid();
247 t.frameTimelineInfo.vsyncId = proto.vsync_id();
248 t.frameTimelineInfo.inputEventId = proto.input_event_id();
249 t.postTime = proto.post_time();
250 int32_t layerCount = proto.layer_changes_size();
251 t.states.reserve(static_cast<size_t>(layerCount));
252 for (int i = 0; i < layerCount; i++) {
253 ComposerState s;
254 s.state = std::move(fromProto(proto.layer_changes(i), getLayerHandle));
255 t.states.add(s);
256 }
257
258 int32_t displayCount = proto.display_changes_size();
259 t.displays.reserve(static_cast<size_t>(displayCount));
260 for (int i = 0; i < displayCount; i++) {
261 t.displays.add(fromProto(proto.display_changes(i), getDisplayHandle));
262 }
263 return t;
264}
265
266layer_state_t TransactionProtoParser::fromProto(
267 const proto::LayerState& proto, std::function<sp<IBinder>(int32_t)> getLayerHandle) {
268 layer_state_t layer;
269 layer.layerId = proto.layer_id();
270 layer.what = proto.what();
271
272 if (layer.what & layer_state_t::ePositionChanged) {
273 layer.x = proto.x();
274 layer.y = proto.y();
275 }
276 if (layer.what & layer_state_t::eLayerChanged) {
277 layer.z = proto.z();
278 }
279 if (layer.what & layer_state_t::eSizeChanged) {
280 layer.w = proto.w();
281 layer.h = proto.h();
282 }
283 if (layer.what & layer_state_t::eLayerStackChanged) {
284 layer.layerStack.id = proto.layer_stack();
285 }
286 if (layer.what & layer_state_t::eFlagsChanged) {
287 layer.flags = proto.flags();
288 layer.mask = proto.mask();
289 }
290 if (layer.what & layer_state_t::eMatrixChanged) {
291 const proto::LayerState_Matrix22& matrixProto = proto.matrix();
292 layer.matrix.dsdx = matrixProto.dsdx();
293 layer.matrix.dsdy = matrixProto.dsdy();
294 layer.matrix.dtdx = matrixProto.dtdx();
295 layer.matrix.dtdy = matrixProto.dtdy();
296 }
297 if (layer.what & layer_state_t::eCornerRadiusChanged) {
298 layer.cornerRadius = proto.corner_radius();
299 }
300 if (layer.what & layer_state_t::eBackgroundBlurRadiusChanged) {
301 layer.backgroundBlurRadius = proto.background_blur_radius();
302 }
303
304 if (layer.what & layer_state_t::eAlphaChanged) {
305 layer.alpha = proto.alpha();
306 }
307
308 if (layer.what & layer_state_t::eColorChanged) {
309 const proto::LayerState_Color3& colorProto = proto.color();
310 layer.color.r = colorProto.r();
311 layer.color.g = colorProto.g();
312 layer.color.b = colorProto.b();
313 }
314 if (layer.what & layer_state_t::eTransparentRegionChanged) {
315 LayerProtoHelper::readFromProto(proto.transparent_region(), layer.transparentRegion);
316 }
317 if (layer.what & layer_state_t::eTransformChanged) {
318 layer.transform = proto.transform();
319 }
320 if (layer.what & layer_state_t::eTransformToDisplayInverseChanged) {
321 layer.transformToDisplayInverse = proto.transform_to_display_inverse();
322 }
323 if (layer.what & layer_state_t::eCropChanged) {
324 LayerProtoHelper::readFromProto(proto.crop(), layer.crop);
325 }
326 if (layer.what & layer_state_t::eBufferChanged) {
327 const proto::LayerState_BufferData& bufferProto = proto.buffer_data();
328 layer.bufferData.buffer = new GraphicBuffer(bufferProto.width(), bufferProto.height(),
329 HAL_PIXEL_FORMAT_RGBA_8888, 1, 0);
330 layer.bufferData.frameNumber = bufferProto.frame_number();
331 layer.bufferData.flags = Flags<BufferData::BufferDataChange>(bufferProto.flags());
332 layer.bufferData.cachedBuffer.id = bufferProto.cached_buffer_id();
333 }
334 if (layer.what & layer_state_t::eSidebandStreamChanged) {
335 native_handle_t* handle = native_handle_create(0, 0);
336 layer.sidebandStream =
337 proto.has_sideband_stream() ? NativeHandle::create(handle, true) : nullptr;
338 }
339
340 if (layer.what & layer_state_t::eApiChanged) {
341 layer.api = proto.api();
342 }
343
344 if (layer.what & layer_state_t::eColorTransformChanged) {
345 LayerProtoHelper::readFromProto(proto.color_transform(), layer.colorTransform);
346 }
347 if (layer.what & layer_state_t::eBlurRegionsChanged) {
348 layer.blurRegions.reserve(static_cast<size_t>(proto.blur_regions_size()));
349 for (int i = 0; i < proto.blur_regions_size(); i++) {
350 android::BlurRegion region;
351 LayerProtoHelper::readFromProto(proto.blur_regions(i), region);
352 layer.blurRegions.push_back(region);
353 }
354 }
355
356 if (layer.what & layer_state_t::eReparent) {
357 int32_t layerId = proto.parent_id();
358 layer.parentSurfaceControlForChild =
359 new SurfaceControl(SurfaceComposerClient::getDefault(), getLayerHandle(layerId),
360 nullptr, layerId);
361 }
362 if (layer.what & layer_state_t::eRelativeLayerChanged) {
363 int32_t layerId = proto.relative_parent_id();
364 layer.relativeLayerSurfaceControl =
365 new SurfaceControl(SurfaceComposerClient::getDefault(), getLayerHandle(layerId),
366 nullptr, layerId);
367 }
368
369 if ((layer.what & layer_state_t::eInputInfoChanged) && proto.has_window_info_handle()) {
370 gui::WindowInfo inputInfo;
371 const proto::LayerState_WindowInfo& windowInfoProto = proto.window_info_handle();
372
373 inputInfo.flags = static_cast<gui::WindowInfo::Flag>(windowInfoProto.layout_params_flags());
374 inputInfo.type = static_cast<gui::WindowInfo::Type>(windowInfoProto.layout_params_type());
375 LayerProtoHelper::readFromProto(windowInfoProto.touchable_region(),
376 inputInfo.touchableRegion);
377 inputInfo.surfaceInset = windowInfoProto.surface_inset();
378 inputInfo.focusable = windowInfoProto.focusable();
379 inputInfo.hasWallpaper = windowInfoProto.has_wallpaper();
380 inputInfo.globalScaleFactor = windowInfoProto.global_scale_factor();
381 const proto::LayerState_Transform& transformProto = windowInfoProto.transform();
382 inputInfo.transform.set(transformProto.dsdx(), transformProto.dtdx(), transformProto.dtdy(),
383 transformProto.dsdy());
384 inputInfo.transform.set(transformProto.tx(), transformProto.ty());
385 inputInfo.replaceTouchableRegionWithCrop =
386 windowInfoProto.replace_touchable_region_with_crop();
387 int32_t layerId = windowInfoProto.crop_layer_id();
388 inputInfo.touchableRegionCropHandle = getLayerHandle(layerId);
389 layer.windowInfoHandle = sp<gui::WindowInfoHandle>::make(inputInfo);
390 }
391 if (layer.what & layer_state_t::eBackgroundColorChanged) {
392 layer.bgColorAlpha = proto.bg_color_alpha();
393 layer.bgColorDataspace = static_cast<ui::Dataspace>(proto.bg_color_dataspace());
394 const proto::LayerState_Color3& colorProto = proto.color();
395 layer.color.r = colorProto.r();
396 layer.color.g = colorProto.g();
397 layer.color.b = colorProto.b();
398 }
399 if (layer.what & layer_state_t::eColorSpaceAgnosticChanged) {
400 layer.colorSpaceAgnostic = proto.color_space_agnostic();
401 }
402 if (layer.what & layer_state_t::eShadowRadiusChanged) {
403 layer.shadowRadius = proto.shadow_radius();
404 }
405 if (layer.what & layer_state_t::eFrameRateSelectionPriority) {
406 layer.frameRateSelectionPriority = proto.frame_rate_selection_priority();
407 }
408 if (layer.what & layer_state_t::eFrameRateChanged) {
409 layer.frameRate = proto.frame_rate();
410 layer.frameRateCompatibility = static_cast<int8_t>(proto.frame_rate_compatibility());
411 layer.changeFrameRateStrategy = static_cast<int8_t>(proto.change_frame_rate_strategy());
412 }
413 if (layer.what & layer_state_t::eFixedTransformHintChanged) {
414 layer.fixedTransformHint =
415 static_cast<ui::Transform::RotationFlags>(proto.fixed_transform_hint());
416 }
417 if (layer.what & layer_state_t::eAutoRefreshChanged) {
418 layer.autoRefresh = proto.auto_refresh();
419 }
420 if (layer.what & layer_state_t::eTrustedOverlayChanged) {
421 layer.isTrustedOverlay = proto.is_trusted_overlay();
422 }
423 if (layer.what & layer_state_t::eBufferCropChanged) {
424 LayerProtoHelper::readFromProto(proto.buffer_crop(), layer.bufferCrop);
425 }
426 if (layer.what & layer_state_t::eDestinationFrameChanged) {
427 LayerProtoHelper::readFromProto(proto.destination_frame(), layer.destinationFrame);
428 }
429 if (layer.what & layer_state_t::eDropInputModeChanged) {
430 layer.dropInputMode = static_cast<gui::DropInputMode>(proto.drop_input_mode());
431 }
432 return layer;
433}
434
435DisplayState TransactionProtoParser::fromProto(
436 const proto::DisplayState& proto, std::function<sp<IBinder>(int32_t)> getDisplayHandle) {
437 DisplayState display;
438 display.what = proto.what();
439 display.token = getDisplayHandle(proto.id());
440
441 if (display.what & DisplayState::eLayerStackChanged) {
442 display.layerStack.id = proto.layer_stack();
443 }
444 if (display.what & DisplayState::eDisplayProjectionChanged) {
445 display.orientation = static_cast<ui::Rotation>(proto.orientation());
446 LayerProtoHelper::readFromProto(proto.oriented_display_space_rect(),
447 display.orientedDisplaySpaceRect);
448 LayerProtoHelper::readFromProto(proto.layer_stack_space_rect(),
449 display.layerStackSpaceRect);
450 }
451 if (display.what & DisplayState::eDisplaySizeChanged) {
452 display.width = proto.width();
453 display.height = proto.height();
454 }
455 if (display.what & DisplayState::eFlagsChanged) {
456 display.flags = proto.flags();
457 }
458 return display;
459}
460
461} // namespace android::surfaceflinger