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