• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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 "v1_0/codec_callback_service.h"
28 #include "v1_0/icodec_component.h"
29 #include "v1_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_benchmark_omx_test
35 #define CODEC_NUM 0
36 #define TUNNELE_COMP 1002
37 #define TUNNELED_PORT 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::V1_0;
44 using namespace OHOS::HDI::Display::Buffer::V1_0;
45 using namespace OHOS::HDI::Display::Composer::V1_0;
46 namespace {
47 const int32_t ITERATION_FREQUENCY = 100;
48 const int32_t REPETITION_FREQUENCY = 3;
49 constexpr int32_t WIDTH = 640;
50 constexpr int FD_DEFAULT = -1;
51 constexpr int64_t APP_DATA = 3;
52 constexpr int32_t HEIGHT = 480;
53 constexpr int32_t BUFFER_SIZE = WIDTH * HEIGHT * 3;
54 constexpr int32_t FRAMERATE = 30 << 16;
55 constexpr uint32_t BUFFER_ID_ERROR = 65000;
56 static IDisplayBuffer *gralloc_ = nullptr;
57 static sptr<ICodecComponent> component_ = nullptr;
58 static sptr<ICodecCallback> callback_ = nullptr;
59 static sptr<ICodecComponentManager> manager_ = nullptr;
60 static OHOS::HDI::Codec::V1_0::CodecVersionType version_;
61 static inline std::string compName_ = "";
62 
63 class CodecBenchmarkOmxTest : public benchmark::Fixture {
64 public:
65     enum class PortIndex { PORT_INDEX_INPUT = 0, PORT_INDEX_OUTPUT = 1 };
66     struct BufferInfo {
67         std::shared_ptr<OmxCodecBuffer> omxBuffer;
68         std::shared_ptr<OHOS::Ashmem> sharedMem;
69         BufferHandle *bufferHandle;
BufferInfo__anonf14ae2720111::CodecBenchmarkOmxTest::BufferInfo70         BufferInfo()
71         {
72             omxBuffer = nullptr;
73             sharedMem = nullptr;
74             bufferHandle = nullptr;
75         }
~BufferInfo__anonf14ae2720111::CodecBenchmarkOmxTest::BufferInfo76         ~BufferInfo()
77         {
78             omxBuffer = nullptr;
79             if (sharedMem != nullptr) {
80                 sharedMem->UnmapAshmem();
81                 sharedMem->CloseAshmem();
82                 sharedMem = nullptr;
83             }
84             if (bufferHandle != nullptr && gralloc_ != nullptr) {
85                 gralloc_->FreeMem(*bufferHandle);
86                 bufferHandle = nullptr;
87             }
88         }
89     };
90     template <typename T>
InitParam(T & param)91     void InitParam(T &param)
92     {
93         int32_t ret = memset_s(&param, sizeof(param), 0, sizeof(param));
94         ASSERT_EQ(ret, EOK);
95         param.nSize = sizeof(param);
96         param.nVersion.nVersion = 1;
97     }
98 
99     template <typename T>
InitExtParam(T & param)100     void InitExtParam(T &param)
101     {
102         int32_t ret = memset_s(&param, sizeof(param), 0, sizeof(param));
103         ASSERT_EQ(ret, EOK);
104         param.size = sizeof(param);
105         param.version.nVersion = 1;
106     }
107 
InitOmxCodecBuffer(OmxCodecBuffer & buffer,CodecBufferType type)108     void InitOmxCodecBuffer(OmxCodecBuffer& buffer, CodecBufferType type)
109     {
110         buffer.bufferType = type;
111         buffer.fenceFd = -1;
112         buffer.version = version_;
113         buffer.allocLen = BUFFER_SIZE;
114         buffer.fd = FD_DEFAULT;
115         buffer.bufferhandle = nullptr;
116         buffer.pts = CODEC_NUM;
117         buffer.flag = CODEC_NUM;
118         buffer.size = sizeof(OmxCodecBuffer);
119         buffer.type = READ_ONLY_TYPE;
120     }
121 
InitCodecBufferWithAshMem(enum PortIndex port,int bufferSize,shared_ptr<OmxCodecBuffer> omxBuffer,shared_ptr<OHOS::Ashmem> sharedMem)122     void InitCodecBufferWithAshMem(enum PortIndex port, int bufferSize, shared_ptr<OmxCodecBuffer> omxBuffer,
123                                    shared_ptr<OHOS::Ashmem> sharedMem)
124     {
125         InitOmxCodecBuffer(*omxBuffer.get(), CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
126         omxBuffer->fd = sharedMem->GetAshmemFd();
127         if (port == PortIndex::PORT_INDEX_INPUT) {
128             omxBuffer->type = READ_ONLY_TYPE;
129             sharedMem->MapReadAndWriteAshmem();
130         } else {
131             omxBuffer->type = READ_WRITE_TYPE;
132             sharedMem->MapReadOnlyAshmem();
133         }
134     }
135 
UseBufferOnPort(enum PortIndex port,int32_t bufferCount,int32_t bufferSize)136     bool UseBufferOnPort(enum PortIndex port, int32_t bufferCount, int32_t bufferSize)
137     {
138         for (int i = 0; i < bufferCount; i++) {
139             std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
140             if (omxBuffer == nullptr) {
141                 return false;
142             }
143 
144             int fd = OHOS::AshmemCreate(CODEC_NUM, bufferSize);
145             shared_ptr<OHOS::Ashmem> sharedMem = make_shared<OHOS::Ashmem>(fd, bufferSize);
146             if (sharedMem == nullptr) {
147                 if (fd >= 0) {
148                     close(fd);
149                     fd = -1;
150                 }
151                 return false;
152             }
153             InitCodecBufferWithAshMem(port, bufferSize, omxBuffer, sharedMem);
154             OmxCodecBuffer outBuffer;
155             auto err = component_->UseBuffer(static_cast<uint32_t>(port), *omxBuffer.get(), outBuffer);
156             if (err != HDF_SUCCESS) {
157                 sharedMem->UnmapAshmem();
158                 sharedMem->CloseAshmem();
159                 sharedMem = nullptr;
160                 omxBuffer = nullptr;
161                 return false;
162             }
163             omxBuffer->bufferId = outBuffer.bufferId;
164             omxBuffer->fd = FD_DEFAULT;
165             std::shared_ptr<BufferInfo> bufferInfo = std::make_shared<BufferInfo>();
166             bufferInfo->omxBuffer = omxBuffer;
167             bufferInfo->sharedMem = sharedMem;
168             if (port == PortIndex::PORT_INDEX_INPUT) {
169                 inputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
170             } else {
171                 outputBuffers_.emplace(std::make_pair(omxBuffer->bufferId, bufferInfo));
172             }
173         }
174         return true;
175     }
176 
FreeBufferOnPort(enum PortIndex port)177     bool FreeBufferOnPort(enum PortIndex port)
178     {
179         std::map<int32_t, std::shared_ptr<BufferInfo>> &buffer = inputBuffers_;
180         if (port == PortIndex::PORT_INDEX_OUTPUT) {
181             buffer = outputBuffers_;
182         }
183         for (auto [bufferId, bufferInfo] : buffer) {
184             auto ret = component_->FreeBuffer(static_cast<uint32_t>(port), *bufferInfo->omxBuffer.get());
185             if (ret != HDF_SUCCESS) {
186                 return false;
187             }
188         }
189         buffer.clear();
190         return true;
191     }
192 
InitComponent()193     void InitComponent()
194     {
195         int32_t count = CODEC_NUM;
196         (void)manager_->GetComponentNum(count);
197         if (count > 0) {
198             std::vector<CodecCompCapability> capList;
199             auto err = manager_->GetComponentCapabilityList(capList, count);
200             ASSERT_TRUE(err == HDF_SUCCESS);
201             compName_ = capList[0].compName;
202         }
203 
204         callback_ = new CodecCallbackService();
205         if (callback_ == nullptr) {
206             return;
207         }
208         if (compName_.empty()) {
209             return;
210         }
211 
212         auto ret = manager_->CreateComponent(component_, componentId_, compName_.data(),
213                                              APP_DATA, callback_);
214         ASSERT_TRUE(component_ != nullptr);
215         if (ret != HDF_SUCCESS) {
216             return;
217         }
218         struct CompVerInfo verInfo;
219         ret = component_->GetComponentVersion(verInfo);
220         ASSERT_TRUE(component_ != nullptr);
221         if (ret != HDF_SUCCESS) {
222             return;
223         }
224         version_ = verInfo.compVersion;
225         return;
226     }
227 
SetUp(benchmark::State & state)228     void SetUp(benchmark::State &state)
229     {
230         manager_ = ICodecComponentManager::Get();
231         gralloc_ = IDisplayBuffer::Get();
232         if (manager_ == nullptr || gralloc_ == nullptr) {
233             std::cout << "GetCodecComponentManager  or GetDisplayBuffer ret nullptr" << std::endl;
234             return;
235         }
236         InitComponent();
237     }
238 
TearDown(benchmark::State & state)239     void TearDown(benchmark::State &state)
240     {
241         if (manager_ != nullptr && component_ != nullptr) {
242             manager_->DestroyComponent(componentId_);
243         }
244         component_ = nullptr;
245         callback_ = nullptr;
246         manager_ = nullptr;
247         gralloc_ = nullptr;
248     }
249 
250 public:
251     uint32_t componentId_ = CODEC_NUM;
252     std::map<int32_t, std::shared_ptr<BufferInfo>> inputBuffers_;
253     std::map<int32_t, std::shared_ptr<BufferInfo>> outputBuffers_;
254     const static uint32_t inputIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_INPUT);
255     const static uint32_t outputIndex = static_cast<uint32_t>(PortIndex::PORT_INDEX_OUTPUT);
256 };
257 
258 template <typename T>
ObjectToVector(T & param,std::vector<int8_t> & vec)259 void ObjectToVector(T &param, std::vector<int8_t> &vec)
260 {
261     int8_t *paramPointer = reinterpret_cast<int8_t *>(&param);
262     vec.insert(vec.end(), paramPointer, paramPointer + sizeof(param));
263 }
264 
BENCHMARK_F(CodecBenchmarkOmxTest,GetComponentVersion)265 BENCHMARK_F(CodecBenchmarkOmxTest, GetComponentVersion)(benchmark::State &state)
266 {
267     ASSERT_TRUE(component_ != nullptr);
268     int32_t ret;
269     struct CompVerInfo verInfo;
270     for (auto _ : state) {
271         ret = component_->GetComponentVersion(verInfo);
272         ASSERT_EQ(ret, HDF_SUCCESS);
273     }
274 }
275 
276 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetComponentVersion)->
277     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
278 
BENCHMARK_F(CodecBenchmarkOmxTest,GetParameter)279 BENCHMARK_F(CodecBenchmarkOmxTest, GetParameter)(benchmark::State &state)
280 {
281     ASSERT_TRUE(component_ != nullptr);
282     int32_t ret;
283     CodecVideoPortFormatParam pixFormat;
284     InitExtParam(pixFormat);
285     pixFormat.portIndex = outputIndex;
286     pixFormat.codecColorIndex = CODEC_NUM;
287 
288     std::vector<int8_t> inParam;
289     ObjectToVector(pixFormat, inParam);
290 
291     std::vector<int8_t> outParam;
292     for (auto _ : state) {
293         ret = component_->GetParameter(OMX_IndexCodecVideoPortFormat, inParam, outParam);
294         ASSERT_EQ(ret, HDF_SUCCESS);
295     }
296 }
297 
298 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetParameter)->
299     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
300 
BENCHMARK_F(CodecBenchmarkOmxTest,SetParameter)301 BENCHMARK_F(CodecBenchmarkOmxTest, SetParameter)(benchmark::State &state)
302 {
303     ASSERT_TRUE(component_ != nullptr);
304     int32_t ret;
305     OMX_VIDEO_PARAM_PORTFORMATTYPE param;
306     InitParam(param);
307     param.nPortIndex = inputIndex;
308     std::vector<int8_t> paramVec;
309     ObjectToVector(param, paramVec);
310     for (auto _ : state) {
311         ret = component_->SetParameter(OMX_IndexParamVideoPortFormat, paramVec);
312         ASSERT_EQ(ret, HDF_SUCCESS);
313     }
314 }
315 
316 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, SetParameter)->
317     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
318 
BENCHMARK_F(CodecBenchmarkOmxTest,GetConfig)319 BENCHMARK_F(CodecBenchmarkOmxTest, GetConfig)(benchmark::State &state)
320 {
321     ASSERT_TRUE(component_ != nullptr);
322     int32_t ret;
323     OMX_VIDEO_CONFIG_BITRATETYPE param;
324     InitParam(param);
325     param.nPortIndex = outputIndex;
326     std::vector<int8_t> inParam;
327     ObjectToVector(param, inParam);
328     std::vector<int8_t> outParam;
329     for (auto _ : state) {
330         ret = component_->GetConfig(OMX_IndexConfigVideoBitrate, inParam, outParam);
331         ASSERT_EQ(ret, HDF_SUCCESS);
332     }
333 }
334 
335 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetConfig)->
336     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
337 
BENCHMARK_F(CodecBenchmarkOmxTest,SetConfig)338 BENCHMARK_F(CodecBenchmarkOmxTest, SetConfig)(benchmark::State &state)
339 {
340     ASSERT_TRUE(component_ != nullptr);
341     int32_t ret;
342     OMX_VIDEO_CONFIG_BITRATETYPE param;
343     InitParam(param);
344     param.nPortIndex = outputIndex;
345     param.nEncodeBitrate = FRAMERATE;
346 
347     std::vector<int8_t> inParam;
348     ObjectToVector(param, inParam);
349     for (auto _ : state) {
350         ret = component_->SetConfig(OMX_IndexConfigVideoBitrate, inParam);
351         ASSERT_EQ(ret, HDF_SUCCESS);
352     }
353 }
354 
355 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, SetConfig)->
356     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
357 
358 #ifdef SUPPORT_OMX_EXTEND
BENCHMARK_F(CodecBenchmarkOmxTest,GetExtensionIndex)359 BENCHMARK_F(CodecBenchmarkOmxTest, GetExtensionIndex)(benchmark::State &state)
360 {
361     ASSERT_TRUE(component_ != nullptr);
362     int32_t ret;
363     uint32_t indexType = CODEC_NUM;
364     for (auto _ : state) {
365         ret = component_->GetExtensionIndex("OMX.Topaz.index.param.extended_video", indexType);
366         ASSERT_EQ(ret, HDF_SUCCESS);
367     }
368 }
369 
370 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetExtensionIndex)->
371     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
372 #endif
373 
BENCHMARK_F(CodecBenchmarkOmxTest,GetState)374 BENCHMARK_F(CodecBenchmarkOmxTest, GetState)(benchmark::State &state)
375 {
376     ASSERT_TRUE(component_ != nullptr);
377     int32_t ret;
378     CodecStateType codecState = CODEC_STATE_INVALID;
379     for (auto _ : state) {
380         ret = component_->GetState(codecState);
381         ASSERT_EQ(codecState, CODEC_STATE_LOADED);
382         ASSERT_EQ(ret, HDF_SUCCESS);
383     }
384 }
385 
386 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, GetState)->
387     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
388 
389 #ifdef SUPPORT_OMX_EXTEND
BENCHMARK_F(CodecBenchmarkOmxTest,ComponentTunnelRequest)390 BENCHMARK_F(CodecBenchmarkOmxTest, ComponentTunnelRequest)(benchmark::State &state)
391 {
392     ASSERT_TRUE(component_ != nullptr);
393     int32_t ret;
394     const int32_t tunneledComp = TUNNELE_COMP;
395     const uint32_t tunneledPort = TUNNELED_PORT;
396     OHOS::HDI::Codec::V1_0::CodecTunnelSetupType tunnelSetup;
397     tunnelSetup.supplier = OHOS::HDI::Codec::V1_0::CODEC_BUFFER_SUPPLY_INPUT;
398     for (auto _ : state) {
399     ret = component_->ComponentTunnelRequest(outputIndex, tunneledComp, tunneledPort,
400         tunnelSetup, tunnelSetup);
401         ASSERT_NE(ret, HDF_SUCCESS);
402     }
403 }
404 
405 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, ComponentTunnelRequest)->
406     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
407 
BENCHMARK_F(CodecBenchmarkOmxTest,SendCommand)408 BENCHMARK_F(CodecBenchmarkOmxTest, SendCommand)(benchmark::State &state)
409 {
410     std::vector<int8_t> cmdData;
411     int32_t ret;
412     for (auto _ : state) {
413         if (component_ == nullptr) {
414             InitComponent();
415         }
416         ret = component_->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
417         manager_->DestroyComponent(componentId_);
418         ASSERT_EQ(ret, HDF_SUCCESS);
419         component_ = nullptr;
420     }
421 }
422 
423 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, SendCommand)->
424     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
425 
BENCHMARK_F(CodecBenchmarkOmxTest,AllocateBuffer)426 BENCHMARK_F(CodecBenchmarkOmxTest, AllocateBuffer)(benchmark::State &state)
427 {
428     ASSERT_TRUE(component_ != nullptr);
429     std::vector<int8_t> cmdData;
430     auto err = component_->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
431     ASSERT_EQ(err, HDF_SUCCESS);
432 
433     struct OmxCodecBuffer allocBuffer;
434     InitOmxCodecBuffer(allocBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
435     allocBuffer.type = READ_WRITE_TYPE;
436     struct OmxCodecBuffer outBuffer;
437     for (auto _ : state) {
438         err = component_->AllocateBuffer(outputIndex, allocBuffer, outBuffer);
439         ASSERT_EQ(err, HDF_SUCCESS);
440         err = component_->FreeBuffer(outputIndex, outBuffer);
441         ASSERT_EQ(err, HDF_SUCCESS);
442     }
443 }
444 
445 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, AllocateBuffer)->
446     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
447 
BENCHMARK_F(CodecBenchmarkOmxTest,UseBuffer)448 BENCHMARK_F(CodecBenchmarkOmxTest, UseBuffer)(benchmark::State &state)
449 {
450     ASSERT_TRUE(component_ != nullptr);
451     std::vector<int8_t> cmdData;
452     auto ret = component_->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
453     ASSERT_EQ(ret, HDF_SUCCESS);
454 
455     AllocInfo alloc = {.width = WIDTH,
456                        .height = HEIGHT,
457                        .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
458                        .format = PIXEL_FMT_YCBCR_420_SP};
459     BufferHandle *bufferHandle = nullptr;
460     ASSERT_TRUE(gralloc_ != nullptr);
461     ret = gralloc_->AllocMem(alloc, bufferHandle);
462     ASSERT_EQ(ret, DISPLAY_SUCCESS);
463 
464     std::shared_ptr<OmxCodecBuffer> omxBuffer = std::make_shared<OmxCodecBuffer>();
465     size_t handleSize =
466         sizeof(BufferHandle) + (sizeof(int32_t) * (bufferHandle->reserveFds + bufferHandle->reserveInts));
467     InitOmxCodecBuffer(*omxBuffer.get(), CODEC_BUFFER_TYPE_HANDLE);
468     omxBuffer->bufferhandle = new NativeBuffer(bufferHandle);
469     omxBuffer->allocLen = handleSize;
470     OmxCodecBuffer outBuffer;
471     for (auto _ : state) {
472         ret = component_->UseBuffer(inputIndex, *omxBuffer.get(), outBuffer);
473         omxBuffer->bufferId = outBuffer.bufferId;
474         ASSERT_EQ(ret, HDF_SUCCESS);
475         ret = component_->FreeBuffer(inputIndex, outBuffer);
476         ASSERT_EQ(ret, HDF_SUCCESS);
477     }
478 }
479 
480 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, UseBuffer)->
481     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
482 #endif
483 
BENCHMARK_F(CodecBenchmarkOmxTest,UseEglImage)484 BENCHMARK_F(CodecBenchmarkOmxTest, UseEglImage)(benchmark::State &state)
485 {
486     ASSERT_TRUE(component_ != nullptr);
487     int32_t ret;
488     struct OmxCodecBuffer omxBuffer;
489     InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
490     auto eglImage = std::make_unique<int8_t[]>(BUFFER_SIZE);
491     ASSERT_TRUE(eglImage != nullptr);
492     std::vector<int8_t> eglImageVec;
493     eglImageVec.assign(eglImage.get(), eglImage.get() + BUFFER_SIZE);
494     struct OmxCodecBuffer outbuffer;
495     for (auto _ : state) {
496         ret = component_->UseEglImage(inputIndex, omxBuffer, outbuffer, eglImageVec);
497         ASSERT_NE(ret, HDF_SUCCESS);
498         eglImage = nullptr;
499     }
500 }
501 
502 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, UseEglImage)->
503     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
504 
BENCHMARK_F(CodecBenchmarkOmxTest,FillThisBuffer)505 BENCHMARK_F(CodecBenchmarkOmxTest, FillThisBuffer)(benchmark::State &state)
506 {
507     ASSERT_TRUE(component_ != nullptr);
508     int32_t ret;
509     struct OmxCodecBuffer omxBuffer;
510     InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
511     omxBuffer.bufferId = BUFFER_ID_ERROR;
512     for (auto _ : state) {
513         ret = component_->FillThisBuffer(omxBuffer);
514         ASSERT_NE(ret, HDF_SUCCESS);
515     }
516 }
517 
518 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, FillThisBuffer)->
519     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
520 
BENCHMARK_F(CodecBenchmarkOmxTest,EmptyThisBuffer)521 BENCHMARK_F(CodecBenchmarkOmxTest, EmptyThisBuffer)(benchmark::State &state)
522 {
523     ASSERT_TRUE(component_ != nullptr);
524     int32_t ret;
525     struct OmxCodecBuffer omxBuffer;
526     InitOmxCodecBuffer(omxBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
527     omxBuffer.bufferId = BUFFER_ID_ERROR;
528     for (auto _ : state) {
529         ret = component_->EmptyThisBuffer(omxBuffer);
530         ASSERT_NE(ret, HDF_SUCCESS);
531     }
532 }
533 
534 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, EmptyThisBuffer)->
535     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
536 
BENCHMARK_F(CodecBenchmarkOmxTest,SetCallbacks)537 BENCHMARK_F(CodecBenchmarkOmxTest, SetCallbacks)(benchmark::State &state)
538 {
539     ASSERT_TRUE(component_ != nullptr);
540     int32_t ret;
541     callback_ = new CodecCallbackService();
542     ASSERT_TRUE(callback_ != nullptr);
543     for (auto _ : state) {
544         ret = component_->SetCallbacks(callback_, APP_DATA);
545         ASSERT_EQ(ret, HDF_SUCCESS);
546     }
547 }
548 
549 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, SetCallbacks)->
550     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
551 
552 #ifdef SUPPORT_OMX_EXTEND
BENCHMARK_F(CodecBenchmarkOmxTest,ComponentRoleEnum)553 BENCHMARK_F(CodecBenchmarkOmxTest, ComponentRoleEnum)(benchmark::State &state)
554 {
555     ASSERT_TRUE(component_ != nullptr);
556     int32_t ret;
557     std::vector<uint8_t> role;
558     for (auto _ : state) {
559         ret = component_->ComponentRoleEnum(role, CODEC_NUM);
560         ASSERT_EQ(ret, HDF_SUCCESS);
561     }
562 }
563 
564 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, ComponentRoleEnum)->
565     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
566 
BENCHMARK_F(CodecBenchmarkOmxTest,FreeBuffer)567 BENCHMARK_F(CodecBenchmarkOmxTest, FreeBuffer)(benchmark::State &state)
568 {
569     ASSERT_TRUE(component_ != nullptr);
570     std::vector<int8_t> cmdData;
571     auto err = component_->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
572     ASSERT_EQ(err, HDF_SUCCESS);
573 
574     struct OmxCodecBuffer allocBuffer;
575     InitOmxCodecBuffer(allocBuffer, CODEC_BUFFER_TYPE_AVSHARE_MEM_FD);
576     allocBuffer.type = READ_WRITE_TYPE;
577     struct OmxCodecBuffer outBuffer;
578     for (auto _ : state) {
579         err = component_->AllocateBuffer(outputIndex, allocBuffer, outBuffer);
580         ASSERT_EQ(err, HDF_SUCCESS);
581         err = component_->FreeBuffer(outputIndex, outBuffer);
582         ASSERT_EQ(err, HDF_SUCCESS);
583     }
584 }
585 
586 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, FreeBuffer)->
587     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
588 
BENCHMARK_F(CodecBenchmarkOmxTest,ComponentDeInit)589 BENCHMARK_F(CodecBenchmarkOmxTest, ComponentDeInit)(benchmark::State &state)
590 {
591     std::vector<int8_t> cmdData;
592     int32_t ret;
593     for (auto _ : state) {
594         if (component_ == nullptr) {
595             InitComponent();
596         }
597         ret = component_->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_LOADED, cmdData);
598         ASSERT_EQ(ret, HDF_SUCCESS);
599         CodecStateType state = CODEC_STATE_INVALID;
600         do {
601             usleep(ITERATION_FREQUENCY);
602             ret = component_->GetState(state);
603         } while (state != CODEC_STATE_LOADED);
604         ret = component_->ComponentDeInit();
605         if (manager_ != nullptr && component_ != nullptr) {
606             manager_->DestroyComponent(componentId_);
607         }
608         component_ = nullptr;
609         ASSERT_EQ(ret, HDF_SUCCESS);
610     }
611 }
612 
613 BENCHMARK_REGISTER_F(CodecBenchmarkOmxTest, ComponentDeInit)->
614     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
615 #endif
616 }  // namespace
617 BENCHMARK_MAIN();
618