1 /*
2  * Copyright (c) 2022 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 #include "dmabufferheap_allocator.h"
16 #include <cerrno>
17 #include "display_common.h"
18 #include "dmabuf_alloc.h"
19 namespace OHOS {
20 namespace HDI {
21 namespace DISPLAY {
Init()22 int32_t DmaBufferHeapAllocator::Init()
23 {
24     DISPLAY_LOGD();
25     static constexpr const char *HEAP_NAME = "system";
26     deviceFd_ = DmabufHeapOpen(HEAP_NAME);
27     if (deviceFd_ < 0) {
28         DISPLAY_LOGE("Failed to open dmabufferheap errno %{public}d", errno);
29         return DISPLAY_FAILURE;
30     }
31     return DISPLAY_SUCCESS;
32 }
33 
Allocate(const BufferInfo & bufferInfo,BufferHandle & handle)34 int32_t DmaBufferHeapAllocator::Allocate(const BufferInfo &bufferInfo, BufferHandle &handle)
35 {
36     DISPLAY_LOGD();
37     DmabufHeapBuffer buffer;
38     buffer.size = bufferInfo.size_;
39     buffer.heapFlags = 0;
40     DmabufHeapBufferAlloc(deviceFd_, &buffer);
41     handle.fd = buffer.fd;
42     return DISPLAY_SUCCESS;
43 }
44 
~DmaBufferHeapAllocator()45 DmaBufferHeapAllocator::~DmaBufferHeapAllocator()
46 {
47     DISPLAY_LOGD();
48     if (deviceFd_ >= 0) {
49         DmabufHeapClose(deviceFd_);
50         deviceFd_ = -1;
51     }
52 }
53 } // namespace DISPLAY
54 } // namespace HDI
55 } // namespace OHOS
56