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