1 /*
2 * Copyright (c) 2015-2020, 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
32 #include <core/buffer_allocator.h>
33 #include <utils/constants.h>
34 #include <utils/debug.h>
35
36 #include "gr_utils.h"
37 #include "hwc_buffer_allocator.h"
38 #include "hwc_debugger.h"
39 #include "hwc_layers.h"
40
41 #define __CLASS__ "HWCBufferAllocator"
42
43 using android::hardware::graphics::mapper::V2_0::Error;
44 using MapperV3Error = android::hardware::graphics::mapper::V3_0::Error;
45 using android::hardware::graphics::mapper::V2_0::BufferDescriptor;
46 using MapperV3BufferDescriptor = android::hardware::graphics::mapper::V3_0::BufferDescriptor;
47 using android::hardware::hidl_handle;
48 using android::hardware::hidl_vec;
49
50 namespace sdm {
51
GetGrallocInstance()52 DisplayError HWCBufferAllocator::GetGrallocInstance() {
53 // Lazy initialization of gralloc HALs
54 if (mapper_V3_ != nullptr || mapper_V2_ != nullptr || allocator_V3_ != nullptr ||
55 allocator_V2_ != nullptr) {
56 return kErrorNone;
57 }
58
59 allocator_V3_ = IAllocatorV3::getService();
60 if (allocator_V3_ == nullptr) {
61 allocator_V2_ = IAllocatorV2::getService();
62 if (allocator_V2_ == nullptr) {
63 DLOGE("Unable to get allocator");
64 return kErrorCriticalResource;
65 }
66 }
67
68 mapper_V3_ = IMapperV3::getService();
69 if (mapper_V3_ == nullptr) {
70 mapper_V2_ = IMapperV2::getService();
71 if (mapper_V2_ == nullptr) {
72 DLOGE("Unable to get mapper");
73 return kErrorCriticalResource;
74 }
75 }
76
77 return kErrorNone;
78 }
79
AllocateBuffer(BufferInfo * buffer_info)80 DisplayError HWCBufferAllocator::AllocateBuffer(BufferInfo *buffer_info) {
81 auto err = GetGrallocInstance();
82 if (err != kErrorNone) {
83 return err;
84 }
85 const BufferConfig &buffer_config = buffer_info->buffer_config;
86 AllocatedBufferInfo *alloc_buffer_info = &buffer_info->alloc_buffer_info;
87 int format;
88 uint64_t alloc_flags = 0;
89 int error = SetBufferInfo(buffer_config.format, &format, &alloc_flags);
90 if (error != 0) {
91 return kErrorParameters;
92 }
93
94 if (buffer_config.secure) {
95 alloc_flags |= BufferUsage::PROTECTED;
96 }
97
98 if (buffer_config.secure_camera) {
99 alloc_flags |= BufferUsage::CAMERA_OUTPUT;
100 }
101
102 if (!buffer_config.cache) {
103 // Allocate uncached buffers
104 alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
105 }
106
107 if (buffer_config.gfx_client) {
108 alloc_flags |= BufferUsage::GPU_TEXTURE;
109 }
110
111 alloc_flags |= BufferUsage::COMPOSER_OVERLAY;
112
113 const native_handle_t *buf = nullptr;
114
115 if (mapper_V3_ != nullptr) {
116 IMapperV3::BufferDescriptorInfo descriptor_info;
117 descriptor_info.width = buffer_config.width;
118 descriptor_info.height = buffer_config.height;
119 descriptor_info.layerCount = 1;
120 descriptor_info.format =
121 static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(format);
122 descriptor_info.usage = alloc_flags;
123
124 auto hidl_err = MapperV3Error::NONE;
125
126 auto descriptor = BufferDescriptor();
127 mapper_V3_->createDescriptor(descriptor_info, [&](const auto &_error, const auto &_descriptor) {
128 hidl_err = _error;
129 descriptor = _descriptor;
130 });
131
132 if (hidl_err != MapperV3Error::NONE) {
133 DLOGE("Failed to create descriptor");
134 return kErrorMemory;
135 }
136
137 hidl_handle raw_handle = nullptr;
138
139 allocator_V3_->allocate(descriptor, 1,
140 [&](const auto &_error, const auto &_stride, const auto &_buffers) {
141 hidl_err = _error;
142 raw_handle = _buffers[0];
143 });
144
145 if (hidl_err != MapperV3Error::NONE) {
146 DLOGE("Failed to allocate buffer");
147 return kErrorMemory;
148 }
149
150 mapper_V3_->importBuffer(raw_handle, [&](const auto &_error, const auto &_buffer) {
151 hidl_err = _error;
152 buf = static_cast<const native_handle_t *>(_buffer);
153 });
154
155 if (hidl_err != MapperV3Error::NONE) {
156 DLOGE("Failed to import buffer into HWC");
157 return kErrorMemory;
158 }
159 } else {
160 IMapperV2::BufferDescriptorInfo descriptor_info;
161 descriptor_info.width = buffer_config.width;
162 descriptor_info.height = buffer_config.height;
163 descriptor_info.layerCount = 1;
164 descriptor_info.format =
165 static_cast<android::hardware::graphics::common::V1_0::PixelFormat>(format);
166 descriptor_info.usage = alloc_flags;
167
168 auto hidl_err = Error::NONE;
169
170 auto descriptor = BufferDescriptor();
171 mapper_V2_->createDescriptor(descriptor_info, [&](const auto &_error, const auto &_descriptor) {
172 hidl_err = _error;
173 descriptor = _descriptor;
174 });
175
176 if (hidl_err != Error::NONE) {
177 DLOGE("Failed to create descriptor");
178 return kErrorMemory;
179 }
180
181 hidl_handle raw_handle = nullptr;
182
183 allocator_V2_->allocate(descriptor, 1,
184 [&](const auto &_error, const auto &_stride, const auto &_buffers) {
185 hidl_err = _error;
186 raw_handle = _buffers[0];
187 });
188
189 if (hidl_err != Error::NONE) {
190 DLOGE("Failed to allocate buffer");
191 return kErrorMemory;
192 }
193
194 mapper_V2_->importBuffer(raw_handle, [&](const auto &_error, const auto &_buffer) {
195 hidl_err = _error;
196 buf = static_cast<const native_handle_t *>(_buffer);
197 });
198
199 if (hidl_err != Error::NONE) {
200 DLOGE("Failed to import buffer into HWC");
201 return kErrorMemory;
202 }
203 }
204
205 private_handle_t *hnd = nullptr;
206 hnd = (private_handle_t *)buf; // NOLINT
207 alloc_buffer_info->fd = hnd->fd;
208 alloc_buffer_info->stride = UINT32(hnd->width);
209 alloc_buffer_info->aligned_width = UINT32(hnd->width);
210 alloc_buffer_info->aligned_height = UINT32(hnd->height);
211 alloc_buffer_info->size = hnd->size;
212 alloc_buffer_info->id = hnd->id;
213 alloc_buffer_info->format = HWCLayer::GetSDMFormat(hnd->format, hnd->flags);
214
215 buffer_info->private_data = reinterpret_cast<void *>(hnd);
216 return kErrorNone;
217 }
218
FreeBuffer(BufferInfo * buffer_info)219 DisplayError HWCBufferAllocator::FreeBuffer(BufferInfo *buffer_info) {
220 DisplayError err = kErrorNone;
221 auto hnd = reinterpret_cast<void *>(buffer_info->private_data);
222 if (mapper_V3_ != nullptr) {
223 mapper_V3_->freeBuffer(hnd);
224 } else {
225 mapper_V2_->freeBuffer(hnd);
226 }
227 AllocatedBufferInfo &alloc_buffer_info = buffer_info->alloc_buffer_info;
228
229 alloc_buffer_info.fd = -1;
230 alloc_buffer_info.stride = 0;
231 alloc_buffer_info.size = 0;
232 buffer_info->private_data = NULL;
233 return err;
234 }
235
GetCustomWidthAndHeight(const private_handle_t * handle,int * width,int * height)236 void HWCBufferAllocator::GetCustomWidthAndHeight(const private_handle_t *handle, int *width,
237 int *height) {
238 *width = handle->width;
239 *height = handle->height;
240 gralloc::GetCustomDimensions(const_cast<private_handle_t *>(handle), width, height);
241 }
242
GetAlignedWidthAndHeight(int width,int height,int format,uint32_t alloc_type,int * aligned_width,int * aligned_height)243 void HWCBufferAllocator::GetAlignedWidthAndHeight(int width, int height, int format,
244 uint32_t alloc_type, int *aligned_width,
245 int *aligned_height) {
246 uint64_t usage = 0;
247 if (alloc_type & GRALLOC_USAGE_HW_FB) {
248 usage |= BufferUsage::COMPOSER_CLIENT_TARGET;
249 }
250 if (alloc_type & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) {
251 usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
252 }
253 uint32_t aligned_w = UINT(width);
254 uint32_t aligned_h = UINT(height);
255 gralloc::BufferInfo info(width, height, format, usage);
256 gralloc::GetAlignedWidthAndHeight(info, &aligned_w, &aligned_h);
257 *aligned_width = INT(aligned_w);
258 *aligned_height = INT(aligned_h);
259 }
260
GetBufferSize(BufferInfo * buffer_info)261 uint32_t HWCBufferAllocator::GetBufferSize(BufferInfo *buffer_info) {
262 const BufferConfig &buffer_config = buffer_info->buffer_config;
263 uint64_t alloc_flags = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
264
265 int width = INT(buffer_config.width);
266 int height = INT(buffer_config.height);
267 int format;
268
269 if (buffer_config.secure) {
270 alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
271 }
272
273 if (!buffer_config.cache) {
274 // Allocate uncached buffers
275 alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
276 }
277
278 if (SetBufferInfo(buffer_config.format, &format, &alloc_flags) < 0) {
279 return 0;
280 }
281
282 uint32_t aligned_width = 0, aligned_height = 0, buffer_size = 0;
283 gralloc::BufferInfo info(width, height, format, alloc_flags);
284 int ret = GetBufferSizeAndDimensions(info, &buffer_size, &aligned_width, &aligned_height);
285 if (ret < 0) {
286 return 0;
287 }
288 return buffer_size;
289 }
290
SetBufferInfo(LayerBufferFormat format,int * target,uint64_t * flags)291 int HWCBufferAllocator::SetBufferInfo(LayerBufferFormat format, int *target, uint64_t *flags) {
292 switch (format) {
293 case kFormatRGBA8888:
294 *target = HAL_PIXEL_FORMAT_RGBA_8888;
295 break;
296 case kFormatRGBX8888:
297 *target = HAL_PIXEL_FORMAT_RGBX_8888;
298 break;
299 case kFormatRGB888:
300 *target = HAL_PIXEL_FORMAT_RGB_888;
301 break;
302 case kFormatRGB565:
303 *target = HAL_PIXEL_FORMAT_RGB_565;
304 break;
305 case kFormatBGR565:
306 *target = HAL_PIXEL_FORMAT_BGR_565;
307 break;
308 case kFormatBGR888:
309 *target = HAL_PIXEL_FORMAT_BGR_888;
310 break;
311 case kFormatBGRA8888:
312 *target = HAL_PIXEL_FORMAT_BGRA_8888;
313 break;
314 case kFormatYCrCb420PlanarStride16:
315 *target = HAL_PIXEL_FORMAT_YV12;
316 break;
317 case kFormatYCrCb420SemiPlanar:
318 *target = HAL_PIXEL_FORMAT_YCrCb_420_SP;
319 break;
320 case kFormatYCbCr420SemiPlanar:
321 *target = HAL_PIXEL_FORMAT_YCbCr_420_SP;
322 break;
323 case kFormatYCbCr422H2V1Packed:
324 *target = HAL_PIXEL_FORMAT_YCbCr_422_I;
325 break;
326 case kFormatCbYCrY422H2V1Packed:
327 *target = HAL_PIXEL_FORMAT_CbYCrY_422_I;
328 break;
329 case kFormatYCbCr422H2V1SemiPlanar:
330 *target = HAL_PIXEL_FORMAT_YCbCr_422_SP;
331 break;
332 case kFormatYCbCr420SemiPlanarVenus:
333 *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS;
334 break;
335 case kFormatYCrCb420SemiPlanarVenus:
336 *target = HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS;
337 break;
338 case kFormatYCbCr420SPVenusUbwc:
339 case kFormatYCbCr420SPVenusTile:
340 *target = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
341 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
342 break;
343 case kFormatRGBA5551:
344 *target = HAL_PIXEL_FORMAT_RGBA_5551;
345 break;
346 case kFormatRGBA4444:
347 *target = HAL_PIXEL_FORMAT_RGBA_4444;
348 break;
349 case kFormatRGBA1010102:
350 *target = HAL_PIXEL_FORMAT_RGBA_1010102;
351 break;
352 case kFormatARGB2101010:
353 *target = HAL_PIXEL_FORMAT_ARGB_2101010;
354 break;
355 case kFormatRGBX1010102:
356 *target = HAL_PIXEL_FORMAT_RGBX_1010102;
357 break;
358 case kFormatXRGB2101010:
359 *target = HAL_PIXEL_FORMAT_XRGB_2101010;
360 break;
361 case kFormatBGRA1010102:
362 *target = HAL_PIXEL_FORMAT_BGRA_1010102;
363 break;
364 case kFormatABGR2101010:
365 *target = HAL_PIXEL_FORMAT_ABGR_2101010;
366 break;
367 case kFormatBGRX1010102:
368 *target = HAL_PIXEL_FORMAT_BGRX_1010102;
369 break;
370 case kFormatXBGR2101010:
371 *target = HAL_PIXEL_FORMAT_XBGR_2101010;
372 break;
373 case kFormatYCbCr420P010:
374 *target = HAL_PIXEL_FORMAT_YCbCr_420_P010;
375 break;
376 case kFormatYCbCr420TP10Ubwc:
377 case kFormatYCbCr420TP10Tile:
378 *target = HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC;
379 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
380 break;
381 case kFormatYCbCr420P010Ubwc:
382 case kFormatYCbCr420P010Tile:
383 *target = HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC;
384 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
385 break;
386 case kFormatYCbCr420P010Venus:
387 *target = HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS;
388 break;
389 case kFormatRGBA8888Ubwc:
390 *target = HAL_PIXEL_FORMAT_RGBA_8888;
391 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
392 break;
393 case kFormatRGBX8888Ubwc:
394 *target = HAL_PIXEL_FORMAT_RGBX_8888;
395 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
396 break;
397 case kFormatBGR565Ubwc:
398 *target = HAL_PIXEL_FORMAT_BGR_565;
399 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
400 break;
401 case kFormatRGBA1010102Ubwc:
402 *target = HAL_PIXEL_FORMAT_RGBA_1010102;
403 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
404 break;
405 case kFormatRGBX1010102Ubwc:
406 *target = HAL_PIXEL_FORMAT_RGBX_1010102;
407 *flags |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
408 break;
409 default:
410 DLOGW("Unsupported format = 0x%x", format);
411 return -EINVAL;
412 }
413 return 0;
414 }
415
GetAllocatedBufferInfo(const BufferConfig & buffer_config,AllocatedBufferInfo * allocated_buffer_info)416 DisplayError HWCBufferAllocator::GetAllocatedBufferInfo(
417 const BufferConfig &buffer_config, AllocatedBufferInfo *allocated_buffer_info) {
418 // TODO(user): This API should pass the buffer_info of the already allocated buffer
419 // The private_data can then be typecast to the private_handle and used directly.
420 uint64_t alloc_flags = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
421
422 int width = INT(buffer_config.width);
423 int height = INT(buffer_config.height);
424 int format;
425
426 if (buffer_config.secure) {
427 alloc_flags |= INT(GRALLOC_USAGE_PROTECTED);
428 }
429
430 if (!buffer_config.cache) {
431 // Allocate uncached buffers
432 alloc_flags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
433 }
434
435 if (SetBufferInfo(buffer_config.format, &format, &alloc_flags) < 0) {
436 return kErrorParameters;
437 }
438
439 uint32_t aligned_width = 0, aligned_height = 0, buffer_size = 0;
440 gralloc::BufferInfo info(width, height, format, alloc_flags);
441 int ret = GetBufferSizeAndDimensions(info, &buffer_size, &aligned_width, &aligned_height);
442 if (ret < 0) {
443 return kErrorParameters;
444 }
445 allocated_buffer_info->stride = UINT32(aligned_width);
446 allocated_buffer_info->aligned_width = UINT32(aligned_width);
447 allocated_buffer_info->aligned_height = UINT32(aligned_height);
448 allocated_buffer_info->size = UINT32(buffer_size);
449
450 return kErrorNone;
451 }
452
GetBufferLayout(const AllocatedBufferInfo & buf_info,uint32_t stride[4],uint32_t offset[4],uint32_t * num_planes)453 DisplayError HWCBufferAllocator::GetBufferLayout(const AllocatedBufferInfo &buf_info,
454 uint32_t stride[4], uint32_t offset[4],
455 uint32_t *num_planes) {
456 // TODO(user): Transition APIs to not need a private handle
457 private_handle_t hnd(-1, 0, 0, 0, 0, 0, 0);
458 int format = HAL_PIXEL_FORMAT_RGBA_8888;
459 uint64_t flags = 0;
460
461 SetBufferInfo(buf_info.format, &format, &flags);
462 // Setup only the required stuff, skip rest
463 hnd.format = format;
464 hnd.width = INT32(buf_info.aligned_width);
465 hnd.height = INT32(buf_info.aligned_height);
466 if (flags & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) {
467 hnd.flags = private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
468 }
469
470 int ret = gralloc::GetBufferLayout(&hnd, stride, offset, num_planes);
471 if (ret < 0) {
472 DLOGE("GetBufferLayout failed");
473 return kErrorParameters;
474 }
475
476 return kErrorNone;
477 }
478
MapBuffer(const private_handle_t * handle,shared_ptr<Fence> acquire_fence)479 DisplayError HWCBufferAllocator::MapBuffer(const private_handle_t *handle,
480 shared_ptr<Fence> acquire_fence) {
481 auto err = GetGrallocInstance();
482 if (err != kErrorNone) {
483 return err;
484 }
485
486 Fence::ScopedRef scoped_ref;
487 NATIVE_HANDLE_DECLARE_STORAGE(acquire_fence_storage, 1, 0);
488 hidl_handle acquire_fence_handle;
489 if (acquire_fence) {
490 auto h = native_handle_init(acquire_fence_storage, 1, 0);
491 h->data[0] = scoped_ref.Get(acquire_fence);
492 acquire_fence_handle = h;
493 }
494
495 auto hnd = const_cast<private_handle_t *>(handle);
496 void *buffer_ptr = NULL;
497 if (mapper_V3_ != nullptr) {
498 const IMapperV3::Rect access_region = {.left = 0, .top = 0, .width = 0, .height = 0};
499 mapper_V3_->lock(
500 reinterpret_cast<void *>(hnd), (uint64_t)BufferUsage::CPU_READ_OFTEN, access_region,
501 acquire_fence_handle,
502 [&](const auto &_error, const auto &_buffer, const auto &_bpp, const auto &_stride) {
503 if (_error == MapperV3Error::NONE) {
504 buffer_ptr = _buffer;
505 }
506 });
507 } else {
508 const IMapperV2::Rect access_region = {.left = 0, .top = 0, .width = 0, .height = 0};
509 mapper_V2_->lock(reinterpret_cast<void *>(hnd), (uint64_t)BufferUsage::CPU_READ_OFTEN,
510 access_region, acquire_fence_handle,
511 [&](const auto &_error, const auto &_buffer) {
512 if (_error == Error::NONE) {
513 buffer_ptr = _buffer;
514 }
515 });
516 }
517 if (!buffer_ptr) {
518 return kErrorUndefined;
519 }
520 return kErrorNone;
521 }
522
UnmapBuffer(const private_handle_t * handle,int * release_fence)523 DisplayError HWCBufferAllocator::UnmapBuffer(const private_handle_t *handle, int *release_fence) {
524 DisplayError err = kErrorNone;
525 *release_fence = -1;
526 auto hnd = const_cast<private_handle_t *>(handle);
527 if (mapper_V3_ != nullptr) {
528 mapper_V3_->unlock(reinterpret_cast<void *>(hnd),
529 [&](const auto &_error, const auto &_release_fence) {
530 if (_error != MapperV3Error::NONE) {
531 err = kErrorUndefined;
532 }
533 });
534 } else {
535 mapper_V2_->unlock(reinterpret_cast<void *>(hnd),
536 [&](const auto &_error, const auto &_release_fence) {
537 if (_error != Error::NONE) {
538 err = kErrorUndefined;
539 }
540 });
541 }
542 return err;
543 }
544
545 } // namespace sdm
546