blob: d3381e5757c10dd5a09048bd5595ad05ca15317f [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
Yiwei Zhang7124ad32018-02-21 13:02:45 -080040const LayerProtoParser::LayerGlobal LayerProtoParser::generateLayerGlobalInfo(
41 const LayersProto& layersProto) {
Yiwei Zhang60d1a192018-03-07 14:52:28 -080042 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;
Yiwei Zhang7124ad32018-02-21 13:02:45 -080048}
49
Chia-I Wu2f884132018-09-13 15:17:58 -070050LayerProtoParser::LayerTree LayerProtoParser::generateLayerTree(const LayersProto& layersProto) {
51 LayerTree layerTree;
52 layerTree.allLayers = generateLayerList(layersProto);
chaviw1d044282017-09-27 12:19:28 -070053
Chia-I Wu2f884132018-09-13 15:17:58 -070054 // find and sort the top-level layers
55 for (Layer& layer : layerTree.allLayers) {
56 if (layer.parent == nullptr) {
57 layerTree.topLevelLayers.push_back(&layer);
chaviw7ba019b2018-03-14 13:28:39 -070058 }
59 }
Chia-I Wu2f884132018-09-13 15:17:58 -070060 std::sort(layerTree.topLevelLayers.begin(), layerTree.topLevelLayers.end(), sortLayers);
chaviw1d044282017-09-27 12:19:28 -070061
Chia-I Wu2f884132018-09-13 15:17:58 -070062 return layerTree;
chaviw1d044282017-09-27 12:19:28 -070063}
64
Chia-I Wu2f884132018-09-13 15:17:58 -070065std::vector<LayerProtoParser::Layer> LayerProtoParser::generateLayerList(
chaviw1d044282017-09-27 12:19:28 -070066 const LayersProto& layersProto) {
Chia-I Wu2f884132018-09-13 15:17:58 -070067 std::vector<Layer> layerList;
chaviw1d044282017-09-27 12:19:28 -070068 std::unordered_map<int32_t, Layer*> layerMap;
69
Chia-I Wu2f884132018-09-13 15:17:58 -070070 // build the layer list and the layer map
71 layerList.reserve(layersProto.layers_size());
72 layerMap.reserve(layersProto.layers_size());
chaviw1d044282017-09-27 12:19:28 -070073 for (int i = 0; i < layersProto.layers_size(); i++) {
Chia-I Wu2f884132018-09-13 15:17:58 -070074 layerList.emplace_back(generateLayer(layersProto.layers(i)));
75 // this works because layerList never changes capacity
76 layerMap[layerList.back().id] = &layerList.back();
chaviw1d044282017-09-27 12:19:28 -070077 }
78
Chia-I Wu2f884132018-09-13 15:17:58 -070079 // fix up children and relatives
chaviw1d044282017-09-27 12:19:28 -070080 for (int i = 0; i < layersProto.layers_size(); i++) {
Chia-I Wu2f884132018-09-13 15:17:58 -070081 updateChildrenAndRelative(layersProto.layers(i), layerMap);
chaviw1d044282017-09-27 12:19:28 -070082 }
83
Chia-I Wu2f884132018-09-13 15:17:58 -070084 return layerList;
chaviw1d044282017-09-27 12:19:28 -070085}
86
Chia-I Wu2f884132018-09-13 15:17:58 -070087LayerProtoParser::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(),
chaviw1d044282017-09-27 12:19:28 -070099 layerProto.requested_position().y()};
Chia-I Wu2f884132018-09-13 15:17:58 -0700100 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(),
chaviw1d044282017-09-27 12:19:28 -0700107 layerProto.color().a()};
Chia-I Wu2f884132018-09-13 15:17:58 -0700108 layer.requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
chaviw1d044282017-09-27 12:19:28 -0700109 layerProto.requested_color().b(), layerProto.requested_color().a()};
Chia-I Wu2f884132018-09-13 15:17:58 -0700110 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();
Chia-I Wu2f884132018-09-13 15:17:58 -0700120 layer.hwcCompositionType = layerProto.hwc_composition_type();
121 layer.isProtected = layerProto.is_protected();
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700122 layer.cornerRadius = layerProto.corner_radius();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800123 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 }
chaviw1d044282017-09-27 12:19:28 -0700129
130 return layer;
131}
132
133LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
134 LayerProtoParser::Region region;
chaviw1d044282017-09-27 12:19:28 -0700135 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
143LayerProtoParser::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
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800153LayerProtoParser::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
chaviw1d044282017-09-27 12:19:28 -0700163LayerProtoParser::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
174LayerProtoParser::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
185void 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) {
Chia-I Wu2f884132018-09-13 15:17:58 -0700191 currLayer->children.push_back(layerMap[layerProto.children(i)]);
chaviw1d044282017-09-27 12:19:28 -0700192 }
193 }
194
195 for (int i = 0; i < layerProto.relatives_size(); i++) {
196 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700197 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700198 }
199 }
200
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -0800201 if (layerProto.parent() != -1) {
chaviw1d044282017-09-27 12:19:28 -0700202 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700203 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700204 }
205 }
206
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -0800207 if (layerProto.z_order_relative_of() != -1) {
chaviw1d044282017-09-27 12:19:28 -0700208 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700209 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700210 }
211 }
212}
213
Chia-I Wu2f884132018-09-13 15:17:58 -0700214std::string LayerProtoParser::layerTreeToString(const LayerTree& layerTree) {
chaviw1d044282017-09-27 12:19:28 -0700215 std::string result;
Chia-I Wu2f884132018-09-13 15:17:58 -0700216 for (const LayerProtoParser::Layer* layer : layerTree.topLevelLayers) {
chaviw1d044282017-09-27 12:19:28 -0700217 if (layer->zOrderRelativeOf != nullptr) {
218 continue;
219 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700220 result.append(layerToString(layer));
chaviw1d044282017-09-27 12:19:28 -0700221 }
222
223 return result;
224}
225
Chia-I Wu2f884132018-09-13 15:17:58 -0700226std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700227 std::string result;
228
chaviw7ba019b2018-03-14 13:28:39 -0700229 std::vector<Layer*> traverse(layer->relatives);
Chia-I Wu2f884132018-09-13 15:17:58 -0700230 for (LayerProtoParser::Layer* child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700231 if (child->zOrderRelativeOf != nullptr) {
232 continue;
233 }
234
Chia-I Wu2f884132018-09-13 15:17:58 -0700235 traverse.push_back(child);
chaviw1d044282017-09-27 12:19:28 -0700236 }
237
238 std::sort(traverse.begin(), traverse.end(), sortLayers);
239
240 size_t i = 0;
241 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700242 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700243 if (relative->z >= 0) {
244 break;
245 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700246 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700247 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700248 result.append(layer->to_string());
chaviw1d044282017-09-27 12:19:28 -0700249 result.append("\n");
250 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700251 auto& relative = traverse[i];
Chia-I Wu2f884132018-09-13 15:17:58 -0700252 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700253 }
254
255 return result;
256}
257
chaviw5bf9d682017-10-25 16:31:08 -0700258std::string LayerProtoParser::ActiveBuffer::to_string() const {
259 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
260 decodePixelFormat(format).c_str());
261}
262
263std::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
269std::string LayerProtoParser::Rect::to_string() const {
270 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
271}
272
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800273std::string LayerProtoParser::FloatRect::to_string() const {
274 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
275}
276
chaviw5bf9d682017-10-25 16:31:08 -0700277std::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
289std::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
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700300 StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700301 StringAppendF(&result, "cornerRadius=%f, ", cornerRadius);
Peiyong Lin51598552019-04-17 14:09:22 -0700302 StringAppendF(&result, "isProtected=%1d, ", isProtected);
chaviw5bf9d682017-10-25 16:31:08 -0700303 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
304 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
Chia-I Wu1e043612018-03-01 09:45:09 -0800305 StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
chaviw5bf9d682017-10-25 16:31:08 -0700306 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());
Yichi Chen6ca35192018-05-29 12:20:43 +0800315 StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str());
rongliucfb187b2018-03-14 12:26:23 -0700316 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800317 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("}");
chaviw5bf9d682017-10-25 16:31:08 -0700325
326 return result;
327}
328
chaviw1d044282017-09-27 12:19:28 -0700329} // namespace surfaceflinger
330} // namespace android