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()24int32_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 DISPLAY_LOGD("init success"); 54 return DISPLAY_SUCCESS; 55 } 56 DeInit()57int32_t AllocatorManager::DeInit() 58 { 59 DISPLAY_LOGD(); 60 init_ = false; 61 frameBufferAllocator_.reset(); 62 allocator_.reset(); 63 return DISPLAY_SUCCESS; 64 } 65 GetAllocator(uint64_t usage)66Allocator* AllocatorManager::GetAllocator(uint64_t usage) 67 { 68 DISPLAY_LOGD(); 69 if ((usage & HBM_USE_MEM_FB) != 0) { 70 return frameBufferAllocator_.get(); 71 } else { 72 return allocator_.get(); 73 } 74 } 75 } // namespace DISPLAY 76 } // namespace HDI 77 } // namespace OHOS