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