From 2a2f523a32947b6f2e6564645e87cc170e6a1218 Mon Sep 17 00:00:00 2001 From: Changyeon Lee Date: Wed, 22 Jun 2022 17:04:50 +0900 Subject: remove legacy libtbm backend Change-Id: Id9b3842a254b72456719f1667f2de2d6c40af254 --- configure.ac | 21 +- libtbm-shm.manifest | 5 - packaging/hal-backend-tbm-shm.manifest | 5 + packaging/hal-backend-tbm-shm.spec | 54 ++ packaging/libhal-backend-tbm-shm.manifest | 5 - packaging/libtbm-shm.manifest | 5 - packaging/libtbm-shm.spec | 81 --- src/Makefile.am | 13 +- 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 | 936 -------------------------- src/libtbm-shm/Makefile.am | 11 - src/libtbm-shm/tbm_bufmgr_shm.c | 970 --------------------------- src/tbm_backend_log.c | 74 ++ src/tbm_backend_log.h | 105 +++ src/tbm_backend_shm.c | 936 ++++++++++++++++++++++++++ 17 files changed, 1189 insertions(+), 2223 deletions(-) delete mode 100644 libtbm-shm.manifest create mode 100644 packaging/hal-backend-tbm-shm.manifest create mode 100644 packaging/hal-backend-tbm-shm.spec delete mode 100644 packaging/libhal-backend-tbm-shm.manifest delete mode 100644 packaging/libtbm-shm.manifest delete mode 100644 packaging/libtbm-shm.spec delete mode 100644 src/libhal-backend-tbm-shm/Makefile.am delete mode 100644 src/libhal-backend-tbm-shm/tbm_backend_log.c delete mode 100644 src/libhal-backend-tbm-shm/tbm_backend_log.h delete mode 100644 src/libhal-backend-tbm-shm/tbm_backend_shm.c delete mode 100644 src/libtbm-shm/Makefile.am delete mode 100644 src/libtbm-shm/tbm_bufmgr_shm.c create mode 100644 src/tbm_backend_log.c create mode 100644 src/tbm_backend_log.h create mode 100644 src/tbm_backend_shm.c diff --git a/configure.ac b/configure.ac index 646bb16..5241e4a 100644 --- a/configure.ac +++ b/configure.ac @@ -18,7 +18,7 @@ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. AC_PREREQ(2.60) -AC_INIT(libtbm-shm, 2.0.0) +AC_INIT(hal-backend-tbm-shm, 2.0.0) AC_USE_SYSTEM_EXTENSIONS AC_CONFIG_SRCDIR([Makefile.am]) AM_INIT_AUTOMAKE([dist-bzip2]) @@ -36,7 +36,6 @@ AC_FUNC_ALLOCA # Enable quiet compiles on automake 1.11. 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) @@ -57,35 +56,21 @@ AC_ARG_ENABLE(cachectrl, if test "x$CACHE_CTRL" = xyes; then AC_DEFINE(ENABLE_CACHECRTL, 1, [Enable cache control]) fi - -LIBTBM_SHM_CFLAGS="$LIBDRM_CFLAGS $LIBTBM_CFLAGS $DLOG_CFLAGS " -LIBTBM_SHM_LIBS="$LIBDRM_LIBS $LIBTBM_LIBS $DLOG_LIBS " -AC_SUBST(LIBTBM_SHM_CFLAGS) -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 "" echo "CFLAGS : $CFLAGS" 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 "HAL_LIBDIR : $HAL_LIBDIR" echo "" diff --git a/libtbm-shm.manifest b/libtbm-shm.manifest deleted file mode 100644 index 97e8c31..0000000 --- a/libtbm-shm.manifest +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/packaging/hal-backend-tbm-shm.manifest b/packaging/hal-backend-tbm-shm.manifest new file mode 100644 index 0000000..75b0fa5 --- /dev/null +++ b/packaging/hal-backend-tbm-shm.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/hal-backend-tbm-shm.spec b/packaging/hal-backend-tbm-shm.spec new file mode 100644 index 0000000..526883a --- /dev/null +++ b/packaging/hal-backend-tbm-shm.spec @@ -0,0 +1,54 @@ +Name: hal-backend-tbm-shm +Version: 3.0.1 +Release: 1 +License: MIT +Summary: hal-backend-tbm module for shm +Group: System/Libraries +ExcludeArch: i586 +Source0: %{name}-%{version}.tar.gz +Source1001: %{name}.manifest + +BuildRequires: pkgconfig(libdrm) +BuildRequires: pkgconfig(hal-api-common) +BuildRequires: pkgconfig(hal-api-tbm) +BuildRequires: pkgconfig(dlog) + +%description +description: 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} . + +%build + +%reconfigure --prefix=%{_prefix} --libdir=%{_libdir} \ + --with-hal-libdir=%{_hal_libdir} \ + --disable-cachectrl \ + FLAGS="${CFLAGS} -Wall -Werror" LDFLAGS="${LDFLAGS} -Wl,--hash-style=both -Wl,--as-needed" + +make %{?_smp_mflags} + +%install +rm -rf %{buildroot} +%make_install + +# make rule and license files +mkdir -p %{buildroot}%{_hal_licensedir}/libhal-backend-tbm-shm +cp -af COPYING %{buildroot}%{_hal_licensedir}/libhal-backend-tbm-shm + +%post +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 -p /sbin/ldconfig + +%files +%defattr(-,root,root,-) +%manifest %{name}.manifest +%{_hal_licensedir}/libhal-backend-tbm-shm/COPYING +%{_hal_libdir}/libhal-backend-*.so* diff --git a/packaging/libhal-backend-tbm-shm.manifest b/packaging/libhal-backend-tbm-shm.manifest deleted file mode 100644 index 75b0fa5..0000000 --- a/packaging/libhal-backend-tbm-shm.manifest +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/packaging/libtbm-shm.manifest b/packaging/libtbm-shm.manifest deleted file mode 100644 index 75b0fa5..0000000 --- a/packaging/libtbm-shm.manifest +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/packaging/libtbm-shm.spec b/packaging/libtbm-shm.spec deleted file mode 100644 index 2acd41d..0000000 --- a/packaging/libtbm-shm.spec +++ /dev/null @@ -1,81 +0,0 @@ -Name: libtbm-shm -Version: 3.0.1 -Release: 1 -License: MIT -Summary: Tizen Buffer Manager - drm shm backend -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 \ - --with-hal-libdir=%{_hal_libdir} \ - --disable-cachectrl \ - FLAGS="${CFLAGS} -Wall -Werror" LDFLAGS="${LDFLAGS} -Wl,--hash-style=both -Wl,--as-needed" - -make %{?_smp_mflags} - -%install -rm -rf %{buildroot} -%make_install - -# make rule and license files -mkdir -p %{buildroot}%{_hal_licensedir}/libhal-backend-tbm-shm -cp -af COPYING %{buildroot}%{_hal_licensedir}/libhal-backend-tbm-shm - - -%post -if [ -f %{_libdir}/bufmgr/libtbm_default.so ]; then - rm -rf %{_libdir}/bufmgr/libtbm_default.so -fi -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 -%{_hal_licensedir}/libhal-backend-tbm-shm/COPYING -%{_hal_libdir}/libhal-backend-*.so* diff --git a/src/Makefile.am b/src/Makefile.am index 9cbd3b5..3477f41 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1 +1,12 @@ -SUBDIRS = libtbm-shm libhal-backend-tbm-shm +AM_CFLAGS = \ + @LIBHAL_BACKEND_TBM_SHM_CFLAGS@ \ + -I$(top_srcdir) \ + -I$(top_srcdir)/src + +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/Makefile.am b/src/libhal-backend-tbm-shm/Makefile.am deleted file mode 100644 index e2c1f91..0000000 --- a/src/libhal-backend-tbm-shm/Makefile.am +++ /dev/null @@ -1,12 +0,0 @@ -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 deleted file mode 100644 index 641b2b8..0000000 --- a/src/libhal-backend-tbm-shm/tbm_backend_log.c +++ /dev/null @@ -1,74 +0,0 @@ -/************************************************************************** - -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 deleted file mode 100644 index d405f05..0000000 --- a/src/libhal-backend-tbm-shm/tbm_backend_log.h +++ /dev/null @@ -1,105 +0,0 @@ -/************************************************************************** - -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 deleted file mode 100644 index 98b51d5..0000000 --- a/src/libhal-backend-tbm-shm/tbm_backend_shm.c +++ /dev/null @@ -1,936 +0,0 @@ -/************************************************************************** - - 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]; - hal_tbm_fd fd; - - 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; - } - - fd = dup(key); - if (fd < 0) { - TBM_BACKEND_ERR("dup failed: %m"); - free(bo_data); - if (error) - *error = HAL_TBM_ERROR_INVALID_PARAMETER; - return 0; - } - - bo_data->size = real_size; - bo_data->fd = fd; - bo_data->flags_tbm = HAL_TBM_BO_DEFAULT | HAL_TBM_BO_NONCACHABLE; - - TBM_BACKEND_DBG(" [%s] bo:%p, shm:%d, flags:%d, size:%d imported_fd:%d\n", target_name(), bo_data, bo_data->fd, - bo_data->flags_tbm, bo_data->size, key); - - 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; - hal_tbm_fd fd; - - if (!bo_data) { - if (error) - *error = HAL_TBM_ERROR_INVALID_PARAMETER; - return -1; - } - - fd = dup(bo_data->fd); - if (fd < 0) { - TBM_BACKEND_ERR("dup failed: %m"); - if (error) - *error = HAL_TBM_ERROR_INVALID_PARAMETER; - return -1; - } - - TBM_BACKEND_DBG(" [%s] bo:%p, shm:%d, flags:%d, size:%d, exported_fd:%d\n", target_name(), bo_data, bo_data->fd, - bo_data->flags_tbm, bo_data->size, fd); - - if (error) - *error = HAL_TBM_ERROR_NONE; - - return 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 -}; - diff --git a/src/libtbm-shm/Makefile.am b/src/libtbm-shm/Makefile.am deleted file mode 100644 index 05a380b..0000000 --- a/src/libtbm-shm/Makefile.am +++ /dev/null @@ -1,11 +0,0 @@ -AM_CFLAGS = \ - @LIBTBM_SHM_CFLAGS@ \ - -I$(top_srcdir) \ - -I$(top_srcdir)/src - -libtbm_shm_la_LTLIBRARIES = libtbm_shm.la -libtbm_shm_ladir = /${bufmgr_dir} -libtbm_shm_la_LIBADD = @LIBTBM_SHM_LIBS@ - -libtbm_shm_la_SOURCES = \ - tbm_bufmgr_shm.c diff --git a/src/libtbm-shm/tbm_bufmgr_shm.c b/src/libtbm-shm/tbm_bufmgr_shm.c deleted file mode 100644 index 88c6522..0000000 --- a/src/libtbm-shm/tbm_bufmgr_shm.c +++ /dev/null @@ -1,970 +0,0 @@ -/************************************************************************** - - 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 - - -#define DEBUG -#define TBM_COLOR_FORMAT_COUNT 4 -#define STRERR_BUFSIZE 128 - -#ifdef DEBUG -#define LOG_TAG "TBM_BACKEND" -#include -static int bDebug = 0; - -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; -} -#define TBM_SHM_LOG(fmt, args...) LOGE("\033[31m" "[%s]" fmt "\033[0m", target_name(), ##args) -#define DBG(fmt, args...) if (bDebug&01) LOGE(fmt, ##args) -#else -#define TBM_SHM_LOG(...) -#define DBG(...) -#endif - -#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) - -/* check condition */ -#define SHM_RETURN_IF_FAIL(cond) {\ - if (!(cond)) {\ - TBM_SHM_LOG("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\ - return;\ - } \ -} -#define SHM_RETURN_VAL_IF_FAIL(cond, val) {\ - if (!(cond)) {\ - TBM_SHM_LOG("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\ - return val;\ - } \ -} - -typedef struct _tbm_bufmgr_shm *tbm_bufmgr_shm; -typedef struct _tbm_bo_shm *tbm_bo_shm; - -/* tbm buffor object for shm */ -struct _tbm_bo_shm { - key_t key; - void * pBase; - - unsigned int size; - - unsigned int flags_tbm; - tbm_fd fd; - - pthread_mutex_t mutex; - int device; - int opt; -}; - -/* tbm bufmgr private for shm */ -struct _tbm_bufmgr_shm { - int isLocal; - void* hashBos; - - tbm_backend_bufmgr_func *bufmgr_func; - tbm_backend_bo_func *bo_func; - - tbm_bufmgr bufmgr; -}; - -char *STR_DEVICE[] = { - "DEF", - "CPU", - "2D", - "3D", - "MM" -}; - -char *STR_OPT[] = { - "NONE", - "RD", - "WR", - "RDWR" -}; - -uint32_t tbm_shm_color_format_list[TBM_COLOR_FORMAT_COUNT] = { - TBM_FORMAT_ARGB8888, - TBM_FORMAT_XRGB8888, - TBM_FORMAT_NV12, - TBM_FORMAT_YUV420 - }; - -static tbm_bo_handle -_shm_bo_handle(tbm_bo_shm bo_shm, int device) -{ - tbm_bo_handle bo_handle; - memset(&bo_handle, 0x0, sizeof(uint64_t)); - - switch (device) { - case TBM_DEVICE_DEFAULT: - case TBM_DEVICE_2D: - bo_handle.u32 = (uint32_t) bo_shm->fd; - break; - case TBM_DEVICE_CPU: - bo_handle.ptr = (void *) bo_shm->pBase; - break; - case TBM_DEVICE_3D: - case TBM_DEVICE_MM: - default: - TBM_SHM_LOG("[libtbm-shm:%d] error %s:%d Not supported device:%d\n", getpid(), __FUNCTION__, __LINE__, device); - bo_handle.ptr = (void *) bo_shm->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 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 (tbm_fd)fd; -} - -static tbm_bufmgr_capability -tbm_shm_bufmgr_get_capabilities(tbm_backend_bufmgr_data *bufmgr_data, tbm_error_e *error) -{ - tbm_bufmgr_capability capabilities = TBM_BUFMGR_CAPABILITY_NONE; - - capabilities = TBM_BUFMGR_CAPABILITY_SHARE_FD; - - if (error) - *error = TBM_ERROR_NONE; - - return capabilities; -} - -static tbm_error_e -tbm_shm_bufmgr_bind_native_display(tbm_backend_bufmgr_data *bufmgr_data, tbm_native_display *native_display) -{ - return TBM_ERROR_NONE; -} - -static tbm_error_e -tbm_shm_bufmgr_get_supported_formats(tbm_backend_bufmgr_data *bufmgr_data, - uint32_t **formats, uint32_t *num) -{ - tbm_bufmgr_shm bufmgr_shm = (tbm_bufmgr_shm)bufmgr_data; - uint32_t *color_formats; - - TBM_RETURN_VAL_IF_FAIL(bufmgr_shm != NULL, TBM_ERROR_INVALID_PARAMETER); - - color_formats = (uint32_t *)calloc(1, sizeof(uint32_t) * TBM_COLOR_FORMAT_COUNT); - if (color_formats == NULL) - return 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_DBG("supported format count = %d\n", *num); - - return TBM_ERROR_NONE; -} - -static tbm_error_e -tbm_shm_bufmgr_get_plane_data(tbm_backend_bufmgr_data *bufmgr_data, - tbm_format format, int plane_idx, int width, - int height, uint32_t *size, uint32_t *offset, - uint32_t *pitch, int *bo_idx) - -{ - int ret = TBM_ERROR_NONE; - int bpp; - int _offset = 0; - int _pitch = 0; - int _size = 0; - int _bo_idx = 0; - - switch (format) { - /* 16 bpp RGB */ - case TBM_FORMAT_XRGB4444: - case TBM_FORMAT_XBGR4444: - case TBM_FORMAT_RGBX4444: - case TBM_FORMAT_BGRX4444: - case TBM_FORMAT_ARGB4444: - case TBM_FORMAT_ABGR4444: - case TBM_FORMAT_RGBA4444: - case TBM_FORMAT_BGRA4444: - case TBM_FORMAT_XRGB1555: - case TBM_FORMAT_XBGR1555: - case TBM_FORMAT_RGBX5551: - case TBM_FORMAT_BGRX5551: - case TBM_FORMAT_ARGB1555: - case TBM_FORMAT_ABGR1555: - case TBM_FORMAT_RGBA5551: - case TBM_FORMAT_BGRA5551: - case 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 TBM_FORMAT_RGB888: - case 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 TBM_FORMAT_XRGB8888: - case TBM_FORMAT_XBGR8888: - case TBM_FORMAT_RGBX8888: - case TBM_FORMAT_BGRX8888: - case TBM_FORMAT_ARGB8888: - case TBM_FORMAT_ABGR8888: - case TBM_FORMAT_RGBA8888: - case 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 TBM_FORMAT_YUYV: - case TBM_FORMAT_YVYU: - case TBM_FORMAT_UYVY: - case TBM_FORMAT_VYUY: - case 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 TBM_FORMAT_NV12: - case 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 TBM_FORMAT_NV16: - case 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 TBM_FORMAT_YUV410: - case TBM_FORMAT_YVU410: - bpp = 9; - _bo_idx = 0; - break; - case TBM_FORMAT_YUV411: - case TBM_FORMAT_YVU411: - case TBM_FORMAT_YUV420: - case 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 TBM_FORMAT_YUV422: - case 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 TBM_FORMAT_YUV444: - case 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 tbm_backend_bo_data * -tbm_shm_bufmgr_alloc_bo(tbm_backend_bufmgr_data *bufmgr_data, unsigned int size, - tbm_bo_memory_type flags, tbm_error_e *error) - -{ - tbm_bufmgr_shm bufmgr_shm = (tbm_bufmgr_shm)bufmgr_data; - tbm_bo_shm bo_shm; - - if (bufmgr_shm == NULL) { - TBM_ERR("bufmgr_data is null\n"); - if (error) *error = TBM_ERROR_INVALID_PARAMETER; - return NULL; - } - - tbm_fd fd = -1; - - if (flags & TBM_BO_SCANOUT) - TBM_SHM_LOG("[libtbm-shm:%d] warning %s:%d TBM_BO_SCANOUT ins't supported\n", getpid(), __FUNCTION__, __LINE__); - - bo_shm = calloc(1, sizeof(struct _tbm_bo_shm)); - if (!bo_shm) { - TBM_SHM_LOG("[libtbm-shm:%d] error %s:%d Fail to allocate the bo private\n", getpid(), __FUNCTION__, __LINE__); - if (error) *error = TBM_ERROR_INVALID_OPERATION; - return 0; - } - - bo_shm->fd = -1; - fd = _shm_create_anonymous_file((off_t)size); - if (fd < 0) { - TBM_SHM_LOG("[libtbm-shm:%d] error %s:%d Fail to create a buffer file(size: %d) %m\n", getpid(), - __FUNCTION__, __LINE__, size); - free(bo_shm); - if (error) *error = TBM_ERROR_INVALID_OPERATION; - return 0; - } - - bo_shm->fd = fd; - bo_shm->pBase = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (bo_shm->pBase == MAP_FAILED) { - TBM_SHM_LOG("[libtbm-shm:%d] error %s:%d Fail to mmap: %m\n", getpid(), - __FUNCTION__, __LINE__); - close(fd); - free(bo_shm); - if (error) *error = TBM_ERROR_INVALID_OPERATION; - return 0; - } - - bo_shm->size = size; - bo_shm->flags_tbm = TBM_BO_DEFAULT | TBM_BO_NONCACHABLE; - - DBG(" [%s] bo:%p, fd:%d, flags:%d, size:%d\n", target_name(), bo_shm, bo_shm->fd, bo_shm->flags_tbm, bo_shm->size); - if (error) *error = TBM_ERROR_NONE; - - return (tbm_backend_bo_data *) bo_shm; -} - -static tbm_backend_bo_data * -tbm_shm_bufmgr_import_fd(tbm_backend_bufmgr_data *bufmgr_data, tbm_fd key, tbm_error_e *error) -{ - tbm_bo_shm bo_shm; - int real_size; - char buf[STRERR_BUFSIZE]; - - if (key < 0) { - TBM_ERR("Invalid fd(%d)\n", key); - if (error) - *error = TBM_ERROR_INVALID_PARAMETER; - return 0; - } - - bo_shm = calloc(1, sizeof(struct _tbm_bo_shm)); - if (!bo_shm) { - TBM_ERR("Fail to allocate the bo private\n"); - if (error) - *error = TBM_ERROR_INVALID_OPERATION; - return 0; - } - - real_size = lseek(key, 0, SEEK_END); - if (real_size < 0) { - TBM_ERR("Fail to lseek(err:%s)\n", - strerror_r(errno, buf, STRERR_BUFSIZE)); - free(bo_shm); - if (error) - *error = TBM_ERROR_INVALID_OPERATION; - return 0; - } - - bo_shm->pBase = mmap(NULL, real_size, - PROT_READ | PROT_WRITE, MAP_SHARED, key, 0); - if (bo_shm->pBase == MAP_FAILED) { - TBM_ERR("Fail to mmap (size:%d, err:%s)\n", - real_size, strerror_r(errno, buf, STRERR_BUFSIZE)); - free(bo_shm); - if (error) - *error = TBM_ERROR_INVALID_OPERATION; - return 0; - } - - bo_shm->size = real_size; - bo_shm->fd = key; - bo_shm->flags_tbm = TBM_BO_DEFAULT | TBM_BO_NONCACHABLE; - - DBG(" [%s] bo:%p, shm:%d, flags:%d, size:%d\n", target_name(), bo_shm, bo_shm->fd, - bo_shm->flags_tbm, bo_shm->size); - if (error) - *error = TBM_ERROR_NONE; - - return (tbm_backend_bo_data *) bo_shm; -} - -static void -tbm_shm_bo_free(tbm_backend_bo_data *bo_data) -{ - tbm_bo_shm bo_shm = (tbm_bo_shm)bo_data; - char buf[STRERR_BUFSIZE]; - - SHM_RETURN_IF_FAIL(bo_shm != NULL); - - DBG(" [%s] bo:%p, shm:%d, size:%d\n", target_name(), bo_shm, bo_shm->fd, bo_shm->size); - - if (bo_shm->pBase) { - if (munmap(bo_shm->pBase, bo_shm->size) == -1) { - TBM_ERR("bo_shm:%p fail to munmap(%s)\n", - bo_shm, strerror_r(errno, buf, STRERR_BUFSIZE)); - } - } - - /* Free shm object*/ - close(bo_shm->fd); - free(bo_shm); -} - -static int -tbm_shm_bo_get_size(tbm_backend_bo_data *bo_data, tbm_error_e *error) -{ - tbm_bo_shm bo_shm = (tbm_bo_shm)bo_data; - - if (!bo_shm) { - if (error) - *error = TBM_ERROR_INVALID_PARAMETER; - return 0; - } - - if (error) - *error = TBM_ERROR_NONE; - - return bo_shm->size; -} - - -static tbm_bo_memory_type -tbm_shm_bo_get_memory_type(tbm_backend_bo_data *bo_data, tbm_error_e *error) -{ - tbm_bo_shm bo_shm = (tbm_bo_shm)bo_data; - - if (!bo_shm) { - if (error) - *error = TBM_ERROR_INVALID_PARAMETER; - return TBM_BO_DEFAULT; - } - - if (error) - *error = TBM_ERROR_NONE; - - return bo_shm->flags_tbm; -} - -static tbm_bo_handle -tbm_shm_bo_get_handle(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, tbm_error_e *error) -{ - tbm_bo_handle bo_handle; - tbm_bo_shm bo_shm = (tbm_bo_shm)bo_data;; - - if (!bo_shm) { - if (error) - *error = TBM_ERROR_INVALID_PARAMETER; - return (tbm_bo_handle) NULL; - } - - DBG("[%s] bo:%p, shm:%d, flags:%d, size:%d, %s\n", target_name(), bo_shm, bo_shm->fd, - bo_shm->flags_tbm, bo_shm->size, STR_DEVICE[device]); - - /*Get mapped bo_handle*/ - bo_handle = _shm_bo_handle(bo_shm, device); - if (bo_handle.ptr == NULL) { - TBM_SHM_LOG("error Cannot get handle: shm:%d, device:%d\n", bo_shm->fd, device); - - if (error) - *error = TBM_ERROR_INVALID_OPERATION; - return (tbm_bo_handle) NULL; - } - - if (error) - *error = TBM_ERROR_NONE; - - return bo_handle; -} - - -static tbm_bo_handle -tbm_shm_bo_map(tbm_backend_bo_data *bo_data, tbm_bo_device_type device, - tbm_bo_access_option opt, tbm_error_e *error) -{ - tbm_bo_handle bo_handle; - tbm_bo_shm bo_shm = (tbm_bo_shm)bo_data;; - - if (!bo_shm) { - if (error) - *error = TBM_ERROR_INVALID_PARAMETER; - return (tbm_bo_handle) NULL; - } - - DBG(" [%s] bo:%p, shm:%d, %s, %s\n", target_name(), bo_shm, bo_shm->fd, STR_DEVICE[device], - STR_OPT[opt]); - - /*Get mapped bo_handle*/ - bo_handle = _shm_bo_handle(bo_shm, device); - if (bo_handle.ptr == NULL) { - TBM_SHM_LOG("error Cannot get handle: shm:%d, device:%d, opt:%d\n", bo_shm->fd, device, opt); - if (error) - *error = TBM_ERROR_INVALID_OPERATION; - return (tbm_bo_handle) NULL; - } - - if (error) - *error = TBM_ERROR_NONE; - - return bo_handle; -} - -static tbm_error_e -tbm_shm_bo_unmap(tbm_backend_bo_data *bo_data) - -{ - tbm_bo_shm bo_shm = (tbm_bo_shm)bo_data;; - if (!bo_shm) - return TBM_ERROR_INVALID_PARAMETER; - - DBG(" [%s] bo:%p, shm:%d\n", target_name(), bo_shm, bo_shm->fd); - - return TBM_ERROR_NONE; -} - -static tbm_fd -tbm_sprd_bo_export_fd(tbm_backend_bo_data *bo_data, tbm_error_e *error) -{ - tbm_bo_shm bo_shm = (tbm_bo_shm)bo_data;; - - if (!bo_shm) { - if (error) - *error = TBM_ERROR_INVALID_PARAMETER; - return -1; - } - - DBG(" [%s] bo:%p, shm:%d, flags:%d, size:%d\n", target_name(), bo_shm, bo_shm->fd, - bo_shm->flags_tbm, bo_shm->size); - - if (error) - *error = TBM_ERROR_NONE; - - return bo_shm->fd; -} - -static tbm_backend_bufmgr_data * -tbm_shm_init(tbm_bufmgr bufmgr, tbm_error_e *error) -{ - tbm_bufmgr_shm bufmgr_shm = NULL; - tbm_backend_bufmgr_func *bufmgr_func = NULL; - tbm_backend_bo_func *bo_func = NULL; - tbm_error_e err; - - if (!bufmgr) { - TBM_ERR("bufmgr is null.\n"); - if (error) - *error = TBM_ERROR_INVALID_PARAMETER; - return NULL; - } - - bufmgr_shm = calloc(1, sizeof(struct _tbm_bufmgr_shm)); - if (!bufmgr_shm) { - TBM_ERR("fail to alloc bufmgr_shm!\n"); - if (error) - *error = TBM_ERROR_OUT_OF_MEMORY; - return NULL; - } - - /* alloc and register bufmgr_funcs */ - bufmgr_func = tbm_backend_bufmgr_alloc_bufmgr_func(bufmgr, &err); - if (!bufmgr_func) { - TBM_ERR("fail to alloc bufmgr_func! err(%d)\n", err); - if (error) - *error = TBM_ERROR_OUT_OF_MEMORY; - goto fail_alloc_bufmgr_func; - } - - bufmgr_func->bufmgr_get_capabilities = tbm_shm_bufmgr_get_capabilities; - bufmgr_func->bufmgr_bind_native_display = tbm_shm_bufmgr_bind_native_display; - bufmgr_func->bufmgr_get_supported_formats = tbm_shm_bufmgr_get_supported_formats; - bufmgr_func->bufmgr_get_plane_data = tbm_shm_bufmgr_get_plane_data; - bufmgr_func->bufmgr_alloc_bo = tbm_shm_bufmgr_alloc_bo; - bufmgr_func->bufmgr_alloc_bo_with_format = NULL; - bufmgr_func->bufmgr_import_fd = tbm_shm_bufmgr_import_fd; - bufmgr_func->bufmgr_import_key = NULL; - - err = tbm_backend_bufmgr_register_bufmgr_func(bufmgr, bufmgr_func); - if (err != TBM_ERROR_NONE) { - TBM_ERR("fail to register bufmgr_func! err(%d)\n", err); - if (error) - *error = TBM_ERROR_INVALID_OPERATION; - goto fail_register_bufmgr_func; - } - bufmgr_shm->bufmgr_func = bufmgr_func; - - /* alloc and register bo_funcs */ - bo_func = tbm_backend_bufmgr_alloc_bo_func(bufmgr, &err); - if (!bo_func) { - TBM_ERR("fail to alloc bo_func! err(%d)\n", err); - if (error) - *error = TBM_ERROR_OUT_OF_MEMORY; - goto fail_alloc_bo_func; - } - - bo_func->bo_free = tbm_shm_bo_free; - bo_func->bo_get_size = tbm_shm_bo_get_size; - bo_func->bo_get_memory_types = tbm_shm_bo_get_memory_type; - bo_func->bo_get_handle = tbm_shm_bo_get_handle; - bo_func->bo_map = tbm_shm_bo_map; - bo_func->bo_unmap = tbm_shm_bo_unmap; - bo_func->bo_lock = NULL; - bo_func->bo_unlock = NULL; - bo_func->bo_export_fd = tbm_sprd_bo_export_fd; - bo_func->bo_export_key = NULL; - - err = tbm_backend_bufmgr_register_bo_func(bufmgr, bo_func); - if (err != TBM_ERROR_NONE) { - TBM_ERR("fail to register bo_func! err(%d)\n", err); - if (error) - *error = TBM_ERROR_INVALID_OPERATION; - goto fail_register_bo_func; - } - bufmgr_shm->bo_func = bo_func; - - TBM_DBG("tbm_shm_init: done\n"); - - if (error) - *error = TBM_ERROR_NONE; - - bufmgr_shm->bufmgr = bufmgr; - - return (tbm_backend_bufmgr_data *)bufmgr_shm; - -fail_register_bo_func: - tbm_backend_bufmgr_free_bo_func(bufmgr, bo_func); -fail_alloc_bo_func: -fail_register_bufmgr_func: - tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_func); -fail_alloc_bufmgr_func: - free(bufmgr_shm); - return NULL; -} - -static void -tbm_shm_deinit(tbm_backend_bufmgr_data *bufmgr_data) -{ - tbm_bufmgr_shm bufmgr_shm = (tbm_bufmgr_shm)bufmgr_data; - tbm_bufmgr bufmgr; - - TBM_RETURN_IF_FAIL(bufmgr_shm != NULL); - - bufmgr = bufmgr_shm->bufmgr; - - tbm_backend_bufmgr_free_bufmgr_func(bufmgr, bufmgr_shm->bufmgr_func); - tbm_backend_bufmgr_free_bo_func(bufmgr, bufmgr_shm->bo_func); - - free(bufmgr_shm); -} - - -tbm_backend_module tbm_backend_module_data = { - "shm", - "Samsung", - TBM_BACKEND_ABI_VERSION_3_0, - tbm_shm_init, - tbm_shm_deinit -}; - diff --git a/src/tbm_backend_log.c b/src/tbm_backend_log.c new file mode 100644 index 0000000..641b2b8 --- /dev/null +++ b/src/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/tbm_backend_log.h b/src/tbm_backend_log.h new file mode 100644 index 0000000..d405f05 --- /dev/null +++ b/src/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/tbm_backend_shm.c b/src/tbm_backend_shm.c new file mode 100644 index 0000000..98b51d5 --- /dev/null +++ b/src/tbm_backend_shm.c @@ -0,0 +1,936 @@ +/************************************************************************** + + 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]; + hal_tbm_fd fd; + + 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; + } + + fd = dup(key); + if (fd < 0) { + TBM_BACKEND_ERR("dup failed: %m"); + free(bo_data); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return 0; + } + + bo_data->size = real_size; + bo_data->fd = fd; + bo_data->flags_tbm = HAL_TBM_BO_DEFAULT | HAL_TBM_BO_NONCACHABLE; + + TBM_BACKEND_DBG(" [%s] bo:%p, shm:%d, flags:%d, size:%d imported_fd:%d\n", target_name(), bo_data, bo_data->fd, + bo_data->flags_tbm, bo_data->size, key); + + 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; + hal_tbm_fd fd; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return -1; + } + + fd = dup(bo_data->fd); + if (fd < 0) { + TBM_BACKEND_ERR("dup failed: %m"); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return -1; + } + + TBM_BACKEND_DBG(" [%s] bo:%p, shm:%d, flags:%d, size:%d, exported_fd:%d\n", target_name(), bo_data, bo_data->fd, + bo_data->flags_tbm, bo_data->size, fd); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return 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