Add helper functions to wsialloc

Moves out the non ion specific code from the wsialloc ion implementation
to a new wsialloc_helpers file.

Change-Id: I7ca816b27eb7e68fd1f138ec7bda006b204c0cbe
Signed-off-by: Iason Paraskevopoulos <iason.paraskevopoulos@arm.com>
This commit is contained in:
Iason Paraskevopoulos 2024-10-28 15:49:19 +00:00
parent 21dbc4d332
commit 8619f42973
4 changed files with 272 additions and 198 deletions

View file

@ -101,7 +101,7 @@ if(NOT SELECT_EXTERNAL_ALLOCATOR STREQUAL "none" AND EXTERNAL_WSIALLOC_LIBRARY S
set_target_properties(wsialloc PROPERTIES C_STANDARD 99)
if(SELECT_EXTERNAL_ALLOCATOR STREQUAL "ion")
target_sources(wsialloc PRIVATE util/wsialloc/wsialloc_ion.c)
target_sources(wsialloc PRIVATE util/wsialloc/wsialloc_ion.c util/wsialloc/wsialloc_helpers.c)
target_link_libraries(wsialloc drm_utils)
if(DEFINED KERNEL_DIR)
target_include_directories(wsialloc PRIVATE "${KERNEL_DIR}/drivers/staging/android/uapi")

View file

@ -0,0 +1,199 @@
/*
* Copyright (c) 2024 Arm Limited.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "wsialloc_helpers.h"
#include "format_table.h"
#include <assert.h>
/** Default alignment */
#define WSIALLOCP_MIN_ALIGN_SZ (64u)
/** Maximum image size allowed for each dimension */
#define MAX_IMAGE_SIZE 128000
typedef struct wsialloc_format_descriptor
{
wsialloc_format format;
fmt_spec format_spec;
} wsialloc_format_descriptor;
static uint64_t round_size_up_to_align(uint64_t size)
{
return (size + WSIALLOCP_MIN_ALIGN_SZ - 1) & ~(WSIALLOCP_MIN_ALIGN_SZ - 1);
}
static wsialloc_error calculate_format_properties(const wsialloc_format_descriptor *descriptor,
const wsialloc_allocate_info *info, int *strides, uint32_t *offsets,
uint64_t *total_size)
{
assert(descriptor != NULL);
assert(info != NULL);
assert(strides != NULL);
assert(offsets != NULL);
assert(total_size != NULL);
const uint8_t *bits_per_pixel = descriptor->format_spec.bpp;
const uint64_t flags = descriptor->format.flags;
const uint64_t modifier = descriptor->format.modifier;
const uint32_t num_planes = descriptor->format_spec.nr_planes;
/* We currently don't support any kind of custom modifiers */
if (modifier != DRM_FORMAT_MOD_LINEAR)
{
return WSIALLOC_ERROR_NOT_SUPPORTED;
}
/* No multi-plane format support */
if (num_planes > 1)
{
return WSIALLOC_ERROR_NOT_SUPPORTED;
}
size_t size = 0;
for (size_t plane = 0; plane < num_planes; plane++)
{
/* Assumes multiple of 8--rework otherwise. */
const uint32_t plane_bytes_per_pixel = bits_per_pixel[plane] / 8;
assert(plane_bytes_per_pixel * 8 == bits_per_pixel[plane]);
/* With large enough width, this can overflow as strides are signed. In practice, this shouldn't happen */
strides[plane] = round_size_up_to_align(info->width * plane_bytes_per_pixel);
offsets[plane] = size;
size += strides[plane] * info->height;
}
*total_size = size;
return WSIALLOC_ERROR_NONE;
}
static const fmt_spec *find_format(uint32_t fourcc)
{
/* Mask off any bits not necessary for allocation size */
fourcc = fourcc & (~(uint32_t)DRM_FORMAT_BIG_ENDIAN);
/* Search table for the format*/
for (size_t i = 0; i < fourcc_format_table_len; i++)
{
if (fourcc == fourcc_format_table[i].drm_format)
{
const fmt_spec *found_fmt = &fourcc_format_table[i];
assert(found_fmt->nr_planes <= WSIALLOC_MAX_PLANES);
return found_fmt;
}
}
return NULL;
}
static bool validate_parameters(const wsialloc_allocator *allocator, const wsialloc_allocate_info *info,
wsialloc_allocate_result *result)
{
if (allocator == NULL)
{
return false;
}
else if (!result)
{
return false;
}
else if (info->format_count == 0 || info->formats == NULL)
{
return false;
}
else if (info->width < 1 || info->height < 1 || info->width > MAX_IMAGE_SIZE || info->height > MAX_IMAGE_SIZE)
{
return false;
}
return true;
}
wsialloc_error wsiallocp_alloc(wsialloc_allocator *allocator, wsiallocp_alloc_callback fn_alloc,
const wsialloc_allocate_info *info, wsialloc_allocate_result *result)
{
if (!validate_parameters(allocator, info, result))
{
return WSIALLOC_ERROR_INVALID;
}
int local_strides[WSIALLOC_MAX_PLANES];
int local_offsets[WSIALLOC_MAX_PLANES];
wsialloc_error err = WSIALLOC_ERROR_NONE;
wsialloc_format_descriptor selected_format_desc = {};
uint64_t total_size = 0;
for (size_t i = 0; i < info->format_count; i++)
{
const wsialloc_format *current_format = &info->formats[i];
const fmt_spec *format_spec = find_format(current_format->fourcc);
if (!format_spec)
{
err = WSIALLOC_ERROR_NOT_SUPPORTED;
continue;
}
wsialloc_format_descriptor current_format_desc = { *current_format, *format_spec };
err = calculate_format_properties(&current_format_desc, info, local_strides, local_offsets, &total_size);
if (err != WSIALLOC_ERROR_NONE)
{
continue;
}
/* A compatible format was found */
selected_format_desc = current_format_desc;
break;
}
if (err != WSIALLOC_ERROR_NONE)
{
return err;
}
int local_fds[WSIALLOC_MAX_PLANES] = { -1, -1, -1, -1 };
if (!(info->flags & WSIALLOC_ALLOCATE_NO_MEMORY))
{
local_fds[0] = fn_alloc(allocator, info, total_size);
if (local_fds[0] < 0)
{
return WSIALLOC_ERROR_NO_RESOURCE;
}
assert(result->buffer_fds != NULL);
result->buffer_fds[0] = local_fds[0];
for (size_t plane = 1; plane < selected_format_desc.format_spec.nr_planes; plane++)
{
result->buffer_fds[plane] = result->buffer_fds[0];
}
}
result->format = selected_format_desc.format;
for (size_t plane = 0; plane < selected_format_desc.format_spec.nr_planes; plane++)
{
result->average_row_strides[plane] = local_strides[plane];
result->offsets[plane] = local_offsets[plane];
}
result->is_disjoint = false;
return WSIALLOC_ERROR_NONE;
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2024 Arm Limited.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "wsialloc.h"
/**
* @brief Internal callback used in wsiallocp_alloc(). Different wsialloc implementations define this
* callback and use wsiallocp_alloc to implement the wsialloc_alloc entrypoint.
*/
typedef int (*wsiallocp_alloc_callback)(const wsialloc_allocator *allocator, const wsialloc_allocate_info *info,
uint64_t size);
/**
*
* @brief Allocate a new buffer using the allocator
*
* A helper function to pick the best format out of a list of formats
* and allocate selected format using the allocator
*
* @param allocator The wsialloc allocator
* @param fn_alloc The function that will be called to perform the actual memory allocation
* @param info The requested allocation info
* @param[out] result The allocation result.
* @retval WSIALLOC_ERROR_NONE Indicates success
* @retval WSIALLOC_ERROR_INVALID Indicates failure in formats such as invalid fourcc
* or lack of any formats provided for selection or invalid parameters
* @retval WSIALLOC_ERROR_NOT_SUPPORTED Can indicate multiple errors, namely:
* * None of the formats are supported by the wsialloc implementation
* * The allocator does not support allocating with the selected flags
*/
wsialloc_error wsiallocp_alloc(wsialloc_allocator *allocator, wsiallocp_alloc_callback fn_alloc,
const wsialloc_allocate_info *info, wsialloc_allocate_result *result);

View file

@ -23,7 +23,7 @@
*/
#include "wsialloc.h"
#include "format_table.h"
#include "wsialloc_helpers.h"
#include <assert.h>
#include <errno.h>
@ -53,11 +53,6 @@
const uint32_t WSIALLOC_IMPLEMENTATION_VERSION_SYMBOL = WSIALLOC_IMPLEMENTATION_VERSION;
/** Default alignment */
#define WSIALLOCP_MIN_ALIGN_SZ (64u)
/** Maximum image size allowed for each dimension */
#define MAX_IMAGE_SIZE 128000
struct wsialloc_allocator
{
/* File descriptor of /dev/ion. */
@ -69,12 +64,6 @@ struct wsialloc_allocator
bool protected_heap_exists;
};
typedef struct wsialloc_format_descriptor
{
wsialloc_format format;
fmt_spec format_spec;
} wsialloc_format_descriptor;
static int find_alloc_heap_id(int fd)
{
assert(fd != -1);
@ -123,47 +112,38 @@ static int allocate(int fd, size_t size, uint32_t heap_id)
return alloc.fd;
}
static uint64_t round_size_up_to_align(uint64_t size)
{
return (size + WSIALLOCP_MIN_ALIGN_SZ - 1) & ~(WSIALLOCP_MIN_ALIGN_SZ - 1);
}
wsialloc_error wsialloc_new(wsialloc_allocator **allocator)
{
assert(allocator != NULL);
wsialloc_error ret = WSIALLOC_ERROR_NONE;
wsialloc_allocator *ion = malloc(sizeof(wsialloc_allocator));
if (NULL == ion)
{
ret = WSIALLOC_ERROR_NO_RESOURCE;
goto fail;
wsialloc_delete(ion);
return WSIALLOC_ERROR_NO_RESOURCE;
}
ion->fd = open("/dev/ion", O_RDONLY);
if (ion->fd < 0)
{
ret = WSIALLOC_ERROR_NO_RESOURCE;
goto fail;
wsialloc_delete(ion);
return WSIALLOC_ERROR_NO_RESOURCE;
}
ion->alloc_heap_id = find_alloc_heap_id(ion->fd);
if (ion->alloc_heap_id < 0)
{
ret = WSIALLOC_ERROR_NO_RESOURCE;
goto fail;
wsialloc_delete(ion);
return WSIALLOC_ERROR_NO_RESOURCE;
}
ion->protected_heap_exists = false;
*allocator = ion;
return ret;
fail:
wsialloc_delete(ion);
return ret;
return WSIALLOC_ERROR_NONE;
}
void wsialloc_delete(wsialloc_allocator *allocator)
{
assert(allocator != NULL);
if (NULL == allocator)
{
return;
@ -172,200 +152,43 @@ void wsialloc_delete(wsialloc_allocator *allocator)
if (allocator->fd >= 0)
{
close(allocator->fd);
allocator->fd = -1;
}
free(allocator);
}
static wsialloc_error calculate_format_properties(const wsialloc_format_descriptor *descriptor,
const wsialloc_allocate_info *info, int *strides, uint32_t *offsets)
{
assert(descriptor != NULL);
assert(info != NULL);
assert(strides != NULL);
assert(offsets != NULL);
const uint8_t *bits_per_pixel = descriptor->format_spec.bpp;
const uint64_t flags = descriptor->format.flags;
const uint64_t modifier = descriptor->format.modifier;
const uint32_t num_planes = descriptor->format_spec.nr_planes;
/* We currently don't support any kind of custom modifiers */
if (modifier != DRM_FORMAT_MOD_LINEAR)
{
return WSIALLOC_ERROR_NOT_SUPPORTED;
}
/* No multi-plane format support */
if (num_planes > 1)
{
return WSIALLOC_ERROR_NOT_SUPPORTED;
}
size_t size = 0;
for (size_t plane = 0; plane < num_planes; plane++)
{
/* Assumes multiple of 8--rework otherwise. */
const uint32_t plane_bytes_per_pixel = bits_per_pixel[plane] / 8;
assert(plane_bytes_per_pixel * 8 == bits_per_pixel[plane]);
/* With large enough width, this can overflow as strides are signed. In practice, this shouldn't happen */
strides[plane] = round_size_up_to_align(info->width * plane_bytes_per_pixel);
offsets[plane] = size;
size += strides[plane] * info->height;
}
return WSIALLOC_ERROR_NONE;
}
static wsialloc_error allocate_format(const wsialloc_allocator *allocator, const wsialloc_format_descriptor *descriptor,
const wsialloc_allocate_info *info, const int *strides, const uint32_t *offsets,
int *buffer_fds)
static int ion_allocate(const wsialloc_allocator *allocator, const wsialloc_allocate_info *info, uint64_t size)
{
assert(allocator != NULL);
assert(descriptor != NULL);
assert(info != NULL);
assert(offsets != NULL);
assert(strides != NULL);
assert(strides[0] >= 0);
assert(buffer_fds != NULL);
const uint64_t flags = descriptor->format.flags;
const uint32_t num_planes = descriptor->format_spec.nr_planes;
assert(allocator->fd != -1);
assert(size > 0);
/* The only error that can be encountered on allocations is lack of resources. Other parameter validation and
* support checks are done on format selection. */
assert(num_planes == 1);
uint32_t alloc_heap_id = allocator->alloc_heap_id;
if (info->flags & WSIALLOC_ALLOCATE_PROTECTED)
{
/* Exit if we don't support allocating protected memory */
/* Exit if we don't support allocating protected memory. */
if (!allocator->protected_heap_exists)
{
return WSIALLOC_ERROR_NO_RESOURCE;
assert(false);
return -1;
}
alloc_heap_id = allocator->protected_alloc_heap_id;
}
uint64_t total_size = offsets[0] + (uint64_t)strides[0] * info->height;
if (total_size > SIZE_MAX)
{
return WSIALLOC_ERROR_NO_RESOURCE;
}
buffer_fds[0] = allocate(allocator->fd, (size_t)total_size, alloc_heap_id);
if (buffer_fds[0] < 0)
{
return WSIALLOC_ERROR_NO_RESOURCE;
}
return WSIALLOC_ERROR_NONE;
}
static const fmt_spec *find_format(uint32_t fourcc)
{
/* Mask off any bits not necessary for allocation size */
fourcc = fourcc & (~(uint32_t)DRM_FORMAT_BIG_ENDIAN);
/* Search table for the format*/
for (size_t i = 0; i < fourcc_format_table_len; i++)
{
if (fourcc == fourcc_format_table[i].drm_format)
{
const fmt_spec *found_fmt = &fourcc_format_table[i];
assert(found_fmt->nr_planes <= WSIALLOC_MAX_PLANES);
return found_fmt;
}
}
return NULL;
}
static bool validate_parameters(const wsialloc_allocator *allocator, const wsialloc_allocate_info *info,
wsialloc_allocate_result *result)
{
if (allocator == NULL)
{
return false;
}
else if (!result)
{
return false;
}
else if (info->format_count == 0 || info->formats == NULL)
{
return false;
}
else if (info->width < 1 || info->height < 1 || info->width > MAX_IMAGE_SIZE || info->height > MAX_IMAGE_SIZE)
{
return false;
}
return true;
return allocate(allocator->fd, size, alloc_heap_id);
}
wsialloc_error wsialloc_alloc(wsialloc_allocator *allocator, const wsialloc_allocate_info *info,
wsialloc_allocate_result *result)
{
assert(allocator != NULL);
assert(info != NULL);
assert(result != NULL);
if (!validate_parameters(allocator, info, result))
if ((info->flags & WSIALLOC_ALLOCATE_PROTECTED) && (!allocator->protected_heap_exists))
{
return WSIALLOC_ERROR_INVALID;
return WSIALLOC_ERROR_NO_RESOURCE;
}
int local_strides[WSIALLOC_MAX_PLANES];
int local_fds[WSIALLOC_MAX_PLANES] = { -1 };
int local_offsets[WSIALLOC_MAX_PLANES];
wsialloc_error err = WSIALLOC_ERROR_NONE;
wsialloc_format_descriptor selected_format_desc = {};
for (size_t i = 0; i < info->format_count; i++)
{
const wsialloc_format *current_format = &info->formats[i];
const fmt_spec *format_spec = find_format(current_format->fourcc);
if (!format_spec)
{
err = WSIALLOC_ERROR_NOT_SUPPORTED;
continue;
}
wsialloc_format_descriptor current_format_desc = { *current_format, *format_spec };
err = calculate_format_properties(&current_format_desc, info, local_strides, local_offsets);
if (err != WSIALLOC_ERROR_NONE)
{
continue;
}
/* A compatible format was found */
selected_format_desc = current_format_desc;
break;
}
if (err == WSIALLOC_ERROR_NONE)
{
if (!(info->flags & WSIALLOC_ALLOCATE_NO_MEMORY))
{
err = allocate_format(allocator, &selected_format_desc, info, local_strides, local_offsets, local_fds);
}
}
if (err == WSIALLOC_ERROR_NONE)
{
result->format = selected_format_desc.format;
result->average_row_strides[0] = local_strides[0];
result->offsets[0] = local_offsets[0];
if (!(info->flags & WSIALLOC_ALLOCATE_NO_MEMORY))
{
result->buffer_fds[0] = local_fds[0];
}
result->is_disjoint = false;
}
return err;
return wsiallocp_alloc(allocator, ion_allocate, info, result);
}