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) ? 1 : -1;
35     }
36 
37     return lhs->id < rhs->id;
38 }
39 
sortLayerUniquePtrs(const std::unique_ptr<LayerProtoParser::Layer> & lhs,const std::unique_ptr<LayerProtoParser::Layer> & rhs)40 bool 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 
generateLayerGlobalInfo(const LayersProto & layersProto)45 const LayerProtoParser::LayerGlobal LayerProtoParser::generateLayerGlobalInfo(
46         const LayersProto& layersProto) {
47     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;
53 }
54 
generateLayerTree(const LayersProto & layersProto)55 std::vector<std::unique_ptr<LayerProtoParser::Layer>> LayerProtoParser::generateLayerTree(
56         const LayersProto& layersProto) {
57     std::unordered_map<int32_t, LayerProtoParser::Layer*> layerMap = generateMap(layersProto);
58     std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers;
59 
60     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     }
67 
68     std::sort(layers.begin(), layers.end(), sortLayerUniquePtrs);
69     return layers;
70 }
71 
generateMap(const LayersProto & layersProto)72 std::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 
generateLayer(const LayerProto & layerProto)89 LayerProtoParser::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());
104     layer->finalCrop = generateRect(layerProto.final_crop());
105     layer->isOpaque = layerProto.is_opaque();
106     layer->invalidate = layerProto.invalidate();
107     layer->dataspace = layerProto.dataspace();
108     layer->pixelFormat = layerProto.pixel_format();
109     layer->color = {layerProto.color().r(), layerProto.color().g(), layerProto.color().b(),
110                     layerProto.color().a()};
111     layer->requestedColor = {layerProto.requested_color().r(), layerProto.requested_color().g(),
112                              layerProto.requested_color().b(), layerProto.requested_color().a()};
113     layer->flags = layerProto.flags();
114     layer->transform = generateTransform(layerProto.transform());
115     layer->requestedTransform = generateTransform(layerProto.requested_transform());
116     layer->activeBuffer = generateActiveBuffer(layerProto.active_buffer());
117     layer->queuedFrames = layerProto.queued_frames();
118     layer->refreshPending = layerProto.refresh_pending();
119     layer->hwcFrame = generateRect(layerProto.hwc_frame());
120     layer->hwcCrop = generateFloatRect(layerProto.hwc_crop());
121     layer->hwcTransform = layerProto.hwc_transform();
122     layer->windowType = layerProto.window_type();
123     layer->appId = layerProto.app_id();
124     layer->hwcCompositionType = layerProto.hwc_composition_type();
125     layer->isProtected = layerProto.is_protected();
126 
127     return layer;
128 }
129 
generateRegion(const RegionProto & regionProto)130 LayerProtoParser::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 
generateRect(const RectProto & rectProto)141 LayerProtoParser::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 
generateFloatRect(const FloatRectProto & rectProto)151 LayerProtoParser::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 
generateTransform(const TransformProto & transformProto)161 LayerProtoParser::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 
generateActiveBuffer(const ActiveBufferProto & activeBufferProto)172 LayerProtoParser::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 
updateChildrenAndRelative(const LayerProto & layerProto,std::unordered_map<int32_t,Layer * > & layerMap)183 void 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) {
189             // 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)]));
192         }
193     }
194 
195     for (int i = 0; i < layerProto.relatives_size(); i++) {
196         if (layerMap.count(layerProto.relatives(i)) > 0) {
197             currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
198         }
199     }
200 
201     if (layerProto.has_parent()) {
202         if (layerMap.count(layerProto.parent()) > 0) {
203             currLayer->parent = layerMap[layerProto.parent()];
204         }
205     }
206 
207     if (layerProto.has_z_order_relative_of()) {
208         if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
209             currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
210         }
211     }
212 }
213 
layersToString(std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers)214 std::string LayerProtoParser::layersToString(
215         std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers) {
216     std::string result;
217     for (std::unique_ptr<LayerProtoParser::Layer>& layer : layers) {
218         if (layer->zOrderRelativeOf != nullptr) {
219             continue;
220         }
221         result.append(layerToString(layer.get()).c_str());
222     }
223 
224     return result;
225 }
226 
layerToString(LayerProtoParser::Layer * layer)227 std::string LayerProtoParser::layerToString(LayerProtoParser::Layer* layer) {
228     std::string result;
229 
230     std::vector<Layer*> traverse(layer->relatives);
231     for (std::unique_ptr<LayerProtoParser::Layer>& child : layer->children) {
232         if (child->zOrderRelativeOf != nullptr) {
233             continue;
234         }
235 
236         traverse.push_back(child.get());
237     }
238 
239     std::sort(traverse.begin(), traverse.end(), sortLayers);
240 
241     size_t i = 0;
242     for (; i < traverse.size(); i++) {
243         auto& relative = traverse[i];
244         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++) {
252         auto& relative = traverse[i];
253         result.append(layerToString(relative).c_str());
254     }
255 
256     return result;
257 }
258 
to_string() const259 std::string LayerProtoParser::ActiveBuffer::to_string() const {
260     return StringPrintf("[%4ux%4u:%4u,%s]", width, height, stride,
261                         decodePixelFormat(format).c_str());
262 }
263 
to_string() const264 std::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 
to_string() const270 std::string LayerProtoParser::Rect::to_string() const {
271     return StringPrintf("[%3d, %3d, %3d, %3d]", left, top, right, bottom);
272 }
273 
to_string() const274 std::string LayerProtoParser::FloatRect::to_string() const {
275     return StringPrintf("[%.2f, %.2f, %.2f, %.2f]", left, top, right, bottom);
276 }
277 
to_string(const char * what) const278 std::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 
to_string() const290 std::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 
301     StringAppendF(&result, "crop=%s, finalCrop=%s, ", crop.to_string().c_str(),
302                   finalCrop.to_string().c_str());
303     StringAppendF(&result, "isOpaque=%1d, invalidate=%1d, ", isOpaque, invalidate);
304     StringAppendF(&result, "dataspace=%s, ", dataspace.c_str());
305     StringAppendF(&result, "defaultPixelFormat=%s, ", pixelFormat.c_str());
306     StringAppendF(&result, "color=(%.3f,%.3f,%.3f,%.3f), flags=0x%08x, ",
307                   static_cast<double>(color.r), static_cast<double>(color.g),
308                   static_cast<double>(color.b), static_cast<double>(color.a), flags);
309     StringAppendF(&result, "tr=%s", transform.to_string().c_str());
310     result.append("\n");
311     StringAppendF(&result, "      parent=%s\n", parent == nullptr ? "none" : parent->name.c_str());
312     StringAppendF(&result, "      zOrderRelativeOf=%s\n",
313                   zOrderRelativeOf == nullptr ? "none" : zOrderRelativeOf->name.c_str());
314     StringAppendF(&result, "      activeBuffer=%s,", activeBuffer.to_string().c_str());
315     StringAppendF(&result, " queued-frames=%d, mRefreshPending=%d,", queuedFrames, refreshPending);
316     StringAppendF(&result, " windowType=%d, appId=%d", windowType, appId);
317 
318     return result;
319 }
320 
321 } // namespace surfaceflinger
322 } // namespace android
323