LayerStats.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2018 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. #undef LOG_TAG
  17. #define LOG_TAG "LayerStats"
  18. #define ATRACE_TAG ATRACE_TAG_GRAPHICS
  19. #include "LayerStats.h"
  20. #include "DisplayHardware/HWComposer.h"
  21. #include "ui/DebugUtils.h"
  22. #include <android-base/stringprintf.h>
  23. #include <log/log.h>
  24. #include <utils/Trace.h>
  25. namespace android {
  26. using base::StringAppendF;
  27. using base::StringPrintf;
  28. void LayerStats::enable() {
  29. ATRACE_CALL();
  30. std::lock_guard<std::mutex> lock(mMutex);
  31. if (mEnabled) return;
  32. mLayerShapeStatsMap.clear();
  33. mEnabled = true;
  34. ALOGD("Logging enabled");
  35. }
  36. void LayerStats::disable() {
  37. ATRACE_CALL();
  38. std::lock_guard<std::mutex> lock(mMutex);
  39. if (!mEnabled) return;
  40. mEnabled = false;
  41. ALOGD("Logging disabled");
  42. }
  43. void LayerStats::clear() {
  44. ATRACE_CALL();
  45. std::lock_guard<std::mutex> lock(mMutex);
  46. mLayerShapeStatsMap.clear();
  47. ALOGD("Cleared current layer stats");
  48. }
  49. bool LayerStats::isEnabled() {
  50. return mEnabled;
  51. }
  52. void LayerStats::traverseLayerTreeStatsLocked(
  53. const std::vector<LayerProtoParser::Layer*>& layerTree,
  54. const LayerProtoParser::LayerGlobal& layerGlobal,
  55. std::vector<std::string>* const outLayerShapeVec) {
  56. for (const auto& layer : layerTree) {
  57. if (!layer) continue;
  58. traverseLayerTreeStatsLocked(layer->children, layerGlobal, outLayerShapeVec);
  59. std::string key = "";
  60. StringAppendF(&key, ",%s", layer->type.c_str());
  61. StringAppendF(&key, ",%s", layerCompositionType(layer->hwcCompositionType));
  62. StringAppendF(&key, ",%d", layer->isProtected);
  63. StringAppendF(&key, ",%s", layerTransform(layer->hwcTransform));
  64. StringAppendF(&key, ",%s", layerPixelFormat(layer->activeBuffer.format).c_str());
  65. StringAppendF(&key, ",%s", layer->dataspace.c_str());
  66. StringAppendF(&key, ",%s",
  67. destinationLocation(layer->hwcFrame.left, layerGlobal.resolution[0], true));
  68. StringAppendF(&key, ",%s",
  69. destinationLocation(layer->hwcFrame.top, layerGlobal.resolution[1], false));
  70. StringAppendF(&key, ",%s",
  71. destinationSize(layer->hwcFrame.right - layer->hwcFrame.left,
  72. layerGlobal.resolution[0], true));
  73. StringAppendF(&key, ",%s",
  74. destinationSize(layer->hwcFrame.bottom - layer->hwcFrame.top,
  75. layerGlobal.resolution[1], false));
  76. StringAppendF(&key, ",%s", scaleRatioWH(layer).c_str());
  77. StringAppendF(&key, ",%s", alpha(static_cast<float>(layer->color.a)));
  78. outLayerShapeVec->push_back(key);
  79. ALOGV("%s", key.c_str());
  80. }
  81. }
  82. void LayerStats::logLayerStats(const LayersProto& layersProto) {
  83. ATRACE_CALL();
  84. ALOGV("Logging");
  85. auto layerGlobal = LayerProtoParser::generateLayerGlobalInfo(layersProto);
  86. auto layerTree = LayerProtoParser::generateLayerTree(layersProto);
  87. std::vector<std::string> layerShapeVec;
  88. std::lock_guard<std::mutex> lock(mMutex);
  89. traverseLayerTreeStatsLocked(layerTree.topLevelLayers, layerGlobal, &layerShapeVec);
  90. std::string layerShapeKey =
  91. StringPrintf("%d,%s,%s,%s", static_cast<int32_t>(layerShapeVec.size()),
  92. layerGlobal.colorMode.c_str(), layerGlobal.colorTransform.c_str(),
  93. layerTransform(layerGlobal.globalTransform));
  94. ALOGV("%s", layerShapeKey.c_str());
  95. std::sort(layerShapeVec.begin(), layerShapeVec.end(), std::greater<std::string>());
  96. for (auto const& s : layerShapeVec) {
  97. layerShapeKey += s;
  98. }
  99. mLayerShapeStatsMap[layerShapeKey]++;
  100. }
  101. void LayerStats::dump(std::string& result) {
  102. ATRACE_CALL();
  103. ALOGD("Dumping");
  104. std::lock_guard<std::mutex> lock(mMutex);
  105. result.append("Frequency,LayerCount,ColorMode,ColorTransform,Orientation\n");
  106. result.append("LayerType,CompositionType,IsProtected,Transform,PixelFormat,Dataspace,");
  107. result.append("DstX,DstY,DstWidth,DstHeight,WScale,HScale,Alpha\n");
  108. for (auto& u : mLayerShapeStatsMap) {
  109. StringAppendF(&result, "%u,%s\n", u.second, u.first.c_str());
  110. }
  111. }
  112. const char* LayerStats::destinationLocation(int32_t location, int32_t range, bool isHorizontal) {
  113. static const char* locationArray[8] = {"0", "1/8", "1/4", "3/8", "1/2", "5/8", "3/4", "7/8"};
  114. int32_t ratio = location * 8 / range;
  115. if (ratio < 0) return "N/A";
  116. if (isHorizontal) {
  117. // X location is divided into 4 buckets {"0", "1/4", "1/2", "3/4"}
  118. if (ratio > 6) return "3/4";
  119. // use index 0, 2, 4, 6
  120. return locationArray[ratio & ~1];
  121. }
  122. if (ratio > 7) return "7/8";
  123. return locationArray[ratio];
  124. }
  125. const char* LayerStats::destinationSize(int32_t size, int32_t range, bool isWidth) {
  126. static const char* sizeArray[8] = {"1/8", "1/4", "3/8", "1/2", "5/8", "3/4", "7/8", "1"};
  127. int32_t ratio = size * 8 / range;
  128. if (ratio < 0) return "N/A";
  129. if (isWidth) {
  130. // width is divided into 4 buckets {"1/4", "1/2", "3/4", "1"}
  131. if (ratio > 6) return "1";
  132. // use index 1, 3, 5, 7
  133. return sizeArray[ratio | 1];
  134. }
  135. if (ratio > 7) return "1";
  136. return sizeArray[ratio];
  137. }
  138. const char* LayerStats::layerTransform(int32_t transform) {
  139. return getTransformName(static_cast<hwc_transform_t>(transform));
  140. }
  141. const char* LayerStats::layerCompositionType(int32_t compositionType) {
  142. return getCompositionName(static_cast<hwc2_composition_t>(compositionType));
  143. }
  144. std::string LayerStats::layerPixelFormat(int32_t pixelFormat) {
  145. return decodePixelFormat(pixelFormat);
  146. }
  147. std::string LayerStats::scaleRatioWH(const LayerProtoParser::Layer* layer) {
  148. if (!layer->type.compare("ColorLayer")) return "N/A,N/A";
  149. std::string ret = "";
  150. if (isRotated(layer->hwcTransform)) {
  151. ret += scaleRatio(layer->hwcFrame.right - layer->hwcFrame.left,
  152. static_cast<int32_t>(layer->hwcCrop.bottom - layer->hwcCrop.top));
  153. ret += ",";
  154. ret += scaleRatio(layer->hwcFrame.bottom - layer->hwcFrame.top,
  155. static_cast<int32_t>(layer->hwcCrop.right - layer->hwcCrop.left));
  156. } else {
  157. ret += scaleRatio(layer->hwcFrame.right - layer->hwcFrame.left,
  158. static_cast<int32_t>(layer->hwcCrop.right - layer->hwcCrop.left));
  159. ret += ",";
  160. ret += scaleRatio(layer->hwcFrame.bottom - layer->hwcFrame.top,
  161. static_cast<int32_t>(layer->hwcCrop.bottom - layer->hwcCrop.top));
  162. }
  163. return ret;
  164. }
  165. const char* LayerStats::scaleRatio(int32_t destinationScale, int32_t sourceScale) {
  166. // Make scale buckets from <1/64 to >= 16, to avoid floating point
  167. // calculation, x64 on destinationScale first
  168. int32_t scale = destinationScale * 64 / sourceScale;
  169. if (!scale) return "<1/64";
  170. if (scale < 2) return "1/64";
  171. if (scale < 4) return "1/32";
  172. if (scale < 8) return "1/16";
  173. if (scale < 16) return "1/8";
  174. if (scale < 32) return "1/4";
  175. if (scale < 64) return "1/2";
  176. if (scale < 128) return "1";
  177. if (scale < 256) return "2";
  178. if (scale < 512) return "4";
  179. if (scale < 1024) return "8";
  180. return ">=16";
  181. }
  182. const char* LayerStats::alpha(float a) {
  183. if (a == 1.0f) return "1.0";
  184. if (a > 0.9f) return "0.99";
  185. if (a > 0.8f) return "0.9";
  186. if (a > 0.7f) return "0.8";
  187. if (a > 0.6f) return "0.7";
  188. if (a > 0.5f) return "0.6";
  189. if (a > 0.4f) return "0.5";
  190. if (a > 0.3f) return "0.4";
  191. if (a > 0.2f) return "0.3";
  192. if (a > 0.1f) return "0.2";
  193. if (a > 0.0f) return "0.1";
  194. return "0.0";
  195. }
  196. bool LayerStats::isRotated(int32_t transform) {
  197. return transform & HWC_TRANSFORM_ROT_90;
  198. }
  199. bool LayerStats::isVFlipped(int32_t transform) {
  200. return transform & HWC_TRANSFORM_FLIP_V;
  201. }
  202. bool LayerStats::isHFlipped(int32_t transform) {
  203. return transform & HWC_TRANSFORM_FLIP_H;
  204. }
  205. } // namespace android