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");
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 buffer->Free();
71 return RC_OK;
72 }
73
MapBuffer(std::shared_ptr<IBuffer> &)74 RetCode HeapBufferAllocator::MapBuffer(std::shared_ptr<IBuffer>&)
75 {
76 return RC_OK;
77 }
78
UnmapBuffer(std::shared_ptr<IBuffer> &)79 RetCode HeapBufferAllocator::UnmapBuffer(std::shared_ptr<IBuffer>&)
80 {
81 return RC_OK;
82 }
83
FlushCache(std::shared_ptr<IBuffer> &)84 RetCode HeapBufferAllocator::FlushCache(std::shared_ptr<IBuffer>&)
85 {
86 return RC_OK;
87 }
88
InvalidateCache(std::shared_ptr<IBuffer> &)89 RetCode HeapBufferAllocator::InvalidateCache(std::shared_ptr<IBuffer>&)
90 {
91 return RC_OK;
92 }
93
CalculateSize(const uint32_t width,const uint32_t height,const uint64_t usage,const uint32_t format) const94 uint32_t HeapBufferAllocator::CalculateSize(const uint32_t width,
95 const uint32_t height,
96 const uint64_t usage,
97 const uint32_t format) const
98 {
99 (void)usage;
100 switch (format) {
101 case CAMERA_FORMAT_RGB_565:
102 case CAMERA_FORMAT_RGBA_5658:
103 case CAMERA_FORMAT_RGBX_4444:
104 case CAMERA_FORMAT_RGBA_4444:
105 case CAMERA_FORMAT_RGB_444:
106 case CAMERA_FORMAT_RGBX_5551:
107 case CAMERA_FORMAT_RGBA_5551:
108 case CAMERA_FORMAT_RGB_555:
109 case CAMERA_FORMAT_RGBX_8888:
110 case CAMERA_FORMAT_RGBA_8888:
111 case CAMERA_FORMAT_RGB_888:
112 case CAMERA_FORMAT_BGR_565:
113 case CAMERA_FORMAT_BGRX_4444:
114 case CAMERA_FORMAT_BGRA_4444:
115 case CAMERA_FORMAT_BGRX_5551:
116 case CAMERA_FORMAT_BGRA_5551:
117 case CAMERA_FORMAT_BGRX_8888:
118 case CAMERA_FORMAT_BGRA_8888:
119 break;
120 case CAMERA_FORMAT_YCBCR_420_SP:
121 case CAMERA_FORMAT_YCRCB_420_SP:
122 case CAMERA_FORMAT_YCRCB_422_P:
123 case CAMERA_FORMAT_YCBCR_420_P:
124 case CAMERA_FORMAT_YCRCB_420_P:
125 /*
126 Fixed calculation formula of yuv
127 yuv420 size= w * h * 3 / 2
128 */
129 return width * height * 3 / 2; // 3:Fixed calculated value of yuv 2:Fixed calculated value of yuv
130 case CAMERA_FORMAT_YCBCR_422_P:
131 case CAMERA_FORMAT_YUV_422_I:
132 case CAMERA_FORMAT_YCBCR_422_SP:
133 case CAMERA_FORMAT_YCRCB_422_SP:
134 case CAMERA_FORMAT_YUYV_422_PKG:
135 case CAMERA_FORMAT_UYVY_422_PKG:
136 case CAMERA_FORMAT_YVYU_422_PKG:
137 case CAMERA_FORMAT_VYUY_422_PKG:
138 /*
139 Fixed calculation formula of yuv
140 yuv422 size= w * h * 2
141 */
142 return width * height * 2; // 2:Fixed calculated value of yuv
143 default:
144 break;
145 }
146 return 0;
147 }
148 REGISTER_BUFFER_ALLOCATOR(HeapBufferAllocator, CAMERA_BUFFER_SOURCE_TYPE_HEAP);
149 } // namespace OHOS::Camera
150
151