1 /*
2  * Copyright (c) 2023 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_0/display_composer_type.h"
26 #include "v1_0/display_buffer_type.h"
27 #include "v1_1/include/idisplay_buffer.h"
28 using namespace OHOS::HDI::Display::Buffer::V1_1;
29 using namespace OHOS::HDI::Display::Composer::V1_0;
30 using namespace testing::ext;
31 using OHOS::HDI::Display::Buffer::V1_0::AllocInfo;
32 
33 const uint32_t ALLOC_SIZE_1080 = 1080; // alloc size 1080
34 const uint32_t ALLOC_SIZE_1920 = 1920; // alloc size 1920
35 
36 static std::shared_ptr<IDisplayBuffer> g_gralloc = nullptr;
37 static BufferHandle* g_bufferHandle = nullptr;
38 static AllocInfo g_allocInfo = {
39     .width = ALLOC_SIZE_1920,
40     .height = ALLOC_SIZE_1080,
41     .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
42     .format = PIXEL_FMT_RGBX_8888
43 };
44 
45 namespace {
46 class DisplayBenchmarkTest : public benchmark::Fixture {
47 public:
48     void SetUp(const ::benchmark::State &state);
49     void TearDown(const ::benchmark::State &state);
50 };
51 
SetUp(const::benchmark::State & state)52 void DisplayBenchmarkTest::SetUp(const ::benchmark::State &state)
53 {
54     g_gralloc.reset(IDisplayBuffer::Get());
55     if (g_gralloc == nullptr) {
56         HDF_LOGE("IDisplayBuffer get failed");
57         ASSERT_TRUE(0);
58     }
59 
60     int32_t ret = g_gralloc->AllocMem(g_allocInfo, g_bufferHandle);
61     if (ret != DISPLAY_SUCCESS || g_bufferHandle == nullptr) {
62         HDF_LOGE("AllocMem failed");
63         ASSERT_TRUE(ret == DISPLAY_SUCCESS && g_bufferHandle != nullptr);
64     }
65 
66     ret = g_gralloc->RegisterBuffer(*g_bufferHandle);
67     ASSERT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
68 }
69 
TearDown(const::benchmark::State & state)70 void DisplayBenchmarkTest::TearDown(const ::benchmark::State &state)
71 {
72     if (g_bufferHandle != nullptr) {
73         g_gralloc->FreeMem(*g_bufferHandle);
74     }
75 }
76 
77 /**
78   * @tc.name: SetMetadataTest
79   * @tc.desc: Benchmarktest for interface SetMetadata.
80   */
BENCHMARK_F(DisplayBenchmarkTest,SetMetadataTest)81 BENCHMARK_F(DisplayBenchmarkTest, SetMetadataTest)(benchmark::State &state)
82 {
83     int32_t ret;
84     int32_t key = 0;
85     for (auto _ : state) {
86         std::vector<uint8_t> values(2880, 0);
87         ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
88         EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
89     }
90 }
91 
92 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, SetMetadataTest)->
93     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
94 
95 /**
96   * @tc.name: GetMetadataTest
97   * @tc.desc: Benchmarktest for interface GetMetadata.
98   */
BENCHMARK_F(DisplayBenchmarkTest,GetMetadataTest)99 BENCHMARK_F(DisplayBenchmarkTest, GetMetadataTest)(benchmark::State &state)
100 {
101     int32_t ret;
102     int32_t key = 0;
103     for (auto _ : state) {
104         std::vector<uint8_t> values(2880, 0);
105         ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
106         EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
107         std::vector<uint8_t> rets;
108         ret = g_gralloc->GetMetadata(*g_bufferHandle, key, rets);
109         EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
110     }
111 }
112 
113 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, GetMetadataTest)->
114     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
115 
116 /**
117   * @tc.name: ListMetadataKeysTest
118   * @tc.desc: Benchmarktest for interface ListMetadataKeys.
119   */
BENCHMARK_F(DisplayBenchmarkTest,ListMetadataKeysTest)120 BENCHMARK_F(DisplayBenchmarkTest, ListMetadataKeysTest)(benchmark::State &state)
121 {
122     int32_t ret;
123     int32_t key = 0;
124     for (auto _ : state) {
125         std::vector<uint32_t> keys;
126         std::vector<uint8_t> values(2880, 0);
127         ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
128         EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
129         ret = g_gralloc->ListMetadataKeys(*g_bufferHandle, keys);
130         EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
131     }
132 }
133 
134 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, ListMetadataKeysTest)->
135     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
136 
137 /**
138   * @tc.name: EraseMetadataKeyTest
139   * @tc.desc: Benchmarktest for interface EraseMetadataKey.
140   */
BENCHMARK_F(DisplayBenchmarkTest,EraseMetadataKeyTest)141 BENCHMARK_F(DisplayBenchmarkTest, EraseMetadataKeyTest)(benchmark::State &state)
142 {
143     int32_t ret;
144     int32_t key = 0;
145     for (auto _ : state) {
146         std::vector<uint8_t> values(2880, 0);
147         ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
148         EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
149         ret = g_gralloc->EraseMetadataKey(*g_bufferHandle, key);
150         EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
151     }
152 }
153 
154 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, EraseMetadataKeyTest)->
155     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
156 
157 } // namespace
158 BENCHMARK_MAIN();
159 
160