blob: a2d1feb40df263cc50ccf58562976c8455fd9cb2 [file] [log] [blame]
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001/*
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
20#include "LayerStats.h"
21#include "DisplayHardware/HWComposer.h"
Yiwei Zhang7c64f172018-03-07 14:52:28 -080022#include "ui/DebugUtils.h"
Yiwei Zhang068e31b2018-02-21 13:02:45 -080023
24#include <android-base/stringprintf.h>
25#include <log/log.h>
Yiwei Zhang068e31b2018-02-21 13:02:45 -080026#include <utils/Trace.h>
27
28namespace android {
29
Yiwei Zhang5434a782018-12-05 18:06:32 -080030using base::StringAppendF;
31using base::StringPrintf;
32
Yiwei Zhang068e31b2018-02-21 13:02:45 -080033void LayerStats::enable() {
34 ATRACE_CALL();
35 std::lock_guard<std::mutex> lock(mMutex);
36 if (mEnabled) return;
Yiwei Zhang7c64f172018-03-07 14:52:28 -080037 mLayerShapeStatsMap.clear();
Yiwei Zhang068e31b2018-02-21 13:02:45 -080038 mEnabled = true;
39 ALOGD("Logging enabled");
40}
41
42void LayerStats::disable() {
43 ATRACE_CALL();
44 std::lock_guard<std::mutex> lock(mMutex);
45 if (!mEnabled) return;
46 mEnabled = false;
47 ALOGD("Logging disabled");
48}
49
50void LayerStats::clear() {
51 ATRACE_CALL();
52 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang7c64f172018-03-07 14:52:28 -080053 mLayerShapeStatsMap.clear();
Yiwei Zhang068e31b2018-02-21 13:02:45 -080054 ALOGD("Cleared current layer stats");
55}
56
57bool LayerStats::isEnabled() {
58 return mEnabled;
59}
60
61void LayerStats::traverseLayerTreeStatsLocked(
Chia-I Wu2f884132018-09-13 15:17:58 -070062 const std::vector<LayerProtoParser::Layer*>& layerTree,
Yiwei Zhang24180102018-04-09 11:05:39 -070063 const LayerProtoParser::LayerGlobal& layerGlobal,
64 std::vector<std::string>* const outLayerShapeVec) {
65 for (const auto& layer : layerTree) {
Yiwei Zhang068e31b2018-02-21 13:02:45 -080066 if (!layer) continue;
Yiwei Zhang24180102018-04-09 11:05:39 -070067 traverseLayerTreeStatsLocked(layer->children, layerGlobal, outLayerShapeVec);
Yiwei Zhang7c64f172018-03-07 14:52:28 -080068 std::string key = "";
Yiwei Zhang5434a782018-12-05 18:06:32 -080069 StringAppendF(&key, ",%s", layer->type.c_str());
70 StringAppendF(&key, ",%s", layerCompositionType(layer->hwcCompositionType));
71 StringAppendF(&key, ",%d", layer->isProtected);
72 StringAppendF(&key, ",%s", layerTransform(layer->hwcTransform));
73 StringAppendF(&key, ",%s", layerPixelFormat(layer->activeBuffer.format).c_str());
74 StringAppendF(&key, ",%s", layer->dataspace.c_str());
75 StringAppendF(&key, ",%s",
76 destinationLocation(layer->hwcFrame.left, layerGlobal.resolution[0], true));
77 StringAppendF(&key, ",%s",
78 destinationLocation(layer->hwcFrame.top, layerGlobal.resolution[1], false));
79 StringAppendF(&key, ",%s",
80 destinationSize(layer->hwcFrame.right - layer->hwcFrame.left,
81 layerGlobal.resolution[0], true));
82 StringAppendF(&key, ",%s",
83 destinationSize(layer->hwcFrame.bottom - layer->hwcFrame.top,
84 layerGlobal.resolution[1], false));
85 StringAppendF(&key, ",%s", scaleRatioWH(layer).c_str());
86 StringAppendF(&key, ",%s", alpha(static_cast<float>(layer->color.a)));
Yiwei Zhang7c64f172018-03-07 14:52:28 -080087
Yiwei Zhang24180102018-04-09 11:05:39 -070088 outLayerShapeVec->push_back(key);
Yiwei Zhang7c64f172018-03-07 14:52:28 -080089 ALOGV("%s", key.c_str());
Yiwei Zhang068e31b2018-02-21 13:02:45 -080090 }
91}
92
93void LayerStats::logLayerStats(const LayersProto& layersProto) {
94 ATRACE_CALL();
Yiwei Zhang7c64f172018-03-07 14:52:28 -080095 ALOGV("Logging");
Yiwei Zhang068e31b2018-02-21 13:02:45 -080096 auto layerGlobal = LayerProtoParser::generateLayerGlobalInfo(layersProto);
97 auto layerTree = LayerProtoParser::generateLayerTree(layersProto);
Yiwei Zhang7c64f172018-03-07 14:52:28 -080098 std::vector<std::string> layerShapeVec;
99
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800100 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu2f884132018-09-13 15:17:58 -0700101 traverseLayerTreeStatsLocked(layerTree.topLevelLayers, layerGlobal, &layerShapeVec);
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800102
103 std::string layerShapeKey =
Yiwei Zhang5434a782018-12-05 18:06:32 -0800104 StringPrintf("%d,%s,%s,%s", static_cast<int32_t>(layerShapeVec.size()),
105 layerGlobal.colorMode.c_str(), layerGlobal.colorTransform.c_str(),
106 layerTransform(layerGlobal.globalTransform));
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800107 ALOGV("%s", layerShapeKey.c_str());
108
109 std::sort(layerShapeVec.begin(), layerShapeVec.end(), std::greater<std::string>());
110 for (auto const& s : layerShapeVec) {
111 layerShapeKey += s;
112 }
113
114 mLayerShapeStatsMap[layerShapeKey]++;
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800115}
116
Yiwei Zhang5434a782018-12-05 18:06:32 -0800117void LayerStats::dump(std::string& result) {
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800118 ATRACE_CALL();
119 ALOGD("Dumping");
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800120 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800121 result.append("Frequency,LayerCount,ColorMode,ColorTransform,Orientation\n");
122 result.append("LayerType,CompositionType,IsProtected,Transform,PixelFormat,Dataspace,");
123 result.append("DstX,DstY,DstWidth,DstHeight,WScale,HScale,Alpha\n");
124 for (auto& u : mLayerShapeStatsMap) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800125 StringAppendF(&result, "%u,%s\n", u.second, u.first.c_str());
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800126 }
127}
128
129const char* LayerStats::destinationLocation(int32_t location, int32_t range, bool isHorizontal) {
130 static const char* locationArray[8] = {"0", "1/8", "1/4", "3/8", "1/2", "5/8", "3/4", "7/8"};
131 int32_t ratio = location * 8 / range;
132 if (ratio < 0) return "N/A";
133 if (isHorizontal) {
134 // X location is divided into 4 buckets {"0", "1/4", "1/2", "3/4"}
135 if (ratio > 6) return "3/4";
136 // use index 0, 2, 4, 6
137 return locationArray[ratio & ~1];
138 }
139 if (ratio > 7) return "7/8";
140 return locationArray[ratio];
141}
142
143const char* LayerStats::destinationSize(int32_t size, int32_t range, bool isWidth) {
144 static const char* sizeArray[8] = {"1/8", "1/4", "3/8", "1/2", "5/8", "3/4", "7/8", "1"};
145 int32_t ratio = size * 8 / range;
146 if (ratio < 0) return "N/A";
147 if (isWidth) {
148 // width is divided into 4 buckets {"1/4", "1/2", "3/4", "1"}
149 if (ratio > 6) return "1";
150 // use index 1, 3, 5, 7
151 return sizeArray[ratio | 1];
152 }
153 if (ratio > 7) return "1";
154 return sizeArray[ratio];
155}
156
157const char* LayerStats::layerTransform(int32_t transform) {
158 return getTransformName(static_cast<hwc_transform_t>(transform));
159}
160
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800161const char* LayerStats::layerCompositionType(int32_t compositionType) {
162 return getCompositionName(static_cast<hwc2_composition_t>(compositionType));
163}
164
George Burgess IV3ebb48d2018-10-03 12:08:27 -0700165std::string LayerStats::layerPixelFormat(int32_t pixelFormat) {
166 return decodePixelFormat(pixelFormat);
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800167}
168
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800169std::string LayerStats::scaleRatioWH(const LayerProtoParser::Layer* layer) {
170 if (!layer->type.compare("ColorLayer")) return "N/A,N/A";
171 std::string ret = "";
172 if (isRotated(layer->hwcTransform)) {
173 ret += scaleRatio(layer->hwcFrame.right - layer->hwcFrame.left,
174 static_cast<int32_t>(layer->hwcCrop.bottom - layer->hwcCrop.top));
175 ret += ",";
176 ret += scaleRatio(layer->hwcFrame.bottom - layer->hwcFrame.top,
177 static_cast<int32_t>(layer->hwcCrop.right - layer->hwcCrop.left));
178 } else {
179 ret += scaleRatio(layer->hwcFrame.right - layer->hwcFrame.left,
180 static_cast<int32_t>(layer->hwcCrop.right - layer->hwcCrop.left));
181 ret += ",";
182 ret += scaleRatio(layer->hwcFrame.bottom - layer->hwcFrame.top,
183 static_cast<int32_t>(layer->hwcCrop.bottom - layer->hwcCrop.top));
184 }
185 return ret;
186}
187
188const char* LayerStats::scaleRatio(int32_t destinationScale, int32_t sourceScale) {
189 // Make scale buckets from <1/64 to >= 16, to avoid floating point
190 // calculation, x64 on destinationScale first
191 int32_t scale = destinationScale * 64 / sourceScale;
192 if (!scale) return "<1/64";
193 if (scale < 2) return "1/64";
194 if (scale < 4) return "1/32";
195 if (scale < 8) return "1/16";
196 if (scale < 16) return "1/8";
197 if (scale < 32) return "1/4";
198 if (scale < 64) return "1/2";
199 if (scale < 128) return "1";
200 if (scale < 256) return "2";
201 if (scale < 512) return "4";
202 if (scale < 1024) return "8";
203 return ">=16";
204}
205
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800206const char* LayerStats::alpha(float a) {
207 if (a == 1.0f) return "1.0";
208 if (a > 0.9f) return "0.99";
209 if (a > 0.8f) return "0.9";
210 if (a > 0.7f) return "0.8";
211 if (a > 0.6f) return "0.7";
212 if (a > 0.5f) return "0.6";
213 if (a > 0.4f) return "0.5";
214 if (a > 0.3f) return "0.4";
215 if (a > 0.2f) return "0.3";
216 if (a > 0.1f) return "0.2";
217 if (a > 0.0f) return "0.1";
218 return "0.0";
219}
220
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800221bool LayerStats::isRotated(int32_t transform) {
222 return transform & HWC_TRANSFORM_ROT_90;
223}
224
225bool LayerStats::isVFlipped(int32_t transform) {
226 return transform & HWC_TRANSFORM_FLIP_V;
227}
228
229bool LayerStats::isHFlipped(int32_t transform) {
230 return transform & HWC_TRANSFORM_FLIP_H;
231}
232
Yiwei Zhang7c64f172018-03-07 14:52:28 -0800233} // namespace android