• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 #include <android-base/stringprintf.h>
17 #include <layerproto/LayerProtoParser.h>
18 #include <ui/DebugUtils.h>
19 
20 using android::base::StringAppendF;
21 using android::base::StringPrintf;
22 
23 namespace android {
24 namespace surfaceflinger {
25 
sortLayers(LayerProtoParser::Layer * lhs,const LayerProtoParser::Layer * rhs)26 bool sortLayers(LayerProtoParser::Layer* lhs, const LayerProtoParser::Layer* rhs) {
27     uint32_t ls = lhs->layerStack;
28     uint32_t rs = rhs->layerStack;
29     if (ls != rs) return ls < rs;
30 
31     int32_t lz = lhs->z;
32     int32_t rz = rhs->z;
33     if (lz != rz) {
34         return lz < rz;
35     }
36 
37     return lhs->id < rhs->id;
38 }
39 
generateLayerTree(const LayersProto & layersProto)40 LayerProtoParser::LayerTree LayerProtoParser::generateLayerTree(const LayersProto& layersProto) {
41     LayerTree layerTree;
42     layerTree.allLayers = generateLayerList(layersProto);
43 
44     // find and sort the top-level layers
45     for (Layer& layer : layerTree.allLayers) {
46         if (layer.parent == nullptr) {
47             layerTree.topLevelLayers.push_back(&layer);
48         }
49     }
50     std::sort(layerTree.topLevelLayers.begin(), layerTree.topLevelLayers.end(), sortLayers);
51 
52     return layerTree;
53 }
54 
generateLayerList(const LayersProto & layersProto)55 std::vector<LayerProtoParser::Layer> LayerProtoParser::generateLayerList(
56         const LayersProto& layersProto) {
57     std::vector<Layer> layerList;
58     std::unordered_map<int32_t, Layer*> layerMap;
59 
60     // build the layer list and the layer map
61     layerList.reserve(layersProto.layers_size());
62     layerMap.reserve(layersProto.layers_size());
63     for (int i = 0; i < layersProto.layers_size(); i++) {
64         layerList.emplace_back(generateLayer(layersProto.layers(i)));
65         // this works because layerList never changes capacity
66         layerMap[layerList.back().id] = &layerList.back();
67     }
68 
69     // fix up children and relatives
70     for (int i = 0; i < layersProto.layers_size(); i++) {
71         updateChildrenAndRelative(layersProto.layers(i), layerMap);
72     }
73 
74     return layerList;
75 }
76 
generateLayer(const LayerProto & layerProto)77 LayerProtoParser::Layer LayerProtoParser::generateLayer(const LayerProto& layerProto) {
78     Layer layer;
79     layer.id = layerProto.id();
80     layer.name = layerProto.name();
81     layer.type = layerProto.type();
82     layer.transparentRegion = generateRegion(layerProto.transparent_region());
83     layer.visibleRegion = generateRegion(layerProto.visible_region());
84     layer.damageRegion = generateRegion(layerProto.damage_region());
85     layer.layerStack = layerProto.layer_stack();
86     layer.z = layerProto.z();
87     layer.position = {layerProto.position().x(), layerProto.position().y()};
88     layer.requestedPosition = {layerProto.requested_position().x(),
89                                 layerProto.requested_position().y()};
90     layer.size = {layerProto.size().w(), layerProto.size().h()};
91     layer.crop = generateRect(layerProto.crop());
92     layer.isOpaque = layerProto.is_opaque();
93     layer.invalidate = layerProto.invalidate();
94     layer.dataspace = layerProto.dataspace();
95     layer.pixelFormat = layerProto.pixel_format();
96     layer.color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(),
97                     layerProto.color().a()};
98     layer.requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
99                              layerProto.requested_color().b(), layerProto.requested_color().a()};
100     layer.flags = layerProto.flags();
101     layer.transform = generateTransform(layerProto.transform());
102     layer.requestedTransform = generateTransform(layerProto.requested_transform());
103     layer.activeBuffer = generateActiveBuffer(layerProto.active_buffer());
104     layer.bufferTransform = generateTransform(layerProto.buffer_transform());
105     layer.queuedFrames = layerProto.queued_frames();
106     layer.refreshPending = layerProto.refresh_pending();
107     layer.isProtected = layerProto.is_protected();
108     layer.isTrustedOverlay = layerProto.is_trusted_overlay();
109     layer.cornerRadius = layerProto.corner_radius();
110     layer.backgroundBlurRadius = layerProto.background_blur_radius();
111     for (const auto& entry : layerProto.metadata()) {
112         const std::string& dataStr = entry.second;
113         std::vector<uint8_t>& outData = layer.metadata.mMap[entry.first];
114         outData.resize(dataStr.size());
115         memcpy(outData.data(), dataStr.data(), dataStr.size());
116     }
117     layer.cornerRadiusCrop = generateFloatRect(layerProto.corner_radius_crop());
118     layer.shadowRadius = layerProto.shadow_radius();
119     layer.ownerUid = layerProto.owner_uid();
120     return layer;
121 }
122 
generateRegion(const RegionProto & regionProto)123 LayerProtoParser::Region LayerProtoParser::generateRegion(const RegionProto& regionProto) {
124     LayerProtoParser::Region region;
125     for (int i = 0; i < regionProto.rect_size(); i++) {
126         const RectProto& rectProto = regionProto.rect(i);
127         region.rects.push_back(generateRect(rectProto));
128     }
129 
130     return region;
131 }
132 
generateRect(const RectProto & rectProto)133 LayerProtoParser::Rect LayerProtoParser::generateRect(const RectProto& rectProto) {
134     LayerProtoParser::Rect rect;
135     rect.left = rectProto.left();
136     rect.top = rectProto.top();
137     rect.right = rectProto.right();
138     rect.bottom = rectProto.bottom();
139 
140     return rect;
141 }
142 
generateFloatRect(const FloatRectProto & rectProto)143 LayerProtoParser::FloatRect LayerProtoParser::generateFloatRect(const FloatRectProto& rectProto) {
144     LayerProtoParser::FloatRect rect;
145     rect.left = rectProto.left();
146     rect.top = rectProto.top();
147     rect.right = rectProto.right();
148     rect.bottom = rectProto.bottom();
149 
150     return rect;
151 }
152 
generateTransform(const TransformProto & transformProto)153 LayerProtoParser::Transform LayerProtoParser::generateTransform(
154         const TransformProto& transformProto) {
155     LayerProtoParser::Transform transform;
156     transform.dsdx = transformProto.dsdx();
157     transform.dtdx = transformProto.dtdx();
158     transform.dsdy = transformProto.dsdy();
159     transform.dtdy = transformProto.dtdy();
160 
161     return transform;
162 }
163 
generateActiveBuffer(const ActiveBufferProto & activeBufferProto)164 LayerProtoParser::ActiveBuffer LayerProtoParser::generateActiveBuffer(
165         const ActiveBufferProto& activeBufferProto) {
166     LayerProtoParser::ActiveBuffer activeBuffer;
167     activeBuffer.width = activeBufferProto.width();
168     activeBuffer.height = activeBufferProto.height();
169     activeBuffer.stride = activeBufferProto.stride();
170     activeBuffer.format = activeBufferProto.format();
171 
172     return activeBuffer;
173 }
174 
updateChildrenAndRelative(const LayerProto & layerProto,std::unordered_map<int32_t,Layer * > & layerMap)175 void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,
176                                                  std::unordered_map<int32_t, Layer*>& layerMap) {
177     auto currLayer = layerMap[layerProto.id()];
178 
179     for (int i = 0; i < layerProto.children_size(); i++) {
180         if (layerMap.count(layerProto.children(i)) > 0) {
181             currLayer->children.push_back(layerMap[layerProto.children(i)]);
182         }
183     }
184 
185     for (int i = 0; i < layerProto.relatives_size(); i++) {
186         if (layerMap.count(layerProto.relatives(i)) > 0) {
187             currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
188         }
189     }
190 
191     if (layerProto.parent() != -1) {
192         if (layerMap.count(layerProto.parent()) > 0) {
193             currLayer->parent = layerMap[layerProto.parent()];
194         }
195     }
196 
197     if (layerProto.z_order_relative_of() != -1) {
198         if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
199             currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
200         }
201     }
202 }
203 
layerTreeToString(const LayerTree & layerTree)204 std::string LayerProtoParser::layerTreeToString(const LayerTree& layerTree) {
205     std::string result;
206     for (const LayerProtoParser::Layer* layer : layerTree.topLevelLayers) {
207         if (layer->zOrderRelativeOf != nullptr) {
208             continue;
209         }
210         result.append(layerToString(layer));
211     }
212 
213     return result;
214 }
215 
layerToString(const LayerProtoParser::Layer * layer)216 std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
217     std::string result;
218 
219     std::vector<Layer*> traverse(layer->relatives);
220     for (LayerProtoParser::Layer* child : layer->children) {
221         if (child->zOrderRelativeOf != nullptr) {
222             continue;
223         }
224 
225         traverse.push_back(child);
226     }
227 
228     std::sort(traverse.begin(), traverse.end(), sortLayers);
229 
230     size_t i = 0;
231     for (; i < traverse.size(); i++) {
232         auto& relative = traverse[i];
233         if (relative->z >= 0) {
234             break;
235         }
236         result.append(layerToString(relative));
237     }
238     result.append(layer->to_string());
239     result.append("\n");
240     for (; i < traverse.size(); i++) {
241         auto& relative = traverse[i];
242         result.append(layerToString(relative));
243     }
244 
245     return result;
246 }
247 
to_string() const248 std::string LayerProtoParser::ActiveBuffer::to_string() const {
249     return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
250                         decodePixelFormat(format).c_str());
251 }
252 
to_string() const253 std::string LayerProtoParser::Transform::to_string() const {
254     return StringPrintf("[%.2f, %.2f][%.2f, %.2f]", static_cast<double>(dsdx),
255                         static_cast<double>(dtdx), static_cast<double>(dsdy),
256                         static_cast<double>(dtdy));
257 }
258 
to_string() const259 std::string LayerProtoParser::Rect::to_string() const {
260     return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
261 }
262 
to_string() const263 std::string LayerProtoParser::FloatRect::to_string() const {
264     return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
265 }
266 
to_string(const char * what) const267 std::string LayerProtoParser::Region::to_string(const char* what) const {
268     std::string result =
269             StringPrintf("  Region %s (this=%lx count=%d)\n", what, static_cast<unsigned long>(id),
270                          static_cast<int>(rects.size()));
271 
272     for (auto& rect : rects) {
273         StringAppendF(&result, "    %s\n", rect.to_string().c_str());
274     }
275 
276     return result;
277 }
278 
to_string() const279 std::string LayerProtoParser::Layer::to_string() const {
280     std::string result;
281     StringAppendF(&result, "+ %s (%s) uid=%d\n", type.c_str(), name.c_str(), ownerUid);
282     result.append(transparentRegion.to_string("TransparentRegion").c_str());
283     result.append(visibleRegion.to_string("VisibleRegion").c_str());
284     result.append(damageRegion.to_string("SurfaceDamageRegion").c_str());
285 
286     StringAppendF(&result, "      layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), ", layerStack,
287                   z, static_cast<double>(position.x), static_cast<double>(position.y), size.x,
288                   size.y);
289 
290     StringAppendF(&result, "crop=%s, ", crop.to_string().c_str());
291     StringAppendF(&result, "cornerRadius=%f, ", cornerRadius);
292     StringAppendF(&result, "isProtected=%1d, ", isProtected);
293     StringAppendF(&result, "isTrustedOverlay=%1d, ", isTrustedOverlay);
294     StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
295     StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
296     StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
297     StringAppendF(&result, "backgroundBlurRadius=%1d, ", backgroundBlurRadius);
298     StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
299                   static_cast<double>(color.r), static_cast<double>(color.g),
300                   static_cast<double>(color.b), static_cast<double>(color.a), flags);
301     StringAppendF(&result, "tr=%s", transform.to_string().c_str());
302     result.append("\n");
303     StringAppendF(&result, "      parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
304     StringAppendF(&result, "      zOrderRelativeOf=%s\n",
305                   zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
306     StringAppendF(&result, "      activeBuffer=%s,", activeBuffer.to_string().c_str());
307     StringAppendF(&result, " tr=%s", bufferTransform.to_string().c_str());
308     StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
309     StringAppendF(&result, " metadata={");
310     bool first = true;
311     for (const auto& entry : metadata.mMap) {
312         if (!first) result.append(", ");
313         first = false;
314         result.append(metadata.itemToString(entry.first, ":"));
315     }
316     result.append("},");
317     StringAppendF(&result, " cornerRadiusCrop=%s, ", cornerRadiusCrop.to_string().c_str());
318     StringAppendF(&result, " shadowRadius=%.3f, ", shadowRadius);
319     return result;
320 }
321 
322 } // namespace surfaceflinger
323 } // namespace android
324