1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <mutex>
17094332d3Sopenharmony_ci#include <chrono>
18094332d3Sopenharmony_ci#include <cinttypes>
19094332d3Sopenharmony_ci#include <algorithm>
20094332d3Sopenharmony_ci#include <condition_variable>
21094332d3Sopenharmony_ci#include <benchmark/benchmark.h>
22094332d3Sopenharmony_ci#include "gtest/gtest.h"
23094332d3Sopenharmony_ci#include "hdf_base.h"
24094332d3Sopenharmony_ci#include "hdf_log.h"
25094332d3Sopenharmony_ci#include "v1_0/display_composer_type.h"
26094332d3Sopenharmony_ci#include "v1_0/display_buffer_type.h"
27094332d3Sopenharmony_ci#include "v1_1/include/idisplay_buffer.h"
28094332d3Sopenharmony_ciusing namespace OHOS::HDI::Display::Buffer::V1_1;
29094332d3Sopenharmony_ciusing namespace OHOS::HDI::Display::Composer::V1_0;
30094332d3Sopenharmony_ciusing namespace testing::ext;
31094332d3Sopenharmony_ciusing OHOS::HDI::Display::Buffer::V1_0::AllocInfo;
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_ciconst uint32_t ALLOC_SIZE_1080 = 1080; // alloc size 1080
34094332d3Sopenharmony_ciconst uint32_t ALLOC_SIZE_1920 = 1920; // alloc size 1920
35094332d3Sopenharmony_ci
36094332d3Sopenharmony_cistatic std::shared_ptr<IDisplayBuffer> g_gralloc = nullptr;
37094332d3Sopenharmony_cistatic BufferHandle* g_bufferHandle = nullptr;
38094332d3Sopenharmony_cistatic AllocInfo g_allocInfo = {
39094332d3Sopenharmony_ci    .width = ALLOC_SIZE_1920,
40094332d3Sopenharmony_ci    .height = ALLOC_SIZE_1080,
41094332d3Sopenharmony_ci    .usage = HBM_USE_MEM_DMA | HBM_USE_CPU_READ | HBM_USE_CPU_WRITE,
42094332d3Sopenharmony_ci    .format = PIXEL_FMT_RGBX_8888
43094332d3Sopenharmony_ci};
44094332d3Sopenharmony_ci
45094332d3Sopenharmony_cinamespace {
46094332d3Sopenharmony_ciclass DisplayBenchmarkTest : public benchmark::Fixture {
47094332d3Sopenharmony_cipublic:
48094332d3Sopenharmony_ci    void SetUp(const ::benchmark::State &state);
49094332d3Sopenharmony_ci    void TearDown(const ::benchmark::State &state);
50094332d3Sopenharmony_ci};
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_civoid DisplayBenchmarkTest::SetUp(const ::benchmark::State &state)
53094332d3Sopenharmony_ci{
54094332d3Sopenharmony_ci    g_gralloc.reset(IDisplayBuffer::Get());
55094332d3Sopenharmony_ci    if (g_gralloc == nullptr) {
56094332d3Sopenharmony_ci        HDF_LOGE("IDisplayBuffer get failed");
57094332d3Sopenharmony_ci        ASSERT_TRUE(0);
58094332d3Sopenharmony_ci    }
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_ci    int32_t ret = g_gralloc->AllocMem(g_allocInfo, g_bufferHandle);
61094332d3Sopenharmony_ci    if (ret != DISPLAY_SUCCESS || g_bufferHandle == nullptr) {
62094332d3Sopenharmony_ci        HDF_LOGE("AllocMem failed");
63094332d3Sopenharmony_ci        ASSERT_TRUE(ret == DISPLAY_SUCCESS && g_bufferHandle != nullptr);
64094332d3Sopenharmony_ci    }
65094332d3Sopenharmony_ci
66094332d3Sopenharmony_ci    ret = g_gralloc->RegisterBuffer(*g_bufferHandle);
67094332d3Sopenharmony_ci    ASSERT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
68094332d3Sopenharmony_ci}
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_civoid DisplayBenchmarkTest::TearDown(const ::benchmark::State &state)
71094332d3Sopenharmony_ci{
72094332d3Sopenharmony_ci    if (g_bufferHandle != nullptr) {
73094332d3Sopenharmony_ci        g_gralloc->FreeMem(*g_bufferHandle);
74094332d3Sopenharmony_ci    }
75094332d3Sopenharmony_ci}
76094332d3Sopenharmony_ci
77094332d3Sopenharmony_ci/**
78094332d3Sopenharmony_ci  * @tc.name: SetMetadataTest
79094332d3Sopenharmony_ci  * @tc.desc: Benchmarktest for interface SetMetadata.
80094332d3Sopenharmony_ci  */
81094332d3Sopenharmony_ciBENCHMARK_F(DisplayBenchmarkTest, SetMetadataTest)(benchmark::State &state)
82094332d3Sopenharmony_ci{
83094332d3Sopenharmony_ci    int32_t ret;
84094332d3Sopenharmony_ci    int32_t key = 0;
85094332d3Sopenharmony_ci    for (auto _ : state) {
86094332d3Sopenharmony_ci        std::vector<uint8_t> values(2880, 0);
87094332d3Sopenharmony_ci        ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
88094332d3Sopenharmony_ci        EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
89094332d3Sopenharmony_ci    }
90094332d3Sopenharmony_ci}
91094332d3Sopenharmony_ci
92094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(DisplayBenchmarkTest, SetMetadataTest)->
93094332d3Sopenharmony_ci    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_ci/**
96094332d3Sopenharmony_ci  * @tc.name: GetMetadataTest
97094332d3Sopenharmony_ci  * @tc.desc: Benchmarktest for interface GetMetadata.
98094332d3Sopenharmony_ci  */
99094332d3Sopenharmony_ciBENCHMARK_F(DisplayBenchmarkTest, GetMetadataTest)(benchmark::State &state)
100094332d3Sopenharmony_ci{
101094332d3Sopenharmony_ci    int32_t ret;
102094332d3Sopenharmony_ci    int32_t key = 0;
103094332d3Sopenharmony_ci    for (auto _ : state) {
104094332d3Sopenharmony_ci        std::vector<uint8_t> values(2880, 0);
105094332d3Sopenharmony_ci        ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
106094332d3Sopenharmony_ci        EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
107094332d3Sopenharmony_ci        std::vector<uint8_t> rets;
108094332d3Sopenharmony_ci        ret = g_gralloc->GetMetadata(*g_bufferHandle, key, rets);
109094332d3Sopenharmony_ci        EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
110094332d3Sopenharmony_ci    }
111094332d3Sopenharmony_ci}
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(DisplayBenchmarkTest, GetMetadataTest)->
114094332d3Sopenharmony_ci    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_ci/**
117094332d3Sopenharmony_ci  * @tc.name: ListMetadataKeysTest
118094332d3Sopenharmony_ci  * @tc.desc: Benchmarktest for interface ListMetadataKeys.
119094332d3Sopenharmony_ci  */
120094332d3Sopenharmony_ciBENCHMARK_F(DisplayBenchmarkTest, ListMetadataKeysTest)(benchmark::State &state)
121094332d3Sopenharmony_ci{
122094332d3Sopenharmony_ci    int32_t ret;
123094332d3Sopenharmony_ci    int32_t key = 0;
124094332d3Sopenharmony_ci    for (auto _ : state) {
125094332d3Sopenharmony_ci        std::vector<uint32_t> keys;
126094332d3Sopenharmony_ci        std::vector<uint8_t> values(2880, 0);
127094332d3Sopenharmony_ci        ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
128094332d3Sopenharmony_ci        EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
129094332d3Sopenharmony_ci        ret = g_gralloc->ListMetadataKeys(*g_bufferHandle, keys);
130094332d3Sopenharmony_ci        EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
131094332d3Sopenharmony_ci    }
132094332d3Sopenharmony_ci}
133094332d3Sopenharmony_ci
134094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(DisplayBenchmarkTest, ListMetadataKeysTest)->
135094332d3Sopenharmony_ci    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
136094332d3Sopenharmony_ci
137094332d3Sopenharmony_ci/**
138094332d3Sopenharmony_ci  * @tc.name: EraseMetadataKeyTest
139094332d3Sopenharmony_ci  * @tc.desc: Benchmarktest for interface EraseMetadataKey.
140094332d3Sopenharmony_ci  */
141094332d3Sopenharmony_ciBENCHMARK_F(DisplayBenchmarkTest, EraseMetadataKeyTest)(benchmark::State &state)
142094332d3Sopenharmony_ci{
143094332d3Sopenharmony_ci    int32_t ret;
144094332d3Sopenharmony_ci    int32_t key = 0;
145094332d3Sopenharmony_ci    for (auto _ : state) {
146094332d3Sopenharmony_ci        std::vector<uint8_t> values(2880, 0);
147094332d3Sopenharmony_ci        ret = g_gralloc->SetMetadata(*g_bufferHandle, key, values);
148094332d3Sopenharmony_ci        EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
149094332d3Sopenharmony_ci        ret = g_gralloc->EraseMetadataKey(*g_bufferHandle, key);
150094332d3Sopenharmony_ci        EXPECT_TRUE(ret == DISPLAY_SUCCESS || ret == DISPLAY_NOT_SUPPORT);
151094332d3Sopenharmony_ci    }
152094332d3Sopenharmony_ci}
153094332d3Sopenharmony_ci
154094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(DisplayBenchmarkTest, EraseMetadataKeyTest)->
155094332d3Sopenharmony_ci    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
156094332d3Sopenharmony_ci
157094332d3Sopenharmony_ci} // namespace
158094332d3Sopenharmony_ciBENCHMARK_MAIN();
159094332d3Sopenharmony_ci
160