OSDN Git Service

gralloc: split resolve_drm_format
[android-x86/external-drm_gralloc.git] / gralloc.c
1 /*
2  * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3  * Copyright (C) 2010-2011 LunarG Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #define LOG_TAG "GRALLOC-MOD"
25
26 #include <cutils/log.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <pthread.h>
30 #include <errno.h>
31
32 #include "gralloc_drm.h"
33
34 struct drm_module_t {
35         gralloc_module_t base;
36
37         pthread_mutex_t mutex;
38         struct gralloc_drm_t *drm;
39 };
40
41 /*
42  * Initialize the DRM device object, optionally with KMS.
43  */
44 static int drm_init(struct drm_module_t *dmod, int kms)
45 {
46         int err = 0;
47
48         pthread_mutex_lock(&dmod->mutex);
49         if (!dmod->drm) {
50                 dmod->drm = gralloc_drm_create();
51                 if (!dmod->drm)
52                         err = -EINVAL;
53         }
54         if (!err && kms)
55                 err = gralloc_drm_init_kms(dmod->drm);
56         pthread_mutex_unlock(&dmod->mutex);
57
58         return err;
59 }
60
61 static int drm_mod_perform(const struct gralloc_module_t *mod, int op, ...)
62 {
63         struct drm_module_t *dmod = (struct drm_module_t *) mod;
64         va_list args;
65         int err;
66
67         err = drm_init(dmod, 0);
68         if (err)
69                 return err;
70
71         va_start(args, op);
72         switch (op) {
73         case GRALLOC_MODULE_PERFORM_GET_DRM_FD:
74                 {
75                         int *fd = va_arg(args, int *);
76                         *fd = gralloc_drm_get_fd(dmod->drm);
77                         err = 0;
78                 }
79                 break;
80         /* should we remove this and next ops, and make it transparent? */
81         case GRALLOC_MODULE_PERFORM_GET_DRM_MAGIC:
82                 {
83                         int32_t *magic = va_arg(args, int32_t *);
84                         err = gralloc_drm_get_magic(dmod->drm, magic);
85                 }
86                 break;
87         case GRALLOC_MODULE_PERFORM_AUTH_DRM_MAGIC:
88                 {
89                         int32_t magic = va_arg(args, int32_t);
90                         err = gralloc_drm_auth_magic(dmod->drm, magic);
91                 }
92                 break;
93         case GRALLOC_MODULE_PERFORM_ENTER_VT:
94                 {
95                         err = gralloc_drm_set_master(dmod->drm);
96                 }
97                 break;
98         case GRALLOC_MODULE_PERFORM_LEAVE_VT:
99                 {
100                         gralloc_drm_drop_master(dmod->drm);
101                         err = 0;
102                 }
103                 break;
104         default:
105                 err = -EINVAL;
106                 break;
107         }
108         va_end(args);
109
110         return err;
111 }
112
113 static int drm_mod_register_buffer(const gralloc_module_t *mod,
114                 buffer_handle_t handle)
115 {
116         struct drm_module_t *dmod = (struct drm_module_t *) mod;
117         int err;
118
119         err = drm_init(dmod, 0);
120         if (err)
121                 return err;
122
123         return gralloc_drm_handle_register(handle, dmod->drm);
124 }
125
126 static int drm_mod_unregister_buffer(const gralloc_module_t *mod,
127                 buffer_handle_t handle)
128 {
129         return gralloc_drm_handle_unregister(handle);
130 }
131
132 static int drm_mod_lock(const gralloc_module_t *mod, buffer_handle_t handle,
133                 int usage, int x, int y, int w, int h, void **ptr)
134 {
135         struct gralloc_drm_bo_t *bo;
136         int err;
137
138         bo = gralloc_drm_bo_from_handle(handle);
139         if (!bo)
140                 return -EINVAL;
141
142         return gralloc_drm_bo_lock(bo, usage, x, y, w, h, ptr);
143 }
144
145 static int drm_mod_unlock(const gralloc_module_t *mod, buffer_handle_t handle)
146 {
147         struct drm_module_t *dmod = (struct drm_module_t *) mod;
148         struct gralloc_drm_bo_t *bo;
149
150         bo = gralloc_drm_bo_from_handle(handle);
151         if (!bo)
152                 return -EINVAL;
153
154         gralloc_drm_bo_unlock(bo);
155
156         return 0;
157 }
158
159 static int drm_mod_close_gpu0(struct hw_device_t *dev)
160 {
161         struct alloc_device_t *alloc = (struct alloc_device_t *) dev;
162
163         free(alloc);
164
165         return 0;
166 }
167
168 static int drm_mod_free_gpu0(alloc_device_t *dev, buffer_handle_t handle)
169 {
170         struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
171         struct gralloc_drm_bo_t *bo;
172
173         bo = gralloc_drm_bo_from_handle(handle);
174         if (!bo)
175                 return -EINVAL;
176
177         gralloc_drm_bo_decref(bo);
178
179         return 0;
180 }
181
182 static int drm_mod_alloc_gpu0(alloc_device_t *dev,
183                 int w, int h, int format, int usage,
184                 buffer_handle_t *handle, int *stride)
185 {
186         struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
187         struct gralloc_drm_bo_t *bo;
188         int size, bpp, err;
189
190         bpp = gralloc_drm_get_bpp(format);
191         if (!bpp)
192                 return -EINVAL;
193
194         bo = gralloc_drm_bo_create(dmod->drm, w, h, format, usage);
195         if (!bo)
196                 return -ENOMEM;
197
198         if (gralloc_drm_bo_need_fb(bo)) {
199                 err = gralloc_drm_bo_add_fb(bo);
200                 if (err) {
201                         ALOGE("failed to add fb");
202                         gralloc_drm_bo_decref(bo);
203                         return err;
204                 }
205         }
206
207         *handle = gralloc_drm_bo_get_handle(bo, stride);
208         /* in pixels */
209         *stride /= bpp;
210
211         return 0;
212 }
213
214 static int drm_mod_open_gpu0(struct drm_module_t *dmod, hw_device_t **dev)
215 {
216         struct alloc_device_t *alloc;
217         int err;
218
219         err = drm_init(dmod, 0);
220         if (err)
221                 return err;
222
223         alloc = calloc(1, sizeof(*alloc));
224         if (!alloc)
225                 return -EINVAL;
226
227         alloc->common.tag = HARDWARE_DEVICE_TAG;
228         alloc->common.version = 0;
229         alloc->common.module = &dmod->base.common;
230         alloc->common.close = drm_mod_close_gpu0;
231
232         alloc->alloc = drm_mod_alloc_gpu0;
233         alloc->free = drm_mod_free_gpu0;
234
235         *dev = &alloc->common;
236
237         return 0;
238 }
239
240 static int drm_mod_close_fb0(struct hw_device_t *dev)
241 {
242         struct framebuffer_device_t *fb = (struct framebuffer_device_t *) dev;
243
244         free(fb);
245
246         return 0;
247 }
248
249 static int drm_mod_set_swap_interval_fb0(struct framebuffer_device_t *fb,
250                 int interval)
251 {
252         if (interval < fb->minSwapInterval || interval > fb->maxSwapInterval)
253                 return -EINVAL;
254         return 0;
255 }
256
257 static int drm_mod_post_fb0(struct framebuffer_device_t *fb,
258                 buffer_handle_t handle)
259 {
260         struct drm_module_t *dmod = (struct drm_module_t *) fb->common.module;
261         struct gralloc_drm_bo_t *bo;
262
263         bo = gralloc_drm_bo_from_handle(handle);
264         if (!bo)
265                 return -EINVAL;
266
267         return gralloc_drm_bo_post(bo);
268 }
269
270 #include <GLES/gl.h>
271 static int drm_mod_composition_complete_fb0(struct framebuffer_device_t *fb)
272 {
273         struct drm_module_t *dmod = (struct drm_module_t *) fb->common.module;
274
275         if (gralloc_drm_is_kms_pipelined(dmod->drm))
276                 glFlush();
277         else
278                 glFinish();
279
280         return 0;
281 }
282
283 static int drm_mod_open_fb0(struct drm_module_t *dmod, struct hw_device_t **dev)
284 {
285         struct framebuffer_device_t *fb;
286         int err;
287
288         err = drm_init(dmod, 1);
289         if (err)
290                 return err;
291
292         fb = calloc(1, sizeof(*fb));
293         if (!fb)
294                 return -ENOMEM;
295
296         fb->common.tag = HARDWARE_DEVICE_TAG;
297         fb->common.version = 0;
298         fb->common.module = &dmod->base.common;
299         fb->common.close = drm_mod_close_fb0;
300
301         fb->setSwapInterval = drm_mod_set_swap_interval_fb0;
302         fb->post = drm_mod_post_fb0;
303         fb->compositionComplete = drm_mod_composition_complete_fb0;
304
305         gralloc_drm_get_kms_info(dmod->drm, fb);
306
307         *dev = &fb->common;
308
309         ALOGI("mode.hdisplay %d\n"
310              "mode.vdisplay %d\n"
311              "mode.vrefresh %f\n"
312              "format 0x%x\n"
313              "xdpi %f\n"
314              "ydpi %f\n",
315              fb->width,
316              fb->height,
317              fb->fps,
318              fb->format,
319              fb->xdpi, fb->ydpi);
320
321         return 0;
322 }
323
324 static int drm_mod_open(const struct hw_module_t *mod,
325                 const char *name, struct hw_device_t **dev)
326 {
327         struct drm_module_t *dmod = (struct drm_module_t *) mod;
328         int err;
329
330         if (strcmp(name, GRALLOC_HARDWARE_GPU0) == 0)
331                 err = drm_mod_open_gpu0(dmod, dev);
332         else if (strcmp(name, GRALLOC_HARDWARE_FB0) == 0)
333                 err = drm_mod_open_fb0(dmod, dev);
334         else
335                 err = -EINVAL;
336
337         return err;
338 }
339
340 static struct hw_module_methods_t drm_mod_methods = {
341         .open = drm_mod_open
342 };
343
344 struct drm_module_t HAL_MODULE_INFO_SYM = {
345         .base = {
346                 .common = {
347                         .tag = HARDWARE_MODULE_TAG,
348                         .version_major = 1,
349                         .version_minor = 0,
350                         .id = GRALLOC_HARDWARE_MODULE_ID,
351                         .name = "DRM Memory Allocator",
352                         .author = "Chia-I Wu",
353                         .methods = &drm_mod_methods
354                 },
355                 .registerBuffer = drm_mod_register_buffer,
356                 .unregisterBuffer = drm_mod_unregister_buffer,
357                 .lock = drm_mod_lock,
358                 .unlock = drm_mod_unlock,
359                 .perform = drm_mod_perform
360         },
361         .mutex = PTHREAD_MUTEX_INITIALIZER,
362         .drm = NULL
363 };