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 
17 #include <gtest/gtest.h>
18 
19 #include <gui/SurfaceComposerClient.h>
20 
21 #include <utils/String8.h>
22 
23 #include <thread>
24 #include <functional>
25 #include <layerproto/LayerProtoParser.h>
26 
27 namespace android {
28 
TEST(SurfaceFlingerStress,create_and_destroy)29 TEST(SurfaceFlingerStress, create_and_destroy) {
30     auto do_stress = []() {
31         sp<SurfaceComposerClient> client = new SurfaceComposerClient;
32         ASSERT_EQ(NO_ERROR, client->initCheck());
33         for (int j = 0; j < 1000; j++) {
34             auto surf = client->createSurface(String8("t"), 100, 100,
35                     PIXEL_FORMAT_RGBA_8888, 0);
36             ASSERT_TRUE(surf != nullptr);
37             surf.clear();
38         }
39     };
40 
41     std::vector<std::thread> threads;
42     for (int i = 0; i < 10; i++) {
43         threads.push_back(std::thread(do_stress));
44     }
45     for (auto& thread : threads) {
46         thread.join();
47     }
48 }
49 
generateLayerProto()50 surfaceflinger::LayersProto generateLayerProto() {
51     surfaceflinger::LayersProto layersProto;
52     std::array<surfaceflinger::LayerProto*, 10> layers = {};
53     for (size_t i = 0; i < layers.size(); ++i) {
54         layers[i] = layersProto.add_layers();
55         layers[i]->set_id(i);
56     }
57 
58     layers[0]->add_children(1);
59     layers[1]->set_parent(0);
60     layers[0]->add_children(2);
61     layers[2]->set_parent(0);
62     layers[0]->add_children(3);
63     layers[3]->set_parent(0);
64     layers[2]->add_children(4);
65     layers[4]->set_parent(2);
66     layers[3]->add_children(5);
67     layers[5]->set_parent(3);
68     layers[5]->add_children(6);
69     layers[6]->set_parent(5);
70     layers[5]->add_children(7);
71     layers[7]->set_parent(5);
72     layers[6]->add_children(8);
73     layers[8]->set_parent(6);
74 
75     layers[4]->set_z_order_relative_of(3);
76     layers[3]->add_relatives(4);
77     layers[8]->set_z_order_relative_of(9);
78     layers[9]->add_relatives(8);
79     layers[3]->set_z_order_relative_of(1);
80     layers[1]->add_relatives(3);
81 
82 /* ----------------------------
83  *       - 0 -      - 9 -
84  *      /  |  \
85  *     1   2   3(1)
86  *         |    |
87  *         4(3) 5
88  *             / \
89  *            6   7
90  *            |
91  *            8(9)
92  * -------------------------- */
93 
94     return layersProto;
95 }
96 
TEST(LayerProtoStress,mem_info)97 TEST(LayerProtoStress, mem_info) {
98     std::string cmd = "dumpsys meminfo ";
99     cmd += std::to_string(getpid());
100     system(cmd.c_str());
101     for (int i = 0; i < 100000; i++) {
102         surfaceflinger::LayersProto layersProto = generateLayerProto();
103         auto layerTree = surfaceflinger::LayerProtoParser::generateLayerTree(layersProto);
104         surfaceflinger::LayerProtoParser::layerTreeToString(layerTree);
105     }
106     system(cmd.c_str());
107 }
108 
109 }
110