blob: d020a394cedf1ed61d41f17d1cee19b6bbdba495 [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();
120 layer.windowType = layerProto.window_type();
121 layer.appId = layerProto.app_id();
122 layer.hwcCompositionType = layerProto.hwc_composition_type();
123 layer.isProtected = layerProto.is_protected();
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700124 layer.cornerRadius = layerProto.corner_radius();
chaviw1d044282017-09-27 12:19:28 -0700125
126 return layer;
127}
128
129LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
130 LayerProtoParser::Region region;
131 region.id = regionProto.id();
132 for (int i = 0; i < regionProto.rect_size(); i++) {
133 const RectProto& rectProto = regionProto.rect(i);
134 region.rects.push_back(generateRect(rectProto));
135 }
136
137 return region;
138}
139
140LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
141 LayerProtoParser::Rect 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
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800150LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
151 LayerProtoParser::FloatRect rect;
152 rect.left = rectProto.left();
153 rect.top = rectProto.top();
154 rect.right = rectProto.right();
155 rect.bottom = rectProto.bottom();
156
157 return rect;
158}
159
chaviw1d044282017-09-27 12:19:28 -0700160LayerProtoParser::Transform LayerProtoParser::generateTransform(
161 const TransformProto& transformProto) {
162 LayerProtoParser::Transform transform;
163 transform.dsdx = transformProto.dsdx();
164 transform.dtdx = transformProto.dtdx();
165 transform.dsdy = transformProto.dsdy();
166 transform.dtdy = transformProto.dtdy();
167
168 return transform;
169}
170
171LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
172 const ActiveBufferProto& activeBufferProto) {
173 LayerProtoParser::ActiveBuffer activeBuffer;
174 activeBuffer.width = activeBufferProto.width();
175 activeBuffer.height = activeBufferProto.height();
176 activeBuffer.stride = activeBufferProto.stride();
177 activeBuffer.format = activeBufferProto.format();
178
179 return activeBuffer;
180}
181
182void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
183 std::unordered_map<int32_t, Layer*>& layerMap) {
184 auto currLayer = layerMap[layerProto.id()];
185
186 for (int i = 0; i < layerProto.children_size(); i++) {
187 if (layerMap.count(layerProto.children(i)) > 0) {
Chia-I Wu2f884132018-09-13 15:17:58 -0700188 currLayer->children.push_back(layerMap[layerProto.children(i)]);
chaviw1d044282017-09-27 12:19:28 -0700189 }
190 }
191
192 for (int i = 0; i < layerProto.relatives_size(); i++) {
193 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700194 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700195 }
196 }
197
198 if (layerProto.has_parent()) {
199 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700200 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700201 }
202 }
203
204 if (layerProto.has_z_order_relative_of()) {
205 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700206 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700207 }
208 }
209}
210
Chia-I Wu2f884132018-09-13 15:17:58 -0700211std::string LayerProtoParser::layerTreeToString(const LayerTree& layerTree) {
chaviw1d044282017-09-27 12:19:28 -0700212 std::string result;
Chia-I Wu2f884132018-09-13 15:17:58 -0700213 for (const LayerProtoParser::Layer* layer : layerTree.topLevelLayers) {
chaviw1d044282017-09-27 12:19:28 -0700214 if (layer->zOrderRelativeOf != nullptr) {
215 continue;
216 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700217 result.append(layerToString(layer));
chaviw1d044282017-09-27 12:19:28 -0700218 }
219
220 return result;
221}
222
Chia-I Wu2f884132018-09-13 15:17:58 -0700223std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700224 std::string result;
225
chaviw7ba019b2018-03-14 13:28:39 -0700226 std::vector<Layer*> traverse(layer->relatives);
Chia-I Wu2f884132018-09-13 15:17:58 -0700227 for (LayerProtoParser::Layer* child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700228 if (child->zOrderRelativeOf != nullptr) {
229 continue;
230 }
231
Chia-I Wu2f884132018-09-13 15:17:58 -0700232 traverse.push_back(child);
chaviw1d044282017-09-27 12:19:28 -0700233 }
234
235 std::sort(traverse.begin(), traverse.end(), sortLayers);
236
237 size_t i = 0;
238 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700239 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700240 if (relative->z >= 0) {
241 break;
242 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700243 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700244 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700245 result.append(layer->to_string());
chaviw1d044282017-09-27 12:19:28 -0700246 result.append("\n");
247 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700248 auto& relative = traverse[i];
Chia-I Wu2f884132018-09-13 15:17:58 -0700249 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700250 }
251
252 return result;
253}
254
chaviw5bf9d682017-10-25 16:31:08 -0700255std::string LayerProtoParser::ActiveBuffer::to_string() const {
256 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
257 decodePixelFormat(format).c_str());
258}
259
260std::string LayerProtoParser::Transform::to_string() const {
261 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
262 static_cast<double>(dtdx), static_cast<double>(dsdy),
263 static_cast<double>(dtdy));
264}
265
266std::string LayerProtoParser::Rect::to_string() const {
267 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
268}
269
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800270std::string LayerProtoParser::FloatRect::to_string() const {
271 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
272}
273
chaviw5bf9d682017-10-25 16:31:08 -0700274std::string LayerProtoParser::Region::to_string(const char* what) const {
275 std::string result =
276 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
277 static_cast<int>(rects.size()));
278
279 for (auto& rect : rects) {
280 StringAppendF(&result, " %s\n", rect.to_string().c_str());
281 }
282
283 return result;
284}
285
286std::string LayerProtoParser::Layer::to_string() const {
287 std::string result;
288 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
289 result.append(transparentRegion.to_string("TransparentRegion").c_str());
290 result.append(visibleRegion.to_string("VisibleRegion").c_str());
291 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
292
293 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
294 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
295 size.y);
296
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700297 StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700298 StringAppendF(&result, "cornerRadius=%f, ", cornerRadius);
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());
chaviw5bf9d682017-10-25 16:31:08 -0700302 StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
303 static_cast<double>(color.r), static_cast<double>(color.g),
304 static_cast<double>(color.b), static_cast<double>(color.a), flags);
305 StringAppendF(&result, "tr=%s", transform.to_string().c_str());
306 result.append("\n");
307 StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
308 StringAppendF(&result, " zOrderRelativeOf=%s\n",
309 zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
310 StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
Yichi Chen6ca35192018-05-29 12:20:43 +0800311 StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str());
rongliucfb187b2018-03-14 12:26:23 -0700312 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
313 StringAppendF(&result, " windowType=%d, appId=%d", windowType, appId);
chaviw5bf9d682017-10-25 16:31:08 -0700314
315 return result;
316}
317
chaviw1d044282017-09-27 12:19:28 -0700318} // namespace surfaceflinger
319} // namespace android