blob: 7ab57f9a2cce9bbfc58bbf384a90171fe73e3bbf [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;
135 region.id = regionProto.id();
136 for (int i = 0; i < regionProto.rect_size(); i++) {
137 const RectProto& rectProto = regionProto.rect(i);
138 region.rects.push_back(generateRect(rectProto));
139 }
140
141 return region;
142}
143
144LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
145 LayerProtoParser::Rect rect;
146 rect.left = rectProto.left();
147 rect.top = rectProto.top();
148 rect.right = rectProto.right();
149 rect.bottom = rectProto.bottom();
150
151 return rect;
152}
153
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800154LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
155 LayerProtoParser::FloatRect rect;
156 rect.left = rectProto.left();
157 rect.top = rectProto.top();
158 rect.right = rectProto.right();
159 rect.bottom = rectProto.bottom();
160
161 return rect;
162}
163
chaviw1d044282017-09-27 12:19:28 -0700164LayerProtoParser::Transform LayerProtoParser::generateTransform(
165 const TransformProto& transformProto) {
166 LayerProtoParser::Transform transform;
167 transform.dsdx = transformProto.dsdx();
168 transform.dtdx = transformProto.dtdx();
169 transform.dsdy = transformProto.dsdy();
170 transform.dtdy = transformProto.dtdy();
171
172 return transform;
173}
174
175LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
176 const ActiveBufferProto& activeBufferProto) {
177 LayerProtoParser::ActiveBuffer activeBuffer;
178 activeBuffer.width = activeBufferProto.width();
179 activeBuffer.height = activeBufferProto.height();
180 activeBuffer.stride = activeBufferProto.stride();
181 activeBuffer.format = activeBufferProto.format();
182
183 return activeBuffer;
184}
185
186void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
187 std::unordered_map<int32_t, Layer*>& layerMap) {
188 auto currLayer = layerMap[layerProto.id()];
189
190 for (int i = 0; i < layerProto.children_size(); i++) {
191 if (layerMap.count(layerProto.children(i)) > 0) {
Chia-I Wu2f884132018-09-13 15:17:58 -0700192 currLayer->children.push_back(layerMap[layerProto.children(i)]);
chaviw1d044282017-09-27 12:19:28 -0700193 }
194 }
195
196 for (int i = 0; i < layerProto.relatives_size(); i++) {
197 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700198 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700199 }
200 }
201
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -0800202 if (layerProto.parent() != -1) {
chaviw1d044282017-09-27 12:19:28 -0700203 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700204 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700205 }
206 }
207
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -0800208 if (layerProto.z_order_relative_of() != -1) {
chaviw1d044282017-09-27 12:19:28 -0700209 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700210 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700211 }
212 }
213}
214
Chia-I Wu2f884132018-09-13 15:17:58 -0700215std::string LayerProtoParser::layerTreeToString(const LayerTree& layerTree) {
chaviw1d044282017-09-27 12:19:28 -0700216 std::string result;
Chia-I Wu2f884132018-09-13 15:17:58 -0700217 for (const LayerProtoParser::Layer* layer : layerTree.topLevelLayers) {
chaviw1d044282017-09-27 12:19:28 -0700218 if (layer->zOrderRelativeOf != nullptr) {
219 continue;
220 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700221 result.append(layerToString(layer));
chaviw1d044282017-09-27 12:19:28 -0700222 }
223
224 return result;
225}
226
Chia-I Wu2f884132018-09-13 15:17:58 -0700227std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700228 std::string result;
229
chaviw7ba019b2018-03-14 13:28:39 -0700230 std::vector<Layer*> traverse(layer->relatives);
Chia-I Wu2f884132018-09-13 15:17:58 -0700231 for (LayerProtoParser::Layer* child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700232 if (child->zOrderRelativeOf != nullptr) {
233 continue;
234 }
235
Chia-I Wu2f884132018-09-13 15:17:58 -0700236 traverse.push_back(child);
chaviw1d044282017-09-27 12:19:28 -0700237 }
238
239 std::sort(traverse.begin(), traverse.end(), sortLayers);
240
241 size_t i = 0;
242 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700243 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700244 if (relative->z >= 0) {
245 break;
246 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700247 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700248 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700249 result.append(layer->to_string());
chaviw1d044282017-09-27 12:19:28 -0700250 result.append("\n");
251 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700252 auto& relative = traverse[i];
Chia-I Wu2f884132018-09-13 15:17:58 -0700253 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700254 }
255
256 return result;
257}
258
chaviw5bf9d682017-10-25 16:31:08 -0700259std::string LayerProtoParser::ActiveBuffer::to_string() const {
260 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
261 decodePixelFormat(format).c_str());
262}
263
264std::string LayerProtoParser::Transform::to_string() const {
265 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
266 static_cast<double>(dtdx), static_cast<double>(dsdy),
267 static_cast<double>(dtdy));
268}
269
270std::string LayerProtoParser::Rect::to_string() const {
271 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
272}
273
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800274std::string LayerProtoParser::FloatRect::to_string() const {
275 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
276}
277
chaviw5bf9d682017-10-25 16:31:08 -0700278std::string LayerProtoParser::Region::to_string(const char* what) const {
279 std::string result =
280 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
281 static_cast<int>(rects.size()));
282
283 for (auto& rect : rects) {
284 StringAppendF(&result, " %s\n", rect.to_string().c_str());
285 }
286
287 return result;
288}
289
290std::string LayerProtoParser::Layer::to_string() const {
291 std::string result;
292 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
293 result.append(transparentRegion.to_string("TransparentRegion").c_str());
294 result.append(visibleRegion.to_string("VisibleRegion").c_str());
295 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
296
297 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
298 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
299 size.y);
300
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700301 StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700302 StringAppendF(&result, "cornerRadius=%f, ", cornerRadius);
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