--- /dev/null
+SooChan Lim <sc1.lim@samsung.com>
+SangJin Lee <lsj119@samsung.com>
+Boram Park <boram1288.park@samsung.com>
--- /dev/null
+Copyright (C) 2000 - 2012 Samsung Electronics co., Ltd. All Rights Reserved.
+
+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 fur-
+nished 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, FIT-
+NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
+NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--- /dev/null
+SUBDIRS = src drm_slp
+
+pkgconfigdir = $(libdir)/pkgconfig
+pkgconfig_DATA = libtbm.pc
+
--- /dev/null
+#! /bin/sh
+
+srcdir=`dirname $0`
+test -z "$srcdir" && srcdir=.
+
+ORIGDIR=`pwd`
+cd $srcdir
+
+autoreconf -v --install || exit 1
+cd $ORIGDIR || exit $?
+
+$srcdir/configure --enable-maintainer-mode "$@"
--- /dev/null
+#
+# 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
+# on 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
+# ADAM JACKSON 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.
+
+AC_PREREQ(2.60)
+AC_INIT(libtbm, 1.0.0)
+AC_USE_SYSTEM_EXTENSIONS
+AC_CONFIG_SRCDIR([Makefile.am])
+AM_INIT_AUTOMAKE([dist-bzip2])
+
+AM_CONFIG_HEADER([config.h])
+
+AC_DISABLE_STATIC
+AC_PROG_LIBTOOL
+AC_PROG_CC
+
+AC_HEADER_STDC
+AC_SYS_LARGEFILE
+AC_FUNC_ALLOCA
+
+# Enable quiet compiles on automake 1.11.
+m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
+
+# set the dir for the tbm module
+DEFAULT_BUFMGR_MODULE_PATH="/usr/lib/bufmgr"
+AC_ARG_WITH(bufmgr-module-path, AS_HELP_STRING([--with-bufmgr-module-path=PATH], [tbm bufmgr module dir]),
+ [ BUFMGR_MODULE_PATH="$withval" ],
+ [ BUFMGR_MODULE_PATH="${DEFAULT_BUFMGR_MODULE_PATH}" ])
+#AC_DEFINE(BUFMGR_MODULE_DIR, "${BUFMGR_MODULE_PATH}", [Directory for the modules of tbm_bufmgr])
+AC_DEFINE_UNQUOTED(BUFMGR_MODULE_DIR, "${BUFMGR_MODULE_PATH}", [Directory for the modules of tbm_bufmgr])
+
+AC_CHECK_FUNCS([clock_gettime], [CLOCK_LIB=],
+ [AC_CHECK_LIB([rt], [clock_gettime], [CLOCK_LIB=-lrt],
+ [AC_MSG_ERROR([Couldn't find clock_gettime])])])
+AC_SUBST([CLOCK_LIB])
+
+PKG_CHECK_MODULES(PTHREADSTUBS, pthread-stubs)
+PKG_CHECK_MODULES(LIBDRM, libdrm)
+PKG_CHECK_MODULES(LIBDRI2, libdri2)
+PKG_CHECK_MODULES(DLOG, dlog)
+PKG_CHECK_MODULES(X11, x11)
+PKG_CHECK_MODULES(CAPI, [capi-base-common >= 0.1.1], [capi_0_1_1=yes], [capi_0_1_1=no])
+if test x"$capi_0_1_1" = xyes; then
+ AC_DEFINE(HAVE_CAPI_0_1_1,1,[The version of capi-base-common is over 0.1.1])
+fi
+
+LIBTBM_CFLAGS="$PTHREADSTUBS_CFLAGS $LIBDRM_CFLAGS $LIBDRI2_CFLAGS $X11_CFLAGS $CAPI_CFLAGS"
+LIBTBM_LIBS="$PTHREADSTUBS_LIBS $LIBDRM_LIBS $LIBDRI2_LIBS $DLOG_LIBS $X11_LIBS $CAPI_LIBS"
+AC_SUBST(LIBTBM_CFLAGS)
+AC_SUBST(LIBTBM_LIBS)
+
+AC_OUTPUT([
+ src/Makefile
+ drm_slp/Makefile
+ Makefile
+ libtbm.pc])
+
+echo ""
+echo "CFLAGS : $CFLAGS"
+echo "LDFLAGS : $LDFLAGS"
+echo "LIBTBM_CFLAGS : $LIBTBM_CFLAGS"
+echo "LIBTBM_LIBS : $LIBTBM_LIBS"
+echo "BUFMGR_MODULE_DIR : $BUFMGR_MODULE_PATH"
+echo ""
+
--- /dev/null
+
+#ifndef __TIZEN_UI_TBM_SURFACE_DOC_H__
+#define __TIZEN_UI_TBM_SURFACE_DOC_H__
+
+/**
+ * @defgroup CAPI_UI_TBM_SURFACE_MODULE TBM Surface
+ * @brief The tbm_surface stands for the surface in the tizen.
+ * @ingroup CAPI_UI_FRAMEWORK
+ *
+ * @section CAPI_UI_TBM_SURFACE_MODULE_HEADER Required Header
+ * \#include <tbm_surface.h>
+ * @section CAPI_UI_TBM_SURFACE_MODULE_OVERVIEW Overview
+ * TBM Surface API provides user interface for The TBM Surface.\n
+ * The user can get pointer of plane in the surface.\n
+ * The user can store data to low level graphic buffer by using pointer of each plane.\n\n
+ * Features :\n
+ * - Support formats RGB format and YUV format.\n
+ * - Get format list supported by the system.\n
+ * - Access the surface with the access type.\n
+ * - Support the multiple plane graphic buffer.\n
+ * - Get the information of surface and planes.
+ */
+
+#endif /* __TIZEN_UI_TBM_SURFACE_DOC_H__ */
+
--- /dev/null
+SUBDIRS = .
+
+AM_CFLAGS = \
+ $(WARN_CFLAGS) \
+ -I$(top_srcdir) \
+ -I$(top_srcdir)/src \
+ -I$(top_srcdir)/drm_slp \
+ $(PTHREADSTUBS_CFLAGS) \
+ -I$(top_srcdir)/include/drm
+
+libdrm_slp_la_LTLIBRARIES = libdrm_slp.la
+libdrm_slp_ladir = $(libdir)
+libdrm_slp_la_LDFLAGS = -version-number 1:0:0 -no-undefined
+libdrm_slp_la_LIBADD = ../src/libtbm.la @PTHREADSTUBS_LIBS@ @CLOCK_LIB@ -ldl
+
+libdrm_slp_la_SOURCES = \
+ drm_slp_bufmgr.c \
+ drm_slp_bufmgr.h
+
--- /dev/null
+/**************************************************************************
+
+xserver-xorg-video-sec
+
+Copyright 2011 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.
+
+**************************************************************************/
+
+#include "config.h"
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include "drm_slp_bufmgr.h"
+#include "tbm_bufmgr.h"
+#include "tbm_bufmgr_int.h"
+
+drm_slp_bufmgr
+drm_slp_bufmgr_init(int fd, void *arg)
+{
+ tbm_bufmgr bufmgr = NULL;
+
+ bufmgr = tbm_bufmgr_init (fd);
+ if (!bufmgr)
+ {
+ TBM_LOG ("[libdrm_slp:%d]: error bufmgr is null\n", getpid());
+ return NULL;
+ }
+
+ return (drm_slp_bufmgr)bufmgr;
+}
+
+void
+drm_slp_bufmgr_destroy(drm_slp_bufmgr bufmgr)
+{
+ tbm_bufmgr_deinit ((tbm_bufmgr)bufmgr);
+}
+
+void
+drm_slp_bo_unref(drm_slp_bo bo)
+{
+ tbm_bo_unref ((tbm_bo)bo);
+}
+
+drm_slp_bo
+drm_slp_bo_import(drm_slp_bufmgr bufmgr, unsigned int key)
+{
+ tbm_bo bo = NULL;
+
+ bo = tbm_bo_import ((tbm_bufmgr)bufmgr, key);
+ if (!bo)
+ {
+ TBM_LOG ("[libdrm_slp:%d]: error bo is null\n", getpid());
+ return NULL;
+ }
+
+ return (drm_slp_bo)bo;
+}
+
+unsigned int
+drm_slp_bo_map(drm_slp_bo bo, int device, int opt)
+{
+ tbm_bo_handle bo_handle;
+ unsigned int ret = 0;
+
+ bo_handle = tbm_bo_map ((tbm_bo)bo, device, opt);
+ if (bo_handle.ptr == NULL)
+ {
+ TBM_LOG ("[libdrm_slp:%d]: error bo_handle is null\n", getpid());
+ return 0;
+ }
+
+ switch (device)
+ {
+ case TBM_DEVICE_DEFAULT:
+ case TBM_DEVICE_2D:
+ case TBM_DEVICE_3D:
+ case TBM_DEVICE_MM:
+ ret = (unsigned int)bo_handle.u32;
+ break;
+ case TBM_DEVICE_CPU:
+ ret = (unsigned int)bo_handle.ptr;
+ break;
+ default:
+ TBM_LOG ("[libdrm_slp:%d]: error wrong device type\n", getpid());
+ return 0;
+ }
+
+ return ret;
+}
+
+int
+drm_slp_bo_unmap(drm_slp_bo bo, int device)
+{
+ tbm_bo_unmap ((tbm_bo)bo);
+
+ return 1;
+}
+
--- /dev/null
+/**************************************************************************
+
+xserver-xorg-video-sec
+
+Copyright 2012 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.
+
+**************************************************************************/
+
+#ifndef _DRM_SLP_BUFMGR_H_
+#define _DRM_SLP_BUFMGR_H_
+
+#include "tbm_bufmgr.h"
+
+typedef struct _drm_slp_bo * drm_slp_bo;
+typedef struct _drm_slp_bufmgr * drm_slp_bufmgr;
+
+/* Functions for buffer mnager */
+drm_slp_bufmgr drm_slp_bufmgr_init(int fd, void * arg);
+void drm_slp_bufmgr_destroy(drm_slp_bufmgr bufmgr);
+
+/*Functions for bo*/
+void drm_slp_bo_unref(drm_slp_bo bo);
+drm_slp_bo drm_slp_bo_import(drm_slp_bufmgr bufmgr, unsigned int key);
+unsigned int drm_slp_bo_map(drm_slp_bo bo, int device, int opt);
+int drm_slp_bo_unmap(drm_slp_bo bo, int device);
+
+#endif /* _DRM_SLP_BUFMGR_H_ */
--- /dev/null
+<manifest>
+ <request>
+ <domain name="_"/>
+ </request>
+</manifest>
--- /dev/null
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libtbm
+Description: the library for Tizen Buffer Manager
+Version: @PACKAGE_VERSION@
+Requires: capi-base-common
+Libs: -L${libdir} -ltbm
+Cflags: -I${includedir}
+
--- /dev/null
+Name: libtbm
+Version: 1.1.1
+Release: 5
+License: MIT
+Summary: The library for Tizen Buffer Manager
+Group: System/Libraries
+Source0: %{name}-%{version}.tar.gz
+
+BuildRequires: pkgconfig(pthread-stubs)
+BuildRequires: pkgconfig(libdrm)
+BuildRequires: pkgconfig(x11)
+BuildRequires: pkgconfig(libdri2)
+BuildRequires: pkgconfig(capi-base-common)
+BuildRequires: pkgconfig(dlog)
+
+%description
+Description: %{summary}
+
+%package devel
+Summary: Tizen Buffer Manager Library - Development
+Group: Development/Libraries
+Requires: libtbm = %{version}
+Requires: pkgconfig(capi-base-common)
+
+%description devel
+The library for Tizen Buffer Manager.
+
+Development Files.
+
+%prep
+%setup -q
+
+%build
+
+%reconfigure --prefix=%{_prefix} \
+ CFLAGS="${CFLAGS} -Wall -Werror" LDFLAGS="${LDFLAGS} -Wl,--hash-style=both -Wl,--as-needed"
+
+make %{?_smp_mflags}
+
+%install
+rm -rf %{buildroot}
+mkdir -p %{buildroot}/usr/share/license
+cp -af COPYING %{buildroot}/usr/share/license/%{name}
+%make_install
+
+
+%clean
+rm -rf %{buildroot}
+
+%post -p /sbin/ldconfig
+%postun -p /sbin/ldconfig
+
+
+%files
+%manifest libtbm.manifest
+%defattr(-,root,root,-)
+/usr/share/license/%{name}
+%{_libdir}/libtbm.so.*
+%{_libdir}/libdrm_slp.so.*
+
+%files devel
+%defattr(-,root,root,-)
+%dir %{_includedir}
+%{_includedir}/tbm_bufmgr.h
+%{_includedir}/tbm_surface.h
+%{_includedir}/tbm_surface_internal.h
+%{_includedir}/tbm_bufmgr_backend.h
+%{_includedir}/tbm_type.h
+%{_libdir}/libtbm.so
+%{_libdir}/libdrm_slp.so
+%{_libdir}/pkgconfig/libtbm.pc
+
--- /dev/null
+SUBDIRS = .
+
+AM_CFLAGS = \
+ $(WARN_CFLAGS) \
+ -I$(top_srcdir) \
+ -I$(top_srcdir)/src \
+ $(PTHREADSTUBS_CFLAGS) \
+ @LIBTBM_CFLAGS@
+
+libtbm_la_LTLIBRARIES = libtbm.la
+libtbm_ladir = $(libdir)
+libtbm_la_LDFLAGS = -version-number 1:0:0 -no-undefined
+libtbm_la_LIBADD = @LIBTBM_LIBS@ @PTHREADSTUBS_LIBS@ @CLOCK_LIB@ -ldl
+
+libtbm_la_SOURCES = \
+ tbm_surface_internal.c \
+ tbm_surface.c \
+ tbm_bufmgr_backend.c \
+ tbm_bufmgr.c
+
+libtbmincludedir=$(includedir)
+libtbminclude_HEADERS = tbm_bufmgr.h tbm_surface.h tbm_bufmgr_backend.h tbm_type.h tbm_surface_internal.h
+
--- /dev/null
+/*
+ *
+ * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
+ * All Rights Reserved.
+ *
+ * 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 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
+ * THE COPYRIGHT HOLDERS, AUTHORS 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.
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ */
+
+/**
+ * \file
+ * List macros heavily inspired by the Linux kernel
+ * list handling. No list looping yet.
+ *
+ * Is not threadsafe, so common operations need to
+ * be protected using an external mutex.
+ */
+#ifndef _U_DOUBLE_LIST_H_
+#define _U_DOUBLE_LIST_H_
+
+#include <stddef.h>
+
+static void list_inithead(struct list_head *item)
+{
+ item->prev = item;
+ item->next = item;
+}
+
+static inline void list_add(struct list_head *item, struct list_head *list)
+{
+ item->prev = list;
+ item->next = list->next;
+ list->next->prev = item;
+ list->next = item;
+}
+
+static inline void list_addtail(struct list_head *item, struct list_head *list)
+{
+ item->next = list;
+ item->prev = list->prev;
+ list->prev->next = item;
+ list->prev = item;
+}
+
+static inline void list_replace(struct list_head *from, struct list_head *to)
+{
+ to->prev = from->prev;
+ to->next = from->next;
+ from->next->prev = to;
+ from->prev->next = to;
+}
+
+static inline void list_del(struct list_head *item)
+{
+ item->prev->next = item->next;
+ item->next->prev = item->prev;
+}
+
+static inline void list_delinit(struct list_head *item)
+{
+ item->prev->next = item->next;
+ item->next->prev = item->prev;
+ item->next = item;
+ item->prev = item;
+}
+
+#define LIST_INITHEAD(__item) list_inithead(__item)
+#define LIST_ADD(__item, __list) list_add(__item, __list)
+#define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
+#define LIST_REPLACE(__from, __to) list_replace(__from, __to)
+#define LIST_DEL(__item) list_del(__item)
+#define LIST_DELINIT(__item) list_delinit(__item)
+
+#define LIST_ENTRY(__type, __item, __field) \
+ ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
+
+#define LIST_IS_EMPTY(__list) \
+ ((__list)->next == (__list))
+
+#ifndef container_of
+#define container_of(ptr, sample, member) \
+ (void *)((char *)(ptr) \
+ - ((char *)&(sample)->member - (char *)(sample)))
+#endif
+
+#define LIST_FOR_EACH_ENTRY(pos, head, member) \
+ for (pos = container_of((head)->next, pos, member); \
+ &pos->member != (head); \
+ pos = container_of(pos->member.next, pos, member))
+
+#define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
+ for (pos = container_of((head)->next, pos, member), \
+ storage = container_of(pos->member.next, pos, member); \
+ &pos->member != (head); \
+ pos = storage, storage = container_of(storage->member.next, storage, member))
+
+#define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
+ for (pos = container_of((head)->prev, pos, member), \
+ storage = container_of(pos->member.prev, pos, member); \
+ &pos->member != (head); \
+ pos = storage, storage = container_of(storage->member.prev, storage, member))
+
+#define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
+ for (pos = container_of((start), pos, member); \
+ &pos->member != (head); \
+ pos = container_of(pos->member.next, pos, member))
+
+#define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
+ for (pos = container_of((start), pos, member); \
+ &pos->member != (head); \
+ pos = container_of(pos->member.prev, pos, member))
+
+#endif /*_U_DOUBLE_LIST_H_*/
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#include "config.h"
+
+#include "tbm_bufmgr.h"
+#include "tbm_bufmgr_int.h"
+#include "tbm_bufmgr_backend.h"
+#include "tbm_bufmgr_tgl.h"
+#include "list.h"
+#include <X11/Xmd.h>
+#include <dri2.h>
+#include <xf86drm.h>
+
+#define DEBUG
+#ifdef DEBUG
+int bDebug = 0;
+#define DBG(...) if(bDebug&0x1) TBM_LOG (__VA_ARGS__)
+#define DBG_LOCK(...) if(bDebug&0x2) TBM_LOG (__VA_ARGS__)
+#else
+#define DBG(...)
+#define DBG_LOCK(...)
+#endif
+
+#define PREFIX_LIB "libtbm_"
+#define SUFFIX_LIB ".so"
+#define DEFAULT_LIB PREFIX_LIB"default"SUFFIX_LIB
+
+#define BO_IS_CACHEABLE(bo) ((bo->flags & TBM_BO_NONCACHABLE)?0:1)
+#define DEVICE_IS_CACHE_AWARE(device) ((device == TBM_DEVICE_CPU)?(1):(0))
+
+/* tgl key values */
+#define GLOBAL_KEY ((unsigned int)(-1))
+#define INITIAL_KEY ((unsigned int)(-2))
+
+#define CACHE_OP_CREATE (-1)
+#define CACHE_OP_ATTACH (-2)
+#define CACHE_OP_IMPORT (-3)
+
+/* values to indicate unspecified fields in XF86ModReqInfo. */
+#define MAJOR_UNSPEC 0xFF
+#define MINOR_UNSPEC 0xFF
+#define PATCH_UNSPEC 0xFFFF
+#define ABI_VERS_UNSPEC 0xFFFFFFFF
+
+#define MODULE_VERSION_NUMERIC(maj, min, patch) \
+ ((((maj) & 0xFF) << 24) | (((min) & 0xFF) << 16) | (patch & 0xFFFF))
+#define GET_MODULE_MAJOR_VERSION(vers) (((vers) >> 24) & 0xFF)
+#define GET_MODULE_MINOR_VERSION(vers) (((vers) >> 16) & 0xFF)
+#define GET_MODULE_PATCHLEVEL(vers) ((vers) & 0xFFFF)
+
+enum {
+ LOCK_TRY_ONCE,
+ LOCK_TRY_ALWAYS,
+ LOCK_TRY_NEVER
+};
+
+enum {
+ DEVICE_NONE = 0,
+ DEVICE_CA, /* cache aware device */
+ DEVICE_CO /* cache oblivious device */
+};
+
+typedef struct
+{
+ unsigned long key;
+ void *data;
+ tbm_data_free free_func ;
+
+ /* link of user_data */
+ struct list_head item_link;
+} tbm_user_data;
+
+pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
+tbm_bufmgr gBufMgr = NULL;
+
+static inline int
+_tgl_init (int fd, unsigned int key)
+{
+ struct tgl_attribute attr;
+ int err;
+
+ attr.key = key;
+ attr.timeout_ms = 1000;
+
+ err = ioctl (fd, TGL_IOC_INIT_LOCK, &attr);
+ if (err)
+ {
+ TBM_LOG ( "[libtbm:%d] "
+ "error(%s) %s:%d key:%d\n",
+ getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
+ return 0;
+ }
+
+ return 1;
+}
+
+static inline int
+_tgl_destroy (int fd, unsigned int key)
+{
+ int err;
+ err = ioctl (fd, TGL_IOC_DESTROY_LOCK, key);
+ if (err)
+ {
+ TBM_LOG ( "[libtbm:%d] "
+ "error(%s) %s:%d key:%d\n",
+ getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
+ return 0;
+ }
+
+ return 1;
+}
+
+static inline int
+_tgl_lock (int fd, unsigned int key)
+{
+ int err;
+ err = ioctl (fd, TGL_IOC_LOCK_LOCK, key);
+ if (err)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error(%s) %s:%d key:%d\n",
+ getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
+ return 0;
+ }
+
+ return 1;
+}
+
+static inline int
+_tgl_unlock (int fd, unsigned int key)
+{
+ int err;
+ err = ioctl (fd, TGL_IOC_UNLOCK_LOCK, key);
+ if (err)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error(%s) %s:%d key:%d\n",
+ getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
+ return 0;
+ }
+
+ return 1;
+}
+
+static inline int
+_tgl_set_data (int fd, unsigned int key, unsigned int val)
+{
+ int err;
+ struct tgl_user_data arg;
+
+ arg.key = key;
+ arg.data1 = val;
+ err = ioctl (fd, TGL_IOC_SET_DATA, &arg);
+ if (err)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error(%s) %s:%d key:%d\n",
+ getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
+ return 0;
+ }
+
+ return 1;
+}
+
+static inline unsigned int
+_tgl_get_data (int fd, unsigned int key, unsigned int *locked)
+{
+ int err;
+ struct tgl_user_data arg = {0,};
+
+ arg.key = key;
+ err = ioctl (fd, TGL_IOC_GET_DATA, &arg);
+ if (err)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error(%s) %s:%d key:%d\n",
+ getpid(), strerror(errno), __FUNCTION__, __LINE__, key);
+ return 0;
+ }
+
+ if (locked)
+ *locked = arg.locked;
+
+ return arg.data1;
+}
+
+static tbm_user_data *
+_user_data_lookup (struct list_head *user_data_list, unsigned long key)
+{
+ tbm_user_data *user_data = NULL;
+ tbm_user_data *old_data = NULL, *tmp = NULL;
+
+ if (!LIST_IS_EMPTY (user_data_list))
+ {
+ LIST_FOR_EACH_ENTRY_SAFE (old_data, tmp, user_data_list, item_link)
+ {
+ if (old_data->key == key)
+ {
+ user_data = old_data;
+ return user_data;
+ }
+ }
+ }
+
+ return user_data;
+}
+
+static tbm_user_data *
+_user_data_create (unsigned long key, tbm_data_free data_free_func)
+{
+ tbm_user_data * user_data = NULL;
+
+ user_data = calloc (1, sizeof (tbm_user_data));
+ if (!user_data)
+ return NULL;
+
+ user_data->key = key;
+ user_data->free_func = data_free_func;
+ user_data->data = (void *)0;
+
+ return user_data;
+}
+
+static void
+_user_data_delete (tbm_user_data *user_data)
+{
+ if (user_data->data && user_data->free_func)
+ user_data->free_func(user_data->data);
+
+ LIST_DEL (&user_data->item_link);
+
+ free(user_data);
+}
+
+static int
+_bo_lock (tbm_bo bo, int device, int opt)
+{
+ tbm_bufmgr bufmgr = bo->bufmgr;
+ int ret = 0;
+
+ if (TBM_LOCK_CTRL_BACKEND_VALID(bufmgr->backend->flags))
+ {
+ if (bufmgr->backend->bo_lock2)
+ {
+ /* use bo_lock2 backend lock */
+ ret = bufmgr->backend->bo_lock2 (bo, device, opt);
+ }
+ else if (bufmgr->backend->bo_lock)
+ {
+ /* use bo_lock backend lock */
+ ret = bufmgr->backend->bo_lock (bo);
+ }
+ else
+ TBM_LOG ("[libtbm:%d] "
+ "error %s:%d no backend lock functions\n",
+ getpid(), __FUNCTION__, __LINE__);
+ }
+ else
+ {
+ /* use tizen global lock */
+ ret = _tgl_lock (bufmgr->lock_fd, bo->tgl_key);
+ }
+
+ return ret;
+}
+
+static void
+_bo_unlock (tbm_bo bo)
+{
+ tbm_bufmgr bufmgr = bo->bufmgr;
+
+ if (TBM_LOCK_CTRL_BACKEND_VALID(bufmgr->backend->flags))
+ {
+ if (bufmgr->backend->bo_unlock)
+ {
+ /* use backend unlock */
+ bufmgr->backend->bo_unlock (bo);
+ }
+ else
+ TBM_LOG ("[libtbm:%d] "
+ "error %s:%d no backend unlock functions\n",
+ getpid(), __FUNCTION__, __LINE__);
+ }
+ else
+ {
+ /* use tizen global unlock */
+ _tgl_unlock (bufmgr->lock_fd, bo->tgl_key);
+ }
+}
+
+static int
+_tbm_bo_init_state (tbm_bo bo, int opt)
+{
+ tbm_bufmgr bufmgr = bo->bufmgr;
+ tbm_bo_cache_state cache_state;
+
+ RETURN_VAL_CHECK_FLAG (TBM_ALL_CTRL_BACKEND_VALID(bufmgr->backend->flags), 1);
+
+ cache_state.val = 0;
+ switch (opt)
+ {
+ case CACHE_OP_CREATE: /*Create*/
+ if (bo->tgl_key == INITIAL_KEY)
+ bo->tgl_key = bufmgr->backend->bo_get_global_key (bo);
+
+ _tgl_init (bufmgr->lock_fd, bo->tgl_key);
+
+ cache_state.data.isCacheable = BO_IS_CACHEABLE(bo);
+ cache_state.data.isDirtied = DEVICE_NONE;
+ cache_state.data.isCached = 0;
+ cache_state.data.cntFlush = 0;
+
+ _tgl_set_data (bufmgr->lock_fd, bo->tgl_key, cache_state.val);
+ break;
+ case CACHE_OP_IMPORT: /*Import*/
+ if (bo->tgl_key == INITIAL_KEY)
+ bo->tgl_key = bufmgr->backend->bo_get_global_key (bo);
+
+ _tgl_init (bufmgr->lock_fd, bo->tgl_key);
+ break;
+ default:
+ break;
+ }
+
+ return 1;
+}
+
+static void
+_tbm_bo_destroy_state (tbm_bo bo)
+{
+ tbm_bufmgr bufmgr = bo->bufmgr;
+
+ RETURN_CHECK_FLAG (TBM_ALL_CTRL_BACKEND_VALID(bufmgr->backend->flags));
+
+ _tgl_destroy (bufmgr->lock_fd, bo->tgl_key);
+}
+
+static int
+_tbm_bo_set_state (tbm_bo bo, int device, int opt)
+{
+ tbm_bufmgr bufmgr = bo->bufmgr;
+ char need_flush = 0;
+ unsigned short cntFlush = 0;
+ unsigned int is_locked;
+
+ RETURN_VAL_CHECK_FLAG (TBM_CACHE_CTRL_BACKEND_VALID(bufmgr->backend->flags), 1);
+
+ /* get cache state of a bo */
+ bo->cache_state.val = _tgl_get_data (bufmgr->lock_fd, bo->tgl_key, &is_locked);
+
+ if (!bo->cache_state.data.isCacheable)
+ return 1;
+
+ /* get global cache flush count */
+ cntFlush = (unsigned short)_tgl_get_data (bufmgr->lock_fd, GLOBAL_KEY, NULL);
+
+ if (DEVICE_IS_CACHE_AWARE (device))
+ {
+ if (bo->cache_state.data.isDirtied == DEVICE_CO &&
+ bo->cache_state.data.isCached)
+ {
+ need_flush = TBM_CACHE_INV;
+ }
+
+ bo->cache_state.data.isCached = 1;
+ if (opt & TBM_OPTION_WRITE)
+ bo->cache_state.data.isDirtied = DEVICE_CA;
+ else
+ {
+ if( bo->cache_state.data.isDirtied != DEVICE_CA )
+ bo->cache_state.data.isDirtied = DEVICE_NONE;
+ }
+ }
+ else
+ {
+ if (bo->cache_state.data.isDirtied == DEVICE_CA &&
+ bo->cache_state.data.isCached &&
+ bo->cache_state.data.cntFlush == cntFlush)
+ {
+ need_flush = TBM_CACHE_CLN | TBM_CACHE_ALL;
+ }
+
+ if (opt & TBM_OPTION_WRITE)
+ bo->cache_state.data.isDirtied = DEVICE_CO;
+ else
+ {
+ if( bo->cache_state.data.isDirtied != DEVICE_CO )
+ bo->cache_state.data.isDirtied = DEVICE_NONE;
+ }
+ }
+
+ if (need_flush)
+ {
+ /* set global cache flush count */
+ if (need_flush & TBM_CACHE_ALL)
+ _tgl_set_data (bufmgr->lock_fd, GLOBAL_KEY, (unsigned int)(++cntFlush));
+
+ /* call backend cache flush */
+ bufmgr->backend->bo_cache_flush (bo, need_flush);
+
+ DBG ("[libtbm:%d] \tcache(%d,%d,%d)....flush:0x%x, cntFlush(%d)\n", getpid(),
+ bo->cache_state.data.isCacheable,
+ bo->cache_state.data.isCached,
+ bo->cache_state.data.isDirtied,
+ need_flush, cntFlush);
+ }
+
+ return 1;
+}
+
+static void
+_tbm_bo_save_state (tbm_bo bo)
+{
+ tbm_bufmgr bufmgr = bo->bufmgr;
+ unsigned short cntFlush = 0;
+
+ RETURN_CHECK_FLAG (TBM_CACHE_CTRL_BACKEND_VALID(bufmgr->backend->flags));
+
+ /* get global cache flush count */
+ cntFlush = (unsigned short)_tgl_get_data (bufmgr->lock_fd, GLOBAL_KEY, NULL);
+
+ /* save global cache flush count */
+ bo->cache_state.data.cntFlush = cntFlush;
+ _tgl_set_data(bufmgr->lock_fd, bo->tgl_key, bo->cache_state.val);
+}
+
+
+static int
+_tbm_bo_lock (tbm_bo bo, int device, int opt)
+{
+ tbm_bufmgr bufmgr = NULL;
+ int old;
+ int ret = 0;
+
+ if (!bo)
+ return ret;
+
+ bufmgr = bo->bufmgr;
+
+ /* do not try to lock the bo */
+ if (bufmgr->lock_type == LOCK_TRY_NEVER)
+ return 1;
+
+ if (bo->lock_cnt < 0)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error %s:%d bo:%p(%d) LOCK_CNT=%d\n",
+ getpid(), __FUNCTION__, __LINE__, bo, bo->tgl_key, bo->lock_cnt);
+ }
+
+ old = bo->lock_cnt;
+ if (bufmgr->lock_type == LOCK_TRY_ONCE)
+ {
+ if (bo->lock_cnt == 0)
+ {
+ pthread_mutex_unlock (&bufmgr->lock);
+ ret = _bo_lock (bo, device, opt);
+ pthread_mutex_lock (&bufmgr->lock);
+ if (ret)
+ bo->lock_cnt++;
+ }
+ else
+ ret = 1;
+ }
+ else if (bufmgr->lock_type == LOCK_TRY_ALWAYS)
+ {
+ pthread_mutex_unlock (&bufmgr->lock);
+ ret = _bo_lock (bo, device, opt);
+ pthread_mutex_lock (&bufmgr->lock);
+ if(ret)
+ bo->lock_cnt++;
+ }
+ else
+ TBM_DLOG ("[libtbm:%d] "
+ "error %s:%d bo:%p lock_type is wrong.\n",
+ getpid(), __FUNCTION__, __LINE__, bo);
+
+ DBG_LOCK ("[libtbm:%d] >> LOCK bo:%p(%d, %d->%d)\n", getpid(),
+ bo, bo->tgl_key, old, bo->lock_cnt);
+
+ return ret;
+}
+
+static void
+_tbm_bo_unlock (tbm_bo bo)
+{
+ tbm_bufmgr bufmgr = NULL;
+
+ int old;
+
+ if (!bo)
+ return;
+
+ bufmgr = bo->bufmgr;
+
+ /* do not try to unlock the bo */
+ if (bufmgr->lock_type == LOCK_TRY_NEVER)
+ return;
+
+ old = bo->lock_cnt;
+ if (bufmgr->lock_type == LOCK_TRY_ONCE)
+ {
+ if (bo->lock_cnt > 0)
+ {
+ bo->lock_cnt--;
+ if (bo->lock_cnt == 0)
+ _bo_unlock (bo);
+ }
+ }
+ else if (bufmgr->lock_type == LOCK_TRY_ALWAYS)
+ {
+ if (bo->lock_cnt > 0)
+ {
+ bo->lock_cnt--;
+ _bo_unlock (bo);
+ }
+ }
+ else
+ TBM_LOG ("[libtbm:%d] "
+ "error %s:%d bo:%p lock_type is wrong.\n",
+ getpid(), __FUNCTION__, __LINE__, bo);
+
+ if (bo->lock_cnt < 0)
+ bo->lock_cnt = 0;
+
+ DBG_LOCK ("[libtbm:%d] << unlock bo:%p(%d, %d->%d)\n", getpid(),
+ bo, bo->tgl_key, old, bo->lock_cnt);
+}
+
+static int
+_tbm_bo_is_valid(tbm_bo bo)
+{
+ tbm_bo old_data=NULL, tmp = NULL;;
+
+ if( bo == NULL )
+ return 0;
+
+ if(!LIST_IS_EMPTY (&gBufMgr->bo_list))
+ {
+ LIST_FOR_EACH_ENTRY_SAFE (old_data, tmp, &gBufMgr->bo_list, item_link)
+ {
+ if(old_data == bo)
+ {
+ return 1;
+ }
+ }
+
+ }
+ return 0;
+}
+
+static void
+_tbm_bo_ref (tbm_bo bo)
+{
+ bo->ref_cnt++;
+}
+
+static void
+_tbm_bo_unref (tbm_bo bo)
+{
+ tbm_bufmgr bufmgr = bo->bufmgr;
+ tbm_user_data *old_data = NULL, *tmp = NULL;
+
+ if (bo->ref_cnt <= 0)
+ return;
+
+ bo->ref_cnt--;
+ if (bo->ref_cnt == 0)
+ {
+ /* destory the user_data_list */
+ if (!LIST_IS_EMPTY (&bo->user_data_list))
+ {
+ LIST_FOR_EACH_ENTRY_SAFE (old_data, tmp, &bo->user_data_list, item_link)
+ {
+ DBG ("[libtbm:%d] free user_data \n", getpid());
+ _user_data_delete (old_data);
+ }
+ }
+
+ if (bo->lock_cnt > 0)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error %s:%d lock_cnt:%d\n",
+ getpid(), __FUNCTION__, __LINE__, bo->lock_cnt);
+ _bo_unlock (bo);
+ }
+
+ /* Destroy Global Lock */
+ _tbm_bo_destroy_state (bo);
+
+ /* call the bo_free */
+ bufmgr->backend->bo_free (bo);
+ bo->priv = NULL;
+
+ LIST_DEL (&bo->item_link);
+ free(bo);
+ bo = NULL;
+ }
+
+}
+
+static int
+_tbm_bufmgr_init_state (tbm_bufmgr bufmgr)
+{
+ RETURN_VAL_CHECK_FLAG (TBM_ALL_CTRL_BACKEND_VALID(bufmgr->backend->flags), 1);
+
+ bufmgr->lock_fd = open (tgl_devfile, O_RDWR);
+
+ if(bufmgr->lock_fd < 0)
+ {
+ bufmgr->lock_fd = open (tgl_devfile1, O_RDWR);
+ if(bufmgr->lock_fd < 0)
+ {
+
+ TBM_LOG ("[libtbm:%d] "
+ "error: Fail to open global_lock:%s\n",
+ getpid(), tgl_devfile);
+ return 0;
+ }
+ }
+
+ if (!_tgl_init(bufmgr->lock_fd, GLOBAL_KEY))
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error: Fail to initialize the tgl\n",
+ getpid());
+ return 0;
+ }
+
+ return 1;
+}
+
+static void
+_tbm_bufmgr_destroy_state (tbm_bufmgr bufmgr)
+{
+ RETURN_CHECK_FLAG (TBM_ALL_CTRL_BACKEND_VALID(bufmgr->backend->flags));
+
+ close (bufmgr->lock_fd);
+}
+
+static int
+_check_version (TBMModuleVersionInfo *data)
+{
+ int abimaj, abimin;
+ int vermaj, vermin;
+
+ abimaj = GET_ABI_MAJOR (data->abiversion);
+ abimin = GET_ABI_MINOR (data->abiversion);
+
+ TBM_LOG ("[libtbm:%d] "
+ "TBM module %s: vendor=\"%s\" ABI=%d,%d\n",
+ getpid(), data->modname ? data->modname : "UNKNOWN!",
+ data->vendor ? data->vendor : "UNKNOWN!", abimaj, abimin);
+
+ vermaj = GET_ABI_MAJOR (TBM_ABI_VERSION);
+ vermin = GET_ABI_MINOR (TBM_ABI_VERSION);
+
+ DBG ("[libtbm:%d] " "TBM ABI version %d.%d\n", getpid(), vermaj, vermin);
+
+ if (abimaj != vermaj)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "TBM module ABI major ver(%d) doesn't match the TBM's ver(%d)\n",
+ getpid(), abimaj, vermaj);
+ return 0;
+ }
+ else if (abimin > vermin)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "TBM module ABI minor ver(%d) is newer than the TBM's ver(%d)\n",
+ getpid(), abimin, vermin);
+ return 0;
+ }
+ return 1;
+}
+
+static int
+_tbm_bufmgr_load_module (tbm_bufmgr bufmgr, int fd, const char *file)
+{
+ char path[PATH_MAX] = {0,};
+ TBMModuleData *initdata = NULL;
+ void * module_data;
+
+ snprintf(path, sizeof(path), BUFMGR_MODULE_DIR "/%s", file);
+
+ module_data = dlopen (path, RTLD_LAZY);
+ if (!module_data)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "failed to load module: %s(%s)\n",
+ getpid(), dlerror(), file);
+ return 0;
+ }
+
+ initdata = dlsym (module_data, "tbmModuleData");
+ if (initdata)
+ {
+ ModuleInitProc init;
+ TBMModuleVersionInfo *vers;
+
+ vers = initdata->vers;
+ init = initdata->init;
+
+ if (vers)
+ {
+ if (!_check_version (vers))
+ {
+ dlclose (module_data);
+ return 0;
+ }
+ }
+ else
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "Error: module does not supply version information.\n",
+ getpid());
+
+ dlclose (module_data);
+ return 0;
+ }
+
+ if (init)
+ {
+ if(!init (bufmgr, fd))
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "Fail to init module(%s)\n",
+ getpid(), file);
+ dlclose (module_data);
+ return 0;
+ }
+
+ if (!bufmgr->backend || !bufmgr->backend->priv)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "Error: module(%s) wrong operation. Check backend or backend's priv.\n",
+ getpid(), file);
+ dlclose (module_data);
+ return 0;
+ }
+ }
+ else
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "Error: module does not supply init symbol.\n", getpid());
+ dlclose (module_data);
+ return 0;
+ }
+ }
+ else
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "Error: module does not have data object.\n", getpid());
+ dlclose (module_data);
+ return 0;
+ }
+
+ bufmgr->module_data = module_data;
+
+ TBM_LOG ("[libtbm:%d] "
+ "Success to load module(%s)\n", getpid(), file);
+
+ return 1;
+}
+
+static int _tbm_load_module (tbm_bufmgr bufmgr, int fd)
+{
+ struct dirent **namelist;
+ const char *p = NULL;
+ int n;
+ int ret = 0;
+
+ /* load bufmgr priv from default lib */
+ ret = _tbm_bufmgr_load_module (bufmgr, fd, DEFAULT_LIB);
+
+ /* load bufmgr priv from configured path */
+ if (!ret)
+ {
+ n = scandir (BUFMGR_MODULE_DIR, &namelist, 0, alphasort);
+ if (n < 0)
+ TBM_LOG ("[libtbm:%d] "
+ "no files : %s\n", getpid(), BUFMGR_MODULE_DIR);
+ else
+ {
+ while(n--)
+ {
+ if (!ret && strstr (namelist[n]->d_name, PREFIX_LIB))
+ {
+ p = strstr (namelist[n]->d_name, SUFFIX_LIB);
+ if (p != NULL)
+ {
+ if (!strcmp (p, SUFFIX_LIB))
+ {
+ ret = _tbm_bufmgr_load_module (bufmgr, fd, namelist[n]->d_name);
+ }
+ }
+ }
+ free(namelist[n]);
+ }
+ free(namelist);
+ }
+ }
+
+ return ret;
+}
+
+static int
+_tbm_bufmgr_get_drm_fd()
+{
+ int screen;
+ Display *display;
+ int dri2Major, dri2Minor;
+ int eventBase, errorBase;
+ drm_magic_t magic;
+ char *driver_name, *device_name;
+ int fd;
+
+ display = XOpenDisplay(NULL);
+ if (!display)
+ {
+ TBM_LOG ("[libtbm:%d] Fail XOpenDisplay\n", getpid());
+ return -1;
+ }
+
+ screen = DefaultScreen(display);
+
+ if (!DRI2QueryExtension (display, &eventBase, &errorBase))
+ {
+ TBM_LOG ("[libtbm:%d] Fail DRI2QueryExtention\n", getpid());
+ XCloseDisplay(display);
+ return -1;
+ }
+
+ if (!DRI2QueryVersion (display, &dri2Major, &dri2Minor))
+ {
+ TBM_LOG ("[libtbm:%d] Fail DRI2QueryVersion\n", getpid());
+ XCloseDisplay(display);
+ return -1;
+ }
+
+ if (!DRI2Connect (display, RootWindow(display, screen), &driver_name, &device_name))
+ {
+ TBM_LOG ("[libtbm:%d] Fail DRI2Connect\n", getpid());
+ XCloseDisplay(display);
+ return -1;
+ }
+
+ fd = open (device_name, O_RDWR);
+ if (fd < 0)
+ {
+ TBM_LOG ("[libtbm:%d] cannot open drm device (%s)\n", getpid(), device_name);
+ free (driver_name);
+ free (device_name);
+ XCloseDisplay(display);
+ return -1;
+ }
+
+ if (drmGetMagic (fd, &magic))
+ {
+ TBM_LOG ("[libtbm:%d] Fail drmGetMagic\n", getpid());
+ free (driver_name);
+ free (device_name);
+ close(fd);
+ XCloseDisplay(display);
+ return -1;
+ }
+
+ if (!DRI2Authenticate(display, RootWindow(display, screen), magic))
+ {
+ TBM_LOG ("[libtbm:%d] Fail DRI2Authenticate\n", getpid());
+ free (driver_name);
+ free (device_name);
+ close(fd);
+ XCloseDisplay(display);
+ return -1;
+ }
+
+ if(!drmAuthMagic(fd, magic))
+ {
+ TBM_LOG ("[libtbm:%d] Fail drmAuthMagic\n", getpid());
+ free (driver_name);
+ free (device_name);
+ close(fd);
+ XCloseDisplay(display);
+ return -1;
+ }
+
+ free (driver_name);
+ free (device_name);
+ XCloseDisplay(display);
+ return fd;
+}
+
+tbm_bufmgr
+tbm_bufmgr_init (int fd)
+{
+ char *env;
+ int fd_flag = 0;
+
+ pthread_mutex_lock (&gLock);
+
+#ifdef DEBUG
+ env = getenv("GEM_DEBUG");
+ if(env)
+ {
+ bDebug = atoi(env);
+ TBM_LOG ("GEM_DEBUG=%s\n", env);
+ }
+ else
+ bDebug = 0;
+#endif
+
+ /* initialize buffer manager */
+ if (gBufMgr)
+ {
+ TBM_LOG ("[libtbm:%d] use previous gBufMgr\n", getpid());
+ gBufMgr->ref_count++;
+
+ if (dup2(gBufMgr->fd, fd) < 0) {
+ TBM_LOG ("[libtbm:%d] Fail to duplicate the drm fd\n", getpid());
+ pthread_mutex_unlock (&gLock);
+ }
+ TBM_LOG ("[libtbm:%d] duplicate the drm_fd(%d), new drm_fd(%d).\n",
+ getpid(), gBufMgr->fd, fd);
+
+ TBM_LOG ("[libtbm:%d] bufmgr ref: fd=%d, ref_count:%d\n",
+ getpid(), gBufMgr->fd, gBufMgr->ref_count);
+ pthread_mutex_unlock (&gLock);
+ return gBufMgr;
+ }
+
+ if (fd < 0)
+ {
+ fd = _tbm_bufmgr_get_drm_fd();
+ if (fd < 0)
+ {
+ TBM_LOG ("[libtbm:%d] Fail get drm fd\n", getpid());
+ pthread_mutex_unlock (&gLock);
+ return NULL;
+ }
+ fd_flag = 1;
+ }
+
+ TBM_LOG ("[libtbm:%d] bufmgr init: fd=%d\n", getpid(), fd);
+
+ /* allocate bufmgr */
+ gBufMgr = calloc (1, sizeof(struct _tbm_bufmgr));
+ if (!gBufMgr)
+ {
+ if (fd_flag)
+ close (fd);
+
+ pthread_mutex_unlock (&gLock);
+ return NULL;
+ }
+
+ /* load bufmgr priv from env */
+ if (!_tbm_load_module(gBufMgr, fd))
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error : Fail to load bufmgr backend\n",
+ getpid());
+ free (gBufMgr);
+ gBufMgr = NULL;
+
+ if (fd_flag)
+ close (fd);
+
+ pthread_mutex_unlock (&gLock);
+ return NULL;
+ }
+
+ gBufMgr->fd_flag = fd_flag;
+ gBufMgr->fd = fd;
+ gBufMgr->ref_count = 1;
+
+ TBM_LOG ("[libtbm:%d] create tizen bufmgr: ref_count:%d\n", getpid(), gBufMgr->ref_count);
+
+ if (pthread_mutex_init (&gBufMgr->lock, NULL) != 0)
+ {
+ gBufMgr->backend->bufmgr_deinit (gBufMgr->backend->priv);
+ tbm_backend_free (gBufMgr->backend);
+ dlclose (gBufMgr->module_data);
+ free (gBufMgr);
+ gBufMgr = NULL;
+ pthread_mutex_unlock (&gLock);
+ return NULL;
+ }
+
+ /* intialize the tizen global status */
+ if (!_tbm_bufmgr_init_state (gBufMgr))
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error: Fail to init state\n",
+ getpid());
+ gBufMgr->backend->bufmgr_deinit (gBufMgr->backend->priv);
+ tbm_backend_free (gBufMgr->backend);
+ pthread_mutex_destroy (&gBufMgr->lock);
+ dlclose (gBufMgr->module_data);
+ free (gBufMgr);
+ gBufMgr = NULL;
+ pthread_mutex_unlock (&gLock);
+ return NULL;
+ }
+
+ /* setup the lock_type */
+ env = getenv ("BUFMGR_LOCK_TYPE");
+ if (env && !strcmp (env, "always"))
+ gBufMgr->lock_type = LOCK_TRY_ALWAYS;
+ else if(env && !strcmp(env, "none"))
+ gBufMgr->lock_type = LOCK_TRY_NEVER;
+ else if(env && !strcmp(env, "once"))
+ gBufMgr->lock_type = LOCK_TRY_ONCE;
+ else
+ gBufMgr->lock_type = LOCK_TRY_ALWAYS;
+
+ DBG ("[libtbm:%d] BUFMGR_LOCK_TYPE=%s\n", getpid(), env?env:"default:once");
+
+ /* setup the map_cache */
+ env = getenv ("BUFMGR_MAP_CACHE");
+ if (env && !strcmp (env, "false"))
+ gBufMgr->use_map_cache = 0;
+ else
+ gBufMgr->use_map_cache = 1;
+ DBG ("[libtbm:%d] BUFMGR_MAP_CACHE=%s\n", getpid(), env?env:"default:true");
+
+ /* intialize bo_list */
+ LIST_INITHEAD (&gBufMgr->bo_list);
+
+ /* intialize surf_list */
+ LIST_INITHEAD (&gBufMgr->surf_list);
+
+ pthread_mutex_unlock (&gLock);
+ return gBufMgr;
+}
+
+void
+tbm_bufmgr_deinit (tbm_bufmgr bufmgr)
+{
+ TBM_RETURN_IF_FAIL (TBM_BUFMGR_IS_VALID(bufmgr));
+
+ tbm_bo bo = NULL;
+ tbm_bo tmp = NULL;
+
+ tbm_surface_h surf = NULL;
+ tbm_surface_h tmp_surf = NULL;
+
+ pthread_mutex_lock (&gLock);
+
+ bufmgr->ref_count--;
+ if (bufmgr->ref_count > 0)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "tizen bufmgr destroy: bufmgr:%p, ref_count:%d\n",
+ getpid(), bufmgr, bufmgr->ref_count);
+ pthread_mutex_unlock (&gLock);
+ return;
+ }
+
+ /* destroy bo_list */
+ if(!LIST_IS_EMPTY (&bufmgr->bo_list))
+ {
+ LIST_FOR_EACH_ENTRY_SAFE (bo, tmp, &bufmgr->bo_list, item_link)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "Un-freed bo(%p, ref:%d) \n",
+ getpid(), bo, bo->ref_cnt);
+ bo->ref_cnt = 1;
+ tbm_bo_unref(bo);
+ }
+ }
+
+ /* destroy surf_list */
+ if(!LIST_IS_EMPTY (&bufmgr->surf_list))
+ {
+ LIST_FOR_EACH_ENTRY_SAFE (surf, tmp_surf, &bufmgr->surf_list, item_link)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "Destroy surf(%p) \n",
+ getpid(), surf);
+ tbm_surface_destroy(surf);
+ }
+ }
+
+ /* destroy the tizen global status */
+ _tbm_bufmgr_destroy_state (bufmgr);
+
+ /* destroy bufmgr priv */
+ bufmgr->backend->bufmgr_deinit (bufmgr->backend->priv);
+ bufmgr->backend->priv = NULL;
+ tbm_backend_free (bufmgr->backend);
+ bufmgr->backend = NULL;
+
+ pthread_mutex_destroy (&bufmgr->lock);
+
+ TBM_LOG ("[libtbm:%d] "
+ "tizen bufmgr destroy: bufmgr:%p\n",
+ getpid(), bufmgr);
+
+ dlclose (bufmgr->module_data);
+
+ if(bufmgr->fd_flag)
+ close(bufmgr->fd);
+
+ free (bufmgr);
+ bufmgr = NULL;
+ gBufMgr = NULL;
+
+ pthread_mutex_unlock (&gLock);
+}
+
+int
+tbm_bo_size (tbm_bo bo)
+{
+ TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), 0);
+
+ tbm_bufmgr bufmgr = bo->bufmgr;
+ int size;
+
+ pthread_mutex_lock(&bufmgr->lock);
+
+ size = bufmgr->backend->bo_size(bo);
+
+ pthread_mutex_unlock(&bufmgr->lock);
+
+ return size;
+}
+
+tbm_bo
+tbm_bo_ref (tbm_bo bo)
+{
+ TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), NULL);
+
+ tbm_bufmgr bufmgr = bo->bufmgr;
+
+ pthread_mutex_lock(&bufmgr->lock);
+
+ _tbm_bo_ref (bo);
+
+ pthread_mutex_unlock(&bufmgr->lock);
+
+ return bo;
+}
+
+void
+tbm_bo_unref (tbm_bo bo)
+{
+ TBM_RETURN_IF_FAIL(_tbm_bo_is_valid(bo));
+
+ tbm_bufmgr bufmgr = bo->bufmgr;
+
+ pthread_mutex_lock (&bufmgr->lock);
+
+ _tbm_bo_unref (bo);
+
+ pthread_mutex_unlock(&bufmgr->lock);
+}
+
+tbm_bo
+tbm_bo_alloc (tbm_bufmgr bufmgr, int size, int flags)
+{
+ TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(bufmgr) && (size > 0), NULL);
+
+ tbm_bo bo = NULL;
+ void * bo_priv = NULL;
+
+ bo = calloc (1, sizeof(struct _tbm_bo));
+ if(!bo)
+ return NULL;
+
+ bo->bufmgr = bufmgr;
+
+ pthread_mutex_lock (&bufmgr->lock);
+
+ bo_priv = bufmgr->backend->bo_alloc (bo, size, flags);
+ if (!bo_priv)
+ {
+ free (bo);
+ pthread_mutex_unlock (&bufmgr->lock);
+ return NULL;
+ }
+
+ bo->ref_cnt = 1;
+ bo->flags = flags;
+ bo->tgl_key = INITIAL_KEY;
+ bo->priv = bo_priv;
+
+ /* init bo state */
+ if (!_tbm_bo_init_state (bo, CACHE_OP_CREATE))
+ {
+ _tbm_bo_unref (bo);
+ pthread_mutex_unlock (&bufmgr->lock);
+ return NULL;
+ }
+
+ LIST_INITHEAD (&bo->user_data_list);
+
+ LIST_ADD (&bo->item_link, &bufmgr->bo_list);
+
+ pthread_mutex_unlock(&bufmgr->lock);
+
+ return bo;
+}
+
+tbm_bo
+tbm_bo_import (tbm_bufmgr bufmgr, unsigned int key)
+{
+ TBM_RETURN_VAL_IF_FAIL(TBM_BUFMGR_IS_VALID(bufmgr), NULL);
+
+ tbm_bo bo = NULL;
+ void * bo_priv = NULL;
+
+ bo = calloc (1, sizeof(struct _tbm_bo));
+ if(!bo)
+ return NULL;
+
+ bo->bufmgr = bufmgr;
+
+ pthread_mutex_lock (&bufmgr->lock);
+
+ bo_priv = bufmgr->backend->bo_import (bo, key);
+ if (!bo_priv)
+ {
+ free (bo);
+ pthread_mutex_unlock (&bufmgr->lock);
+ return NULL;
+ }
+
+ bo->ref_cnt = 1;
+ bo->tgl_key = INITIAL_KEY;
+ bo->priv = bo_priv;
+
+ /* init bo state */
+ if (!_tbm_bo_init_state (bo, CACHE_OP_IMPORT))
+ {
+ _tbm_bo_unref (bo);
+ pthread_mutex_unlock (&bufmgr->lock);
+ return NULL;
+ }
+
+ LIST_INITHEAD (&bo->user_data_list);
+
+ LIST_ADD (&bo->item_link, &bufmgr->bo_list);
+
+ pthread_mutex_unlock (&bufmgr->lock);
+
+ return bo;
+}
+
+tbm_bo
+tbm_bo_import_fd (tbm_bufmgr bufmgr, tbm_fd fd)
+{
+ tbm_bo bo = NULL;
+
+ return bo;
+}
+
+unsigned int
+tbm_bo_export (tbm_bo bo)
+{
+ TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), 0);
+
+ tbm_bufmgr bufmgr;
+ int ret;
+
+ bufmgr = bo->bufmgr;
+
+ pthread_mutex_lock (&bufmgr->lock);
+ ret = bufmgr->backend->bo_export (bo);
+ pthread_mutex_unlock (&bufmgr->lock);
+
+ return ret;
+}
+
+tbm_fd
+tbm_bo_export_fd (tbm_bo bo)
+{
+ tbm_fd fd = 0;
+
+ return fd;
+}
+
+
+tbm_bo_handle
+tbm_bo_get_handle (tbm_bo bo, int device)
+{
+ TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), (tbm_bo_handle)0);
+
+ tbm_bufmgr bufmgr;
+ tbm_bo_handle bo_handle;
+
+ bufmgr = bo->bufmgr;
+
+ pthread_mutex_lock (&bufmgr->lock);
+ bo_handle = bufmgr->backend->bo_get_handle (bo, device);
+ pthread_mutex_unlock (&bufmgr->lock);
+
+ return bo_handle;
+}
+
+tbm_bo_handle
+tbm_bo_map (tbm_bo bo, int device, int opt)
+{
+ TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), (tbm_bo_handle)0);
+
+ tbm_bufmgr bufmgr;
+ tbm_bo_handle bo_handle;
+ int ret = 0;
+
+ bufmgr = bo->bufmgr;
+
+ pthread_mutex_lock (&bufmgr->lock);
+
+ bo_handle = bufmgr->backend->bo_get_handle (bo, device);
+
+ ret = _tbm_bo_lock (bo, device, opt);
+ if(!ret)
+ {
+ pthread_mutex_unlock (&bufmgr->lock);
+ TBM_DLOG ("[libtbm:%d] "
+ "error %s:%d LOCK Failed \n",
+ getpid(), __FUNCTION__, __LINE__);
+ return (tbm_bo_handle)NULL;
+
+ }
+
+ bo_handle = bufmgr->backend->bo_map (bo, device, opt);
+ //As tbm_bo_handle is union just checking for NULL in first 32 bits only.
+ if(bo_handle.u32 == 0)
+ {
+ //clean up lock if map fails.
+ _tbm_bo_unlock (bo);
+ pthread_mutex_unlock (&bufmgr->lock);
+ TBM_DLOG ("[libtbm:%d] "
+ "error %s:%d LOCK Failed \n",
+ getpid(), __FUNCTION__, __LINE__);
+ return (tbm_bo_handle)NULL;
+
+ }
+
+ if (bufmgr->use_map_cache == 1 && bo->map_cnt == 0)
+ _tbm_bo_set_state (bo, device, opt);
+
+ /* increase the map_count */
+ bo->map_cnt++;
+
+ pthread_mutex_unlock (&bufmgr->lock);
+
+ return bo_handle;
+}
+
+int
+tbm_bo_unmap (tbm_bo bo)
+{
+ TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), 0);
+
+ tbm_bufmgr bufmgr;
+ int ret;
+
+ bufmgr = bo->bufmgr;
+
+ pthread_mutex_lock (&bufmgr->lock);
+
+
+ ret = bufmgr->backend->bo_unmap (bo);
+
+ /* decrease the map_count */
+ bo->map_cnt--;
+
+ if (bo->map_cnt == 0)
+ _tbm_bo_save_state (bo);
+
+ _tbm_bo_unlock (bo);
+
+ pthread_mutex_unlock (&bufmgr->lock);
+
+ return ret;
+}
+
+int
+tbm_bo_swap (tbm_bo bo1, tbm_bo bo2)
+{
+ TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo1), 0);
+ TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo2), 0);
+
+ void* temp;
+ unsigned int tmp_key;
+
+ if (bo1->bufmgr->backend->bo_size (bo1) != bo2->bufmgr->backend->bo_size (bo2))
+ return 0;
+
+ pthread_mutex_lock (&bo1->bufmgr->lock);
+
+ tmp_key = bo1->tgl_key;
+ bo1->tgl_key = bo2->tgl_key;
+ bo2->tgl_key = tmp_key;
+
+ temp = bo1->priv;
+ bo1->priv = bo2->priv;
+ bo2->priv = temp;
+
+ pthread_mutex_unlock (&bo1->bufmgr->lock);
+
+ return 1;
+}
+
+int
+tbm_bo_locked (tbm_bo bo)
+{
+ TBM_RETURN_VAL_IF_FAIL (_tbm_bo_is_valid(bo), 0);
+
+ tbm_bufmgr bufmgr;
+
+ bufmgr = bo->bufmgr;
+
+ if (bufmgr->lock_type == LOCK_TRY_NEVER)
+ return 0;
+
+ pthread_mutex_lock (&bufmgr->lock);
+
+ if (bo->lock_cnt > 0)
+ {
+ pthread_mutex_unlock (&bufmgr->lock);
+ return 1;
+ }
+
+ pthread_mutex_unlock (&bufmgr->lock);
+
+ return 0;
+}
+
+
+int
+tbm_bo_add_user_data (tbm_bo bo, unsigned long key, tbm_data_free data_free_func)
+{
+ TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
+
+ tbm_user_data *data;
+
+ /* check if the data according to the key exist if so, return false.*/
+ data = _user_data_lookup (&bo->user_data_list, key);
+ if (data)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "waring: %s:%d user data already exist. key:%ld\n",
+ getpid(), __FUNCTION__, __LINE__, key);
+ return 0;
+ }
+
+ data = _user_data_create (key, data_free_func);
+ if (!data)
+ return 0;
+
+ LIST_ADD (&data->item_link, &bo->user_data_list);
+
+ return 1;
+}
+
+int
+tbm_bo_set_user_data (tbm_bo bo, unsigned long key, void* data)
+{
+ TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
+
+ tbm_user_data *old_data;
+
+ if (LIST_IS_EMPTY (&bo->user_data_list))
+ return 0;
+
+ old_data = _user_data_lookup (&bo->user_data_list, key);
+ if (!old_data)
+ return 0;
+
+ if (old_data->data && old_data->free_func)
+ old_data->free_func(old_data->data);
+
+ old_data->data = data;
+
+ return 1;
+}
+
+int
+tbm_bo_get_user_data (tbm_bo bo, unsigned long key, void** data)
+{
+ TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
+
+ tbm_user_data* old_data;
+
+ if (!data || LIST_IS_EMPTY (&bo->user_data_list))
+ return 0;
+
+ old_data = _user_data_lookup (&bo->user_data_list, key);
+ if (!old_data)
+ {
+ *data = NULL;
+ return 0;
+ }
+
+ *data = old_data->data;
+
+ return 1;
+}
+
+int
+tbm_bo_delete_user_data (tbm_bo bo, unsigned long key)
+{
+ TBM_RETURN_VAL_IF_FAIL(_tbm_bo_is_valid(bo), 0);
+
+ tbm_user_data *old_data = (void *)0;
+
+ if (LIST_IS_EMPTY (&bo->user_data_list))
+ return 0;
+
+ old_data = _user_data_lookup (&bo->user_data_list, key);
+ if (!old_data)
+ return 0;
+
+ _user_data_delete (old_data);
+
+ return 1;
+}
+
+int
+tbm_bo_cache_flush(tbm_bo bo, int flags)
+{
+ tbm_bufmgr bufmgr = bo->bufmgr;
+
+ bufmgr->backend->bo_cache_flush (bo, flags);
+
+ RETURN_VAL_CHECK_FLAG (TBM_CACHE_CTRL_BACKEND_VALID(bufmgr->backend->flags), 1);
+
+ unsigned short cntFlush = 0;
+ unsigned int is_locked;
+
+ /* get cache state of a bo */
+ bo->cache_state.val = _tgl_get_data (bufmgr->lock_fd, bo->tgl_key, &is_locked);
+
+ if (!bo->cache_state.data.isCacheable)
+ return 1;
+
+ /* get global cache flush count */
+ cntFlush = (unsigned short)_tgl_get_data (bufmgr->lock_fd, GLOBAL_KEY, NULL);
+
+ bo->cache_state.data.isDirtied = DEVICE_NONE;
+ bo->cache_state.data.isCached = 0;
+
+ /* set global cache flush count */
+ _tgl_set_data (bufmgr->lock_fd, GLOBAL_KEY, (unsigned int)(++cntFlush));
+
+ DBG ("[libtbm:%d] \tcache(%d,%d,%d).... cntFlush(%d)\n", getpid(),
+ bo->cache_state.data.isCacheable,
+ bo->cache_state.data.isCached,
+ bo->cache_state.data.isDirtied,
+ cntFlush);
+
+ return 1;
+}
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#ifndef _TBM_BUFMGR_H_
+#define _TBM_BUFMGR_H_
+
+#include <tbm_type.h>
+#include <stdint.h>
+
+/**
+ * \file tbm_bufmgr.h
+ * \brief Tizen Buffer Manager
+ */
+
+/**
+ * @brief Definition for the tizen buffer manager
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+typedef struct _tbm_bufmgr * tbm_bufmgr;
+
+/**
+ * @brief Definition for the tizen buffer object
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+typedef struct _tbm_bo *tbm_bo;
+/**
+ * @brief Definition for the key associated with the buffer object
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+typedef uint32_t tbm_key;
+/**
+ * @brief Definition for the file descripter of the system buffer manager
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+typedef int32_t tbm_fd;
+
+
+/* TBM_DEVICE_TYPE */
+
+/**
+ * @brief Definition for the device type to get the default handle
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_DEVICE_DEFAULT 0
+/**
+ * @brief Definition for the device type to get the virtual memory
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_DEVICE_CPU 1
+/**
+ * @brief Definition for the device type to get the 2D memory handle
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_DEVICE_2D 2
+/**
+ * @brief Definition for the device type to get the 3D memory handle
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_DEVICE_3D 3
+/**
+ * @brief Definition for the device type to get the multimedia handle
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_DEVICE_MM 4
+
+/**
+ * @brief Definition for the cache invalidate
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_CACHE_INV 0x01
+/**
+ * @brief Definition for the cache clean
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_CACHE_CLN 0x02
+
+/* TBM_OPTION */
+
+/**
+ * @brief Definition for the access option to read
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_OPTION_READ (1 << 0)
+/**
+ * @brief Definition for the access option to write
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_OPTION_WRITE (1 << 1)
+/**
+ * @brief Definition for the vendor specific option that depends on the backend
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_OPTION_VENDOR (0xffff0000)
+
+/**
+ * @brief tbm_bo_handle abstraction of the memory handle by TBM_DEVICE_TYPE
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+typedef union _tbm_bo_handle
+{
+ void *ptr;
+ int32_t s32;
+ uint32_t u32;
+ int64_t s64;
+ uint64_t u64;
+} tbm_bo_handle;
+
+/**
+ * @brief Enumeration of bo memory type
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+enum TBM_BO_FLAGS
+{
+ TBM_BO_DEFAULT = 0, /**< default memory: it depends on the backend */
+ TBM_BO_SCANOUT = (1<<0), /**< scanout memory */
+ TBM_BO_NONCACHABLE = (1<<1), /**< non-cachable memory */
+ TBM_BO_WC = (1<<2), /**< write-combine memory */
+ TBM_BO_VENDOR = (0xffff0000), /**< vendor specific memory: it depends on the backend */
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Functions for buffer manager */
+
+/**
+ * @brief Initializes the buffer manager.
+ * @details If fd is lower than zero, fd is get drm fd in tbm_bufmgr_init function\n
+ * The user can decide the lock type and cache flush type with the environment variables, which are BUFMGR_LOCK_TYPE and BUFMGR_MAP_CACHE.\n
+ * \n
+ * BUFMGR_LOCK default is once\n
+ * once : The previous bo which is locked is unlock when the new bo is trying to be locked\n
+ * always : The new bo is locked until the previous bo which is locked is unlocked\n
+ * never : Every bo is never locked.\n
+ * \n
+ * BUFMGR_MAP_CACHE default is true\n
+ * true : use map cache flushing\n
+ * false : to use map cache flushing
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] fd : file descripter of the system buffer manager
+ * @return a buffer manager
+ * @retval #tbm_bufmgr
+ * @see tbm_bufmgr_deinit();
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+ int bufmgr_fd;
+
+
+ setenv("BUFMGR_LOCK_TYPE", "once", 1);
+ setenv("BUFMGR_MAP_CACHE", "true", 1);
+
+ tbm_bufmgr bufmgr;
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+
+ ....
+
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_bufmgr tbm_bufmgr_init (int fd);
+
+/**
+ * @brief Deinitializes the buffer manager.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bufmgr : the buffer manager
+ * @see tbm_bufmgr_init()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+
+ ....
+
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+void tbm_bufmgr_deinit (tbm_bufmgr bufmgr);
+
+/* Functions for bo */
+
+/**
+ * @brief Allocates the buffer object.
+ * @details This function create tbm_bo and set reference count to 1.\n
+ * The user can craete tbm_bo with memory type flag #TBM_BO_FLAGS\n\n
+ * #TBM_BO_DEFAULT indecates default memory: it depends on the backend\n
+ * #TBM_BO_SCANOUT indecates scanout memory\n
+ * #TBM_BO_NONCACHABLE indecates non-cachable memory\n
+ * #TBM_BO_WC indecates write-combine memory\n
+ * #TBM_BO_VENDOR indecates vendor specific memory: it depends on the tbm backend
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bufmgr : the buffer manager
+ * @param[in] size : the size of buffer object
+ * @param[in] flags : the flags of memory type
+ * @return a buffer object
+ * @retval #tbm_bo
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+
+ ....
+
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_bo tbm_bo_alloc (tbm_bufmgr bufmgr, int size, int flags);
+
+/**
+ * @brief Increases the reference count of bo.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @return a buffer object
+ * @retval #tbm_bo
+ * @see tbm_bo_unref()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+
+ ...
+
+ bo = tbm_bo_ref (bo);
+
+ ....
+
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_bo tbm_bo_ref (tbm_bo bo);
+
+/**
+ * @brief Decreases the reference count of bo
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @see tbm_bo_ref()
+ * @see tbm_bo_alloc()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+void tbm_bo_unref (tbm_bo bo);
+
+/**
+ * @brief Maps the buffer object according to the device type and the option.
+ * @details Cache flushing and Locking is executed, while tbm_bo is mapping in the proper condition according to the device type and the access option.\n
+ * If the cache flush type of bufmgr set true, the map cache flushing is executed
+ * If the lock type of bufmgr set once, the previous bo which is locked is unlock when the new bo is trying to be locked.\n
+ * If the lock type of bufmgr set always, the new bo is locked until the previous bo which is locked is unlocked.\n
+ * If the lock type of bufmgr set never, Every bo is never locked.\n\n
+ * #TBM_DEVICE_DEFAULT indecates the default handle.\n
+ * #TBM_DEVICE_2D indecates the 2D memory handle.\n
+ * #TBM_DEVICE_3D indecates the 3D memory handle.\n
+ * #TBM_DEVICE_CPU indecates the virtual memory handle.\n
+ * #TBM_DEVICE_MM indecates the multimedia handle.\n\n
+ * #TBM_OPTION_READ indecates the accss option to read.\n
+ * #TBM_OPTION_WRITE indecates the access option to write.\n
+ * #TBM_OPTION_VENDOR indecates the vendor specific option that depends on the backend.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @param[in] device : the device type to get a handle
+ * @param[in] opt : the option to access the buffer object
+ * @return the handle of the buffer object
+ * @retval #tbm_bo
+ * @see tbm_bo_unmap()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+ tbm_bo_handle handle;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+
+ ...
+
+ handle = tbm_bo_map (bo, TBM_DEVICE_2D, TBM_OPTION_READ|TBM_OPTION_WRITE);
+
+ ...
+
+ tbm_bo_unmap (bo);
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_bo_handle tbm_bo_map (tbm_bo bo, int device, int opt);
+
+/**
+ * @brief Unmaps the buffer object.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @return 1 if this function succeeds, otherwise 0.
+ * @see tbm_bo_map()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo
+ tbm_bo_handle handle;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+
+ ...
+
+ handle = tbm_bo_map (bo, TBM_DEVICE_2D, TBM_OPTION_READ|TBM_OPTION_WRITE);
+
+ ...
+
+ tbm_bo_unmap (bo);
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+int tbm_bo_unmap (tbm_bo bo);
+
+/**
+ * @brief Gets the tbm_bo_handle according to the device type.
+ * @details The tbm_bo_handle can be get without the map of the tbm_bo.\n
+ * In this case, TBM does not guarantee the lock and the cache flush of the tbm_bo.\n\n
+ * #TBM_DEVICE_DEFAULT indecates the default handle.\n
+ * #TBM_DEVICE_2D indecates the 2D memory handle.\n
+ * #TBM_DEVICE_3D indecates the 3D memory handle.\n
+ * #TBM_DEVICE_CPU indecates the virtual memory handle.\n
+ * #TBM_DEVICE_MM indecates the multimedia handle.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @param[in] device : the device type to get a handle
+ * @return the handle of the buffer object
+ * @retval #tbm_bo_handle
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+ tbm_bo_handle handle;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+
+ ...
+
+ handle = tbm_bo_get_handle (bo, TBM_DEVICE_2D);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_bo_handle tbm_bo_get_handle (tbm_bo bo, int device);
+
+/**
+ * @brief Exports the buffer object by key.
+ * @details The tbm_bo can be exported to the anther process with the unique key associated with the the tbm_bo.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @return key associated with the buffer object
+ * @retval #tbm_key
+ * @see tbm_bo_import()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo;
+ tbm_key key;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+ key = tbm_bo_export (bo);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_key tbm_bo_export (tbm_bo bo);
+
+/**
+ * @brief Exports the buffer object by fd.
+ * @details The tbm_bo can be exported to the anther process with the unique fd associated with the the tbm_bo.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @return fd associated with the buffer object
+ * @retval #tbm_fd
+ * @see tbm_bo_import_fd()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_fd bo_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+ bo_fd = tbm_bo_export (bo);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_fd tbm_bo_export_fd (tbm_bo bo);
+
+/**
+ * @brief Imports the buffer object associated with the key.
+ * @details The reference count of the tbm_bo is 1.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bufmgr : the buffer manager
+ * @param[in] key : the key associated with the buffer object
+ * @return a buffer object
+ * @retval #tbm_bo
+ * @see tbm_bo_export()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ int bo_key;
+ tbm_bufmgr bufmgr;
+ tbm_bo;
+
+ ...
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_import (key);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_bo tbm_bo_import (tbm_bufmgr bufmgr, tbm_key key);
+
+/**
+ * @brief Imports the buffer object associated with the fd.
+ * @details The reference count of the tbm_bo is 1.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bufmgr : the buffer manager
+ * @param[in] fd : the fd associated with the buffer object
+ * @return a buffer object
+ * @retval #tbm_bo
+ * @see tbm_bo_export_fd()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_fd bo_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+
+ ...
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_import (bo_fd);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_bo tbm_bo_import_fd (tbm_bufmgr bufmgr, tbm_fd fd);
+
+/**
+ * @brief Gets the size of a bo.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @return 1 if this function succeeds, otherwise 0.
+ * @see tbm_bo_alloc()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo;
+ int size;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+ size = tbm_bo_size (bo);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+int tbm_bo_size (tbm_bo bo);
+
+/**
+ * @brief Gets the state where the buffer object is locked.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @return 1 if this bo is locked, otherwise 0.
+ * @see tbm_bo_map()
+ * @see tbm_bo_unmap()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+
+ ...
+
+ if (tbm_bo_locked (bo))
+ {
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+*/
+int tbm_bo_locked (tbm_bo bo);
+
+/**
+ * @brief Swaps the buffer object.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo1 : the buffer object
+ * @param[in] bo2 : the buffer object
+ * @return 1 if this function succeeds, otherwise 0.
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo1;
+ tbm_bo bo2;
+ int ret;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo1 = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+ bo2 = tbm_bo_alloc (bufmgr, 256 * 256, TBM_BO_DEFAULT);
+
+ ...
+
+ ret = tbm_bo_swap (bo1, bo2);
+
+ ...
+
+ tbm_bo_unref (bo1);
+ tbm_bo_unref (bo2);
+ tbm_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+int tbm_bo_swap (tbm_bo bo1, tbm_bo bo2);
+
+
+/**
+ * @brief Called when the user data is deleted in buffer object.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] user_data User_data to be passed to callback function
+ * @pre The callback must be registered using tbm_bo_add_user_data().\n
+ * tbm_bo_delete_user_data() must be called to invoke this callback.
+ * @see tbm_bo_add_user_data()
+ * @see tbm_bo_delete_user_data()
+ */
+typedef void (*tbm_data_free)(void *user_data);
+
+/**
+ * @brief Adds a user_data to the buffer object.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @param[in] key : the key associated with the user_data
+ * @param[in] data_free_func : the function pointer to free the user_data
+ * @return 1 if this function succeeds, otherwise 0.
+ * @post tbm_data_free() will be called under certain conditions, after calling tbm_bo_delete_user_data().
+ * @see tbm_data_free()
+ * @see tbm_bo_set_user_data()
+ * @see tbm_bo_get_user_data()
+ * @see tbm_bo_delete_user_data()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ void example_data_free (void *user_data)
+ {
+ char *data = (char*) user_data;
+ free(data);
+ }
+
+ int main()
+ {
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+ char *user_data;
+ char *get_data;
+ int ret;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+ user_data = (char*) malloc (sizeof(char) * 128);
+
+ ...
+
+ tbm_bo_add_user_data (bo, 1, example_data_free);
+ tbm_bo_set_user_data (bo, 1, user_data);
+
+ ...
+
+ ret = tbm_bo_get_user_data (bo, 1, &get_data);
+ tbm_bo_delete_user_data (bo, 1);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ }
+ @endcode
+ */
+
+int tbm_bo_add_user_data (tbm_bo bo, unsigned long key, tbm_data_free data_free_func);
+
+/**
+ * @brief Deletes the user_data in the buffer object.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @param[in] key : the key associated with the user_date
+ * @return 1 if this function succeeds, otherwise 0.
+ * @see tbm_bo_add_user_data()
+ * @see tbm_bo_get_user_data()
+ * @see tbm_bo_delete_user_data()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ void example_data_free (void *user_data)
+ {
+ char *data = (char*) user_data;
+ free(data);
+ }
+
+ int main()
+ {
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+ char *user_data;
+ char *get_data;
+ int ret;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+ user_data = (char*) malloc (sizeof(char) * 128);
+
+ ...
+
+ tbm_bo_add_user_data (bo, 1, example_data_free);
+ tbm_bo_set_user_data (bo, 1, user_data);
+
+ ...
+
+ ret = tbm_bo_get_user_data (bo, 1, &get_data);
+ tbm_bo_delete_user_data (bo, 1);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ }
+ @endcode
+ */
+int tbm_bo_delete_user_data (tbm_bo bo, unsigned long key);
+
+/**
+ * @brief Sets a user_date to the buffer object.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @param[in] key : the key associated with the user_date
+ * @param[in] data : a pointer of the user_data
+ * @return 1 if this function succeeds, otherwise 0.
+ * @see tbm_bo_add_user_data()
+ * @see tbm_bo_set_user_data()
+ * @see tbm_bo_delete_user_data()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ void example_data_free (void *user_data)
+ {
+ char *data = (char*) user_data;
+ free(data);
+ }
+
+ int main()
+ {
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+ char *user_data;
+ char *get_data;
+ int ret;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+ user_data = (char*) malloc (sizeof(char) * 128);
+
+ ...
+
+ tbm_bo_add_user_data (bo, 1, example_data_free);
+ tbm_bo_set_user_data (bo, 1, user_data);
+
+ ...
+
+ ret = tbm_bo_get_user_data (bo, 1, &get_data);
+ tbm_bo_delete_user_data (bo, 1);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ }
+ @endcode
+ */
+int tbm_bo_set_user_data (tbm_bo bo, unsigned long key, void* data);
+
+/**
+ * @brief Gets a user_data from the buffer object with the key.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bo : the buffer object
+ * @param[in] key : the key associated with the user_date
+ * @param[out] data : to get the user data
+ * @return 1 if this function succeeds, otherwise 0.
+ * @see tbm_bo_add_user_data()
+ * @see tbm_bo_set_user_data()
+ * @see tbm_bo_get_user_data()
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+
+ void example_data_free (void *user_data)
+ {
+ char *data = (char*) user_data;
+ free(data);
+ }
+
+ int main()
+ {
+ int bufmgr_fd;
+ tbm_bufmgr bufmgr;
+ tbm_bo bo;
+ char *user_data;
+ char *get_data;
+ int ret;
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+ user_data = (char*) malloc (sizeof(char) * 128);
+
+ ...
+
+ tbm_bo_add_user_data (bo, 1, example_data_free);
+ tbm_bo_set_user_data (bo, 1, user_data);
+
+ ...
+
+ ret = tbm_bo_get_user_data (bo, 1, &get_data);
+ tbm_bo_delete_user_data (bo, 1);
+
+ ...
+
+ tbm_bo_unref (bo);
+ tbm_bufmgr_deinit (bufmgr);
+ }
+ @endcode
+ */
+int tbm_bo_get_user_data (tbm_bo bo, unsigned long key, void** data);
+
+int tbm_bo_cache_flush (tbm_bo bo, int flags);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _TBM_BUFMGR_H_ */
+
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#include "config.h"
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include "tbm_bufmgr_int.h"
+
+tbm_bufmgr_backend
+tbm_backend_alloc (void)
+{
+ tbm_bufmgr_backend bufmgr_backend;
+
+ bufmgr_backend = calloc (1, sizeof(struct _tbm_bufmgr_backend));
+ if (!bufmgr_backend)
+ return NULL;
+
+ return bufmgr_backend;
+}
+
+void
+tbm_backend_free (tbm_bufmgr_backend backend)
+{
+ if (!backend)
+ return;
+
+ free (backend);
+ backend = NULL;
+}
+
+int
+tbm_backend_init (tbm_bufmgr bufmgr, tbm_bufmgr_backend backend)
+{
+ int flags = 0;
+
+ if (!bufmgr)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error (%s): fail to init tbm backend... bufmgr is null\n",
+ getpid(), __FUNCTION__);
+ return 0;
+ }
+
+ if (!backend)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error (%s): fail to init tbm backend... backend is null\n",
+ getpid(), __FUNCTION__);
+ return 0;
+ }
+
+ flags = backend->flags;
+ /* check the backend flags */
+ if (!(flags&TBM_CACHE_CTRL_BACKEND))
+ {
+ if (!backend->bo_cache_flush)
+ {
+ TBM_LOG ("[libtbm:%d] "
+ "error (%s): TBM_FLAG_CACHE_CTRL_TBM needs backend->bo_cache_flush\n",
+ getpid(), __FUNCTION__);
+ return 0;
+ }
+ }
+
+ /* log for tbm flags */
+ TBM_LOG ("[libtbm:%d] ", getpid());
+ TBM_LOG ("cache_crtl:");
+ if (flags&TBM_CACHE_CTRL_BACKEND)
+ TBM_LOG ("BACKEND ");
+ else
+ TBM_LOG ("TBM ");
+ TBM_LOG ("lock_crtl:");
+ if (flags&TBM_LOCK_CTRL_BACKEND)
+ TBM_LOG ("BACKEND ");
+ else
+ TBM_LOG ("TBM ");
+ TBM_LOG ("\n");
+
+ bufmgr->backend = backend;
+
+ return 1;
+}
+
+void *
+tbm_backend_get_bufmgr_priv (tbm_bo bo)
+{
+ tbm_bufmgr_backend backend = bo->bufmgr->backend;
+
+ return backend->priv;
+}
+
+void
+tbm_backend_set_bo_priv (tbm_bo bo, void *bo_priv)
+{
+ bo->priv = bo_priv;
+}
+
+void *
+tbm_backend_get_bo_priv (tbm_bo bo)
+{
+ return bo->priv;
+}
+
+
+
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+/*
+ * This file is referenced by the xf86Module.h in xorg server.
+ */
+
+#ifndef _TBM_BUFMGR_BACKEND_H_
+#define _TBM_BUFMGR_BACKEND_H_
+
+#include <tbm_bufmgr.h>
+#include <pthread.h>
+
+/**
+ * \file tbm_bufmgr_backend.h
+ * \brief backend header for Tizen Buffer Manager
+ * This header is for the implementation of the TBM backend module.
+ */
+
+
+#define ABI_MINOR_MASK 0x0000FFFF
+#define ABI_MAJOR_MASK 0xFFFF0000
+#define GET_ABI_MINOR(v) ((v) & ABI_MINOR_MASK)
+#define GET_ABI_MAJOR(v) (((v) & ABI_MAJOR_MASK) >> 16)
+
+/*
+ * ABI versions. Each version has a major and minor revision. Modules
+ * using lower minor revisions must work with servers of a higher minor
+ * revision. There is no compatibility between different major revisions.
+ * Whenever the ABI_ANSIC_VERSION is changed, the others must also be
+ * changed. The minor revision mask is 0x0000FFFF and the major revision
+ * mask is 0xFFFF0000.
+ */
+#define SET_ABI_VERSION(maj, min) \
+ ((((maj) << 16) & ABI_MAJOR_MASK) | ((min) & ABI_MINOR_MASK))
+
+#define TBM_ABI_VERSION SET_ABI_VERSION(1, 1) /**< current abi vertion */
+
+/* TBM_CACHE */
+#define TBM_CACHE_INV 0x01 /**< cache invalidate */
+#define TBM_CACHE_CLN 0x02 /**< cache clean */
+#define TBM_CACHE_ALL 0x10 /**< cache all */
+#define TBM_CACHE_FLUSH (TBM_CACHE_INV|TBM_CACHE_CLN) /**< cache flush */
+#define TBM_CACHE_FLUSH_ALL (TBM_CACHE_FLUSH|TBM_CACHE_ALL) /**< cache flush all */
+
+/* TBM flag for cache control and lock control */
+/**
+ * TBM_CACHE_CTRL_BACKEND indicates that the backend control the cache coherency.
+ */
+#define TBM_CACHE_CTRL_BACKEND (1 << 0)
+
+/**
+ * TBM_LOCK_CTRL_BACKEND indicates that the backend control the lock of bos.
+ */
+#define TBM_LOCK_CTRL_BACKEND (1 << 1)
+
+typedef struct _tbm_bufmgr_backend *tbm_bufmgr_backend;
+
+/**
+ * @brief TBM backend functions
+ * the set of function pointers for the backend module of TBM.
+ */
+struct _tbm_bufmgr_backend
+{
+ int flags;
+
+ void *priv; /**< bufmgr private */
+
+ /**
+ * @brief deinitialize the bufmgr private.
+ * @param[in] bufmgr : the private of the bufmgr
+ * @return 1 if this function succeeds, otherwise 0.
+ */
+ void (*bufmgr_deinit) (void *priv);
+
+ /**
+ * @brief get the size of a bo.
+ * @param[in] bo : the buffer object
+ * @return 1 if this function succeeds, otherwise 0.
+ */
+ int (*bo_size) (tbm_bo bo);
+
+ /**
+ * @brief allocate the buffer object
+ * @param[in] bo : the buffer object
+ * @param[in] size : the size of buffer object
+ * @param[in] flags : the flags of memory type
+ * @return pointer of the bo private.
+ */
+ void * (*bo_alloc) (tbm_bo bo, int size, int flags);
+
+ /**
+ * @brief free the buffer object.
+ * @param[in] bo : the buffer object
+ */
+ void (*bo_free) (tbm_bo bo);
+
+ /**
+ * @brief import the buffer object associated with the key.
+ * @param[in] bo : the buffer object
+ * @param[in] key : the key associated with the buffer object
+ * @return pointer of the bo private.
+ */
+ void * (*bo_import) (tbm_bo bo, unsigned int key);
+
+ /**
+ * @brief export the buffer object
+ * @param[in] bo : the buffer object
+ * @return key associated with the buffer object
+ */
+ unsigned int (*bo_export) (tbm_bo bo);
+
+ /**
+ * @brief get the tbm_bo_handle according to the device type.
+ * @param[in] bo : the buffer object
+ * @param[in] device : the device type to get a handle
+ * @return the handle of the buffer object
+ */
+ tbm_bo_handle (*bo_get_handle) (tbm_bo bo, int device);
+
+ /**
+ * @brief map the buffer object according to the device type and the option.
+ * @param[in] bo : the buffer object
+ * @param[in] device : the device type to get a handle
+ * @param[in] option : the option to access the buffer object
+ * @return the handle of the buffer object
+ */
+ tbm_bo_handle (*bo_map) (tbm_bo bo, int device, int opt);
+
+ /**
+ * @brief unmap the buffer object.
+ * @param[in] bo : the buffer object
+ * @return 1 if this function succeeds, otherwise 0.
+ */
+ int (*bo_unmap) (tbm_bo bo);
+
+ /**
+ * @brief flush the cache of the buffer object.
+ * @param[in] bo : the buffer object
+ * @param[in] flags : the flags of cache flush type
+ * @return 1 if this function succeeds, otherwise 0.
+ */
+ int (*bo_cache_flush) (tbm_bo bo, int flags);
+
+ /**
+ * @brief get the global key associated with the buffer object.
+ * @param[in] bo : the buffer object
+ * @return global key associated with the buffer object.
+ */
+ int (*bo_get_global_key) (tbm_bo bo);
+
+ /**
+ * @brief lock the buffer object.
+ * @param[in] bo : the buffer object
+ * @return 1 if this function succeeds, otherwise 0.
+ * @remark This function pointer could be null. (default: use the tizen global lock)
+ */
+ int (*bo_lock) (tbm_bo bo);
+
+ /**
+ * @brief unlock the buffer object.
+ * @param[in] bo : the buffer object
+ * @return 1 if this function succeeds, otherwise 0.
+ * @remark This function pointer could be null. (default: use the tizen global lock)
+ */
+ int (*bo_unlock) (tbm_bo bo);
+
+ /**
+ * @brief lock the buffer object with a device and an opt.
+ * @param[in] bo : the buffer object
+ * @param[in] device : the device type to get a handle
+ * @param[in] option : the option to access the buffer object
+ * @return 1 if this function succeeds, otherwise 0.
+ * @remark This function pointer could be null. (default: use the tizen global lock)
+ */
+ int (*bo_lock2) (tbm_bo bo, int device, int opt);
+
+ /**
+ * @brief query the formats list and the num to be supported by backend.
+ * @param[out] *formats : format array list. this array has to be allocated by backend funtion
+ * @param[out] *num : the number of the formats to be supported by backend
+ * @return 1 if this function succeeds, otherwise 0.
+ */
+ int (*surface_supported_format) (uint32_t **formats, uint32_t *num);
+
+ /**
+ * @brief get the size of the surface with a format.
+ * @param[in] surface : the surface
+ * @param[in] width : the width of the surface
+ * @param[in] height : the height of the surface
+ * @param[in] format : the format of the surface
+ * @return size of the surface if this function succeeds, otherwise 0.
+ */
+ int (*surface_get_size) (tbm_surface_h surface, int width, int height, tbm_format format);
+
+ /**
+ * @brief get the plane data of the surface.
+ * @param[in] surface : the surface
+ * @param[in] width : the width of the surface
+ * @param[in] height : the height of the surface
+ * @param[in] format : the format of the surface
+ * @param[in] plane_idx : the format of the surface
+ * @param[out] size : the size of the plane
+ * @param[out] offset : the offset of the plane
+ * @param[out] pitch : the pitch of the plane
+ * @return 1 if this function succeeds, otherwise 0.
+ */
+ int (*surface_get_plane_data) (tbm_surface_h surface, int width, int height, tbm_format format, int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch);
+
+ /* Padding for future extension */
+ void (*reserved1) (void);
+ void (*reserved2) (void);
+ void (*reserved3) (void);
+ void (*reserved4) (void);
+ void (*reserved5) (void);
+ void (*reserved6) (void);
+ void (*reserved7) (void);
+ void (*reserved8) (void);
+};
+
+/**
+ * @brief tbm module information
+ * data type for the module information
+ */
+typedef struct
+{
+ const char *modname; /**< name of module, e.g. "foo" */
+ const char *vendor; /**< vendor specific string */
+ unsigned long abiversion; /**< ABI version */
+} TBMModuleVersionInfo;
+
+typedef int (*ModuleInitProc) (tbm_bufmgr, int);
+
+#define MODULEINITPPROTO(func) int func(tbm_bufmgr, int) /**< prototype for init symbol of bakcend module */
+
+/**
+ * @brief tbm module data
+ * data type for the entry point of the backend module
+ */
+typedef struct
+{
+ TBMModuleVersionInfo *vers; /**< tbm module informtaion */
+ ModuleInitProc init; /**< init function of a backend module */
+} TBMModuleData;
+
+tbm_bufmgr_backend tbm_backend_alloc (void);
+void tbm_backend_free (tbm_bufmgr_backend backend);
+int tbm_backend_init (tbm_bufmgr bufmgr, tbm_bufmgr_backend backend);
+
+void *tbm_backend_get_bufmgr_priv (tbm_bo bo);
+void *tbm_backend_get_bo_priv (tbm_bo bo);
+
+#endif /* _TBM_BUFMGR_BACKEND_H_ */
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#ifndef _TBM_BUFMGR_INT_H_
+#define _TBM_BUFMGR_INT_H_
+
+#include <unistd.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <dlfcn.h>
+#include <dirent.h>
+#include <string.h>
+#include <errno.h>
+#include <pthread.h>
+#include <tbm_bufmgr.h>
+#include <tbm_surface.h>
+#include <tbm_bufmgr_backend.h>
+
+#include </usr/include/dlog/dlog.h>
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "TBM"
+
+/* check condition */
+#define TBM_RETURN_IF_FAIL(cond) {\
+ if (!(cond)) {\
+ TBM_DLOG ("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
+ return;\
+ }\
+}
+#define TBM_RETURN_VAL_IF_FAIL(cond, val) {\
+ if (!(cond)) {\
+ TBM_DLOG ("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
+ return val;\
+ }\
+}
+
+/* check flags */
+#define RETURN_CHECK_FLAG(cond) {\
+ if ((cond)) {\
+ return;\
+ }\
+}
+#define RETURN_VAL_CHECK_FLAG(cond, val) {\
+ if ((cond)) {\
+ return val;\
+ }\
+}
+
+
+/* check validation */
+#define TBM_BUFMGR_IS_VALID(mgr) (mgr)
+#define TBM_BO_IS_VALID(bo) (bo && \
+ TBM_BUFMGR_IS_VALID(bo->bufmgr) && \
+ bo->item_link.next && \
+ bo->item_link.next->prev == &bo->item_link)
+#define TBM_SURFACE_IS_VALID(surf) (surf && \
+ TBM_BUFMGR_IS_VALID(surf->bufmgr) && \
+ surf->item_link.next && \
+ surf->item_link.next->prev == &surf->item_link)
+
+#define TBM_ALL_CTRL_BACKEND_VALID(flags) \
+ ((flags&TBM_CACHE_CTRL_BACKEND) &&\
+ (flags&TBM_LOCK_CTRL_BACKEND))
+#define TBM_CACHE_CTRL_BACKEND_VALID(flags) \
+ (flags&TBM_CACHE_CTRL_BACKEND)
+#define TBM_LOCK_CTRL_BACKEND_VALID(flags) \
+ (flags&TBM_LOCK_CTRL_BACKEND)
+
+#define TBM_LOG(...) fprintf (stderr, __VA_ARGS__)
+#define TBM_DLOG(...) LOGE (__VA_ARGS__)
+
+
+typedef union _tbm_bo_cache_state tbm_bo_cache_state;
+
+struct list_head
+{
+ struct list_head *prev;
+ struct list_head *next;
+};
+
+union _tbm_bo_cache_state
+{
+ unsigned int val;
+ struct {
+ unsigned int cntFlush:16; /*Flush all index for sync*/
+ unsigned int isCacheable:1;
+ unsigned int isCached:1;
+ unsigned int isDirtied:2;
+ } data;
+};
+
+/**
+ * @brief tbm_bo : buffer object of Tizen Buffer Manager
+ */
+struct _tbm_bo
+{
+ tbm_bufmgr bufmgr; /* tbm buffer manager */
+
+ int ref_cnt; /* ref count of bo */
+
+ int flags; /* TBM_BO_FLAGS :bo memory type */
+
+ unsigned int tgl_key; /*global key for tizen global lock */
+
+ /* for cache control */
+ unsigned int map_cnt; /* device map count */
+ tbm_bo_cache_state cache_state; /*cache state */
+
+ int lock_cnt; /* lock count of bo */
+
+ struct list_head user_data_list; /* list of the user_date in bo */
+
+ void *priv; /* bo private */
+
+ struct list_head item_link; /* link of bo */
+};
+
+/**
+ * @brief tbm_bufmgr : structure for tizen buffer manager
+ *
+ */
+struct _tbm_bufmgr
+{
+ pthread_mutex_t lock; /* mutex lock */
+
+ int ref_count; /*reference count */
+
+ int fd; /* bufmgr fd */
+
+ int fd_flag; /* flag set 1 when bufmgr fd open in tbm_bufmgr_init*/
+
+ int lock_fd; /* fd of tizen global lock */
+
+ int lock_type; /* lock_type of bufmgr */
+
+ int use_map_cache; /* flag to use the map_cahce */
+
+ struct list_head bo_list; /* list of bos belonging to bufmgr */
+
+ struct list_head surf_list; /* list of surfaces belonging to bufmgr */
+
+ void *module_data;
+
+ tbm_bufmgr_backend backend; /* bufmgr backend */
+};
+
+/**
+ * @brief tbm_surface : structure for tizen buffer surface
+ *
+ */
+struct _tbm_surface {
+ tbm_bufmgr bufmgr; /* tbm buffer manager */
+
+ tbm_surface_info_s info; /* tbm surface information */
+
+ int flags;
+
+ int num_bos; /* the number of buffer objects */
+ tbm_bo bos[4]; /* the array of buffer objects */
+
+ struct list_head item_link; /* link of surface */
+};
+
+#endif /* _TBM_BUFMGR_INT_H_ */
--- /dev/null
+/**************************************************************************
+ *
+ * libtbm
+ *
+ * Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+ * Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * **************************************************************************/
+
+#ifndef __TBM_BUFMGR_TGL_H__
+#define __TBM_BUFMGR_TGL_H__
+
+#include <linux/ioctl.h>
+
+static char tgl_devfile[] = "/dev/slp_global_lock";
+static char tgl_devfile1[] = "/dev/tgl";
+
+#define TGL_IOC_BASE 0x32
+
+struct tgl_attribute {
+ unsigned int key;
+ unsigned int timeout_ms;
+};
+
+struct tgl_user_data {
+ unsigned int key;
+ unsigned int data1;
+ unsigned int data2;
+ unsigned int locked;
+};
+
+typedef enum {
+ _TGL_INIT_LOCK = 1,
+ _TGL_DESTROY_LOCK,
+ _TGL_LOCK_LOCK,
+ _TGL_UNLOCK_LOCK,
+ _TGL_SET_DATA,
+ _TGL_GET_DATA,
+} _tgl_ioctls;
+
+#define TGL_IOC_INIT_LOCK _IOW(TGL_IOC_BASE, _TGL_INIT_LOCK, struct tgl_attribute *)
+#define TGL_IOC_DESTROY_LOCK _IOW(TGL_IOC_BASE, _TGL_DESTROY_LOCK, unsigned int)
+#define TGL_IOC_LOCK_LOCK _IOW(TGL_IOC_BASE, _TGL_LOCK_LOCK, unsigned int)
+#define TGL_IOC_UNLOCK_LOCK _IOW(TGL_IOC_BASE, _TGL_UNLOCK_LOCK, unsigned int)
+#define TGL_IOC_SET_DATA _IOW(TGL_IOC_BASE, _TGL_SET_DATA, struct tgl_user_data *)
+#define TGL_IOC_GET_DATA _IOW(TGL_IOC_BASE, _TGL_GET_DATA, struct tgl_user_data *)
+
+#endif /* __TBM_BUFMGR_TGL_H__ */
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#include "config.h"
+#include "tbm_bufmgr.h"
+#include "tbm_bufmgr_int.h"
+#include "tbm_surface_internal.h"
+
+static int
+_tbm_surface_get_info (struct _tbm_surface *surf, int opt, tbm_surface_info_s *info, int map)
+{
+ tbm_bo_handle bo_handles[4];
+ int i;
+
+ info->width = surf->info.width;
+ info->height = surf->info.height;
+ info->format = surf->info.format;
+ info->bpp = surf->info.bpp;
+ info->size = surf->info.size;
+ info->num_planes = surf->info.num_planes;
+
+ if (surf->num_bos == 1)
+ {
+ if (map == 1)
+ {
+ bo_handles[0] = tbm_bo_map (surf->bos[0], TBM_DEVICE_CPU, opt);
+ if (bo_handles[0].ptr == NULL)
+ return 0;
+ }
+ else
+ {
+ bo_handles[0] = tbm_bo_get_handle (surf->bos[0], TBM_DEVICE_CPU);
+ if (bo_handles[0].ptr == NULL)
+ return 0;
+ }
+
+ for (i = 0; i < surf->info.num_planes; i++)
+ {
+ info->planes[i].size = surf->info.planes[i].size;
+ info->planes[i].offset = surf->info.planes[i].offset;
+ info->planes[i].stride = surf->info.planes[i].stride;
+ info->planes[i].ptr = bo_handles[0].ptr + surf->info.planes[i].offset;
+ }
+ }
+ else
+ {
+ /* TODO: calculate the virtaul address when num_bos is over 1 */
+ }
+
+ return 1;
+}
+
+int
+tbm_surface_query_formats (uint32_t **formats, uint32_t *num)
+{
+ if (!tbm_surface_internal_query_supported_formats (formats, num))
+ return TBM_SURFACE_ERROR_INVALID_OPERATION;
+
+ return TBM_SURFACE_ERROR_NONE;
+}
+
+tbm_surface_h
+tbm_surface_create (int width, int height, tbm_format format)
+{
+ if (!(width > 0) || !(height > 0))
+ {
+#ifdef HAVE_CAPI_0_1_1
+ set_last_result (TBM_SURFACE_ERROR_INVALID_PARAMETER);
+#endif
+ return NULL;
+ }
+
+ struct _tbm_surface *surf = NULL;
+
+ surf = tbm_surface_internal_create_with_flags (width, height, format, TBM_BO_DEFAULT);
+ if (!surf)
+ {
+#ifdef HAVE_CAPI_0_1_1
+ set_last_result (TBM_SURFACE_ERROR_INVALID_OPERATION);
+#endif
+ return NULL;
+ }
+
+#ifdef HAVE_CAPI_0_1_1
+ set_last_result (TBM_SURFACE_ERROR_NONE);
+#endif
+ return surf;
+}
+
+
+int
+tbm_surface_destroy (tbm_surface_h surface)
+{
+ if (!surface)
+ return TBM_SURFACE_ERROR_INVALID_PARAMETER;
+
+ tbm_surface_internal_destroy (surface);
+
+ return TBM_SURFACE_ERROR_NONE;
+}
+
+int
+tbm_surface_map (tbm_surface_h surface, int opt, tbm_surface_info_s *info)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
+ TBM_RETURN_VAL_IF_FAIL (info != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *)surface;
+ int ret = 0;
+
+ ret = _tbm_surface_get_info (surf, opt, info, 1);
+ if (ret == 0)
+ return TBM_SURFACE_ERROR_INVALID_OPERATION;
+
+ return TBM_SURFACE_ERROR_NONE;
+}
+
+int
+tbm_surface_unmap (tbm_surface_h surface)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *)surface;
+ int i;
+
+ for (i = 0; i < surf->num_bos; i++)
+ tbm_bo_unmap (surf->bos[i]);
+
+ return TBM_SURFACE_ERROR_NONE;
+}
+
+int
+tbm_surface_get_info (tbm_surface_h surface, tbm_surface_info_s *info)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
+ TBM_RETURN_VAL_IF_FAIL (info != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *)surface;
+ int ret = 0;
+
+ ret = _tbm_surface_get_info (surf, 0, info, 0);
+ if (ret == 0)
+ return TBM_SURFACE_ERROR_INVALID_OPERATION;
+
+ return TBM_SURFACE_ERROR_NONE;
+}
+
+int
+tbm_surface_get_width (tbm_surface_h surface)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *)surface;
+
+ return surf->info.width;
+}
+
+int
+tbm_surface_get_height (tbm_surface_h surface)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface != NULL, TBM_SURFACE_ERROR_INVALID_PARAMETER);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *)surface;
+
+ return surf->info.height;
+}
+
+tbm_format
+tbm_surface_get_format (tbm_surface_h surface)
+{
+ if (!surface)
+ {
+#ifdef HAVE_CAPI_0_1_1
+ set_last_result (TBM_SURFACE_ERROR_INVALID_PARAMETER);
+#endif
+ return 0;
+ }
+
+ struct _tbm_surface *surf = (struct _tbm_surface *)surface;
+
+#ifdef HAVE_CAPI_0_1_1
+ set_last_result (TBM_SURFACE_ERROR_NONE);
+#endif
+ return surf->info.format;
+}
+
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Inpyo Kang <mantiger@samsung.com>, Dongyeon Kim <dy5.kim@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#ifndef _TBM_SURFACE_H_
+#define _TBM_SURFACE_H_
+
+/**
+ * @addtogroup CAPI_UI_TBM_SURFACE_MODULE
+ * @{
+ */
+
+#include <tbm_type.h>
+#include <tizen.h>
+
+/**
+ * \file tbm_surface.h
+ * \brief TBM Surface
+ */
+
+/**
+ * @brief Enumeration for tbm_surface error type.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+typedef enum
+{
+ TBM_SURFACE_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
+ TBM_SURFACE_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
+ TBM_SURFACE_ERROR_INVALID_OPERATION = TIZEN_ERROR_INVALID_OPERATION, /**< Invalid Operation */
+} tbm_surface_error_e;
+
+/**
+ * @brief Definition for the max number of TBM surface plane.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_SURF_PLANE_MAX 4
+
+/* option to map the tbm_surface */
+/**
+ * @brief Definition for the access option to read.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_SURF_OPTION_READ (1 << 0)
+/**
+ * @brief Definition for the access option to write.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_SURF_OPTION_WRITE (1 << 1)
+
+/**
+ * @brief Definition for the TBM plane struct.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+typedef struct _tbm_surface_plane
+{
+ unsigned char *ptr; /**< Plane pointer */
+ uint32_t size; /**< Plane size */
+ uint32_t offset; /**< Plane offset */
+ uint32_t stride; /**< Plane stride */
+
+ void *reserved1; /**< Reserved pointer1 */
+ void *reserved2; /**< Reserved pointer2 */
+ void *reserved3; /**< Reserved pointer3 */
+} tbm_surface_plane_s;
+
+/**
+ * @brief Definition for the TBM surface information struct.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+typedef struct _tbm_surface_info
+{
+ uint32_t width; /**< TBM surface width */
+ uint32_t height; /**< TBM surface height */
+ tbm_format format; /**< TBM surface format*/
+ uint32_t bpp; /**< TBM surface bbp */
+ uint32_t size; /**< TBM surface size */
+
+ uint32_t num_planes; /**< The number of planes */
+ tbm_surface_plane_s planes[TBM_SURF_PLANE_MAX]; /**< Array of planes */
+
+ void *reserved4; /**< Reserved pointer4 */
+ void *reserved5; /**< Reserved pointer5 */
+ void *reserved6; /**< Reserved pointer6 */
+} tbm_surface_info_s;
+
+#define __tbm_fourcc_code(a,b,c,d) ((uint32_t)(a) | ((uint32_t)(b) << 8) | \
+ ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
+
+/* color index */
+/**
+ * @brief Definition for the TBM surface format C8 ([7:0] C).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_C8 __tbm_fourcc_code('C', '8', ' ', ' ')
+
+/* 8 bpp RGB */
+/**
+ * @brief Definition for the TBM surface format RGB322 ([7:0] R:G:B 3:3:2).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGB332 __tbm_fourcc_code('R', 'G', 'B', '8')
+/**
+ * @brief Definition for the TBM surface format RGB233 ([7:0] B:G:R 2:3:3).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGR233 __tbm_fourcc_code('B', 'G', 'R', '8')
+
+/* 16 bpp RGB */
+/**
+ * @brief Definition for the TBM surface format XRGB4444 ([15:0] x:R:G:B 4:4:4:4 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_XRGB4444 __tbm_fourcc_code('X', 'R', '1', '2')
+/**
+ * @brief Definition for the TBM surface format XBRG4444 ([15:0] x:B:G:R 4:4:4:4 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_XBGR4444 __tbm_fourcc_code('X', 'B', '1', '2')
+/**
+ * @brief Definition for the TBM surface format RGBX4444 ([15:0] R:G:B:x 4:4:4:4 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGBX4444 __tbm_fourcc_code('R', 'X', '1', '2')
+/**
+ * @brief Definition for the TBM surface format BGRX4444 ([15:0] B:G:R:x 4:4:4:4 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGRX4444 __tbm_fourcc_code('B', 'X', '1', '2')
+
+/**
+ * @brief Definition for the TBM surface format ARGB4444 ([15:0] A:R:G:B 4:4:4:4 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_ARGB4444 __tbm_fourcc_code('A', 'R', '1', '2')
+/**
+ * @brief Definition for the TBM surface format ABGR4444 ([15:0] A:B:G:R 4:4:4:4 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_ABGR4444 __tbm_fourcc_code('A', 'B', '1', '2')
+/**
+ * @brief Definition for the TBM surface format RGBA4444 ([15:0] R:G:B:A 4:4:4:4 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGBA4444 __tbm_fourcc_code('R', 'A', '1', '2')
+/**
+ * @brief Definition for the TBM surface format BGRA4444 ([15:0] B:G:R:A 4:4:4:4 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGRA4444 __tbm_fourcc_code('B', 'A', '1', '2')
+
+/**
+ * @brief Definition for the TBM surface format XRGB1555 ([15:0] x:R:G:B 1:5:5:5 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_XRGB1555 __tbm_fourcc_code('X', 'R', '1', '5')
+/**
+ * @brief Definition for the TBM surface format XBGR1555 ([15:0] x:B:G:R 1:5:5:5 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_XBGR1555 __tbm_fourcc_code('X', 'B', '1', '5')
+/**
+ * @brief Definition for the TBM surface format RGBX5551 ([15:0] R:G:B:x 5:5:5:1 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGBX5551 __tbm_fourcc_code('R', 'X', '1', '5')
+/**
+ * @brief Definition for the TBM surface format BGRX5551 ([15:0] B:G:R:x 5:5:5:1 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGRX5551 __tbm_fourcc_code('B', 'X', '1', '5')
+
+/**
+ * @brief Definition for the TBM surface format ARGB1555 ([15:0] A:R:G:B 1:5:5:5 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_ARGB1555 __tbm_fourcc_code('A', 'R', '1', '5')
+/**
+ * @brief Definition for the TBM surface format ABGR1555 ([15:0] A:B:G:R 1:5:5:5 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_ABGR1555 __tbm_fourcc_code('A', 'B', '1', '5')
+/**
+ * @brief Definition for the TBM surface format RGBA5551 ([15:0] R:G:B:A 5:5:5:1 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGBA5551 __tbm_fourcc_code('R', 'A', '1', '5')
+/**
+ * @brief Definition for the TBM surface format BGRA5551 ([15:0] B:G:R:A 5:5:5:1 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGRA5551 __tbm_fourcc_code('B', 'A', '1', '5')
+
+/**
+ * @brief Definition for the TBM surface format RGB565 ([15:0] R:G:B 5:6:5 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGB565 __tbm_fourcc_code('R', 'G', '1', '6')
+/**
+ * @brief Definition for the TBM surface format BGR565 ([15:0] B:G:R 5:6:5 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGR565 __tbm_fourcc_code('B', 'G', '1', '6')
+
+/* 24 bpp RGB */
+/**
+ * @brief Definition for the TBM surface format RGB888 ([23:0] R:G:B little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGB888 __tbm_fourcc_code('R', 'G', '2', '4')
+/**
+ * @brief Definition for the TBM surface format BGR888 ([23:0] B:G:R little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGR888 __tbm_fourcc_code('B', 'G', '2', '4')
+
+/* 32 bpp RGB */
+/**
+ * @brief Definition for the TBM surface format XRGB8888 ([31:0] x:R:G:B 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_XRGB8888 __tbm_fourcc_code('X', 'R', '2', '4')
+/**
+ * @brief Definition for the TBM surface format XBGR8888 ([31:0] x:B:G:R 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_XBGR8888 __tbm_fourcc_code('X', 'B', '2', '4')
+/**
+ * @brief Definition for the TBM surface format RGBX8888 ([31:0] R:G:B:x 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGBX8888 __tbm_fourcc_code('R', 'X', '2', '4')
+/**
+ * @brief Definition for the TBM surface format BGRX8888 ([31:0] B:G:R:x 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGRX8888 __tbm_fourcc_code('B', 'X', '2', '4')
+
+/**
+ * @brief Definition for the TBM surface format ARGB8888 ([31:0] A:R:G:B 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_ARGB8888 __tbm_fourcc_code('A', 'R', '2', '4')
+/**
+ * @brief Definition for the TBM surface format ABGR8888 ([31:0] [31:0] A:B:G:R 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_ABGR8888 __tbm_fourcc_code('A', 'B', '2', '4')
+/**
+ * @brief Definition for the TBM surface format RGBA8888 ([31:0] R:G:B:A 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGBA8888 __tbm_fourcc_code('R', 'A', '2', '4')
+/**
+ * @brief Definition for the TBM surface format BGRA8888 ([31:0] B:G:R:A 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGRA8888 __tbm_fourcc_code('B', 'A', '2', '4')
+
+/**
+ * @brief Definition for the TBM surface format XRGB2101010 ([31:0] x:R:G:B 2:10:10:10 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_XRGB2101010 __tbm_fourcc_code('X', 'R', '3', '0')
+/**
+ * @brief Definition for the TBM surface format XBGR2101010 ([31:0] x:B:G:R 2:10:10:10 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_XBGR2101010 __tbm_fourcc_code('X', 'B', '3', '0')
+/**
+ * @brief Definition for the TBM surface format RGBX1010102 ([31:0] R:G:B:x 10:10:10:2 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGBX1010102 __tbm_fourcc_code('R', 'X', '3', '0')
+/**
+ * @brief Definition for the TBM surface format BGRX1010102 ([31:0] B:G:R:x 10:10:10:2 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGRX1010102 __tbm_fourcc_code('B', 'X', '3', '0')
+
+/**
+ * @brief Definition for the TBM surface format ARGB2101010 ([31:0] A:R:G:B 2:10:10:10 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_ARGB2101010 __tbm_fourcc_code('A', 'R', '3', '0')
+/**
+ * @brief Definition for the TBM surface format ABGR2101010 ([31:0] A:B:G:R 2:10:10:10 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_ABGR2101010 __tbm_fourcc_code('A', 'B', '3', '0')
+/**
+ * @brief Definition for the TBM surface format RGBA1010102 ([31:0] R:G:B:A 10:10:10:2 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_RGBA1010102 __tbm_fourcc_code('R', 'A', '3', '0')
+/**
+ * @brief Definition for the TBM surface format BGRA1010102 ([31:0] B:G:R:A 10:10:10:2 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_BGRA1010102 __tbm_fourcc_code('B', 'A', '3', '0') /* */
+
+/* packed YCbCr */
+/**
+ * @brief Definition for the TBM surface format YUYV ([31:0] Cr0:Y1:Cb0:Y0 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YUYV __tbm_fourcc_code('Y', 'U', 'Y', 'V')
+/**
+ * @brief Definition for the TBM surface format YVYU ([31:0] Cb0:Y1:Cr0:Y0 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YVYU __tbm_fourcc_code('Y', 'V', 'Y', 'U') /* */
+/**
+ * @brief Definition for the TBM surface format UYVY ([31:0] Y1:Cr0:Y0:Cb0 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_UYVY __tbm_fourcc_code('U', 'Y', 'V', 'Y')
+/**
+ * @brief Definition for the TBM surface format VYUY ([31:0] Y1:Cb0:Y0:Cr0 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_VYUY __tbm_fourcc_code('V', 'Y', 'U', 'Y')
+
+/**
+ * @brief Definition for the TBM surface format AYUV ([31:0] A:Y:Cb:Cr 8:8:8:8 little endian).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_AYUV __tbm_fourcc_code('A', 'Y', 'U', 'V')
+
+/*
+ * 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
+ */
+/**
+ * @brief Definition for the TBM surface format NV12 (2x2 subsampled Cr:Cb plane).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_NV12 __tbm_fourcc_code('N', 'V', '1', '2')
+/**
+ * @brief Definition for the TBM surface format NV21 (2x2 subsampled Cb:Cr plane).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_NV21 __tbm_fourcc_code('N', 'V', '2', '1') /* */
+/**
+ * @brief Definition for the TBM surface format NV16 (2x1 subsampled Cr:Cb plane).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_NV16 __tbm_fourcc_code('N', 'V', '1', '6')
+/**
+ * @brief Definition for the TBM surface format NV61 (2x1 subsampled Cb:Cr plane).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_NV61 __tbm_fourcc_code('N', 'V', '6', '1')
+
+/*
+ * 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
+ */
+/**
+ * @brief Definition for the TBM surface format YUV410 (4x4 subsampled Cb (1) and Cr (2) planes).
+ */
+#define TBM_FORMAT_YUV410 __tbm_fourcc_code('Y', 'U', 'V', '9')
+/**
+ * @brief Definition for the TBM surface format YVU410 (4x4 subsampled Cr (1) and Cb (2) planes).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YVU410 __tbm_fourcc_code('Y', 'V', 'U', '9')
+/**
+ * @brief Definition for the TBM surface format YUV411 (4x1 subsampled Cb (1) and Cr (2) planes).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YUV411 __tbm_fourcc_code('Y', 'U', '1', '1')
+/**
+ * @brief Definition for the TBM surface format YVU411 (4x1 subsampled Cr (1) and Cb (2) planes).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YVU411 __tbm_fourcc_code('Y', 'V', '1', '1')
+/**
+ * @brief Definition for the TBM surface format YUV420 (2x2 subsampled Cb (1) and Cr (2) planes).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YUV420 __tbm_fourcc_code('Y', 'U', '1', '2')
+/**
+ * @brief Definition for the TBM surface format YVU420 (2x2 subsampled Cr (1) and Cb (2) planes).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YVU420 __tbm_fourcc_code('Y', 'V', '1', '2')
+/**
+ * @brief Definition for the TBM surface format YUV422 (2x1 subsampled Cb (1) and Cr (2) planes).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YUV422 __tbm_fourcc_code('Y', 'U', '1', '6')
+/**
+ * @brief Definition for the TBM surface format YVU422 (2x1 subsampled Cr (1) and Cb (2) planes).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YVU422 __tbm_fourcc_code('Y', 'V', '1', '6')
+/**
+ * @brief Definition for the TBM surface format YUV444 (non-subsampled Cb (1) and Cr (2) planes).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YUV444 __tbm_fourcc_code('Y', 'U', '2', '4')
+/**
+ * @brief Definition for the TBM surface format YVU444 (non-subsampled Cr (1) and Cb (2) planes).
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ */
+#define TBM_FORMAT_YVU444 __tbm_fourcc_code('Y', 'V', '2', '4')
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Queries surface format list and number of format supported by the system.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ *
+ * @remarks You must release the formats using free().
+ *
+ * @param[out] formats The format array which the system can support \n
+ * This pointer has to be freed by user.
+ * @param[out] num The number of formats
+ *
+ * @return #TBM_SURFACE_ERROR_NONE if this function succeeds,
+ * otherwise an error status value
+ *
+ * @retval #TBM_SURFACE_ERROR_NONE Success
+ * @retval #TBM_SURFACE_ERROR_INVALID_OPERATION Invalid operation
+ *
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+
+ uint32_t *formats;
+ uint32_t format_num;
+ int ret, i;
+
+ if (tbm_surface_query_formats (&formats, &format_num))
+ {
+ for( i = 0 ; i < format_num ; i++)
+ {
+ if (formats[i] == TBM_FORMAT_RGB332)
+ {
+
+ ....
+
+ free (formats);
+ @endcode
+ */
+int tbm_surface_query_formats (uint32_t **formats, uint32_t *num);
+
+/**
+ * @brief Creates the tbm_surface.
+ * @details This function creates the tbm_surface with the given width, height, and format.
+ *
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ *
+ * @remark The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
+ *
+ * @param[in] width The width of surface
+ * @param[in] height The height of surface
+ * @param[in] format The format of surface
+ *
+ * @return #tbm_surface_h on success,
+ * otherwise @c NULL
+ *
+ * @retval #tbm_surface_h The TBM surface handle
+ *
+ * @exception #TBM_SURFACE_ERROR_NONE Success
+ * @exception #TBM_SURFACE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @exception #TBM_SURFACE_ERROR_INVALID_OPERATION Invalid operation
+ *
+ * @see tbm_surface_destroy()
+ *
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+
+ tbm_surface_h surface;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_RGB332);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+tbm_surface_h tbm_surface_create (int width, int height, tbm_format format);
+
+/**
+ * @brief Destroys the tbm_surface.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ *
+ * @param[in] surface The #tbm_surface_h
+ *
+ * @return #TBM_SURFACE_ERROR_NONE on success,
+ * otherwise an error status value
+ *
+ * @retval #TBM_SURFACE_ERROR_NONE Success
+ * @retval #TBM_SURFACE_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @see tbm_surface_create()
+ *
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+
+ tbm_surface_h surface;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_RGB332);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+int tbm_surface_destroy (tbm_surface_h surface);
+
+/**
+ * @brief Maps the tbm_surface according to the access option.
+ * @details After mapping tbm_surface, the information of tbm_surface is assigned in #tbm_surface_info_s struct. \n
+ * The information of tbm_surface has width, height, format, bpp, size, number of planes and information of planes. \n
+ * The information of planes has stride, offset, size and pointer of plane. \n
+ * #TBM_SURF_OPTION_READ indicates access option to read. \n
+ * #TBM_SURF_OPTION_WRITE indicates access option to write.
+ *
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ *
+ * @param[in] surface The #tbm_surface_h
+ * @param[in] opt The option to access the tbm_surface
+ * @param[out] info The information of the tbm_surface
+ *
+ * @return #TBM_SURFACE_ERROR_NONE on success,
+ * otherwise an error status value
+ *
+ * @retval #TBM_SURFACE_ERROR_NONE Success
+ * @retval #TBM_SURFACE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #TBM_SURFACE_ERROR_INVALID_OPERATION Invalid operation
+ *
+ * @see tbm_surface_unmap();
+ *
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+
+ tbm_surface_h surface;
+ tbm_surface_info_s info;
+ int ret;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_RGB332);
+ ret = tbm_surface_map (surface, TBM_SURF_OPTION_WRITE|TBM_SURF_OPTION_READ, &info);
+
+ ...
+
+ tbm_surface_unmap (surface);
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+int tbm_surface_map (tbm_surface_h surface, int opt, tbm_surface_info_s *info);
+
+/**
+ * @brief Unmaps the tbm_surface.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ *
+ * @param[in] surface The #tbm_surface_h
+ *
+ * @return #TBM_SURFACE_ERROR_NONE on success,
+ * otherwise an error status value
+ *
+ * @retval #TBM_SURFACE_ERROR_NONE Success
+ * @retval #TBM_SURFACE_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @see tbm_surface_map()
+ *
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+
+ tbm_surface_h surface;
+ tbm_surface_info_s info;
+ int ret;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_RGB332);
+ ret = tbm_surface_map (surface, TBM_SURF_OPTION_WRITE|TBM_SURF_OPTION_READ, &info);
+
+ ...
+
+ tbm_surface_unmap (surface);
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+int tbm_surface_unmap (tbm_surface_h surface);
+
+/**
+ * @brief Gets the information of the tbm_surface.
+ * @details The information of tbm_surface is assigned in #tbm_surface_info_s struct. \n
+ * The information of tbm_surface has width, height, format, bpp, size, number of planes and information of planes. \n
+ * The information of planes has stride, offset, size and pointer of plane.
+ *
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ *
+ * @param[in] surface The #tbm_surface_h
+ * @param[out] info The information of the tbm_surface
+ *
+ * @return #TBM_SURFACE_ERROR_NONE on success,
+ * otherwise an error status value
+ *
+ * @retval #TBM_SURFACE_ERROR_NONE Success
+ * @retval #TBM_SURFACE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #TBM_SURFACE_ERROR_INVALID_OPERATION Invalid operation
+ *
+ * @see tbm_surface_map()
+ *
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+
+ tbm_surface_h surface;
+ tbm_surface_info_s info;
+ int ret;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_RGB332);
+ ret = tbm_surface_get_info (surface, &info);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+int tbm_surface_get_info (tbm_surface_h surface, tbm_surface_info_s *info);
+
+/**
+ * @brief Gets the width of the tbm_surface.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ *
+ * @param[in] surface The #tbm_surface_h
+ *
+ * @return The width of the tbm_surface on success,
+ * otherwise an error status value
+ *
+ * @retval #TBM_SURFACE_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+
+ tbm_surface_h surface;
+ int width;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_RGB332);
+
+ ...
+
+ width = tbm_surface_get_width (surface);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+int tbm_surface_get_width (tbm_surface_h surface);
+
+/**
+ * @brief Gets the height of the tbm_surface.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ *
+ * @param[in] surface The #tbm_surface_h
+ *
+ * @return The height of the tbm_surface if this function succeeds,
+ * otherwise an error status value
+ *
+ * @retval #TBM_SURFACE_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+
+ tbm_surface_h surface;
+ int height;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_RGB332);
+
+ ...
+
+ height = tbm_surface_get_height (surface);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+int tbm_surface_get_height (tbm_surface_h surface);
+
+/**
+ * @brief Gets the format of the tbm_surface.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ *
+ * @remark The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
+ *
+ * @param[in] surface The #tbm_surface_h
+ *
+ * @return The format of the tbm_surface on success,
+ * otherwise @c 0 on failure
+ *
+ * @retval #tbm_format The format of surface
+ *
+ * @exception #TBM_SURFACE_ERROR_NONE Success
+ * @exception #TBM_SURFACE_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+
+ tbm_surface_s surface;
+ tbm_format format;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_RGB332);
+
+ ...
+
+ format = tbm_surface_get_format (surface);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+tbm_format tbm_surface_get_format (tbm_surface_h surface);
+
+#ifdef __cplusplus
+}
+#endif
+
+/**
+* @}
+*/
+
+#endif /* _TBM_SURFACE_H_ */
+
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#include "config.h"
+#include "tbm_bufmgr.h"
+#include "tbm_bufmgr_int.h"
+#include "tbm_surface_internal.h"
+#include "list.h"
+
+static tbm_bufmgr g_surface_bufmgr = NULL;
+struct list_head g_surface_list; /* list of surfaces belonging to bufmgr */
+
+static void
+_init_surface_bufmgr()
+{
+ g_surface_bufmgr = tbm_bufmgr_init (-1);
+}
+
+static void
+_deinit_surface_bufmgr()
+{
+ if (!g_surface_bufmgr)
+ return;
+
+ tbm_bufmgr_deinit (g_surface_bufmgr);
+ g_surface_bufmgr = NULL;
+}
+
+static int
+_tbm_surface_internal_query_size (tbm_surface_h surface)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface, 0);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *) surface;
+ struct _tbm_bufmgr *mgr = surf->bufmgr;
+ int size = 0;
+
+ TBM_RETURN_VAL_IF_FAIL (mgr != NULL, 0);
+ TBM_RETURN_VAL_IF_FAIL (surf->info.width > 0, 0);
+ TBM_RETURN_VAL_IF_FAIL (surf->info.height > 0, 0);
+ TBM_RETURN_VAL_IF_FAIL (surf->info.format > 0, 0);
+
+ pthread_mutex_lock (&mgr->lock);
+
+ size = mgr->backend->surface_get_size (surf, surf->info.width, surf->info.height, surf->info.format);
+
+ pthread_mutex_unlock (&mgr->lock);
+
+ return size;
+}
+
+static int
+_tbm_surface_internal_query_plane_data (tbm_surface_h surface, int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface, 0);
+ TBM_RETURN_VAL_IF_FAIL (plane_idx > -1, 0);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *) surface;
+ struct _tbm_bufmgr *mgr = surf->bufmgr;
+ int ret = 0;
+
+ TBM_RETURN_VAL_IF_FAIL (mgr != NULL, 0);
+ TBM_RETURN_VAL_IF_FAIL (surf->info.width > 0, 0);
+ TBM_RETURN_VAL_IF_FAIL (surf->info.height > 0, 0);
+ TBM_RETURN_VAL_IF_FAIL (surf->info.format > 0, 0);
+
+ pthread_mutex_lock (&mgr->lock);
+
+ ret = mgr->backend->surface_get_plane_data (surf, surf->info.width, surf->info.height, surf->info.format, plane_idx, size, offset, pitch);
+ if (!ret)
+ {
+ pthread_mutex_unlock (&mgr->lock);
+ return 0;
+ }
+
+ pthread_mutex_unlock (&mgr->lock);
+
+ return 1;
+}
+
+int
+tbm_surface_internal_query_supported_formats (uint32_t **formats, uint32_t *num)
+{
+ if (!g_surface_bufmgr)
+ {
+ _init_surface_bufmgr();
+ LIST_INITHEAD (&g_surface_list);
+ }
+
+ struct _tbm_bufmgr *mgr = g_surface_bufmgr;
+ int ret = 0;
+
+ pthread_mutex_lock (&mgr->lock);
+
+ ret = mgr->backend->surface_supported_format (formats, num);
+
+ pthread_mutex_unlock (&mgr->lock);
+
+ return ret;
+}
+
+int tbm_surface_internal_get_num_planes (tbm_format format)
+{
+ int num_planes = 0;
+
+ switch (format)
+ {
+ case TBM_FORMAT_C8:
+ case TBM_FORMAT_RGB332:
+ case TBM_FORMAT_BGR233:
+ 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:
+ case TBM_FORMAT_BGR565:
+ case TBM_FORMAT_RGB888:
+ case TBM_FORMAT_BGR888:
+ 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:
+ case TBM_FORMAT_XRGB2101010:
+ case TBM_FORMAT_XBGR2101010:
+ case TBM_FORMAT_RGBX1010102:
+ case TBM_FORMAT_BGRX1010102:
+ case TBM_FORMAT_ARGB2101010:
+ case TBM_FORMAT_ABGR2101010:
+ case TBM_FORMAT_RGBA1010102:
+ case TBM_FORMAT_BGRA1010102:
+ case TBM_FORMAT_YUYV:
+ case TBM_FORMAT_YVYU:
+ case TBM_FORMAT_UYVY:
+ case TBM_FORMAT_VYUY:
+ case TBM_FORMAT_AYUV:
+ num_planes = 1;
+ break;
+ case TBM_FORMAT_NV12:
+ case TBM_FORMAT_NV21:
+ case TBM_FORMAT_NV16:
+ case TBM_FORMAT_NV61:
+ num_planes = 2;
+ break;
+ case TBM_FORMAT_YUV410:
+ case TBM_FORMAT_YVU410:
+ case TBM_FORMAT_YUV411:
+ case TBM_FORMAT_YVU411:
+ case TBM_FORMAT_YUV420:
+ case TBM_FORMAT_YVU420:
+ case TBM_FORMAT_YUV422:
+ case TBM_FORMAT_YVU422:
+ case TBM_FORMAT_YUV444:
+ case TBM_FORMAT_YVU444:
+ num_planes = 3;
+ break;
+
+ default :
+ break;
+ }
+
+ return num_planes;
+}
+
+int tbm_surface_internal_get_bpp (tbm_format format)
+{
+ int bpp = 0;
+
+ switch (format)
+ {
+ case TBM_FORMAT_C8:
+ case TBM_FORMAT_RGB332:
+ case TBM_FORMAT_BGR233:
+ bpp = 8;
+ break;
+ 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:
+ case TBM_FORMAT_BGR565:
+ bpp = 16;
+ break;
+ case TBM_FORMAT_RGB888:
+ case TBM_FORMAT_BGR888:
+ bpp = 24;
+ break;
+ 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:
+ case TBM_FORMAT_XRGB2101010:
+ case TBM_FORMAT_XBGR2101010:
+ case TBM_FORMAT_RGBX1010102:
+ case TBM_FORMAT_BGRX1010102:
+ case TBM_FORMAT_ARGB2101010:
+ case TBM_FORMAT_ABGR2101010:
+ case TBM_FORMAT_RGBA1010102:
+ case TBM_FORMAT_BGRA1010102:
+ case TBM_FORMAT_YUYV:
+ case TBM_FORMAT_YVYU:
+ case TBM_FORMAT_UYVY:
+ case TBM_FORMAT_VYUY:
+ case TBM_FORMAT_AYUV:
+ bpp = 32;
+ break;
+ case TBM_FORMAT_NV12:
+ case TBM_FORMAT_NV21:
+ bpp = 12;
+ break;
+ case TBM_FORMAT_NV16:
+ case TBM_FORMAT_NV61:
+ bpp = 16;
+ break;
+ case TBM_FORMAT_YUV410:
+ case TBM_FORMAT_YVU410:
+ bpp = 9;
+ break;
+ case TBM_FORMAT_YUV411:
+ case TBM_FORMAT_YVU411:
+ case TBM_FORMAT_YUV420:
+ case TBM_FORMAT_YVU420:
+ bpp = 12;
+ break;
+ case TBM_FORMAT_YUV422:
+ case TBM_FORMAT_YVU422:
+ bpp = 16;
+ break;
+ case TBM_FORMAT_YUV444:
+ case TBM_FORMAT_YVU444:
+ bpp = 24;
+ break;
+ default :
+ break;
+ }
+
+ return bpp;
+}
+
+
+tbm_surface_h
+tbm_surface_internal_create_with_flags (int width, int height, int format, int flags)
+{
+ TBM_RETURN_VAL_IF_FAIL (width > 0, NULL);
+ TBM_RETURN_VAL_IF_FAIL (height > 0, NULL);
+
+ if (!g_surface_bufmgr)
+ {
+ _init_surface_bufmgr();
+ LIST_INITHEAD (&g_surface_list);
+ }
+
+ struct _tbm_bufmgr *mgr = g_surface_bufmgr;
+
+
+ TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(mgr), NULL);
+
+ struct _tbm_surface *surf = NULL;
+ uint32_t size = 0;
+ uint32_t offset = 0;
+ uint32_t stride = 0;
+ int i;
+
+ surf = calloc (1, sizeof(struct _tbm_surface));
+ if (!surf)
+ return NULL;
+
+ surf->bufmgr = mgr;
+ surf->info.width = width;
+ surf->info.height = height;
+ surf->info.format = format;
+ surf->info.bpp = tbm_surface_internal_get_bpp (format);
+ surf->info.size = _tbm_surface_internal_query_size (surf);
+ surf->info.num_planes = tbm_surface_internal_get_num_planes(format);
+
+ /* get size, stride and offset */
+ for (i = 0; i < surf->info.num_planes; i++)
+ {
+ _tbm_surface_internal_query_plane_data (surf, i, &size, &offset, &stride);
+ surf->info.planes[i].size = size;
+ surf->info.planes[i].offset = offset;
+ surf->info.planes[i].stride = stride;
+ }
+
+ surf->flags = flags;
+
+ /* create only one bo */
+ surf->num_bos = 1;
+ surf->bos[0] = tbm_bo_alloc (mgr, surf->info.size, flags);
+ if (!surf->bos[0])
+ {
+ free (surf);
+ surf = NULL;
+
+ if(LIST_IS_EMPTY (&g_surface_list))
+ {
+ _deinit_surface_bufmgr ();
+ LIST_DELINIT (&g_surface_list);
+ return NULL;
+ }
+ }
+
+ LIST_ADD (&surf->item_link, &g_surface_list);
+
+ return surf;
+}
+
+tbm_surface_h
+tbm_surface_internal_create_with_bos (tbm_surface_info_s *info, tbm_bo *bos, int num)
+{
+ TBM_RETURN_VAL_IF_FAIL (bos, NULL);
+ TBM_RETURN_VAL_IF_FAIL (info, NULL);
+
+ if (!g_surface_bufmgr)
+ {
+ _init_surface_bufmgr();
+ LIST_INITHEAD (&g_surface_list);
+ }
+
+ struct _tbm_bufmgr *mgr = g_surface_bufmgr;
+
+ TBM_RETURN_VAL_IF_FAIL (TBM_BUFMGR_IS_VALID(mgr), NULL);
+
+ struct _tbm_surface *surf = NULL;
+ int i;
+
+ surf = calloc (1, sizeof(struct _tbm_surface));
+ if (!surf)
+ return NULL;
+
+ surf->bufmgr = mgr;
+ surf->info.width = info->width;
+ surf->info.height = info->height;
+ surf->info.format = info->format;
+ surf->info.bpp = info->bpp;
+ surf->info.size = info->size;
+ surf->info.num_planes = info->num_planes;
+
+ /* get size, stride and offset */
+ for (i = 0; i < info->num_planes; i++)
+ {
+ surf->info.planes[i].size = info->planes[i].size;
+ surf->info.planes[i].offset = info->planes[i].offset;
+ surf->info.planes[i].stride = info->planes[i].stride;
+ }
+
+ surf->flags = TBM_BO_DEFAULT;
+
+ /* create only one bo */
+ surf->num_bos = num;
+ for (i = 0; i < num; i++)
+ {
+ if (bos[i] == NULL)
+ goto bail1;
+
+ surf->bos[i] = tbm_bo_ref(bos[i]);
+ }
+
+ LIST_ADD (&surf->item_link, &g_surface_list);
+
+ return surf;
+bail1:
+ for (i = 0; i < num; i++)
+ {
+ if (surf->bos[i])
+ {
+ tbm_bo_unref (surf->bos[i]);
+ surf->bos[i] = NULL;
+ }
+ }
+
+ free (surf);
+ surf = NULL;
+
+ if(LIST_IS_EMPTY (&g_surface_list))
+ {
+ _deinit_surface_bufmgr ();
+ LIST_DELINIT (&g_surface_list);
+ }
+
+ return NULL;
+}
+
+
+void
+tbm_surface_internal_destroy (tbm_surface_h surface)
+{
+ int i;
+
+ if (!surface)
+ return;
+
+ surface = (struct _tbm_surface *)surface;
+
+ for (i = 0; i < surface->num_bos; i++)
+ {
+ tbm_bo_unref (surface->bos[i]);
+ surface->bos[i] = NULL;
+ }
+
+ LIST_DEL (&surface->item_link);
+
+ free (surface);
+ surface = NULL;
+
+ if(LIST_IS_EMPTY (&g_surface_list))
+ {
+ _deinit_surface_bufmgr ();
+ LIST_DELINIT (&g_surface_list);
+ }
+}
+
+
+int
+tbm_surface_internal_get_num_bos (tbm_surface_h surface)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface, 0);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *) surface;
+
+ return surf->num_bos;
+}
+
+tbm_bo
+tbm_surface_internal_get_bo (tbm_surface_h surface, int bo_idx)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface, NULL);
+ TBM_RETURN_VAL_IF_FAIL (bo_idx > -1, NULL);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *) surface;
+
+ return surf->bos[bo_idx];
+}
+
+int
+tbm_surface_internal_get_size (tbm_surface_h surface)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface, 0);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *) surface;
+
+ return surf->info.size;
+}
+
+int
+tbm_surface_internal_get_plane_data (tbm_surface_h surface, int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch)
+{
+ TBM_RETURN_VAL_IF_FAIL (surface, 0);
+ TBM_RETURN_VAL_IF_FAIL (plane_idx > -1, 0);
+
+ struct _tbm_surface *surf = (struct _tbm_surface *) surface;
+
+ TBM_RETURN_VAL_IF_FAIL (plane_idx < surf->info.num_planes, 0);
+
+ *size = surf->info.planes[plane_idx].size;
+ *offset = surf->info.planes[plane_idx].offset;
+ *pitch = surf->info.planes[plane_idx].stride;
+
+ return 1;
+}
+
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Inpyo Kang <mantiger@samsung.com>, Dongyeon Kim <dy5.kim@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#ifndef _TBM_SURFACE_INTERNAL_H_
+#define _TBM_SURFACE_INTERNAL_H_
+
+#include <tbm_bufmgr.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Queries formats which the system can support.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @remarks The formats must be released using free().
+ * @param[in] bufmgr : the buffer manager
+ * @param[out] *formats : format array which the system can support. This pointer has to be freed by user.
+ * @param[out] num : the number of formats.
+ * @return a tbm_surface_h if this function succeeds, otherwise NULL
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+ #include <tbm_surface_internal.h>
+
+ tbm_bufmgr bufmgr;
+ uint32_t *formats;
+ uint32_t format_num;
+
+ bufmgr = tbm_bufmgr_create (-1);
+ ret = tbm_surface_internal_query_surpported_foramts (bufmgr, &formats, &format_num);
+
+ ...
+
+ free (foramts);
+ tbm_surface_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+int tbm_surface_internal_query_supported_formats (uint32_t **formats, uint32_t *num);
+
+/**
+ * @brief Creates the tbm_surface with memory type.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @details
+ * #TBM_BO_DEFAULT is default memory: it depends on the backend\n
+ * #TBM_BO_SCANOUT is scanout memory\n
+ * #TBM_BO_NONCACHABLE is non-cachable memory\n
+ * #TBM_BO_WC is write-combine memory\n
+ * #TBM_BO_VENDOR vendor specific memory: it depends on the tbm backend\n
+ * @param[in] bufmgr : the buffer manager
+ * @param[in] width : the width of surface
+ * @param[in] height : the height of surface
+ * @param[in] format : the format of surface
+ * @param[in] flags : the flags of memory type
+ * @return a tbm_surface_h if this function succeeds, otherwise NULL
+ * @retval #tbm_surface_h
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+ #include <tbm_surface_internal.h>
+
+ int bufmgr_fd
+ tbm_bufmgr bufmgr;
+ tbm_surface_h surface;
+ uint32_t *format;
+ uint32_t format_num;
+
+ bufmgr = tbm_bufmgr_create (bufmgr_fd);
+ surface = tbm_surface_internal_create_with_flags (128, 128, TBM_FORMAT_YUV420, TBM_BO_DEFAULT);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ tbm_surface_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_surface_h tbm_surface_internal_create_with_flags (int width, int height, int format, int flags);
+
+/**
+ * @brief Creates the tbm_surface with buffer objects.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] bufmgr : the buffer manager
+ * @param[in] width : the width of surface
+ * @param[in] height : the height of surface
+ * @param[in] format : the format of surface
+ * @param[in] *bos : the array pointer of buffer objects
+ * @param[in] num : the number of buffer objects
+ * @return a tbm_surface_h if this function succeeds, otherwise NULL
+ * @retval #tbm_surface_h
+ * @par Example
+ @code
+ #include <tbm_bufmgr.h>
+ #include <tbm_surface.h>
+ #include <tbm_surface_internal.h>
+
+ int bufmgr_fd
+ tbm_bufmgr bufmgr;
+ tbm_surface_h surface;
+ tbm_surface_info_s info;
+ uint32_t *format;
+ uint32_t format_num;
+ tbm_bo bo[1];
+
+ bufmgr = tbm_bufmgr_init (bufmgr_fd);
+ bo[0] = tbm_bo_alloc (bufmgr, 128 * 128, TBM_BO_DEFAULT);
+
+ info.with = 128;
+ info.height = 128;
+ info.format = TBM_FORMAT_ARGB8888;
+ info.bpp = 32;
+ info.size = 65536;
+ info.num_planes = 1;
+ info.planes[0].size = 65536;
+ info.planes[0].offset = 0;
+ info.planes[0].stride = 512;
+
+ surface = tbm_surface_internal_create_with_bos (&info, bo, 1);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ tbm_surface_bufmgr_deinit (bufmgr);
+ @endcode
+ */
+tbm_surface_h tbm_surface_internal_create_with_bos (tbm_surface_info_s *info, tbm_bo *bos, int num);
+
+
+/**
+ * @brief Destroy the tbm surface
+ TODO:
+ */
+void tbm_surface_internal_destroy (tbm_surface_h surface);
+
+/**
+ * @brief Gets the number of buffer objects associated with the tbm_surface.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] surface : the tbm_surface_h
+ * @return the number of buffer objects associated with the tbm_surface_h, otherwise -1.
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+ #include <tbm_surface_internal.h>
+
+ tbm_surface_h surface;
+ int num_bos;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_YUV420);
+ num_bos = tbm_surface_internal_get_num_bos (surface);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+int tbm_surface_internal_get_num_bos (tbm_surface_h surface);
+
+/**
+ * @brief Gets the buffor object by the bo_index.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] surface : the tbm_surface_h
+ * @param[in] bo_idx : the bo index in the the tbm_surface
+ * @return the buffer object, otherwise NULL.
+ * @retval #tbm_bo
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+ #include <tbm_surface_internal.h>
+
+ tbm_surface_h surface;
+ int num_bos;
+ tbm_bo bo;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_YUV420);
+ num_bos = tbm_surface_internal_get_num_bos (surface);
+
+ for (i=0 ; i < num_bos ; i++)
+ {
+ bo = tbm_surface_internal_get_bo (surface, i);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+tbm_bo tbm_surface_internal_get_bo (tbm_surface_h surface, int bo_idx);
+
+/**
+ * @brief Gets the size of the surface.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] surface : the tbm_surface_h
+ * @return the size of tbm_surface, otherwise -1.
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+ #include <tbm_surface_internal.h>
+
+ tbm_surface_h surface;
+ int size;
+
+ surface = tbm_surface_create (128, 128, TBM_FORMAT_YUV420);
+ size = tbm_surface_internal_get_size (surface);
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+int tbm_surface_internal_get_size (tbm_surface_h surface);
+
+/**
+ * @brief Gets size, offset and pitch data of plane by the plane_index.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] surface : the tbm_surface_h
+ * @param[in] plane_idx : the bo index in the the tbm_surface
+ * @param[out] size : the size of plan in tbm_surface
+ * @param[out] offset : the offset of plan in tbm_surface
+ * @param[out] pitch : the pitch of plan in tbm_surface
+ * @return 1 if this function succeeds, otherwise 0.
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+ #include <tbm_surface_internal.h>
+
+ tbm_surface_h surface;
+ uint32_t size, offset, pitch;
+ int ret;
+
+ surface = tbm_surfacel_create (128, 128, TBM_FORMAT_YUV420);
+ ret = tbm_surface_internal_get_plane_data (surface, 1, &size, &offset, &pitch);
+
+ ...
+
+ tbm_surface_destroy (surface);
+ @endcode
+ */
+int tbm_surface_internal_get_plane_data (tbm_surface_h surface, int plane_idx, uint32_t *size, uint32_t *offset, uint32_t *pitch);
+
+/**
+ * @brief Gets number of planes by the format.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] format : the format of surface
+ * @return number of planes by the format, otherwise 0.
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+ #include <tbm_surface_internal.h>
+
+ int num;
+
+ num = tbm_surface_internal_get_num_planes (TBM_FORMAT_YUV420);
+
+ ...
+
+ @endcode
+ */
+int tbm_surface_internal_get_num_planes (tbm_format format);
+
+/**
+ * @brief Gets bpp by the format.
+ * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
+ * @param[in] format : the format of surface
+ * @return bpp by the format, otherwise 0.
+ * @par Example
+ @code
+ #include <tbm_surface.h>
+ #include <tbm_surface_internal.h>
+
+ int bpp;
+
+ bpp = tbm_surface_internal_get_bpp (TBM_FORMAT_YUV420);
+
+ ...
+
+ @endcode
+ */
+int tbm_surface_internal_get_bpp (tbm_format format);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _TBM_SURFACE_INTERNAL_H_ */
+
--- /dev/null
+/**************************************************************************
+
+libtbm
+
+Copyright 2014 Samsung Electronics co., Ltd. All Rights Reserved.
+
+Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
+Inpyo Kang <mantiger@samsung.com>, Dongyeon Kim <dy5.kim@samsung.com>
+Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+#ifndef _TBM_TYPE_H_
+#define _TBM_TYPE_H_
+
+/**
+ * @addtogroup CAPI_UI_TBM_SURFACE_MODULE
+ * @{
+ */
+
+#include <stdint.h>
+
+/**
+ * @brief Definition for the Tizen buffer surface.
+ */
+typedef struct _tbm_surface * tbm_surface_h;
+/**
+ * @brief Definition for the Tizen buffer surface format.
+ */
+typedef uint32_t tbm_format;
+
+/**
+* @}
+*/
+
+#endif /* _TBM_TYPE_H_ */
+