Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gbm / main / gbm.c
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Benjamin Franzke <benjaminfranzke@googlemail.com>
26  */
27
28 #define _BSD_SOURCE
29
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdint.h>
35
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39
40 #include "gbm.h"
41 #include "gbmint.h"
42 #include "common.h"
43 #include "backend.h"
44
45 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
46
47 struct gbm_device *devices[16];
48
49 static int device_num = 0;
50
51 GBM_EXPORT int
52 gbm_device_get_fd(struct gbm_device *gbm)
53 {
54    return gbm->fd;
55 }
56
57 /* FIXME: maybe superfluous, use udev subclass from the fd? */
58 GBM_EXPORT const char *
59 gbm_device_get_backend_name(struct gbm_device *gbm)
60 {
61    return gbm->name;
62 }
63
64 int
65 gbm_device_is_format_supported(struct gbm_device *gbm,
66                                enum gbm_bo_format format,
67                                uint32_t usage)
68 {
69    return gbm->is_format_supported(gbm, format, usage);
70 }
71
72 GBM_EXPORT void
73 gbm_device_destroy(struct gbm_device *gbm)
74 {
75    gbm->refcount--;
76    if (gbm->refcount == 0)
77       gbm->destroy(gbm);
78 }
79
80 GBM_EXPORT struct gbm_device *
81 _gbm_mesa_get_device(int fd)
82 {
83    struct gbm_device *gbm = NULL;
84    struct stat buf;
85    dev_t dev;
86    int i;
87
88    if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
89       fprintf(stderr, "_gbm_mesa_get_device: invalid fd: %d\n", fd);
90       return NULL;
91    }
92
93    for (i = 0; i < device_num; ++i) {
94       dev = devices[i]->stat.st_rdev;
95       if (major(dev) == major(buf.st_rdev) &&
96           minor(dev) == minor(buf.st_rdev)) {
97          gbm = devices[i];
98          gbm->refcount++;
99          break;
100       }
101    }
102
103    return gbm;
104 }
105
106 GBM_EXPORT struct gbm_device *
107 gbm_create_device(int fd)
108 {
109    struct gbm_device *gbm = NULL;
110    struct stat buf;
111
112    if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
113       fprintf(stderr, "gbm_create_device: invalid fd: %d\n", fd);
114       return NULL;
115    }
116
117    if (device_num == 0)
118       memset(devices, 0, sizeof devices);
119
120    gbm = _gbm_create_device(fd);
121    if (gbm == NULL)
122       return NULL;
123
124    gbm->dummy = gbm_create_device;
125    gbm->stat = buf;
126    gbm->refcount = 1;
127
128    if (device_num < ARRAY_SIZE(devices)-1)
129       devices[device_num++] = gbm;
130
131    return gbm;
132 }
133
134 GBM_EXPORT unsigned int
135 gbm_bo_get_width(struct gbm_bo *bo)
136 {
137    return bo->width;
138 }
139
140 GBM_EXPORT unsigned int
141 gbm_bo_get_height(struct gbm_bo *bo)
142 {
143    return bo->height;
144 }
145
146 GBM_EXPORT uint32_t
147 gbm_bo_get_pitch(struct gbm_bo *bo)
148 {
149    return bo->pitch;
150 }
151
152 GBM_EXPORT union gbm_bo_handle
153 gbm_bo_get_handle(struct gbm_bo *bo)
154 {
155    return bo->handle;
156 }
157
158 GBM_EXPORT void
159 gbm_bo_destroy(struct gbm_bo *bo)
160 {
161    bo->gbm->bo_destroy(bo);
162 }
163
164 GBM_EXPORT struct gbm_bo *
165 gbm_bo_create(struct gbm_device *gbm,
166               uint32_t width, uint32_t height,
167               enum gbm_bo_format format, uint32_t usage)
168 {
169    if (width == 0 || height == 0)
170       return NULL;
171
172    if (usage & GBM_BO_USE_CURSOR_64X64 &&
173        (width != 64 || height != 64))
174       return NULL;
175
176    return gbm->bo_create(gbm, width, height, format, usage);
177 }
178
179 GBM_EXPORT struct gbm_bo *
180 gbm_bo_create_from_egl_image(struct gbm_device *gbm,
181                              void *egl_dpy, void *egl_image,
182                              uint32_t width, uint32_t height,
183                              uint32_t usage)
184 {
185    if (width == 0 || height == 0)
186       return NULL;
187
188    return gbm->bo_create_from_egl_image(gbm, egl_dpy, egl_image,
189                                         width, height, usage);
190 }