chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 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 | */ |
chaviw | 5bf9d68 | 2017-10-25 16:31:08 -0700 | [diff] [blame] | 16 | #include <android-base/stringprintf.h> |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 17 | #include <layerproto/LayerProtoParser.h> |
chaviw | 5bf9d68 | 2017-10-25 16:31:08 -0700 | [diff] [blame] | 18 | #include <ui/DebugUtils.h> |
| 19 | |
| 20 | using android::base::StringAppendF; |
| 21 | using android::base::StringPrintf; |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace surfaceflinger { |
chaviw | 5bf9d68 | 2017-10-25 16:31:08 -0700 | [diff] [blame] | 25 | |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 26 | bool sortLayers(LayerProtoParser::Layer* lhs, const LayerProtoParser::Layer* rhs) { |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 27 | uint32_t ls = lhs->layerStack; |
| 28 | uint32_t rs = rhs->layerStack; |
| 29 | if (ls != rs) return ls < rs; |
| 30 | |
Robert Carr | 83f8e4d | 2017-11-15 14:37:37 -0800 | [diff] [blame] | 31 | int32_t lz = lhs->z; |
| 32 | int32_t rz = rhs->z; |
| 33 | if (lz != rz) { |
| 34 | return (lz > rz) ? 1 : -1; |
| 35 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 36 | |
| 37 | return lhs->id < rhs->id; |
| 38 | } |
| 39 | |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 40 | bool sortLayerUniquePtrs(const std::unique_ptr<LayerProtoParser::Layer>& lhs, |
| 41 | const std::unique_ptr<LayerProtoParser::Layer>& rhs) { |
| 42 | return sortLayers(lhs.get(), rhs.get()); |
| 43 | } |
| 44 | |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 45 | const LayerProtoParser::LayerGlobal LayerProtoParser::generateLayerGlobalInfo( |
| 46 | const LayersProto& layersProto) { |
Yiwei Zhang | 60d1a19 | 2018-03-07 14:52:28 -0800 | [diff] [blame] | 47 | LayerGlobal layerGlobal; |
| 48 | layerGlobal.resolution = {layersProto.resolution().w(), layersProto.resolution().h()}; |
| 49 | layerGlobal.colorMode = layersProto.color_mode(); |
| 50 | layerGlobal.colorTransform = layersProto.color_transform(); |
| 51 | layerGlobal.globalTransform = layersProto.global_transform(); |
| 52 | return layerGlobal; |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 53 | } |
| 54 | |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 55 | std::vector<std::unique_ptr<LayerProtoParser::Layer>> LayerProtoParser::generateLayerTree( |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 56 | const LayersProto& layersProto) { |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 57 | std::unordered_map<int32_t, LayerProtoParser::Layer*> layerMap = generateMap(layersProto); |
| 58 | std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers; |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 59 | |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 60 | for (std::pair<int32_t, Layer*> kv : layerMap) { |
| 61 | if (kv.second->parent == nullptr) { |
| 62 | // Make unique_ptr for top level layers since they are not children. This ensures there |
| 63 | // will only be one unique_ptr made for each layer. |
| 64 | layers.push_back(std::unique_ptr<Layer>(kv.second)); |
| 65 | } |
| 66 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 67 | |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 68 | std::sort(layers.begin(), layers.end(), sortLayerUniquePtrs); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 69 | return layers; |
| 70 | } |
| 71 | |
| 72 | std::unordered_map<int32_t, LayerProtoParser::Layer*> LayerProtoParser::generateMap( |
| 73 | const LayersProto& layersProto) { |
| 74 | std::unordered_map<int32_t, Layer*> layerMap; |
| 75 | |
| 76 | for (int i = 0; i < layersProto.layers_size(); i++) { |
| 77 | const LayerProto& layerProto = layersProto.layers(i); |
| 78 | layerMap[layerProto.id()] = generateLayer(layerProto); |
| 79 | } |
| 80 | |
| 81 | for (int i = 0; i < layersProto.layers_size(); i++) { |
| 82 | const LayerProto& layerProto = layersProto.layers(i); |
| 83 | updateChildrenAndRelative(layerProto, layerMap); |
| 84 | } |
| 85 | |
| 86 | return layerMap; |
| 87 | } |
| 88 | |
| 89 | LayerProtoParser::Layer* LayerProtoParser::generateLayer(const LayerProto& layerProto) { |
| 90 | Layer* layer = new Layer(); |
| 91 | layer->id = layerProto.id(); |
| 92 | layer->name = layerProto.name(); |
| 93 | layer->type = layerProto.type(); |
| 94 | layer->transparentRegion = generateRegion(layerProto.transparent_region()); |
| 95 | layer->visibleRegion = generateRegion(layerProto.visible_region()); |
| 96 | layer->damageRegion = generateRegion(layerProto.damage_region()); |
| 97 | layer->layerStack = layerProto.layer_stack(); |
| 98 | layer->z = layerProto.z(); |
| 99 | layer->position = {layerProto.position().x(), layerProto.position().y()}; |
| 100 | layer->requestedPosition = {layerProto.requested_position().x(), |
| 101 | layerProto.requested_position().y()}; |
| 102 | layer->size = {layerProto.size().w(), layerProto.size().h()}; |
| 103 | layer->crop = generateRect(layerProto.crop()); |
| 104 | layer->finalCrop = generateRect(layerProto.final_crop()); |
| 105 | layer->isOpaque = layerProto.is_opaque(); |
| 106 | layer->invalidate = layerProto.invalidate(); |
| 107 | layer->dataspace = layerProto.dataspace(); |
| 108 | layer->pixelFormat = layerProto.pixel_format(); |
| 109 | layer->color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(), |
| 110 | layerProto.color().a()}; |
| 111 | layer->requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(), |
| 112 | layerProto.requested_color().b(), layerProto.requested_color().a()}; |
| 113 | layer->flags = layerProto.flags(); |
| 114 | layer->transform = generateTransform(layerProto.transform()); |
| 115 | layer->requestedTransform = generateTransform(layerProto.requested_transform()); |
| 116 | layer->activeBuffer = generateActiveBuffer(layerProto.active_buffer()); |
Yichi Chen | 6ca3519 | 2018-05-29 12:20:43 +0800 | [diff] [blame^] | 117 | layer->bufferTransform = generateTransform(layerProto.buffer_transform()); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 118 | layer->queuedFrames = layerProto.queued_frames(); |
| 119 | layer->refreshPending = layerProto.refresh_pending(); |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 120 | layer->hwcFrame = generateRect(layerProto.hwc_frame()); |
| 121 | layer->hwcCrop = generateFloatRect(layerProto.hwc_crop()); |
| 122 | layer->hwcTransform = layerProto.hwc_transform(); |
rongliu | cfb187b | 2018-03-14 12:26:23 -0700 | [diff] [blame] | 123 | layer->windowType = layerProto.window_type(); |
| 124 | layer->appId = layerProto.app_id(); |
Yiwei Zhang | 60d1a19 | 2018-03-07 14:52:28 -0800 | [diff] [blame] | 125 | layer->hwcCompositionType = layerProto.hwc_composition_type(); |
| 126 | layer->isProtected = layerProto.is_protected(); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 127 | |
| 128 | return layer; |
| 129 | } |
| 130 | |
| 131 | LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) { |
| 132 | LayerProtoParser::Region region; |
| 133 | region.id = regionProto.id(); |
| 134 | for (int i = 0; i < regionProto.rect_size(); i++) { |
| 135 | const RectProto& rectProto = regionProto.rect(i); |
| 136 | region.rects.push_back(generateRect(rectProto)); |
| 137 | } |
| 138 | |
| 139 | return region; |
| 140 | } |
| 141 | |
| 142 | LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) { |
| 143 | LayerProtoParser::Rect rect; |
| 144 | rect.left = rectProto.left(); |
| 145 | rect.top = rectProto.top(); |
| 146 | rect.right = rectProto.right(); |
| 147 | rect.bottom = rectProto.bottom(); |
| 148 | |
| 149 | return rect; |
| 150 | } |
| 151 | |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 152 | LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) { |
| 153 | LayerProtoParser::FloatRect rect; |
| 154 | rect.left = rectProto.left(); |
| 155 | rect.top = rectProto.top(); |
| 156 | rect.right = rectProto.right(); |
| 157 | rect.bottom = rectProto.bottom(); |
| 158 | |
| 159 | return rect; |
| 160 | } |
| 161 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 162 | LayerProtoParser::Transform LayerProtoParser::generateTransform( |
| 163 | const TransformProto& transformProto) { |
| 164 | LayerProtoParser::Transform transform; |
| 165 | transform.dsdx = transformProto.dsdx(); |
| 166 | transform.dtdx = transformProto.dtdx(); |
| 167 | transform.dsdy = transformProto.dsdy(); |
| 168 | transform.dtdy = transformProto.dtdy(); |
| 169 | |
| 170 | return transform; |
| 171 | } |
| 172 | |
| 173 | LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer( |
| 174 | const ActiveBufferProto& activeBufferProto) { |
| 175 | LayerProtoParser::ActiveBuffer activeBuffer; |
| 176 | activeBuffer.width = activeBufferProto.width(); |
| 177 | activeBuffer.height = activeBufferProto.height(); |
| 178 | activeBuffer.stride = activeBufferProto.stride(); |
| 179 | activeBuffer.format = activeBufferProto.format(); |
| 180 | |
| 181 | return activeBuffer; |
| 182 | } |
| 183 | |
| 184 | void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto, |
| 185 | std::unordered_map<int32_t, Layer*>& layerMap) { |
| 186 | auto currLayer = layerMap[layerProto.id()]; |
| 187 | |
| 188 | for (int i = 0; i < layerProto.children_size(); i++) { |
| 189 | if (layerMap.count(layerProto.children(i)) > 0) { |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 190 | // Only make unique_ptrs for children since they are guaranteed to be unique, only one |
| 191 | // parent per child. This ensures there will only be one unique_ptr made for each layer. |
| 192 | currLayer->children.push_back(std::unique_ptr<Layer>(layerMap[layerProto.children(i)])); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | for (int i = 0; i < layerProto.relatives_size(); i++) { |
| 197 | if (layerMap.count(layerProto.relatives(i)) > 0) { |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 198 | currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | if (layerProto.has_parent()) { |
| 203 | if (layerMap.count(layerProto.parent()) > 0) { |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 204 | currLayer->parent = layerMap[layerProto.parent()]; |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | if (layerProto.has_z_order_relative_of()) { |
| 209 | if (layerMap.count(layerProto.z_order_relative_of()) > 0) { |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 210 | currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()]; |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | std::string LayerProtoParser::layersToString( |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 216 | std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers) { |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 217 | std::string result; |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 218 | for (std::unique_ptr<LayerProtoParser::Layer>& layer : layers) { |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 219 | if (layer->zOrderRelativeOf != nullptr) { |
| 220 | continue; |
| 221 | } |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 222 | result.append(layerToString(layer.get()).c_str()); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return result; |
| 226 | } |
| 227 | |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 228 | std::string LayerProtoParser::layerToString(LayerProtoParser::Layer* layer) { |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 229 | std::string result; |
| 230 | |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 231 | std::vector<Layer*> traverse(layer->relatives); |
| 232 | for (std::unique_ptr<LayerProtoParser::Layer>& child : layer->children) { |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 233 | if (child->zOrderRelativeOf != nullptr) { |
| 234 | continue; |
| 235 | } |
| 236 | |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 237 | traverse.push_back(child.get()); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | std::sort(traverse.begin(), traverse.end(), sortLayers); |
| 241 | |
| 242 | size_t i = 0; |
| 243 | for (; i < traverse.size(); i++) { |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 244 | auto& relative = traverse[i]; |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 245 | if (relative->z >= 0) { |
| 246 | break; |
| 247 | } |
| 248 | result.append(layerToString(relative).c_str()); |
| 249 | } |
| 250 | result.append(layer->to_string().c_str()); |
| 251 | result.append("\n"); |
| 252 | for (; i < traverse.size(); i++) { |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 253 | auto& relative = traverse[i]; |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 254 | result.append(layerToString(relative).c_str()); |
| 255 | } |
| 256 | |
| 257 | return result; |
| 258 | } |
| 259 | |
chaviw | 5bf9d68 | 2017-10-25 16:31:08 -0700 | [diff] [blame] | 260 | std::string LayerProtoParser::ActiveBuffer::to_string() const { |
| 261 | return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride, |
| 262 | decodePixelFormat(format).c_str()); |
| 263 | } |
| 264 | |
| 265 | std::string LayerProtoParser::Transform::to_string() const { |
| 266 | return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx), |
| 267 | static_cast<double>(dtdx), static_cast<double>(dsdy), |
| 268 | static_cast<double>(dtdy)); |
| 269 | } |
| 270 | |
| 271 | std::string LayerProtoParser::Rect::to_string() const { |
| 272 | return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom); |
| 273 | } |
| 274 | |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 275 | std::string LayerProtoParser::FloatRect::to_string() const { |
| 276 | return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom); |
| 277 | } |
| 278 | |
chaviw | 5bf9d68 | 2017-10-25 16:31:08 -0700 | [diff] [blame] | 279 | std::string LayerProtoParser::Region::to_string(const char* what) const { |
| 280 | std::string result = |
| 281 | StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id), |
| 282 | static_cast<int>(rects.size())); |
| 283 | |
| 284 | for (auto& rect : rects) { |
| 285 | StringAppendF(&result, " %s\n", rect.to_string().c_str()); |
| 286 | } |
| 287 | |
| 288 | return result; |
| 289 | } |
| 290 | |
| 291 | std::string LayerProtoParser::Layer::to_string() const { |
| 292 | std::string result; |
| 293 | StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str()); |
| 294 | result.append(transparentRegion.to_string("TransparentRegion").c_str()); |
| 295 | result.append(visibleRegion.to_string("VisibleRegion").c_str()); |
| 296 | result.append(damageRegion.to_string("SurfaceDamageRegion").c_str()); |
| 297 | |
| 298 | StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack, |
| 299 | z, static_cast<double>(position.x), static_cast<double>(position.y), size.x, |
| 300 | size.y); |
| 301 | |
| 302 | StringAppendF(&result, "crop=%s, finalCrop=%s, ", crop.to_string().c_str(), |
| 303 | finalCrop.to_string().c_str()); |
| 304 | StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate); |
| 305 | StringAppendF(&result, "dataspace=%s, ", dataspace.c_str()); |
Chia-I Wu | 1e04361 | 2018-03-01 09:45:09 -0800 | [diff] [blame] | 306 | StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str()); |
chaviw | 5bf9d68 | 2017-10-25 16:31:08 -0700 | [diff] [blame] | 307 | StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ", |
| 308 | static_cast<double>(color.r), static_cast<double>(color.g), |
| 309 | static_cast<double>(color.b), static_cast<double>(color.a), flags); |
| 310 | StringAppendF(&result, "tr=%s", transform.to_string().c_str()); |
| 311 | result.append("\n"); |
| 312 | StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str()); |
| 313 | StringAppendF(&result, " zOrderRelativeOf=%s\n", |
| 314 | zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str()); |
| 315 | StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str()); |
Yichi Chen | 6ca3519 | 2018-05-29 12:20:43 +0800 | [diff] [blame^] | 316 | StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str()); |
rongliu | cfb187b | 2018-03-14 12:26:23 -0700 | [diff] [blame] | 317 | StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending); |
| 318 | StringAppendF(&result, " windowType=%d, appId=%d", windowType, appId); |
chaviw | 5bf9d68 | 2017-10-25 16:31:08 -0700 | [diff] [blame] | 319 | |
| 320 | return result; |
| 321 | } |
| 322 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 323 | } // namespace surfaceflinger |
| 324 | } // namespace android |