1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <mutex>
17 #include <chrono>
18 #include <cinttypes>
19 #include <algorithm>
20 #include <condition_variable>
21 #include <benchmark/benchmark.h>
22 #include "gtest/gtest.h"
23 #include "hdf_base.h"
24 #include "hdf_log.h"
25 #include "v1_2/display_composer_type.h"
26 #include "v1_2/display_buffer_type.h"
27 #include "v1_2/include/idisplay_buffer.h"
28 using namespace OHOS::HDI::Display::Buffer;
29 using namespace OHOS::HDI::Display::Buffer::V1_2;
30 using namespace OHOS::HDI::Display::Composer::V1_0;
31 using namespace testing::ext;
32 using OHOS::HDI::Display::Buffer::V1_0::AllocInfo;
33
34 const uint32_t ALLOC_SIZE_1080 = 1080; // alloc size 1080
35 const uint32_t ALLOC_SIZE_1920 = 1920; // alloc size 1920
36
37 static std::shared_ptr<V1_2::IDisplayBuffer> g_gralloc = nullptr;
38 static BufferHandle* g_bufferHandle = nullptr;
39 static AllocInfo g_allocInfo = {
40 .width = ALLOC_SIZE_1920,
41 .height = ALLOC_SIZE_1080,
42 .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
43 .format = PIXEL_FMT_RGBX_8888
44 };
45
46 namespace {
47 class DisplayBenchmarkTest : public benchmark::Fixture {
48 public:
49 void SetUp(const ::benchmark::State &state);
50 void TearDown(const ::benchmark::State &state);
51 };
52
SetUp(const::benchmark::State & state)53 void DisplayBenchmarkTest::SetUp(const ::benchmark::State &state)
54 {
55 g_gralloc.reset(V1_2::IDisplayBuffer::Get());
56 if (g_gralloc == nullptr) {
57 HDF_LOGE("IDisplayBuffer get failed");
58 ASSERT_TRUE(0);
59 }
60
61 int32_t ret = g_gralloc->AllocMem(g_allocInfo, g_bufferHandle);
62 if (ret != DISPLAY_SUCCESS || g_bufferHandle == nullptr) {
63 HDF_LOGE("AllocMem failed");
64 ASSERT_TRUE(ret == DISPLAY_SUCCESS && g_bufferHandle != nullptr);
65 }
66
67 ret = g_gralloc->RegisterBuffer(*g_bufferHandle);
68 ASSERT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
69 }
70
TearDown(const::benchmark::State & state)71 void DisplayBenchmarkTest::TearDown(const ::benchmark::State &state)
72 {
73 if (g_bufferHandle != nullptr) {
74 g_gralloc->FreeMem(*g_bufferHandle);
75 }
76 }
77
78 /**
79 * @tc.name: SetMetadataTest
80 * @tc.desc: Benchmarktest for interface SetMetadata.
81 */
BENCHMARK_F(DisplayBenchmarkTest,SetMetadataTest)82 BENCHMARK_F(DisplayBenchmarkTest, SetMetadataTest)(benchmark::State &state)
83 {
84 int32_t ret;
85 int32_t key = 0;
86 for (auto _ : state) {
87 std::vector<uint8_t> values(2880, 0);
88 ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
89 EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
90 }
91 }
92
93 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, SetMetadataTest)->
94 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
95
96 /**
97 * @tc.name: GetMetadataTest
98 * @tc.desc: Benchmarktest for interface GetMetadata.
99 */
BENCHMARK_F(DisplayBenchmarkTest,GetMetadataTest)100 BENCHMARK_F(DisplayBenchmarkTest, GetMetadataTest)(benchmark::State &state)
101 {
102 int32_t ret;
103 int32_t key = 0;
104 for (auto _ : state) {
105 std::vector<uint8_t> values(2880, 0);
106 ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
107 EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
108 std::vector<uint8_t> rets;
109 ret = g_gralloc->GetMetadata(*g_bufferHandle, key, rets);
110 EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
111 }
112 }
113
114 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, GetMetadataTest)->
115 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
116
117 /**
118 * @tc.name: ListMetadataKeysTest
119 * @tc.desc: Benchmarktest for interface ListMetadataKeys.
120 */
BENCHMARK_F(DisplayBenchmarkTest,ListMetadataKeysTest)121 BENCHMARK_F(DisplayBenchmarkTest, ListMetadataKeysTest)(benchmark::State &state)
122 {
123 int32_t ret;
124 int32_t key = 0;
125 for (auto _ : state) {
126 std::vector<uint32_t> keys;
127 std::vector<uint8_t> values(2880, 0);
128 ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
129 EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
130 ret = g_gralloc->ListMetadataKeys(*g_bufferHandle, keys);
131 EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
132 }
133 }
134
135 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, ListMetadataKeysTest)->
136 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
137
138 /**
139 * @tc.name: EraseMetadataKeyTest
140 * @tc.desc: Benchmarktest for interface EraseMetadataKey.
141 */
BENCHMARK_F(DisplayBenchmarkTest,EraseMetadataKeyTest)142 BENCHMARK_F(DisplayBenchmarkTest, EraseMetadataKeyTest)(benchmark::State &state)
143 {
144 int32_t ret;
145 int32_t key = 0;
146 for (auto _ : state) {
147 std::vector<uint8_t> values(2880, 0);
148 ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
149 EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
150 ret = g_gralloc->EraseMetadataKey(*g_bufferHandle, key);
151 EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
152 }
153 }
154
155 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, EraseMetadataKeyTest)->
156 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
157
158 /**
159 * @tc.name: SetAllocFreeTest
160 * @tc.desc: Benchmarktest for interface AllocMem and FreeMem.
161 */
BENCHMARK_F(DisplayBenchmarkTest,SetAllocFreeTest)162 BENCHMARK_F(DisplayBenchmarkTest, SetAllocFreeTest)(benchmark::State &state)
163 {
164 int32_t ret;
165 BufferHandle* bufferHandle = nullptr;
166 for (auto _ : state) {
167 ret = g_gralloc->AllocMem(g_allocInfo, bufferHandle);
168 EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
169 g_gralloc->FreeMem(*bufferHandle);
170 }
171 }
172
173 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, SetAllocFreeTest)->
174 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
175
176 /**
177 * @tc.name: MmapTest
178 * @tc.desc: Benchmarktest for interface Mmap.
179 */
BENCHMARK_F(DisplayBenchmarkTest,MmapTest)180 BENCHMARK_F(DisplayBenchmarkTest, MmapTest)(benchmark::State &state)
181 {
182 for (auto _ : state) {
183 g_gralloc->Mmap(*g_bufferHandle);
184 }
185 }
186
187 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, MmapTest)->
188 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
189
190 /**
191 * @tc.name: UnmapTest
192 * @tc.desc: Benchmarktest for interface Unmap.
193 */
BENCHMARK_F(DisplayBenchmarkTest,UnmapTest)194 BENCHMARK_F(DisplayBenchmarkTest, UnmapTest)(benchmark::State &state)
195 {
196 for (auto _ : state) {
197 g_gralloc->Unmap(*g_bufferHandle);
198 }
199 }
200
201 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, UnmapTest)->
202 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
203
204 /**
205 * @tc.name: FlushCacheTest
206 * @tc.desc: Benchmarktest for interface FlushCache.
207 */
BENCHMARK_F(DisplayBenchmarkTest,FlushCacheTest)208 BENCHMARK_F(DisplayBenchmarkTest, FlushCacheTest)(benchmark::State &state)
209 {
210 for (auto _ : state) {
211 g_gralloc->FlushCache(*g_bufferHandle);
212 }
213 }
214
215 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, FlushCacheTest)->
216 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
217
218 /**
219 * @tc.name: InvalidateCacheTest
220 * @tc.desc: Benchmarktest for interface InvalidateCache.
221 */
BENCHMARK_F(DisplayBenchmarkTest,InvalidateCacheTest)222 BENCHMARK_F(DisplayBenchmarkTest, InvalidateCacheTest)(benchmark::State &state)
223 {
224 for (auto _ : state) {
225 g_gralloc->InvalidateCache(*g_bufferHandle);
226 }
227 }
228
229 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, InvalidateCacheTest)->
230 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
231
232 /**
233 * @tc.name: GetImageLayoutTest
234 * @tc.desc: Benchmarktest for interface GetImageLayout.
235 */
BENCHMARK_F(DisplayBenchmarkTest,GetImageLayoutTest)236 BENCHMARK_F(DisplayBenchmarkTest, GetImageLayoutTest)(benchmark::State &state)
237 {
238 V1_2::ImageLayout layout = {0};
239 for (auto _ : state) {
240 g_gralloc->GetImageLayout(*g_bufferHandle, layout);
241 }
242 }
243
244 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, GetImageLayoutTest)->
245 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
246
247 } // namespace
248 BENCHMARK_MAIN();
249
250