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