LayerProtoHelper.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include "LayerProtoHelper.h"
  17. namespace android {
  18. namespace surfaceflinger {
  19. void LayerProtoHelper::writePositionToProto(const float x, const float y,
  20. std::function<PositionProto*()> getPositionProto) {
  21. if (x != 0 || y != 0) {
  22. // Use a lambda do avoid writing the object header when the object is empty
  23. PositionProto* position = getPositionProto();
  24. position->set_x(x);
  25. position->set_y(y);
  26. }
  27. }
  28. void LayerProtoHelper::writeSizeToProto(const uint32_t w, const uint32_t h,
  29. std::function<SizeProto*()> getSizeProto) {
  30. if (w != 0 || h != 0) {
  31. // Use a lambda do avoid writing the object header when the object is empty
  32. SizeProto* size = getSizeProto();
  33. size->set_w(w);
  34. size->set_h(h);
  35. }
  36. }
  37. void LayerProtoHelper::writeToProto(const Region& region,
  38. std::function<RegionProto*()> getRegionProto) {
  39. if (region.isEmpty()) {
  40. return;
  41. }
  42. Region::const_iterator head = region.begin();
  43. Region::const_iterator const tail = region.end();
  44. // Use a lambda do avoid writing the object header when the object is empty
  45. RegionProto* regionProto = getRegionProto();
  46. while (head != tail) {
  47. std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); };
  48. writeToProto(*head, getProtoRect);
  49. head++;
  50. }
  51. }
  52. void LayerProtoHelper::writeToProto(const Rect& rect, std::function<RectProto*()> getRectProto) {
  53. if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
  54. // Use a lambda do avoid writing the object header when the object is empty
  55. RectProto* rectProto = getRectProto();
  56. rectProto->set_left(rect.left);
  57. rectProto->set_top(rect.top);
  58. rectProto->set_bottom(rect.bottom);
  59. rectProto->set_right(rect.right);
  60. }
  61. }
  62. void LayerProtoHelper::writeToProto(const FloatRect& rect,
  63. std::function<FloatRectProto*()> getFloatRectProto) {
  64. if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
  65. // Use a lambda do avoid writing the object header when the object is empty
  66. FloatRectProto* rectProto = getFloatRectProto();
  67. rectProto->set_left(rect.left);
  68. rectProto->set_top(rect.top);
  69. rectProto->set_bottom(rect.bottom);
  70. rectProto->set_right(rect.right);
  71. }
  72. }
  73. void LayerProtoHelper::writeToProto(const half4 color, std::function<ColorProto*()> getColorProto) {
  74. if (color.r != 0 || color.g != 0 || color.b != 0 || color.a != 0) {
  75. // Use a lambda do avoid writing the object header when the object is empty
  76. ColorProto* colorProto = getColorProto();
  77. colorProto->set_r(color.r);
  78. colorProto->set_g(color.g);
  79. colorProto->set_b(color.b);
  80. colorProto->set_a(color.a);
  81. }
  82. }
  83. void LayerProtoHelper::writeToProto(const ui::Transform& transform,
  84. TransformProto* transformProto) {
  85. const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
  86. transformProto->set_type(type);
  87. // Rotations that are 90/180/270 have their own type so the transform matrix can be
  88. // reconstructed later. All other rotation have the type UKNOWN so we need to save the transform
  89. // values in that case.
  90. if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
  91. transformProto->set_dsdx(transform[0][0]);
  92. transformProto->set_dtdx(transform[0][1]);
  93. transformProto->set_dsdy(transform[1][0]);
  94. transformProto->set_dtdy(transform[1][1]);
  95. }
  96. }
  97. void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer,
  98. std::function<ActiveBufferProto*()> getActiveBufferProto) {
  99. if (buffer->getWidth() != 0 || buffer->getHeight() != 0 || buffer->getStride() != 0 ||
  100. buffer->format != 0) {
  101. // Use a lambda do avoid writing the object header when the object is empty
  102. ActiveBufferProto* activeBufferProto = getActiveBufferProto();
  103. activeBufferProto->set_width(buffer->getWidth());
  104. activeBufferProto->set_height(buffer->getHeight());
  105. activeBufferProto->set_stride(buffer->getStride());
  106. activeBufferProto->set_format(buffer->format);
  107. }
  108. }
  109. void LayerProtoHelper::writeToProto(
  110. const InputWindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds,
  111. std::function<InputWindowInfoProto*()> getInputWindowInfoProto) {
  112. if (inputInfo.token == nullptr) {
  113. return;
  114. }
  115. InputWindowInfoProto* proto = getInputWindowInfoProto();
  116. proto->set_layout_params_flags(inputInfo.layoutParamsFlags);
  117. proto->set_layout_params_type(inputInfo.layoutParamsType);
  118. LayerProtoHelper::writeToProto({inputInfo.frameLeft, inputInfo.frameTop, inputInfo.frameRight,
  119. inputInfo.frameBottom},
  120. [&]() { return proto->mutable_frame(); });
  121. LayerProtoHelper::writeToProto(inputInfo.touchableRegion,
  122. [&]() { return proto->mutable_touchable_region(); });
  123. proto->set_surface_inset(inputInfo.surfaceInset);
  124. proto->set_visible(inputInfo.visible);
  125. proto->set_can_receive_keys(inputInfo.canReceiveKeys);
  126. proto->set_has_focus(inputInfo.hasFocus);
  127. proto->set_has_wallpaper(inputInfo.hasWallpaper);
  128. proto->set_global_scale_factor(inputInfo.globalScaleFactor);
  129. proto->set_window_x_scale(inputInfo.windowXScale);
  130. proto->set_window_y_scale(inputInfo.windowYScale);
  131. proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop);
  132. auto cropLayer = touchableRegionBounds.promote();
  133. if (cropLayer != nullptr) {
  134. proto->set_crop_layer_id(cropLayer->sequence);
  135. LayerProtoHelper::writeToProto(cropLayer->getScreenBounds(
  136. false /* reduceTransparentRegion */),
  137. [&]() { return proto->mutable_touchable_region_crop(); });
  138. }
  139. }
  140. } // namespace surfaceflinger
  141. } // namespace android