blob: ef27847e55c40cecc6f281a4a6c8608d4c740470 [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
Chia-I Wu2f884132018-09-13 15:17:58 -070040LayerProtoParser::LayerTree LayerProtoParser::generateLayerTree(const LayersProto& layersProto) {
41 LayerTree layerTree;
42 layerTree.allLayers = generateLayerList(layersProto);
chaviw1d044282017-09-27 12:19:28 -070043
Chia-I Wu2f884132018-09-13 15:17:58 -070044 // find and sort the top-level layers
45 for (Layer& layer : layerTree.allLayers) {
46 if (layer.parent == nullptr) {
47 layerTree.topLevelLayers.push_back(&layer);
chaviw7ba019b2018-03-14 13:28:39 -070048 }
49 }
Chia-I Wu2f884132018-09-13 15:17:58 -070050 std::sort(layerTree.topLevelLayers.begin(), layerTree.topLevelLayers.end(), sortLayers);
chaviw1d044282017-09-27 12:19:28 -070051
Chia-I Wu2f884132018-09-13 15:17:58 -070052 return layerTree;
chaviw1d044282017-09-27 12:19:28 -070053}
54
Chia-I Wu2f884132018-09-13 15:17:58 -070055std::vector<LayerProtoParser::Layer> LayerProtoParser::generateLayerList(
chaviw1d044282017-09-27 12:19:28 -070056 const LayersProto& layersProto) {
Chia-I Wu2f884132018-09-13 15:17:58 -070057 std::vector<Layer> layerList;
chaviw1d044282017-09-27 12:19:28 -070058 std::unordered_map<int32_t, Layer*> layerMap;
59
Chia-I Wu2f884132018-09-13 15:17:58 -070060 // build the layer list and the layer map
61 layerList.reserve(layersProto.layers_size());
62 layerMap.reserve(layersProto.layers_size());
chaviw1d044282017-09-27 12:19:28 -070063 for (int i = 0; i < layersProto.layers_size(); i++) {
Chia-I Wu2f884132018-09-13 15:17:58 -070064 layerList.emplace_back(generateLayer(layersProto.layers(i)));
65 // this works because layerList never changes capacity
66 layerMap[layerList.back().id] = &layerList.back();
chaviw1d044282017-09-27 12:19:28 -070067 }
68
Chia-I Wu2f884132018-09-13 15:17:58 -070069 // fix up children and relatives
chaviw1d044282017-09-27 12:19:28 -070070 for (int i = 0; i < layersProto.layers_size(); i++) {
Chia-I Wu2f884132018-09-13 15:17:58 -070071 updateChildrenAndRelative(layersProto.layers(i), layerMap);
chaviw1d044282017-09-27 12:19:28 -070072 }
73
Chia-I Wu2f884132018-09-13 15:17:58 -070074 return layerList;
chaviw1d044282017-09-27 12:19:28 -070075}
76
Chia-I Wu2f884132018-09-13 15:17:58 -070077LayerProtoParser::Layer LayerProtoParser::generateLayer(const LayerProto& layerProto) {
78 Layer layer;
79 layer.id = layerProto.id();
80 layer.name = layerProto.name();
81 layer.type = layerProto.type();
82 layer.transparentRegion = generateRegion(layerProto.transparent_region());
83 layer.visibleRegion = generateRegion(layerProto.visible_region());
84 layer.damageRegion = generateRegion(layerProto.damage_region());
85 layer.layerStack = layerProto.layer_stack();
86 layer.z = layerProto.z();
87 layer.position = {layerProto.position().x(), layerProto.position().y()};
88 layer.requestedPosition = {layerProto.requested_position().x(),
chaviw1d044282017-09-27 12:19:28 -070089 layerProto.requested_position().y()};
Chia-I Wu2f884132018-09-13 15:17:58 -070090 layer.size = {layerProto.size().w(), layerProto.size().h()};
91 layer.crop = generateRect(layerProto.crop());
92 layer.isOpaque = layerProto.is_opaque();
93 layer.invalidate = layerProto.invalidate();
94 layer.dataspace = layerProto.dataspace();
95 layer.pixelFormat = layerProto.pixel_format();
96 layer.color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(),
chaviw1d044282017-09-27 12:19:28 -070097 layerProto.color().a()};
Chia-I Wu2f884132018-09-13 15:17:58 -070098 layer.requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
chaviw1d044282017-09-27 12:19:28 -070099 layerProto.requested_color().b(), layerProto.requested_color().a()};
Chia-I Wu2f884132018-09-13 15:17:58 -0700100 layer.flags = layerProto.flags();
101 layer.transform = generateTransform(layerProto.transform());
102 layer.requestedTransform = generateTransform(layerProto.requested_transform());
103 layer.activeBuffer = generateActiveBuffer(layerProto.active_buffer());
104 layer.bufferTransform = generateTransform(layerProto.buffer_transform());
105 layer.queuedFrames = layerProto.queued_frames();
106 layer.refreshPending = layerProto.refresh_pending();
Chia-I Wu2f884132018-09-13 15:17:58 -0700107 layer.isProtected = layerProto.is_protected();
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700108 layer.cornerRadius = layerProto.corner_radius();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800109 for (const auto& entry : layerProto.metadata()) {
110 const std::string& dataStr = entry.second;
111 std::vector<uint8_t>& outData = layer.metadata.mMap[entry.first];
112 outData.resize(dataStr.size());
113 memcpy(outData.data(), dataStr.data(), dataStr.size());
114 }
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800115 layer.cornerRadiusCrop = generateFloatRect(layerProto.corner_radius_crop());
116 layer.shadowRadius = layerProto.shadow_radius();
chaviw1d044282017-09-27 12:19:28 -0700117 return layer;
118}
119
120LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
121 LayerProtoParser::Region region;
chaviw1d044282017-09-27 12:19:28 -0700122 for (int i = 0; i < regionProto.rect_size(); i++) {
123 const RectProto& rectProto = regionProto.rect(i);
124 region.rects.push_back(generateRect(rectProto));
125 }
126
127 return region;
128}
129
130LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
131 LayerProtoParser::Rect rect;
132 rect.left = rectProto.left();
133 rect.top = rectProto.top();
134 rect.right = rectProto.right();
135 rect.bottom = rectProto.bottom();
136
137 return rect;
138}
139
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800140LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
141 LayerProtoParser::FloatRect rect;
142 rect.left = rectProto.left();
143 rect.top = rectProto.top();
144 rect.right = rectProto.right();
145 rect.bottom = rectProto.bottom();
146
147 return rect;
148}
149
chaviw1d044282017-09-27 12:19:28 -0700150LayerProtoParser::Transform LayerProtoParser::generateTransform(
151 const TransformProto& transformProto) {
152 LayerProtoParser::Transform transform;
153 transform.dsdx = transformProto.dsdx();
154 transform.dtdx = transformProto.dtdx();
155 transform.dsdy = transformProto.dsdy();
156 transform.dtdy = transformProto.dtdy();
157
158 return transform;
159}
160
161LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
162 const ActiveBufferProto& activeBufferProto) {
163 LayerProtoParser::ActiveBuffer activeBuffer;
164 activeBuffer.width = activeBufferProto.width();
165 activeBuffer.height = activeBufferProto.height();
166 activeBuffer.stride = activeBufferProto.stride();
167 activeBuffer.format = activeBufferProto.format();
168
169 return activeBuffer;
170}
171
172void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
173 std::unordered_map<int32_t, Layer*>& layerMap) {
174 auto currLayer = layerMap[layerProto.id()];
175
176 for (int i = 0; i < layerProto.children_size(); i++) {
177 if (layerMap.count(layerProto.children(i)) > 0) {
Chia-I Wu2f884132018-09-13 15:17:58 -0700178 currLayer->children.push_back(layerMap[layerProto.children(i)]);
chaviw1d044282017-09-27 12:19:28 -0700179 }
180 }
181
182 for (int i = 0; i < layerProto.relatives_size(); i++) {
183 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700184 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700185 }
186 }
187
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -0800188 if (layerProto.parent() != -1) {
chaviw1d044282017-09-27 12:19:28 -0700189 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700190 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700191 }
192 }
193
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -0800194 if (layerProto.z_order_relative_of() != -1) {
chaviw1d044282017-09-27 12:19:28 -0700195 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700196 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700197 }
198 }
199}
200
Chia-I Wu2f884132018-09-13 15:17:58 -0700201std::string LayerProtoParser::layerTreeToString(const LayerTree& layerTree) {
chaviw1d044282017-09-27 12:19:28 -0700202 std::string result;
Chia-I Wu2f884132018-09-13 15:17:58 -0700203 for (const LayerProtoParser::Layer* layer : layerTree.topLevelLayers) {
chaviw1d044282017-09-27 12:19:28 -0700204 if (layer->zOrderRelativeOf != nullptr) {
205 continue;
206 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700207 result.append(layerToString(layer));
chaviw1d044282017-09-27 12:19:28 -0700208 }
209
210 return result;
211}
212
Chia-I Wu2f884132018-09-13 15:17:58 -0700213std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700214 std::string result;
215
chaviw7ba019b2018-03-14 13:28:39 -0700216 std::vector<Layer*> traverse(layer->relatives);
Chia-I Wu2f884132018-09-13 15:17:58 -0700217 for (LayerProtoParser::Layer* child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700218 if (child->zOrderRelativeOf != nullptr) {
219 continue;
220 }
221
Chia-I Wu2f884132018-09-13 15:17:58 -0700222 traverse.push_back(child);
chaviw1d044282017-09-27 12:19:28 -0700223 }
224
225 std::sort(traverse.begin(), traverse.end(), sortLayers);
226
227 size_t i = 0;
228 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700229 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700230 if (relative->z >= 0) {
231 break;
232 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700233 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700234 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700235 result.append(layer->to_string());
chaviw1d044282017-09-27 12:19:28 -0700236 result.append("\n");
237 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700238 auto& relative = traverse[i];
Chia-I Wu2f884132018-09-13 15:17:58 -0700239 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700240 }
241
242 return result;
243}
244
chaviw5bf9d682017-10-25 16:31:08 -0700245std::string LayerProtoParser::ActiveBuffer::to_string() const {
246 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
247 decodePixelFormat(format).c_str());
248}
249
250std::string LayerProtoParser::Transform::to_string() const {
251 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
252 static_cast<double>(dtdx), static_cast<double>(dsdy),
253 static_cast<double>(dtdy));
254}
255
256std::string LayerProtoParser::Rect::to_string() const {
257 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
258}
259
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800260std::string LayerProtoParser::FloatRect::to_string() const {
261 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
262}
263
chaviw5bf9d682017-10-25 16:31:08 -0700264std::string LayerProtoParser::Region::to_string(const char* what) const {
265 std::string result =
266 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
267 static_cast<int>(rects.size()));
268
269 for (auto& rect : rects) {
270 StringAppendF(&result, " %s\n", rect.to_string().c_str());
271 }
272
273 return result;
274}
275
276std::string LayerProtoParser::Layer::to_string() const {
277 std::string result;
278 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
279 result.append(transparentRegion.to_string("TransparentRegion").c_str());
280 result.append(visibleRegion.to_string("VisibleRegion").c_str());
281 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
282
283 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
284 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
285 size.y);
286
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700287 StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700288 StringAppendF(&result, "cornerRadius=%f, ", cornerRadius);
Peiyong Lin51598552019-04-17 14:09:22 -0700289 StringAppendF(&result, "isProtected=%1d, ", isProtected);
chaviw5bf9d682017-10-25 16:31:08 -0700290 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
291 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
Chia-I Wu1e043612018-03-01 09:45:09 -0800292 StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
chaviw5bf9d682017-10-25 16:31:08 -0700293 StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
294 static_cast<double>(color.r), static_cast<double>(color.g),
295 static_cast<double>(color.b), static_cast<double>(color.a), flags);
296 StringAppendF(&result, "tr=%s", transform.to_string().c_str());
297 result.append("\n");
298 StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
299 StringAppendF(&result, " zOrderRelativeOf=%s\n",
300 zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
301 StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
Yichi Chen6ca35192018-05-29 12:20:43 +0800302 StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str());
rongliucfb187b2018-03-14 12:26:23 -0700303 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800304 StringAppendF(&result, " metadata={");
305 bool first = true;
306 for (const auto& entry : metadata.mMap) {
307 if (!first) result.append(", ");
308 first = false;
309 result.append(metadata.itemToString(entry.first, ":"));
310 }
Vishnu Nair95a1ed42019-12-06 12:25:11 -0800311 result.append("},");
312 StringAppendF(&result, " cornerRadiusCrop=%s, ", cornerRadiusCrop.to_string().c_str());
313 StringAppendF(&result, " shadowRadius=%.3f, ", shadowRadius);
chaviw5bf9d682017-10-25 16:31:08 -0700314 return result;
315}
316
chaviw1d044282017-09-27 12:19:28 -0700317} // namespace surfaceflinger
318} // namespace android