diff options
author | Changyeon Lee <cyeon.lee@samsung.com> | 2021-06-02 18:29:03 +0900 |
---|---|---|
committer | Changyeon Lee <cyeon.lee@samsung.com> | 2021-09-02 11:55:18 +0900 |
commit | 3ddd02cb5a99a349dccf89f69e06d45a5a18a910 (patch) | |
tree | be52fceb23cb8985c672c40b2999f4f033f4dc41 | |
parent | 63d9772f344e879fa86e0ff702146c8d5a258ca7 (diff) | |
download | libtbm-msm-3ddd02cb5a99a349dccf89f69e06d45a5a18a910.tar.gz libtbm-msm-3ddd02cb5a99a349dccf89f69e06d45a5a18a910.tar.bz2 libtbm-msm-3ddd02cb5a99a349dccf89f69e06d45a5a18a910.zip |
make hal-backend-tbm-msm package
Change-Id: I32fae2a84b8775ff5687845473dc663752ac2b5e
-rw-r--r-- | configure.ac | 28 | ||||
-rw-r--r-- | packaging/libhal-backend-tbm-msm.manifest | 5 | ||||
-rw-r--r-- | packaging/libtbm-msm.spec | 42 | ||||
-rw-r--r-- | src/Makefile.am | 13 | ||||
-rw-r--r-- | src/libhal-backend-tbm-msm/Makefile.am | 12 | ||||
-rw-r--r-- | src/libhal-backend-tbm-msm/tbm_backend_log.c | 74 | ||||
-rw-r--r-- | src/libhal-backend-tbm-msm/tbm_backend_log.h | 105 | ||||
-rw-r--r-- | src/libhal-backend-tbm-msm/tbm_backend_msm.c | 1463 | ||||
-rw-r--r-- | src/libtbm-msm/Makefile.am | 12 | ||||
-rw-r--r-- | src/libtbm-msm/tbm_bufmgr_msm.c (renamed from src/tbm_bufmgr_msm.c) | 0 |
10 files changed, 1728 insertions, 26 deletions
diff --git a/configure.ac b/configure.ac index f535ec3..8aa72f2 100644 --- a/configure.ac +++ b/configure.ac @@ -38,21 +38,39 @@ m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) PKG_CHECK_MODULES(LIBDRM, libdrm) PKG_CHECK_MODULES(LIBTBM, libtbm) +PKG_CHECK_MODULES(GBM, gbm) +PKG_CHECK_MODULES(HAL_API_COMMON, hal-api-common) +PKG_CHECK_MODULES(HAL_API_TBM, hal-api-tbm) PKG_CHECK_MODULES(DLOG, dlog) PKG_CHECK_MODULES(LIBUDEV, libudev) -PKG_CHECK_MODULES(GBM, gbm) -LIBTBM_MSM_CFLAGS="$LIBDRM_CFLAGS $LIBTBM_CFLAGS $DLOG_CFLAGS $LIBUDEV_CFLAGS $GBM_CFLAGS " -LIBTBM_MSM_LIBS="$LIBDRM_LIBS $LIBTBM_LIBS $DLOG_LIBS $LIBUDEV_LIBS $GBM_LIBS " +# set the data dir for the tdm 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) +# for libtbm-msm +LIBTBM_MSM_CFLAGS="$LIBDRM_CFLAGS $LIBTBM_CFLAGS $DLOG_CFLAGS $LIBUDEV_CFLAGS $GBM_CFLAGS" +LIBTBM_MSM_LIBS="$LIBDRM_LIBS $LIBTBM_LIBS $DLOG_LIBS $LIBUDEV_LIBS $GBM_LIBS" AC_SUBST(LIBTBM_MSM_CFLAGS) AC_SUBST(LIBTBM_MSM_LIBS) bufmgr_dir=${libdir#*/} AC_SUBST(bufmgr_dir) +# for libhal-backend-tbm-msm +LIBHAL_BACKEND_TBM_MSM_CFLAGS="$HAL_API_COMMON_CFLAGS $HAL_API_TBM_CFLAGS $LIBDRM_CFLAGS $GBM_CFLAGS $DLOG_CFLAGS $LIBUDEV_CFLAGS" +LIBHAL_BACKEND_TBM_MSM_LIBS="$HAL_API_COMMON_LIBS $HAL_API_TBM_LIBS $LIBDRM_LIBS $GBM_LIBS $DLOG_LIBS $LIBUDEV_LIBS" +AC_SUBST(LIBHAL_BACKEND_TBM_MSM_CFLAGS) +AC_SUBST(LIBHAL_BACKEND_TBM_MSM_LIBS) + AC_OUTPUT([ Makefile + src/libtbm-msm/Makefile + src/libhal-backend-tbm-msm/Makefile src/Makefile]) echo "" @@ -61,5 +79,7 @@ echo "LDFLAGS : $LDFLAGS" echo "LIBTBM_MSM_CFLAGS : $LIBTBM_MSM_CFLAGS" echo "LIBTBM_MSM_LIBS : $LIBTBM_MSM_LIBS" echo "bufmgr_dir : $bufmgr_dir" +echo "LIBHAL_BACKEND_TBM_MSM_CFLAGS : $LIBHAL_BACKEND_TBM_MSM_CFLAGS" +echo "LIBHAL_BACKEND_TBM_MSM_LIBS : $LIBHAL_BACKEND_TBM_MSM_LIBS" +echo "hal-libdir : $HAL_LIBDIR" echo "" - diff --git a/packaging/libhal-backend-tbm-msm.manifest b/packaging/libhal-backend-tbm-msm.manifest new file mode 100644 index 0000000..75b0fa5 --- /dev/null +++ b/packaging/libhal-backend-tbm-msm.manifest @@ -0,0 +1,5 @@ +<manifest> + <request> + <domain name="_"/> + </request> +</manifest> diff --git a/packaging/libtbm-msm.spec b/packaging/libtbm-msm.spec index 8949abe..930e33d 100644 --- a/packaging/libtbm-msm.spec +++ b/packaging/libtbm-msm.spec @@ -5,51 +5,73 @@ License: MIT Summary: Tizen Buffer Manager - drm msm backend Group: System/Libraries Source0: %{name}-%{version}.tar.gz -Source1001: %name.manifest +Source1001: %{name}.manifest +Source1002: libhal-backend-tbm-msm.manifest BuildRequires: pkgconfig(libdrm) BuildRequires: pkgconfig(libtbm) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(libudev) BuildRequires: pkgconfig(gbm) +BuildRequires: pkgconfig(hal-api-common) +BuildRequires: pkgconfig(hal-api-tbm) ExclusiveArch: %{arm} aarch64 %description descriptionion: Tizen Buffer manager backend module uses drm msm -%global TZ_SYS_RO_SHARE %{?TZ_SYS_RO_SHARE:%TZ_SYS_RO_SHARE}%{!?TZ_SYS_RO_SHARE:/usr/share} +%package -n hal-backend-tbm-msm +Summary: hal-backend-tbm module for msm +Group: System/Libraries +Requires: hal-api-tbm +Requires: hal-api-common + +%description -n hal-backend-tbm-msm +descriptionion: hal tbm backend module for msm %prep %setup -q cp %{SOURCE1001} . +cp %{SOURCE1002} . %build %reconfigure --prefix=%{_prefix} --libdir=%{_libdir}/bufmgr \ - CFLAGS="${CFLAGS} -Wall -Werror" LDFLAGS="${LDFLAGS} -Wl,--hash-style=both -Wl,--as-needed" + --with-hal-libdir=%{_hal_libdir} \ + --disable-align-eight \ + --disable-cachectrl \ + CFLAGS="${CFLAGS} -Wall -Werror" LDFLAGS="${LDFLAGS} -Wl,--hash-style=both -Wl,--as-needed" make %{?_smp_mflags} %install rm -rf %{buildroot} + %make_install %post -rpm -e --nodeps hal-backend-tbm-vc4 -rm /hal/lib64/libhal-backend-tbm* - if [ -f %{_libdir}/bufmgr/libtbm_default.so ]; then rm -rf %{_libdir}/bufmgr/libtbm_default.so fi -ln -s libtbm_msm.so %{_libdir}/bufmgr/libtbm_default.so +ln -s libtbm-msm.so %{_libdir}/bufmgr/libtbm_default.so %postun -p /sbin/ldconfig +%post -n hal-backend-tbm-msm +if [ -f %{_hal_libdir}/libhal-backend-tbm.so ]; then + rm -rf %{_hal_libdir}/libhal-backend-tbm.so +fi +ln -s libhal-backend-tbm-msm.so %{_hal_libdir}/libhal-backend-tbm.so + +%postun -n hal-backend-tbm-msm -p /sbin/ldconfig + %files %manifest %{name}.manifest -%defattr(-,root,root,-) %license COPYING -%{_libdir}/bufmgr/libtbm_*.so* - +%{_libdir}/bufmgr/libtbm-*.so* +%files -n hal-backend-tbm-msm +%manifest libhal-backend-tbm-msm.manifest +%license COPYING +%{_hal_libdir}/libhal-backend-*.so* diff --git a/src/Makefile.am b/src/Makefile.am index 8c91b09..d9eba17 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,12 +1 @@ -AM_CFLAGS = \ - @LIBTBM_MSM_CFLAGS@ \ - -I$(top_srcdir) \ - -I$(top_srcdir)/src \ - -I/usr/include/drm - -libtbm_msm_la_LTLIBRARIES = libtbm_msm.la -libtbm_msm_ladir = /${bufmgr_dir} -libtbm_msm_la_LIBADD = @LIBTBM_MSM_LIBS@ - -libtbm_msm_la_SOURCES = \ - tbm_bufmgr_msm.c +SUBDIRS = libtbm-msm libhal-backend-tbm-msm diff --git a/src/libhal-backend-tbm-msm/Makefile.am b/src/libhal-backend-tbm-msm/Makefile.am new file mode 100644 index 0000000..f79415e --- /dev/null +++ b/src/libhal-backend-tbm-msm/Makefile.am @@ -0,0 +1,12 @@ +AM_CFLAGS = \ + @LIBHAL_BACKEND_TBM_MSM_CFLAGS@ \ + -I$(top_srcdir) \ + -I$(top_srcdir)/src/libhal-backend-tbm-msm + +libhal_backend_tbm_msm_la_LTLIBRARIES = libhal-backend-tbm-msm.la +libhal_backend_tbm_msm_ladir = @HAL_LIBDIR@ +libhal_backend_tbm_msm_la_LIBADD = @LIBHAL_BACKEND_TBM_MSM_LIBS@ + +libhal_backend_tbm_msm_la_SOURCES = \ + tbm_backend_log.c \ + tbm_backend_msm.c diff --git a/src/libhal-backend-tbm-msm/tbm_backend_log.c b/src/libhal-backend-tbm-msm/tbm_backend_log.c new file mode 100644 index 0000000..58f9a17 --- /dev/null +++ b/src/libhal-backend-tbm-msm/tbm_backend_log.c @@ -0,0 +1,74 @@ +/************************************************************************** + +libtbm_vc4 + +Copyright 2021 Samsung Electronics co., Ltd. All Rights Reserved. + +Contact: SooChan Lim <sc1.lim@samsung.com> + +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" + +int tbm_log_debug_level = TBM_BACKEND_LOG_LEVEL_INFO; + +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-msm/tbm_backend_log.h b/src/libhal-backend-tbm-msm/tbm_backend_log.h new file mode 100644 index 0000000..ff60777 --- /dev/null +++ b/src/libhal-backend-tbm-msm/tbm_backend_log.h @@ -0,0 +1,105 @@ +/************************************************************************** + +libtbm_vc4 + +Copyright 2021 Samsung Electronics co., Ltd. All Rights Reserved. + +Contact: SooChan Lim <sc1.lim@samsung.com> + +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 <sys/syscall.h> +#include <time.h> +#include <dlog.h> + +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__ */
\ No newline at end of file diff --git a/src/libhal-backend-tbm-msm/tbm_backend_msm.c b/src/libhal-backend-tbm-msm/tbm_backend_msm.c new file mode 100644 index 0000000..90f69c0 --- /dev/null +++ b/src/libhal-backend-tbm-msm/tbm_backend_msm.c @@ -0,0 +1,1463 @@ +/************************************************************************** + +libtbm_msm + +Copyright 2021 Samsung Electronics co., Ltd. All Rights Reserved. + +Contact: SooChan Lim <sc1.lim@samsung.com>, + Junkyeong Kim <jk0430.kim@samsung.com>, + Changyeon Lee <cyeon.lee@samsung.com> + +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 <libudev.h> + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <xf86drm.h> +#include <gbm.h> +#include <msm_drm.h> +#include <pthread.h> +#include <hal-common.h> +#include <hal-tbm-types.h> +#include <hal-tbm-interface.h> +#include "tbm_backend_log.h" + +#define MSM_DRM_NAME "msm_drm" + +#define TBM_COLOR_FORMAT_COUNT 4 +#define STRERR_BUFSIZE 128 +#define SIZE_ALIGN(value, base) (((value) + ((base) - 1)) & ~((base) - 1)) +#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define TBM_SURFACE_ALIGNMENT_PLANE (64) +#define TBM_SURFACE_ALIGNMENT_PITCH_RGB (128) +#define TBM_SURFACE_ALIGNMENT_PITCH_YUV (16) +#define TBM_SURFACE_ALIGNMENT_PITCH_NV (512) + +typedef struct _tbm_msm_bufmgr tbm_msm_bufmgr; +typedef struct _tbm_msm_bo tbm_msm_bo; + +/* tbm buffor object for msm */ +struct _tbm_msm_bo { + + int device_fd; + + struct gbm_bo *gbo; + + unsigned int handle; /* GEM Handle */ + unsigned int name; /* FLINK ID */ + int buf_fd; /* fd for dmabuf */ + void *pBase; /* virtual address */ + + unsigned int size; + + unsigned int flags_msm; + unsigned int flags_tbm; + + pthread_mutex_t mutex; + int device; + int opt; + + tbm_msm_bufmgr *bufmgr_data; +}; + +/* tbm bufmgr private for msm */ +struct _tbm_msm_bufmgr { + int fd; + struct gbm_device *gdevice; + + void *hash_bos; + + char *device_name; +}; + +static char *STR_DEVICE[] = { + "DEF", + "CPU", + "2D", + "3D", + "MM" +}; + +static char *STR_OPT[] = { + "NONE", + "RD", + "WR", + "RDWR" +}; + + +static uint32_t tbm_msm_color_format_list[TBM_COLOR_FORMAT_COUNT] = { + HAL_TBM_FORMAT_ARGB8888, + HAL_TBM_FORMAT_XRGB8888, + HAL_TBM_FORMAT_NV12, + HAL_TBM_FORMAT_YUV420 + }; + +static int +_tbm_msm_open_drm() +{ + int fd = -1; + + fd = drmOpen(MSM_DRM_NAME, NULL); + if (fd < 0) { + TBM_BACKEND_ERR("fail to open drm.(%s)\n", MSM_DRM_NAME); + } + + if (fd < 0) { + struct udev *udev = NULL; + struct udev_enumerate *e = NULL; + struct udev_list_entry *entry = NULL; + struct udev_device *device = NULL, *drm_device = NULL, *device_parent = NULL; + const char *filepath; + struct stat s; + int ret; + + TBM_BACKEND_DBG("search drm-device by udev\n"); + + udev = udev_new(); + if (!udev) { + TBM_BACKEND_ERR("udev_new() failed.\n"); + return -1; + } + + e = udev_enumerate_new(udev); + udev_enumerate_add_match_subsystem(e, "drm"); + udev_enumerate_add_match_sysname(e, "card[0-9]*"); + udev_enumerate_scan_devices(e); + + udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { + device = udev_device_new_from_syspath(udev_enumerate_get_udev(e), + udev_list_entry_get_name(entry)); + device_parent = udev_device_get_parent(device); + /* Not need unref device_parent. device_parent and device have same refcnt */ + if (device_parent) { + if (strcmp(udev_device_get_sysname(device_parent), "msm-drm") == 0) { + drm_device = device; + TBM_BACKEND_DBG("Found render device: '%s' (%s)\n", + udev_device_get_syspath(drm_device), + udev_device_get_sysname(device_parent)); + break; + } + } + udev_device_unref(device); + } + + udev_enumerate_unref(e); + + /* Get device file path. */ + filepath = udev_device_get_devnode(drm_device); + if (!filepath) { + TBM_BACKEND_ERR("udev_device_get_devnode() failed.\n"); + udev_device_unref(drm_device); + udev_unref(udev); + return -1; + } + + /* Open DRM device file and check validity. */ + fd = open(filepath, O_RDWR | O_CLOEXEC); + if (fd < 0) { + TBM_BACKEND_ERR("open(%s, O_RDWR | O_CLOEXEC) failed.\n"); + udev_device_unref(drm_device); + udev_unref(udev); + return -1; + } + + ret = fstat(fd, &s); + if (ret) { + TBM_BACKEND_ERR("fstat() failed %s.\n"); + close(fd); + udev_device_unref(drm_device); + udev_unref(udev); + return -1; + } + + udev_device_unref(drm_device); + udev_unref(udev); + } + + return fd; +} + +static unsigned int +_get_msm_flag_from_tbm(unsigned int ftbm) +{ + unsigned int flags = 0; + + flags |= GBM_BO_USE_RENDERING; + + if (ftbm & HAL_TBM_BO_SCANOUT) + flags |= GBM_BO_USE_SCANOUT; + + return flags; +} + +static unsigned int +_get_name(int fd, unsigned int handle) +{ + struct drm_gem_flink arg = {0,}; + + arg.handle = handle; + if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &arg)) { + TBM_BACKEND_ERR("fail to DRM_IOCTL_GEM_FLINK handle:%d", handle); + return 0; + } + + return (unsigned int)arg.name; +} + +static hal_tbm_bo_handle +_msm_bo_handle(tbm_msm_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->handle; + break; + case HAL_TBM_DEVICE_CPU: + if (!bo_data->pBase) { + void *map = NULL; + + map = mmap(0, bo_data->size, PROT_READ | PROT_WRITE, MAP_SHARED, bo_data->buf_fd, 0); + if(map == MAP_FAILED) { + TBM_BACKEND_ERR("Cannot usrptr fd=%d(%m)", bo_data->buf_fd); + return (hal_tbm_bo_handle) NULL; + } + + bo_data->pBase = map; + } + bo_handle.ptr = (void *)bo_data->pBase; + break; + case HAL_TBM_DEVICE_3D: + bo_handle.u32 = (uint32_t)bo_data->buf_fd; + break; + case HAL_TBM_DEVICE_MM: + bo_handle.u32 = (uint32_t)bo_data->buf_fd; + break; + default: + TBM_BACKEND_ERR("Not supported device:%d", device); + bo_handle.ptr = (void *) NULL; + break; + } + + return bo_handle; +} + +static hal_tbm_bufmgr_capability +tbm_msm_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_KEY|HAL_TBM_BUFMGR_CAPABILITY_SHARE_FD; + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return capabilities; +} + +static hal_tbm_error +tbm_msm_bufmgr_get_supported_formats(hal_tbm_bufmgr *bufmgr, + uint32_t **formats, uint32_t *num) +{ + tbm_msm_bufmgr *bufmgr_data = (tbm_msm_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_msm_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_msm_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) +{ + tbm_msm_bufmgr *bufmgr_data = (tbm_msm_bufmgr *)bufmgr; + int bpp; + int _offset = 0; + int _pitch = 0; + int _size = 0; + int _bo_idx = 0; + int _align_height = 0; + + TBM_BACKEND_RETURN_VAL_IF_FAIL(bufmgr_data != NULL, HAL_TBM_ERROR_INVALID_PARAMETER); + + 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; + _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_NV); + _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_PITCH_NV); + _size = _pitch * _align_height; + _bo_idx = 0; + if (plane_idx == 0) + break; + } + /*else if (plane_idx == 1)*/ + { + _offset += _size; + _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_NV); + _align_height = SIZE_ALIGN(height / 2, TBM_SURFACE_ALIGNMENT_PITCH_NV / 2); + _size = _pitch * _align_height; + _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; + /*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 / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4); + _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 / 4, TBM_SURFACE_ALIGNMENT_PITCH_YUV / 4); + _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE); + _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 HAL_TBM_ERROR_NONE; +} + +static hal_tbm_bo * +tbm_msm_bufmgr_alloc_bo(hal_tbm_bufmgr *bufmgr, unsigned int size, + hal_tbm_bo_memory_type flags, hal_tbm_error *error) +{ + tbm_msm_bufmgr *bufmgr_data = (tbm_msm_bufmgr *)bufmgr; + tbm_msm_bo *bo_data; + struct gbm_bo *gbo; + unsigned int msm_flags; + uint32_t width = 2048, height; + + if (bufmgr_data == NULL) { + TBM_BACKEND_ERR("bufmgr_data is null\n"); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return NULL; + } + + bo_data = calloc(1, sizeof(struct _tbm_msm_bo)); + if (!bo_data) { + TBM_BACKEND_ERR("fail to allocate the bo_data private\n"); + if (error) + *error = HAL_TBM_ERROR_OUT_OF_MEMORY; + return NULL; + } + + bo_data->bufmgr_data = bufmgr_data; + + msm_flags = _get_msm_flag_from_tbm(flags); + + height = ((uint32_t) size + (width * 4) - 1) / (width * 4); + + gbo = gbm_bo_create(bufmgr_data->gdevice, width, height, GBM_FORMAT_ARGB8888, msm_flags); + if (!gbo) { + TBM_BACKEND_ERR("Cannot create gbm bo(size:%d flags:%d) (%m)", size, flags); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + bo_data->gbo = gbo; + bo_data->device_fd = bufmgr_data->fd; + bo_data->flags_tbm = flags; + bo_data->flags_msm = msm_flags; + + bo_data->handle = gbm_bo_get_handle(gbo).u32; + if (!bo_data->handle) { + TBM_BACKEND_ERR("Cannot get handle:(%m)"); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_data->name = _get_name(bo_data->device_fd, bo_data->handle); + if (!bo_data->name) { + TBM_BACKEND_ERR("Cannot get name from handle:%d(%m)", bo_data->handle); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_data->buf_fd = gbm_bo_get_fd(gbo); + if (bo_data->buf_fd < 0) { + TBM_BACKEND_ERR("Cannot get fd bo_data:%p", bo_data); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + bo_data->size = lseek(bo_data->buf_fd, 0, SEEK_END); + if (!bo_data->size) { + TBM_BACKEND_ERR("Cannot get size bo_data:%p (%m)", bo_data); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + if (drmHashInsert(bufmgr_data->hash_bos, bo_data->name, (void *)bo_data) < 0) { + TBM_BACKEND_ERR("Cannot insert bo_data to Hash(%d)", bo_data->name); + if (error) + *error = HAL_TBM_ERROR_OUT_OF_MEMORY; + goto fail; + } + + pthread_mutex_init(&bo_data->mutex, NULL); + + TBM_BACKEND_DBG(" bo_data:%p, handle:%d, flags:%d(%d), size:%d", + bo_data, + bo_data->handle, + bo_data->flags_tbm, + bo_data->size); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return (hal_tbm_bo *)bo_data; + +fail: + if (gbo) gbm_bo_destroy(gbo); + if (bo_data) free(bo_data); + + return NULL; +} + +static hal_tbm_bo * +tbm_msm_bufmgr_alloc_bo_with_format(hal_tbm_bufmgr *bufmgr, int format, int bo_idx, + int width, int height, int cpp, hal_tbm_bo_memory_type flags, hal_tbm_error *error) +{ + tbm_msm_bufmgr *bufmgr_data = (tbm_msm_bufmgr *)bufmgr; + tbm_msm_bo *bo_data; + struct gbm_bo *gbo; + unsigned int msm_flags; + + if (bufmgr_data == NULL) { + TBM_BACKEND_ERR("bufmgr_data is null\n"); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return NULL; + } + + bo_data = calloc(1, sizeof(struct _tbm_msm_bo)); + if (!bo_data) { + TBM_BACKEND_ERR("fail to allocate the bo_data private\n"); + if (error) + *error = HAL_TBM_ERROR_OUT_OF_MEMORY; + return NULL; + } + + bo_data->bufmgr_data = bufmgr_data; + + msm_flags = _get_msm_flag_from_tbm(flags); + + gbo = gbm_bo_create(bufmgr_data->gdevice, width, height, format, msm_flags); + if (!gbo) { + TBM_BACKEND_ERR("Cannot create gbm bo(format:%d bo_idx:%d width:%d height:%d flags:%d) (%m)", + format, bo_idx, width, height, flags); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + bo_data->gbo = gbo; + bo_data->device_fd = bufmgr_data->fd; + bo_data->flags_tbm = flags; + bo_data->flags_msm = msm_flags; + + bo_data->handle = gbm_bo_get_handle(gbo).u32; + if (!bo_data->handle) { + TBM_BACKEND_ERR("Cannot get handle:(%m)"); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_data->name = _get_name(bo_data->device_fd, bo_data->handle); + if (!bo_data->name) { + TBM_BACKEND_ERR("Cannot get name from handle:%d(%m)", bo_data->handle); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_data->buf_fd = gbm_bo_get_fd(gbo); + if (bo_data->buf_fd < 0) { + TBM_BACKEND_ERR("Cannot get fd bo_data:%p", bo_data); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + bo_data->size = lseek(bo_data->buf_fd, 0, SEEK_END); + if (!bo_data->size) { + TBM_BACKEND_ERR("Cannot get size bo_data:%p (%m)", bo_data); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + if (drmHashInsert(bufmgr_data->hash_bos, bo_data->name, (void *)bo_data) < 0) { + TBM_BACKEND_ERR("Cannot insert bo_data to Hash(%d)", bo_data->name); + if (error) + *error = HAL_TBM_ERROR_OUT_OF_MEMORY; + goto fail; + } + + pthread_mutex_init(&bo_data->mutex, NULL); + + TBM_BACKEND_DBG(" bo_data:%p, handle:%d, flags:%d(%d), size:%d", + bo_data, + bo_data->handle, + bo_data->flags_tbm, + bo_data->size); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return (hal_tbm_bo *)bo_data; + +fail: + if (gbo) gbm_bo_destroy(gbo); + if (bo_data) free(bo_data); + + return NULL; +} + +static hal_tbm_bo * +tbm_msm_bufmgr_import_fd(hal_tbm_bufmgr *bufmgr, hal_tbm_fd key, hal_tbm_error *error) +{ + tbm_msm_bufmgr *bufmgr_data = (tbm_msm_bufmgr *)bufmgr; + tbm_msm_bo *bo_data = NULL; + struct gbm_bo *gbo = NULL; + struct gbm_import_fd_data fd_data; + union gbm_bo_handle handle; + unsigned int name; + struct drm_prime_handle prime_arg = {0, }; + int ret; + + if (bufmgr_data == NULL) { + TBM_BACKEND_ERR("bufmgr_data is null\n"); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return NULL; + } + + prime_arg.fd = key; + prime_arg.flags = 0; + if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &prime_arg)) { + TBM_BACKEND_ERR("Cannot get gem handle from fd:%d (%m)", prime_arg.fd); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + name = _get_name(bufmgr_data->fd, prime_arg.handle); + if (!name) { + TBM_BACKEND_ERR("Cannot get name from handle:%d, fd:%d (%m)", prime_arg.handle, key); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + ret = drmHashLookup(bufmgr_data->hash_bos, name, (void **)&bo_data); + if (ret == 0) { + if (prime_arg.handle == bo_data->handle) { + if (error) + *error = HAL_TBM_ERROR_NONE; + return bo_data; + } + } + + /* we don't know format in backend */ + fd_data.format = GBM_FORMAT_ABGR8888; + fd_data.fd = dup(key); + + gbo = gbm_bo_import(bufmgr_data->gdevice, GBM_BO_IMPORT_FD, &fd_data, GBM_BO_USE_RENDERING); + if (!gbo) { + TBM_BACKEND_ERR("Cannot import gbm bo(fd:%d) (%m)", key); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + handle = gbm_bo_get_handle(gbo); + if (!handle.ptr) { + TBM_BACKEND_ERR("Cannot get handle gbm bo(fd:%d) (%m)", key); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + bo_data = calloc(1, sizeof(struct _tbm_msm_bo)); + if (!bo_data) { + TBM_BACKEND_ERR("bo_data:%p fail to allocate the bo_data", bo_data); + if (error) + *error = HAL_TBM_ERROR_OUT_OF_MEMORY; + return NULL; + } + + bo_data->bufmgr_data = bufmgr_data; + bo_data->gbo = gbo; + bo_data->device_fd = bufmgr_data->fd; + + bo_data->handle = gbm_bo_get_handle(gbo).u32; + if (!bo_data->handle) { + TBM_BACKEND_ERR("Cannot get handle (%m)"); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_data->name = _get_name(bo_data->device_fd, bo_data->handle); + if (!bo_data->name) { + TBM_BACKEND_ERR("Cannot get name from handle:%d (%m)", bo_data->handle); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + bo_data->buf_fd = gbm_bo_get_fd(gbo); + if (bo_data->buf_fd < 0) { + TBM_BACKEND_ERR("Cannot get fd bo_data:%p (%m)", bo_data); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + bo_data->size = lseek(bo_data->buf_fd, 0, SEEK_END); + if (!bo_data->size) { + TBM_BACKEND_ERR("Cannot get size bo_data:%p (%m)", bo_data); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + goto fail; + } + + if (drmHashInsert(bufmgr_data->hash_bos, bo_data->name, (void *)bo_data) < 0) { + TBM_BACKEND_ERR("Cannot insert bo_data to Hash(%d)", bo_data->name); + if (error) + *error = HAL_TBM_ERROR_OUT_OF_MEMORY; + goto fail; + } + + pthread_mutex_init(&bo_data->mutex, NULL); + + TBM_BACKEND_DBG(" bo_data:%p, handle:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d", + bo_data, + bo_data->handle, + bo_data->buf_fd, + key, + bo_data->flags_tbm, + bo_data->size); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return (hal_tbm_bo *)bo_data; + +fail: + if (fd_data.fd >= 0) + close(fd_data.fd); + if (gbo) gbm_bo_destroy(gbo); + if (bo_data) free(bo_data); + + return NULL; +} + +static hal_tbm_bo * +tbm_msm_bufmgr_import_key(hal_tbm_bufmgr *bufmgr, hal_tbm_key key, hal_tbm_error *error) +{ + tbm_msm_bufmgr *bufmgr_data = (tbm_msm_bufmgr *)bufmgr; + tbm_msm_bo *bo_data = NULL; + struct drm_gem_open open_arg = {0, }; + struct drm_gem_close close_arg = {0, }; + struct drm_prime_handle prime_arg = {0, }; + int ret; + + if (bufmgr_data == NULL) { + TBM_BACKEND_ERR("bufmgr_data is null\n"); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return NULL; + } + + ret = drmHashLookup(bufmgr_data->hash_bos, key, (void **)&bo_data); + if (ret == 0) { + if (error) + *error = HAL_TBM_ERROR_NONE; + return (hal_tbm_bo *)bo_data; + } + + open_arg.name = key; + if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_OPEN, &open_arg)) { + TBM_BACKEND_ERR("Cannot open gem name:%d (%m)", key); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return NULL; + } + + prime_arg.handle = open_arg.handle; + if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &prime_arg)) { + TBM_BACKEND_ERR("fail to DRM_IOCTL_PRIME_HANDLE_TO_FD handle:%d (%m)", prime_arg.handle); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + + goto fail; + } + + close_arg.handle = open_arg.handle; + if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_CLOSE, &close_arg)) + TBM_BACKEND_ERR("Cannot close gem_handle:%d (%m)", close_arg.handle); + + open_arg.handle = 0; + + bo_data = tbm_msm_bufmgr_import_fd(bufmgr_data, prime_arg.fd, error); + if (!bo_data) { + TBM_BACKEND_ERR("fail to import fd:%d", prime_arg.fd); + + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + + goto fail; + } + + close(prime_arg.fd); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + TBM_BACKEND_DBG(" bo_data:%p, handle:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d", + bo_data, + bo_data->handle, + bo_data->buf_fd, + key, + bo_data->flags_tbm, + bo_data->size); + + return (hal_tbm_bo *)bo_data; + +fail: + if (open_arg.handle) { + close_arg.handle = open_arg.handle; + drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_CLOSE, &close_arg); + } + if (prime_arg.fd >= 0) close(prime_arg.fd); + + return NULL; +} + +static void +tbm_msm_bo_free(hal_tbm_bo *bo) +{ + tbm_msm_bo *bo_data = (tbm_msm_bo *)bo; + tbm_msm_bufmgr *bufmgr_data; + tbm_msm_bo *temp; + int ret; + + if (!bo_data) + return; + + bufmgr_data = bo_data->bufmgr_data; + if (!bufmgr_data) + return; + + TBM_BACKEND_DBG(" bo_data:%p, handle:%d(%d), fd:%d, size:%d", + bo_data, + bo_data->handle, + bo_data->buf_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(%m)"); + } + + ret = drmHashLookup(bufmgr_data->hash_bos, bo_data->name, (void **)&temp); + if (ret == 0) + drmHashDelete(bufmgr_data->hash_bos, bo_data->name); + else + TBM_BACKEND_ERR("Cannot find bo_data to Hash(%d), ret:%d", bo_data->name, ret); + + if (temp != bo_data) + TBM_BACKEND_ERR("hash_bos probably has several BOs with same name!!!\n"); + + gbm_bo_destroy(bo_data->gbo); + + free(bo_data); +} + +static int +tbm_msm_bo_get_size(hal_tbm_bo *bo, hal_tbm_error *error) +{ + tbm_msm_bo *bo_data = (tbm_msm_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_msm_bo_get_memory_type(hal_tbm_bo *bo, hal_tbm_error *error) +{ + tbm_msm_bo *bo_data = (tbm_msm_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_msm_bo_get_handle(hal_tbm_bo *bo, hal_tbm_bo_device_type device, hal_tbm_error *error) +{ + tbm_msm_bo *bo_data = (tbm_msm_bo *)bo; + hal_tbm_bo_handle bo_handle; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return (hal_tbm_bo_handle) NULL; + } + + if (!bo_data->handle) { + TBM_BACKEND_ERR("Cannot map handle:%d", bo_data->handle); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return (hal_tbm_bo_handle) NULL; + } + + /*Get mapped bo_handle*/ + bo_handle = _msm_bo_handle(bo_data, device); + if (bo_handle.ptr == NULL) { + TBM_BACKEND_ERR("Cannot get handle: handle:%d, device:%d\n", + bo_data->handle, device); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return (hal_tbm_bo_handle) NULL; + } + + TBM_BACKEND_DBG("bo_data:%p, handle:%d(%d), fd:%d, flags:%d, size:%d, %s", + bo_data, + bo_data->handle, + bo_data->buf_fd, + bo_data->flags_tbm, + bo_data->size, + STR_DEVICE[device]); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return bo_handle; +} + +static hal_tbm_bo_handle +tbm_msm_bo_map(hal_tbm_bo *bo, hal_tbm_bo_device_type device, + hal_tbm_bo_access_option opt, hal_tbm_error *error) +{ + tbm_msm_bo *bo_data = (tbm_msm_bo *)bo; + hal_tbm_bo_handle bo_handle; + tbm_msm_bufmgr *bufmgr_data; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return (hal_tbm_bo_handle) NULL; + } + + bufmgr_data = bo_data->bufmgr_data; + if (!bufmgr_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return (hal_tbm_bo_handle) NULL; + } + + if (!bo_data->handle) { + TBM_BACKEND_ERR("Cannot map handle:%d", bo_data->handle); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return (hal_tbm_bo_handle) NULL; + } + + /*Get mapped bo_handle*/ + bo_handle = _msm_bo_handle(bo_data, device); + if (bo_handle.ptr == NULL) { + TBM_BACKEND_ERR("Cannot get handle: handle:%d, device:%d, opt:%d", + bo_data->handle, device, opt); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return (hal_tbm_bo_handle) NULL; + } + + TBM_BACKEND_DBG(" bo_data:%p, handle:%d(%d), fd:%d, %s, %s", + bo_data, + bo_data->handle, + bo_data->buf_fd, + STR_DEVICE[device], + STR_OPT[opt]); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return bo_handle; +} + +static hal_tbm_error +tbm_msm_bo_unmap(hal_tbm_bo *bo) +{ + tbm_msm_bo *bo_data = (tbm_msm_bo *)bo; + tbm_msm_bufmgr *bufmgr_data; + + if (!bo_data) + return HAL_TBM_ERROR_INVALID_PARAMETER; + + bufmgr_data = bo_data->bufmgr_data; + if (!bufmgr_data) + return HAL_TBM_ERROR_INVALID_PARAMETER; + + if (!bo_data->handle) + return HAL_TBM_ERROR_INVALID_PARAMETER; + + TBM_BACKEND_DBG(" bo_data:%p, handle:%d(%d), fd:%d", + bo_data, + bo_data->handle, + bo_data->buf_fd); + + return HAL_TBM_ERROR_NONE; +} + +static hal_tbm_fd +tbm_msm_bo_export_fd(hal_tbm_bo *bo, hal_tbm_error *error) +{ + tbm_msm_bo *bo_data = (tbm_msm_bo *)bo; + int fd = -1; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return -1; + } + + fd = gbm_bo_get_fd(bo_data->gbo); + if (fd < 0) { + TBM_BACKEND_ERR("bo_data:%p Cannot handle=%d", + bo_data, bo_data->handle); + if (error) + *error = HAL_TBM_ERROR_INVALID_OPERATION; + return (hal_tbm_fd)-1; + } + + TBM_BACKEND_DBG(" bo_data:%p, handle:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d", + bo_data, + bo_data->handle, + bo_data->buf_fd, + fd, + bo_data->flags_tbm, + bo_data->size); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return (hal_tbm_fd)dup(fd); +} + +static hal_tbm_key +tbm_msm_bo_export_key(hal_tbm_bo *bo, hal_tbm_error *error) +{ + tbm_msm_bo *bo_data = (tbm_msm_bo *)bo; + + if (!bo_data) { + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return 0; + } + + if (!bo_data->name) { + bo_data->name = _get_name(bo_data->device_fd, bo_data->handle); + if (!bo_data->name) { + TBM_BACKEND_ERR("error Cannot get name"); + if (error) + *error = HAL_TBM_ERROR_INVALID_PARAMETER; + return 0; + } + } + + TBM_BACKEND_DBG(" bo_data:%p, handle:%d(%d), fd:%d, flags:%d, size:%d\n", + bo_data, + bo_data->handle, bo_data->name, + bo_data->buf_fd, + bo_data->flags_tbm, + bo_data->size); + + if (error) + *error = HAL_TBM_ERROR_NONE; + + return (hal_tbm_key)bo_data->name; +} + +static hal_tbm_error +_tbm_msm_authenticated_drm_fd_handler(hal_tbm_fd auth_fd, void *user_data) +{ + tbm_msm_bufmgr *bufmgr_data = (tbm_msm_bufmgr *) user_data; + + TBM_BACKEND_RETURN_VAL_IF_FAIL(bufmgr_data != NULL, HAL_TBM_ERROR_INVALID_PARAMETER); + + bufmgr_data->fd = auth_fd; + TBM_BACKEND_INFO("Get the authenticated drm_fd(%d)!\n", bufmgr_data->fd); + + bufmgr_data->gdevice = gbm_create_device(bufmgr_data->fd); + if (!bufmgr_data->gdevice) { + TBM_BACKEND_ERR("fail to create gbm_device:%m"); + return HAL_TBM_ERROR_INVALID_OPERATION; + } + + return HAL_TBM_ERROR_NONE; +} + +static int +hal_backend_tbm_msm_exit(void *data) +{ + hal_tbm_backend_data *backend_data = (hal_tbm_backend_data *)data; + tbm_msm_bufmgr *bufmgr_data; + unsigned long key; + void *value; + + TBM_BACKEND_RETURN_VAL_IF_FAIL(backend_data != NULL, -1); + + bufmgr_data = (tbm_msm_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); + + if (bufmgr_data->hash_bos) { + while (drmHashFirst(bufmgr_data->hash_bos, &key, &value) > 0) { + free(value); + drmHashDelete(bufmgr_data->hash_bos, key); + } + + drmHashDestroy(bufmgr_data->hash_bos); + bufmgr_data->hash_bos = NULL; + } + + if (bufmgr_data->gdevice) + gbm_device_destroy(bufmgr_data->gdevice); + + close(bufmgr_data->fd); + + free(backend_data->bufmgr); + free(backend_data); + + return HAL_TBM_ERROR_NONE; +} + +static int +hal_backend_tbm_msm_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_msm_bufmgr *bufmgr_data = NULL; + int drm_fd = -1; + + /* 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_msm_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; + + // open drm_fd + drm_fd = _tbm_msm_open_drm(); + if (drm_fd < 0) { + TBM_BACKEND_ERR("fail to open drm!\n"); + goto fail_open_drm; + } + + // set true when backend has a drm_device. + backend_data->has_drm_device = 1; + + // check if drm_fd is master_drm_fd. + if (drmIsMaster(drm_fd)) { + // drm_fd is a master_drm_fd. + backend_data->drm_info.drm_fd = drm_fd; + backend_data->drm_info.is_master = 1; + + bufmgr_data->fd = drm_fd; + TBM_BACKEND_INFO("Get the master drm_fd(%d)!\n", bufmgr_data->fd); + + bufmgr_data->gdevice = gbm_create_device(bufmgr_data->fd); + if (!bufmgr_data->gdevice) { + TBM_BACKEND_ERR("fail to create gbm_device:%m"); + goto fail_create_gbm_device; + } + } else { + // drm_fd is not a master_drm_fd. + // request authenticated fd + close(drm_fd); + backend_data->drm_info.drm_fd = -1; + backend_data->drm_info.is_master = 0; + backend_data->drm_info.auth_drm_fd_func = _tbm_msm_authenticated_drm_fd_handler; + backend_data->drm_info.user_data = bufmgr_data; + + TBM_BACKEND_INFO("A backend requests an authenticated drm_fd.\n"); + } + + /*Create Hash Table*/ + bufmgr_data->hash_bos = drmHashCreate(); + + /* 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_msm_bufmgr_get_capabilities; + bufmgr_funcs->bufmgr_get_supported_formats = tbm_msm_bufmgr_get_supported_formats; + bufmgr_funcs->bufmgr_get_plane_data = tbm_msm_bufmgr_get_plane_data; + bufmgr_funcs->bufmgr_alloc_bo = tbm_msm_bufmgr_alloc_bo; + bufmgr_funcs->bufmgr_alloc_bo_with_format = tbm_msm_bufmgr_alloc_bo_with_format; + bufmgr_funcs->bufmgr_import_fd = tbm_msm_bufmgr_import_fd; + bufmgr_funcs->bufmgr_import_key = tbm_msm_bufmgr_import_key; + + /* 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_msm_bo_free; + bo_funcs->bo_get_size = tbm_msm_bo_get_size; + bo_funcs->bo_get_memory_types = tbm_msm_bo_get_memory_type; + bo_funcs->bo_get_handle = tbm_msm_bo_get_handle; + bo_funcs->bo_map = tbm_msm_bo_map; + bo_funcs->bo_unmap = tbm_msm_bo_unmap; + bo_funcs->bo_export_fd = tbm_msm_bo_export_fd; + bo_funcs->bo_export_key = tbm_msm_bo_export_key; + + TBM_BACKEND_DBG("drm_fd:%d\n", bufmgr_data->fd); + + return HAL_TBM_ERROR_NONE; + +fail_alloc_bo_funcs: + free(bufmgr_funcs); +fail_alloc_bufmgr_funcs: + if (bufmgr_data->hash_bos) + drmHashDestroy(bufmgr_data->hash_bos); +fail_create_gbm_device: + close(bufmgr_data->fd); +fail_open_drm: + free(bufmgr_data); +fail_alloc_bufmgr_data: + free(backend_data); + + *data = NULL; + + return -1; +} + +hal_backend hal_backend_tbm_data = { + "msm", + "Samsung", + HAL_ABI_VERSION_TIZEN_6_5, + hal_backend_tbm_msm_init, + hal_backend_tbm_msm_exit +}; diff --git a/src/libtbm-msm/Makefile.am b/src/libtbm-msm/Makefile.am new file mode 100644 index 0000000..57c16a8 --- /dev/null +++ b/src/libtbm-msm/Makefile.am @@ -0,0 +1,12 @@ +AM_CFLAGS = \ + @LIBTBM_MSM_CFLAGS@ \ + -I$(top_srcdir) \ + -I$(top_srcdir)/src \ + -I/usr/include/drm + +libtbm_msm_la_LTLIBRARIES = libtbm-msm.la +libtbm_msm_ladir = /${bufmgr_dir} +libtbm_msm_la_LIBADD = @LIBTBM_MSM_LIBS@ + +libtbm_msm_la_SOURCES = \ + tbm_bufmgr_msm.c diff --git a/src/tbm_bufmgr_msm.c b/src/libtbm-msm/tbm_bufmgr_msm.c index 15e3b5d..15e3b5d 100644 --- a/src/tbm_bufmgr_msm.c +++ b/src/libtbm-msm/tbm_bufmgr_msm.c |