blob: bf12df6de46cc9dd24f1ef9eaa35889a79614d04 [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
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) {
Yiwei Zhang60d1a192018-03-07 14:52:28 -080047 LayerGlobal layerGlobal;
48 layerGlobal.resolution = {layersProto.resolution().w(), layersProto.resolution().h()};
49 layerGlobal.colorMode = layersProto.color_mode();
50 layerGlobal.colorTransform = layersProto.color_transform();
51 layerGlobal.globalTransform = layersProto.global_transform();
52 return layerGlobal;
Yiwei Zhang7124ad32018-02-21 13:02:45 -080053}
54
chaviw7ba019b2018-03-14 13:28:39 -070055std::vector<std::unique_ptr<LayerProtoParser::Layer>> LayerProtoParser::generateLayerTree(
chaviw1d044282017-09-27 12:19:28 -070056 const LayersProto& layersProto) {
chaviw7ba019b2018-03-14 13:28:39 -070057 std::unordered_map<int32_t, LayerProtoParser::Layer*> layerMap = generateMap(layersProto);
58 std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers;
chaviw1d044282017-09-27 12:19:28 -070059
chaviw7ba019b2018-03-14 13:28:39 -070060 for (std::pair<int32_t, Layer*> kv : layerMap) {
61 if (kv.second->parent == nullptr) {
62 // Make unique_ptr for top level layers since they are not children. This ensures there
63 // will only be one unique_ptr made for each layer.
64 layers.push_back(std::unique_ptr<Layer>(kv.second));
65 }
66 }
chaviw1d044282017-09-27 12:19:28 -070067
chaviw7ba019b2018-03-14 13:28:39 -070068 std::sort(layers.begin(), layers.end(), sortLayerUniquePtrs);
chaviw1d044282017-09-27 12:19:28 -070069 return layers;
70}
71
72std::unordered_map<int32_t, LayerProtoParser::Layer*> LayerProtoParser::generateMap(
73 const LayersProto& layersProto) {
74 std::unordered_map<int32_t, Layer*> layerMap;
75
76 for (int i = 0; i < layersProto.layers_size(); i++) {
77 const LayerProto& layerProto = layersProto.layers(i);
78 layerMap[layerProto.id()] = generateLayer(layerProto);
79 }
80
81 for (int i = 0; i < layersProto.layers_size(); i++) {
82 const LayerProto& layerProto = layersProto.layers(i);
83 updateChildrenAndRelative(layerProto, layerMap);
84 }
85
86 return layerMap;
87}
88
89LayerProtoParser::Layer* LayerProtoParser::generateLayer(const LayerProto& layerProto) {
90 Layer* layer = new Layer();
91 layer->id = layerProto.id();
92 layer->name = layerProto.name();
93 layer->type = layerProto.type();
94 layer->transparentRegion = generateRegion(layerProto.transparent_region());
95 layer->visibleRegion = generateRegion(layerProto.visible_region());
96 layer->damageRegion = generateRegion(layerProto.damage_region());
97 layer->layerStack = layerProto.layer_stack();
98 layer->z = layerProto.z();
99 layer->position = {layerProto.position().x(), layerProto.position().y()};
100 layer->requestedPosition = {layerProto.requested_position().x(),
101 layerProto.requested_position().y()};
102 layer->size = {layerProto.size().w(), layerProto.size().h()};
103 layer->crop = generateRect(layerProto.crop());
chaviw1d044282017-09-27 12:19:28 -0700104 layer->isOpaque = layerProto.is_opaque();
105 layer->invalidate = layerProto.invalidate();
106 layer->dataspace = layerProto.dataspace();
107 layer->pixelFormat = layerProto.pixel_format();
108 layer->color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(),
109 layerProto.color().a()};
110 layer->requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
111 layerProto.requested_color().b(), layerProto.requested_color().a()};
112 layer->flags = layerProto.flags();
113 layer->transform = generateTransform(layerProto.transform());
114 layer->requestedTransform = generateTransform(layerProto.requested_transform());
115 layer->activeBuffer = generateActiveBuffer(layerProto.active_buffer());
Yichi Chen6ca35192018-05-29 12:20:43 +0800116 layer->bufferTransform = generateTransform(layerProto.buffer_transform());
chaviw1d044282017-09-27 12:19:28 -0700117 layer->queuedFrames = layerProto.queued_frames();
118 layer->refreshPending = layerProto.refresh_pending();
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800119 layer->hwcFrame = generateRect(layerProto.hwc_frame());
120 layer->hwcCrop = generateFloatRect(layerProto.hwc_crop());
121 layer->hwcTransform = layerProto.hwc_transform();
rongliucfb187b2018-03-14 12:26:23 -0700122 layer->windowType = layerProto.window_type();
123 layer->appId = layerProto.app_id();
Yiwei Zhang60d1a192018-03-07 14:52:28 -0800124 layer->hwcCompositionType = layerProto.hwc_composition_type();
125 layer->isProtected = layerProto.is_protected();
chaviw1d044282017-09-27 12:19:28 -0700126
127 return layer;
128}
129
130LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
131 LayerProtoParser::Region region;
132 region.id = regionProto.id();
133 for (int i = 0; i < regionProto.rect_size(); i++) {
134 const RectProto& rectProto = regionProto.rect(i);
135 region.rects.push_back(generateRect(rectProto));
136 }
137
138 return region;
139}
140
141LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
142 LayerProtoParser::Rect rect;
143 rect.left = rectProto.left();
144 rect.top = rectProto.top();
145 rect.right = rectProto.right();
146 rect.bottom = rectProto.bottom();
147
148 return rect;
149}
150
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800151LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
152 LayerProtoParser::FloatRect rect;
153 rect.left = rectProto.left();
154 rect.top = rectProto.top();
155 rect.right = rectProto.right();
156 rect.bottom = rectProto.bottom();
157
158 return rect;
159}
160
chaviw1d044282017-09-27 12:19:28 -0700161LayerProtoParser::Transform LayerProtoParser::generateTransform(
162 const TransformProto& transformProto) {
163 LayerProtoParser::Transform transform;
164 transform.dsdx = transformProto.dsdx();
165 transform.dtdx = transformProto.dtdx();
166 transform.dsdy = transformProto.dsdy();
167 transform.dtdy = transformProto.dtdy();
168
169 return transform;
170}
171
172LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
173 const ActiveBufferProto& activeBufferProto) {
174 LayerProtoParser::ActiveBuffer activeBuffer;
175 activeBuffer.width = activeBufferProto.width();
176 activeBuffer.height = activeBufferProto.height();
177 activeBuffer.stride = activeBufferProto.stride();
178 activeBuffer.format = activeBufferProto.format();
179
180 return activeBuffer;
181}
182
183void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
184 std::unordered_map<int32_t, Layer*>& layerMap) {
185 auto currLayer = layerMap[layerProto.id()];
186
187 for (int i = 0; i < layerProto.children_size(); i++) {
188 if (layerMap.count(layerProto.children(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700189 // Only make unique_ptrs for children since they are guaranteed to be unique, only one
190 // parent per child. This ensures there will only be one unique_ptr made for each layer.
191 currLayer->children.push_back(std::unique_ptr<Layer>(layerMap[layerProto.children(i)]));
chaviw1d044282017-09-27 12:19:28 -0700192 }
193 }
194
195 for (int i = 0; i < layerProto.relatives_size(); i++) {
196 if (layerMap.count(layerProto.relatives(i)) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700197 currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
chaviw1d044282017-09-27 12:19:28 -0700198 }
199 }
200
201 if (layerProto.has_parent()) {
202 if (layerMap.count(layerProto.parent()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700203 currLayer->parent = layerMap[layerProto.parent()];
chaviw1d044282017-09-27 12:19:28 -0700204 }
205 }
206
207 if (layerProto.has_z_order_relative_of()) {
208 if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
chaviw7ba019b2018-03-14 13:28:39 -0700209 currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
chaviw1d044282017-09-27 12:19:28 -0700210 }
211 }
212}
213
214std::string LayerProtoParser::layersToString(
chaviw7ba019b2018-03-14 13:28:39 -0700215 std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers) {
chaviw1d044282017-09-27 12:19:28 -0700216 std::string result;
chaviw7ba019b2018-03-14 13:28:39 -0700217 for (std::unique_ptr<LayerProtoParser::Layer>& layer : layers) {
chaviw1d044282017-09-27 12:19:28 -0700218 if (layer->zOrderRelativeOf != nullptr) {
219 continue;
220 }
chaviw7ba019b2018-03-14 13:28:39 -0700221 result.append(layerToString(layer.get()).c_str());
chaviw1d044282017-09-27 12:19:28 -0700222 }
223
224 return result;
225}
226
chaviw7ba019b2018-03-14 13:28:39 -0700227std::string LayerProtoParser::layerToString(LayerProtoParser::Layer* layer) {
chaviw1d044282017-09-27 12:19:28 -0700228 std::string result;
229
chaviw7ba019b2018-03-14 13:28:39 -0700230 std::vector<Layer*> traverse(layer->relatives);
231 for (std::unique_ptr<LayerProtoParser::Layer>& child : layer->children) {
chaviw1d044282017-09-27 12:19:28 -0700232 if (child->zOrderRelativeOf != nullptr) {
233 continue;
234 }
235
chaviw7ba019b2018-03-14 13:28:39 -0700236 traverse.push_back(child.get());
chaviw1d044282017-09-27 12:19:28 -0700237 }
238
239 std::sort(traverse.begin(), traverse.end(), sortLayers);
240
241 size_t i = 0;
242 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700243 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700244 if (relative->z >= 0) {
245 break;
246 }
247 result.append(layerToString(relative).c_str());
248 }
249 result.append(layer->to_string().c_str());
250 result.append("\n");
251 for (; i < traverse.size(); i++) {
chaviw7ba019b2018-03-14 13:28:39 -0700252 auto& relative = traverse[i];
chaviw1d044282017-09-27 12:19:28 -0700253 result.append(layerToString(relative).c_str());
254 }
255
256 return result;
257}
258
chaviw5bf9d682017-10-25 16:31:08 -0700259std::string LayerProtoParser::ActiveBuffer::to_string() const {
260 return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
261 decodePixelFormat(format).c_str());
262}
263
264std::string LayerProtoParser::Transform::to_string() const {
265 return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
266 static_cast<double>(dtdx), static_cast<double>(dsdy),
267 static_cast<double>(dtdy));
268}
269
270std::string LayerProtoParser::Rect::to_string() const {
271 return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
272}
273
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800274std::string LayerProtoParser::FloatRect::to_string() const {
275 return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
276}
277
chaviw5bf9d682017-10-25 16:31:08 -0700278std::string LayerProtoParser::Region::to_string(const char* what) const {
279 std::string result =
280 StringPrintf(" Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
281 static_cast<int>(rects.size()));
282
283 for (auto& rect : rects) {
284 StringAppendF(&result, " %s\n", rect.to_string().c_str());
285 }
286
287 return result;
288}
289
290std::string LayerProtoParser::Layer::to_string() const {
291 std::string result;
292 StringAppendF(&result, "+ %s (%s)\n", type.c_str(), name.c_str());
293 result.append(transparentRegion.to_string("TransparentRegion").c_str());
294 result.append(visibleRegion.to_string("VisibleRegion").c_str());
295 result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
296
297 StringAppendF(&result, " layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
298 z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
299 size.y);
300
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700301 StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
chaviw5bf9d682017-10-25 16:31:08 -0700302 StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
303 StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
Chia-I Wu1e043612018-03-01 09:45:09 -0800304 StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
chaviw5bf9d682017-10-25 16:31:08 -0700305 StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
306 static_cast<double>(color.r), static_cast<double>(color.g),
307 static_cast<double>(color.b), static_cast<double>(color.a), flags);
308 StringAppendF(&result, "tr=%s", transform.to_string().c_str());
309 result.append("\n");
310 StringAppendF(&result, " parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
311 StringAppendF(&result, " zOrderRelativeOf=%s\n",
312 zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
313 StringAppendF(&result, " activeBuffer=%s,", activeBuffer.to_string().c_str());
Yichi Chen6ca35192018-05-29 12:20:43 +0800314 StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str());
rongliucfb187b2018-03-14 12:26:23 -0700315 StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
316 StringAppendF(&result, " windowType=%d, appId=%d", windowType, appId);
chaviw5bf9d682017-10-25 16:31:08 -0700317
318 return result;
319}
320
chaviw1d044282017-09-27 12:19:28 -0700321} // namespace surfaceflinger
322} // namespace android