From cfd52ccfcf02d395134d6cac2ef3ed66be983ed6 Mon Sep 17 00:00:00 2001 From: Junkyeong Kim Date: Fri, 2 Apr 2021 20:35:32 +0900 Subject: Make hal-backend-tbm-shm package This is the first step for implementing the new hal backend. libhal-backend-tbm-shm package has a new hal backend module. The new hal architecture will be available for Tizen 6.5. Change-Id: I828dcd6a0720285a94052a3a5af690463b9fb48c Signed-off-by: Junkyeong Kim --- configure.ac | 20 + packaging/libhal-backend-tbm-shm.manifest | 5 + packaging/libtbm-shm.spec | 33 +- src/Makefile.am | 2 +- src/libhal-backend-tbm-shm/Makefile.am | 12 + src/libhal-backend-tbm-shm/tbm_backend_log.c | 74 +++ src/libhal-backend-tbm-shm/tbm_backend_log.h | 105 +++ src/libhal-backend-tbm-shm/tbm_backend_shm.c | 916 +++++++++++++++++++++++++++ 8 files changed, 1164 insertions(+), 3 deletions(-) create mode 100644 packaging/libhal-backend-tbm-shm.manifest create mode 100644 src/libhal-backend-tbm-shm/Makefile.am create mode 100644 src/libhal-backend-tbm-shm/tbm_backend_log.c create mode 100644 src/libhal-backend-tbm-shm/tbm_backend_log.h create mode 100644 src/libhal-backend-tbm-shm/tbm_backend_shm.c diff --git a/configure.ac b/configure.ac index 4667577..646bb16 100644 --- a/configure.ac +++ b/configure.ac @@ -37,8 +37,18 @@ AC_FUNC_ALLOCA m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) PKG_CHECK_MODULES(LIBTBM, libtbm) +PKG_CHECK_MODULES(HAL_API_COMMON, hal-api-common) +PKG_CHECK_MODULES(HAL_API_TBM, hal-api-tbm) PKG_CHECK_MODULES(DLOG, dlog) +# set the data dir for the tbm config +DEFAULT_HAL_LIBDIR="/hal/lib" +AC_ARG_WITH(hal-libdir, AS_HELP_STRING([--with-hal-libdir=PATH], [hal backend library path]), + [ HAL_LIBDIR="$withval" ], + [ HAL_LIBDIR="${DEFAULT_HAL_LIBDIR}" ]) +AC_DEFINE_UNQUOTED(HAL_LIBDIR, "${HAL_LIBDIR}", [Directory for hal backend library path]) +AC_SUBST(HAL_LIBDIR) + AC_ARG_ENABLE(cachectrl, AS_HELP_STRING([--enable-cachectrl], [Enable cache control (default: enable)]), @@ -56,9 +66,16 @@ AC_SUBST(LIBTBM_SHM_LIBS) bufmgr_dir=${libdir#*/} AC_SUBST(bufmgr_dir) +# for libhal-backend-tbm-shm +LIBHAL_BACKEND_TBM_SHM_CFLAGS="$HAL_API_COMMON_CFLAGS $HAL_API_TBM_CFLAGS $LIBDRM_CFLAGS $DLOG_CFLAGS" +LIBHAL_BACKEND_TBM_SHM_LIBS="$HAL_API_COMMON_LIBS $HAL_API_TBM_LIBS $LIBDRM_LIBS $DLOG_LIBS" +AC_SUBST(LIBHAL_BACKEND_TBM_SHM_CFLAGS) +AC_SUBST(LIBHAL_BACKEND_TBM_SHM_LIBS) + AC_OUTPUT([ Makefile src/libtbm-shm/Makefile + src/libhal-backend-tbm-shm/Makefile src/Makefile]) echo "" @@ -67,5 +84,8 @@ echo "LDFLAGS : $LDFLAGS" echo "LIBTBM_SHM_CFLAGS : $LIBTBM_SHM_CFLAGS" echo "LIBTBM_SHM_LIBS : $LIBTBM_SHM_LIBS" echo "bufmgr_dir : $bufmgr_dir" +echo "LIBHAL_BACKEND_TBM_SHM_CFLAGS : $LIBHAL_BACKEND_TBM_SHM_CFLAGS" +echo "LIBHAL_BACKEND_TBM_SHM_LIBS : $LIBHAL_BACKEND_TBM_SHM_LIBS" +echo "hal-libdir : $HAL_LIBDIR" echo "" diff --git a/packaging/libhal-backend-tbm-shm.manifest b/packaging/libhal-backend-tbm-shm.manifest new file mode 100644 index 0000000..75b0fa5 --- /dev/null +++ b/packaging/libhal-backend-tbm-shm.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/libtbm-shm.spec b/packaging/libtbm-shm.spec index 754822c..d17247e 100644 --- a/packaging/libtbm-shm.spec +++ b/packaging/libtbm-shm.spec @@ -7,23 +7,39 @@ Group: System/Libraries ExcludeArch: i586 Source0: %{name}-%{version}.tar.gz Source1001: %{name}.manifest +Source1002: libhal-backend-tbm-shm.manifest +BuildRequires: pkgconfig(libdrm) BuildRequires: pkgconfig(libtbm) +BuildRequires: pkgconfig(hal-api-common) +BuildRequires: pkgconfig(hal-api-tbm) BuildRequires: pkgconfig(dlog) %description descriptionion: Tizen Buffer manager backend module uses drm shm +%package -n hal-backend-tbm-shm +Summary: hal-backend-tbm module for drm shm +Group: System/Libraries +Requires: hal-api-tbm +Requires: hal-api-common + +%description -n hal-backend-tbm-shm +descriptionion: hal tbm backend module for drm shm + %global TZ_SYS_RO_SHARE %{?TZ_SYS_RO_SHARE:%TZ_SYS_RO_SHARE}%{!?TZ_SYS_RO_SHARE:/usr/share} %prep %setup -q cp %{SOURCE1001} . +cp %{SOURCE1002} . %build -%reconfigure --prefix=%{_prefix} --libdir=%{_libdir}/bufmgr --disable-cachectrl \ - CFLAGS="${CFLAGS} -Wall -Werror" LDFLAGS="${LDFLAGS} -Wl,--hash-style=both -Wl,--as-needed" +%reconfigure --prefix=%{_prefix} --libdir=%{_libdir}/bufmgr \ + --with-hal-libdir=%{_hal_libdir} \ + --disable-cachectrl \ + FLAGS="${CFLAGS} -Wall -Werror" LDFLAGS="${LDFLAGS} -Wl,--hash-style=both -Wl,--as-needed" make %{?_smp_mflags} @@ -40,9 +56,22 @@ ln -s libtbm_shm.so %{_libdir}/bufmgr/libtbm_default.so %postun -p /sbin/ldconfig +%post -n hal-backend-tbm-shm +if [ -f %{_hal_libdir}/libhal-backend-tbm.so ]; then + rm -rf %{_hal_libdir}/libhal-backend-tbm.so +fi +ln -s libhal-backend-tbm-shm.so %{_hal_libdir}/libhal-backend-tbm.so + +%postun -n hal-backend-tbm-shm -p /sbin/ldconfig + %files %defattr(-,root,root,-) %manifest libtbm-shm.manifest %license COPYING %{_libdir}/bufmgr/libtbm_*.so* +%files -n hal-backend-tbm-shm +%defattr(-,root,root,-) +%manifest libhal-backend-tbm-shm.manifest +%license COPYING +%{_hal_libdir}/libhal-backend-*.so* diff --git a/src/Makefile.am b/src/Makefile.am index b89be83..9cbd3b5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1 +1 @@ -SUBDIRS = libtbm-shm +SUBDIRS = libtbm-shm libhal-backend-tbm-shm diff --git a/src/libhal-backend-tbm-shm/Makefile.am b/src/libhal-backend-tbm-shm/Makefile.am new file mode 100644 index 0000000..e2c1f91 --- /dev/null +++ b/src/libhal-backend-tbm-shm/Makefile.am @@ -0,0 +1,12 @@ +AM_CFLAGS = \ + @LIBHAL_BACKEND_TBM_SHM_CFLAGS@ \ + -I$(top_srcdir) \ + -I$(top_srcdir)/src/libhal-backend-tbm-shm + +libhal_backend_tbm_shm_la_LTLIBRARIES = libhal-backend-tbm-shm.la +libhal_backend_tbm_shm_ladir = @HAL_LIBDIR@ +libhal_backend_tbm_shm_la_LIBADD = @LIBHAL_BACKEND_TBM_SHM_LIBS@ + +libhal_backend_tbm_shm_la_SOURCES = \ + tbm_backend_log.c \ + tbm_backend_shm.c diff --git a/src/libhal-backend-tbm-shm/tbm_backend_log.c b/src/libhal-backend-tbm-shm/tbm_backend_log.c new file mode 100644 index 0000000..641b2b8 --- /dev/null +++ b/src/libhal-backend-tbm-shm/tbm_backend_log.c @@ -0,0 +1,74 @@ +/************************************************************************** + +libtbm_shm + +Copyright 2021 Samsung Electronics co., Ltd. All Rights Reserved. + +Contact: SooChan Lim + +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, sub license, 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 (including the +next paragraph) 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 NON-INFRINGEMENT. +IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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 "tbm_backend_log.h" + +#undef LOG_TAG +#define LOG_TAG "TBM_BACKEND" + +unsigned int tbm_log_debug_level = TBM_BACKEND_LOG_LEVEL_DBG; + +static void +_tbm_backend_log_dlog_print(int level, const char *fmt, va_list arg) +{ + log_priority dlog_prio; + + switch (level) { + case TBM_BACKEND_LOG_LEVEL_ERR: + dlog_prio = DLOG_ERROR; + break; + case TBM_BACKEND_LOG_LEVEL_WRN: + dlog_prio = DLOG_WARN; + break; + case TBM_BACKEND_LOG_LEVEL_INFO: + dlog_prio = DLOG_INFO; + break; + case TBM_BACKEND_LOG_LEVEL_DBG: + dlog_prio = DLOG_DEBUG; + break; + default: + return; + } + __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmt, arg); +} + +void +tbm_backend_log_print(int level, const char *fmt, ...) +{ + va_list arg; + + if (level > tbm_log_debug_level) + return; + + va_start(arg, fmt); + _tbm_backend_log_dlog_print(level, fmt, arg); + va_end(arg); +} + diff --git a/src/libhal-backend-tbm-shm/tbm_backend_log.h b/src/libhal-backend-tbm-shm/tbm_backend_log.h new file mode 100644 index 0000000..d405f05 --- /dev/null +++ b/src/libhal-backend-tbm-shm/tbm_backend_log.h @@ -0,0 +1,105 @@ +/************************************************************************** + +libtbm_shm + +Copyright 2021 Samsung Electronics co., Ltd. All Rights Reserved. + +Contact: SooChan Lim + +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, sub license, 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 (including the +next paragraph) 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 NON-INFRINGEMENT. +IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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. + +**************************************************************************/ + +#ifndef __TBM_BACKEND_LOG_H__ +#define __TBM_BACKEND_LOG_H__ + +#include +#include +#include + +enum { + TBM_BACKEND_LOG_LEVEL_NONE, + TBM_BACKEND_LOG_LEVEL_ERR, + TBM_BACKEND_LOG_LEVEL_WRN, + TBM_BACKEND_LOG_LEVEL_INFO, + TBM_BACKEND_LOG_LEVEL_DBG, +}; + + +/* log level */ +void tbm_backend_log_print(int level, const char *fmt, ...); + +#define TBM_BACKEND_DBG(fmt, args...) \ + do { \ + struct timespec ts; \ + clock_gettime(CLOCK_MONOTONIC, &ts); \ + tbm_backend_log_print(TBM_BACKEND_LOG_LEVEL_DBG, "[%5d.%06d][%d][%s %d]"fmt, \ + (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ + (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + } while (0) + +#define TBM_BACKEND_INFO(fmt, args...) \ + do { \ + struct timespec ts; \ + clock_gettime(CLOCK_MONOTONIC, &ts); \ + tbm_backend_log_print(TBM_BACKEND_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d]"fmt, \ + (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ + (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + } while (0) + +#define TBM_BACKEND_WRN(fmt, args...) \ + do { \ + struct timespec ts; \ + clock_gettime(CLOCK_MONOTONIC, &ts); \ + tbm_backend_log_print(TBM_BACKEND_LOG_LEVEL_WRN, "[%5d.%06d][%d][%s %d]"fmt, \ + (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ + (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + } while (0) + +#define TBM_BACKEND_ERR(fmt, args...) \ + do { \ + struct timespec ts; \ + clock_gettime(CLOCK_MONOTONIC, &ts); \ + tbm_backend_log_print(TBM_BACKEND_LOG_LEVEL_ERR, "[%5d.%06d][%d][%s %d]"fmt, \ + (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \ + (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args); \ + } while (0) + +#define TBM_BACKEND_RETURN_IF_FAIL(cond) {\ + if (!(cond)) {\ + TBM_BACKEND_ERR("'%s' failed.\n", #cond);\ + return;\ + } \ +} +#define TBM_BACKEND_RETURN_VAL_IF_FAIL(cond, val) {\ + if (!(cond)) {\ + TBM_BACKEND_ERR("'%s' failed.\n", #cond);\ + return val;\ + } \ +} +#define TBM_BACKEND_GOTO_VAL_IF_FAIL(cond, val) {\ + if (!(cond)) {\ + TBM_BACKEND_ERR("'%s' failed.\n", #cond);\ + goto val;\ + } \ +} + +#endif /* __TBM_BACKEND_LOG_H__ */ diff --git a/src/libhal-backend-tbm-shm/tbm_backend_shm.c b/src/libhal-backend-tbm-shm/tbm_backend_shm.c new file mode 100644 index 0000000..ae73967 --- /dev/null +++ b/src/libhal-backend-tbm-shm/tbm_backend_shm.c @@ -0,0 +1,916 @@ +/************************************************************************** + + libtbm_shm + + Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved. + + Contact: SooChan Lim , Sangjin Lee + + 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, sub license, 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 (including the + next paragraph) 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 NON-INFRINGEMENT. + IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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. + + **************************************************************************/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "tbm_backend_log.h" + +#define TBM_COLOR_FORMAT_COUNT 4 +#define STRERR_BUFSIZE 128 + +#define SIZE_ALIGN(value, base) (((value) + ((base) - 1)) & ~((base) - 1)) + +#define TBM_SURFACE_ALIGNMENT_PLANE (64) +#define TBM_SURFACE_ALIGNMENT_PITCH_RGB (64) +#define TBM_SURFACE_ALIGNMENT_PITCH_YUV (16) + +typedef struct _tbm_shm_bufmgr tbm_shm_bufmgr; +typedef struct _tbm_shm_bo tbm_shm_bo; + +/* tbm buffor object for shm */ +struct _tbm_shm_bo { + key_t key; + void * pBase; + + unsigned int size; + + unsigned int flags_tbm; + hal_tbm_fd fd; + + pthread_mutex_t mutex; + int device; + int opt; + + tbm_shm_bufmgr *bufmgr_data; +}; + +/* tbm bufmgr private for shm */ +struct _tbm_shm_bufmgr { + int isLocal; + void* hashBos; +}; + +static char *STR_DEVICE[] = { + "DEF", + "CPU", + "2D", + "3D", + "MM" +}; + +static char *STR_OPT[] = { + "NONE", + "RD", + "WR", + "RDWR" +}; + +uint32_t tbm_shm_color_format_list[TBM_COLOR_FORMAT_COUNT] = { + HAL_TBM_FORMAT_ARGB8888, + HAL_TBM_FORMAT_XRGB8888, + HAL_TBM_FORMAT_NV12, + HAL_TBM_FORMAT_YUV420 +}; + +char* +target_name() +{ + FILE *f; + char *slash; + static int initialized = 0; + static char app_name[128]; + + if (initialized) + return app_name; + + /* get the application name */ + f = fopen("/proc/self/cmdline", "r"); + + if (!f) + return 0; + + memset(app_name, 0x00, sizeof(app_name)); + + if (fgets(app_name, 100, f) == NULL) { + fclose(f); + return 0; + } + + fclose(f); + + if ((slash = strrchr(app_name, '/')) != NULL) + memmove(app_name, slash + 1, strlen(slash)); + + initialized = 1; + + return app_name; +} + +static hal_tbm_bo_handle +_shm_bo_handle(tbm_shm_bo *bo_data, int device) +{ + hal_tbm_bo_handle bo_handle; + memset(&bo_handle, 0x0, sizeof(uint64_t)); + + switch (device) { + case HAL_TBM_DEVICE_DEFAULT: + case HAL_TBM_DEVICE_2D: + bo_handle.u32 = (uint32_t) bo_data->fd; + break; + case HAL_TBM_DEVICE_CPU: + bo_handle.ptr = (void *) bo_data->pBase; + break; + case HAL_TBM_DEVICE_3D: + case HAL_TBM_DEVICE_MM: + default: + TBM_BACKEND_DBG("Not supported device:%d\n", device); + bo_handle.ptr = (void *) bo_data->pBase; + break; + } + + return bo_handle; +} + +static int +_shm_os_fd_set_cloexec(int fd) +{ + long flags; + + if (fd == -1) + return -1; + + flags = fcntl(fd, F_GETFD); + if (flags == -1) + return -1; + + if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) + return -1; + + return 0; +} + +static int +_shm_set_cloexec_or_close(int fd) +{ + if (_shm_os_fd_set_cloexec(fd) != 0) { + close(fd); + return -1; + } + return fd; +} + +static int +_shm_create_tmpfile_cloexec(char *tmpname) +{ + int fd; + +#ifdef HAVE_MKOSTEMP + fd = mkostemp(tmpname, O_CLOEXEC); + if (fd >= 0) + unlink(tmpname); +#else + fd = mkstemp(tmpname); + if (fd >= 0) { + fd = _shm_set_cloexec_or_close(fd); + unlink(tmpname); + } +#endif + + return fd; +} + +static hal_tbm_fd +_shm_create_anonymous_file(off_t size) +{ + static const char template[] = "/tbm-shared-XXXXXX"; + const char *path; + char *name; + int fd; + int ret; + + path = getenv("XDG_RUNTIME_DIR"); + if (!path) { + errno = ENOENT; + return -1; + } + + name = malloc(strlen(path) + sizeof(template)); + if (!name) + return -1; + + snprintf(name, strlen(path) + sizeof(template), "%s%s", path, template); + + fd = _shm_create_tmpfile_cloexec(name); + + free(name); + + if (fd < 0) + return -1; + +#ifdef HAVE_POSIX_FALLOCATE + do { + ret = posix_fallocate(fd, 0, size); + } while (ret == EINTR); + if (ret != 0) { + close(fd); + errno = ret; + return -1; + } +#else + do { + ret = ftruncate(fd, size); + } while (ret < 0 && errno == EINTR); + if (ret < 0) { + close(fd); + return -1; + } +#endif + + return (hal_tbm_fd)fd; +} + +static hal_tbm_bufmgr_capability +tbm_shm_bufmgr_get_capabilities(hal_tbm_bufmgr *bufmgr, hal_tbm_error *error) +{ + hal_tbm_bufmgr_capability capabilities = HAL_TBM_BUFMGR_CAPABILITY_NONE; + + capabilities = HAL_TBM_BUFMGR_CAPABILITY_SHARE_FD; + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return capabilities; +} + +static hal_tbm_error +tbm_shm_bufmgr_get_supported_formats(hal_tbm_bufmgr *bufmgr, + uint32_t **formats, uint32_t *num) +{ + tbm_shm_bufmgr *bufmgr_data = (tbm_shm_bufmgr *)bufmgr; + uint32_t *color_formats; + + TBM_BACKEND_RETURN_VAL_IF_FAIL(bufmgr_data != NULL, HAL_TBM_ERROR_INVALID_PARAMETER); + + color_formats = (uint32_t *)calloc(1, sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT); + if (color_formats == NULL) + return HAL_TBM_ERROR_OUT_OF_MEMORY; + + memcpy(color_formats, tbm_shm_color_format_list, sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT); + + *formats = color_formats; + *num = TBM_COLOR_FORMAT_COUNT; + + TBM_BACKEND_DBG("supported format count = %d\n", *num); + + return HAL_TBM_ERROR_NONE; +} + +static hal_tbm_error +tbm_shm_bufmgr_get_plane_data(hal_tbm_bufmgr *bufmgr, + hal_tbm_format format, int plane_idx, int width, int height, + uint32_t *size, uint32_t *offset, uint32_t *pitch, int *bo_idx) +{ + int ret = HAL_TBM_ERROR_NONE; + int bpp; + int _offset = 0; + int _pitch = 0; + int _size = 0; + int _bo_idx = 0; + + switch (format) { + /* 16 bpp RGB */ + case HAL_TBM_FORMAT_XRGB4444: + case HAL_TBM_FORMAT_XBGR4444: + case HAL_TBM_FORMAT_RGBX4444: + case HAL_TBM_FORMAT_BGRX4444: + case HAL_TBM_FORMAT_ARGB4444: + case HAL_TBM_FORMAT_ABGR4444: + case HAL_TBM_FORMAT_RGBA4444: + case HAL_TBM_FORMAT_BGRA4444: + case HAL_TBM_FORMAT_XRGB1555: + case HAL_TBM_FORMAT_XBGR1555: + case HAL_TBM_FORMAT_RGBX5551: + case HAL_TBM_FORMAT_BGRX5551: + case HAL_TBM_FORMAT_ARGB1555: + case HAL_TBM_FORMAT_ABGR1555: + case HAL_TBM_FORMAT_RGBA5551: + case HAL_TBM_FORMAT_BGRA5551: + case HAL_TBM_FORMAT_RGB565: + bpp = 16; + _offset = 0; + _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + break; + /* 24 bpp RGB */ + case HAL_TBM_FORMAT_RGB888: + case HAL_TBM_FORMAT_BGR888: + bpp = 24; + _offset = 0; + _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + break; + /* 32 bpp RGB */ + case HAL_TBM_FORMAT_XRGB8888: + case HAL_TBM_FORMAT_XBGR8888: + case HAL_TBM_FORMAT_RGBX8888: + case HAL_TBM_FORMAT_BGRX8888: + case HAL_TBM_FORMAT_ARGB8888: + case HAL_TBM_FORMAT_ABGR8888: + case HAL_TBM_FORMAT_RGBA8888: + case HAL_TBM_FORMAT_BGRA8888: + bpp = 32; + _offset = 0; + _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + break; + + /* packed YCbCr */ + case HAL_TBM_FORMAT_YUYV: + case HAL_TBM_FORMAT_YVYU: + case HAL_TBM_FORMAT_UYVY: + case HAL_TBM_FORMAT_VYUY: + case HAL_TBM_FORMAT_AYUV: + bpp = 32; + _offset = 0; + _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + break; + + /* + * 2 plane YCbCr + * index 0 = Y plane, [7:0] Y + * index 1 = Cr:Cb plane, [15:0] Cr:Cb little endian + * or + * index 1 = Cb:Cr plane, [15:0] Cb:Cr little endian + */ + case HAL_TBM_FORMAT_NV12: + case HAL_TBM_FORMAT_NV21: + bpp = 12; + //if (plane_idx == 0) + { + _offset = 0; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + if (plane_idx == 0) + break; + } + //else if (plane_idx == 1) + { + _offset += _size; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + } + break; + case HAL_TBM_FORMAT_NV16: + case HAL_TBM_FORMAT_NV61: + bpp = 16; + //if(plane_idx == 0) + { + _offset = 0; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + if (plane_idx == 0) + break; + } + //else if( plane_idx ==1 ) + { + _offset += _size; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + } + break; + + /* + * 3 plane YCbCr + * index 0: Y plane, [7:0] Y + * index 1: Cb plane, [7:0] Cb + * index 2: Cr plane, [7:0] Cr + * or + * index 1: Cr plane, [7:0] Cr + * index 2: Cb plane, [7:0] Cb + */ + /* + NATIVE_BUFFER_FORMAT_YV12 + NATIVE_BUFFER_FORMAT_I420 + */ + case HAL_TBM_FORMAT_YUV410: + case HAL_TBM_FORMAT_YVU410: + bpp = 9; + _bo_idx = 0; + break; + case HAL_TBM_FORMAT_YUV411: + case HAL_TBM_FORMAT_YVU411: + case HAL_TBM_FORMAT_YUV420: + case HAL_TBM_FORMAT_YVU420: + bpp = 12; + //if(plane_idx == 0) + { + _offset = 0; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + if (plane_idx == 0) + break; + } + //else if( plane_idx == 1 ) + { + _offset += _size; + _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV/2); + _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + if (plane_idx == 1) + break; + } + //else if (plane_idx == 2 ) + { + _offset += _size; + _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV/2); + _size = SIZE_ALIGN(_pitch * (height / 2), TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + } + break; + case HAL_TBM_FORMAT_YUV422: + case HAL_TBM_FORMAT_YVU422: + bpp = 16; + //if(plane_idx == 0) + { + _offset = 0; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + if (plane_idx == 0) + break; + } + //else if( plane_idx == 1 ) + { + _offset += _size; + _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV/2); + _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + if (plane_idx == 1) + break; + } + //else if (plane_idx == 2 ) + { + _offset += _size; + _pitch = SIZE_ALIGN(width / 2, TBM_SURFACE_ALIGNMENT_PITCH_YUV/2); + _size = SIZE_ALIGN(_pitch * (height), TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + } + break; + case HAL_TBM_FORMAT_YUV444: + case HAL_TBM_FORMAT_YVU444: + bpp = 24; + //if(plane_idx == 0) + { + _offset = 0; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + if (plane_idx == 0) + break; + } + //else if( plane_idx == 1 ) + { + _offset += _size; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + if (plane_idx == 1) + break; + } + //else if (plane_idx == 2 ) + { + _offset += _size; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _bo_idx = 0; + } + break; + default: + bpp = 0; + break; + } + + *size = _size; + *offset = _offset; + *pitch = _pitch; + *bo_idx = _bo_idx; + + return ret; +} + +static hal_tbm_bo * +tbm_shm_bufmgr_alloc_bo(hal_tbm_bufmgr *bufmgr, unsigned int size, + hal_tbm_bo_memory_type flags, hal_tbm_error *error) + +{ + tbm_shm_bufmgr *bufmgr_data = (tbm_shm_bufmgr *)bufmgr; + tbm_shm_bo *bo_data; + hal_tbm_fd fd = -1; + + if (bufmgr_data == NULL) { + TBM_BACKEND_ERR("bufmgr_data is null\n"); + if (error) *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return NULL; + } + + if (flags & HAL_TBM_BO_SCANOUT) + TBM_BACKEND_DBG("[libtbm-shm:%d] warning %s:%d HAL_TBM_BO_SCANOUT ins't supported\n", getpid(), __FUNCTION__, __LINE__); + + bo_data = calloc(1, sizeof(struct _tbm_shm_bo)); + if (!bo_data) { + TBM_BACKEND_DBG("[libtbm-shm:%d] error %s:%d Fail to allocate the bo private\n", getpid(), __FUNCTION__, __LINE__); + if (error) *error = HAL_TBM_ERROR_INVALID_OPERATION; + return 0; + } + + bo_data->fd = -1; + fd = _shm_create_anonymous_file((off_t)size); + if (fd < 0) { + TBM_BACKEND_DBG("[libtbm-shm:%d] error %s:%d Fail to create a buffer file(size: %d) %m\n", getpid(), + __FUNCTION__, __LINE__, size); + free(bo_data); + if (error) *error = HAL_TBM_ERROR_INVALID_OPERATION; + return 0; + } + + bo_data->fd = fd; + bo_data->pBase = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (bo_data->pBase == MAP_FAILED) { + TBM_BACKEND_DBG("[libtbm-shm:%d] error %s:%d Fail to mmap: %m\n", getpid(), + __FUNCTION__, __LINE__); + close(fd); + free(bo_data); + if (error) *error = HAL_TBM_ERROR_INVALID_OPERATION; + return 0; + } + + bo_data->size = size; + bo_data->flags_tbm = HAL_TBM_BO_DEFAULT | HAL_TBM_BO_NONCACHABLE; + + TBM_BACKEND_DBG(" [%s] bo:%p, fd:%d, flags:%d, size:%d\n", target_name(), bo_data, bo_data->fd, bo_data->flags_tbm, bo_data->size); + if (error) *error = HAL_TBM_ERROR_NONE; + + return (hal_tbm_bo *) bo_data; +} + +static hal_tbm_bo * +tbm_shm_bufmgr_import_fd(hal_tbm_bufmgr *bufmgr, hal_tbm_fd key, hal_tbm_error *error) +{ + tbm_shm_bo *bo_data; + int real_size; + char buf[STRERR_BUFSIZE]; + + if (key < 0) { + TBM_BACKEND_ERR("Invalid fd(%d)\n", key); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return 0; + } + + bo_data = calloc(1, sizeof(struct _tbm_shm_bo)); + if (!bo_data) { + TBM_BACKEND_ERR("Fail to allocate the bo private\n"); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return 0; + } + + real_size = lseek(key, 0, SEEK_END); + if (real_size < 0) { + TBM_BACKEND_ERR("Fail to lseek(err:%s)\n", + strerror_r(errno, buf, STRERR_BUFSIZE)); + free(bo_data); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return 0; + } + + bo_data->pBase = mmap(NULL, real_size, + PROT_READ | PROT_WRITE, MAP_SHARED, key, 0); + if (bo_data->pBase == MAP_FAILED) { + TBM_BACKEND_ERR("Fail to mmap (size:%d, err:%s)\n", + real_size, strerror_r(errno, buf, STRERR_BUFSIZE)); + free(bo_data); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return 0; + } + + bo_data->size = real_size; + bo_data->fd = key; + bo_data->flags_tbm = HAL_TBM_BO_DEFAULT | HAL_TBM_BO_NONCACHABLE; + + TBM_BACKEND_DBG(" [%s] bo:%p, shm:%d, flags:%d, size:%d\n", target_name(), bo_data, bo_data->fd, + bo_data->flags_tbm, bo_data->size); + if (error) + *error = HAL_TBM_ERROR_NONE; + + return (hal_tbm_bo *)bo_data; +} + +static void +tbm_shm_bo_free(hal_tbm_bo *bo) +{ + tbm_shm_bo *bo_data = (tbm_shm_bo *)bo; + char buf[STRERR_BUFSIZE]; + + TBM_BACKEND_RETURN_IF_FAIL(bo_data != NULL); + + TBM_BACKEND_DBG(" [%s] bo:%p, shm:%d, size:%d\n", target_name(), bo_data, bo_data->fd, bo_data->size); + + if (bo_data->pBase) { + if (munmap(bo_data->pBase, bo_data->size) == -1) { + TBM_BACKEND_ERR("bo_data:%p fail to munmap(%s)\n", + bo_data, strerror_r(errno, buf, STRERR_BUFSIZE)); + } + } + + /* Free shm object*/ + close(bo_data->fd); + free(bo_data); +} + +static int +tbm_shm_bo_get_size(hal_tbm_bo *bo, hal_tbm_error *error) +{ + tbm_shm_bo *bo_data = (tbm_shm_bo *)bo; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return 0; + } + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return bo_data->size; +} + + +static hal_tbm_bo_memory_type +tbm_shm_bo_get_memory_type(hal_tbm_bo *bo, hal_tbm_error *error) +{ + tbm_shm_bo *bo_data = (tbm_shm_bo *)bo; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return HAL_TBM_BO_DEFAULT; + } + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return bo_data->flags_tbm; +} + +static hal_tbm_bo_handle +tbm_shm_bo_get_handle(hal_tbm_bo *bo, hal_tbm_bo_device_type device, hal_tbm_error *error) +{ + hal_tbm_bo_handle bo_handle; + tbm_shm_bo *bo_data = (tbm_shm_bo *)bo;; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return (hal_tbm_bo_handle) NULL; + } + + TBM_BACKEND_DBG("[%s] bo:%p, shm:%d, flags:%d, size:%d, %s\n", target_name(), bo_data, bo_data->fd, + bo_data->flags_tbm, bo_data->size, STR_DEVICE[device]); + + /*Get mapped bo_handle*/ + bo_handle = _shm_bo_handle(bo_data, device); + if (bo_handle.ptr == NULL) { + TBM_BACKEND_DBG("error Cannot get handle: shm:%d, device:%d\n", bo_data->fd, device); + + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return (hal_tbm_bo_handle) NULL; + } + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return bo_handle; +} + + +static hal_tbm_bo_handle +tbm_shm_bo_map(hal_tbm_bo *bo, hal_tbm_bo_device_type device, + hal_tbm_bo_access_option opt, hal_tbm_error *error) +{ + hal_tbm_bo_handle bo_handle; + tbm_shm_bo *bo_data = (tbm_shm_bo *)bo;; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return (hal_tbm_bo_handle) NULL; + } + + TBM_BACKEND_DBG(" [%s] bo:%p, shm:%d, %s, %s\n", target_name(), bo_data, bo_data->fd, STR_DEVICE[device], + STR_OPT[opt]); + + /*Get mapped bo_handle*/ + bo_handle = _shm_bo_handle(bo_data, device); + if (bo_handle.ptr == NULL) { + TBM_BACKEND_DBG("error Cannot get handle: shm:%d, device:%d, opt:%d\n", bo_data->fd, device, opt); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return (hal_tbm_bo_handle) NULL; + } + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return bo_handle; +} + +static hal_tbm_error +tbm_shm_bo_unmap(hal_tbm_bo *bo) + +{ + tbm_shm_bo *bo_data = (tbm_shm_bo *)bo;; + if (!bo_data) + return HAL_TBM_ERROR_INVALID_PARAMETER; + + TBM_BACKEND_DBG(" [%s] bo:%p, shm:%d\n", target_name(), bo_data, bo_data->fd); + + return HAL_TBM_ERROR_NONE; +} + +static hal_tbm_fd +tbm_shm_bo_export_fd(hal_tbm_bo *bo, hal_tbm_error *error) +{ + tbm_shm_bo *bo_data = (tbm_shm_bo *)bo;; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return -1; + } + + TBM_BACKEND_DBG(" [%s] bo:%p, shm:%d, flags:%d, size:%d\n", target_name(), bo_data, bo_data->fd, + bo_data->flags_tbm, bo_data->size); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return (hal_tbm_fd)bo_data->fd; +} + +static int +hal_backend_tbm_shm_exit(void *data) +{ + hal_tbm_backend_data *backend_data = (hal_tbm_backend_data *)data; + tbm_shm_bufmgr *bufmgr_data; + + TBM_BACKEND_RETURN_VAL_IF_FAIL(backend_data != NULL, -1); + + bufmgr_data = (tbm_shm_bufmgr *)backend_data->bufmgr; + TBM_BACKEND_RETURN_VAL_IF_FAIL(bufmgr_data != NULL, -1); + + if (backend_data->bufmgr_funcs) + free(backend_data->bufmgr_funcs); + if (backend_data->bo_funcs) + free(backend_data->bo_funcs); + + free(backend_data->bufmgr); + free(backend_data); + + return HAL_TBM_ERROR_NONE; +} + +static int +hal_backend_tbm_shm_init(void **data) +{ + hal_tbm_backend_data *backend_data = NULL; + hal_tbm_bufmgr_funcs *bufmgr_funcs = NULL; + hal_tbm_bo_funcs *bo_funcs = NULL; + tbm_shm_bufmgr *bufmgr_data = NULL; + + /* allocate a hal_tbm_backend_data */ + backend_data = calloc(1, sizeof(struct _hal_tbm_backend_data)); + if (!backend_data) { + TBM_BACKEND_ERR("fail to alloc backend_data!\n"); + *data = NULL; + return -1; + } + *data = backend_data; + + /* allocate a hal_tbm_bufmgr */ + bufmgr_data = calloc(1, sizeof(struct _tbm_shm_bufmgr)); + if (!bufmgr_data) { + TBM_BACKEND_ERR("fail to alloc bufmgr_data!\n"); + goto fail_alloc_bufmgr_data; + } + backend_data->bufmgr = (hal_tbm_bufmgr *)bufmgr_data; + + /* alloc and register bufmgr_funcs */ + bufmgr_funcs = calloc(1, sizeof(struct _hal_tbm_bufmgr_funcs)); + if (!bufmgr_funcs) { + TBM_BACKEND_ERR("fail to alloc bufmgr_funcs!\n"); + goto fail_alloc_bufmgr_funcs; + } + backend_data->bufmgr_funcs = bufmgr_funcs; + + bufmgr_funcs->bufmgr_get_capabilities = tbm_shm_bufmgr_get_capabilities; + bufmgr_funcs->bufmgr_get_supported_formats = tbm_shm_bufmgr_get_supported_formats; + bufmgr_funcs->bufmgr_get_plane_data = tbm_shm_bufmgr_get_plane_data; + bufmgr_funcs->bufmgr_alloc_bo = tbm_shm_bufmgr_alloc_bo; + bufmgr_funcs->bufmgr_alloc_bo_with_format = NULL; + bufmgr_funcs->bufmgr_import_fd = tbm_shm_bufmgr_import_fd; + bufmgr_funcs->bufmgr_import_key = NULL; + + /* alloc and register bo_funcs */ + bo_funcs = calloc(1, sizeof(struct _hal_tbm_bo_funcs)); + if (!bo_funcs) { + TBM_BACKEND_ERR("fail to alloc bo_funcs!\n"); + goto fail_alloc_bo_funcs; + } + backend_data->bo_funcs = bo_funcs; + + bo_funcs->bo_free = tbm_shm_bo_free; + bo_funcs->bo_get_size = tbm_shm_bo_get_size; + bo_funcs->bo_get_memory_types = tbm_shm_bo_get_memory_type; + bo_funcs->bo_get_handle = tbm_shm_bo_get_handle; + bo_funcs->bo_map = tbm_shm_bo_map; + bo_funcs->bo_unmap = tbm_shm_bo_unmap; + bo_funcs->bo_lock = NULL; + bo_funcs->bo_unlock = NULL; + bo_funcs->bo_export_fd = tbm_shm_bo_export_fd; + bo_funcs->bo_export_key = NULL; + + TBM_BACKEND_DBG("tbm_shm_init: done\n"); + TBM_BACKEND_DBG("no drm_fd\n"); + + return HAL_TBM_ERROR_NONE; + +fail_alloc_bo_funcs: + free(bufmgr_funcs); +fail_alloc_bufmgr_funcs: + free(bufmgr_data); +fail_alloc_bufmgr_data: + free(backend_data); + + *data = NULL; + + return -1; +} + +hal_backend hal_backend_tbm_data = { + "shm", + "Samsung", + HAL_ABI_VERSION_TIZEN_6_5, + hal_backend_tbm_shm_init, + hal_backend_tbm_shm_exit +}; + -- cgit v1.2.3