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