LayerProtoParser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <android-base/stringprintf.h>
  17. #include <layerproto/LayerProtoParser.h>
  18. #include <ui/DebugUtils.h>
  19. using android::base::StringAppendF;
  20. using android::base::StringPrintf;
  21. namespace android {
  22. namespace surfaceflinger {
  23. bool sortLayers(LayerProtoParser::Layer* lhs, const LayerProtoParser::Layer* rhs) {
  24. uint32_t ls = lhs->layerStack;
  25. uint32_t rs = rhs->layerStack;
  26. if (ls != rs) return ls < rs;
  27. int32_t lz = lhs->z;
  28. int32_t rz = rhs->z;
  29. if (lz != rz) {
  30. return lz < rz;
  31. }
  32. return lhs->id < rhs->id;
  33. }
  34. const LayerProtoParser::LayerGlobal LayerProtoParser::generateLayerGlobalInfo(
  35. const LayersProto& layersProto) {
  36. LayerGlobal layerGlobal;
  37. layerGlobal.resolution = {layersProto.resolution().w(), layersProto.resolution().h()};
  38. layerGlobal.colorMode = layersProto.color_mode();
  39. layerGlobal.colorTransform = layersProto.color_transform();
  40. layerGlobal.globalTransform = layersProto.global_transform();
  41. return layerGlobal;
  42. }
  43. LayerProtoParser::LayerTree LayerProtoParser::generateLayerTree(const LayersProto& layersProto) {
  44. LayerTree layerTree;
  45. layerTree.allLayers = generateLayerList(layersProto);
  46. // find and sort the top-level layers
  47. for (Layer& layer : layerTree.allLayers) {
  48. if (layer.parent == nullptr) {
  49. layerTree.topLevelLayers.push_back(&layer);
  50. }
  51. }
  52. std::sort(layerTree.topLevelLayers.begin(), layerTree.topLevelLayers.end(), sortLayers);
  53. return layerTree;
  54. }
  55. std::vector<LayerProtoParser::Layer> LayerProtoParser::generateLayerList(
  56. const LayersProto& layersProto) {
  57. std::vector<Layer> layerList;
  58. std::unordered_map<int32_t, Layer*> layerMap;
  59. // build the layer list and the layer map
  60. layerList.reserve(layersProto.layers_size());
  61. layerMap.reserve(layersProto.layers_size());
  62. for (int i = 0; i < layersProto.layers_size(); i++) {
  63. layerList.emplace_back(generateLayer(layersProto.layers(i)));
  64. // this works because layerList never changes capacity
  65. layerMap[layerList.back().id] = &layerList.back();
  66. }
  67. // fix up children and relatives
  68. for (int i = 0; i < layersProto.layers_size(); i++) {
  69. updateChildrenAndRelative(layersProto.layers(i), layerMap);
  70. }
  71. return layerList;
  72. }
  73. LayerProtoParser::Layer LayerProtoParser::generateLayer(const LayerProto& layerProto) {
  74. Layer layer;
  75. layer.id = layerProto.id();
  76. layer.name = layerProto.name();
  77. layer.type = layerProto.type();
  78. layer.transparentRegion = generateRegion(layerProto.transparent_region());
  79. layer.visibleRegion = generateRegion(layerProto.visible_region());
  80. layer.damageRegion = generateRegion(layerProto.damage_region());
  81. layer.layerStack = layerProto.layer_stack();
  82. layer.z = layerProto.z();
  83. layer.position = {layerProto.position().x(), layerProto.position().y()};
  84. layer.requestedPosition = {layerProto.requested_position().x(),
  85. layerProto.requested_position().y()};
  86. layer.size = {layerProto.size().w(), layerProto.size().h()};
  87. layer.crop = generateRect(layerProto.crop());
  88. layer.isOpaque = layerProto.is_opaque();
  89. layer.invalidate = layerProto.invalidate();
  90. layer.dataspace = layerProto.dataspace();
  91. layer.pixelFormat = layerProto.pixel_format();
  92. layer.color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(),
  93. layerProto.color().a()};
  94. layer.requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
  95. layerProto.requested_color().b(), layerProto.requested_color().a()};
  96. layer.flags = layerProto.flags();
  97. layer.transform = generateTransform(layerProto.transform());
  98. layer.requestedTransform = generateTransform(layerProto.requested_transform());
  99. layer.activeBuffer = generateActiveBuffer(layerProto.active_buffer());
  100. layer.bufferTransform = generateTransform(layerProto.buffer_transform());
  101. layer.queuedFrames = layerProto.queued_frames();
  102. layer.refreshPending = layerProto.refresh_pending();
  103. layer.hwcFrame = generateRect(layerProto.hwc_frame());
  104. layer.hwcCrop = generateFloatRect(layerProto.hwc_crop());
  105. layer.hwcTransform = layerProto.hwc_transform();
  106. layer.hwcCompositionType = layerProto.hwc_composition_type();
  107. layer.isProtected = layerProto.is_protected();
  108. layer.cornerRadius = layerProto.corner_radius();
  109. for (const auto& entry : layerProto.metadata()) {
  110. const std::string& dataStr = entry.second;
  111. std::vector<uint8_t>& outData = layer.metadata.mMap[entry.first];
  112. outData.resize(dataStr.size());
  113. memcpy(outData.data(), dataStr.data(), dataStr.size());
  114. }
  115. return layer;
  116. }
  117. LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
  118. LayerProtoParser::Region region;
  119. for (int i = 0; i < regionProto.rect_size(); i++) {
  120. const RectProto& rectProto = regionProto.rect(i);
  121. region.rects.push_back(generateRect(rectProto));
  122. }
  123. return region;
  124. }
  125. LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
  126. LayerProtoParser::Rect rect;
  127. rect.left = rectProto.left();
  128. rect.top = rectProto.top();
  129. rect.right = rectProto.right();
  130. rect.bottom = rectProto.bottom();
  131. return rect;
  132. }
  133. LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
  134. LayerProtoParser::FloatRect rect;
  135. rect.left = rectProto.left();
  136. rect.top = rectProto.top();
  137. rect.right = rectProto.right();
  138. rect.bottom = rectProto.bottom();
  139. return rect;
  140. }
  141. LayerProtoParser::Transform LayerProtoParser::generateTransform(
  142. const TransformProto& transformProto) {
  143. LayerProtoParser::Transform transform;
  144. transform.dsdx = transformProto.dsdx();
  145. transform.dtdx = transformProto.dtdx();
  146. transform.dsdy = transformProto.dsdy();
  147. transform.dtdy = transformProto.dtdy();
  148. return transform;
  149. }
  150. LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
  151. const ActiveBufferProto& activeBufferProto) {
  152. LayerProtoParser::ActiveBuffer activeBuffer;
  153. activeBuffer.width = activeBufferProto.width();
  154. activeBuffer.height = activeBufferProto.height();
  155. activeBuffer.stride = activeBufferProto.stride();
  156. activeBuffer.format = activeBufferProto.format();
  157. return activeBuffer;
  158. }
  159. void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
  160. std::unordered_map<int32_t, Layer*>& layerMap) {
  161. auto currLayer = layerMap[layerProto.id()];
  162. for (int i = 0; i < layerProto.children_size(); i++) {
  163. if (layerMap.count(layerProto.children(i)) > 0) {
  164. currLayer->children.push_back(layerMap[layerProto.children(i)]);
  165. }
  166. }
  167. for (int i = 0; i < layerProto.relatives_size(); i++) {
  168. if (layerMap.count(layerProto.relatives(i)) > 0) {
  169. currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
  170. }
  171. }
  172. if (layerProto.parent() != -1) {
  173. if (layerMap.count(layerProto.parent()) > 0) {
  174. currLayer->parent = layerMap[layerProto.parent()];
  175. }
  176. }
  177. if (layerProto.z_order_relative_of() != -1) {
  178. if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
  179. currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
  180. }
  181. }
  182. }
  183. std::string LayerProtoParser::layerTreeToString(const LayerTree& layerTree) {
  184. std::string result;
  185. for (const LayerProtoParser::Layer* layer : layerTree.topLevelLayers) {
  186. if (layer->zOrderRelativeOf != nullptr) {
  187. continue;
  188. }
  189. result.append(layerToString(layer));
  190. }
  191. return result;
  192. }
  193. std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
  194. std::string result;
  195. std::vector<Layer*> traverse(layer->relatives);
  196. for (LayerProtoParser::Layer* child : layer->children) {
  197. if (child->zOrderRelativeOf != nullptr) {
  198. continue;
  199. }
  200. traverse.push_back(child);
  201. }
  202. std::sort(traverse.begin(), traverse.end(), sortLayers);
  203. size_t i = 0;
  204. for (; i < traverse.size(); i++) {
  205. auto& relative = traverse[i];
  206. if (relative->z >= 0) {
  207. break;
  208. }
  209. result.append(layerToString(relative));
  210. }
  211. result.append(layer->to_string());
  212. result.append("\n");
  213. for (; i < traverse.size(); i++) {
  214. auto& relative = traverse[i];
  215. result.append(layerToString(relative));
  216. }
  217. return result;
  218. }
  219. std::string LayerProtoParser::ActiveBuffer::to_string() const {
  220. return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
  221. decodePixelFormat(format).c_str());
  222. }
  223. std::string LayerProtoParser::Transform::to_string() const {
  224. return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
  225. static_cast<double>(dtdx), static_cast<double>(dsdy),
  226. static_cast<double>(dtdy));
  227. }
  228. std::string LayerProtoParser::Rect::to_string() const {
  229. return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
  230. }
  231. std::string LayerProtoParser::FloatRect::to_string() const {
  232. return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
  233. }
  234. std::string LayerProtoParser::Region::to_string(const char* what) const {
  235. std::string result =
  236. StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
  237. static_cast<int>(rects.size()));
  238. for (auto& rect : rects) {
  239. StringAppendF(&result, " %s\n", rect.to_string().c_str());
  240. }
  241. return result;
  242. }
  243. std::string LayerProtoParser::Layer::to_string() const {
  244. std::string result;
  245. StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
  246. result.append(transparentRegion.to_string("TransparentRegion").c_str());
  247. result.append(visibleRegion.to_string("VisibleRegion").c_str());
  248. result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
  249. StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
  250. z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
  251. size.y);
  252. StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
  253. StringAppendF(&result, "cornerRadius=%f, ", cornerRadius);
  254. StringAppendF(&result, "isProtected=%1d, ", isProtected);
  255. StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
  256. StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
  257. StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
  258. StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
  259. static_cast<double>(color.r), static_cast<double>(color.g),
  260. static_cast<double>(color.b), static_cast<double>(color.a), flags);
  261. StringAppendF(&result, "tr=%s", transform.to_string().c_str());
  262. result.append("\n");
  263. StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
  264. StringAppendF(&result, " zOrderRelativeOf=%s\n",
  265. zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
  266. StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
  267. StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str());
  268. StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
  269. StringAppendF(&result, " metadata={");
  270. bool first = true;
  271. for (const auto& entry : metadata.mMap) {
  272. if (!first) result.append(", ");
  273. first = false;
  274. result.append(metadata.itemToString(entry.first, ":"));
  275. }
  276. result.append("}");
  277. return result;
  278. }
  279. } // namespace surfaceflinger
  280. } // namespace android