blob: 1d7fb6791f6cc9406542578333b13e3fc609582f [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();
chaviw1d044282017-09-27 12:19:28 -0700124
125 return layer;
126}
127
128LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
129 LayerProtoParser::Region region;
130 region.id = regionProto.id();
131 for (int i = 0; i < regionProto.rect_size(); i++) {
132 const RectProto& rectProto = regionProto.rect(i);
133 region.rects.push_back(generateRect(rectProto));
134 }
135
136 return region;
137}
138
139LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
140 LayerProtoParser::Rect rect;
141 rect.left = rectProto.left();
142 rect.top = rectProto.top();
143 rect.right = rectProto.right();
144 rect.bottom = rectProto.bottom();
145
146 return rect;
147}
148
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800149LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
150 LayerProtoParser::FloatRect rect;
151 rect.left = rectProto.left();
152 rect.top = rectProto.top();
153 rect.right = rectProto.right();
154 rect.bottom = rectProto.bottom();
155
156 return rect;
157}
158
chaviw1d044282017-09-27 12:19:28 -0700159LayerProtoParser::Transform LayerProtoParser::generateTransform(
160 const TransformProto& transformProto) {
161 LayerProtoParser::Transform transform;
162 transform.dsdx = transformProto.dsdx();
163 transform.dtdx = transformProto.dtdx();
164 transform.dsdy = transformProto.dsdy();
165 transform.dtdy = transformProto.dtdy();
166
167 return transform;
168}
169
170LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
171 const ActiveBufferProto& activeBufferProto) {
172 LayerProtoParser::ActiveBuffer activeBuffer;
173 activeBuffer.width = activeBufferProto.width();
174 activeBuffer.height = activeBufferProto.height();
175 activeBuffer.stride = activeBufferProto.stride();
176 activeBuffer.format = activeBufferProto.format();
177
178 return activeBuffer;
179}
180
181void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
182 std::unordered_map<int32_t, Layer*>& layerMap) {
183 auto currLayer = layerMap[layerProto.id()];
184
185 for (int i = 0; i < layerProto.children_size(); i++) {
186 if (layerMap.count(layerProto.children(i)) > 0) {
Chia-I Wu2f884132018-09-13 15:17:58 -0700187 currLayer->children.push_back(layerMap[layerProto.children(i)]);
chaviw1d044282017-09-27 12:19:28 -0700188 }
189 }
190
191 for (int i = 0; i < layerProto.relatives_size(); i++) {
192 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700193 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700194 }
195 }
196
197 if (layerProto.has_parent()) {
198 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700199 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700200 }
201 }
202
203 if (layerProto.has_z_order_relative_of()) {
204 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700205 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700206 }
207 }
208}
209
Chia-I Wu2f884132018-09-13 15:17:58 -0700210std::string LayerProtoParser::layerTreeToString(const LayerTree& layerTree) {
chaviw1d044282017-09-27 12:19:28 -0700211 std::string result;
Chia-I Wu2f884132018-09-13 15:17:58 -0700212 for (const LayerProtoParser::Layer* layer : layerTree.topLevelLayers) {
chaviw1d044282017-09-27 12:19:28 -0700213 if (layer->zOrderRelativeOf != nullptr) {
214 continue;
215 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700216 result.append(layerToString(layer));
chaviw1d044282017-09-27 12:19:28 -0700217 }
218
219 return result;
220}
221
Chia-I Wu2f884132018-09-13 15:17:58 -0700222std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700223 std::string result;
224
chaviw7ba019b2018-03-14 13:28:39 -0700225 std::vector<Layer*> traverse(layer->relatives);
Chia-I Wu2f884132018-09-13 15:17:58 -0700226 for (LayerProtoParser::Layer* child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700227 if (child->zOrderRelativeOf != nullptr) {
228 continue;
229 }
230
Chia-I Wu2f884132018-09-13 15:17:58 -0700231 traverse.push_back(child);
chaviw1d044282017-09-27 12:19:28 -0700232 }
233
234 std::sort(traverse.begin(), traverse.end(), sortLayers);
235
236 size_t i = 0;
237 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700238 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700239 if (relative->z >= 0) {
240 break;
241 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700242 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700243 }
Chia-I Wu2f884132018-09-13 15:17:58 -0700244 result.append(layer->to_string());
chaviw1d044282017-09-27 12:19:28 -0700245 result.append("\n");
246 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700247 auto& relative = traverse[i];
Chia-I Wu2f884132018-09-13 15:17:58 -0700248 result.append(layerToString(relative));
chaviw1d044282017-09-27 12:19:28 -0700249 }
250
251 return result;
252}
253
chaviw5bf9d682017-10-25 16:31:08 -0700254std::string LayerProtoParser::ActiveBuffer::to_string() const {
255 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
256 decodePixelFormat(format).c_str());
257}
258
259std::string LayerProtoParser::Transform::to_string() const {
260 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
261 static_cast<double>(dtdx), static_cast<double>(dsdy),
262 static_cast<double>(dtdy));
263}
264
265std::string LayerProtoParser::Rect::to_string() const {
266 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
267}
268
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800269std::string LayerProtoParser::FloatRect::to_string() const {
270 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
271}
272
chaviw5bf9d682017-10-25 16:31:08 -0700273std::string LayerProtoParser::Region::to_string(const char* what) const {
274 std::string result =
275 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
276 static_cast<int>(rects.size()));
277
278 for (auto& rect : rects) {
279 StringAppendF(&result, " %s\n", rect.to_string().c_str());
280 }
281
282 return result;
283}
284
285std::string LayerProtoParser::Layer::to_string() const {
286 std::string result;
287 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
288 result.append(transparentRegion.to_string("TransparentRegion").c_str());
289 result.append(visibleRegion.to_string("VisibleRegion").c_str());
290 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
291
292 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
293 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
294 size.y);
295
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700296 StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
chaviw5bf9d682017-10-25 16:31:08 -0700297 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
298 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
Chia-I Wu1e043612018-03-01 09:45:09 -0800299 StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
chaviw5bf9d682017-10-25 16:31:08 -0700300 StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
301 static_cast<double>(color.r), static_cast<double>(color.g),
302 static_cast<double>(color.b), static_cast<double>(color.a), flags);
303 StringAppendF(&result, "tr=%s", transform.to_string().c_str());
304 result.append("\n");
305 StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
306 StringAppendF(&result, " zOrderRelativeOf=%s\n",
307 zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
308 StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
Yichi Chen6ca35192018-05-29 12:20:43 +0800309 StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str());
rongliucfb187b2018-03-14 12:26:23 -0700310 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
311 StringAppendF(&result, " windowType=%d, appId=%d", windowType, appId);
chaviw5bf9d682017-10-25 16:31:08 -0700312
313 return result;
314}
315
chaviw1d044282017-09-27 12:19:28 -0700316} // namespace surfaceflinger
317} // namespace android