diff options
Diffstat (limited to 'gralloc4/src/allocator/mali_gralloc_ion.cpp')
-rw-r--r-- | gralloc4/src/allocator/mali_gralloc_ion.cpp | 199 |
1 files changed, 110 insertions, 89 deletions
diff --git a/gralloc4/src/allocator/mali_gralloc_ion.cpp b/gralloc4/src/allocator/mali_gralloc_ion.cpp index 8bfc6d8..986af11 100644 --- a/gralloc4/src/allocator/mali_gralloc_ion.cpp +++ b/gralloc4/src/allocator/mali_gralloc_ion.cpp @@ -48,6 +48,7 @@ #include "mali_gralloc_ion.h" #include <array> +#include <cassert> #include <string> static const char kDmabufSensorDirectHeapName[] = "sensor_direct_heap"; @@ -60,6 +61,8 @@ static const char kDmabufVframeSecureHeapName[] = "vframe-secure"; static const char kDmabufVstreamSecureHeapName[] = "vstream-secure"; static const char kDmabufVscalerSecureHeapName[] = "vscaler-secure"; static const char kDmabufFramebufferSecureHeapName[] = "framebuffer-secure"; +static const char kDmabufGcmaCameraHeapName[] = "gcma_camera"; +static const char kDmabufGcmaCameraUncachedHeapName[] = "gcma_camera-uncached"; BufferAllocator& get_allocator() { static BufferAllocator allocator; @@ -84,7 +87,7 @@ std::string select_dmabuf_heap(uint64_t usage) std::string name; }; - static const std::array<HeapSpecifier, 6> exact_usage_heaps = + static const std::array<HeapSpecifier, 7> exact_usage_heaps = {{ // Faceauth heaps { // isp_image_heap @@ -114,9 +117,15 @@ std::string select_dmabuf_heap(uint64_t usage) GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_FB, find_first_available_heap({kDmabufFramebufferSecureHeapName, kDmabufVframeSecureHeapName}) }, + + { + GRALLOC_USAGE_PROTECTED | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | + GRALLOC_USAGE_HW_COMPOSER, + find_first_available_heap({kDmabufFramebufferSecureHeapName, kDmabufVframeSecureHeapName}) + }, }}; - static const std::array<HeapSpecifier, 6> inexact_usage_heaps = + static const std::array<HeapSpecifier, 8> inexact_usage_heaps = {{ // If GPU, use vframe-secure { @@ -146,6 +155,18 @@ std::string select_dmabuf_heap(uint64_t usage) kDmabufSensorDirectHeapName }, + // Camera GCMA heap + { + GRALLOC_USAGE_HW_CAMERA_WRITE, + find_first_available_heap({kDmabufGcmaCameraUncachedHeapName, kDmabufSystemUncachedHeapName}) + }, + + // Camera GCMA heap + { + GRALLOC_USAGE_HW_CAMERA_READ, + find_first_available_heap({kDmabufGcmaCameraUncachedHeapName, kDmabufSystemUncachedHeapName}) + }, + // Catchall to system { 0, @@ -165,7 +186,10 @@ std::string select_dmabuf_heap(uint64_t usage) { if ((usage & heap.usage_bits) == heap.usage_bits) { - if (heap.name == kDmabufSystemUncachedHeapName && + if (heap.name == kDmabufGcmaCameraUncachedHeapName && + ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)) + return kDmabufGcmaCameraHeapName; + else if (heap.name == kDmabufSystemUncachedHeapName && ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)) return kDmabufSystemHeapName; @@ -275,20 +299,8 @@ void mali_gralloc_ion_free(private_handle_t * const hnd) { for (int i = 0; i < hnd->fd_count; i++) { - void* mapped_addr = reinterpret_cast<void*>(hnd->bases[i]); - - /* Buffer might be unregistered already so we need to assure we have a valid handle */ - if (mapped_addr != nullptr) - { - if (munmap(mapped_addr, hnd->alloc_sizes[i]) != 0) - { - /* TODO: more detailed error logs */ - MALI_GRALLOC_LOGE("Failed to munmap handle %p", hnd); - } - } close(hnd->fds[i]); hnd->fds[i] = -1; - hnd->bases[i] = 0; } delete hnd; } @@ -340,108 +352,115 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, uint32_t numDescriptors, buffer_handle_t *pHandle, bool *shared_backend, int ion_fd) { + ATRACE_CALL(); GRALLOC_UNUSED(shared_backend); unsigned int priv_heap_flag = 0; uint64_t usage; uint32_t i; - int fds[MAX_FDS]; - std::fill(fds, fds + MAX_FDS, -1); for (i = 0; i < numDescriptors; i++) { - buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]); - usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; - - for (int fidx = 0; fidx < bufDescriptor->fd_count; fidx++) - { - if (ion_fd >= 0 && fidx == 0) { - fds[fidx] = ion_fd; - } else { - fds[fidx] = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); - } - if (fds[fidx] < 0) - { - MALI_GRALLOC_LOGE("ion_alloc failed"); - - for (int cidx = 0; cidx < fidx; cidx++) - { - close(fds[cidx]); - } - - /* need to free already allocated memory. not just this one */ - mali_gralloc_ion_free_internal(pHandle, numDescriptors); - - return -1; - } - } + buffer_descriptor_t *bufDescriptor = reinterpret_cast<buffer_descriptor_t *>(descriptors[i]); + assert(bufDescriptor); + assert(bufDescriptor->fd_count >= 0); + assert(bufDescriptor->fd_count <= MAX_FDS); - private_handle_t *hnd = new private_handle_t( + auto hnd = new private_handle_t( priv_heap_flag, bufDescriptor->alloc_sizes, bufDescriptor->consumer_usage, bufDescriptor->producer_usage, - fds, bufDescriptor->fd_count, + nullptr, bufDescriptor->fd_count, bufDescriptor->hal_format, bufDescriptor->alloc_format, bufDescriptor->width, bufDescriptor->height, bufDescriptor->pixel_stride, bufDescriptor->layer_count, bufDescriptor->plane_info); - if (NULL == hnd) + /* Reset the number of valid filedescriptors, we will increment + * it each time a valid fd is added, so we can rely on the + * cleanup functions to close open fds. */ + hnd->set_numfds(0); + + if (nullptr == hnd) { MALI_GRALLOC_LOGE("Private handle could not be created for descriptor:%d in non-shared usecase", i); + mali_gralloc_ion_free_internal(pHandle, i); + return -1; + } + + pHandle[i] = hnd; + usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; + + for (uint32_t fidx = 0; fidx < bufDescriptor->fd_count; fidx++) + { + int& fd = hnd->fds[fidx]; + + if (ion_fd >= 0 && fidx == 0) { + fd = ion_fd; + } else { + fd = alloc_from_dmabuf_heap(usage, bufDescriptor->alloc_sizes[fidx], bufDescriptor->name); + } - /* Close the obtained shared file descriptor for the current handle */ - for (int j = 0; j < bufDescriptor->fd_count; j++) + if (fd < 0) { - close(fds[j]); + MALI_GRALLOC_LOGE("ion_alloc failed for fds[%u] = %d", fidx, fd); + mali_gralloc_ion_free_internal(pHandle, i + 1); + return -1; } - mali_gralloc_ion_free_internal(pHandle, numDescriptors); - return -1; + hnd->incr_numfds(1); } - - pHandle[i] = hnd; } #if defined(GRALLOC_INIT_AFBC) && (GRALLOC_INIT_AFBC == 1) + ATRACE_NAME("AFBC init block"); unsigned char *cpu_ptr = NULL; for (i = 0; i < numDescriptors; i++) { - buffer_descriptor_t *bufDescriptor = (buffer_descriptor_t *)(descriptors[i]); - private_handle_t *hnd = (private_handle_t *)(pHandle[i]); + buffer_descriptor_t *bufDescriptor = reinterpret_cast<buffer_descriptor_t *>(descriptors[i]); + const private_handle_t *hnd = static_cast<const private_handle_t *>(pHandle[i]); usage = bufDescriptor->consumer_usage | bufDescriptor->producer_usage; if ((bufDescriptor->alloc_format & MALI_GRALLOC_INTFMT_AFBCENABLE_MASK) && !(usage & GRALLOC_USAGE_PROTECTED)) { - /* TODO: only map for AFBC buffers */ - cpu_ptr = - (unsigned char *)mmap(NULL, bufDescriptor->alloc_sizes[0], PROT_READ | PROT_WRITE, MAP_SHARED, hnd->fds[0], 0); - - if (MAP_FAILED == cpu_ptr) { - MALI_GRALLOC_LOGE("mmap failed for fd ( %d )", hnd->fds[0]); - mali_gralloc_ion_free_internal(pHandle, numDescriptors); - return -1; - } + ATRACE_NAME("mmap"); + /* TODO: only map for AFBC buffers */ + cpu_ptr = + (unsigned char *)mmap(NULL, bufDescriptor->alloc_sizes[0], PROT_READ | PROT_WRITE, MAP_SHARED, hnd->fds[0], 0); - mali_gralloc_ion_sync_start(hnd, true, true); + if (MAP_FAILED == cpu_ptr) + { + MALI_GRALLOC_LOGE("mmap failed for fd ( %d )", hnd->fds[0]); + mali_gralloc_ion_free_internal(pHandle, numDescriptors); + return -1; + } - /* For separated plane YUV, there is a header to initialise per plane. */ - const plane_info_t *plane_info = bufDescriptor->plane_info; - const bool is_multi_plane = hnd->is_multi_plane(); - for (int i = 0; i < MAX_PLANES && (i == 0 || plane_info[i].byte_stride != 0); i++) - { - init_afbc(cpu_ptr + plane_info[i].offset, - bufDescriptor->alloc_format, - is_multi_plane, - plane_info[i].alloc_width, - plane_info[i].alloc_height); + mali_gralloc_ion_sync_start(hnd, true, true); } - mali_gralloc_ion_sync_end(hnd, true, true); + { + ATRACE_NAME("data init"); + /* For separated plane YUV, there is a header to initialise per plane. */ + const plane_info_t *plane_info = bufDescriptor->plane_info; + assert(plane_info); + const bool is_multi_plane = hnd->is_multi_plane(); + for (int i = 0; i < MAX_PLANES && (i == 0 || plane_info[i].byte_stride != 0); i++) + { + init_afbc(cpu_ptr + plane_info[i].offset, + bufDescriptor->alloc_format, + is_multi_plane, + plane_info[i].alloc_width, + plane_info[i].alloc_height); + } + } - munmap(cpu_ptr, bufDescriptor->alloc_sizes[0]); + { + ATRACE_NAME("munmap"); + mali_gralloc_ion_sync_end(hnd, true, true); + munmap(cpu_ptr, bufDescriptor->alloc_sizes[0]); + } } } #endif @@ -449,15 +468,17 @@ int mali_gralloc_ion_allocate(const gralloc_buffer_descriptor_t *descriptors, return 0; } -int mali_gralloc_ion_map(private_handle_t *hnd) +std::array<void*, MAX_BUFFER_FDS> mali_gralloc_ion_map(private_handle_t *hnd) { - uint64_t usage = hnd->producer_usage | hnd->consumer_usage; + std::array<void*, MAX_BUFFER_FDS> vaddrs; + vaddrs.fill(nullptr); + uint64_t usage = hnd->producer_usage | hnd->consumer_usage; /* Do not allow cpu access to secure buffers */ if (usage & (GRALLOC_USAGE_PROTECTED | GRALLOC_USAGE_NOZEROED) && !(usage & GRALLOC_USAGE_PRIVATE_NONSECURE)) { - return 0; + return vaddrs; } for (int fidx = 0; fidx < hnd->fd_count; fidx++) { @@ -474,38 +495,38 @@ int mali_gralloc_ion_map(private_handle_t *hnd) for (int cidx = 0; cidx < fidx; fidx++) { - munmap((void*)hnd->bases[cidx], hnd->alloc_sizes[cidx]); - hnd->bases[cidx] = 0; + munmap((void*)vaddrs[cidx], hnd->alloc_sizes[cidx]); + vaddrs[cidx] = 0; } - return -err; + return vaddrs; } - hnd->bases[fidx] = uintptr_t(mappedAddress); + vaddrs[fidx] = mappedAddress; } - return 0; + return vaddrs; } -void mali_gralloc_ion_unmap(private_handle_t *hnd) +void mali_gralloc_ion_unmap(private_handle_t *hnd, std::array<void*, MAX_BUFFER_FDS>& vaddrs) { for (int i = 0; i < hnd->fd_count; i++) { int err = 0; - if (hnd->bases[i]) + if (vaddrs[i]) { - err = munmap((void*)hnd->bases[i], hnd->alloc_sizes[i]); + err = munmap(vaddrs[i], hnd->alloc_sizes[i]); } if (err) { MALI_GRALLOC_LOGE("Could not munmap base:%p size:%" PRIu64 " '%s'", - (void*)hnd->bases[i], hnd->alloc_sizes[i], strerror(errno)); + (void*)vaddrs[i], hnd->alloc_sizes[i], strerror(errno)); } else { - hnd->bases[i] = 0; + vaddrs[i] = 0; } } |