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