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