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 "v1_1/include/idisplay_composer_interface.h"
24 #include "v1_1/display_composer_type.h"
25 #include "v1_0/display_buffer_type.h"
26 #include "display_test.h"
27 #include "display_test_utils.h"
28 #include "hdi_composition_check.h"
29 #include "hdi_test_device.h"
30 #include "hdi_test_device_common.h"
31 #include "hdi_test_display.h"
32 #include "hdi_test_render_utils.h"
33
34 using namespace OHOS::HDI::Display::Buffer::V1_0;
35 using namespace OHOS::HDI::Display::Composer::V1_1;
36 using namespace OHOS::HDI::Display::TEST;
37 using namespace testing::ext;
38 using namespace std;
39
40 namespace {
41 class VblankCtr {
42 public:
GetInstance()43 static VblankCtr& GetInstance()
44 {
45 static VblankCtr instance;
46 return instance;
47 }
48 void NotifyVblank(unsigned int sequence, uint64_t ns, const void* data);
49 int32_t WaitVblank(uint32_t ms);
50
51 protected:
52 void TearDown();
53
54 private:
55 std::mutex vblankMutex_;
56 std::condition_variable vblankCondition_;
VblankCtr()57 VblankCtr() {}
58 ~VblankCtr();
59 bool hasVblank_ = false;
60 };
61
62 static sptr<Composer::V1_1::IDisplayComposerInterface> g_composerDevice = nullptr;
63 static std::shared_ptr<IDisplayBuffer> g_gralloc = nullptr;
64 static std::vector<uint32_t> g_displayIds;
65 const int SLEEP_CONT_100 = 100;
66 const int SLEEP_CONT_1000 = 1000;
67
68 class DisplayBenchmarkTest : public benchmark::Fixture {
69 public:
70 void SetUp(const ::benchmark::State &state);
71 void TearDown(const ::benchmark::State &state);
72 };
73
TestVBlankCallback(unsigned int sequence,uint64_t ns,void * data)74 static void TestVBlankCallback(unsigned int sequence, uint64_t ns, void* data)
75 {
76 static uint64_t lastns;
77 DISPLAY_TEST_LOGE("seq %{public}d ns %" PRId64 " duration %" PRId64 " ns", sequence, ns, (ns - lastns));
78 lastns = ns;
79 VblankCtr::GetInstance().NotifyVblank(sequence, ns, data);
80 }
81
NotifyVblank(unsigned int sequence,uint64_t ns,const void * data)82 void VblankCtr::NotifyVblank(unsigned int sequence, uint64_t ns, const void* data)
83 {
84 DISPLAY_TEST_LOGE();
85 if (data != nullptr) {
86 DISPLAY_TEST_LOGE("sequence = %{public}u, ns = %" PRIu64 "", sequence, ns);
87 }
88 std::unique_lock<std::mutex> lg(vblankMutex_);
89 hasVblank_ = true;
90 vblankCondition_.notify_one();
91 DISPLAY_TEST_LOGE();
92 }
93
~VblankCtr()94 VblankCtr::~VblankCtr() {}
95
WaitVblank(uint32_t ms)96 int32_t VblankCtr::WaitVblank(uint32_t ms)
97 {
98 bool ret = false;
99 DISPLAY_TEST_LOGE();
100 std::unique_lock<std::mutex> lck(vblankMutex_);
101 hasVblank_ = false; // must wait next vblank
102 ret = vblankCondition_.wait_for(lck, std::chrono::milliseconds(ms), [=] { return hasVblank_; });
103 DISPLAY_TEST_LOGE();
104 if (!ret) {
105 return DISPLAY_FAILURE;
106 }
107 return DISPLAY_SUCCESS;
108 }
109
SetUp(const::benchmark::State & state)110 void DisplayBenchmarkTest::SetUp(const ::benchmark::State &state)
111 {
112 }
113
TearDown(const::benchmark::State & state)114 void DisplayBenchmarkTest::TearDown(const ::benchmark::State &state)
115 {
116 auto display = HdiTestDevice::GetInstance().GetFirstDisplay();
117 int32_t ret = display->SetDisplayVsyncEnabled(false);
118 if (ret != DISPLAY_SUCCESS) {
119 DISPLAY_TEST_LOGE("vsync disable failed");
120 }
121 VblankCtr::GetInstance().WaitVblank(SLEEP_CONT_100); // wait for last vsync 100ms.
122 HdiTestDevice::GetInstance().Clear();
123 }
124
BENCHMARK_F(DisplayBenchmarkTest,SUB_Driver_Display_Performace_3700)125 BENCHMARK_F(DisplayBenchmarkTest, SUB_Driver_Display_Performace_3700)(benchmark::State &state)
126 {
127 int ret;
128 DISPLAY_TEST_LOGE();
129 std::shared_ptr<HdiTestDisplay> display = HdiTestDevice::GetInstance().GetFirstDisplay();
130 for (auto _ : state) {
131 ret = display->RegDisplayVBlankCallback(TestVBlankCallback, nullptr);
132 }
133 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "RegDisplayVBlankCallback failed";
134 }
135
136 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, SUB_Driver_Display_Performace_3700)->
137 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
138
139
BENCHMARK_F(DisplayBenchmarkTest,SUB_Driver_Display_Performace_3800)140 BENCHMARK_F(DisplayBenchmarkTest, SUB_Driver_Display_Performace_3800)(benchmark::State &state)
141 {
142 int ret;
143 DISPLAY_TEST_LOGE();
144 std::shared_ptr<HdiTestDisplay> display = HdiTestDevice::GetInstance().GetFirstDisplay();
145 ret = display->RegDisplayVBlankCallback(TestVBlankCallback, nullptr);
146 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "RegDisplayVBlankCallback failed";
147 for (auto _ : state) {
148 ret = display->SetDisplayVsyncEnabled(true);
149 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "SetDisplayVsyncEnabled failed";
150 ret = VblankCtr::GetInstance().WaitVblank(SLEEP_CONT_1000); // 1000ms
151 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "WaitVblank timeout";
152 ret = display->SetDisplayVsyncEnabled(false);
153 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "SetDisplayVsyncEnabled failed";
154 }
155 usleep(SLEEP_CONT_100 * SLEEP_CONT_1000); // wait for 100ms avoid the last vsync.
156 ret = VblankCtr::GetInstance().WaitVblank(SLEEP_CONT_1000); // 1000ms
157 ASSERT_TRUE(ret != DISPLAY_SUCCESS) << "vblank do not disable";
158 }
159
160 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, SUB_Driver_Display_Performace_3800)->
161 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
162
163 } // namespace