From 338f791f7d2ade0306a7b2d86e9f0baef241e7a4 Mon Sep 17 00:00:00 2001 From: SooChan Lim Date: Wed, 3 Feb 2021 09:21:34 +0900 Subject: move source files to src/libtbm-shm/ Change-Id: Ia374f1bc93ab21307d6d25e532694c58d1e186b3 --- configure.ac | 1 + src/Makefile.am | 12 +- src/libtbm-shm/Makefile.am | 11 + src/libtbm-shm/tbm_bufmgr_shm.c | 970 ++++++++++++++++++++++++++++++++++++++++ src/tbm_bufmgr_shm.c | 970 ---------------------------------------- 5 files changed, 983 insertions(+), 981 deletions(-) create mode 100644 src/libtbm-shm/Makefile.am create mode 100644 src/libtbm-shm/tbm_bufmgr_shm.c delete mode 100644 src/tbm_bufmgr_shm.c diff --git a/configure.ac b/configure.ac index 89eb0d3..4667577 100644 --- a/configure.ac +++ b/configure.ac @@ -58,6 +58,7 @@ AC_SUBST(bufmgr_dir) AC_OUTPUT([ Makefile + src/libtbm-shm/Makefile src/Makefile]) echo "" diff --git a/src/Makefile.am b/src/Makefile.am index 05a380b..b89be83 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,11 +1 @@ -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 +SUBDIRS = libtbm-shm diff --git a/src/libtbm-shm/Makefile.am b/src/libtbm-shm/Makefile.am new file mode 100644 index 0000000..05a380b --- /dev/null +++ b/src/libtbm-shm/Makefile.am @@ -0,0 +1,11 @@ +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 new file mode 100644 index 0000000..88c6522 --- /dev/null +++ b/src/libtbm-shm/tbm_bufmgr_shm.c @@ -0,0 +1,970 @@ +/************************************************************************** + + 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_bufmgr_shm.c b/src/tbm_bufmgr_shm.c deleted file mode 100644 index 88c6522..0000000 --- a/src/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 -}; - -- cgit v1.2.3