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 #include <android-base/stringprintf.h>
17 #include <layerproto/LayerProtoParser.h>
18 #include <ui/DebugUtils.h>
19
20 using android::base::StringAppendF;
21 using android::base::StringPrintf;
22
23 namespace android {
24 namespace surfaceflinger {
25
sortLayers(LayerProtoParser::Layer * lhs,const LayerProtoParser::Layer * rhs)26 bool sortLayers(LayerProtoParser::Layer* lhs, const LayerProtoParser::Layer* rhs) {
27 uint32_t ls = lhs->layerStack;
28 uint32_t rs = rhs->layerStack;
29 if (ls != rs) return ls < rs;
30
31 int32_t lz = lhs->z;
32 int32_t rz = rhs->z;
33 if (lz != rz) {
34 return lz < rz;
35 }
36
37 return lhs->id < rhs->id;
38 }
39
generateLayerGlobalInfo(const LayersProto & layersProto)40 const LayerProtoParser::LayerGlobal LayerProtoParser::generateLayerGlobalInfo(
41 const LayersProto& layersProto) {
42 LayerGlobal layerGlobal;
43 layerGlobal.resolution = {layersProto.resolution().w(), layersProto.resolution().h()};
44 layerGlobal.colorMode = layersProto.color_mode();
45 layerGlobal.colorTransform = layersProto.color_transform();
46 layerGlobal.globalTransform = layersProto.global_transform();
47 return layerGlobal;
48 }
49
generateLayerTree(const LayersProto & layersProto)50 LayerProtoParser::LayerTree LayerProtoParser::generateLayerTree(const LayersProto& layersProto) {
51 LayerTree layerTree;
52 layerTree.allLayers = generateLayerList(layersProto);
53
54 // find and sort the top-level layers
55 for (Layer& layer : layerTree.allLayers) {
56 if (layer.parent == nullptr) {
57 layerTree.topLevelLayers.push_back(&layer);
58 }
59 }
60 std::sort(layerTree.topLevelLayers.begin(), layerTree.topLevelLayers.end(), sortLayers);
61
62 return layerTree;
63 }
64
generateLayerList(const LayersProto & layersProto)65 std::vector<LayerProtoParser::Layer> LayerProtoParser::generateLayerList(
66 const LayersProto& layersProto) {
67 std::vector<Layer> layerList;
68 std::unordered_map<int32_t, Layer*> layerMap;
69
70 // build the layer list and the layer map
71 layerList.reserve(layersProto.layers_size());
72 layerMap.reserve(layersProto.layers_size());
73 for (int i = 0; i < layersProto.layers_size(); i++) {
74 layerList.emplace_back(generateLayer(layersProto.layers(i)));
75 // this works because layerList never changes capacity
76 layerMap[layerList.back().id] = &layerList.back();
77 }
78
79 // fix up children and relatives
80 for (int i = 0; i < layersProto.layers_size(); i++) {
81 updateChildrenAndRelative(layersProto.layers(i), layerMap);
82 }
83
84 return layerList;
85 }
86
generateLayer(const LayerProto & layerProto)87 LayerProtoParser::Layer LayerProtoParser::generateLayer(const LayerProto& layerProto) {
88 Layer layer;
89 layer.id = layerProto.id();
90 layer.name = layerProto.name();
91 layer.type = layerProto.type();
92 layer.transparentRegion = generateRegion(layerProto.transparent_region());
93 layer.visibleRegion = generateRegion(layerProto.visible_region());
94 layer.damageRegion = generateRegion(layerProto.damage_region());
95 layer.layerStack = layerProto.layer_stack();
96 layer.z = layerProto.z();
97 layer.position = {layerProto.position().x(), layerProto.position().y()};
98 layer.requestedPosition = {layerProto.requested_position().x(),
99 layerProto.requested_position().y()};
100 layer.size = {layerProto.size().w(), layerProto.size().h()};
101 layer.crop = generateRect(layerProto.crop());
102 layer.isOpaque = layerProto.is_opaque();
103 layer.invalidate = layerProto.invalidate();
104 layer.dataspace = layerProto.dataspace();
105 layer.pixelFormat = layerProto.pixel_format();
106 layer.color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(),
107 layerProto.color().a()};
108 layer.requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
109 layerProto.requested_color().b(), layerProto.requested_color().a()};
110 layer.flags = layerProto.flags();
111 layer.transform = generateTransform(layerProto.transform());
112 layer.requestedTransform = generateTransform(layerProto.requested_transform());
113 layer.activeBuffer = generateActiveBuffer(layerProto.active_buffer());
114 layer.bufferTransform = generateTransform(layerProto.buffer_transform());
115 layer.queuedFrames = layerProto.queued_frames();
116 layer.refreshPending = layerProto.refresh_pending();
117 layer.hwcFrame = generateRect(layerProto.hwc_frame());
118 layer.hwcCrop = generateFloatRect(layerProto.hwc_crop());
119 layer.hwcTransform = layerProto.hwc_transform();
120 layer.hwcCompositionType = layerProto.hwc_composition_type();
121 layer.isProtected = layerProto.is_protected();
122 layer.cornerRadius = layerProto.corner_radius();
123 for (const auto& entry : layerProto.metadata()) {
124 const std::string& dataStr = entry.second;
125 std::vector<uint8_t>& outData = layer.metadata.mMap[entry.first];
126 outData.resize(dataStr.size());
127 memcpy(outData.data(), dataStr.data(), dataStr.size());
128 }
129
130 return layer;
131 }
132
generateRegion(const RegionProto & regionProto)133 LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
134 LayerProtoParser::Region region;
135 for (int i = 0; i < regionProto.rect_size(); i++) {
136 const RectProto& rectProto = regionProto.rect(i);
137 region.rects.push_back(generateRect(rectProto));
138 }
139
140 return region;
141 }
142
generateRect(const RectProto & rectProto)143 LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
144 LayerProtoParser::Rect rect;
145 rect.left = rectProto.left();
146 rect.top = rectProto.top();
147 rect.right = rectProto.right();
148 rect.bottom = rectProto.bottom();
149
150 return rect;
151 }
152
generateFloatRect(const FloatRectProto & rectProto)153 LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
154 LayerProtoParser::FloatRect rect;
155 rect.left = rectProto.left();
156 rect.top = rectProto.top();
157 rect.right = rectProto.right();
158 rect.bottom = rectProto.bottom();
159
160 return rect;
161 }
162
generateTransform(const TransformProto & transformProto)163 LayerProtoParser::Transform LayerProtoParser::generateTransform(
164 const TransformProto& transformProto) {
165 LayerProtoParser::Transform transform;
166 transform.dsdx = transformProto.dsdx();
167 transform.dtdx = transformProto.dtdx();
168 transform.dsdy = transformProto.dsdy();
169 transform.dtdy = transformProto.dtdy();
170
171 return transform;
172 }
173
generateActiveBuffer(const ActiveBufferProto & activeBufferProto)174 LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
175 const ActiveBufferProto& activeBufferProto) {
176 LayerProtoParser::ActiveBuffer activeBuffer;
177 activeBuffer.width = activeBufferProto.width();
178 activeBuffer.height = activeBufferProto.height();
179 activeBuffer.stride = activeBufferProto.stride();
180 activeBuffer.format = activeBufferProto.format();
181
182 return activeBuffer;
183 }
184
updateChildrenAndRelative(const LayerProto & layerProto,std::unordered_map<int32_t,Layer * > & layerMap)185 void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
186 std::unordered_map<int32_t, Layer*>& layerMap) {
187 auto currLayer = layerMap[layerProto.id()];
188
189 for (int i = 0; i < layerProto.children_size(); i++) {
190 if (layerMap.count(layerProto.children(i)) > 0) {
191 currLayer->children.push_back(layerMap[layerProto.children(i)]);
192 }
193 }
194
195 for (int i = 0; i < layerProto.relatives_size(); i++) {
196 if (layerMap.count(layerProto.relatives(i)) > 0) {
197 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
198 }
199 }
200
201 if (layerProto.parent() != -1) {
202 if (layerMap.count(layerProto.parent()) > 0) {
203 currLayer->parent = layerMap[layerProto.parent()];
204 }
205 }
206
207 if (layerProto.z_order_relative_of() != -1) {
208 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
209 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
210 }
211 }
212 }
213
layerTreeToString(const LayerTree & layerTree)214 std::string LayerProtoParser::layerTreeToString(const LayerTree& layerTree) {
215 std::string result;
216 for (const LayerProtoParser::Layer* layer : layerTree.topLevelLayers) {
217 if (layer->zOrderRelativeOf != nullptr) {
218 continue;
219 }
220 result.append(layerToString(layer));
221 }
222
223 return result;
224 }
225
layerToString(const LayerProtoParser::Layer * layer)226 std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
227 std::string result;
228
229 std::vector<Layer*> traverse(layer->relatives);
230 for (LayerProtoParser::Layer* child : layer->children) {
231 if (child->zOrderRelativeOf != nullptr) {
232 continue;
233 }
234
235 traverse.push_back(child);
236 }
237
238 std::sort(traverse.begin(), traverse.end(), sortLayers);
239
240 size_t i = 0;
241 for (; i < traverse.size(); i++) {
242 auto& relative = traverse[i];
243 if (relative->z >= 0) {
244 break;
245 }
246 result.append(layerToString(relative));
247 }
248 result.append(layer->to_string());
249 result.append("\n");
250 for (; i < traverse.size(); i++) {
251 auto& relative = traverse[i];
252 result.append(layerToString(relative));
253 }
254
255 return result;
256 }
257
to_string() const258 std::string LayerProtoParser::ActiveBuffer::to_string() const {
259 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
260 decodePixelFormat(format).c_str());
261 }
262
to_string() const263 std::string LayerProtoParser::Transform::to_string() const {
264 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
265 static_cast<double>(dtdx), static_cast<double>(dsdy),
266 static_cast<double>(dtdy));
267 }
268
to_string() const269 std::string LayerProtoParser::Rect::to_string() const {
270 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
271 }
272
to_string() const273 std::string LayerProtoParser::FloatRect::to_string() const {
274 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
275 }
276
to_string(const char * what) const277 std::string LayerProtoParser::Region::to_string(const char* what) const {
278 std::string result =
279 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
280 static_cast<int>(rects.size()));
281
282 for (auto& rect : rects) {
283 StringAppendF(&result, " %s\n", rect.to_string().c_str());
284 }
285
286 return result;
287 }
288
to_string() const289 std::string LayerProtoParser::Layer::to_string() const {
290 std::string result;
291 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
292 result.append(transparentRegion.to_string("TransparentRegion").c_str());
293 result.append(visibleRegion.to_string("VisibleRegion").c_str());
294 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
295
296 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
297 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
298 size.y);
299
300 StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
301 StringAppendF(&result, "cornerRadius=%f, ", cornerRadius);
302 StringAppendF(&result, "isProtected=%1d, ", isProtected);
303 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
304 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
305 StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
306 StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
307 static_cast<double>(color.r), static_cast<double>(color.g),
308 static_cast<double>(color.b), static_cast<double>(color.a), flags);
309 StringAppendF(&result, "tr=%s", transform.to_string().c_str());
310 result.append("\n");
311 StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
312 StringAppendF(&result, " zOrderRelativeOf=%s\n",
313 zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
314 StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
315 StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str());
316 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
317 StringAppendF(&result, " metadata={");
318 bool first = true;
319 for (const auto& entry : metadata.mMap) {
320 if (!first) result.append(", ");
321 first = false;
322 result.append(metadata.itemToString(entry.first, ":"));
323 }
324 result.append("}");
325
326 return result;
327 }
328
329 } // namespace surfaceflinger
330 } // namespace android
331