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_0/include/idisplay_composer_interface.h"
24 #include "v1_0/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_0;
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 std::shared_ptr<IDisplayComposerInterface> g_composerDevice {};
63 static std::shared_ptr<HdiTestLayer> g_testFreshLayer;
64 std::shared_ptr<IDisplayBuffer> g_gralloc = nullptr;
65 std::vector<uint32_t> g_displayIds;
66 const int SLEEP_CONT_100 = 100;
67 const int SLEEP_CONT_1000 = 1000;
68
69 class DisplayBenchmarkTest : public benchmark::Fixture {
70 public:
71 void SetUp(const ::benchmark::State &state);
72 void TearDown(const ::benchmark::State &state);
73 };
74
TestVBlankCallback(unsigned int sequence,uint64_t ns,void * data)75 static void TestVBlankCallback(unsigned int sequence, uint64_t ns, void* data)
76 {
77 static uint64_t lastns;
78 DISPLAY_TEST_LOGE("seq %{public}d ns %" PRId64 " duration %" PRId64 " ns", sequence, ns, (ns - lastns));
79 lastns = ns;
80 VblankCtr::GetInstance().NotifyVblank(sequence, ns, data);
81 }
82
NotifyVblank(unsigned int sequence,uint64_t ns,const void * data)83 void VblankCtr::NotifyVblank(unsigned int sequence, uint64_t ns, const void* data)
84 {
85 DISPLAY_TEST_LOGE();
86 if (data != nullptr) {
87 DISPLAY_TEST_LOGE("sequence = %{public}u, ns = %" PRIu64 "", sequence, ns);
88 }
89 std::unique_lock<std::mutex> lg(vblankMutex_);
90 hasVblank_ = true;
91 vblankCondition_.notify_one();
92 DISPLAY_TEST_LOGE();
93 }
94
~VblankCtr()95 VblankCtr::~VblankCtr() {}
96
WaitVblank(uint32_t ms)97 int32_t VblankCtr::WaitVblank(uint32_t ms)
98 {
99 bool ret = false;
100 DISPLAY_TEST_LOGE();
101 std::unique_lock<std::mutex> lck(vblankMutex_);
102 hasVblank_ = false; // must wait next vblank
103 ret = vblankCondition_.wait_for(lck, std::chrono::milliseconds(ms), [=] { return hasVblank_; });
104 DISPLAY_TEST_LOGE();
105 if (!ret) {
106 return DISPLAY_FAILURE;
107 }
108 return DISPLAY_SUCCESS;
109 }
110
SetUp(const::benchmark::State & state)111 void DisplayBenchmarkTest::SetUp(const ::benchmark::State &state)
112 {
113 }
114
TearDown(const::benchmark::State & state)115 void DisplayBenchmarkTest::TearDown(const ::benchmark::State &state)
116 {
117 auto display = HdiTestDevice::GetInstance().GetFirstDisplay();
118 int32_t ret = display->SetDisplayVsyncEnabled(false);
119 if (ret != DISPLAY_SUCCESS) {
120 DISPLAY_TEST_LOGE("vsync disable failed");
121 }
122 VblankCtr::GetInstance().WaitVblank(SLEEP_CONT_100); // wait for last vsync 100ms.
123 HdiTestDevice::GetInstance().Clear();
124 }
125
BENCHMARK_F(DisplayBenchmarkTest,RegDisplayVBlankCallbackTest)126 BENCHMARK_F(DisplayBenchmarkTest, RegDisplayVBlankCallbackTest)(benchmark::State &state)
127 {
128 int ret;
129 DISPLAY_TEST_LOGE();
130 std::shared_ptr<HdiTestDisplay> display = HdiTestDevice::GetInstance().GetFirstDisplay();
131 for (auto _ : state) {
132 ret = display->RegDisplayVBlankCallback(TestVBlankCallback, nullptr);
133 }
134 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "RegDisplayVBlankCallback failed";
135 }
136
137 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, RegDisplayVBlankCallbackTest)->
138 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
139
140
BENCHMARK_F(DisplayBenchmarkTest,SetDisplayVsyncEnabledTest)141 BENCHMARK_F(DisplayBenchmarkTest, SetDisplayVsyncEnabledTest)(benchmark::State &state)
142 {
143 int ret;
144 DISPLAY_TEST_LOGE();
145 std::shared_ptr<HdiTestDisplay> display = HdiTestDevice::GetInstance().GetFirstDisplay();
146 ret = display->RegDisplayVBlankCallback(TestVBlankCallback, nullptr);
147 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "RegDisplayVBlankCallback failed";
148 for (auto _ : state) {
149 ret = display->SetDisplayVsyncEnabled(true);
150 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "SetDisplayVsyncEnabled failed";
151 ret = VblankCtr::GetInstance().WaitVblank(SLEEP_CONT_1000); // 1000ms
152 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "WaitVblank timeout";
153 ret = display->SetDisplayVsyncEnabled(false);
154 ASSERT_TRUE(ret == DISPLAY_SUCCESS) << "SetDisplayVsyncEnabled failed";
155 }
156 usleep(SLEEP_CONT_100 * SLEEP_CONT_1000); // wait for 100ms avoid the last vsync.
157 ret = VblankCtr::GetInstance().WaitVblank(SLEEP_CONT_1000); // 1000ms
158 ASSERT_TRUE(ret != DISPLAY_SUCCESS) << "vblank do not disable";
159 }
160
161 BENCHMARK_REGISTER_F(DisplayBenchmarkTest, SetDisplayVsyncEnabledTest)->
162 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
163
164 } // namespace