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 | |
| 17 | #include <layerproto/LayerProtoParser.h> |
| 18 | |
| 19 | namespace android { |
| 20 | namespace surfaceflinger { |
| 21 | bool sortLayers(const LayerProtoParser::Layer* lhs, const LayerProtoParser::Layer* rhs) { |
| 22 | uint32_t ls = lhs->layerStack; |
| 23 | uint32_t rs = rhs->layerStack; |
| 24 | if (ls != rs) return ls < rs; |
| 25 | |
| 26 | uint32_t lz = lhs->z; |
| 27 | uint32_t rz = rhs->z; |
| 28 | if (lz != rz) return lz < rz; |
| 29 | |
| 30 | return lhs->id < rhs->id; |
| 31 | } |
| 32 | |
| 33 | std::vector<const LayerProtoParser::Layer*> LayerProtoParser::generateLayerTree( |
| 34 | const LayersProto& layersProto) { |
| 35 | auto layerMap = generateMap(layersProto); |
| 36 | |
| 37 | std::vector<const Layer*> layers; |
| 38 | std::for_each(layerMap.begin(), layerMap.end(), |
| 39 | [&](const std::pair<const int32_t, Layer*>& ref) { |
| 40 | if (ref.second->parent == nullptr) { |
| 41 | // only save top level layers |
| 42 | layers.push_back(ref.second); |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | std::sort(layers.begin(), layers.end(), sortLayers); |
| 47 | return layers; |
| 48 | } |
| 49 | |
| 50 | std::unordered_map<int32_t, LayerProtoParser::Layer*> LayerProtoParser::generateMap( |
| 51 | const LayersProto& layersProto) { |
| 52 | std::unordered_map<int32_t, Layer*> layerMap; |
| 53 | |
| 54 | for (int i = 0; i < layersProto.layers_size(); i++) { |
| 55 | const LayerProto& layerProto = layersProto.layers(i); |
| 56 | layerMap[layerProto.id()] = generateLayer(layerProto); |
| 57 | } |
| 58 | |
| 59 | for (int i = 0; i < layersProto.layers_size(); i++) { |
| 60 | const LayerProto& layerProto = layersProto.layers(i); |
| 61 | updateChildrenAndRelative(layerProto, layerMap); |
| 62 | } |
| 63 | |
| 64 | return layerMap; |
| 65 | } |
| 66 | |
| 67 | LayerProtoParser::Layer* LayerProtoParser::generateLayer(const LayerProto& layerProto) { |
| 68 | Layer* layer = new Layer(); |
| 69 | layer->id = layerProto.id(); |
| 70 | layer->name = layerProto.name(); |
| 71 | layer->type = layerProto.type(); |
| 72 | layer->transparentRegion = generateRegion(layerProto.transparent_region()); |
| 73 | layer->visibleRegion = generateRegion(layerProto.visible_region()); |
| 74 | layer->damageRegion = generateRegion(layerProto.damage_region()); |
| 75 | layer->layerStack = layerProto.layer_stack(); |
| 76 | layer->z = layerProto.z(); |
| 77 | layer->position = {layerProto.position().x(), layerProto.position().y()}; |
| 78 | layer->requestedPosition = {layerProto.requested_position().x(), |
| 79 | layerProto.requested_position().y()}; |
| 80 | layer->size = {layerProto.size().w(), layerProto.size().h()}; |
| 81 | layer->crop = generateRect(layerProto.crop()); |
| 82 | layer->finalCrop = generateRect(layerProto.final_crop()); |
| 83 | layer->isOpaque = layerProto.is_opaque(); |
| 84 | layer->invalidate = layerProto.invalidate(); |
| 85 | layer->dataspace = layerProto.dataspace(); |
| 86 | layer->pixelFormat = layerProto.pixel_format(); |
| 87 | layer->color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(), |
| 88 | layerProto.color().a()}; |
| 89 | layer->requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(), |
| 90 | layerProto.requested_color().b(), layerProto.requested_color().a()}; |
| 91 | layer->flags = layerProto.flags(); |
| 92 | layer->transform = generateTransform(layerProto.transform()); |
| 93 | layer->requestedTransform = generateTransform(layerProto.requested_transform()); |
| 94 | layer->activeBuffer = generateActiveBuffer(layerProto.active_buffer()); |
| 95 | layer->queuedFrames = layerProto.queued_frames(); |
| 96 | layer->refreshPending = layerProto.refresh_pending(); |
| 97 | |
| 98 | return layer; |
| 99 | } |
| 100 | |
| 101 | LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) { |
| 102 | LayerProtoParser::Region region; |
| 103 | region.id = regionProto.id(); |
| 104 | for (int i = 0; i < regionProto.rect_size(); i++) { |
| 105 | const RectProto& rectProto = regionProto.rect(i); |
| 106 | region.rects.push_back(generateRect(rectProto)); |
| 107 | } |
| 108 | |
| 109 | return region; |
| 110 | } |
| 111 | |
| 112 | LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) { |
| 113 | LayerProtoParser::Rect rect; |
| 114 | rect.left = rectProto.left(); |
| 115 | rect.top = rectProto.top(); |
| 116 | rect.right = rectProto.right(); |
| 117 | rect.bottom = rectProto.bottom(); |
| 118 | |
| 119 | return rect; |
| 120 | } |
| 121 | |
| 122 | LayerProtoParser::Transform LayerProtoParser::generateTransform( |
| 123 | const TransformProto& transformProto) { |
| 124 | LayerProtoParser::Transform transform; |
| 125 | transform.dsdx = transformProto.dsdx(); |
| 126 | transform.dtdx = transformProto.dtdx(); |
| 127 | transform.dsdy = transformProto.dsdy(); |
| 128 | transform.dtdy = transformProto.dtdy(); |
| 129 | |
| 130 | return transform; |
| 131 | } |
| 132 | |
| 133 | LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer( |
| 134 | const ActiveBufferProto& activeBufferProto) { |
| 135 | LayerProtoParser::ActiveBuffer activeBuffer; |
| 136 | activeBuffer.width = activeBufferProto.width(); |
| 137 | activeBuffer.height = activeBufferProto.height(); |
| 138 | activeBuffer.stride = activeBufferProto.stride(); |
| 139 | activeBuffer.format = activeBufferProto.format(); |
| 140 | |
| 141 | return activeBuffer; |
| 142 | } |
| 143 | |
| 144 | void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto, |
| 145 | std::unordered_map<int32_t, Layer*>& layerMap) { |
| 146 | auto currLayer = layerMap[layerProto.id()]; |
| 147 | |
| 148 | for (int i = 0; i < layerProto.children_size(); i++) { |
| 149 | if (layerMap.count(layerProto.children(i)) > 0) { |
| 150 | auto childLayer = layerMap[layerProto.children(i)]; |
| 151 | currLayer->children.push_back(childLayer); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | for (int i = 0; i < layerProto.relatives_size(); i++) { |
| 156 | if (layerMap.count(layerProto.relatives(i)) > 0) { |
| 157 | auto relativeLayer = layerMap[layerProto.relatives(i)]; |
| 158 | currLayer->relatives.push_back(relativeLayer); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (layerProto.has_parent()) { |
| 163 | if (layerMap.count(layerProto.parent()) > 0) { |
| 164 | auto parentLayer = layerMap[layerProto.parent()]; |
| 165 | currLayer->parent = parentLayer; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (layerProto.has_z_order_relative_of()) { |
| 170 | if (layerMap.count(layerProto.z_order_relative_of()) > 0) { |
| 171 | auto relativeLayer = layerMap[layerProto.z_order_relative_of()]; |
| 172 | currLayer->zOrderRelativeOf = relativeLayer; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | std::string LayerProtoParser::layersToString( |
| 178 | const std::vector<const LayerProtoParser::Layer*> layers) { |
| 179 | std::string result; |
| 180 | for (const LayerProtoParser::Layer* layer : layers) { |
| 181 | if (layer->zOrderRelativeOf != nullptr) { |
| 182 | continue; |
| 183 | } |
| 184 | result.append(layerToString(layer).c_str()); |
| 185 | } |
| 186 | |
| 187 | return result; |
| 188 | } |
| 189 | |
| 190 | std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) { |
| 191 | std::string result; |
| 192 | |
| 193 | std::vector<const Layer*> traverse(layer->relatives); |
| 194 | for (const LayerProtoParser::Layer* child : layer->children) { |
| 195 | if (child->zOrderRelativeOf != nullptr) { |
| 196 | continue; |
| 197 | } |
| 198 | |
| 199 | traverse.push_back(child); |
| 200 | } |
| 201 | |
| 202 | std::sort(traverse.begin(), traverse.end(), sortLayers); |
| 203 | |
| 204 | size_t i = 0; |
| 205 | for (; i < traverse.size(); i++) { |
| 206 | const auto& relative = traverse[i]; |
| 207 | if (relative->z >= 0) { |
| 208 | break; |
| 209 | } |
| 210 | result.append(layerToString(relative).c_str()); |
| 211 | } |
| 212 | result.append(layer->to_string().c_str()); |
| 213 | result.append("\n"); |
| 214 | for (; i < traverse.size(); i++) { |
| 215 | const auto& relative = traverse[i]; |
| 216 | result.append(layerToString(relative).c_str()); |
| 217 | } |
| 218 | |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | } // namespace surfaceflinger |
| 223 | } // namespace android |