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