blob: 1383d288b90412d0aa1c4bca5834a9f1be0f3676 [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
chaviw7794ec12018-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
chaviw7794ec12018-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
45std::vector<std::unique_ptr<LayerProtoParser::Layer>> LayerProtoParser::generateLayerTree(
chaviw1d044282017-09-27 12:19:28 -070046 const LayersProto& layersProto) {
chaviw7794ec12018-03-14 13:28:39 -070047 std::unordered_map<int32_t, LayerProtoParser::Layer*> layerMap = generateMap(layersProto);
48 std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers;
chaviw1d044282017-09-27 12:19:28 -070049
chaviw7794ec12018-03-14 13:28:39 -070050 for (std::pair<int32_t, Layer*> kv : layerMap) {
51 if (kv.second->parent == nullptr) {
52 // Make unique_ptr for top level layers since they are not children. This ensures there
53 // will only be one unique_ptr made for each layer.
54 layers.push_back(std::unique_ptr<Layer>(kv.second));
55 }
56 }
chaviw1d044282017-09-27 12:19:28 -070057
chaviw7794ec12018-03-14 13:28:39 -070058 std::sort(layers.begin(), layers.end(), sortLayerUniquePtrs);
chaviw1d044282017-09-27 12:19:28 -070059 return layers;
60}
61
62std::unordered_map<int32_t, LayerProtoParser::Layer*> LayerProtoParser::generateMap(
63 const LayersProto& layersProto) {
64 std::unordered_map<int32_t, Layer*> layerMap;
65
66 for (int i = 0; i < layersProto.layers_size(); i++) {
67 const LayerProto& layerProto = layersProto.layers(i);
68 layerMap[layerProto.id()] = generateLayer(layerProto);
69 }
70
71 for (int i = 0; i < layersProto.layers_size(); i++) {
72 const LayerProto& layerProto = layersProto.layers(i);
73 updateChildrenAndRelative(layerProto, layerMap);
74 }
75
76 return layerMap;
77}
78
79LayerProtoParser::Layer* LayerProtoParser::generateLayer(const LayerProto& layerProto) {
80 Layer* layer = new Layer();
81 layer->id = layerProto.id();
82 layer->name = layerProto.name();
83 layer->type = layerProto.type();
84 layer->transparentRegion = generateRegion(layerProto.transparent_region());
85 layer->visibleRegion = generateRegion(layerProto.visible_region());
86 layer->damageRegion = generateRegion(layerProto.damage_region());
87 layer->layerStack = layerProto.layer_stack();
88 layer->z = layerProto.z();
89 layer->position = {layerProto.position().x(), layerProto.position().y()};
90 layer->requestedPosition = {layerProto.requested_position().x(),
91 layerProto.requested_position().y()};
92 layer->size = {layerProto.size().w(), layerProto.size().h()};
93 layer->crop = generateRect(layerProto.crop());
94 layer->finalCrop = generateRect(layerProto.final_crop());
95 layer->isOpaque = layerProto.is_opaque();
96 layer->invalidate = layerProto.invalidate();
97 layer->dataspace = layerProto.dataspace();
98 layer->pixelFormat = layerProto.pixel_format();
99 layer->color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(),
100 layerProto.color().a()};
101 layer->requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
102 layerProto.requested_color().b(), layerProto.requested_color().a()};
103 layer->flags = layerProto.flags();
104 layer->transform = generateTransform(layerProto.transform());
105 layer->requestedTransform = generateTransform(layerProto.requested_transform());
106 layer->activeBuffer = generateActiveBuffer(layerProto.active_buffer());
107 layer->queuedFrames = layerProto.queued_frames();
108 layer->refreshPending = layerProto.refresh_pending();
rongliuccd34842018-03-14 12:26:23 -0700109 layer->windowType = layerProto.window_type();
110 layer->appId = layerProto.app_id();
chaviw1d044282017-09-27 12:19:28 -0700111
112 return layer;
113}
114
115LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
116 LayerProtoParser::Region region;
117 region.id = regionProto.id();
118 for (int i = 0; i < regionProto.rect_size(); i++) {
119 const RectProto& rectProto = regionProto.rect(i);
120 region.rects.push_back(generateRect(rectProto));
121 }
122
123 return region;
124}
125
126LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
127 LayerProtoParser::Rect rect;
128 rect.left = rectProto.left();
129 rect.top = rectProto.top();
130 rect.right = rectProto.right();
131 rect.bottom = rectProto.bottom();
132
133 return rect;
134}
135
136LayerProtoParser::Transform LayerProtoParser::generateTransform(
137 const TransformProto& transformProto) {
138 LayerProtoParser::Transform transform;
139 transform.dsdx = transformProto.dsdx();
140 transform.dtdx = transformProto.dtdx();
141 transform.dsdy = transformProto.dsdy();
142 transform.dtdy = transformProto.dtdy();
143
144 return transform;
145}
146
147LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
148 const ActiveBufferProto& activeBufferProto) {
149 LayerProtoParser::ActiveBuffer activeBuffer;
150 activeBuffer.width = activeBufferProto.width();
151 activeBuffer.height = activeBufferProto.height();
152 activeBuffer.stride = activeBufferProto.stride();
153 activeBuffer.format = activeBufferProto.format();
154
155 return activeBuffer;
156}
157
158void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
159 std::unordered_map<int32_t, Layer*>& layerMap) {
160 auto currLayer = layerMap[layerProto.id()];
161
162 for (int i = 0; i < layerProto.children_size(); i++) {
163 if (layerMap.count(layerProto.children(i)) > 0) {
chaviw7794ec12018-03-14 13:28:39 -0700164 // Only make unique_ptrs for children since they are guaranteed to be unique, only one
165 // parent per child. This ensures there will only be one unique_ptr made for each layer.
166 currLayer->children.push_back(std::unique_ptr<Layer>(layerMap[layerProto.children(i)]));
chaviw1d044282017-09-27 12:19:28 -0700167 }
168 }
169
170 for (int i = 0; i < layerProto.relatives_size(); i++) {
171 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7794ec12018-03-14 13:28:39 -0700172 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700173 }
174 }
175
176 if (layerProto.has_parent()) {
177 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7794ec12018-03-14 13:28:39 -0700178 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700179 }
180 }
181
182 if (layerProto.has_z_order_relative_of()) {
183 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7794ec12018-03-14 13:28:39 -0700184 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700185 }
186 }
187}
188
189std::string LayerProtoParser::layersToString(
chaviw7794ec12018-03-14 13:28:39 -0700190 std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers) {
chaviw1d044282017-09-27 12:19:28 -0700191 std::string result;
chaviw7794ec12018-03-14 13:28:39 -0700192 for (std::unique_ptr<LayerProtoParser::Layer>& layer : layers) {
chaviw1d044282017-09-27 12:19:28 -0700193 if (layer->zOrderRelativeOf != nullptr) {
194 continue;
195 }
chaviw7794ec12018-03-14 13:28:39 -0700196 result.append(layerToString(layer.get()).c_str());
chaviw1d044282017-09-27 12:19:28 -0700197 }
198
199 return result;
200}
201
chaviw7794ec12018-03-14 13:28:39 -0700202std::string LayerProtoParser::layerToString(LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700203 std::string result;
204
chaviw7794ec12018-03-14 13:28:39 -0700205 std::vector<Layer*> traverse(layer->relatives);
206 for (std::unique_ptr<LayerProtoParser::Layer>& child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700207 if (child->zOrderRelativeOf != nullptr) {
208 continue;
209 }
210
chaviw7794ec12018-03-14 13:28:39 -0700211 traverse.push_back(child.get());
chaviw1d044282017-09-27 12:19:28 -0700212 }
213
214 std::sort(traverse.begin(), traverse.end(), sortLayers);
215
216 size_t i = 0;
217 for (; i < traverse.size(); i++) {
chaviw7794ec12018-03-14 13:28:39 -0700218 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700219 if (relative->z >= 0) {
220 break;
221 }
222 result.append(layerToString(relative).c_str());
223 }
224 result.append(layer->to_string().c_str());
225 result.append("\n");
226 for (; i < traverse.size(); i++) {
chaviw7794ec12018-03-14 13:28:39 -0700227 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700228 result.append(layerToString(relative).c_str());
229 }
230
231 return result;
232}
233
chaviw5bf9d682017-10-25 16:31:08 -0700234std::string LayerProtoParser::ActiveBuffer::to_string() const {
235 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
236 decodePixelFormat(format).c_str());
237}
238
239std::string LayerProtoParser::Transform::to_string() const {
240 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
241 static_cast<double>(dtdx), static_cast<double>(dsdy),
242 static_cast<double>(dtdy));
243}
244
245std::string LayerProtoParser::Rect::to_string() const {
246 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
247}
248
249std::string LayerProtoParser::Region::to_string(const char* what) const {
250 std::string result =
251 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
252 static_cast<int>(rects.size()));
253
254 for (auto& rect : rects) {
255 StringAppendF(&result, " %s\n", rect.to_string().c_str());
256 }
257
258 return result;
259}
260
261std::string LayerProtoParser::Layer::to_string() const {
262 std::string result;
263 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
264 result.append(transparentRegion.to_string("TransparentRegion").c_str());
265 result.append(visibleRegion.to_string("VisibleRegion").c_str());
266 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
267
268 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
269 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
270 size.y);
271
272 StringAppendF(&result, "crop=%s, finalCrop=%s, ", crop.to_string().c_str(),
273 finalCrop.to_string().c_str());
274 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
275 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
Chia-I Wu1e043612018-03-01 09:45:09 -0800276 StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
chaviw5bf9d682017-10-25 16:31:08 -0700277 StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
278 static_cast<double>(color.r), static_cast<double>(color.g),
279 static_cast<double>(color.b), static_cast<double>(color.a), flags);
280 StringAppendF(&result, "tr=%s", transform.to_string().c_str());
281 result.append("\n");
282 StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
283 StringAppendF(&result, " zOrderRelativeOf=%s\n",
284 zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
285 StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
rongliuccd34842018-03-14 12:26:23 -0700286 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
287 StringAppendF(&result, " windowType=%d, appId=%d", windowType, appId);
chaviw5bf9d682017-10-25 16:31:08 -0700288
289 return result;
290}
291
chaviw1d044282017-09-27 12:19:28 -0700292} // namespace surfaceflinger
293} // namespace android