blob: e1df1c093e32d97cfbac05a9e2880d68e9861730 [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();
109
110 return layer;
111}
112
113LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
114 LayerProtoParser::Region region;
115 region.id = regionProto.id();
116 for (int i = 0; i < regionProto.rect_size(); i++) {
117 const RectProto& rectProto = regionProto.rect(i);
118 region.rects.push_back(generateRect(rectProto));
119 }
120
121 return region;
122}
123
124LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
125 LayerProtoParser::Rect rect;
126 rect.left = rectProto.left();
127 rect.top = rectProto.top();
128 rect.right = rectProto.right();
129 rect.bottom = rectProto.bottom();
130
131 return rect;
132}
133
134LayerProtoParser::Transform LayerProtoParser::generateTransform(
135 const TransformProto& transformProto) {
136 LayerProtoParser::Transform transform;
137 transform.dsdx = transformProto.dsdx();
138 transform.dtdx = transformProto.dtdx();
139 transform.dsdy = transformProto.dsdy();
140 transform.dtdy = transformProto.dtdy();
141
142 return transform;
143}
144
145LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
146 const ActiveBufferProto& activeBufferProto) {
147 LayerProtoParser::ActiveBuffer activeBuffer;
148 activeBuffer.width = activeBufferProto.width();
149 activeBuffer.height = activeBufferProto.height();
150 activeBuffer.stride = activeBufferProto.stride();
151 activeBuffer.format = activeBufferProto.format();
152
153 return activeBuffer;
154}
155
156void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
157 std::unordered_map<int32_t, Layer*>& layerMap) {
158 auto currLayer = layerMap[layerProto.id()];
159
160 for (int i = 0; i < layerProto.children_size(); i++) {
161 if (layerMap.count(layerProto.children(i)) > 0) {
chaviw7794ec12018-03-14 13:28:39 -0700162 // Only make unique_ptrs for children since they are guaranteed to be unique, only one
163 // parent per child. This ensures there will only be one unique_ptr made for each layer.
164 currLayer->children.push_back(std::unique_ptr<Layer>(layerMap[layerProto.children(i)]));
chaviw1d044282017-09-27 12:19:28 -0700165 }
166 }
167
168 for (int i = 0; i < layerProto.relatives_size(); i++) {
169 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7794ec12018-03-14 13:28:39 -0700170 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700171 }
172 }
173
174 if (layerProto.has_parent()) {
175 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7794ec12018-03-14 13:28:39 -0700176 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700177 }
178 }
179
180 if (layerProto.has_z_order_relative_of()) {
181 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7794ec12018-03-14 13:28:39 -0700182 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700183 }
184 }
185}
186
187std::string LayerProtoParser::layersToString(
chaviw7794ec12018-03-14 13:28:39 -0700188 std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers) {
chaviw1d044282017-09-27 12:19:28 -0700189 std::string result;
chaviw7794ec12018-03-14 13:28:39 -0700190 for (std::unique_ptr<LayerProtoParser::Layer>& layer : layers) {
chaviw1d044282017-09-27 12:19:28 -0700191 if (layer->zOrderRelativeOf != nullptr) {
192 continue;
193 }
chaviw7794ec12018-03-14 13:28:39 -0700194 result.append(layerToString(layer.get()).c_str());
chaviw1d044282017-09-27 12:19:28 -0700195 }
196
197 return result;
198}
199
chaviw7794ec12018-03-14 13:28:39 -0700200std::string LayerProtoParser::layerToString(LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700201 std::string result;
202
chaviw7794ec12018-03-14 13:28:39 -0700203 std::vector<Layer*> traverse(layer->relatives);
204 for (std::unique_ptr<LayerProtoParser::Layer>& child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700205 if (child->zOrderRelativeOf != nullptr) {
206 continue;
207 }
208
chaviw7794ec12018-03-14 13:28:39 -0700209 traverse.push_back(child.get());
chaviw1d044282017-09-27 12:19:28 -0700210 }
211
212 std::sort(traverse.begin(), traverse.end(), sortLayers);
213
214 size_t i = 0;
215 for (; i < traverse.size(); i++) {
chaviw7794ec12018-03-14 13:28:39 -0700216 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700217 if (relative->z >= 0) {
218 break;
219 }
220 result.append(layerToString(relative).c_str());
221 }
222 result.append(layer->to_string().c_str());
223 result.append("\n");
224 for (; i < traverse.size(); i++) {
chaviw7794ec12018-03-14 13:28:39 -0700225 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700226 result.append(layerToString(relative).c_str());
227 }
228
229 return result;
230}
231
chaviw5bf9d682017-10-25 16:31:08 -0700232std::string LayerProtoParser::ActiveBuffer::to_string() const {
233 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
234 decodePixelFormat(format).c_str());
235}
236
237std::string LayerProtoParser::Transform::to_string() const {
238 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
239 static_cast<double>(dtdx), static_cast<double>(dsdy),
240 static_cast<double>(dtdy));
241}
242
243std::string LayerProtoParser::Rect::to_string() const {
244 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
245}
246
247std::string LayerProtoParser::Region::to_string(const char* what) const {
248 std::string result =
249 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
250 static_cast<int>(rects.size()));
251
252 for (auto& rect : rects) {
253 StringAppendF(&result, " %s\n", rect.to_string().c_str());
254 }
255
256 return result;
257}
258
259std::string LayerProtoParser::Layer::to_string() const {
260 std::string result;
261 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
262 result.append(transparentRegion.to_string("TransparentRegion").c_str());
263 result.append(visibleRegion.to_string("VisibleRegion").c_str());
264 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
265
266 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
267 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
268 size.y);
269
270 StringAppendF(&result, "crop=%s, finalCrop=%s, ", crop.to_string().c_str(),
271 finalCrop.to_string().c_str());
272 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
273 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
Chia-I Wu1e043612018-03-01 09:45:09 -0800274 StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
chaviw5bf9d682017-10-25 16:31:08 -0700275 StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
276 static_cast<double>(color.r), static_cast<double>(color.g),
277 static_cast<double>(color.b), static_cast<double>(color.a), flags);
278 StringAppendF(&result, "tr=%s", transform.to_string().c_str());
279 result.append("\n");
280 StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
281 StringAppendF(&result, " zOrderRelativeOf=%s\n",
282 zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
283 StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
284 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d", queuedFrames, refreshPending);
285
286 return result;
287}
288
chaviw1d044282017-09-27 12:19:28 -0700289} // namespace surfaceflinger
290} // namespace android