• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_utils.h"
17 #include "buffer_allocator_factory.h"
18 
19 namespace OHOS::Camera {
GetBufferAllocator(const int32_t source)20 std::shared_ptr<IBufferAllocator> BufferAllocatorUtils::GetBufferAllocator(const int32_t source)
21 {
22     if (source == CAMERA_BUFFER_SOURCE_TYPE_NONE) {
23         return nullptr;
24     }
25 
26     if (source == CAMERA_BUFFER_SOURCE_TYPE_EXTERNAL) {
27         return nullptr;
28     }
29 
30     auto factory = BufferAllocatorFactory::GetInstance();
31     if (factory == nullptr) {
32         CAMERA_LOGE("can't get factory, alloc failed.");
33         return nullptr;
34     }
35 
36     auto allocator = factory->GetBufferAllocator(source);
37     if (allocator == nullptr) {
38         CAMERA_LOGE("fatal error, can't get allocator, alloc failed.");
39         return nullptr;
40     }
41 
42     if (allocator->Init() != RC_OK) {
43         return nullptr;
44     }
45 
46     return allocator;
47 }
48 
GetAllocator(std::shared_ptr<IBuffer> & buffer)49 std::shared_ptr<IBufferAllocator> BufferAllocatorUtils::GetAllocator(std::shared_ptr<IBuffer>& buffer)
50 {
51     if (buffer == nullptr) {
52         CAMERA_LOGE("buffer is nullptr");
53         return nullptr;
54     }
55 
56     int32_t sourceType = buffer->GetSourceType();
57     return GetBufferAllocator(sourceType);
58 }
59 
AllocBuffer(const int32_t source,const uint32_t width,const uint32_t height,const uint64_t usage,const uint32_t format)60 std::shared_ptr<IBuffer> BufferAllocatorUtils::AllocBuffer(const int32_t source,
61                                                            const uint32_t width,
62                                                            const uint32_t height,
63                                                            const uint64_t usage,
64                                                            const uint32_t format)
65 {
66     auto allocator = GetBufferAllocator(source);
67     CHECK_IF_PTR_NULL_RETURN_VALUE(allocator, nullptr);
68 
69     return allocator->AllocBuffer(width, height, usage, format);
70 }
71 
FreeBuffer(std::shared_ptr<IBuffer> & buffer)72 RetCode BufferAllocatorUtils::FreeBuffer(std::shared_ptr<IBuffer>& buffer)
73 {
74     auto allocator = GetAllocator(buffer);
75     CHECK_IF_PTR_NULL_RETURN_VALUE(allocator, RC_ERROR);
76     return allocator->FreeBuffer(buffer);
77 }
78 
MapBuffer(std::shared_ptr<IBuffer> & buffer)79 RetCode BufferAllocatorUtils::MapBuffer(std::shared_ptr<IBuffer>& buffer)
80 {
81     auto allocator = GetAllocator(buffer);
82     CHECK_IF_PTR_NULL_RETURN_VALUE(allocator, RC_ERROR);
83     return allocator->MapBuffer(buffer);
84 }
85 
UnmapBuffer(std::shared_ptr<IBuffer> & buffer)86 RetCode BufferAllocatorUtils::UnmapBuffer(std::shared_ptr<IBuffer>& buffer)
87 {
88     auto allocator = GetAllocator(buffer);
89     CHECK_IF_PTR_NULL_RETURN_VALUE(allocator, RC_ERROR);
90     return allocator->UnmapBuffer(buffer);
91 }
92 
FlushCache(std::shared_ptr<IBuffer> & buffer)93 RetCode BufferAllocatorUtils::FlushCache(std::shared_ptr<IBuffer>& buffer)
94 {
95     auto allocator = GetAllocator(buffer);
96     CHECK_IF_PTR_NULL_RETURN_VALUE(allocator, RC_ERROR);
97     return allocator->FlushCache(buffer);
98 }
99 
InvalidateCache(std::shared_ptr<IBuffer> & buffer)100 RetCode BufferAllocatorUtils::InvalidateCache(std::shared_ptr<IBuffer>& buffer)
101 {
102     auto allocator = GetAllocator(buffer);
103     CHECK_IF_PTR_NULL_RETURN_VALUE(allocator, RC_ERROR);
104     return allocator->InvalidateCache(buffer);
105 }
106 } // namespace OHOS::Camera
107 
108