blob: c3d0a40261037a2117d0c2bfb83bd10e38a47f74 [file] [log] [blame]
chaviw1d044282017-09-27 12:19:28 -07001/*
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 */
chaviw5bf9d682017-10-25 16:31:08 -070016#include <android-base/stringprintf.h>
chaviw1d044282017-09-27 12:19:28 -070017#include <layerproto/LayerProtoParser.h>
chaviw5bf9d682017-10-25 16:31:08 -070018#include <ui/DebugUtils.h>
19
20using android::base::StringAppendF;
21using android::base::StringPrintf;
chaviw1d044282017-09-27 12:19:28 -070022
23namespace android {
24namespace surfaceflinger {
chaviw5bf9d682017-10-25 16:31:08 -070025
chaviw7ba019b2018-03-14 13:28:39 -070026bool sortLayers(LayerProtoParser::Layer* lhs, const LayerProtoParser::Layer* rhs) {
chaviw1d044282017-09-27 12:19:28 -070027 uint32_t ls = lhs->layerStack;
28 uint32_t rs = rhs->layerStack;
29 if (ls != rs) return ls < rs;
30
Robert Carr83f8e4d2017-11-15 14:37:37 -080031 int32_t lz = lhs->z;
32 int32_t rz = rhs->z;
33 if (lz != rz) {
Chia-I Wub4e0a832018-09-21 12:18:40 -070034 return lz < rz;
Robert Carr83f8e4d2017-11-15 14:37:37 -080035 }
chaviw1d044282017-09-27 12:19:28 -070036
37 return lhs->id < rhs->id;
38}
39
Kean Mariotti4ba343c2023-04-19 13:31:02 +000040LayerProtoParser::LayerTree LayerProtoParser::generateLayerTree(
41 const perfetto::protos::LayersProto& layersProto) {
Chia-I Wu2f884132018-09-13 15:17:58 -070042 LayerTree layerTree;
43 layerTree.allLayers = generateLayerList(layersProto);
chaviw1d044282017-09-27 12:19:28 -070044
Chia-I Wu2f884132018-09-13 15:17:58 -070045 // find and sort the top-level layers
46 for (Layer& layer : layerTree.allLayers) {
47 if (layer.parent == nullptr) {
48 layerTree.topLevelLayers.push_back(&layer);
chaviw7ba019b2018-03-14 13:28:39 -070049 }
50 }
Chia-I Wu2f884132018-09-13 15:17:58 -070051 std::sort(layerTree.topLevelLayers.begin(), layerTree.topLevelLayers.end(), sortLayers);
chaviw1d044282017-09-27 12:19:28 -070052
Chia-I Wu2f884132018-09-13 15:17:58 -070053 return layerTree;
chaviw1d044282017-09-27 12:19:28 -070054}
55
Chia-I Wu2f884132018-09-13 15:17:58 -070056std::vector<LayerProtoParser::Layer> LayerProtoParser::generateLayerList(
Kean Mariotti4ba343c2023-04-19 13:31:02 +000057 const perfetto::protos::LayersProto& layersProto) {
Chia-I Wu2f884132018-09-13 15:17:58 -070058 std::vector<Layer> layerList;
chaviw1d044282017-09-27 12:19:28 -070059 std::unordered_map<int32_t, Layer*> layerMap;
60
Chia-I Wu2f884132018-09-13 15:17:58 -070061 // build the layer list and the layer map
62 layerList.reserve(layersProto.layers_size());
63 layerMap.reserve(layersProto.layers_size());
chaviw1d044282017-09-27 12:19:28 -070064 for (int i = 0; i < layersProto.layers_size(); i++) {
Chia-I Wu2f884132018-09-13 15:17:58 -070065 layerList.emplace_back(generateLayer(layersProto.layers(i)));
66 // this works because layerList never changes capacity
67 layerMap[layerList.back().id] = &layerList.back();
chaviw1d044282017-09-27 12:19:28 -070068 }
69
Chia-I Wu2f884132018-09-13 15:17:58 -070070 // fix up children and relatives
chaviw1d044282017-09-27 12:19:28 -070071 for (int i = 0; i < layersProto.layers_size(); i++) {
Chia-I Wu2f884132018-09-13 15:17:58 -070072 updateChildrenAndRelative(layersProto.layers(i), layerMap);
chaviw1d044282017-09-27 12:19:28 -070073 }
74
Chia-I Wu2f884132018-09-13 15:17:58 -070075 return layerList;
chaviw1d044282017-09-27 12:19:28 -070076}
77
Kean Mariotti4ba343c2023-04-19 13:31:02 +000078LayerProtoParser::Layer LayerProtoParser::generateLayer(
79 const perfetto::protos::LayerProto& layerProto) {
Chia-I Wu2f884132018-09-13 15:17:58 -070080 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(),
chaviw1d044282017-09-27 12:19:28 -070091 layerProto.requested_position().y()};
Chia-I Wu2f884132018-09-13 15:17:58 -070092 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(),
chaviw1d044282017-09-27 12:19:28 -070099 layerProto.color().a()};
Chia-I Wu2f884132018-09-13 15:17:58 -0700100 layer.requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
chaviw1d044282017-09-27 12:19:28 -0700101 layerProto.requested_color().b(), layerProto.requested_color().a()};
Chia-I Wu2f884132018-09-13 15:17:58 -0700102 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();
Chia-I Wu2f884132018-09-13 15:17:58 -0700109 layer.isProtected = layerProto.is_protected();
Winson Chunga30f7c92021-06-29 15:42:56 -0700110 layer.isTrustedOverlay = layerProto.is_trusted_overlay();
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700111 layer.cornerRadius = layerProto.corner_radius();
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800112 layer.backgroundBlurRadius = layerProto.background_blur_radius();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800113 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 }
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800119 layer.cornerRadiusCrop = generateFloatRect(layerProto.corner_radius_crop());
120 layer.shadowRadius = layerProto.shadow_radius();
chaviw250bcbb2020-08-05 11:17:54 -0700121 layer.ownerUid = layerProto.owner_uid();
chaviw1d044282017-09-27 12:19:28 -0700122 return layer;
123}
124
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000125LayerProtoParser::Region LayerProtoParser::generateRegion(
126 const perfetto::protos::RegionProto& regionProto) {
chaviw1d044282017-09-27 12:19:28 -0700127 LayerProtoParser::Region region;
chaviw1d044282017-09-27 12:19:28 -0700128 for (int i = 0; i < regionProto.rect_size(); i++) {
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000129 const perfetto::protos::RectProto& rectProto = regionProto.rect(i);
chaviw1d044282017-09-27 12:19:28 -0700130 region.rects.push_back(generateRect(rectProto));
131 }
132
133 return region;
134}
135
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000136LayerProtoParser::Rect LayerProtoParser::generateRect(
137 const perfetto::protos::RectProto& rectProto) {
chaviw1d044282017-09-27 12:19:28 -0700138 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
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000147LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(
148 const perfetto::protos::FloatRectProto& rectProto) {
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800149 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
chaviw1d044282017-09-27 12:19:28 -0700158LayerProtoParser::Transform LayerProtoParser::generateTransform(
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000159 const perfetto::protos::TransformProto& transformProto) {
chaviw1d044282017-09-27 12:19:28 -0700160 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
169LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000170 const perfetto::protos::ActiveBufferProto& activeBufferProto) {
chaviw1d044282017-09-27 12:19:28 -0700171 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
Kean Mariotti4ba343c2023-04-19 13:31:02 +0000180void LayerProtoParser::updateChildrenAndRelative(const perfetto::protos::LayerProto& layerProto,
chaviw1d044282017-09-27 12:19:28 -0700181 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) {
Chia-I Wu2f884132018-09-13 15:17:58 -0700186 currLayer->children.push_back(layerMap[layerProto.children(i)]);
chaviw1d044282017-09-27 12:19:28 -0700187 }
188 }
189
190 for (int i = 0; i < layerProto.relatives_size(); i++) {
191 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700192 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700193 }
194 }
195
Kean Mariotti7941ecf2023-04-27 16:42:13 +0000196 if (layerProto.has_parent()) {
chaviw1d044282017-09-27 12:19:28 -0700197 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700198 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700199 }
200 }
201
Kean Mariotti7941ecf2023-04-27 16:42:13 +0000202 if (layerProto.has_z_order_relative_of()) {
chaviw1d044282017-09-27 12:19:28 -0700203 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700204 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700205 }
206 }
207}
208
Chia-I Wu2f884132018-09-13 15:17:58 -0700209std::string LayerProtoParser::layerTreeToString(const LayerTree& layerTree) {
chaviw1d044282017-09-27 12:19:28 -0700210 std::string result;
Chia-I Wu2f884132018-09-13 15:17:58 -0700211 for (const LayerProtoParser::Layer* layer : layerTree.topLevelLayers) {
chaviw1d044282017-09-27 12:19:28 -0700212 if (layer->zOrderRelativeOf != nullptr) {
213 continue;
214 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700215 result.append(layerToString(layer));
chaviw1d044282017-09-27 12:19:28 -0700216 }
217
218 return result;
219}
220
Chia-I Wu2f884132018-09-13 15:17:58 -0700221std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700222 std::string result;
223
chaviw7ba019b2018-03-14 13:28:39 -0700224 std::vector<Layer*> traverse(layer->relatives);
Chia-I Wu2f884132018-09-13 15:17:58 -0700225 for (LayerProtoParser::Layer* child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700226 if (child->zOrderRelativeOf != nullptr) {
227 continue;
228 }
229
Chia-I Wu2f884132018-09-13 15:17:58 -0700230 traverse.push_back(child);
chaviw1d044282017-09-27 12:19:28 -0700231 }
232
233 std::sort(traverse.begin(), traverse.end(), sortLayers);
234
235 size_t i = 0;
236 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700237 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700238 if (relative->z >= 0) {
239 break;
240 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700241 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700242 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700243 result.append(layer->to_string());
chaviw1d044282017-09-27 12:19:28 -0700244 result.append("\n");
245 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700246 auto& relative = traverse[i];
Chia-I Wu2f884132018-09-13 15:17:58 -0700247 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700248 }
249
250 return result;
251}
252
chaviw5bf9d682017-10-25 16:31:08 -0700253std::string LayerProtoParser::ActiveBuffer::to_string() const {
254 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
255 decodePixelFormat(format).c_str());
256}
257
258std::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
264std::string LayerProtoParser::Rect::to_string() const {
265 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
266}
267
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800268std::string LayerProtoParser::FloatRect::to_string() const {
269 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
270}
271
chaviw5bf9d682017-10-25 16:31:08 -0700272std::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
284std::string LayerProtoParser::Layer::to_string() const {
285 std::string result;
chaviw250bcbb2020-08-05 11:17:54 -0700286 StringAppendF(&result, "+ %s (%s) uid=%d\n", type.c_str(), name.c_str(), ownerUid);
chaviw5bf9d682017-10-25 16:31:08 -0700287 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
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700295 StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700296 StringAppendF(&result, "cornerRadius=%f, ", cornerRadius);
Peiyong Lin51598552019-04-17 14:09:22 -0700297 StringAppendF(&result, "isProtected=%1d, ", isProtected);
Winson Chunga30f7c92021-06-29 15:42:56 -0700298 StringAppendF(&result, "isTrustedOverlay=%1d, ", isTrustedOverlay);
chaviw5bf9d682017-10-25 16:31:08 -0700299 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
300 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
Chia-I Wu1e043612018-03-01 09:45:09 -0800301 StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800302 StringAppendF(&result, "backgroundBlurRadius=%1d, ", backgroundBlurRadius);
chaviw5bf9d682017-10-25 16:31:08 -0700303 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());
Yichi Chen6ca35192018-05-29 12:20:43 +0800312 StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str());
Vishnu Nair1b700192022-02-04 10:09:47 -0800313 StringAppendF(&result, " queued-frames=%d", queuedFrames);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800314 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 }
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800321 result.append("},");
322 StringAppendF(&result, " cornerRadiusCrop=%s, ", cornerRadiusCrop.to_string().c_str());
323 StringAppendF(&result, " shadowRadius=%.3f, ", shadowRadius);
chaviw5bf9d682017-10-25 16:31:08 -0700324 return result;
325}
326
chaviw1d044282017-09-27 12:19:28 -0700327} // namespace surfaceflinger
328} // namespace android