• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <OMX_Component.h>
17 #include <OMX_Core.h>
18 #include <OMX_Video.h>
19 #include <OMX_VideoExt.h>
20 #include <gtest/gtest.h>
21 #include <hdf_log.h>
22 #include <securec.h>
23 #include <servmgr_hdi.h>
24 #include <vector>
25 #include <benchmark/benchmark.h>
26 #include "codec_omx_ext.h"
27 #include "v2_0/codec_callback_service.h"
28 #include "v2_0/icodec_component.h"
29 #include "v2_0/icodec_component_manager.h"
30 #include "v1_0/display_composer_type.h"
31 #include "v1_0/display_buffer_type.h"
32 #include "v1_0/include/idisplay_buffer.h"
33 
34 #define HDF_LOG_TAG codec_hdi_test
35 #define COMPONENTNUM 0
36 #define TUNNELECOMP 1002
37 #define TUNNELEDPORT 101
38 
39 using namespace std;
40 using namespace testing::ext;
41 using OHOS::sptr;
42 using OHOS::HDI::Base::NativeBuffer;
43 using namespace OHOS::HDI::Codec::V2_0;
44 using namespace OHOS::HDI::Display::Buffer::V1_0;
45 using namespace OHOS::HDI::Display::Composer::V1_0;
46 namespace {
47 constexpr int32_t WIDTH = 640;
48 #ifdef SUPPORT_OMX
49 constexpr uint32_t MAX_ROLE_INDEX = 1000;
50 #endif
51 constexpr int FD_DEFAULT = -1;
52 constexpr int64_t APP_DATA = 3;
53 constexpr int32_t HEIGHT = 480;
54 constexpr int32_t BUFFER_SIZE = WIDTH * HEIGHT * 3;
55 constexpr int32_t FRAMERATE = 30 << 16;
56 constexpr uint32_t BUFFER_ID_ERROR = 65000;
57 static IDisplayBuffer *gralloc_ = nullptr;
58 static sptr<ICodecComponent> component_ = nullptr;
59 static sptr<ICodecCallback> callback_ = nullptr;
60 static sptr<ICodecComponentManager> manager_ = nullptr;
61 static OHOS::HDI::Codec::V2_0::CodecVersionType version_;
62 static inline std::string compName_ = "";
63 
64 class CodecBenchmarkOmxTest : public benchmark::Fixture {
65 public:
66     enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 };
67     template <typename T>
InitParam(T & param)68     void InitParam(T &param)
69     {
70         memset_s(&param, sizeof(param), 0x0, sizeof(param));
71         param.nSize = sizeof(param);
72         param.nVersion.nVersion = 1;
73     }
74 
InitOmxCodecBuffer(OmxCodecBuffer & buffer,CodecBufferType type)75     void InitOmxCodecBuffer(OmxCodecBuffer& buffer, CodecBufferType type)
76     {
77         buffer.bufferType = type;
78         buffer.fenceFd = -1;
79         buffer.version = version_;
80         buffer.allocLen = BUFFER_SIZE;
81         buffer.fd = FD_DEFAULT;
82         buffer.bufferhandle = nullptr;
83         buffer.pts = 0;
84         buffer.flag = 0;
85         buffer.size = sizeof(OmxCodecBuffer);
86         buffer.type = READ_ONLY_TYPE;
87     }
88 
Init()89     void Init()
90     {
91         int32_t count = 0;
92         (void)manager_->GetComponentNum(count);
93         if (count > 0) {
94             std::vector<CodecCompCapability> capList;
95             auto err = manager_->GetComponentCapabilityList(capList, count);
96             ASSERT_TRUE(err == HDF_SUCCESS);
97             compName_ = capList[0].compName;
98         }
99 
100         if (manager_ == nullptr) {
101             return;
102         }
103         callback_ = new CodecCallbackService();
104         if (callback_ == nullptr) {
105             return;
106         }
107         if (compName_.empty()) {
108             return;
109         }
110 
111         auto ret = manager_->CreateComponent(component_, componentId_, compName_.data(),
112                                              APP_DATA, callback_);
113         ASSERT_TRUE(component_ != nullptr);
114         if (ret != HDF_SUCCESS) {
115             return;
116         }
117         struct CompVerInfo verInfo;
118         ret = component_->GetComponentVersion(verInfo);
119         ASSERT_TRUE(component_ != nullptr);
120         if (ret != HDF_SUCCESS) {
121             return;
122         }
123         version_ = verInfo.compVersion;
124         return;
125     }
126 
SetUp(benchmark::State & state)127     void SetUp(benchmark::State &state)
128     {
129         manager_ = ICodecComponentManager::Get();
130         gralloc_ = IDisplayBuffer::Get();
131         Init();
132         if (manager_ == nullptr) {
133             std::cout<<"GetCodecComponentManager ret nullptr"<<std::endl;
134             return;
135         }
136     }
137 
TearDown(benchmark::State & state)138     void TearDown(benchmark::State &state)
139     {
140         if (manager_ != nullptr && component_ != nullptr) {
141             manager_->DestroyComponent(componentId_);
142         }
143         component_ = nullptr;
144         callback_ = nullptr;
145         manager_ = nullptr;
146         gralloc_ = nullptr;
147     }
148 
149 public:
150     uint32_t componentId_ = 0;
151     const static uint32_t inputIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
152     const static uint32_t outputIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT);
153 };
154 
155 template <typename T>
ObjectToVector(T & param,std::vector<int8_t> & vec)156 void ObjectToVector(T &param, std::vector<int8_t> &vec)
157 {
158     int8_t *paramPointer = reinterpret_cast<int8_t *>(&param);
159     vec.insert(vec.end(), paramPointer, paramPointer + sizeof(param));
160 }
161 
BENCHMARK_F(CodecBenchmarkOmxTest,GetComponentVersion)162 BENCHMARK_F(CodecBenchmarkOmxTest, GetComponentVersion)(benchmark::State &state)
163 {
164     ASSERT_TRUE(component_ != nullptr);
165     int32_t ret;
166     struct CompVerInfo verInfo;
167     for (auto _ : state) {
168         ret = component_->GetComponentVersion(verInfo);
169     }
170     ASSERT_EQ(ret, HDF_SUCCESS);
171 }
172 
173 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetComponentVersion)->
174     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
175 
BENCHMARK_F(CodecBenchmarkOmxTest,GetParameter)176 BENCHMARK_F(CodecBenchmarkOmxTest, GetParameter)(benchmark::State &state)
177 {
178     ASSERT_TRUE(component_ != nullptr);
179     int32_t ret;
180     std::vector<int8_t> inParam;
181     std::vector <int8_t> outParam;
182     for (auto _ : state) {
183         ret = component_->GetParameter(OMX_IndexParamVideoPortFormat, inParam, outParam);
184     }
185     ASSERT_NE(ret, HDF_SUCCESS);
186 }
187 
188 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetParameter)->
189     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
190 
BENCHMARK_F(CodecBenchmarkOmxTest,SetParameter)191 BENCHMARK_F(CodecBenchmarkOmxTest, SetParameter)(benchmark::State &state)
192 {
193     ASSERT_TRUE(component_ != nullptr);
194     int32_t ret;
195     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
196     InitParam(param);
197     param.nPortIndex = inputIndex;
198     std::vector<int8_t> paramVec;
199     ObjectToVector(param, paramVec);
200     for (auto _ : state) {
201         ret = component_->SetParameter(OMX_IndexParamVideoPortFormat, paramVec);
202     }
203     ASSERT_EQ(ret, HDF_SUCCESS);
204 }
205 
206 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, SetParameter)->
207     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
208 
BENCHMARK_F(CodecBenchmarkOmxTest,GetConfig)209 BENCHMARK_F(CodecBenchmarkOmxTest, GetConfig)(benchmark::State &state)
210 {
211     ASSERT_TRUE(component_ != nullptr);
212     int32_t ret;
213     OMX_VIDEO_CONFIG_BITRATETYPE param;
214     InitParam(param);
215     param.nPortIndex = outputIndex;
216     std::vector<int8_t> inParam;
217     ObjectToVector(param, inParam);
218     std::vector<int8_t> outParam;
219     for (auto _ : state) {
220         ret = component_->GetConfig(OMX_IndexConfigVideoBitrate, inParam, outParam);
221     }
222     ASSERT_EQ(ret, HDF_SUCCESS);
223 }
224 
225 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetConfig)->
226     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
227 
BENCHMARK_F(CodecBenchmarkOmxTest,SetConfig)228 BENCHMARK_F(CodecBenchmarkOmxTest, SetConfig)(benchmark::State &state)
229 {
230     ASSERT_TRUE(component_ != nullptr);
231     int32_t ret;
232     OMX_VIDEO_CONFIG_BITRATETYPE param;
233     InitParam(param);
234     param.nPortIndex = inputIndex;
235     param.nEncodeBitrate = FRAMERATE;
236     std::vector<int8_t> inParam;
237     ObjectToVector(param, inParam);
238     for (auto _ : state) {
239         ret = component_->SetConfig(OMX_IndexConfigVideoBitrate, inParam);
240     }
241     ASSERT_NE(ret, HDF_SUCCESS);
242 }
243 
244 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, SetConfig)->
245     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
246 
BENCHMARK_F(CodecBenchmarkOmxTest,GetExtensionIndex)247 BENCHMARK_F(CodecBenchmarkOmxTest, GetExtensionIndex)(benchmark::State &state)
248 {
249     ASSERT_TRUE(component_ != nullptr);
250     int32_t ret;
251     uint32_t indexType = 0;
252     for (auto _ : state) {
253         ret = component_->GetExtensionIndex("OMX.Topaz.index.param.extended_test", indexType);
254     }
255     ASSERT_NE(ret, HDF_SUCCESS);
256 }
257 
258 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetExtensionIndex)->
259     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
260 
BENCHMARK_F(CodecBenchmarkOmxTest,GetState)261 BENCHMARK_F(CodecBenchmarkOmxTest, GetState)(benchmark::State &state)
262 {
263     ASSERT_TRUE(component_ != nullptr);
264     int32_t ret;
265     CodecStateType codecState =CODEC_STATE_INVALID;
266     for (auto _ : state) {
267         ret = component_->GetState(codecState);
268     }
269     ASSERT_EQ(codecState, CODEC_STATE_LOADED);
270     ASSERT_EQ(ret, HDF_SUCCESS);
271 }
272 
273 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetState)->
274     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
275 
276 #ifdef SUPPORT_OMX
BENCHMARK_F(CodecBenchmarkOmxTest,ComponentTunnelRequest)277 BENCHMARK_F(CodecBenchmarkOmxTest, ComponentTunnelRequest)(benchmark::State &state)
278 {
279     ASSERT_TRUE(component_ != nullptr);
280     int32_t ret;
281     const int32_t tunneledComp = TUNNELECOMP;
282     const uint32_t tunneledPort = TUNNELEDPORT;
283     OHOS::HDI::Codec::V2_0::CodecTunnelSetupType tunnelSetup;
284     tunnelSetup.supplier = OHOS::HDI::Codec::V2_0::CODEC_BUFFER_SUPPLY_INPUT;
285     for (auto _ : state) {
286     ret = component_->ComponentTunnelRequest(outputIndex, tunneledComp, tunneledPort,
287         tunnelSetup, tunnelSetup);
288     }
289     ASSERT_NE(ret, HDF_SUCCESS);
290 }
291 
292 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, ComponentTunnelRequest)->
293     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
294 #endif
295 
BENCHMARK_F(CodecBenchmarkOmxTest,SendCommand)296 BENCHMARK_F(CodecBenchmarkOmxTest, SendCommand)(benchmark::State &state)
297 {
298     std::vector<int8_t> cmdData;
299     int32_t ret;
300     for (auto _ : state) {
301         Init();
302         ret = component_->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
303         manager_->DestroyComponent(componentId_);
304         if (manager_ != nullptr && component_ != nullptr) {
305             manager_->DestroyComponent(componentId_);
306         }
307         component_ = nullptr;
308         callback_ = nullptr;
309     }
310     ASSERT_EQ(ret, HDF_SUCCESS);
311 }
312 
313 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, SendCommand)->
314     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
315 
BENCHMARK_F(CodecBenchmarkOmxTest,AllocateBuffer)316 BENCHMARK_F(CodecBenchmarkOmxTest, AllocateBuffer)(benchmark::State &state)
317 {
318     ASSERT_TRUE(component_ != nullptr);
319     int32_t ret;
320     struct OmxCodecBuffer allocBuffer;
321     InitOmxCodecBuffer(allocBuffer, CODEC_BUFFER_TYPE_VIRTUAL_ADDR);
322     struct OmxCodecBuffer outBuffer;
323     for (auto _ : state) {
324         ret = component_->AllocateBuffer(inputIndex, allocBuffer, outBuffer);
325     }
326     ASSERT_NE(ret, HDF_SUCCESS);
327 }
328 
329 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, AllocateBuffer)->
330     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
331 
BENCHMARK_F(CodecBenchmarkOmxTest,UseBuffer)332 BENCHMARK_F(CodecBenchmarkOmxTest, UseBuffer)(benchmark::State &state)
333 {
334     ASSERT_TRUE(component_ != nullptr);
335     int32_t ret;
336     struct OmxCodecBuffer omxBuffer;
337     InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_INVALID);
338     struct OmxCodecBuffer outBuffer;
339     for (auto _ : state) {
340         ret = component_->UseBuffer(inputIndex, omxBuffer, outBuffer);
341     }
342     ASSERT_NE(ret, HDF_SUCCESS);
343 }
344 
345 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, UseBuffer)->
346     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
347 
BENCHMARK_F(CodecBenchmarkOmxTest,UseEglImage)348 BENCHMARK_F(CodecBenchmarkOmxTest, UseEglImage)(benchmark::State &state)
349 {
350     ASSERT_TRUE(component_ != nullptr);
351     int32_t ret;
352     struct OmxCodecBuffer omxBuffer;
353     InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
354     auto eglImage = std::make_unique<int8_t[]>(BUFFER_SIZE);
355     ASSERT_TRUE(eglImage != nullptr);
356     std::vector<int8_t> eglImageVec;
357     eglImageVec.assign(eglImage.get(), eglImage.get() + BUFFER_SIZE);
358     struct OmxCodecBuffer outbuffer;
359     for (auto _ : state) {
360         ret = component_->UseEglImage(inputIndex, omxBuffer, outbuffer, eglImageVec);
361     }
362     ASSERT_NE(ret, HDF_SUCCESS);
363     eglImage = nullptr;
364 }
365 
366 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, UseEglImage)->
367     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
368 
BENCHMARK_F(CodecBenchmarkOmxTest,FillThisBuffer)369 BENCHMARK_F(CodecBenchmarkOmxTest, FillThisBuffer)(benchmark::State &state)
370 {
371     ASSERT_TRUE(component_ != nullptr);
372     int32_t ret;
373     struct OmxCodecBuffer omxBuffer;
374     InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
375     omxBuffer.bufferId = BUFFER_ID_ERROR;
376     for (auto _ : state) {
377         ret = component_->FillThisBuffer(omxBuffer);
378     }
379     ASSERT_NE(ret, HDF_SUCCESS);
380 }
381 
382 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, FillThisBuffer)->
383     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
384 
BENCHMARK_F(CodecBenchmarkOmxTest,EmptyThisBuffer)385 BENCHMARK_F(CodecBenchmarkOmxTest, EmptyThisBuffer)(benchmark::State &state)
386 {
387     ASSERT_TRUE(component_ != nullptr);
388     int32_t ret;
389     struct OmxCodecBuffer omxBuffer;
390     InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
391     omxBuffer.bufferId = BUFFER_ID_ERROR;
392     for (auto _ : state) {
393         ret = component_->EmptyThisBuffer(omxBuffer);
394     }
395     ASSERT_NE(ret, HDF_SUCCESS);
396 }
397 
398 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, EmptyThisBuffer)->
399     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
400 
BENCHMARK_F(CodecBenchmarkOmxTest,SetCallbacks)401 BENCHMARK_F(CodecBenchmarkOmxTest, SetCallbacks)(benchmark::State &state)
402 {
403     ASSERT_TRUE(component_ != nullptr);
404     int32_t ret;
405     callback_ = new CodecCallbackService();
406     ASSERT_TRUE(callback_ != nullptr);
407     for (auto _ : state) {
408         ret = component_->SetCallbacks(callback_, APP_DATA);
409     }
410     ASSERT_EQ(ret, HDF_SUCCESS);
411 }
412 
413 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, SetCallbacks)->
414     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
415 
416 #ifdef SUPPORT_OMX
BENCHMARK_F(CodecBenchmarkOmxTest,ComponentRoleEnum)417 BENCHMARK_F(CodecBenchmarkOmxTest, ComponentRoleEnum)(benchmark::State &state)
418 {
419     ASSERT_TRUE(component_ != nullptr);
420     int32_t ret;
421     std::vector<uint8_t> role;
422     for (auto _ : state) {
423         ret = component_->ComponentRoleEnum(role, MAX_ROLE_INDEX);
424     }
425     ASSERT_NE(ret, HDF_SUCCESS);
426 }
427 
428 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, ComponentRoleEnum)->
429     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
430 #endif
431 
BENCHMARK_F(CodecBenchmarkOmxTest,FreeBuffer)432 BENCHMARK_F(CodecBenchmarkOmxTest, FreeBuffer)(benchmark::State &state)
433 {
434     ASSERT_TRUE(component_ != nullptr);
435     int32_t ret;
436     struct OmxCodecBuffer omxBuffer;
437     InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
438     omxBuffer.bufferId = BUFFER_ID_ERROR;
439     for (auto _ : state) {
440         ret = component_->FreeBuffer(outputIndex, omxBuffer);
441     }
442     ASSERT_NE(ret, HDF_SUCCESS);
443 }
444 
445 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, FreeBuffer)->
446     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
447 
448 #ifdef SUPPORT_OMX
BENCHMARK_F(CodecBenchmarkOmxTest,ComponentDeInit)449 BENCHMARK_F(CodecBenchmarkOmxTest, ComponentDeInit)(benchmark::State &state)
450 {
451     std::vector<int8_t> cmdData;
452     int32_t ret;
453     for (auto _ : state) {
454         Init();
455         ret = component_->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_LOADED, cmdData);
456         CodecStateType state = CODEC_STATE_INVALID;
457         do {
458             usleep(100);
459             ret = component_->GetState(state);
460         } while (state != CODEC_STATE_LOADED);
461         ret = component_->ComponentDeInit();
462         if (manager_ != nullptr && component_ != nullptr) {
463             manager_->DestroyComponent(componentId_);
464         }
465         component_ = nullptr;
466         callback_ = nullptr;
467     }
468     ASSERT_EQ(ret, HDF_SUCCESS);
469 }
470 
471 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, ComponentDeInit)->
472     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
473 #endif
474 }  // namespace
475 BENCHMARK_MAIN();
476