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_manager.h" 17 #include <sys/time.h> 18 #include "buffer_pool.h" 19 20 namespace OHOS::Camera { GetInstance()21BufferManager* BufferManager::GetInstance() 22 { 23 static BufferManager manager; 24 return &manager; 25 } 26 GenerateBufferPoolId()27uint64_t BufferManager::GenerateBufferPoolId() 28 { 29 std::lock_guard<std::mutex> l(lock_); 30 31 struct timeval tv; 32 gettimeofday(&tv, NULL); 33 uint64_t id = static_cast<uint64_t>(tv.tv_sec) * 1000 * 1000 + tv.tv_usec; // 1000:usec 34 35 std::shared_ptr<IBufferPool> bufferPool = nullptr; 36 bufferPoolMap_[id] = bufferPool; 37 38 return id; 39 } 40 GetBufferPool(uint64_t id)41std::shared_ptr<IBufferPool> BufferManager::GetBufferPool(uint64_t id) 42 { 43 std::lock_guard<std::mutex> l(lock_); 44 45 if (bufferPoolMap_.find(id) == bufferPoolMap_.end()) { 46 return nullptr; 47 } 48 49 if (bufferPoolMap_[id].expired()) { 50 std::shared_ptr<IBufferPool> bufferPool = std::make_shared<BufferPool>(); 51 bufferPoolMap_[id] = bufferPool; 52 bufferPool->SetId(id); 53 return bufferPool; 54 } 55 56 return bufferPoolMap_[id].lock(); 57 } 58 EraseBufferPoolMapById(uint64_t id)59void BufferManager::EraseBufferPoolMapById(uint64_t id) 60 { 61 std::lock_guard<std::mutex> l(lock_); 62 auto findIter = bufferPoolMap_.find(id); 63 if (findIter != bufferPoolMap_.end()) { 64 bufferPoolMap_.erase(findIter); 65 } 66 } 67 } // namespace OHOS::Camera 68