1 /* 2 * Copyright (c) 2021 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 16 #include "buffer_allocator_factory.h" 17 18 namespace OHOS::Camera { GetInstance()19BufferAllocatorFactory *BufferAllocatorFactory::GetInstance() 20 { 21 static BufferAllocatorFactory factory; 22 return &factory; 23 } 24 25 std::shared_ptr<IBufferAllocator> GetBufferAllocator(const int32_t type)26 BufferAllocatorFactory::GetBufferAllocator(const int32_t type) 27 { 28 std::lock_guard<std::mutex> l(lock_); 29 30 if (bufferAllocatorMap_.find(type) == bufferAllocatorMap_.end()) { 31 return CreateBufferAllocator(type); 32 } 33 34 if (bufferAllocatorMap_[type].expired()) { 35 return CreateBufferAllocator(type); 36 } 37 38 return bufferAllocatorMap_[type].lock(); 39 } 40 CreateBufferAllocator(const int32_t type)41std::shared_ptr<IBufferAllocator> BufferAllocatorFactory::CreateBufferAllocator(const int32_t type) 42 { 43 if (allocatorRegisterMap_.find(type) == allocatorRegisterMap_.end()) { 44 return nullptr; 45 } 46 47 std::shared_ptr<IBufferAllocator> allocator(allocatorRegisterMap_[type]()); 48 bufferAllocatorMap_[type] = allocator; 49 return allocator; 50 } 51 } 52