--- /dev/null
+/*
+ * buffer manager for libtbm-vigs
+ *
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact :
+ * Stanislav Vorobiov <s.vorobiov@samsung.com>
+ * Jinhyung Jo <jinhyung.jo@samsung.com>
+ * Sangho Park <sangho1206.park@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.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, sublicense, 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 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 NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS 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.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "vigs.h"
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <xf86drm.h>
+#include <hal-common.h>
+#include <hal-tbm-types.h>
+#include <hal-tbm-interface.h>
+#include "tbm_backend_log.h"
+
+#define STRERR_BUFSIZE 128
+
+#define VIGS_DRM_NAME "vigs"
+
+/* global singleton drm_dev. this is shared with yagl. */
+struct vigs_drm_device *g_drm_dev = NULL;
+
+static uint32_t tbm_bufmgr_vigs_color_format_list[] = {
+ HAL_TBM_FORMAT_RGB888,
+ HAL_TBM_FORMAT_ARGB8888,
+ HAL_TBM_FORMAT_RGBA8888,
+ HAL_TBM_FORMAT_XRGB8888,
+ HAL_TBM_FORMAT_XBGR8888,
+ HAL_TBM_FORMAT_NV21,
+ HAL_TBM_FORMAT_NV61,
+ HAL_TBM_FORMAT_YUV420,
+};
+
+typedef struct _tbm_vigs_bufmgr tbm_vigs_bufmgr;
+typedef struct _tbm_vigs_bo tbm_vigs_bo;
+
+/* tbm buffor object for vigs */
+struct _tbm_vigs_bo {
+ struct vigs_drm_surface *sfc;
+
+ tbm_vigs_bufmgr *bufmgr_data;
+};
+
+/* tbm bufmgr private for vigs */
+struct _tbm_vigs_bufmgr {
+ int fd;
+ struct vigs_drm_device *drm_dev;
+};
+
+static int
+_tbm_vigs_open_drm(void)
+{
+ int fd = -1;
+
+ fd = drmOpen(VIGS_DRM_NAME, NULL);
+ if (fd < 0) {
+ TBM_BACKEND_ERR("open vigs drm device failed");
+ return -1;
+ }
+
+ return fd;
+}
+
+static hal_tbm_bo_handle
+get_tbm_bo_handle(struct vigs_drm_surface *sfc, int device)
+{
+ hal_tbm_bo_handle bo_handle;
+ int ret;
+ struct vigs_drm_gem *gem = &sfc->gem;
+ char buf[STRERR_BUFSIZE];
+
+ memset(&bo_handle, 0, sizeof(bo_handle));
+
+ switch (device) {
+ case HAL_TBM_DEVICE_DEFAULT:
+ case HAL_TBM_DEVICE_2D:
+ bo_handle.u32 = gem->handle;
+ break;
+ case HAL_TBM_DEVICE_CPU:
+ ret = vigs_drm_gem_map(gem, 1);
+
+ if (ret == 0)
+ bo_handle.ptr = gem->vaddr;
+ else
+ TBM_BACKEND_ERR("vigs_drm_gem_map failed: %s", strerror_r(errno, buf, STRERR_BUFSIZE));
+
+ break;
+ case HAL_TBM_DEVICE_3D:
+ bo_handle.ptr = (void *)sfc;
+ break;
+ case HAL_TBM_DEVICE_MM:
+ TBM_BACKEND_ERR("HAL_TBM_DEVICE_MM not supported");
+ break;
+ default:
+ TBM_BACKEND_ERR("%d not supported", device);
+ break;
+ }
+
+ return bo_handle;
+}
+
+static hal_tbm_bufmgr_capability
+tbm_vigs_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_vigs_bufmgr_get_supported_formats(hal_tbm_bufmgr *bufmgr,
+ uint32_t **formats, uint32_t *num)
+{
+ tbm_vigs_bufmgr *bufmgr_data = (tbm_vigs_bufmgr *)bufmgr;
+ uint32_t *color_formats;
+
+ if (!bufmgr_data)
+ return HAL_TBM_ERROR_INVALID_PARAMETER;
+
+ color_formats = (uint32_t *) calloc(1, sizeof(tbm_bufmgr_vigs_color_format_list));
+ if (!color_formats)
+ return HAL_TBM_ERROR_OUT_OF_MEMORY;
+
+ memcpy(color_formats, tbm_bufmgr_vigs_color_format_list, sizeof(tbm_bufmgr_vigs_color_format_list));
+
+ *formats = color_formats;
+ *num = sizeof(tbm_bufmgr_vigs_color_format_list) / sizeof(tbm_bufmgr_vigs_color_format_list[0]);
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static hal_tbm_error
+tbm_vigs_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_vigs_bufmgr *bufmgr_data = (tbm_vigs_bufmgr *)bufmgr;
+
+ *size = 0;
+ *offset = 0;
+ *pitch = 0;
+ *bo_idx = 0;
+
+ TBM_BACKEND_RETURN_VAL_IF_FAIL(bufmgr_data != NULL, HAL_TBM_ERROR_INVALID_PARAMETER);
+
+ switch (format) {
+ case HAL_TBM_FORMAT_RGB888:
+ *size = width * height * 3;
+ *offset = 0;
+ *pitch = width * 3;
+ *bo_idx = 0;
+ return HAL_TBM_ERROR_NONE;
+ case HAL_TBM_FORMAT_XRGB8888:
+ case HAL_TBM_FORMAT_XBGR8888:
+ case HAL_TBM_FORMAT_ARGB8888:
+ case HAL_TBM_FORMAT_RGBA8888:
+ *size = width * height * 4;
+ *offset = 0;
+ *pitch = width * 4;
+ *bo_idx = 0;
+ return HAL_TBM_ERROR_NONE;
+ case HAL_TBM_FORMAT_NV21:
+ if (plane_idx == 0) {
+ *size = width * height;
+ *offset = 0;
+ *pitch = width;
+ *bo_idx = 0;
+ } else if (plane_idx == 1) {
+ *size = width * (height >> 1);
+ *offset = width * height;
+ *pitch = width;
+ *bo_idx = 0;
+ } else {
+ return HAL_TBM_ERROR_INVALID_PARAMETER;
+ }
+ return HAL_TBM_ERROR_NONE;
+ case HAL_TBM_FORMAT_NV61:
+ if (plane_idx == 0) {
+ *size = width * height;
+ *offset = 0;
+ *pitch = width;
+ *bo_idx = 0;
+ } else if (plane_idx == 1) {
+ *size = width * height;
+ *offset = width * height;
+ *pitch = width;
+ *bo_idx = 0;
+ } else {
+ return HAL_TBM_ERROR_INVALID_PARAMETER;
+ }
+ return HAL_TBM_ERROR_NONE;
+ case HAL_TBM_FORMAT_YUV420:
+ if (plane_idx == 0) {
+ *size = width * height;
+ *offset = 0;
+ *pitch = width;
+ *bo_idx = 0;
+ } else if (plane_idx == 1) {
+ *size = (width * height) >> 2;
+ *offset = width * height;
+ *pitch = width >> 1;
+ *bo_idx = 0;
+ } else if (plane_idx == 2) {
+ *size = (width * height) >> 2;
+ *offset = (width * height) + (width * height >> 2);
+ *pitch = width >> 1;
+ *bo_idx = 0;
+ } else {
+ return HAL_TBM_ERROR_INVALID_PARAMETER;
+ }
+ return HAL_TBM_ERROR_NONE;
+ default:
+ return HAL_TBM_ERROR_INVALID_PARAMETER;
+ }
+}
+
+static hal_tbm_bo *
+tbm_vigs_bufmgr_alloc_bo(hal_tbm_bufmgr *bufmgr, unsigned int size,
+ hal_tbm_bo_memory_type flags, hal_tbm_error *error)
+{
+ tbm_vigs_bufmgr *bufmgr_data = (tbm_vigs_bufmgr *)bufmgr;
+ tbm_vigs_bo *bo_data;
+ struct vigs_drm_device *drm_dev;
+ struct vigs_drm_surface *sfc;
+ uint32_t width = 2048, height;
+ int ret;
+ char buf[STRERR_BUFSIZE];
+
+ if (bufmgr_data == NULL || bufmgr_data->drm_dev == NULL) {
+ TBM_BACKEND_ERR("bufmgr_data is null\n");
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return NULL;
+ }
+
+ drm_dev = bufmgr_data->drm_dev;
+
+ height = ((uint32_t) size + (width * 4) - 1) / (width * 4);
+
+ ret = vigs_drm_surface_create(drm_dev, width, height, width * 4, vigs_drm_surface_bgra8888, 0, &sfc);
+ if (ret != 0) {
+ TBM_BACKEND_ERR("vigs_drm_suface_create failed: %s", strerror_r(errno, buf, STRERR_BUFSIZE));
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return NULL;
+ }
+
+ bo_data = calloc(1, sizeof(struct _tbm_vigs_bo));
+ if (!bo_data) {
+ TBM_BACKEND_ERR("fail to allocate the bo_data private\n");
+ vigs_drm_gem_unref(&sfc->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
+ return NULL;
+ }
+ bo_data->bufmgr_data = bufmgr_data;
+ bo_data->sfc = sfc;
+
+ TBM_BACKEND_DBG("size = %d, flags = 0x%X", size, flags);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_bo *)bo_data;
+}
+
+static hal_tbm_bo *
+tbm_vigs_bufmgr_alloc_bo_with_format(hal_tbm_bufmgr *bufmgr, int format, int bo_idx,
+ int width, int height, hal_tbm_bo_memory_type flags, hal_tbm_error *error)
+{
+ tbm_vigs_bufmgr *bufmgr_data = (tbm_vigs_bufmgr *)bufmgr;
+ tbm_vigs_bo *bo_data;
+ struct vigs_drm_device *drm_dev;
+ struct vigs_drm_surface *sfc;
+ int ret;
+ char buf[STRERR_BUFSIZE];
+
+ if (bufmgr_data == NULL || bufmgr_data->drm_dev == NULL) {
+ TBM_BACKEND_ERR("bufmgr_data is null\n");
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return NULL;
+ }
+
+ if (bo_idx != 0) {
+ TBM_BACKEND_ERR("Not supported bo idx");
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return NULL;
+ }
+
+ drm_dev = bufmgr_data->drm_dev;
+
+ switch (format) {
+ case HAL_TBM_FORMAT_RGB888:
+ ret = vigs_drm_surface_create(drm_dev, width, height, width * 3, vigs_drm_surface_bgra8888, 0, &sfc);
+ break;
+ case HAL_TBM_FORMAT_XRGB8888:
+ case HAL_TBM_FORMAT_XBGR8888:
+ ret = vigs_drm_surface_create(drm_dev, width, height, width * 4, vigs_drm_surface_bgra8888, 0, &sfc);
+ break;
+ case HAL_TBM_FORMAT_ARGB8888:
+ case HAL_TBM_FORMAT_RGBA8888:
+ ret = vigs_drm_surface_create(drm_dev, width, height, width * 4, vigs_drm_surface_bgra8888, 0, &sfc);
+ break;
+ case HAL_TBM_FORMAT_NV21:
+ ret = vigs_drm_surface_create(drm_dev, width, height * 3 >> 1, width, vigs_drm_surface_bgra8888, 0, &sfc);
+ break;
+ case HAL_TBM_FORMAT_NV61:
+ ret = vigs_drm_surface_create(drm_dev, width, height * 2, width, vigs_drm_surface_bgra8888, 0, &sfc);
+ break;
+ case HAL_TBM_FORMAT_YUV420:
+ ret = vigs_drm_surface_create(drm_dev, width >> 2, height * 3 >> 1, width, vigs_drm_surface_bgra8888, 0, &sfc);
+ break;
+ default:
+ TBM_BACKEND_ERR("Not supported format");
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return NULL;
+ }
+
+ if (ret != 0) {
+ TBM_BACKEND_ERR("vigs_drm_suface_create failed: %s", strerror_r(errno, buf, STRERR_BUFSIZE));
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return NULL;
+ }
+
+ bo_data = calloc(1, sizeof(struct _tbm_vigs_bo));
+ if (!bo_data) {
+ TBM_BACKEND_ERR("fail to allocate the bo_data private\n");
+ vigs_drm_gem_unref(&sfc->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
+ return NULL;
+ }
+ bo_data->bufmgr_data = bufmgr_data;
+ bo_data->sfc = sfc;
+
+ TBM_BACKEND_DBG("width = %d, height = %d, format = %x, flags = 0x%X bo_idx = %d",
+ width, height, format, flags, bo_idx);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_bo *)bo_data;
+}
+
+static hal_tbm_bo *
+tbm_vigs_bufmgr_import_fd(hal_tbm_bufmgr *bufmgr, hal_tbm_fd key, hal_tbm_error *error)
+{
+ tbm_vigs_bufmgr *bufmgr_data = (tbm_vigs_bufmgr *)bufmgr;
+ tbm_vigs_bo *bo_data;
+ struct vigs_drm_device *drm_dev;
+ struct vigs_drm_surface *sfc;
+ int ret;
+ char buf[STRERR_BUFSIZE];
+
+ if (bufmgr_data == NULL || bufmgr_data->drm_dev == NULL) {
+ TBM_BACKEND_ERR("bufmgr_data is null\n");
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return NULL;
+ }
+
+ drm_dev = bufmgr_data->drm_dev;
+
+ ret = vigs_drm_prime_import_fd(drm_dev, key, &sfc);
+ if (ret != 0) {
+ TBM_BACKEND_ERR("vigs_drm_prime_import_fd failed for key %d: %s",
+ key, strerror_r(errno, buf, STRERR_BUFSIZE));
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ return NULL;
+ }
+
+ bo_data = calloc(1, sizeof(struct _tbm_vigs_bo));
+ if (!bo_data) {
+ TBM_BACKEND_ERR("fail to allocate the bo_data private\n");
+ vigs_drm_gem_unref(&sfc->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
+ return NULL;
+ }
+ bo_data->bufmgr_data = bufmgr_data;
+ bo_data->sfc = sfc;
+
+ TBM_BACKEND_DBG("bo_data = %p, key = %u handle = %u", bo_data, key, sfc->gem.handle);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_bo *)bo_data;
+}
+
+static hal_tbm_bo *
+tbm_vigs_bufmgr_import_key(hal_tbm_bufmgr *bufmgr, hal_tbm_key key, hal_tbm_error *error)
+{
+ tbm_vigs_bufmgr *bufmgr_data = (tbm_vigs_bufmgr *)bufmgr;
+ tbm_vigs_bo *bo_data;
+ struct vigs_drm_device *drm_dev;
+ int ret;
+ struct vigs_drm_surface *sfc;
+ char buf[STRERR_BUFSIZE];
+
+ if (bufmgr_data == NULL || bufmgr_data->drm_dev == NULL) {
+ TBM_BACKEND_ERR("bufmgr_data is null\n");
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return NULL;
+ }
+
+ drm_dev = bufmgr_data->drm_dev;
+
+ ret = vigs_drm_surface_open(drm_dev, key, &sfc);
+ if (ret != 0) {
+ TBM_BACKEND_ERR("vigs_drm_surface_open failed for key %u: %s",
+ key, strerror_r(errno, buf, STRERR_BUFSIZE));
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ return NULL;
+ }
+
+ bo_data = calloc(1, sizeof(struct _tbm_vigs_bo));
+ if (!bo_data) {
+ TBM_BACKEND_ERR("fail to allocate the bo_data private\n");
+ vigs_drm_gem_unref(&sfc->gem);
+ if (error)
+ *error = HAL_TBM_ERROR_OUT_OF_MEMORY;
+ return NULL;
+ }
+ bo_data->bufmgr_data = bufmgr_data;
+ bo_data->sfc = sfc;
+
+ TBM_BACKEND_DBG("bo_data = %p, key = %u handle = %u", bo_data, key, sfc->gem.handle);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_bo *)bo_data;
+}
+
+static void
+tbm_vigs_bo_free(hal_tbm_bo *bo)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+ struct vigs_drm_surface *sfc;
+
+ if (!bo_data)
+ return;
+
+ TBM_BACKEND_DBG("bo_data = %p", bo_data);
+
+ sfc = bo_data->sfc;
+
+ vigs_drm_gem_unref(&sfc->gem);
+}
+
+static int
+tbm_vigs_bo_get_size(hal_tbm_bo *bo, hal_tbm_error *error)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+ struct vigs_drm_surface *sfc;
+
+ if (!bo_data || !bo_data->sfc) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return 0;
+ }
+
+ TBM_BACKEND_DBG("bo_data = %p", bo_data);
+
+ sfc = bo_data->sfc;
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return sfc->gem.size;
+}
+
+static hal_tbm_bo_memory_type
+tbm_vigs_bo_get_memory_type(hal_tbm_bo *bo, hal_tbm_error *error)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+
+ if (!bo_data || !bo_data->sfc) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return HAL_TBM_BO_DEFAULT;
+ }
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return HAL_TBM_BO_DEFAULT;
+}
+
+static hal_tbm_bo_handle
+tbm_vigs_bo_get_handle(hal_tbm_bo *bo, hal_tbm_bo_device_type device, hal_tbm_error *error)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+ struct vigs_drm_surface *sfc;
+ hal_tbm_bo_handle handle;
+
+ if (!bo_data || !bo_data->sfc) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return (hal_tbm_bo_handle) NULL;
+ }
+
+ sfc = bo_data->sfc;
+
+ TBM_BACKEND_DBG("bo_data = %p, device = %d", bo_data, device);
+
+ handle = get_tbm_bo_handle(sfc, device);
+ if (!handle.ptr) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return handle;
+ }
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return handle;
+}
+
+static hal_tbm_bo_handle
+tbm_vigs_bo_map(hal_tbm_bo *bo, hal_tbm_bo_device_type device,
+ hal_tbm_bo_access_option opt, hal_tbm_error *error)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+ struct vigs_drm_surface *sfc;
+ hal_tbm_bo_handle handle;
+ uint32_t saf = 0;
+
+ if (!bo_data || !bo_data->sfc) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return (hal_tbm_bo_handle) NULL;
+ }
+
+ sfc = bo_data->sfc;
+
+ TBM_BACKEND_DBG("bo_data = %p, device = %d, opt = %d", bo_data, device, opt);
+
+ handle = get_tbm_bo_handle(sfc, device);
+ if (!handle.ptr) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return handle;
+ }
+
+ if ((opt & HAL_TBM_OPTION_READ) != 0)
+ saf |= VIGS_DRM_SAF_READ;
+
+ if ((opt & HAL_TBM_OPTION_WRITE) != 0)
+ saf |= VIGS_DRM_SAF_WRITE;
+
+ vigs_drm_surface_start_access(sfc, saf);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return handle;
+}
+
+static hal_tbm_error
+tbm_vigs_bo_unmap(hal_tbm_bo *bo)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+ struct vigs_drm_surface *sfc;
+
+ if (!bo_data || !bo_data->sfc)
+ return HAL_TBM_ERROR_INVALID_PARAMETER;
+
+ sfc = bo_data->sfc;
+
+ TBM_BACKEND_DBG("bo_data = %p", bo_data);
+
+ vigs_drm_surface_end_access(sfc, 1);
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static hal_tbm_error
+tbm_vigs_bo_lock(hal_tbm_bo *bo, hal_tbm_bo_device_type device,
+ hal_tbm_bo_access_option opt)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+
+ if (!bo_data || !bo_data->sfc)
+ return HAL_TBM_ERROR_INVALID_PARAMETER;
+
+ TBM_BACKEND_DBG("bo_data = %p", bo_data);
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static hal_tbm_error
+tbm_vigs_bo_unlock(hal_tbm_bo *bo)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+
+ if (!bo_data || !bo_data->sfc)
+ return HAL_TBM_ERROR_INVALID_PARAMETER;
+
+ TBM_BACKEND_DBG("bo_data = %p", bo_data);
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+hal_tbm_fd
+tbm_vigs_bo_export_fd(hal_tbm_bo *bo, hal_tbm_error *error)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+ tbm_vigs_bufmgr *bufmgr_data;
+ struct vigs_drm_surface *sfc;
+ struct vigs_drm_device *drm_dev;
+ int ret, fd = 0;
+ char buf[STRERR_BUFSIZE];
+
+ if (!bo_data || !bo_data->sfc) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return -1;
+ }
+
+ bufmgr_data = bo_data->bufmgr_data;
+ if (!bufmgr_data || !bufmgr_data->drm_dev) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return -1;
+ }
+
+ drm_dev = bufmgr_data->drm_dev;
+ sfc = bo_data->sfc;
+
+ ret = vigs_drm_prime_export_fd(drm_dev, sfc, &fd);
+ if (ret != 0) {
+ TBM_BACKEND_ERR("vigs_drm_prime_export_fd failed: %s", strerror_r(errno, buf, STRERR_BUFSIZE));
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ return -1;
+ }
+
+ TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d)\n", bo_data, sfc->gem);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_fd)fd;
+}
+
+static hal_tbm_key
+tbm_vigs_bo_export_key(hal_tbm_bo *bo, hal_tbm_error *error)
+{
+ tbm_vigs_bo *bo_data = (tbm_vigs_bo *)bo;
+ struct vigs_drm_surface *sfc;
+ int ret;
+ char buf[STRERR_BUFSIZE];
+
+ if (!bo_data || !bo_data->sfc) {
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_PARAMETER;
+ return 0;
+ }
+
+ sfc = bo_data->sfc;
+
+ ret = vigs_drm_gem_get_name(&sfc->gem);
+ if (ret != 0) {
+ TBM_BACKEND_ERR("vigs_drm_gem_get_name failed: %s", strerror_r(errno, buf, STRERR_BUFSIZE));
+ if (error)
+ *error = HAL_TBM_ERROR_INVALID_OPERATION;
+ return 0;
+ }
+
+ TBM_BACKEND_DBG(" bo_data:%p, gem:%d(%d)\n", bo_data, sfc->gem);
+
+ if (error)
+ *error = HAL_TBM_ERROR_NONE;
+
+ return (hal_tbm_key)sfc->gem.name;
+}
+static void
+_tbm_vigs_bufmgr_deinitialize(tbm_vigs_bufmgr *bufmgr_data)
+{
+ if (bufmgr_data->drm_dev)
+ vigs_drm_device_destroy(bufmgr_data->drm_dev);
+
+ g_drm_dev = NULL;
+}
+
+static hal_tbm_error
+_tbm_vigs_bufmgr_initailize(tbm_vigs_bufmgr *bufmgr_data)
+{
+ struct vigs_drm_device *drm_dev = NULL;
+ char buf[STRERR_BUFSIZE];
+ int ret = 0;
+
+ ret = vigs_drm_device_create(bufmgr_data->fd, &drm_dev);
+ if (ret != 0) {
+ TBM_BACKEND_ERR("vigs_drm_device_create failed: %s", strerror_r(errno, buf, STRERR_BUFSIZE));
+ vigs_drm_device_destroy(drm_dev);
+ g_drm_dev = NULL;
+ return HAL_TBM_ERROR_INVALID_OPERATION;
+ }
+
+ bufmgr_data->drm_dev = drm_dev;
+ g_drm_dev = drm_dev;
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static hal_tbm_error
+_tbm_vigs_authenticated_drm_fd_handler(hal_tbm_fd auth_fd, void *user_data)
+{
+ tbm_vigs_bufmgr *bufmgr_data = (tbm_vigs_bufmgr *) user_data;
+ int ret = 0;
+
+ 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);
+
+ ret = _tbm_vigs_bufmgr_initailize(bufmgr_data);
+ if (ret != HAL_TBM_ERROR_NONE) {
+ TBM_BACKEND_ERR("_tbm_vigs_bufmgr_initailize failed.");
+ return ret;
+ }
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static int
+hal_backend_tbm_vigs_exit(void *data)
+{
+ hal_tbm_backend_data *backend_data = (hal_tbm_backend_data *)data;
+ tbm_vigs_bufmgr *bufmgr_data;
+
+ TBM_BACKEND_DBG("enter");
+
+ TBM_BACKEND_RETURN_VAL_IF_FAIL(backend_data != NULL, -1);
+
+ bufmgr_data = (tbm_vigs_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);
+
+ _tbm_vigs_bufmgr_deinitialize(bufmgr_data);
+
+ if (bufmgr_data->fd >= 0)
+ close(bufmgr_data->fd);
+
+ free(backend_data->bufmgr);
+ free(backend_data);
+
+ return HAL_TBM_ERROR_NONE;
+}
+
+static int
+hal_backend_tbm_vigs_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_vigs_bufmgr *bufmgr_data = NULL;
+ int drm_fd = -1;
+ int ret = 0;
+
+ TBM_BACKEND_DBG("enter");
+#if 0
+ if (!*data) {
+ TBM_BACKEND_ERR("*data is null.\n");
+ return -1;
+ }
+#endif
+ /* 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_vigs_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;
+
+ bufmgr_data->fd = -1;
+
+ // open drm_fd
+ drm_fd = _tbm_vigs_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);
+
+ ret = _tbm_vigs_bufmgr_initailize(bufmgr_data);
+ if (ret != HAL_TBM_ERROR_NONE) {
+ TBM_BACKEND_ERR("_tbm_vigs_bufmgr_initailize failed.");
+ goto fail_create_vigs_drm_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_vigs_authenticated_drm_fd_handler;
+ backend_data->drm_info.user_data = bufmgr_data;
+
+ TBM_BACKEND_INFO("A backend requests an authenticated drm_fd.\n");
+ }
+
+ /* 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_vigs_bufmgr_get_capabilities;
+ bufmgr_funcs->bufmgr_get_supported_formats = tbm_vigs_bufmgr_get_supported_formats;
+ bufmgr_funcs->bufmgr_get_plane_data = tbm_vigs_bufmgr_get_plane_data;
+ bufmgr_funcs->bufmgr_alloc_bo = tbm_vigs_bufmgr_alloc_bo;
+ bufmgr_funcs->bufmgr_alloc_bo_with_format = tbm_vigs_bufmgr_alloc_bo_with_format;
+ bufmgr_funcs->bufmgr_import_fd = tbm_vigs_bufmgr_import_fd;
+ bufmgr_funcs->bufmgr_import_key = tbm_vigs_bufmgr_import_key;
+ bufmgr_funcs->bufmgr_alloc_bo_with_tiled_format = NULL;
+
+ /* alloc and register bo_funcs */
+ bo_funcs = calloc(1, sizeof(struct _hal_tbm_bo_funcs));
+ if (!bo_funcs) {
+ TBM_BACKEND_ERR("fail to alloc bo_funcs!\n");
+ goto fail_alloc_bo_funcs;
+ }
+ backend_data->bo_funcs = bo_funcs;
+
+ bo_funcs->bo_free = tbm_vigs_bo_free;
+ bo_funcs->bo_get_size = tbm_vigs_bo_get_size;
+ bo_funcs->bo_get_memory_types = tbm_vigs_bo_get_memory_type;
+ bo_funcs->bo_get_handle = tbm_vigs_bo_get_handle;
+ bo_funcs->bo_map = tbm_vigs_bo_map;
+ bo_funcs->bo_unmap = tbm_vigs_bo_unmap;
+ bo_funcs->bo_lock = tbm_vigs_bo_lock;
+ bo_funcs->bo_unlock = tbm_vigs_bo_unlock;
+ bo_funcs->bo_export_fd = tbm_vigs_bo_export_fd;
+ bo_funcs->bo_export_key = tbm_vigs_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:
+ _tbm_vigs_bufmgr_deinitialize(bufmgr_data);
+fail_create_vigs_drm_device:
+ if (bufmgr_data->fd >= 0)
+ close(bufmgr_data->fd);
+fail_open_drm:
+ free(bufmgr_data);
+fail_alloc_bufmgr_data:
+ free(backend_data);
+
+ g_drm_dev = NULL;
+ *data = NULL;
+
+ return -1;
+}
+
+hal_backend hal_backend_tbm_data = {
+ "vigs",
+ "Samsung",
+ HAL_ABI_VERSION_TIZEN_6_5,
+ hal_backend_tbm_vigs_init,
+ hal_backend_tbm_vigs_exit
+};
+
+struct vigs_drm_device *
+libhal_backend_tbm_vigs_get_drm_dev(void)
+{
+ return g_drm_dev;
+}