--- /dev/null
+/**************************************************************************
+
+libtbm_meson
+
+Copyright 2017 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@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 <pthread.h>
+#include <hal-common.h>
+#include <hal-tbm-types.h>
+#include <hal-tbm-interface.h>
+#include <drm/meson_drm.h>
+#include "tbm_backend_log.h"
+
+#define MESON_DRM_NAME "meson"
+
+#define TBM_COLOR_FORMAT_COUNT 4
+#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 (16)
+#define TBM_SURFACE_ALIGNMENT_PITCH_RGB (16)
+#define TBM_SURFACE_ALIGNMENT_PITCH_YUV (16)
+#define TBM_SURFACE_ALIGNMENT_HEIGHT_YUV (16)
+
+typedef struct _tbm_meson_bufmgr tbm_meson_bufmgr;
+typedef struct _tbm_meson_bo tbm_meson_bo;
+
+/* tbm buffor object for meson */
+struct _tbm_meson_bo {
+ int bufmgr_fd;
+ unsigned int name;
+ unsigned int gem;
+ unsigned int dmabuf;
+ void *pBase;
+ unsigned int size;
+ unsigned int flags_tbm;
+ unsigned int flags_meson;
+ pthread_mutex_t mutex;
+ int device;
+ tbm_meson_bufmgr *bufmgr_data;
+};
+
+/* tbm bufmgr private for meson */
+struct _tbm_meson_bufmgr {
+ int fd;
+ void *hash_bos;
+};
+
+static char *STR_DEVICE[] = {
+ "DEF",
+ "CPU",
+ "2D",
+ "3D",
+ "MM"
+};
+
+static char *STR_OPT[] = {
+ "NONE",
+ "RD",
+ "WR",
+ "RDWR"
+};
+
+static uint32_t tbm_meson_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_meson_open_drm()
+{
+ int fd = -1;
+
+ fd = drmOpen(MESON_DRM_NAME, NULL);
+ if (fd < 0) {
+ TBM_BACKEND_ERR("fail to open drm.(%s)", MESON_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");
+
+ udev = udev_new();
+ if (!udev) {
+ TBM_BACKEND_ERR("udev_new() failed.");
+ 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), "meson-drm") == 0) {
+ drm_device = device;
+ TBM_BACKEND_DBG("Found render device: '%s' (%s)",
+ 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.");
+ 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.");
+ udev_device_unref(drm_device);
+ udev_unref(udev);
+ return -1;
+ }
+
+ ret = fstat(fd, &s);
+ if (ret) {
+ TBM_BACKEND_ERR("fstat() failed %s.");
+ 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_name(int fd, unsigned int gem)
+{
+ struct drm_gem_flink arg = {0,};
+
+ arg.handle = gem;
+ if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &arg)) {
+ TBM_BACKEND_ERR("fail to DRM_IOCTL_GEM_FLINK gem:%d", gem);
+ return 0;
+ }
+
+ return (unsigned int)arg.name;
+}
+
+static unsigned int
+_get_meson_flag_from_tbm(unsigned int ftbm)
+{
+ unsigned int flags = MESON_USE_NONE;
+
+ if (ftbm & HAL_TBM_BO_SCANOUT) {
+ flags |= MESON_USE_SCANOUT;
+ } else {
+ flags |= MESON_USE_LINEAR;
+ }
+
+ return flags;
+}
+
+static unsigned int
+_get_tbm_flag_from_meson(unsigned int fmeson)
+{
+ unsigned int flags = 0;
+
+ if (fmeson & MESON_USE_SCANOUT)
+ flags |= HAL_TBM_BO_SCANOUT;
+ else
+ flags |= HAL_TBM_BO_DEFAULT;
+
+ return flags;
+}
+
+static hal_tbm_bo_handle
+_meson_bo_handle(tbm_meson_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->gem;
+ break;
+ case HAL_TBM_DEVICE_CPU:
+ if (!bo_data->pBase) {
+ struct drm_mode_map_dumb arg = {0,};
+ void *map = NULL;
+
+ arg.handle = bo_data->gem;
+ if (drmIoctl(bo_data->bufmgr_fd, DRM_IOCTL_MODE_MAP_DUMB, &arg)) {
+ TBM_BACKEND_ERR("Cannot map_meson gem:%d", bo_data->gem);
+ return (hal_tbm_bo_handle) NULL;
+ }
+
+ map = mmap(NULL, bo_data->size, PROT_READ | PROT_WRITE, MAP_SHARED,
+ bo_data->bufmgr_fd, arg.offset);
+ if (map == MAP_FAILED) {
+ TBM_BACKEND_ERR("Cannot usrptr gem:%d", bo_data->gem);
+ return (hal_tbm_bo_handle) NULL;
+ }
+ bo_data->pBase = map;
+ }
+ bo_handle.ptr = (void *)bo_data->pBase;
+ break;
+ case HAL_TBM_DEVICE_3D:
+ case HAL_TBM_DEVICE_MM:
+ bo_handle.u32 = (uint32_t)bo_data->dmabuf;
+ break;
+ default:
+ TBM_BACKEND_ERR("Not supported device:%d", device);
+ bo_handle.ptr = (void *) NULL;
+ break;
+ }
+
+ return bo_handle;
+}
+
+static int
+_meson_cache_flush(tbm_meson_bo *bo_data)
+{
+ struct drm_meson_gem_sync cache_op = { 0 };
+ int ret;
+
+ cache_op.handle = bo_data->gem;
+ ret = drmCommandWriteRead(bo_data->bufmgr_fd, DRM_MESON_GEM_SYNC,
+ &cache_op, sizeof(cache_op));
+ if (ret) {
+ TBM_BACKEND_ERR("fail to flush the cache ret:%d", ret);
+ return 0;
+ }
+
+ return 1;
+}
+
+static hal_tbm_bufmgr_capability
+tbm_meson_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_meson_bufmgr_get_supported_formats(hal_tbm_bufmgr *bufmgr,
+ uint32_t **formats, uint32_t *num)
+{
+ tbm_meson_bufmgr *bufmgr_data = (tbm_meson_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_meson_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", *num);
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static hal_tbm_error
+tbm_meson_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_meson_bufmgr *bufmgr_data = (tbm_meson_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;
+ _offset = 0;
+ _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_RGB);
+ _size = SIZE_ALIGN(_pitch * height, TBM_SURFACE_ALIGNMENT_PLANE);
+ _bo_idx = 0;
+ break;
+
+ /* packed YCbCr */
+ case HAL_TBM_FORMAT_YUYV:
+ case HAL_TBM_FORMAT_YVYU:
+ case HAL_TBM_FORMAT_UYVY:
+ case HAL_TBM_FORMAT_VYUY:
+ case HAL_TBM_FORMAT_AYUV:
+ bpp = 32;
+ _offset = 0;
+ _pitch = SIZE_ALIGN((width * bpp) >> 3, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_height, TBM_SURFACE_ALIGNMENT_PLANE);
+ _bo_idx = 0;
+ break;
+
+ /*
+ * 2 plane YCbCr
+ * index 0 = Y plane, [7:0] Y
+ * index 1 = Cr:Cb plane, [15:0] Cr:Cb little endian
+ * or
+ * index 1 = Cb:Cr plane, [15:0] Cb:Cr little endian
+ */
+ case HAL_TBM_FORMAT_NV12:
+ case HAL_TBM_FORMAT_NV21:
+ bpp = 12;
+ /*if (plane_idx == 0)*/
+ {
+ _offset = 0;
+ _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height / 2, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_height, TBM_SURFACE_ALIGNMENT_PLANE);
+ _bo_idx = 0;
+ }
+ break;
+ case HAL_TBM_FORMAT_NV16:
+ case HAL_TBM_FORMAT_NV61:
+ bpp = 16;
+ /*if(plane_idx == 0)*/
+ {
+ _offset = 0;
+ _pitch = SIZE_ALIGN(width, TBM_SURFACE_ALIGNMENT_PITCH_YUV);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height / 4, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height / 4, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height / 2, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV / 2);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height / 2, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV / 2);
+ _size = SIZE_ALIGN(_pitch * _align_height, 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);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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);
+ _align_height = SIZE_ALIGN(height, TBM_SURFACE_ALIGNMENT_HEIGHT_YUV);
+ _size = SIZE_ALIGN(_pitch * _align_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_meson_bufmgr_alloc_bo(hal_tbm_bufmgr *bufmgr, unsigned int size,
+ hal_tbm_bo_memory_type flags, hal_tbm_error *error)
+{
+ tbm_meson_bufmgr *bufmgr_data = (tbm_meson_bufmgr *)bufmgr;
+ tbm_meson_bo *bo_data = NULL;
+ struct drm_mode_create_dumb create_arg = {0, };
+ struct drm_prime_handle prime_arg = {0, };
+ struct drm_gem_close close_arg = {0, };
+
+ if (bufmgr_data == NULL) {
+ TBM_BACKEND_ERR("bufmgr is null");
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return NULL;
+ }
+
+ bo_data = calloc(1, sizeof(struct _tbm_meson_bo));
+ if (!bo_data) {
+ TBM_BACKEND_ERR("fail to allocate the bo_data private");
+ if (error)
+ *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
+ return NULL;
+ }
+
+ //as we know only size for new bo set height=1 and bpp=8 and in this case
+ //width will by equal to size in bytes;
+ create_arg.height = 1;
+ create_arg.bpp = 8;
+ create_arg.width = size;
+ create_arg.flags = _get_meson_flag_from_tbm(flags);
+ if (drmIoctl(bufmgr_data->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg)) {
+ TBM_BACKEND_ERR("Cannot create bo_data(flag:%x, size:%d)",
+ create_arg.flags, (unsigned int)create_arg.size);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ goto fail;
+ }
+
+ bo_data->bufmgr_data = bufmgr_data;
+ bo_data->bufmgr_fd = bufmgr_data->fd;
+ bo_data->gem = (unsigned int)create_arg.handle;
+ bo_data->size = size;
+ bo_data->flags_tbm = flags;
+ bo_data->flags_meson = create_arg.flags;
+
+ bo_data->name = _get_name(bo_data->bufmgr_fd, bo_data->gem);
+ if (!bo_data->name) {
+ TBM_BACKEND_ERR("Cannot get name from handle:%d(%m)", bo_data->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ goto fail;
+ }
+
+ prime_arg.handle = bo_data->gem;
+ if (drmIoctl(bo_data->bufmgr_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &prime_arg)) {
+ TBM_BACKEND_ERR("Cannot dmabuf:%d", bo_data->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ goto fail;
+ }
+
+ bo_data->dmabuf = prime_arg.fd;
+
+ pthread_mutex_init(&bo_data->mutex, NULL);
+
+ /* add bo_data to hash */
+ 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);
+
+ TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d), flags:%d, size:%d",
+ bo_data,
+ bo_data->gem,
+ bo_data->name,
+ bo_data->flags_tbm,
+ bo_data->size);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_bo *)bo_data;
+
+fail:
+ if (bo_data) {
+ if (bo_data->gem) {
+ close_arg.handle = bo_data->gem;
+ drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
+ }
+ free(bo_data);
+ }
+
+ return NULL;
+}
+
+static hal_tbm_bo *
+tbm_meson_bufmgr_import_fd(hal_tbm_bufmgr *bufmgr, hal_tbm_fd key, hal_tbm_error *error)
+{
+ tbm_meson_bufmgr *bufmgr_data = (tbm_meson_bufmgr *)bufmgr;
+ tbm_meson_bo *bo_data = NULL;
+ unsigned int gem = 0;
+ unsigned int name;
+ struct drm_prime_handle prime_arg = {0, };
+ struct drm_meson_gem_info info_arg = {0, };
+ struct drm_gem_close close_arg = {0, };
+ int ret;
+
+ if (bufmgr_data == NULL) {
+ TBM_BACKEND_ERR("bufmgr is null");
+ 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->gem) {
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+ return bo_data;
+ }
+ }
+
+ bo_data = calloc(1, sizeof(struct _tbm_meson_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->bufmgr_fd = bufmgr_data->fd;
+ bo_data->gem = prime_arg.handle;
+
+ info_arg.handle = bo_data->gem;
+ if (drmCommandWriteRead(bufmgr_data->fd, DRM_MESON_GEM_GET, &info_arg, sizeof(info_arg))) {
+ TBM_BACKEND_ERR("Cannot get gem info from gem:%d, fd:%d (%m)", gem, key);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ goto fail;
+ }
+
+ bo_data->size = info_arg.size;
+ bo_data->flags_tbm = _get_tbm_flag_from_meson(info_arg.flags);
+ bo_data->flags_meson = info_arg.flags;
+
+ bo_data->name = _get_name(bo_data->bufmgr_fd, bo_data->gem);
+ if (!bo_data->name) {
+ TBM_BACKEND_ERR("Cannot get name from handle:%d (%m)", bo_data->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ goto fail;
+ }
+
+ prime_arg.handle = bo_data->gem;
+ 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;
+ }
+
+ bo_data->dmabuf = prime_arg.fd;
+
+ /* add bo_data to hash */
+ if (drmHashInsert(bufmgr_data->hash_bos, bo_data->name, (void *)bo_data) < 0)
+ TBM_BACKEND_ERR("bo_data:%p Cannot insert bo_data to Hash(%d) from gem:%d, fd:%d",
+ bo_data, bo_data->name, gem, key);
+
+ TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d",
+ bo_data,
+ bo_data->gem,
+ bo_data->name,
+ bo_data->dmabuf,
+ key,
+ bo_data->flags_tbm,
+ bo_data->size);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_bo *)bo_data;
+
+fail:
+ if (bo_data) {
+ if (bo_data->gem) {
+ close_arg.handle = bo_data->gem;
+ drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
+ }
+ free(bo_data);
+ }
+
+ return NULL;
+}
+
+static hal_tbm_bo *
+tbm_meson_bufmgr_import_key(hal_tbm_bufmgr *bufmgr, hal_tbm_key key, hal_tbm_error *error)
+{
+ tbm_meson_bufmgr *bufmgr_data = (tbm_meson_bufmgr *)bufmgr;
+ tbm_meson_bo *bo_data = NULL;
+ struct drm_gem_open open_arg = {0, };
+ struct drm_meson_gem_info info_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 is null");
+ 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", key);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ return NULL;
+ }
+
+ bo_data = calloc(1, sizeof(struct _tbm_meson_bo));
+ if (!bo_data) {
+ TBM_BACKEND_ERR("fail to allocate the bo_data private");
+ if (error)
+ *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
+ return NULL;
+ }
+
+ bo_data->bufmgr_data = bufmgr_data;
+ bo_data->bufmgr_fd = bufmgr_data->fd;
+ bo_data->gem = open_arg.handle;
+
+ info_arg.handle = bo_data->gem;
+ if (drmCommandWriteRead(bufmgr_data->fd, DRM_MESON_GEM_GET, &info_arg, sizeof(info_arg))) {
+ TBM_BACKEND_ERR("Cannot get gem info from gem:%d, key:%d (%m)", bo_data->gem, key);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ goto fail;
+ }
+
+ bo_data->size = info_arg.size;
+ bo_data->flags_meson = info_arg.flags;
+ bo_data->flags_tbm = _get_tbm_flag_from_meson(info_arg.flags);
+ bo_data->name = key;
+
+ prime_arg.handle = bo_data->gem;
+ 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;
+ }
+
+ bo_data->dmabuf = prime_arg.fd;
+
+ /* add bo_data to hash */
+ 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);
+
+ TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d), fd:%d, flags:%d, size:%d",
+ bo_data,
+ bo_data->gem,
+ bo_data->name,
+ bo_data->dmabuf,
+ bo_data->flags_tbm,
+ bo_data->size);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_bo *)bo_data;
+
+fail:
+ if (bo_data) {
+ if (bo_data->gem) {
+ close_arg.handle = bo_data->gem;
+ drmIoctl(bufmgr_data->fd, DRM_IOCTL_GEM_CLOSE, &close_arg);
+ }
+ free(bo_data);
+ }
+
+ return NULL;
+}
+
+static void
+tbm_meson_bo_free(hal_tbm_bo *bo)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_bo *)bo;
+ tbm_meson_bo *temp;
+ tbm_meson_bufmgr *bufmgr_data;
+ struct drm_gem_close arg = {0, };
+ int ret;
+
+ if (!bo_data)
+ return;
+
+ bufmgr_data = bo_data->bufmgr_data;
+ if (!bufmgr_data)
+ return;
+
+ TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d), fd:%d, size:%d",
+ bo_data,
+ bo_data->gem,
+ bo_data->name,
+ bo_data->dmabuf,
+ 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)", bo_data);
+ }
+ }
+
+ /* close dmabuf */
+ if (bo_data->dmabuf) {
+ close(bo_data->dmabuf);
+ bo_data->dmabuf = 0;
+ }
+
+ /* delete bo_data from hash */
+ 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!!!");
+
+ memset(&arg, 0, sizeof(arg));
+ arg.handle = bo_data->gem;
+ if (drmIoctl(bo_data->bufmgr_fd, DRM_IOCTL_GEM_CLOSE, &arg))
+ TBM_BACKEND_ERR("bo_data:%p fail to gem close.(%m)", bo_data);
+
+ free(bo_data);
+}
+
+static int
+tbm_meson_bo_get_size(hal_tbm_bo *bo, hal_tbm_error *error)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_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_meson_bo_get_memory_type(hal_tbm_bo *bo, hal_tbm_error *error)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_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_meson_bo_get_handle(hal_tbm_bo *bo, hal_tbm_bo_device_type device, hal_tbm_error *error)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_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->gem) {
+ TBM_BACKEND_ERR("Cannot map gem:%d", bo_data->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return (hal_tbm_bo_handle) NULL;
+ }
+
+ TBM_BACKEND_DBG("bo_data:%p, gem:%d(%d), fd:%d, flags:%d, size:%d, %s",
+ bo_data,
+ bo_data->gem,
+ bo_data->name,
+ bo_data->dmabuf,
+ bo_data->flags_tbm,
+ bo_data->size,
+ STR_DEVICE[device]);
+
+ /*Get mapped bo_handle*/
+ bo_handle = _meson_bo_handle(bo_data, device);
+ if (bo_handle.ptr == NULL) {
+ TBM_BACKEND_ERR("Cannot get handle: gem:%d, device:%d",
+ bo_data->gem, device);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ return (hal_tbm_bo_handle) NULL;
+ }
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return bo_handle;
+}
+
+static hal_tbm_bo_handle
+tbm_meson_bo_map(hal_tbm_bo *bo, hal_tbm_bo_device_type device,
+ hal_tbm_bo_access_option opt, hal_tbm_error *error)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_bo *)bo;
+ hal_tbm_bo_handle bo_handle;
+ tbm_meson_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->gem) {
+ TBM_BACKEND_ERR("Cannot map gem:%d", bo_data->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return (hal_tbm_bo_handle) NULL;
+ }
+
+ TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d), fd:%d, %s, %s",
+ bo_data,
+ bo_data->gem,
+ bo_data->name,
+ bo_data->dmabuf,
+ STR_DEVICE[device],
+ STR_OPT[opt]);
+
+ /*Get mapped bo_handle*/
+ bo_handle = _meson_bo_handle(bo_data, device);
+ if (bo_handle.ptr == NULL) {
+ TBM_BACKEND_ERR("Cannot get handle: gem:%d, device:%d, opt:%d",
+ bo_data->gem, device, opt);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ return (hal_tbm_bo_handle) NULL;
+ }
+
+ if ((device == HAL_TBM_DEVICE_CPU) && (opt & HAL_TBM_OPTION_WRITE)) {
+ _meson_cache_flush(bo_data);
+ TBM_BACKEND_DBG("cache flush done");
+ }
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return bo_handle;
+}
+
+static hal_tbm_error
+tbm_meson_bo_unmap(hal_tbm_bo *bo)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_bo *)bo;
+ tbm_meson_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->gem)
+ return HAL_TBM_ERROR_INVALID_PARAMETER;
+
+ TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d), fd:%d",
+ bo_data,
+ bo_data->gem,
+ bo_data->name,
+ bo_data->dmabuf);
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static hal_tbm_error
+tbm_meson_bo_lock(hal_tbm_bo *bo, hal_tbm_bo_device_type device,
+ hal_tbm_bo_access_option opt)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_bo *)bo;
+ struct drm_meson_gem_cpu_access arg = {0, };
+
+ if (device != HAL_TBM_DEVICE_CPU)
+ return HAL_TBM_ERROR_NONE;
+
+ pthread_mutex_lock(&bo_data->mutex);
+ bo_data->device = device;
+ pthread_mutex_unlock(&bo_data->mutex);
+
+ arg.handle = bo_data->gem;
+ if (drmCommandWriteRead(bo_data->bufmgr_fd, DRM_MESON_GEM_CPU_PREP, &arg, sizeof(arg))) {
+ TBM_BACKEND_ERR("DRM_MESON_GEM_CPU_FINI failed name:%d (%m)", bo_data->name);
+ return HAL_TBM_ERROR_INVALID_OPERATION;
+ }
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static hal_tbm_error
+tbm_meson_bo_unlock(hal_tbm_bo *bo)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_bo *)bo;
+ struct drm_meson_gem_cpu_access arg = {0, };
+
+ pthread_mutex_lock(&bo_data->mutex);
+ if (bo_data->device != HAL_TBM_DEVICE_CPU) {
+ pthread_mutex_unlock(&bo_data->mutex);
+ return HAL_TBM_ERROR_NONE;
+ }
+
+ bo_data->device = HAL_TBM_DEVICE_DEFAULT;
+ pthread_mutex_unlock(&bo_data->mutex);
+
+ arg.handle = bo_data->gem;
+ if (drmCommandWriteRead(bo_data->bufmgr_fd, DRM_MESON_GEM_CPU_FINI, &arg, sizeof(arg))) {
+ TBM_BACKEND_ERR("DRM_MESON_GEM_CPU_FINI failed name:%d (%m)", bo_data->name);
+ return HAL_TBM_ERROR_INVALID_OPERATION;
+ }
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static hal_tbm_fd
+tbm_meson_bo_export_fd(hal_tbm_bo *bo, hal_tbm_error *error)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_bo *)bo;
+ int ret;
+
+ if (!bo_data) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return -1;
+ }
+
+ struct drm_prime_handle arg = {0, };
+
+ arg.handle = bo_data->gem;
+ ret = drmIoctl(bo_data->bufmgr_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &arg);
+ if (ret) {
+ TBM_BACKEND_ERR("bo_data:%p Cannot dmabuf:%d (%s)", bo_data, bo_data->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ return (hal_tbm_fd) ret;
+ }
+
+ TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d), fd:%d, key_fd:%d, flags:%d, size:%d",
+ bo_data,
+ bo_data->gem,
+ bo_data->name,
+ bo_data->dmabuf,
+ arg.fd,
+ bo_data->flags_tbm,
+ bo_data->size);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_fd)arg.fd;
+}
+
+static hal_tbm_key
+tbm_meson_bo_export_key(hal_tbm_bo *bo, hal_tbm_error *error)
+{
+ tbm_meson_bo *bo_data = (tbm_meson_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->bufmgr_fd, bo_data->gem);
+ 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, gem:%d(%d), fd:%d, flags:%d, size:%d",
+ bo_data,
+ bo_data->gem,
+ bo_data->name,
+ bo_data->dmabuf,
+ 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_meson_authenticated_drm_fd_handler(hal_tbm_fd auth_fd, void *user_data)
+{
+ tbm_meson_bufmgr *bufmgr_data = (tbm_meson_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)!", bufmgr_data->fd);
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static int
+hal_backend_tbm_meson_exit(void *data)
+{
+ hal_tbm_backend_data *backend_data = (hal_tbm_backend_data *)data;
+ tbm_meson_bufmgr *bufmgr_data;
+ unsigned long key;
+ void *value;
+
+ TBM_BACKEND_RETURN_VAL_IF_FAIL(backend_data != NULL, -1);
+
+ bufmgr_data = (tbm_meson_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;
+ }
+
+ close(bufmgr_data->fd);
+
+ free(backend_data->bufmgr);
+ free(backend_data);
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static int
+hal_backend_tbm_meson_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_meson_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!");
+ *data = NULL;
+ return -1;
+ }
+ *data = backend_data;
+
+ /* allocate a hal_tbm_bufmgr */
+ bufmgr_data = calloc(1, sizeof(struct _tbm_meson_bufmgr));
+ if (!bufmgr_data) {
+ TBM_BACKEND_ERR("fail to alloc bufmgr_data!");
+ goto fail_alloc_bufmgr_data;
+ }
+ backend_data->bufmgr = (hal_tbm_bufmgr *)bufmgr_data;
+
+ // open drm_fd
+ drm_fd = _tbm_meson_open_drm();
+ if (drm_fd < 0) {
+ TBM_BACKEND_ERR("fail to open drm!");
+ 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)!", bufmgr_data->fd);
+ } 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_meson_authenticated_drm_fd_handler;
+ backend_data->drm_info.user_data = bufmgr_data;
+
+ TBM_BACKEND_INFO("A backend requests an authenticated drm_fd.");
+ }
+
+ /*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!");
+ goto fail_alloc_bufmgr_funcs;
+ }
+ backend_data->bufmgr_funcs = bufmgr_funcs;
+
+ bufmgr_funcs->bufmgr_get_capabilities = tbm_meson_bufmgr_get_capabilities;
+ bufmgr_funcs->bufmgr_get_supported_formats = tbm_meson_bufmgr_get_supported_formats;
+ bufmgr_funcs->bufmgr_get_plane_data = tbm_meson_bufmgr_get_plane_data;
+ bufmgr_funcs->bufmgr_alloc_bo = tbm_meson_bufmgr_alloc_bo;
+ bufmgr_funcs->bufmgr_alloc_bo_with_format = NULL;
+ bufmgr_funcs->bufmgr_import_fd = tbm_meson_bufmgr_import_fd;
+ bufmgr_funcs->bufmgr_import_key = tbm_meson_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!");
+ goto fail_alloc_bo_funcs;
+ }
+ backend_data->bo_funcs = bo_funcs;
+
+ bo_funcs->bo_free = tbm_meson_bo_free;
+ bo_funcs->bo_get_size = tbm_meson_bo_get_size;
+ bo_funcs->bo_get_memory_types = tbm_meson_bo_get_memory_type;
+ bo_funcs->bo_get_handle = tbm_meson_bo_get_handle;
+ bo_funcs->bo_map = tbm_meson_bo_map;
+ bo_funcs->bo_unmap = tbm_meson_bo_unmap;
+ bo_funcs->bo_lock = tbm_meson_bo_lock;
+ bo_funcs->bo_unlock = tbm_meson_bo_unlock;
+ bo_funcs->bo_export_fd = tbm_meson_bo_export_fd;
+ bo_funcs->bo_export_key = tbm_meson_bo_export_key;
+
+ TBM_BACKEND_DBG("drm_fd:%d", 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);
+ 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 = {
+ "meson",
+ "Samsung",
+ HAL_ABI_VERSION_TIZEN_6_5,
+ hal_backend_tbm_meson_init,
+ hal_backend_tbm_meson_exit
+};