| 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(); | 
|  | 109 |  | 
|  | 110 | return layer; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) { | 
|  | 114 | LayerProtoParser::Region region; | 
|  | 115 | region.id = regionProto.id(); | 
|  | 116 | for (int i = 0; i < regionProto.rect_size(); i++) { | 
|  | 117 | const RectProto& rectProto = regionProto.rect(i); | 
|  | 118 | region.rects.push_back(generateRect(rectProto)); | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | return region; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) { | 
|  | 125 | LayerProtoParser::Rect rect; | 
|  | 126 | rect.left = rectProto.left(); | 
|  | 127 | rect.top = rectProto.top(); | 
|  | 128 | rect.right = rectProto.right(); | 
|  | 129 | rect.bottom = rectProto.bottom(); | 
|  | 130 |  | 
|  | 131 | return rect; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | LayerProtoParser::Transform LayerProtoParser::generateTransform( | 
|  | 135 | const TransformProto& transformProto) { | 
|  | 136 | LayerProtoParser::Transform transform; | 
|  | 137 | transform.dsdx = transformProto.dsdx(); | 
|  | 138 | transform.dtdx = transformProto.dtdx(); | 
|  | 139 | transform.dsdy = transformProto.dsdy(); | 
|  | 140 | transform.dtdy = transformProto.dtdy(); | 
|  | 141 |  | 
|  | 142 | return transform; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer( | 
|  | 146 | const ActiveBufferProto& activeBufferProto) { | 
|  | 147 | LayerProtoParser::ActiveBuffer activeBuffer; | 
|  | 148 | activeBuffer.width = activeBufferProto.width(); | 
|  | 149 | activeBuffer.height = activeBufferProto.height(); | 
|  | 150 | activeBuffer.stride = activeBufferProto.stride(); | 
|  | 151 | activeBuffer.format = activeBufferProto.format(); | 
|  | 152 |  | 
|  | 153 | return activeBuffer; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto, | 
|  | 157 | std::unordered_map<int32_t, Layer*>& layerMap) { | 
|  | 158 | auto currLayer = layerMap[layerProto.id()]; | 
|  | 159 |  | 
|  | 160 | for (int i = 0; i < layerProto.children_size(); i++) { | 
|  | 161 | if (layerMap.count(layerProto.children(i)) > 0) { | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 162 | // Only make unique_ptrs for children since they are guaranteed to be unique, only one | 
|  | 163 | // parent per child. This ensures there will only be one unique_ptr made for each layer. | 
|  | 164 | currLayer->children.push_back(std::unique_ptr<Layer>(layerMap[layerProto.children(i)])); | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 165 | } | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | for (int i = 0; i < layerProto.relatives_size(); i++) { | 
|  | 169 | if (layerMap.count(layerProto.relatives(i)) > 0) { | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 170 | currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]); | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 171 | } | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | if (layerProto.has_parent()) { | 
|  | 175 | if (layerMap.count(layerProto.parent()) > 0) { | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 176 | currLayer->parent = layerMap[layerProto.parent()]; | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 177 | } | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | if (layerProto.has_z_order_relative_of()) { | 
|  | 181 | if (layerMap.count(layerProto.z_order_relative_of()) > 0) { | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 182 | currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()]; | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 183 | } | 
|  | 184 | } | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | std::string LayerProtoParser::layersToString( | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 188 | std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers) { | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 189 | std::string result; | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 190 | for (std::unique_ptr<LayerProtoParser::Layer>& layer : layers) { | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 191 | if (layer->zOrderRelativeOf != nullptr) { | 
|  | 192 | continue; | 
|  | 193 | } | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 194 | result.append(layerToString(layer.get()).c_str()); | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
|  | 197 | return result; | 
|  | 198 | } | 
|  | 199 |  | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 200 | std::string LayerProtoParser::layerToString(LayerProtoParser::Layer* layer) { | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 201 | std::string result; | 
|  | 202 |  | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 203 | std::vector<Layer*> traverse(layer->relatives); | 
|  | 204 | for (std::unique_ptr<LayerProtoParser::Layer>& child : layer->children) { | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 205 | if (child->zOrderRelativeOf != nullptr) { | 
|  | 206 | continue; | 
|  | 207 | } | 
|  | 208 |  | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 209 | traverse.push_back(child.get()); | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 210 | } | 
|  | 211 |  | 
|  | 212 | std::sort(traverse.begin(), traverse.end(), sortLayers); | 
|  | 213 |  | 
|  | 214 | size_t i = 0; | 
|  | 215 | for (; i < traverse.size(); i++) { | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 216 | auto& relative = traverse[i]; | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 217 | if (relative->z >= 0) { | 
|  | 218 | break; | 
|  | 219 | } | 
|  | 220 | result.append(layerToString(relative).c_str()); | 
|  | 221 | } | 
|  | 222 | result.append(layer->to_string().c_str()); | 
|  | 223 | result.append("\n"); | 
|  | 224 | for (; i < traverse.size(); i++) { | 
| chaviw | 7794ec1 | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 225 | auto& relative = traverse[i]; | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 226 | result.append(layerToString(relative).c_str()); | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | return result; | 
|  | 230 | } | 
|  | 231 |  | 
| chaviw | 5bf9d68 | 2017-10-25 16:31:08 -0700 | [diff] [blame] | 232 | std::string LayerProtoParser::ActiveBuffer::to_string() const { | 
|  | 233 | return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride, | 
|  | 234 | decodePixelFormat(format).c_str()); | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | std::string LayerProtoParser::Transform::to_string() const { | 
|  | 238 | return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx), | 
|  | 239 | static_cast<double>(dtdx), static_cast<double>(dsdy), | 
|  | 240 | static_cast<double>(dtdy)); | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | std::string LayerProtoParser::Rect::to_string() const { | 
|  | 244 | return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom); | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | std::string LayerProtoParser::Region::to_string(const char* what) const { | 
|  | 248 | std::string result = | 
|  | 249 | StringPrintf("  Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id), | 
|  | 250 | static_cast<int>(rects.size())); | 
|  | 251 |  | 
|  | 252 | for (auto& rect : rects) { | 
|  | 253 | StringAppendF(&result, "    %s\n", rect.to_string().c_str()); | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | return result; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | std::string LayerProtoParser::Layer::to_string() const { | 
|  | 260 | std::string result; | 
|  | 261 | StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str()); | 
|  | 262 | result.append(transparentRegion.to_string("TransparentRegion").c_str()); | 
|  | 263 | result.append(visibleRegion.to_string("VisibleRegion").c_str()); | 
|  | 264 | result.append(damageRegion.to_string("SurfaceDamageRegion").c_str()); | 
|  | 265 |  | 
|  | 266 | StringAppendF(&result, "      layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack, | 
|  | 267 | z, static_cast<double>(position.x), static_cast<double>(position.y), size.x, | 
|  | 268 | size.y); | 
|  | 269 |  | 
|  | 270 | StringAppendF(&result, "crop=%s, finalCrop=%s, ", crop.to_string().c_str(), | 
|  | 271 | finalCrop.to_string().c_str()); | 
|  | 272 | StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate); | 
|  | 273 | StringAppendF(&result, "dataspace=%s, ", dataspace.c_str()); | 
|  | 274 | StringAppendF(&result, "pixelformat=%s, ", pixelFormat.c_str()); | 
|  | 275 | StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ", | 
|  | 276 | static_cast<double>(color.r), static_cast<double>(color.g), | 
|  | 277 | static_cast<double>(color.b), static_cast<double>(color.a), flags); | 
|  | 278 | StringAppendF(&result, "tr=%s", transform.to_string().c_str()); | 
|  | 279 | result.append("\n"); | 
|  | 280 | StringAppendF(&result, "      parent=%s\n", parent == nullptr ? "none" : parent->name.c_str()); | 
|  | 281 | StringAppendF(&result, "      zOrderRelativeOf=%s\n", | 
|  | 282 | zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str()); | 
|  | 283 | StringAppendF(&result, "      activeBuffer=%s,", activeBuffer.to_string().c_str()); | 
|  | 284 | StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d", queuedFrames, refreshPending); | 
|  | 285 |  | 
|  | 286 | return result; | 
|  | 287 | } | 
|  | 288 |  | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 289 | } // namespace surfaceflinger | 
|  | 290 | } // namespace android |