• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 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 "codec_dma_buffer.h"
17 #include <hdf_base.h>
18 #include <securec.h>
19 #include <unistd.h>
20 #include <hdf_remote_service.h>
21 #include "codec_log_wrapper.h"
22 #include "v4_0/codec_types.h"
23 using namespace OHOS::HDI::Codec::V4_0;
24 namespace OHOS {
25 namespace Codec {
26 namespace Omx {
27 
UseBuffer(OMX_HANDLETYPE comp,uint32_t portIndex,OmxCodecBuffer & codecBuffer,OMX_BUFFERHEADERTYPE * & header)28 sptr<ICodecBuffer> CodecDMABuffer::UseBuffer(OMX_HANDLETYPE comp, uint32_t portIndex,
29     OmxCodecBuffer &codecBuffer, OMX_BUFFERHEADERTYPE *&header)
30 {
31     CHECK_AND_RETURN_RET_LOG(comp != nullptr, nullptr, "null component");
32     CHECK_AND_RETURN_RET_LOG(codecBuffer.fd != nullptr, nullptr, "invalid dma fd");
33     CHECK_AND_RETURN_RET_LOG(codecBuffer.allocLen > 0, nullptr, "invalid allocLen");
34     CODEC_LOGI("port=%{public}u, use dmabuffer, fd=%{public}d", portIndex, codecBuffer.fd->Get());
35 
36     std::shared_ptr<UniqueFd> dmaFd = codecBuffer.fd;
37     OMXBufferAppPrivateData priv{};
38     int fd = codecBuffer.fd->Get();
39     priv.fd = codecBuffer.fd->Get();
40     priv.sizeOfParam = static_cast<uint32_t>(codecBuffer.alongParam.size());
41     priv.param = static_cast<void *>(codecBuffer.alongParam.data());
42     int32_t err = OMX_UseBuffer(comp, &header, portIndex, &priv, codecBuffer.allocLen,
43         reinterpret_cast<OMX_U8 *>(&fd));
44     if (err != OMX_ErrorNone) {
45         CODEC_LOGE("OMX_UseBuffer ret = [%{public}x]", err);
46         return nullptr;
47     }
48     codecBuffer.bufferhandle = nullptr;
49     codecBuffer.fd.reset();
50     codecBuffer.fenceFd.reset();
51     return sptr<ICodecBuffer>(new CodecDMABuffer(
52         InitInfo{comp, portIndex, codecBuffer, header}, dmaFd)
53     );
54 }
55 
AllocateBuffer(OMX_HANDLETYPE comp,uint32_t portIndex,OmxCodecBuffer & codecBuffer,OMX_BUFFERHEADERTYPE * & header)56 sptr<ICodecBuffer> CodecDMABuffer::AllocateBuffer(OMX_HANDLETYPE comp, uint32_t portIndex,
57     OmxCodecBuffer &codecBuffer, OMX_BUFFERHEADERTYPE *&header)
58 {
59     CHECK_AND_RETURN_RET_LOG(comp != nullptr, nullptr, "null component");
60     CHECK_AND_RETURN_RET_LOG(codecBuffer.allocLen > 0, nullptr, "invalid allocLen");
61 
62     OMXBufferAppPrivateData priv{};
63     int32_t err = OMX_AllocateBuffer(comp, &header, portIndex, &priv, codecBuffer.allocLen);
64     if (err != OMX_ErrorNone) {
65         CODEC_LOGE("OMX_AllocateBuffer error, err = %{public}x", err);
66         return nullptr;
67     }
68     CODEC_LOGI("port=%{public}u, allocate dmabuffer, fd=%{public}d", portIndex, priv.fd);
69     std::shared_ptr<UniqueFd> dmaFd = UniqueFd::Create(priv.fd, false);
70     codecBuffer.bufferhandle = nullptr;
71     codecBuffer.fd = dmaFd;
72     codecBuffer.fenceFd.reset();
73     return sptr<ICodecBuffer>(new CodecDMABuffer(
74         InitInfo{comp, portIndex, codecBuffer, header}, dmaFd)
75     );
76 }
77 
78 }  // namespace Omx
79 }  // namespace Codec
80 }  // namespace OHOS