blob: b10e07b2154c8cb786f8b2020dd1dfb2d04c34f1 [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();
rongliucfb187b2018-03-14 12:26:23 -0700117 layer->windowType = layerProto.window_type();
118 layer->appId = layerProto.app_id();
chaviw1d044282017-09-27 12:19:28 -0700119
120 return layer;
121}
122
123LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
124 LayerProtoParser::Region region;
125 region.id = regionProto.id();
126 for (int i = 0; i < regionProto.rect_size(); i++) {
127 const RectProto& rectProto = regionProto.rect(i);
128 region.rects.push_back(generateRect(rectProto));
129 }
130
131 return region;
132}
133
134LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
135 LayerProtoParser::Rect rect;
136 rect.left = rectProto.left();
137 rect.top = rectProto.top();
138 rect.right = rectProto.right();
139 rect.bottom = rectProto.bottom();
140
141 return rect;
142}
143
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800144LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
145 LayerProtoParser::FloatRect rect;
146 rect.left = rectProto.left();
147 rect.top = rectProto.top();
148 rect.right = rectProto.right();
149 rect.bottom = rectProto.bottom();
150
151 return rect;
152}
153
chaviw1d044282017-09-27 12:19:28 -0700154LayerProtoParser::Transform LayerProtoParser::generateTransform(
155 const TransformProto& transformProto) {
156 LayerProtoParser::Transform transform;
157 transform.dsdx = transformProto.dsdx();
158 transform.dtdx = transformProto.dtdx();
159 transform.dsdy = transformProto.dsdy();
160 transform.dtdy = transformProto.dtdy();
161
162 return transform;
163}
164
165LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
166 const ActiveBufferProto& activeBufferProto) {
167 LayerProtoParser::ActiveBuffer activeBuffer;
168 activeBuffer.width = activeBufferProto.width();
169 activeBuffer.height = activeBufferProto.height();
170 activeBuffer.stride = activeBufferProto.stride();
171 activeBuffer.format = activeBufferProto.format();
172
173 return activeBuffer;
174}
175
176void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
177 std::unordered_map<int32_t, Layer*>& layerMap) {
178 auto currLayer = layerMap[layerProto.id()];
179
180 for (int i = 0; i < layerProto.children_size(); i++) {
181 if (layerMap.count(layerProto.children(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700182 // Only make unique_ptrs for children since they are guaranteed to be unique, only one
183 // parent per child. This ensures there will only be one unique_ptr made for each layer.
184 currLayer->children.push_back(std::unique_ptr<Layer>(layerMap[layerProto.children(i)]));
chaviw1d044282017-09-27 12:19:28 -0700185 }
186 }
187
188 for (int i = 0; i < layerProto.relatives_size(); i++) {
189 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700190 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700191 }
192 }
193
194 if (layerProto.has_parent()) {
195 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700196 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700197 }
198 }
199
200 if (layerProto.has_z_order_relative_of()) {
201 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700202 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700203 }
204 }
205}
206
207std::string LayerProtoParser::layersToString(
chaviw7ba019b2018-03-14 13:28:39 -0700208 std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers) {
chaviw1d044282017-09-27 12:19:28 -0700209 std::string result;
chaviw7ba019b2018-03-14 13:28:39 -0700210 for (std::unique_ptr<LayerProtoParser::Layer>& layer : layers) {
chaviw1d044282017-09-27 12:19:28 -0700211 if (layer->zOrderRelativeOf != nullptr) {
212 continue;
213 }
chaviw7ba019b2018-03-14 13:28:39 -0700214 result.append(layerToString(layer.get()).c_str());
chaviw1d044282017-09-27 12:19:28 -0700215 }
216
217 return result;
218}
219
chaviw7ba019b2018-03-14 13:28:39 -0700220std::string LayerProtoParser::layerToString(LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700221 std::string result;
222
chaviw7ba019b2018-03-14 13:28:39 -0700223 std::vector<Layer*> traverse(layer->relatives);
224 for (std::unique_ptr<LayerProtoParser::Layer>& child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700225 if (child->zOrderRelativeOf != nullptr) {
226 continue;
227 }
228
chaviw7ba019b2018-03-14 13:28:39 -0700229 traverse.push_back(child.get());
chaviw1d044282017-09-27 12:19:28 -0700230 }
231
232 std::sort(traverse.begin(), traverse.end(), sortLayers);
233
234 size_t i = 0;
235 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700236 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700237 if (relative->z >= 0) {
238 break;
239 }
240 result.append(layerToString(relative).c_str());
241 }
242 result.append(layer->to_string().c_str());
243 result.append("\n");
244 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700245 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700246 result.append(layerToString(relative).c_str());
247 }
248
249 return result;
250}
251
chaviw5bf9d682017-10-25 16:31:08 -0700252std::string LayerProtoParser::ActiveBuffer::to_string() const {
253 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
254 decodePixelFormat(format).c_str());
255}
256
257std::string LayerProtoParser::Transform::to_string() const {
258 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
259 static_cast<double>(dtdx), static_cast<double>(dsdy),
260 static_cast<double>(dtdy));
261}
262
263std::string LayerProtoParser::Rect::to_string() const {
264 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
265}
266
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800267std::string LayerProtoParser::FloatRect::to_string() const {
268 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
269}
270
chaviw5bf9d682017-10-25 16:31:08 -0700271std::string LayerProtoParser::Region::to_string(const char* what) const {
272 std::string result =
273 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
274 static_cast<int>(rects.size()));
275
276 for (auto& rect : rects) {
277 StringAppendF(&result, " %s\n", rect.to_string().c_str());
278 }
279
280 return result;
281}
282
283std::string LayerProtoParser::Layer::to_string() const {
284 std::string result;
285 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
286 result.append(transparentRegion.to_string("TransparentRegion").c_str());
287 result.append(visibleRegion.to_string("VisibleRegion").c_str());
288 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
289
290 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
291 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
292 size.y);
293
294 StringAppendF(&result, "crop=%s, finalCrop=%s, ", crop.to_string().c_str(),
295 finalCrop.to_string().c_str());
296 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
297 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
298 StringAppendF(&result, "pixelformat=%s, ", pixelFormat.c_str());
299 StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
300 static_cast<double>(color.r), static_cast<double>(color.g),
301 static_cast<double>(color.b), static_cast<double>(color.a), flags);
302 StringAppendF(&result, "tr=%s", transform.to_string().c_str());
303 result.append("\n");
304 StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
305 StringAppendF(&result, " zOrderRelativeOf=%s\n",
306 zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
307 StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
rongliucfb187b2018-03-14 12:26:23 -0700308 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
309 StringAppendF(&result, " windowType=%d, appId=%d", windowType, appId);
chaviw5bf9d682017-10-25 16:31:08 -0700310
311 return result;
312}
313
chaviw1d044282017-09-27 12:19:28 -0700314} // namespace surfaceflinger
315} // namespace android