1 /*
2 * Copyright (c) 2015 - 2017, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <gralloc_priv.h>
31 #include <memalloc.h>
32 #include <gr.h>
33 #include <alloc_controller.h>
34 #include <utils/constants.h>
35 #include <utils/debug.h>
36 #include <core/buffer_allocator.h>
37
38 #include "hwc_debugger.h"
39 #include "hwc_buffer_allocator.h"
40
41 #define __CLASS__ "HWCBufferAllocator"
42
43 namespace sdm {
44
HWCBufferAllocator()45 HWCBufferAllocator::HWCBufferAllocator() {
46 alloc_controller_ = gralloc::IAllocController::getInstance();
47 }
48
AllocateBuffer(BufferInfo * buffer_info)49 DisplayError HWCBufferAllocator::AllocateBuffer(BufferInfo *buffer_info) {
50 gralloc::alloc_data data;
51
52 const BufferConfig &buffer_config = buffer_info->buffer_config;
53 AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
54 MetaBufferInfo *meta_buffer_info = new MetaBufferInfo();
55
56 if (!meta_buffer_info) {
57 return kErrorMemory;
58 }
59
60 int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
61 int error = 0;
62
63 int width = INT(buffer_config.width);
64 int height = INT(buffer_config.height);
65 int format;
66
67 if (buffer_config.secure_camera) {
68 alloc_flags = GRALLOC_USAGE_HW_CAMERA_WRITE;
69 alloc_flags |= (GRALLOC_USAGE_PROTECTED | GRALLOC_USAGE_HW_COMPOSER);
70 data.align = SZ_2M;
71 } else if (buffer_config.secure) {
72 alloc_flags = INT(GRALLOC_USAGE_PRIVATE_MM_HEAP);
73 alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
74 data.align = SECURE_ALIGN;
75 } else {
76 data.align = UINT32(getpagesize());
77 }
78
79 if (buffer_config.cache == false) {
80 // Allocate uncached buffers
81 alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
82 }
83
84 error = SetBufferInfo(buffer_config.format, &format, &alloc_flags);
85 if (error != 0) {
86 delete meta_buffer_info;
87 return kErrorParameters;
88 }
89
90 int aligned_width = 0, aligned_height = 0;
91 uint32_t buffer_size = getBufferSizeAndDimensions(width, height, format, alloc_flags,
92 aligned_width, aligned_height);
93
94 buffer_size = ROUND_UP(buffer_size, data.align) * buffer_config.buffer_count;
95
96 data.base = 0;
97 data.fd = -1;
98 data.offset = 0;
99 data.size = buffer_size;
100 data.uncached = !buffer_config.cache;
101
102 error = alloc_controller_->allocate(data, alloc_flags);
103 if (error != 0) {
104 DLOGE("Error allocating memory size %d uncached %d", data.size, data.uncached);
105 delete meta_buffer_info;
106 return kErrorMemory;
107 }
108
109 alloc_buffer_info->fd = data.fd;
110 // TODO(user): define stride for all planes and fix stride in bytes
111 alloc_buffer_info->stride = UINT32(aligned_width);
112 alloc_buffer_info->aligned_width = UINT32(aligned_width);
113 alloc_buffer_info->aligned_height = UINT32(aligned_height);
114 alloc_buffer_info->size = buffer_size;
115
116 meta_buffer_info->base_addr = data.base;
117 meta_buffer_info->alloc_type = data.allocType;
118
119 buffer_info->private_data = meta_buffer_info;
120
121 return kErrorNone;
122 }
123
FreeBuffer(BufferInfo * buffer_info)124 DisplayError HWCBufferAllocator::FreeBuffer(BufferInfo *buffer_info) {
125 int ret = 0;
126 AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
127
128 // Deallocate the buffer, only if the buffer fd is valid.
129 if (alloc_buffer_info->fd > 0) {
130 MetaBufferInfo *meta_buffer_info = static_cast<MetaBufferInfo *> (buffer_info->private_data);
131 gralloc::IMemAlloc *memalloc = alloc_controller_->getAllocator(meta_buffer_info->alloc_type);
132 if (memalloc == NULL) {
133 DLOGE("Memalloc handle is NULL, alloc type %d", meta_buffer_info->alloc_type);
134 return kErrorResources;
135 }
136
137 ret = memalloc->free_buffer(meta_buffer_info->base_addr, alloc_buffer_info->size, 0,
138 alloc_buffer_info->fd);
139 if (ret != 0) {
140 DLOGE("Error freeing buffer base_addr %p size %d fd %d", meta_buffer_info->base_addr,
141 alloc_buffer_info->size, alloc_buffer_info->fd);
142 return kErrorMemory;
143 }
144
145 alloc_buffer_info->fd = -1;
146 alloc_buffer_info->stride = 0;
147 alloc_buffer_info->aligned_width = 0;
148 alloc_buffer_info->aligned_height = 0;
149 alloc_buffer_info->size = 0;
150
151 meta_buffer_info->base_addr = NULL;
152 meta_buffer_info->alloc_type = 0;
153
154 delete meta_buffer_info;
155 meta_buffer_info = NULL;
156 }
157
158 return kErrorNone;
159 }
160
GetBufferSize(BufferInfo * buffer_info)161 uint32_t HWCBufferAllocator::GetBufferSize(BufferInfo *buffer_info) {
162 uint32_t align = UINT32(getpagesize());
163
164 const BufferConfig &buffer_config = buffer_info->buffer_config;
165
166 int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
167
168 int width = INT(buffer_config.width);
169 int height = INT(buffer_config.height);
170 int format;
171
172 if (buffer_config.secure_camera) {
173 alloc_flags = GRALLOC_USAGE_HW_CAMERA_WRITE;
174 alloc_flags |= (GRALLOC_USAGE_PROTECTED | GRALLOC_USAGE_HW_COMPOSER);
175 align = SZ_2M;
176 } else if (buffer_config.secure) {
177 alloc_flags = INT(GRALLOC_USAGE_PRIVATE_MM_HEAP);
178 alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
179 align = SECURE_ALIGN;
180 }
181
182 if (buffer_config.cache == false) {
183 // Allocate uncached buffers
184 alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
185 }
186
187 if (SetBufferInfo(buffer_config.format, &format, &alloc_flags) < 0) {
188 return 0;
189 }
190
191 int aligned_width = 0;
192 int aligned_height = 0;
193 uint32_t buffer_size = getBufferSizeAndDimensions(width, height, format, alloc_flags,
194 aligned_width, aligned_height);
195
196 buffer_size = ROUND_UP(buffer_size, align) * buffer_config.buffer_count;
197
198 return buffer_size;
199 }
200
SetBufferInfo(LayerBufferFormat format,int * target,int * flags)201 int HWCBufferAllocator::SetBufferInfo(LayerBufferFormat format, int *target, int *flags) {
202 switch (format) {
203 case kFormatRGBA8888: *target = HAL_PIXEL_FORMAT_RGBA_8888; break;
204 case kFormatRGBX8888: *target = HAL_PIXEL_FORMAT_RGBX_8888; break;
205 case kFormatRGB888: *target = HAL_PIXEL_FORMAT_RGB_888; break;
206 case kFormatRGB565: *target = HAL_PIXEL_FORMAT_RGB_565; break;
207 case kFormatBGR565: *target = HAL_PIXEL_FORMAT_BGR_565; break;
208 case kFormatBGRA8888: *target = HAL_PIXEL_FORMAT_BGRA_8888; break;
209 case kFormatYCrCb420PlanarStride16: *target = HAL_PIXEL_FORMAT_YV12; break;
210 case kFormatYCrCb420SemiPlanar: *target = HAL_PIXEL_FORMAT_YCrCb_420_SP; break;
211 case kFormatYCbCr420SemiPlanar: *target = HAL_PIXEL_FORMAT_YCbCr_420_SP; break;
212 case kFormatYCbCr422H2V1Packed: *target = HAL_PIXEL_FORMAT_YCbCr_422_I; break;
213 case kFormatCbYCrY422H2V1Packed: *target = HAL_PIXEL_FORMAT_CbYCrY_422_I; break;
214 case kFormatYCbCr422H2V1SemiPlanar: *target = HAL_PIXEL_FORMAT_YCbCr_422_SP; break;
215 case kFormatYCbCr420SemiPlanarVenus: *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; break;
216 case kFormatYCrCb420SemiPlanarVenus: *target = HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS; break;
217 case kFormatYCbCr420SPVenusUbwc:
218 *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
219 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
220 break;
221 case kFormatRGBA5551: *target = HAL_PIXEL_FORMAT_RGBA_5551; break;
222 case kFormatRGBA4444: *target = HAL_PIXEL_FORMAT_RGBA_4444; break;
223 case kFormatRGBA1010102: *target = HAL_PIXEL_FORMAT_RGBA_1010102; break;
224 case kFormatARGB2101010: *target = HAL_PIXEL_FORMAT_ARGB_2101010; break;
225 case kFormatRGBX1010102: *target = HAL_PIXEL_FORMAT_RGBX_1010102; break;
226 case kFormatXRGB2101010: *target = HAL_PIXEL_FORMAT_XRGB_2101010; break;
227 case kFormatBGRA1010102: *target = HAL_PIXEL_FORMAT_BGRA_1010102; break;
228 case kFormatABGR2101010: *target = HAL_PIXEL_FORMAT_ABGR_2101010; break;
229 case kFormatBGRX1010102: *target = HAL_PIXEL_FORMAT_BGRX_1010102; break;
230 case kFormatXBGR2101010: *target = HAL_PIXEL_FORMAT_XBGR_2101010; break;
231 case kFormatYCbCr420P010: *target = HAL_PIXEL_FORMAT_YCbCr_420_P010; break;
232 case kFormatYCbCr420TP10Ubwc:
233 *target = HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC;
234 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
235 break;
236 case kFormatYCbCr420P010Ubwc:
237 *target = HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC;
238 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
239 break;
240 case kFormatRGBA8888Ubwc:
241 *target = HAL_PIXEL_FORMAT_RGBA_8888;
242 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
243 break;
244 case kFormatRGBX8888Ubwc:
245 *target = HAL_PIXEL_FORMAT_RGBX_8888;
246 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
247 break;
248 case kFormatBGR565Ubwc:
249 *target = HAL_PIXEL_FORMAT_BGR_565;
250 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
251 break;
252 case kFormatRGBA1010102Ubwc:
253 *target = HAL_PIXEL_FORMAT_RGBA_1010102;
254 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
255 break;
256 case kFormatRGBX1010102Ubwc:
257 *target = HAL_PIXEL_FORMAT_RGBX_1010102;
258 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
259 break;
260 default:
261 DLOGE("Unsupported format = 0x%x", format);
262 return -EINVAL;
263 }
264
265 return 0;
266 }
267
GetAllocatedBufferInfo(const BufferConfig & buffer_config,AllocatedBufferInfo * allocated_buffer_info)268 DisplayError HWCBufferAllocator::GetAllocatedBufferInfo(const BufferConfig &buffer_config,
269 AllocatedBufferInfo *allocated_buffer_info) {
270 int width = INT(buffer_config.width);
271 int height = INT(buffer_config.height);
272 int alloc_flags = INT(GRALLOC_USAGE_PRIVATE_IOMMU_HEAP);
273
274 if (buffer_config.secure) {
275 alloc_flags = INT(GRALLOC_USAGE_PRIVATE_MM_HEAP);
276 alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
277 }
278
279 if (buffer_config.cache == false) {
280 // Allocate uncached buffers
281 alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
282 }
283
284 int format;
285 int error = SetBufferInfo(buffer_config.format, &format, &alloc_flags);
286 if (error) {
287 DLOGE("Failed: format = %d or width = %d height = %d", buffer_config.format, width, height);
288 return kErrorNotSupported;
289 }
290
291 int width_aligned = 0, height_aligned = 0;
292 uint32_t buffer_size = 0;
293 buffer_size = getBufferSizeAndDimensions(width, height, format, alloc_flags,
294 width_aligned, height_aligned);
295
296 allocated_buffer_info->stride = UINT32(width_aligned);
297 allocated_buffer_info->aligned_width = UINT32(width_aligned);
298 allocated_buffer_info->aligned_height = UINT32(height_aligned);
299 allocated_buffer_info->size = UINT32(buffer_size);
300 allocated_buffer_info->format = buffer_config.format;
301
302 return kErrorNone;
303 }
304
GetBufferLayout(const AllocatedBufferInfo & buf_info,uint32_t stride[4],uint32_t offset[4],uint32_t * num_planes)305 DisplayError HWCBufferAllocator::GetBufferLayout(const AllocatedBufferInfo &buf_info,
306 uint32_t stride[4], uint32_t offset[4],
307 uint32_t *num_planes) {
308 private_handle_t hnd(-1, 0, 0, 0, 0, 0, 0);
309 int format = HAL_PIXEL_FORMAT_RGBA_8888;
310 int flags = 0;
311
312 SetBufferInfo(buf_info.format, &format, &flags);
313 // Setup only the required stuff, skip rest
314 hnd.format = format;
315 hnd.width = buf_info.aligned_width;
316 hnd.height = buf_info.aligned_height;
317 if (flags & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) {
318 hnd.flags = private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
319 }
320
321 int ret = getBufferLayout(&hnd, stride, offset, num_planes);
322 if (ret < 0) {
323 DLOGE("getBufferLayout failed");
324 return kErrorParameters;
325 }
326
327 return kErrorNone;
328 }
329
330 } // namespace sdm
331