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 "heap_buffer_allocator.h"
17 #include "image_buffer.h"
18
19 namespace OHOS::Camera {
HeapBufferAllocator()20 HeapBufferAllocator::HeapBufferAllocator()
21 {
22 CAMERA_LOGD("buffer allocator construct, instance = %{public}p", this);
23 }
24
~HeapBufferAllocator()25 HeapBufferAllocator::~HeapBufferAllocator() {}
26
Init()27 RetCode HeapBufferAllocator::Init()
28 {
29 return RC_OK;
30 }
31
AllocBuffer(const uint32_t width,const uint32_t height,const uint64_t cameraUsage,const uint32_t cameraFormat)32 std::shared_ptr<IBuffer> HeapBufferAllocator::AllocBuffer(const uint32_t width,
33 const uint32_t height,
34 const uint64_t cameraUsage,
35 const uint32_t cameraFormat)
36 {
37 uint32_t size = CalculateSize(width, height, cameraUsage, cameraFormat);
38 char* heap = nullptr;
39 if (size > 0) {
40 heap = new (std::nothrow) char[size];
41 }
42 if (heap == nullptr) {
43 CAMERA_LOGE("Alloc buffer failed");
44 return nullptr;
45 }
46 std::shared_ptr<IBuffer> buffer = std::make_shared<ImageBuffer>(sourceType_);
47 if (buffer != nullptr) {
48 buffer->SetSize(size);
49 buffer->SetUsage(cameraUsage);
50 buffer->SetVirAddress(heap);
51 buffer->SetStride(width);
52 buffer->SetWidth(width);
53 buffer->SetHeight(height);
54 buffer->SetFormat(cameraFormat);
55 CAMERA_LOGD("Alloc buffer succeed to shared memory segment size:%{public}d.", size);
56 } else {
57 delete[] heap;
58 heap = nullptr;
59 CAMERA_LOGE("Alloc buffer failed to shared memory segment.");
60 }
61 return buffer;
62 }
63
FreeBuffer(std::shared_ptr<IBuffer> & buffer)64 RetCode HeapBufferAllocator::FreeBuffer(std::shared_ptr<IBuffer>& buffer)
65 {
66 if (buffer->GetSourceType() != sourceType_) {
67 return RC_ERROR;
68 }
69
70 char* addr = reinterpret_cast<char*>(buffer->GetVirAddress());
71 if (addr != nullptr) {
72 delete[] addr;
73 addr = nullptr;
74 }
75 buffer->Free();
76 return RC_OK;
77 }
78
MapBuffer(std::shared_ptr<IBuffer> &)79 RetCode HeapBufferAllocator::MapBuffer(std::shared_ptr<IBuffer>&)
80 {
81 return RC_OK;
82 }
83
UnmapBuffer(std::shared_ptr<IBuffer> &)84 RetCode HeapBufferAllocator::UnmapBuffer(std::shared_ptr<IBuffer>&)
85 {
86 return RC_OK;
87 }
88
FlushCache(std::shared_ptr<IBuffer> &)89 RetCode HeapBufferAllocator::FlushCache(std::shared_ptr<IBuffer>&)
90 {
91 return RC_OK;
92 }
93
InvalidateCache(std::shared_ptr<IBuffer> &)94 RetCode HeapBufferAllocator::InvalidateCache(std::shared_ptr<IBuffer>&)
95 {
96 return RC_OK;
97 }
98
CalculateSize(const uint32_t width,const uint32_t height,const uint64_t usage,const uint32_t format) const99 uint32_t HeapBufferAllocator::CalculateSize(const uint32_t width,
100 const uint32_t height,
101 const uint64_t usage,
102 const uint32_t format) const
103 {
104 (void)usage;
105 switch (format) {
106 case CAMERA_FORMAT_RGB_565:
107 case CAMERA_FORMAT_RGBA_5658:
108 case CAMERA_FORMAT_RGBX_4444:
109 case CAMERA_FORMAT_RGBA_4444:
110 case CAMERA_FORMAT_RGB_444:
111 case CAMERA_FORMAT_RGBX_5551:
112 case CAMERA_FORMAT_RGBA_5551:
113 case CAMERA_FORMAT_RGB_555:
114 case CAMERA_FORMAT_RGBX_8888:
115 case CAMERA_FORMAT_RGBA_8888:
116 case CAMERA_FORMAT_RGB_888:
117 case CAMERA_FORMAT_BGR_565:
118 case CAMERA_FORMAT_BGRX_4444:
119 case CAMERA_FORMAT_BGRA_4444:
120 case CAMERA_FORMAT_BGRX_5551:
121 case CAMERA_FORMAT_BGRA_5551:
122 case CAMERA_FORMAT_BGRX_8888:
123 case CAMERA_FORMAT_BGRA_8888:
124 break;
125 case CAMERA_FORMAT_YCBCR_420_SP:
126 case CAMERA_FORMAT_YCRCB_420_SP:
127 case CAMERA_FORMAT_YCRCB_422_P:
128 case CAMERA_FORMAT_YCBCR_420_P:
129 case CAMERA_FORMAT_YCRCB_420_P:
130 /*
131 Fixed calculation formula of yuv
132 yuv420 size= w * h * 3 / 2
133 */
134 return width * height * 3 / 2; // 3:Fixed calculated value of yuv 2:Fixed calculated value of yuv
135 break;
136 case CAMERA_FORMAT_YCBCR_422_P:
137 case CAMERA_FORMAT_YUV_422_I:
138 case CAMERA_FORMAT_YCBCR_422_SP:
139 case CAMERA_FORMAT_YCRCB_422_SP:
140 case CAMERA_FORMAT_YUYV_422_PKG:
141 case CAMERA_FORMAT_UYVY_422_PKG:
142 case CAMERA_FORMAT_YVYU_422_PKG:
143 case CAMERA_FORMAT_VYUY_422_PKG:
144 /*
145 Fixed calculation formula of yuv
146 yuv422 size= w * h * 2
147 */
148 return width * height * 2; // 2:Fixed calculated value of yuv
149 break;
150 default:
151 break;
152 }
153 return 0;
154 }
155 REGISTER_BUFFER_ALLOCATOR(HeapBufferAllocator, CAMERA_BUFFER_SOURCE_TYPE_HEAP);
156 } // namespace OHOS::Camera
157
158