1c5e268c6Sopenharmony_ci/*
2c5e268c6Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3c5e268c6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c5e268c6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c5e268c6Sopenharmony_ci * You may obtain a copy of the License at
6c5e268c6Sopenharmony_ci *
7c5e268c6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c5e268c6Sopenharmony_ci *
9c5e268c6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c5e268c6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c5e268c6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c5e268c6Sopenharmony_ci * See the License for the specific language governing permissions and
13c5e268c6Sopenharmony_ci * limitations under the License.
14c5e268c6Sopenharmony_ci */
15c5e268c6Sopenharmony_ci
16c5e268c6Sopenharmony_ci#include "securec.h"
17c5e268c6Sopenharmony_ci#include <message_parcel.h>
18c5e268c6Sopenharmony_ci#include "buffer_handle_sequenceable.h"
19c5e268c6Sopenharmony_ci#include "buffer_util.h"
20c5e268c6Sopenharmony_ci#include "hdi_log.h"
21c5e268c6Sopenharmony_ci#include "native_buffer.h"
22c5e268c6Sopenharmony_ci
23c5e268c6Sopenharmony_cinamespace OHOS {
24c5e268c6Sopenharmony_cinamespace HDI {
25c5e268c6Sopenharmony_cinamespace Camera {
26c5e268c6Sopenharmony_cinamespace V1_0 {
27c5e268c6Sopenharmony_ciusing namespace OHOS::HDI::Base;
28c5e268c6Sopenharmony_ci
29c5e268c6Sopenharmony_ciclass BufferHandleSequenceable::BufferHandleWrap {
30c5e268c6Sopenharmony_cipublic:
31c5e268c6Sopenharmony_ci    explicit BufferHandleWrap(BufferHandle *bufferHandle = nullptr)
32c5e268c6Sopenharmony_ci    {
33c5e268c6Sopenharmony_ci        nativeBuffer_ = new (std::nothrow) NativeBuffer();
34c5e268c6Sopenharmony_ci        if (nativeBuffer_ == nullptr) {
35c5e268c6Sopenharmony_ci            HDI_CAMERA_LOGE("Native buffer object create failed.");
36c5e268c6Sopenharmony_ci            return;
37c5e268c6Sopenharmony_ci        }
38c5e268c6Sopenharmony_ci        nativeBuffer_->SetBufferHandle(bufferHandle);
39c5e268c6Sopenharmony_ci    }
40c5e268c6Sopenharmony_ci    sptr<NativeBuffer> nativeBuffer_;
41c5e268c6Sopenharmony_ci};
42c5e268c6Sopenharmony_ci
43c5e268c6Sopenharmony_ciBufferHandleSequenceable::BufferHandleSequenceable(const BufferHandle &bufferHandle)
44c5e268c6Sopenharmony_ci{
45c5e268c6Sopenharmony_ci    auto newBufferHandle = NewBufferHandle(bufferHandle.reserveFds, bufferHandle.reserveInts);
46c5e268c6Sopenharmony_ci    bufferHandleWrap_ = std::make_shared<BufferHandleWrap>(newBufferHandle);
47c5e268c6Sopenharmony_ci}
48c5e268c6Sopenharmony_ci
49c5e268c6Sopenharmony_ciBufferHandleSequenceable::BufferHandleSequenceable(BufferHandle *bufferHandle)
50c5e268c6Sopenharmony_ci{
51c5e268c6Sopenharmony_ci    bufferHandleWrap_ = std::make_shared<BufferHandleWrap>(bufferHandle);
52c5e268c6Sopenharmony_ci}
53c5e268c6Sopenharmony_ci
54c5e268c6Sopenharmony_ciBufferHandle* BufferHandleSequenceable::NewBufferHandle(uint32_t reserveFds, uint32_t reserveInts)
55c5e268c6Sopenharmony_ci{
56c5e268c6Sopenharmony_ci    return AllocateNativeBufferHandle(reserveFds, reserveInts);
57c5e268c6Sopenharmony_ci}
58c5e268c6Sopenharmony_ci
59c5e268c6Sopenharmony_civoid BufferHandleSequenceable::SetBufferHandle(BufferHandle *handle)
60c5e268c6Sopenharmony_ci{
61c5e268c6Sopenharmony_ci    if (bufferHandleWrap_->nativeBuffer_ != nullptr) {
62c5e268c6Sopenharmony_ci        bufferHandleWrap_->nativeBuffer_->SetBufferHandle(handle);
63c5e268c6Sopenharmony_ci    }
64c5e268c6Sopenharmony_ci}
65c5e268c6Sopenharmony_ciBufferHandle* BufferHandleSequenceable::GetBufferHandle()
66c5e268c6Sopenharmony_ci{
67c5e268c6Sopenharmony_ci    if (bufferHandleWrap_->nativeBuffer_ == nullptr) {
68c5e268c6Sopenharmony_ci        return nullptr;
69c5e268c6Sopenharmony_ci    }
70c5e268c6Sopenharmony_ci    return bufferHandleWrap_->nativeBuffer_->GetBufferHandle();
71c5e268c6Sopenharmony_ci}
72c5e268c6Sopenharmony_ci
73c5e268c6Sopenharmony_cibool BufferHandleSequenceable::Marshalling(Parcel &parcel) const
74c5e268c6Sopenharmony_ci{
75c5e268c6Sopenharmony_ci    if (bufferHandleWrap_->nativeBuffer_ == nullptr) {
76c5e268c6Sopenharmony_ci        return false;
77c5e268c6Sopenharmony_ci    }
78c5e268c6Sopenharmony_ci    return bufferHandleWrap_->nativeBuffer_->Marshalling(parcel);
79c5e268c6Sopenharmony_ci}
80c5e268c6Sopenharmony_ci
81c5e268c6Sopenharmony_cisptr<BufferHandleSequenceable> BufferHandleSequenceable::Unmarshalling(Parcel &parcel)
82c5e268c6Sopenharmony_ci{
83c5e268c6Sopenharmony_ci    sptr<BufferHandleSequenceable> sequenceObj(new BufferHandleSequenceable());
84c5e268c6Sopenharmony_ci    if (sequenceObj->bufferHandleWrap_ == nullptr) {
85c5e268c6Sopenharmony_ci        return nullptr;
86c5e268c6Sopenharmony_ci    }
87c5e268c6Sopenharmony_ci    sequenceObj->bufferHandleWrap_->nativeBuffer_ = NativeBuffer::Unmarshalling(parcel);
88c5e268c6Sopenharmony_ci    return sequenceObj;
89c5e268c6Sopenharmony_ci}
90c5e268c6Sopenharmony_ci
91c5e268c6Sopenharmony_ci} // V1_0
92c5e268c6Sopenharmony_ci} // Camera
93c5e268c6Sopenharmony_ci} // HDI
94c5e268c6Sopenharmony_ci} // OHOS