blob: 909f1f37bfa9b46e02a2b6a54b61f4979ff25ac9 [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) {
34 return (lz > rz) ? 1 : -1;
35 }
chaviw1d044282017-09-27 12:19:28 -070036
37 return lhs->id < rhs->id;
38}
39
chaviw7ba019b2018-03-14 13:28:39 -070040bool sortLayerUniquePtrs(const std::unique_ptr<LayerProtoParser::Layer>& lhs,
41 const std::unique_ptr<LayerProtoParser::Layer>& rhs) {
42 return sortLayers(lhs.get(), rhs.get());
43}
44
Yiwei Zhang7124ad32018-02-21 13:02:45 -080045const LayerProtoParser::LayerGlobal LayerProtoParser::generateLayerGlobalInfo(
46 const LayersProto& layersProto) {
47 return {{layersProto.resolution().w(), layersProto.resolution().h()}};
48}
49
chaviw7ba019b2018-03-14 13:28:39 -070050std::vector<std::unique_ptr<LayerProtoParser::Layer>> LayerProtoParser::generateLayerTree(
chaviw1d044282017-09-27 12:19:28 -070051 const LayersProto& layersProto) {
chaviw7ba019b2018-03-14 13:28:39 -070052 std::unordered_map<int32_t, LayerProtoParser::Layer*> layerMap = generateMap(layersProto);
53 std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers;
chaviw1d044282017-09-27 12:19:28 -070054
chaviw7ba019b2018-03-14 13:28:39 -070055 for (std::pair<int32_t, Layer*> kv : layerMap) {
56 if (kv.second->parent == nullptr) {
57 // Make unique_ptr for top level layers since they are not children. This ensures there
58 // will only be one unique_ptr made for each layer.
59 layers.push_back(std::unique_ptr<Layer>(kv.second));
60 }
61 }
chaviw1d044282017-09-27 12:19:28 -070062
chaviw7ba019b2018-03-14 13:28:39 -070063 std::sort(layers.begin(), layers.end(), sortLayerUniquePtrs);
chaviw1d044282017-09-27 12:19:28 -070064 return layers;
65}
66
67std::unordered_map<int32_t, LayerProtoParser::Layer*> LayerProtoParser::generateMap(
68 const LayersProto& layersProto) {
69 std::unordered_map<int32_t, Layer*> layerMap;
70
71 for (int i = 0; i < layersProto.layers_size(); i++) {
72 const LayerProto& layerProto = layersProto.layers(i);
73 layerMap[layerProto.id()] = generateLayer(layerProto);
74 }
75
76 for (int i = 0; i < layersProto.layers_size(); i++) {
77 const LayerProto& layerProto = layersProto.layers(i);
78 updateChildrenAndRelative(layerProto, layerMap);
79 }
80
81 return layerMap;
82}
83
84LayerProtoParser::Layer* LayerProtoParser::generateLayer(const LayerProto& layerProto) {
85 Layer* layer = new Layer();
86 layer->id = layerProto.id();
87 layer->name = layerProto.name();
88 layer->type = layerProto.type();
89 layer->transparentRegion = generateRegion(layerProto.transparent_region());
90 layer->visibleRegion = generateRegion(layerProto.visible_region());
91 layer->damageRegion = generateRegion(layerProto.damage_region());
92 layer->layerStack = layerProto.layer_stack();
93 layer->z = layerProto.z();
94 layer->position = {layerProto.position().x(), layerProto.position().y()};
95 layer->requestedPosition = {layerProto.requested_position().x(),
96 layerProto.requested_position().y()};
97 layer->size = {layerProto.size().w(), layerProto.size().h()};
98 layer->crop = generateRect(layerProto.crop());
99 layer->finalCrop = generateRect(layerProto.final_crop());
100 layer->isOpaque = layerProto.is_opaque();
101 layer->invalidate = layerProto.invalidate();
102 layer->dataspace = layerProto.dataspace();
103 layer->pixelFormat = layerProto.pixel_format();
104 layer->color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(),
105 layerProto.color().a()};
106 layer->requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
107 layerProto.requested_color().b(), layerProto.requested_color().a()};
108 layer->flags = layerProto.flags();
109 layer->transform = generateTransform(layerProto.transform());
110 layer->requestedTransform = generateTransform(layerProto.requested_transform());
111 layer->activeBuffer = generateActiveBuffer(layerProto.active_buffer());
112 layer->queuedFrames = layerProto.queued_frames();
113 layer->refreshPending = layerProto.refresh_pending();
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800114 layer->hwcFrame = generateRect(layerProto.hwc_frame());
115 layer->hwcCrop = generateFloatRect(layerProto.hwc_crop());
116 layer->hwcTransform = layerProto.hwc_transform();
chaviw1d044282017-09-27 12:19:28 -0700117
118 return layer;
119}
120
121LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
122 LayerProtoParser::Region region;
123 region.id = regionProto.id();
124 for (int i = 0; i < regionProto.rect_size(); i++) {
125 const RectProto& rectProto = regionProto.rect(i);
126 region.rects.push_back(generateRect(rectProto));
127 }
128
129 return region;
130}
131
132LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
133 LayerProtoParser::Rect rect;
134 rect.left = rectProto.left();
135 rect.top = rectProto.top();
136 rect.right = rectProto.right();
137 rect.bottom = rectProto.bottom();
138
139 return rect;
140}
141
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800142LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
143 LayerProtoParser::FloatRect rect;
144 rect.left = rectProto.left();
145 rect.top = rectProto.top();
146 rect.right = rectProto.right();
147 rect.bottom = rectProto.bottom();
148
149 return rect;
150}
151
chaviw1d044282017-09-27 12:19:28 -0700152LayerProtoParser::Transform LayerProtoParser::generateTransform(
153 const TransformProto& transformProto) {
154 LayerProtoParser::Transform transform;
155 transform.dsdx = transformProto.dsdx();
156 transform.dtdx = transformProto.dtdx();
157 transform.dsdy = transformProto.dsdy();
158 transform.dtdy = transformProto.dtdy();
159
160 return transform;
161}
162
163LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
164 const ActiveBufferProto& activeBufferProto) {
165 LayerProtoParser::ActiveBuffer activeBuffer;
166 activeBuffer.width = activeBufferProto.width();
167 activeBuffer.height = activeBufferProto.height();
168 activeBuffer.stride = activeBufferProto.stride();
169 activeBuffer.format = activeBufferProto.format();
170
171 return activeBuffer;
172}
173
174void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
175 std::unordered_map<int32_t, Layer*>& layerMap) {
176 auto currLayer = layerMap[layerProto.id()];
177
178 for (int i = 0; i < layerProto.children_size(); i++) {
179 if (layerMap.count(layerProto.children(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700180 // Only make unique_ptrs for children since they are guaranteed to be unique, only one
181 // parent per child. This ensures there will only be one unique_ptr made for each layer.
182 currLayer->children.push_back(std::unique_ptr<Layer>(layerMap[layerProto.children(i)]));
chaviw1d044282017-09-27 12:19:28 -0700183 }
184 }
185
186 for (int i = 0; i < layerProto.relatives_size(); i++) {
187 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700188 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700189 }
190 }
191
192 if (layerProto.has_parent()) {
193 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700194 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700195 }
196 }
197
198 if (layerProto.has_z_order_relative_of()) {
199 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700200 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700201 }
202 }
203}
204
205std::string LayerProtoParser::layersToString(
chaviw7ba019b2018-03-14 13:28:39 -0700206 std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers) {
chaviw1d044282017-09-27 12:19:28 -0700207 std::string result;
chaviw7ba019b2018-03-14 13:28:39 -0700208 for (std::unique_ptr<LayerProtoParser::Layer>& layer : layers) {
chaviw1d044282017-09-27 12:19:28 -0700209 if (layer->zOrderRelativeOf != nullptr) {
210 continue;
211 }
chaviw7ba019b2018-03-14 13:28:39 -0700212 result.append(layerToString(layer.get()).c_str());
chaviw1d044282017-09-27 12:19:28 -0700213 }
214
215 return result;
216}
217
chaviw7ba019b2018-03-14 13:28:39 -0700218std::string LayerProtoParser::layerToString(LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700219 std::string result;
220
chaviw7ba019b2018-03-14 13:28:39 -0700221 std::vector<Layer*> traverse(layer->relatives);
222 for (std::unique_ptr<LayerProtoParser::Layer>& child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700223 if (child->zOrderRelativeOf != nullptr) {
224 continue;
225 }
226
chaviw7ba019b2018-03-14 13:28:39 -0700227 traverse.push_back(child.get());
chaviw1d044282017-09-27 12:19:28 -0700228 }
229
230 std::sort(traverse.begin(), traverse.end(), sortLayers);
231
232 size_t i = 0;
233 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700234 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700235 if (relative->z >= 0) {
236 break;
237 }
238 result.append(layerToString(relative).c_str());
239 }
240 result.append(layer->to_string().c_str());
241 result.append("\n");
242 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700243 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700244 result.append(layerToString(relative).c_str());
245 }
246
247 return result;
248}
249
chaviw5bf9d682017-10-25 16:31:08 -0700250std::string LayerProtoParser::ActiveBuffer::to_string() const {
251 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
252 decodePixelFormat(format).c_str());
253}
254
255std::string LayerProtoParser::Transform::to_string() const {
256 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
257 static_cast<double>(dtdx), static_cast<double>(dsdy),
258 static_cast<double>(dtdy));
259}
260
261std::string LayerProtoParser::Rect::to_string() const {
262 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
263}
264
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800265std::string LayerProtoParser::FloatRect::to_string() const {
266 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
267}
268
chaviw5bf9d682017-10-25 16:31:08 -0700269std::string LayerProtoParser::Region::to_string(const char* what) const {
270 std::string result =
271 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
272 static_cast<int>(rects.size()));
273
274 for (auto& rect : rects) {
275 StringAppendF(&result, " %s\n", rect.to_string().c_str());
276 }
277
278 return result;
279}
280
281std::string LayerProtoParser::Layer::to_string() const {
282 std::string result;
283 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
284 result.append(transparentRegion.to_string("TransparentRegion").c_str());
285 result.append(visibleRegion.to_string("VisibleRegion").c_str());
286 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
287
288 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
289 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
290 size.y);
291
292 StringAppendF(&result, "crop=%s, finalCrop=%s, ", crop.to_string().c_str(),
293 finalCrop.to_string().c_str());
294 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
295 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
296 StringAppendF(&result, "pixelformat=%s, ", pixelFormat.c_str());
297 StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
298 static_cast<double>(color.r), static_cast<double>(color.g),
299 static_cast<double>(color.b), static_cast<double>(color.a), flags);
300 StringAppendF(&result, "tr=%s", transform.to_string().c_str());
301 result.append("\n");
302 StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
303 StringAppendF(&result, " zOrderRelativeOf=%s\n",
304 zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
305 StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
306 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d", queuedFrames, refreshPending);
307
308 return result;
309}
310
chaviw1d044282017-09-27 12:19:28 -0700311} // namespace surfaceflinger
312} // namespace android