• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #include "FrontEnd/LayerCreationArgs.h"
19 #include "FrontEnd/LayerSnapshot.h"
20 #pragma clang diagnostic push
21 #pragma clang diagnostic ignored "-Wconversion"
22 #pragma clang diagnostic ignored "-Wextra"
23 
24 #include "LayerProtoHelper.h"
25 
26 namespace android {
27 
28 using gui::WindowInfo;
29 
30 namespace surfaceflinger {
31 
writePositionToProto(const float x,const float y,std::function<PositionProto * ()> getPositionProto)32 void LayerProtoHelper::writePositionToProto(const float x, const float y,
33                                             std::function<PositionProto*()> getPositionProto) {
34     if (x != 0 || y != 0) {
35         // Use a lambda do avoid writing the object header when the object is empty
36         PositionProto* position = getPositionProto();
37         position->set_x(x);
38         position->set_y(y);
39     }
40 }
41 
writeSizeToProto(const uint32_t w,const uint32_t h,std::function<SizeProto * ()> getSizeProto)42 void LayerProtoHelper::writeSizeToProto(const uint32_t w, const uint32_t h,
43                                         std::function<SizeProto*()> getSizeProto) {
44     if (w != 0 || h != 0) {
45         // Use a lambda do avoid writing the object header when the object is empty
46         SizeProto* size = getSizeProto();
47         size->set_w(w);
48         size->set_h(h);
49     }
50 }
51 
writeToProto(const Region & region,std::function<RegionProto * ()> getRegionProto)52 void LayerProtoHelper::writeToProto(const Region& region,
53                                     std::function<RegionProto*()> getRegionProto) {
54     if (region.isEmpty()) {
55         return;
56     }
57 
58     writeToProto(region, getRegionProto());
59 }
60 
writeToProto(const Region & region,RegionProto * regionProto)61 void LayerProtoHelper::writeToProto(const Region& region, RegionProto* regionProto) {
62     if (region.isEmpty()) {
63         return;
64     }
65 
66     Region::const_iterator head = region.begin();
67     Region::const_iterator const tail = region.end();
68     // Use a lambda do avoid writing the object header when the object is empty
69     while (head != tail) {
70         writeToProto(*head, regionProto->add_rect());
71         head++;
72     }
73 }
74 
readFromProto(const RegionProto & regionProto,Region & outRegion)75 void LayerProtoHelper::readFromProto(const RegionProto& regionProto, Region& outRegion) {
76     for (int i = 0; i < regionProto.rect_size(); i++) {
77         Rect rect;
78         readFromProto(regionProto.rect(i), rect);
79         outRegion.orSelf(rect);
80     }
81 }
82 
writeToProto(const Rect & rect,std::function<RectProto * ()> getRectProto)83 void LayerProtoHelper::writeToProto(const Rect& rect, std::function<RectProto*()> getRectProto) {
84     if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
85         // Use a lambda do avoid writing the object header when the object is empty
86         writeToProto(rect, getRectProto());
87     }
88 }
89 
writeToProto(const Rect & rect,RectProto * rectProto)90 void LayerProtoHelper::writeToProto(const Rect& rect, RectProto* rectProto) {
91     rectProto->set_left(rect.left);
92     rectProto->set_top(rect.top);
93     rectProto->set_bottom(rect.bottom);
94     rectProto->set_right(rect.right);
95 }
96 
readFromProto(const RectProto & proto,Rect & outRect)97 void LayerProtoHelper::readFromProto(const RectProto& proto, Rect& outRect) {
98     outRect.left = proto.left();
99     outRect.top = proto.top();
100     outRect.bottom = proto.bottom();
101     outRect.right = proto.right();
102 }
103 
writeToProto(const FloatRect & rect,std::function<FloatRectProto * ()> getFloatRectProto)104 void LayerProtoHelper::writeToProto(const FloatRect& rect,
105                                     std::function<FloatRectProto*()> getFloatRectProto) {
106     if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
107         // Use a lambda do avoid writing the object header when the object is empty
108         FloatRectProto* rectProto = getFloatRectProto();
109         rectProto->set_left(rect.left);
110         rectProto->set_top(rect.top);
111         rectProto->set_bottom(rect.bottom);
112         rectProto->set_right(rect.right);
113     }
114 }
115 
writeToProto(const half4 color,std::function<ColorProto * ()> getColorProto)116 void LayerProtoHelper::writeToProto(const half4 color, std::function<ColorProto*()> getColorProto) {
117     if (color.r != 0 || color.g != 0 || color.b != 0 || color.a != 0) {
118         // Use a lambda do avoid writing the object header when the object is empty
119         ColorProto* colorProto = getColorProto();
120         colorProto->set_r(color.r);
121         colorProto->set_g(color.g);
122         colorProto->set_b(color.b);
123         colorProto->set_a(color.a);
124     }
125 }
126 
writeToProtoDeprecated(const ui::Transform & transform,TransformProto * transformProto)127 void LayerProtoHelper::writeToProtoDeprecated(const ui::Transform& transform,
128                                               TransformProto* transformProto) {
129     const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
130     transformProto->set_type(type);
131 
132     // Rotations that are 90/180/270 have their own type so the transform matrix can be
133     // reconstructed later. All other rotation have the type UKNOWN so we need to save the transform
134     // values in that case.
135     if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
136         transformProto->set_dsdx(transform[0][0]);
137         transformProto->set_dtdx(transform[0][1]);
138         transformProto->set_dsdy(transform[1][0]);
139         transformProto->set_dtdy(transform[1][1]);
140     }
141 }
142 
writeTransformToProto(const ui::Transform & transform,TransformProto * transformProto)143 void LayerProtoHelper::writeTransformToProto(const ui::Transform& transform,
144                                              TransformProto* transformProto) {
145     const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
146     transformProto->set_type(type);
147 
148     // Rotations that are 90/180/270 have their own type so the transform matrix can be
149     // reconstructed later. All other rotation have the type UNKNOWN so we need to save the
150     // transform values in that case.
151     if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
152         transformProto->set_dsdx(transform.dsdx());
153         transformProto->set_dtdx(transform.dtdx());
154         transformProto->set_dtdy(transform.dtdy());
155         transformProto->set_dsdy(transform.dsdy());
156     }
157 }
158 
writeToProto(const renderengine::ExternalTexture & buffer,std::function<ActiveBufferProto * ()> getActiveBufferProto)159 void LayerProtoHelper::writeToProto(const renderengine::ExternalTexture& buffer,
160                                     std::function<ActiveBufferProto*()> getActiveBufferProto) {
161     if (buffer.getWidth() != 0 || buffer.getHeight() != 0 || buffer.getUsage() != 0 ||
162         buffer.getPixelFormat() != 0) {
163         // Use a lambda do avoid writing the object header when the object is empty
164         ActiveBufferProto* activeBufferProto = getActiveBufferProto();
165         activeBufferProto->set_width(buffer.getWidth());
166         activeBufferProto->set_height(buffer.getHeight());
167         activeBufferProto->set_stride(buffer.getUsage());
168         activeBufferProto->set_format(buffer.getPixelFormat());
169     }
170 }
171 
writeToProto(const WindowInfo & inputInfo,const wp<Layer> & touchableRegionBounds,std::function<InputWindowInfoProto * ()> getInputWindowInfoProto)172 void LayerProtoHelper::writeToProto(
173         const WindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds,
174         std::function<InputWindowInfoProto*()> getInputWindowInfoProto) {
175     if (inputInfo.token == nullptr) {
176         return;
177     }
178 
179     InputWindowInfoProto* proto = getInputWindowInfoProto();
180     proto->set_layout_params_flags(inputInfo.layoutParamsFlags.get());
181     proto->set_input_config(inputInfo.inputConfig.get());
182     using U = std::underlying_type_t<WindowInfo::Type>;
183     // TODO(b/129481165): This static assert can be safely removed once conversion warnings
184     // are re-enabled.
185     static_assert(std::is_same_v<U, int32_t>);
186     proto->set_layout_params_type(static_cast<U>(inputInfo.layoutParamsType));
187 
188     LayerProtoHelper::writeToProto({inputInfo.frameLeft, inputInfo.frameTop, inputInfo.frameRight,
189                                     inputInfo.frameBottom},
190                                    [&]() { return proto->mutable_frame(); });
191     LayerProtoHelper::writeToProto(inputInfo.touchableRegion,
192                                    [&]() { return proto->mutable_touchable_region(); });
193 
194     proto->set_surface_inset(inputInfo.surfaceInset);
195     using InputConfig = gui::WindowInfo::InputConfig;
196     proto->set_visible(!inputInfo.inputConfig.test(InputConfig::NOT_VISIBLE));
197     proto->set_focusable(!inputInfo.inputConfig.test(InputConfig::NOT_FOCUSABLE));
198     proto->set_has_wallpaper(inputInfo.inputConfig.test(InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER));
199 
200     proto->set_global_scale_factor(inputInfo.globalScaleFactor);
201     LayerProtoHelper::writeToProtoDeprecated(inputInfo.transform, proto->mutable_transform());
202     proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop);
203     auto cropLayer = touchableRegionBounds.promote();
204     if (cropLayer != nullptr) {
205         proto->set_crop_layer_id(cropLayer->sequence);
206         LayerProtoHelper::writeToProto(cropLayer->getScreenBounds(
207                                                false /* reduceTransparentRegion */),
208                                        [&]() { return proto->mutable_touchable_region_crop(); });
209     }
210 }
211 
writeToProto(const mat4 matrix,ColorTransformProto * colorTransformProto)212 void LayerProtoHelper::writeToProto(const mat4 matrix, ColorTransformProto* colorTransformProto) {
213     for (int i = 0; i < mat4::ROW_SIZE; i++) {
214         for (int j = 0; j < mat4::COL_SIZE; j++) {
215             colorTransformProto->add_val(matrix[i][j]);
216         }
217     }
218 }
219 
readFromProto(const ColorTransformProto & colorTransformProto,mat4 & matrix)220 void LayerProtoHelper::readFromProto(const ColorTransformProto& colorTransformProto, mat4& matrix) {
221     for (int i = 0; i < mat4::ROW_SIZE; i++) {
222         for (int j = 0; j < mat4::COL_SIZE; j++) {
223             matrix[i][j] = colorTransformProto.val(i * mat4::COL_SIZE + j);
224         }
225     }
226 }
227 
writeToProto(const android::BlurRegion region,BlurRegion * proto)228 void LayerProtoHelper::writeToProto(const android::BlurRegion region, BlurRegion* proto) {
229     proto->set_blur_radius(region.blurRadius);
230     proto->set_corner_radius_tl(region.cornerRadiusTL);
231     proto->set_corner_radius_tr(region.cornerRadiusTR);
232     proto->set_corner_radius_bl(region.cornerRadiusBL);
233     proto->set_corner_radius_br(region.cornerRadiusBR);
234     proto->set_alpha(region.alpha);
235     proto->set_left(region.left);
236     proto->set_top(region.top);
237     proto->set_right(region.right);
238     proto->set_bottom(region.bottom);
239 }
240 
readFromProto(const BlurRegion & proto,android::BlurRegion & outRegion)241 void LayerProtoHelper::readFromProto(const BlurRegion& proto, android::BlurRegion& outRegion) {
242     outRegion.blurRadius = proto.blur_radius();
243     outRegion.cornerRadiusTL = proto.corner_radius_tl();
244     outRegion.cornerRadiusTR = proto.corner_radius_tr();
245     outRegion.cornerRadiusBL = proto.corner_radius_bl();
246     outRegion.cornerRadiusBR = proto.corner_radius_br();
247     outRegion.alpha = proto.alpha();
248     outRegion.left = proto.left();
249     outRegion.top = proto.top();
250     outRegion.right = proto.right();
251     outRegion.bottom = proto.bottom();
252 }
253 
generate(const frontend::LayerHierarchy & root)254 LayersProto LayerProtoFromSnapshotGenerator::generate(const frontend::LayerHierarchy& root) {
255     mLayersProto.clear_layers();
256     std::unordered_set<uint64_t> stackIdsToSkip;
257     if ((mTraceFlags & LayerTracing::TRACE_VIRTUAL_DISPLAYS) == 0) {
258         for (const auto& [layerStack, displayInfo] : mDisplayInfos) {
259             if (displayInfo.isVirtual) {
260                 stackIdsToSkip.insert(layerStack.id);
261             }
262         }
263     }
264 
265     frontend::LayerHierarchy::TraversalPath path = frontend::LayerHierarchy::TraversalPath::ROOT;
266     for (auto& [child, variant] : root.mChildren) {
267         if (variant != frontend::LayerHierarchy::Variant::Attached ||
268             stackIdsToSkip.find(child->getLayer()->layerStack.id) != stackIdsToSkip.end()) {
269             continue;
270         }
271         frontend::LayerHierarchy::ScopedAddToTraversalPath addChildToPath(path,
272                                                                           child->getLayer()->id,
273                                                                           variant);
274         LayerProtoFromSnapshotGenerator::writeHierarchyToProto(*child, path);
275     }
276 
277     // fill in relative and parent info
278     for (int i = 0; i < mLayersProto.layers_size(); i++) {
279         auto layerProto = mLayersProto.mutable_layers()->Mutable(i);
280         auto it = mChildToRelativeParent.find(layerProto->id());
281         if (it == mChildToRelativeParent.end()) {
282             layerProto->set_z_order_relative_of(-1);
283         } else {
284             layerProto->set_z_order_relative_of(it->second);
285         }
286         it = mChildToParent.find(layerProto->id());
287         if (it == mChildToParent.end()) {
288             layerProto->set_parent(-1);
289         } else {
290             layerProto->set_parent(it->second);
291         }
292     }
293 
294     mDefaultSnapshots.clear();
295     mChildToRelativeParent.clear();
296     return std::move(mLayersProto);
297 }
298 
getSnapshot(frontend::LayerHierarchy::TraversalPath & path,const frontend::RequestedLayerState & layer)299 frontend::LayerSnapshot* LayerProtoFromSnapshotGenerator::getSnapshot(
300         frontend::LayerHierarchy::TraversalPath& path, const frontend::RequestedLayerState& layer) {
301     frontend::LayerSnapshot* snapshot = mSnapshotBuilder.getSnapshot(path);
302     if (snapshot) {
303         return snapshot;
304     } else {
305         mDefaultSnapshots[path] = frontend::LayerSnapshot(layer, path);
306         return &mDefaultSnapshots[path];
307     }
308 }
309 
writeHierarchyToProto(const frontend::LayerHierarchy & root,frontend::LayerHierarchy::TraversalPath & path)310 void LayerProtoFromSnapshotGenerator::writeHierarchyToProto(
311         const frontend::LayerHierarchy& root, frontend::LayerHierarchy::TraversalPath& path) {
312     using Variant = frontend::LayerHierarchy::Variant;
313     LayerProto* layerProto = mLayersProto.add_layers();
314     const frontend::RequestedLayerState& layer = *root.getLayer();
315     frontend::LayerSnapshot* snapshot = getSnapshot(path, layer);
316     LayerProtoHelper::writeSnapshotToProto(layerProto, layer, *snapshot, mTraceFlags);
317 
318     for (const auto& [child, variant] : root.mChildren) {
319         frontend::LayerHierarchy::ScopedAddToTraversalPath addChildToPath(path,
320                                                                           child->getLayer()->id,
321                                                                           variant);
322         frontend::LayerSnapshot* childSnapshot = getSnapshot(path, layer);
323         if (variant == Variant::Attached || variant == Variant::Detached ||
324             variant == Variant::Mirror) {
325             mChildToParent[childSnapshot->uniqueSequence] = snapshot->uniqueSequence;
326             layerProto->add_children(childSnapshot->uniqueSequence);
327         } else if (variant == Variant::Relative) {
328             mChildToRelativeParent[childSnapshot->uniqueSequence] = snapshot->uniqueSequence;
329             layerProto->add_relatives(childSnapshot->uniqueSequence);
330         }
331     }
332 
333     if (mTraceFlags & LayerTracing::TRACE_COMPOSITION) {
334         auto it = mLegacyLayers.find(layer.id);
335         if (it != mLegacyLayers.end()) {
336             it->second->writeCompositionStateToProto(layerProto, snapshot->outputFilter.layerStack);
337         }
338     }
339 
340     for (const auto& [child, variant] : root.mChildren) {
341         // avoid visiting relative layers twice
342         if (variant == Variant::Detached) {
343             continue;
344         }
345         frontend::LayerHierarchy::ScopedAddToTraversalPath addChildToPath(path,
346                                                                           child->getLayer()->id,
347                                                                           variant);
348         writeHierarchyToProto(*child, path);
349     }
350 }
351 
writeSnapshotToProto(LayerProto * layerInfo,const frontend::RequestedLayerState & requestedState,const frontend::LayerSnapshot & snapshot,uint32_t traceFlags)352 void LayerProtoHelper::writeSnapshotToProto(LayerProto* layerInfo,
353                                             const frontend::RequestedLayerState& requestedState,
354                                             const frontend::LayerSnapshot& snapshot,
355                                             uint32_t traceFlags) {
356     const ui::Transform transform = snapshot.geomLayerTransform;
357     auto buffer = requestedState.externalTexture;
358     if (buffer != nullptr) {
359         LayerProtoHelper::writeToProto(*buffer,
360                                        [&]() { return layerInfo->mutable_active_buffer(); });
361         LayerProtoHelper::writeToProtoDeprecated(ui::Transform(requestedState.bufferTransform),
362                                                  layerInfo->mutable_buffer_transform());
363     }
364     layerInfo->set_invalidate(snapshot.contentDirty);
365     layerInfo->set_is_protected(snapshot.hasProtectedContent);
366     layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(snapshot.dataspace)));
367     layerInfo->set_curr_frame(requestedState.bufferData->frameNumber);
368     layerInfo->set_requested_corner_radius(requestedState.cornerRadius);
369     layerInfo->set_corner_radius(
370             (snapshot.roundedCorner.radius.x + snapshot.roundedCorner.radius.y) / 2.0);
371     layerInfo->set_background_blur_radius(snapshot.backgroundBlurRadius);
372     layerInfo->set_is_trusted_overlay(snapshot.isTrustedOverlay);
373     LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
374     LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
375                                            [&]() { return layerInfo->mutable_position(); });
376     LayerProtoHelper::writeToProto(snapshot.geomLayerBounds,
377                                    [&]() { return layerInfo->mutable_bounds(); });
378     LayerProtoHelper::writeToProto(snapshot.surfaceDamage,
379                                    [&]() { return layerInfo->mutable_damage_region(); });
380 
381     if (requestedState.hasColorTransform) {
382         LayerProtoHelper::writeToProto(snapshot.colorTransform,
383                                        layerInfo->mutable_color_transform());
384     }
385 
386     LayerProtoHelper::writeToProto(snapshot.croppedBufferSize.toFloatRect(),
387                                    [&]() { return layerInfo->mutable_source_bounds(); });
388     LayerProtoHelper::writeToProto(snapshot.transformedBounds,
389                                    [&]() { return layerInfo->mutable_screen_bounds(); });
390     LayerProtoHelper::writeToProto(snapshot.roundedCorner.cropRect,
391                                    [&]() { return layerInfo->mutable_corner_radius_crop(); });
392     layerInfo->set_shadow_radius(snapshot.shadowRadius);
393 
394     layerInfo->set_id(snapshot.uniqueSequence);
395     layerInfo->set_original_id(snapshot.sequence);
396     if (!snapshot.path.isClone()) {
397         layerInfo->set_name(requestedState.name);
398     } else {
399         layerInfo->set_name(requestedState.name + "(Mirror)");
400     }
401     layerInfo->set_type("Layer");
402 
403     LayerProtoHelper::writeToProto(requestedState.transparentRegion,
404                                    [&]() { return layerInfo->mutable_transparent_region(); });
405 
406     layerInfo->set_layer_stack(snapshot.outputFilter.layerStack.id);
407     layerInfo->set_z(requestedState.z);
408 
409     ui::Transform requestedTransform = requestedState.getTransform(0);
410     LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
411         return layerInfo->mutable_requested_position();
412     });
413 
414     LayerProtoHelper::writeToProto(requestedState.crop,
415                                    [&]() { return layerInfo->mutable_crop(); });
416 
417     layerInfo->set_is_opaque(snapshot.contentOpaque);
418     if (requestedState.externalTexture)
419         layerInfo->set_pixel_format(
420                 decodePixelFormat(requestedState.externalTexture->getPixelFormat()));
421     LayerProtoHelper::writeToProto(snapshot.color, [&]() { return layerInfo->mutable_color(); });
422     LayerProtoHelper::writeToProto(requestedState.color,
423                                    [&]() { return layerInfo->mutable_requested_color(); });
424     layerInfo->set_flags(requestedState.flags);
425 
426     LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
427                                              layerInfo->mutable_requested_transform());
428 
429     layerInfo->set_is_relative_of(requestedState.isRelativeOf);
430 
431     layerInfo->set_owner_uid(requestedState.ownerUid.val());
432 
433     if ((traceFlags & LayerTracing::TRACE_INPUT) && snapshot.hasInputInfo()) {
434         LayerProtoHelper::writeToProto(snapshot.inputInfo, {},
435                                        [&]() { return layerInfo->mutable_input_window_info(); });
436     }
437 
438     if (traceFlags & LayerTracing::TRACE_EXTRA) {
439         auto protoMap = layerInfo->mutable_metadata();
440         for (const auto& entry : requestedState.metadata.mMap) {
441             (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
442         }
443     }
444 
445     LayerProtoHelper::writeToProto(requestedState.destinationFrame,
446                                    [&]() { return layerInfo->mutable_destination_frame(); });
447 }
448 
writeDisplayInfoToProto(const frontend::DisplayInfos & displayInfos)449 google::protobuf::RepeatedPtrField<DisplayProto> LayerProtoHelper::writeDisplayInfoToProto(
450         const frontend::DisplayInfos& displayInfos) {
451     google::protobuf::RepeatedPtrField<DisplayProto> displays;
452     displays.Reserve(displayInfos.size());
453     for (const auto& [layerStack, displayInfo] : displayInfos) {
454         auto displayProto = displays.Add();
455         displayProto->set_id(displayInfo.info.displayId);
456         displayProto->set_layer_stack(layerStack.id);
457         displayProto->mutable_size()->set_w(displayInfo.info.logicalWidth);
458         displayProto->mutable_size()->set_h(displayInfo.info.logicalHeight);
459         writeTransformToProto(displayInfo.transform, displayProto->mutable_transform());
460         displayProto->set_is_virtual(displayInfo.isVirtual);
461     }
462     return displays;
463 }
464 
465 } // namespace surfaceflinger
466 } // namespace android
467 
468 // TODO(b/129481165): remove the #pragma below and fix conversion issues
469 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
470