summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Franzke <benjaminfranzke@googlemail.com>2011-06-20 19:13:27 +0200
committerBenjamin Franzke <benjaminfranzke@googlemail.com>2011-06-20 19:54:17 +0200
commit47f5a6a460172c805e98b2f2da08fb154318e430 (patch)
treeba3e22721cbbe5c4cfef1c3fa27c68904516b9ba
parent11663198654d5f7baebc63d6d21e03378ecce038 (diff)
change gbm_get_device back to gbm_create_devicegbm-4
additionally add _gbm_mesa_get_device.
-rw-r--r--src/gbm/main/gbm.c89
-rw-r--r--src/gbm/main/gbm.h2
-rw-r--r--src/gbm/main/gbmint.h7
3 files changed, 56 insertions, 42 deletions
diff --git a/src/gbm/main/gbm.c b/src/gbm/main/gbm.c
index 74010522de..6fab1c7362 100644
--- a/src/gbm/main/gbm.c
+++ b/src/gbm/main/gbm.c
@@ -25,14 +25,29 @@
* 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 "common.h"
#include "backend.h"
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
+
+struct gbm_device *devices[16];
+
+static int device_num = 0;
+
GBM_EXPORT int
gbm_device_get_fd(struct gbm_device *gbm)
{
@@ -62,60 +77,56 @@ gbm_device_destroy(struct gbm_device *gbm)
gbm->destroy(gbm);
}
-static int device_space = 0;
-static int device_num = 0;
-static struct {
- int fd;
- struct gbm_device *gbm;
-} *devices = NULL;
-
GBM_EXPORT struct gbm_device *
-gbm_get_device(int fd)
+_gbm_mesa_get_device(int fd)
{
struct gbm_device *gbm = NULL;
+ struct stat buf;
+ dev_t dev;
int i;
- if (fd < 0)
+ 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;
+ }
- if (devices == NULL) {
- device_space = 2;
- devices = calloc(device_space, sizeof *devices);
- for (i = 0; i < device_space; ++i)
- devices[i].fd = -1;
-
- if (devices == NULL)
- 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;
+ }
}
- for (i = 0; i < device_num; ++i)
- if (devices[i].fd == fd)
- gbm = devices[i].gbm;
+ return gbm;
+}
- if (gbm == NULL) {
- gbm = _gbm_create_device(fd);
- if (gbm == NULL)
- return NULL;
+GBM_EXPORT struct gbm_device *
+gbm_create_device(int fd)
+{
+ struct gbm_device *gbm = NULL;
+ struct stat buf;
- gbm->dummy = gbm_get_device;
- gbm->refcount = 0;
+ 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 == device_space) {
- device_space *= 2;
- devices = realloc(devices, device_space);
- if (devices == NULL)
- return NULL;
+ if (device_num == 0)
+ memset(devices, 0, sizeof devices);
- for (i = device_num; i < device_space; ++i)
- devices[i].fd = -1;
- }
+ gbm = _gbm_create_device(fd);
+ if (gbm == NULL)
+ return NULL;
- devices[device_num].fd = fd;
- devices[device_num].gbm = gbm;
- device_num++;
- }
+ gbm->dummy = gbm_create_device;
+ gbm->stat = buf;
+ gbm->refcount = 1;
- gbm->refcount++;
+ if (device_num < ARRAY_SIZE(devices)-1)
+ devices[device_num++] = gbm;
return gbm;
}
diff --git a/src/gbm/main/gbm.h b/src/gbm/main/gbm.h
index 759ee52d1e..08d1b67a14 100644
--- a/src/gbm/main/gbm.h
+++ b/src/gbm/main/gbm.h
@@ -69,7 +69,7 @@ void
gbm_device_destroy(struct gbm_device *gbm);
struct gbm_device *
-gbm_get_device(int fd);
+gbm_create_device(int fd);
struct gbm_bo *
gbm_bo_create(struct gbm_device *gbm,
diff --git a/src/gbm/main/gbmint.h b/src/gbm/main/gbmint.h
index 443c55a68b..423ac864f4 100644
--- a/src/gbm/main/gbmint.h
+++ b/src/gbm/main/gbmint.h
@@ -29,6 +29,7 @@
#define INTERNAL_H_
#include "gbm.h"
+#include <sys/stat.h>
/* GCC visibility */
#if defined(__GNUC__) && __GNUC__ >= 4
@@ -44,6 +45,7 @@ struct gbm_device {
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,
@@ -54,11 +56,9 @@ struct gbm_device {
uint32_t width, uint32_t height,
enum gbm_bo_format format,
uint32_t usage);
-
struct gbm_bo *(*bo_create_from_egl_image)(struct gbm_device *gbm,
void *egl_dpy, void *egl_img,
uint32_t width, uint32_t height);
-
void (*bo_destroy)(struct gbm_bo *bo);
};
@@ -75,4 +75,7 @@ struct gbm_backend {
struct gbm_device *(*create_device)(int fd);
};
+GBM_EXPORT struct gbm_device *
+_gbm_mesa_get_device(int fd);
+
#endif