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 "allocator_manager.h"
19 #include "display_common.h"
20 #include "framebuffer_allocator.h"
21 #include "dmabufferheap_allocator.h"
22 #include "drm_allocator.h"
23
24 namespace OHOS {
25 namespace HDI {
26 namespace DISPLAY {
Init()27 int32_t AllocatorManager::Init()
28 {
29 DISPLAY_LOGD("AllocatorManager::Init");
30 if (init_) {
31 DISPLAY_LOGW("allocator has initialized");
32 return DISPLAY_SUCCESS;
33 }
34 // first use drm allocator
35 std::shared_ptr<Allocator> drmAllocator = std::make_shared<DrmAllocator>();
36 int ret = drmAllocator->Init();
37 if (ret == DISPLAY_SUCCESS) {
38 frameBufferAllocator_ = drmAllocator;
39 allocator_ = drmAllocator;
40 } else {
41 std::shared_ptr<Allocator> fbAllocator = std::make_shared<FramebufferAllocator>();
42 ret = fbAllocator->Init();
43 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
44 DISPLAY_LOGE("Failed init framebuffer allocator"));
45
46 std::shared_ptr<Allocator> dmaBufferallocator = std::make_shared<DmaBufferHeapAllocator>();
47 ret = dmaBufferallocator->Init();
48 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
49 DISPLAY_LOGE("Failed init Dmabuffer allocator"));
50
51 frameBufferAllocator_ = fbAllocator;
52 allocator_ = dmaBufferallocator;
53 }
54
55 init_ = true;
56 DISPLAY_LOGD("init success");
57 return DISPLAY_SUCCESS;
58 }
59
DeInit()60 int32_t AllocatorManager::DeInit()
61 {
62 DISPLAY_LOGD();
63 init_ = false;
64 frameBufferAllocator_.reset();
65 allocator_.reset();
66 return DISPLAY_SUCCESS;
67 }
68
GetAllocator(uint64_t usage)69 Allocator* AllocatorManager::GetAllocator(uint64_t usage)
70 {
71 DISPLAY_LOGD();
72 if ((usage & HBM_USE_MEM_FB) != 0) {
73 return frameBufferAllocator_.get();
74 } else {
75 return allocator_.get();
76 }
77 }
78 } // namespace DISPLAY
79 } // namespace HDI
80 } // namespace OHOS