1 /*
2 * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3 * This file contains confidential and proprietary information of
4 * OSWare Technology Co., Ltd
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 #include "dmabufferheap_allocator.h"
19 #include <cerrno>
20 #include "display_common.h"
21 #include "dmabuf_alloc.h"
22 namespace OHOS {
23 namespace HDI {
24 namespace DISPLAY {
Init()25 int32_t DmaBufferHeapAllocator::Init()
26 {
27 DISPLAY_LOGD();
28 static constexpr const char *HEAP_NAME = "system";
29 deviceFd_ = DmabufHeapOpen(HEAP_NAME);
30 if (deviceFd_ < 0) {
31 DISPLAY_LOGE("Failed to open dmabufferheap errno %{public}d", errno);
32 return DISPLAY_FAILURE;
33 }
34 return DISPLAY_SUCCESS;
35 }
36
Allocate(const BufferInfo & bufferInfo,BufferHandle & handle)37 int32_t DmaBufferHeapAllocator::Allocate(const BufferInfo &bufferInfo, BufferHandle &handle)
38 {
39 DISPLAY_LOGD();
40 DmabufHeapBuffer buffer;
41 buffer.size = bufferInfo.size_;
42 DmabufHeapBufferAlloc(deviceFd_, &buffer);
43 handle.fd = buffer.fd;
44 return DISPLAY_SUCCESS;
45 }
46
~DmaBufferHeapAllocator()47 DmaBufferHeapAllocator::~DmaBufferHeapAllocator()
48 {
49 DISPLAY_LOGD();
50 if (deviceFd_ >= 0) {
51 DmabufHeapClose(deviceFd_);
52 deviceFd_ = -1;
53 }
54 }
55 } // namespace DISPLAY
56 } // namespace HDI
57 } // namespace OHOS