Upload initial Wayland-only code 21/52321/2
authorjoonbum.ko <joonbum.ko@samsung.com>
Fri, 29 Aug 2014 09:54:13 +0000 (18:54 +0900)
committerSangjin Lee <lsj119@samsung.com>
Mon, 23 Nov 2015 02:53:01 +0000 (18:53 -0800)
Change-Id: I7daa608c3547c4facf919fb89ce182483b5185d6

14 files changed:
Makefile [new file with mode: 0644]
README [deleted file]
libgbm.manifest [new file with mode: 0644]
packaging/libgbm.spec [new file with mode: 0644]
pkgconfig/libgbm.pc [new file with mode: 0644]
src/backend.c [new file with mode: 0644]
src/backend.h [new file with mode: 0644]
src/backends/Makefile [new file with mode: 0644]
src/common.c [new file with mode: 0644]
src/common.h [new file with mode: 0644]
src/common_drm.h [new file with mode: 0644]
src/gbm.c [new file with mode: 0644]
src/gbm.h [new file with mode: 0644]
src/gbmint.h [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..a6d6d38
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,68 @@
+prefix ?= /usr/local
+
+GBM_SO_VER ?= no_ver
+SRC_DIR = ./src
+
+#CROSS_COMPILE ?= arm-none-linux-gnueabi-
+TARGET_CC ?= $(CROSS_COMPILE)gcc
+TARGET_AR ?= $(CROSS_COMPILE)ar
+CFLAGS += -Wall -ludev -fPIC
+
+#gbm backend sources
+GBM_BACKENDS_DIR = $(SRC_DIR)/backends
+
+#gbm backend module location
+CFLAGS += -DMODULEDIR='"/usr/lib/gbm"'
+
+#gbm main source
+GBM_SRCS +=  \
+                       $(SRC_DIR)/gbm.c \
+                       $(SRC_DIR)/common.c \
+                       $(SRC_DIR)/backend.c
+
+%.o: %.c
+       $(TARGET_CC) -c -o $@ $< $(CFLAGS)
+
+GBM_OBJS := $(GBM_SRCS:.c=.o)
+
+libgbm.so.$(GBM_SO_VER): $(GBM_OBJS)
+       $(TARGET_CC) -shared -o $@ $(GBM_OBJS) $(CFLAGS)
+backend: 
+       @for dir in $(GBM_BACKENDS_DIR) ; do \
+               $(MAKE) -C $$dir ; \
+       done
+
+.DEFAULT_GOAL = all
+all: libgbm.so.$(GBM_SO_VER) backend
+
+clean:
+       -rm -f $(GBM_OBJS) libgbm.so.$(GBM_SO_VER)
+       @for dir in $(GBM_BACKENDS_DIR) ; do \
+               $(MAKE) $@ -C $$dir ; \
+       done
+
+install: all
+       cp $(SRC_DIR)/gbm.h $(prefix)/include/
+       -mkdir $(prefix)/include/gbm
+#      cp $(SRC_DIR)/gbm.h $(prefix)/include/gbm/
+       cp $(SRC_DIR)/backend.h $(prefix)/include/gbm/
+       cp $(SRC_DIR)/common_drm.h $(prefix)/include/gbm/
+       cp $(SRC_DIR)/common.h $(prefix)/include/gbm/
+       cp $(SRC_DIR)/gbmint.h $(prefix)/include/gbm/
+       cp pkgconfig/libgbm.pc $(prefix)/lib/pkgconfig/
+       cp libgbm.so.$(GBM_SO_VER) $(prefix)/lib/
+       @for dir in $(GBM_BACKENDS_DIR) ; do \
+               $(MAKE) $@ -C $$dir ; \
+       done
+
+uninstall:
+       -rm -f $(prefix)/include/gbm.h
+       -rm -f $(prefix)/include/gbm/backend.h
+       -rm -f $(prefix)/include/gbm/common_drm.h
+       -rm -f $(prefix)/include/gbm/common.h
+       -rm -f $(prefix)/include/gbm/gbmint.h
+       -rm -f $(prefix)/lib/pkgconfig/libgbm.pc
+       -rm -f $(prefix)/lib/libgbm.so.$(GBM_SO_VER)
+       @for dir in $(GBM_BACKENDS_DIR) ; do \
+               $(MAKE) $@ -C $$dir ; \
+       done
diff --git a/README b/README
deleted file mode 100644 (file)
index 04d79a7..0000000
--- a/README
+++ /dev/null
@@ -1 +0,0 @@
-Empty master branch
diff --git a/libgbm.manifest b/libgbm.manifest
new file mode 100644 (file)
index 0000000..97e8c31
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+       <request>
+               <domain name="_"/>
+       </request>
+</manifest>
diff --git a/packaging/libgbm.spec b/packaging/libgbm.spec
new file mode 100644 (file)
index 0000000..e2f4fe2
--- /dev/null
@@ -0,0 +1,59 @@
+Summary:    Wayland Generic Buffer Management for TIZEN
+Name:       libgbm
+Version:    1.0.0s
+Release:    1
+Group:      System/X Hardware Support
+License:    Unknown
+
+Source0:    %{name}.tar.gz
+
+Provides:   libgbm.so.1.0.0s
+Provides:   libgbm.so.1
+Provides:   libgbm.so
+
+# Requirements
+BuildRequires:  autoconf
+BuildRequires:  libtool
+BuildRequires:  systemd-devel
+BuildRequires:  pkgconfig(libudev)
+BuildRequires:  libdrm-devel
+
+%description
+Wayland Generic Buffer Management for TIZEN
+
+%package devel
+Summary:    Development header files for use with GBM
+Group:      Development/Libraries
+Requires:   %{name}
+
+%description devel
+Development header files for use with gbm
+
+%prep
+%setup -q -n %{name}
+
+%build
+export GBM_SO_VER=1.0.0s
+make
+
+%install
+mkdir -p %{buildroot}%{_includedir}
+mkdir -p %{buildroot}%{_libdir}/pkgconfig
+ln -sf libgbm.so.1.0.0s %{buildroot}%{_libdir}/libgbm.so
+ln -sf libgbm.so.1.0.0s %{buildroot}%{_libdir}/libgbm.so.1
+
+export GBM_SO_VER=1.0.0s
+%makeinstall
+
+%files
+%manifest libgbm.manifest
+%defattr(-,root,root,-)
+%{_libdir}/libgbm.so.1.0.0s
+%{_libdir}/libgbm.so.1
+
+%files devel
+%defattr(-,root,root,-)
+%{_includedir}/gbm.h
+%{_includedir}/gbm/*
+%{_libdir}/pkgconfig/libgbm.pc
+%{_libdir}/libgbm.so
diff --git a/pkgconfig/libgbm.pc b/pkgconfig/libgbm.pc
new file mode 100644 (file)
index 0000000..271ceed
--- /dev/null
@@ -0,0 +1,12 @@
+prefix=/usr/local
+exec_prefix=${prefix}
+libdir=${exec_prefix}/lib
+includedir=${prefix}/include
+
+Name: libgbm
+Description: Mesa GBM library
+Requires.private: libudev >= 151
+Version: 10.2.0-devel
+Libs: -L${libdir} -lgbm
+Libs.private: -ldl
+Cflags: -I${includedir}
diff --git a/src/backend.c b/src/backend.c
new file mode 100644 (file)
index 0000000..6da2abb
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (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
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Benjamin Franzke <benjaminfranzke@googlemail.com>
+ */
+
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <dlfcn.h>
+
+#include "backend.h"
+
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
+
+struct backend_desc {
+   const char *name;
+   const struct gbm_backend *builtin;
+};
+
+static const struct backend_desc backends[] = {
+   { "gbm_dri.so", NULL },
+   { "gbm_gallium_drm.so", NULL },
+   { "gbm_tbm.so", NULL },
+};
+
+static const void *
+load_backend(const struct backend_desc *backend)
+{
+   char path[PATH_MAX];
+   const void *init = NULL;
+   void *module;
+   const char *name;
+   const char *entrypoint = "gbm_backend";
+
+   if (backend == NULL)
+      return NULL;
+
+   name = backend->name;
+
+   if (backend->builtin) {
+      init = backend->builtin;
+   } else { 
+      if (name[0] != '/')
+         snprintf(path, sizeof path, MODULEDIR "/%s", name);
+      else
+         snprintf(path, sizeof path, "%s", name);
+
+      module = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
+      if (!module) {
+         fprintf(stderr,
+                 "failed to load module: %s\n", dlerror());
+         return NULL;
+      }
+
+      init = dlsym(module, entrypoint);
+      if (!init)
+         return NULL;
+   }
+
+   return init;
+}
+
+static const struct backend_desc *
+find_backend(const char *name)
+{
+   const struct backend_desc *backend = NULL;
+   int i;
+
+   for (i = 0; i < ARRAY_SIZE(backends); ++i) {
+      if (strcmp(backends[i].name, name) == 0) {
+         backend = &backends[i];
+         break;
+      }
+   }
+
+   return backend;
+}
+
+struct gbm_device *
+_gbm_create_device(int fd)
+{
+   const struct gbm_backend *backend = NULL;
+   struct gbm_device *dev = NULL;
+   int i;
+   const char *b;
+
+   b = getenv("GBM_BACKEND");
+   if (b)
+      backend = load_backend(find_backend(b));
+
+   if (backend)
+      dev = backend->create_device(fd);
+
+   for (i = 0; i < ARRAY_SIZE(backends) && dev == NULL; ++i) {
+      backend = load_backend(&backends[i]);
+      if (backend == NULL)
+         continue;
+
+      dev = backend->create_device(fd);
+   }
+   
+   return dev;
+}
diff --git a/src/backend.h b/src/backend.h
new file mode 100644 (file)
index 0000000..4a64375
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (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
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Benjamin Franzke <benjaminfranzke@googlemail.com>
+ */
+
+#ifndef MODULE_H_
+#define MODULE_H_
+
+#include "gbmint.h"
+
+struct gbm_device *
+_gbm_create_device(int fd);
+
+#endif
diff --git a/src/backends/Makefile b/src/backends/Makefile
new file mode 100644 (file)
index 0000000..115b394
--- /dev/null
@@ -0,0 +1,23 @@
+#SUBDIRS = 
+
+default: subdirs
+
+subdirs:
+       @for dir in $(SUBDIRS) ; do \
+               $(MAKE) -C $$dir ; \
+       done
+
+clean:
+       @for dir in $(SUBDIRS) ; do \
+               $(MAKE) $@ -C $$dir ; \
+       done
+
+install: subdirs
+       @for dir in $(SUBDIRS) ; do \
+               $(MAKE) $@ -C $$dir ; \
+       done
+
+uninstall:
+       @for dir in $(SUBDIRS) ; do \
+               $(MAKE) $@ -C $$dir ; \
+       done
diff --git a/src/common.c b/src/common.c
new file mode 100644 (file)
index 0000000..f02162d
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (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
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Benjamin Franzke <benjaminfranzke@googlemail.com>
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <libudev.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "gbmint.h"
+
+GBM_EXPORT struct udev_device *
+_gbm_udev_device_new_from_fd(struct udev *udev, int fd)
+{
+   struct udev_device *device;
+   struct stat buf;
+
+   if (fstat(fd, &buf) < 0) {
+      fprintf(stderr, "gbm: failed to stat fd %d", fd);
+      return NULL;
+   }
+
+   device = udev_device_new_from_devnum(udev, 'c', buf.st_rdev);
+   if (device == NULL) {
+      fprintf(stderr,
+              "gbm: could not create udev device for fd %d", fd);
+      return NULL;
+   }
+
+   return device;
+}
+
+GBM_EXPORT char *
+_gbm_fd_get_device_name(int fd)
+{
+   struct udev *udev;
+   struct udev_device *device;
+   const char *const_device_name;
+   char *device_name = NULL;
+
+   udev = udev_new();
+   device = _gbm_udev_device_new_from_fd(udev, fd);
+   if (device == NULL)
+      return NULL;
+
+   const_device_name = udev_device_get_devnode(device);
+   if (!const_device_name)
+      goto out;
+   device_name = strdup(const_device_name);
+
+out:
+   udev_device_unref(device);
+   udev_unref(udev);
+
+   return device_name;
+}
+
+GBM_EXPORT void
+_gbm_log(const char *fmt_str, ...)
+{
+}
diff --git a/src/common.h b/src/common.h
new file mode 100644 (file)
index 0000000..1fcdfca
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (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
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Benjamin Franzke <benjaminfranzke@googlemail.com>
+ */
+
+#ifndef _COMMON_H_
+#define _COMMON_H_
+
+#include <libudev.h>
+
+struct udev_device *
+_gbm_udev_device_new_from_fd(struct udev *udev, int fd);
+
+char *
+_gbm_fd_get_device_name(int fd);
+
+void
+_gbm_log(const char *fmt_str, ...);
+
+#endif
diff --git a/src/common_drm.h b/src/common_drm.h
new file mode 100644 (file)
index 0000000..d28c3f0
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (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
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Benjamin Franzke <benjaminfranzke@googlemail.com>
+ */
+
+#ifndef _COMMON_DRM_H_
+#define _COMMON_DRM_H_
+
+#include "gbmint.h"
+
+enum gbm_drm_driver_type {
+   GBM_DRM_DRIVER_TYPE_DRI,
+   GBM_DRM_DRIVER_TYPE_GALLIUM,
+};
+
+struct gbm_drm_device {
+   struct gbm_device base;
+   enum gbm_drm_driver_type type;
+   char *driver_name;
+};
+
+struct gbm_drm_bo {
+   struct gbm_bo base;
+};
+
+#endif
diff --git a/src/gbm.c b/src/gbm.c
new file mode 100644 (file)
index 0000000..6179e5b
--- /dev/null
+++ b/src/gbm.c
@@ -0,0 +1,489 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (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
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Benjamin Franzke <benjaminfranzke@googlemail.com>
+ */
+
+#define _BSD_SOURCE
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "gbm.h"
+#include "gbmint.h"
+#include "backend.h"
+
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
+
+static struct gbm_device *devices[16];
+
+static int device_num = 0;
+
+/** Returns the file description for the gbm device
+ *
+ * \return The fd that the struct gbm_device was created with
+ */
+GBM_EXPORT int
+gbm_device_get_fd(struct gbm_device *gbm)
+{
+   return gbm->fd;
+}
+
+/* FIXME: maybe superfluous, use udev subclass from the fd? */
+/** Get the backend name for the given gbm device
+ *
+ * \return The backend name string - this belongs to the device and must not
+ * be freed
+ */
+GBM_EXPORT const char *
+gbm_device_get_backend_name(struct gbm_device *gbm)
+{
+   return gbm->name;
+}
+
+/** Test if a format is supported for a given set of usage flags.
+ *
+ * \param gbm The created buffer manager
+ * \param format The format to test
+ * \param usage A bitmask of the usages to test the format against
+ * \return 1 if the format is supported otherwise 0
+ *
+ * \sa enum gbm_bo_flags for the list of flags that the format can be
+ * tested against
+ *
+ * \sa enum gbm_bo_format for the list of formats
+ */
+GBM_EXPORT int
+gbm_device_is_format_supported(struct gbm_device *gbm,
+                               uint32_t format, uint32_t usage)
+{
+   return gbm->is_format_supported(gbm, format, usage);
+}
+
+/** Destroy the gbm device and free all resources associated with it.
+ *
+ * \param gbm The device created using gbm_create_device()
+ */
+GBM_EXPORT void
+gbm_device_destroy(struct gbm_device *gbm)
+{
+   gbm->refcount--;
+   if (gbm->refcount == 0)
+      gbm->destroy(gbm);
+}
+
+struct gbm_device *
+_gbm_mesa_get_device(int fd)
+{
+   struct gbm_device *gbm = NULL;
+   struct stat buf;
+   dev_t dev;
+   int i;
+
+   if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
+      fprintf(stderr, "_gbm_mesa_get_device: invalid fd: %d\n", fd);
+      return NULL;
+   }
+
+   for (i = 0; i < device_num; ++i) {
+      dev = devices[i]->stat.st_rdev;
+      if (major(dev) == major(buf.st_rdev) &&
+          minor(dev) == minor(buf.st_rdev)) {
+         gbm = devices[i];
+         gbm->refcount++;
+         break;
+      }
+   }
+
+   return gbm;
+}
+
+/** Create a gbm device for allocating buffers
+ *
+ * The file descriptor passed in is used by the backend to communicate with
+ * platform for allocating the memory. For allocations using DRI this would be
+ * the file descriptor returned when opening a device such as \c
+ * /dev/dri/card0
+ *
+ * \param fd The file descriptor for an backend specific device
+ * \return The newly created struct gbm_device. The resources associated with
+ * the device should be freed with gbm_device_destroy() when it is no longer
+ * needed. If the creation of the device failed NULL will be returned.
+ */
+GBM_EXPORT struct gbm_device *
+gbm_create_device(int fd)
+{
+   struct gbm_device *gbm = NULL;
+   struct stat buf;
+
+   if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
+      fprintf(stderr, "gbm_create_device: invalid fd: %d\n", fd);
+      return NULL;
+   }
+
+   if (device_num == 0)
+      memset(devices, 0, sizeof devices);
+
+   gbm = _gbm_create_device(fd);
+   if (gbm == NULL)
+      return NULL;
+
+   gbm->dummy = gbm_create_device;
+   gbm->stat = buf;
+   gbm->refcount = 1;
+
+   if (device_num < ARRAY_SIZE(devices)-1)
+      devices[device_num++] = gbm;
+
+   return gbm;
+}
+
+/** Get the width of the buffer object
+ *
+ * \param bo The buffer object
+ * \return The width of the allocated buffer object
+ *
+ */
+GBM_EXPORT unsigned int
+gbm_bo_get_width(struct gbm_bo *bo)
+{
+   return bo->width;
+}
+
+/** Get the height of the buffer object
+ *
+ * \param bo The buffer object
+ * \return The height of the allocated buffer object
+ */
+GBM_EXPORT unsigned int
+gbm_bo_get_height(struct gbm_bo *bo)
+{
+   return bo->height;
+}
+
+/** Get the stride of the buffer object
+ *
+ * This is calculated by the backend when it does the allocation in
+ * gbm_bo_create()
+ *
+ * \param bo The buffer object
+ * \return The stride of the allocated buffer object in bytes
+ */
+GBM_EXPORT uint32_t
+gbm_bo_get_stride(struct gbm_bo *bo)
+{
+   return bo->stride;
+}
+
+/** Get the format of the buffer object
+ *
+ * The format of the pixels in the buffer.
+ *
+ * \param bo The buffer object
+ * \return The format of buffer object, on of the GBM_FORMAT_* codes
+ */
+GBM_EXPORT uint32_t
+gbm_bo_get_format(struct gbm_bo *bo)
+{
+   return bo->format;
+}
+
+/** Get the handle of the buffer object
+ *
+ * This is stored in the platform generic union gbm_bo_handle type. However
+ * the format of this handle is platform specific.
+ *
+ * \param bo The buffer object
+ * \return Returns the handle of the allocated buffer object
+ */
+GBM_EXPORT union gbm_bo_handle
+gbm_bo_get_handle(struct gbm_bo *bo)
+{
+   return bo->handle;
+}
+
+/** Get a DMA-BUF file descriptor for the buffer object
+ *
+ * This function creates a DMA-BUF (also known as PRIME) file descriptor
+ * handle for the buffer object.  Eeach call to gbm_bo_get_fd() returns a new
+ * file descriptor and the caller is responsible for closing the file
+ * descriptor.
+
+ * \param bo The buffer object
+ * \return Returns a file descriptor referring  to the underlying buffer
+ */
+GBM_EXPORT int
+gbm_bo_get_fd(struct gbm_bo *bo)
+{
+   return bo->gbm->bo_get_fd(bo);
+}
+
+
+/** Write data into the buffer object
+ *
+ * If the buffer object was created with the GBM_BO_USE_WRITE flag,
+ * this function can used to write data into the buffer object.  The
+ * data is copied directly into the object and it's the responsiblity
+ * of the caller to make sure the data represents valid pixel data,
+ * according to the width, height, stride and format of the buffer object.
+ *
+ * \param bo The buffer object
+ * \param buf The data to write
+ * \param count The number of bytes to write
+ * \return Returns -1 on error, 0 otherwise
+ */
+GBM_EXPORT int
+gbm_bo_write(struct gbm_bo *bo, const void *buf, size_t count)
+{
+   return bo->gbm->bo_write(bo, buf, count);
+}
+
+/** Get the gbm device used to create the buffer object
+ *
+ * \param bo The buffer object
+ * \return Returns the gbm device with which the buffer object was created
+ */
+GBM_EXPORT struct gbm_device *
+gbm_bo_get_device(struct gbm_bo *bo)
+{
+       return bo->gbm;
+}
+
+/** Set the user data associated with a buffer object
+ *
+ * \param bo The buffer object
+ * \param data The data to associate to the buffer object
+ * \param destroy_user_data A callback (which may be %NULL) that will be
+ * called prior to the buffer destruction
+ */
+GBM_EXPORT void
+gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
+                    void (*destroy_user_data)(struct gbm_bo *, void *))
+{
+   bo->user_data = data;
+   bo->destroy_user_data = destroy_user_data;
+}
+
+/** Get the user data associated with a buffer object
+ *
+ * \param bo The buffer object
+ * \return Returns the user data associated with the buffer object or %NULL
+ * if no data was associated with it
+ *
+ * \sa gbm_bo_set_user_data()
+ */
+GBM_EXPORT void *
+gbm_bo_get_user_data(struct gbm_bo *bo)
+{
+   return bo->user_data;
+}
+
+/**
+ * Destroys the given buffer object and frees all resources associated with
+ * it.
+ *
+ * \param bo The buffer object
+ */
+GBM_EXPORT void
+gbm_bo_destroy(struct gbm_bo *bo)
+{
+   if (bo->destroy_user_data)
+      bo->destroy_user_data(bo, bo->user_data);
+
+   bo->gbm->bo_destroy(bo);
+}
+
+/**
+ * Allocate a buffer object for the given dimensions
+ *
+ * \param gbm The gbm device returned from gbm_create_device()
+ * \param width The width for the buffer
+ * \param height The height for the buffer
+ * \param format The format to use for the buffer
+ * \param usage The union of the usage flags for this buffer
+ *
+ * \return A newly allocated buffer that should be freed with gbm_bo_destroy()
+ * when no longer needed. If an error occurs during allocation %NULL will be
+ * returned.
+ *
+ * \sa enum gbm_bo_format for the list of formats
+ * \sa enum gbm_bo_flags for the list of usage flags
+ */
+GBM_EXPORT struct gbm_bo *
+gbm_bo_create(struct gbm_device *gbm,
+              uint32_t width, uint32_t height,
+              uint32_t format, uint32_t usage)
+{
+   if (width == 0 || height == 0)
+      return NULL;
+
+   if (usage & GBM_BO_USE_CURSOR_64X64 &&
+       (width != 64 || height != 64))
+      return NULL;
+
+   return gbm->bo_create(gbm, width, height, format, usage);
+}
+
+/**
+ * Create a gbm buffer object from an foreign object
+ *
+ * This function imports a foreign object and creates a new gbm bo for it.
+ * This enabled using the foreign object with a display API such as KMS.
+ * Currently two types of foreign objects are supported, indicated by the type
+ * argument:
+ *
+ *   GBM_BO_IMPORT_WL_BUFFER
+ *   GBM_BO_IMPORT_EGL_IMAGE
+ *   GBM_BO_IMPORT_FD
+ *
+ * The the gbm bo shares the underlying pixels but its life-time is
+ * independent of the foreign object.
+ *
+ * \param gbm The gbm device returned from gbm_create_device()
+ * \param gbm The type of object we're importing
+ * \param gbm Pointer to the external object
+ * \param usage The union of the usage flags for this buffer
+ *
+ * \return A newly allocated buffer object that should be freed with
+ * gbm_bo_destroy() when no longer needed.
+ *
+ * \sa enum gbm_bo_flags for the list of usage flags
+ */
+GBM_EXPORT struct gbm_bo *
+gbm_bo_import(struct gbm_device *gbm,
+              uint32_t type, void *buffer, uint32_t usage)
+{
+   return gbm->bo_import(gbm, type, buffer, usage);
+}
+
+/**
+ * Allocate a surface object
+ *
+ * \param gbm The gbm device returned from gbm_create_device()
+ * \param width The width for the surface
+ * \param height The height for the surface
+ * \param format The format to use for the surface
+ *
+ * \return A newly allocated surface that should be freed with
+ * gbm_surface_destroy() when no longer needed. If an error occurs
+ * during allocation %NULL will be returned.
+ *
+ * \sa enum gbm_bo_format for the list of formats
+ */
+GBM_EXPORT struct gbm_surface *
+gbm_surface_create(struct gbm_device *gbm,
+                   uint32_t width, uint32_t height,
+                  uint32_t format, uint32_t flags)
+{
+   return gbm->surface_create(gbm, width, height, format, flags);
+}
+
+/**
+ * Destroys the given surface and frees all resources associated with
+ * it.
+ *
+ * All buffers locked with gbm_surface_lock_front_buffer() should be
+ * released prior to calling this function.
+ *
+ * \param surf The surface
+ */
+GBM_EXPORT void
+gbm_surface_destroy(struct gbm_surface *surf)
+{
+   surf->gbm->surface_destroy(surf);
+}
+
+/**
+ * Lock the surface's current front buffer
+ *
+ * Lock rendering to the surface's current front buffer until it is
+ * released with gbm_surface_release_buffer().
+ *
+ * This function must be called exactly once after calling
+ * eglSwapBuffers.  Calling it before any eglSwapBuffer has happened
+ * on the surface or two or more times after eglSwapBuffers is an
+ * error.  A new bo representing the new front buffer is returned.  On
+ * multiple invocations, all the returned bos must be released in
+ * order to release the actual surface buffer.
+ *
+ * \param surf The surface
+ *
+ * \return A buffer object that should be released with
+ * gbm_surface_release_buffer() when no longer needed.  The implementation
+ * is free to reuse buffers released with gbm_surface_release_buffer() so
+ * this bo should not be destroyed using gbm_bo_destroy().  If an error
+ * occurs this function returns %NULL.
+ */
+GBM_EXPORT struct gbm_bo *
+gbm_surface_lock_front_buffer(struct gbm_surface *surf)
+{
+   return surf->gbm->surface_lock_front_buffer(surf);
+}
+
+/**
+ * Release a locked buffer obtained with gbm_surface_lock_front_buffer()
+ *
+ * Returns the underlying buffer to the gbm surface.  Releasing a bo
+ * will typically make gbm_surface_has_free_buffer() return 1 and thus
+ * allow rendering the next frame, but not always. The implementation
+ * may choose to destroy the bo immediately or reuse it, in which case
+ * the user data associated with it is unchanged.
+ *
+ * \param surf The surface
+ * \param bo The buffer object
+ */
+GBM_EXPORT void
+gbm_surface_release_buffer(struct gbm_surface *surf, struct gbm_bo *bo)
+{
+   surf->gbm->surface_release_buffer(surf, bo);
+}
+
+/**
+ * Return whether or not a surface has free (non-locked) buffers
+ *
+ * Before starting a new frame, the surface must have a buffer
+ * available for rendering.  Initially, a gbm surface will have a free
+ * buffer, but after one of more buffers have been locked (\sa
+ * gbm_surface_lock_front_buffer()), the application must check for a
+ * free buffer before rendering.
+ *
+ * If a surface doesn't have a free buffer, the application must
+ * return a buffer to the surface using gbm_surface_release_buffer()
+ * and after that, the application can query for free buffers again.
+ *
+ * \param surf The surface
+ * \return 1 if the surface has free buffers, 0 otherwise
+ */
+GBM_EXPORT int
+gbm_surface_has_free_buffers(struct gbm_surface *surf)
+{
+   return surf->gbm->surface_has_free_buffers(surf);
+}
diff --git a/src/gbm.h b/src/gbm.h
new file mode 100644 (file)
index 0000000..92d472a
--- /dev/null
+++ b/src/gbm.h
@@ -0,0 +1,304 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (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
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Benjamin Franzke <benjaminfranzke@googlemail.com>
+ */
+
+#ifndef _GBM_H_
+#define _GBM_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#define __GBM__ 1
+
+#include <stdint.h>
+
+/**
+ * \file gbm.h
+ * \brief Generic Buffer Manager
+ */
+
+struct gbm_device;
+struct gbm_bo;
+struct gbm_surface;
+
+/**
+ * \mainpage The Generic Buffer Manager
+ *
+ * This module provides an abstraction that the caller can use to request a
+ * buffer from the underlying memory management system for the platform.
+ *
+ * This allows the creation of portable code whilst still allowing access to
+ * the underlying memory manager.
+ */
+
+/**
+ * Abstraction representing the handle to a buffer allocated by the
+ * manager
+ */
+union gbm_bo_handle {
+   void *ptr;
+   int32_t s32;
+   uint32_t u32;
+   int64_t s64;
+   uint64_t u64;
+};
+
+/** Format of the allocated buffer */
+enum gbm_bo_format {
+   /** RGB with 8 bits per channel in a 32 bit value */
+   GBM_BO_FORMAT_XRGB8888, 
+   /** ARGB with 8 bits per channel in a 32 bit value */
+   GBM_BO_FORMAT_ARGB8888
+};
+
+#define __gbm_fourcc_code(a,b,c,d) ((uint32_t)(a) | ((uint32_t)(b) << 8) | \
+                             ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
+
+#define GBM_FORMAT_BIG_ENDIAN (1<<31) /* format is big endian instead of little endian */
+
+/* color index */
+#define GBM_FORMAT_C8          __gbm_fourcc_code('C', '8', ' ', ' ') /* [7:0] C */
+
+/* 8 bpp RGB */
+#define GBM_FORMAT_RGB332      __gbm_fourcc_code('R', 'G', 'B', '8') /* [7:0] R:G:B 3:3:2 */
+#define GBM_FORMAT_BGR233      __gbm_fourcc_code('B', 'G', 'R', '8') /* [7:0] B:G:R 2:3:3 */
+
+/* 16 bpp RGB */
+#define GBM_FORMAT_XRGB4444    __gbm_fourcc_code('X', 'R', '1', '2') /* [15:0] x:R:G:B 4:4:4:4 little endian */
+#define GBM_FORMAT_XBGR4444    __gbm_fourcc_code('X', 'B', '1', '2') /* [15:0] x:B:G:R 4:4:4:4 little endian */
+#define GBM_FORMAT_RGBX4444    __gbm_fourcc_code('R', 'X', '1', '2') /* [15:0] R:G:B:x 4:4:4:4 little endian */
+#define GBM_FORMAT_BGRX4444    __gbm_fourcc_code('B', 'X', '1', '2') /* [15:0] B:G:R:x 4:4:4:4 little endian */
+
+#define GBM_FORMAT_ARGB4444    __gbm_fourcc_code('A', 'R', '1', '2') /* [15:0] A:R:G:B 4:4:4:4 little endian */
+#define GBM_FORMAT_ABGR4444    __gbm_fourcc_code('A', 'B', '1', '2') /* [15:0] A:B:G:R 4:4:4:4 little endian */
+#define GBM_FORMAT_RGBA4444    __gbm_fourcc_code('R', 'A', '1', '2') /* [15:0] R:G:B:A 4:4:4:4 little endian */
+#define GBM_FORMAT_BGRA4444    __gbm_fourcc_code('B', 'A', '1', '2') /* [15:0] B:G:R:A 4:4:4:4 little endian */
+
+#define GBM_FORMAT_XRGB1555    __gbm_fourcc_code('X', 'R', '1', '5') /* [15:0] x:R:G:B 1:5:5:5 little endian */
+#define GBM_FORMAT_XBGR1555    __gbm_fourcc_code('X', 'B', '1', '5') /* [15:0] x:B:G:R 1:5:5:5 little endian */
+#define GBM_FORMAT_RGBX5551    __gbm_fourcc_code('R', 'X', '1', '5') /* [15:0] R:G:B:x 5:5:5:1 little endian */
+#define GBM_FORMAT_BGRX5551    __gbm_fourcc_code('B', 'X', '1', '5') /* [15:0] B:G:R:x 5:5:5:1 little endian */
+
+#define GBM_FORMAT_ARGB1555    __gbm_fourcc_code('A', 'R', '1', '5') /* [15:0] A:R:G:B 1:5:5:5 little endian */
+#define GBM_FORMAT_ABGR1555    __gbm_fourcc_code('A', 'B', '1', '5') /* [15:0] A:B:G:R 1:5:5:5 little endian */
+#define GBM_FORMAT_RGBA5551    __gbm_fourcc_code('R', 'A', '1', '5') /* [15:0] R:G:B:A 5:5:5:1 little endian */
+#define GBM_FORMAT_BGRA5551    __gbm_fourcc_code('B', 'A', '1', '5') /* [15:0] B:G:R:A 5:5:5:1 little endian */
+
+#define GBM_FORMAT_RGB565      __gbm_fourcc_code('R', 'G', '1', '6') /* [15:0] R:G:B 5:6:5 little endian */
+#define GBM_FORMAT_BGR565      __gbm_fourcc_code('B', 'G', '1', '6') /* [15:0] B:G:R 5:6:5 little endian */
+
+/* 24 bpp RGB */
+#define GBM_FORMAT_RGB888      __gbm_fourcc_code('R', 'G', '2', '4') /* [23:0] R:G:B little endian */
+#define GBM_FORMAT_BGR888      __gbm_fourcc_code('B', 'G', '2', '4') /* [23:0] B:G:R little endian */
+
+/* 32 bpp RGB */
+#define GBM_FORMAT_XRGB8888    __gbm_fourcc_code('X', 'R', '2', '4') /* [31:0] x:R:G:B 8:8:8:8 little endian */
+#define GBM_FORMAT_XBGR8888    __gbm_fourcc_code('X', 'B', '2', '4') /* [31:0] x:B:G:R 8:8:8:8 little endian */
+#define GBM_FORMAT_RGBX8888    __gbm_fourcc_code('R', 'X', '2', '4') /* [31:0] R:G:B:x 8:8:8:8 little endian */
+#define GBM_FORMAT_BGRX8888    __gbm_fourcc_code('B', 'X', '2', '4') /* [31:0] B:G:R:x 8:8:8:8 little endian */
+
+#define GBM_FORMAT_ARGB8888    __gbm_fourcc_code('A', 'R', '2', '4') /* [31:0] A:R:G:B 8:8:8:8 little endian */
+#define GBM_FORMAT_ABGR8888    __gbm_fourcc_code('A', 'B', '2', '4') /* [31:0] A:B:G:R 8:8:8:8 little endian */
+#define GBM_FORMAT_RGBA8888    __gbm_fourcc_code('R', 'A', '2', '4') /* [31:0] R:G:B:A 8:8:8:8 little endian */
+#define GBM_FORMAT_BGRA8888    __gbm_fourcc_code('B', 'A', '2', '4') /* [31:0] B:G:R:A 8:8:8:8 little endian */
+
+#define GBM_FORMAT_XRGB2101010 __gbm_fourcc_code('X', 'R', '3', '0') /* [31:0] x:R:G:B 2:10:10:10 little endian */
+#define GBM_FORMAT_XBGR2101010 __gbm_fourcc_code('X', 'B', '3', '0') /* [31:0] x:B:G:R 2:10:10:10 little endian */
+#define GBM_FORMAT_RGBX1010102 __gbm_fourcc_code('R', 'X', '3', '0') /* [31:0] R:G:B:x 10:10:10:2 little endian */
+#define GBM_FORMAT_BGRX1010102 __gbm_fourcc_code('B', 'X', '3', '0') /* [31:0] B:G:R:x 10:10:10:2 little endian */
+
+#define GBM_FORMAT_ARGB2101010 __gbm_fourcc_code('A', 'R', '3', '0') /* [31:0] A:R:G:B 2:10:10:10 little endian */
+#define GBM_FORMAT_ABGR2101010 __gbm_fourcc_code('A', 'B', '3', '0') /* [31:0] A:B:G:R 2:10:10:10 little endian */
+#define GBM_FORMAT_RGBA1010102 __gbm_fourcc_code('R', 'A', '3', '0') /* [31:0] R:G:B:A 10:10:10:2 little endian */
+#define GBM_FORMAT_BGRA1010102 __gbm_fourcc_code('B', 'A', '3', '0') /* [31:0] B:G:R:A 10:10:10:2 little endian */
+
+/* packed YCbCr */
+#define GBM_FORMAT_YUYV                __gbm_fourcc_code('Y', 'U', 'Y', 'V') /* [31:0] Cr0:Y1:Cb0:Y0 8:8:8:8 little endian */
+#define GBM_FORMAT_YVYU                __gbm_fourcc_code('Y', 'V', 'Y', 'U') /* [31:0] Cb0:Y1:Cr0:Y0 8:8:8:8 little endian */
+#define GBM_FORMAT_UYVY                __gbm_fourcc_code('U', 'Y', 'V', 'Y') /* [31:0] Y1:Cr0:Y0:Cb0 8:8:8:8 little endian */
+#define GBM_FORMAT_VYUY                __gbm_fourcc_code('V', 'Y', 'U', 'Y') /* [31:0] Y1:Cb0:Y0:Cr0 8:8:8:8 little endian */
+
+#define GBM_FORMAT_AYUV                __gbm_fourcc_code('A', 'Y', 'U', 'V') /* [31:0] A:Y:Cb:Cr 8:8:8:8 little endian */
+
+/*
+ * 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
+ */
+#define GBM_FORMAT_NV12                __gbm_fourcc_code('N', 'V', '1', '2') /* 2x2 subsampled Cr:Cb plane */
+#define GBM_FORMAT_NV21                __gbm_fourcc_code('N', 'V', '2', '1') /* 2x2 subsampled Cb:Cr plane */
+#define GBM_FORMAT_NV16                __gbm_fourcc_code('N', 'V', '1', '6') /* 2x1 subsampled Cr:Cb plane */
+#define GBM_FORMAT_NV61                __gbm_fourcc_code('N', 'V', '6', '1') /* 2x1 subsampled Cb:Cr plane */
+
+/*
+ * 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
+ */
+#define GBM_FORMAT_YUV410      __gbm_fourcc_code('Y', 'U', 'V', '9') /* 4x4 subsampled Cb (1) and Cr (2) planes */
+#define GBM_FORMAT_YVU410      __gbm_fourcc_code('Y', 'V', 'U', '9') /* 4x4 subsampled Cr (1) and Cb (2) planes */
+#define GBM_FORMAT_YUV411      __gbm_fourcc_code('Y', 'U', '1', '1') /* 4x1 subsampled Cb (1) and Cr (2) planes */
+#define GBM_FORMAT_YVU411      __gbm_fourcc_code('Y', 'V', '1', '1') /* 4x1 subsampled Cr (1) and Cb (2) planes */
+#define GBM_FORMAT_YUV420      __gbm_fourcc_code('Y', 'U', '1', '2') /* 2x2 subsampled Cb (1) and Cr (2) planes */
+#define GBM_FORMAT_YVU420      __gbm_fourcc_code('Y', 'V', '1', '2') /* 2x2 subsampled Cr (1) and Cb (2) planes */
+#define GBM_FORMAT_YUV422      __gbm_fourcc_code('Y', 'U', '1', '6') /* 2x1 subsampled Cb (1) and Cr (2) planes */
+#define GBM_FORMAT_YVU422      __gbm_fourcc_code('Y', 'V', '1', '6') /* 2x1 subsampled Cr (1) and Cb (2) planes */
+#define GBM_FORMAT_YUV444      __gbm_fourcc_code('Y', 'U', '2', '4') /* non-subsampled Cb (1) and Cr (2) planes */
+#define GBM_FORMAT_YVU444      __gbm_fourcc_code('Y', 'V', '2', '4') /* non-subsampled Cr (1) and Cb (2) planes */
+
+
+/**
+ * Flags to indicate the intended use for the buffer - these are passed into
+ * gbm_bo_create(). The caller must set the union of all the flags that are
+ * appropriate
+ *
+ * \sa Use gbm_device_is_format_supported() to check if the combination of format
+ * and use flags are supported
+ */
+enum gbm_bo_flags {
+   /**
+    * Buffer is going to be presented to the screen using an API such as KMS
+    */
+   GBM_BO_USE_SCANOUT      = (1 << 0),
+   /**
+    * Buffer is going to be used as cursor - the dimensions for the buffer
+    * must be 64x64 if this flag is passed.
+    */
+   GBM_BO_USE_CURSOR_64X64 = (1 << 1),
+   /**
+    * Buffer is to be used for rendering - for example it is going to be used
+    * as the storage for a color buffer
+    */
+   GBM_BO_USE_RENDERING    = (1 << 2),
+   /**
+    * Buffer can be used for gbm_bo_write.  This is guaranteed to work
+    * with GBM_BO_USE_CURSOR_64X64. but may not work for other
+    * combinations.
+    */
+   GBM_BO_USE_WRITE    = (1 << 3),
+};
+
+int
+gbm_device_get_fd(struct gbm_device *gbm);
+
+const char *
+gbm_device_get_backend_name(struct gbm_device *gbm);
+
+int
+gbm_device_is_format_supported(struct gbm_device *gbm,
+                               uint32_t format, uint32_t usage);
+
+void
+gbm_device_destroy(struct gbm_device *gbm);
+
+struct gbm_device *
+gbm_create_device(int fd);
+
+struct gbm_bo *
+gbm_bo_create(struct gbm_device *gbm,
+              uint32_t width, uint32_t height,
+              uint32_t format, uint32_t flags);
+
+#define GBM_BO_IMPORT_WL_BUFFER         0x5501
+#define GBM_BO_IMPORT_EGL_IMAGE         0x5502
+#define GBM_BO_IMPORT_FD                0x5503
+
+struct gbm_import_fd_data {
+   int fd;
+   uint32_t width;
+   uint32_t height;
+   uint32_t stride;
+   uint32_t format;
+};
+
+struct gbm_bo *
+gbm_bo_import(struct gbm_device *gbm, uint32_t type,
+              void *buffer, uint32_t usage);
+
+uint32_t
+gbm_bo_get_width(struct gbm_bo *bo);
+
+uint32_t
+gbm_bo_get_height(struct gbm_bo *bo);
+
+uint32_t
+gbm_bo_get_stride(struct gbm_bo *bo);
+
+uint32_t
+gbm_bo_get_format(struct gbm_bo *bo);
+
+struct gbm_device *
+gbm_bo_get_device(struct gbm_bo *bo);
+
+union gbm_bo_handle
+gbm_bo_get_handle(struct gbm_bo *bo);
+
+int
+gbm_bo_get_fd(struct gbm_bo *bo);
+
+int
+gbm_bo_write(struct gbm_bo *bo, const void *buf, size_t count);
+
+void
+gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
+                    void (*destroy_user_data)(struct gbm_bo *, void *));
+
+void *
+gbm_bo_get_user_data(struct gbm_bo *bo);
+
+void
+gbm_bo_destroy(struct gbm_bo *bo);
+
+struct gbm_surface *
+gbm_surface_create(struct gbm_device *gbm,
+                   uint32_t width, uint32_t height,
+                  uint32_t format, uint32_t flags);
+
+struct gbm_bo *
+gbm_surface_lock_front_buffer(struct gbm_surface *surface);
+
+void
+gbm_surface_release_buffer(struct gbm_surface *surface, struct gbm_bo *bo);
+
+int
+gbm_surface_has_free_buffers(struct gbm_surface *surface);
+
+void
+gbm_surface_destroy(struct gbm_surface *surface);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/gbmint.h b/src/gbmint.h
new file mode 100644 (file)
index 0000000..31345e6
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (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
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Benjamin Franzke <benjaminfranzke@googlemail.com>
+ */
+
+#ifndef INTERNAL_H_
+#define INTERNAL_H_
+
+#include "gbm.h"
+#include <sys/stat.h>
+
+/* GCC visibility */
+#if defined(__GNUC__) && __GNUC__ >= 4
+#define GBM_EXPORT __attribute__ ((visibility("default")))
+#else
+#define GBM_EXPORT
+#endif
+
+/**
+ * \file gbmint.h
+ * \brief Internal implementation details of gbm
+ */
+
+/**
+ * The device used for the memory allocation.
+ *
+ * The members of this structure should be not accessed directly
+ */
+struct gbm_device {
+   /* Hack to make a gbm_device detectable by its first element. */
+   struct gbm_device *(*dummy)(int);
+
+   int fd;
+   const char *name;
+   unsigned int refcount;
+   struct stat stat;
+
+   void (*destroy)(struct gbm_device *gbm);
+   int (*is_format_supported)(struct gbm_device *gbm,
+                              uint32_t format,
+                              uint32_t usage);
+
+   struct gbm_bo *(*bo_create)(struct gbm_device *gbm,
+                               uint32_t width, uint32_t height,
+                               uint32_t format,
+                               uint32_t usage);
+   struct gbm_bo *(*bo_import)(struct gbm_device *gbm, uint32_t type,
+                               void *buffer, uint32_t usage);
+   int (*bo_write)(struct gbm_bo *bo, const void *buf, size_t data);
+   int (*bo_get_fd)(struct gbm_bo *bo);
+   void (*bo_destroy)(struct gbm_bo *bo);
+
+   struct gbm_surface *(*surface_create)(struct gbm_device *gbm,
+                                         uint32_t width, uint32_t height,
+                                         uint32_t format, uint32_t flags);
+   struct gbm_bo *(*surface_lock_front_buffer)(struct gbm_surface *surface);
+   void (*surface_release_buffer)(struct gbm_surface *surface,
+                                  struct gbm_bo *bo);
+   int (*surface_has_free_buffers)(struct gbm_surface *surface);
+   void (*surface_destroy)(struct gbm_surface *surface);
+};
+
+/**
+ * The allocated buffer object.
+ *
+ * The members in this structure should not be accessed directly.
+ */
+struct gbm_bo {
+   struct gbm_device *gbm;
+   uint32_t width;
+   uint32_t height;
+   uint32_t stride;
+   uint32_t format;
+   union gbm_bo_handle  handle;
+   void *user_data;
+   void (*destroy_user_data)(struct gbm_bo *, void *);
+};
+
+struct gbm_surface {
+   struct gbm_device *gbm;
+   uint32_t width;
+   uint32_t height;
+   uint32_t format;
+   uint32_t flags;
+};
+
+struct gbm_backend {
+   const char *backend_name;
+   struct gbm_device *(*create_device)(int fd);
+};
+
+struct gbm_device *
+_gbm_mesa_get_device(int fd);
+
+#endif