2 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3 * Copyright (C) 2010-2011 LunarG Inc.
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:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
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.
24 #define LOG_TAG "GRALLOC-MOD"
26 #include <cutils/log.h>
32 #include "gralloc_drm.h"
35 gralloc_module_t base;
37 pthread_mutex_t mutex;
38 struct gralloc_drm_t *drm;
42 * Initialize the DRM device object, optionally with KMS.
44 static int drm_init(struct drm_module_t *dmod, int kms)
48 pthread_mutex_lock(&dmod->mutex);
50 dmod->drm = gralloc_drm_create();
55 err = gralloc_drm_init_kms(dmod->drm);
56 pthread_mutex_unlock(&dmod->mutex);
61 static int drm_mod_perform(const struct gralloc_module_t *mod, int op, ...)
63 struct drm_module_t *dmod = (struct drm_module_t *) mod;
67 err = drm_init(dmod, 0);
73 case GRALLOC_MODULE_PERFORM_GET_DRM_FD:
75 int *fd = va_arg(args, int *);
76 *fd = gralloc_drm_get_fd(dmod->drm);
80 /* should we remove this and next ops, and make it transparent? */
81 case GRALLOC_MODULE_PERFORM_GET_DRM_MAGIC:
83 int32_t *magic = va_arg(args, int32_t *);
84 err = gralloc_drm_get_magic(dmod->drm, magic);
87 case GRALLOC_MODULE_PERFORM_AUTH_DRM_MAGIC:
89 int32_t magic = va_arg(args, int32_t);
90 err = gralloc_drm_auth_magic(dmod->drm, magic);
93 case GRALLOC_MODULE_PERFORM_ENTER_VT:
95 err = gralloc_drm_set_master(dmod->drm);
98 case GRALLOC_MODULE_PERFORM_LEAVE_VT:
100 gralloc_drm_drop_master(dmod->drm);
113 static int drm_mod_register_buffer(const gralloc_module_t *mod,
114 buffer_handle_t handle)
116 struct drm_module_t *dmod = (struct drm_module_t *) mod;
119 err = drm_init(dmod, 0);
123 return gralloc_drm_handle_register(handle, dmod->drm);
126 static int drm_mod_unregister_buffer(const gralloc_module_t *mod,
127 buffer_handle_t handle)
129 return gralloc_drm_handle_unregister(handle);
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)
135 struct gralloc_drm_bo_t *bo;
138 bo = gralloc_drm_bo_from_handle(handle);
142 return gralloc_drm_bo_lock(bo, usage, x, y, w, h, ptr);
145 static int drm_mod_unlock(const gralloc_module_t *mod, buffer_handle_t handle)
147 struct drm_module_t *dmod = (struct drm_module_t *) mod;
148 struct gralloc_drm_bo_t *bo;
150 bo = gralloc_drm_bo_from_handle(handle);
154 gralloc_drm_bo_unlock(bo);
159 static int drm_mod_close_gpu0(struct hw_device_t *dev)
161 struct alloc_device_t *alloc = (struct alloc_device_t *) dev;
168 static int drm_mod_free_gpu0(alloc_device_t *dev, buffer_handle_t handle)
170 struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
171 struct gralloc_drm_bo_t *bo;
173 bo = gralloc_drm_bo_from_handle(handle);
177 gralloc_drm_bo_decref(bo);
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)
186 struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
187 struct gralloc_drm_bo_t *bo;
190 bpp = gralloc_drm_get_bpp(format);
194 bo = gralloc_drm_bo_create(dmod->drm, w, h, format, usage);
198 if (gralloc_drm_bo_need_fb(bo)) {
199 err = gralloc_drm_bo_add_fb(bo);
201 ALOGE("failed to add fb");
202 gralloc_drm_bo_decref(bo);
207 *handle = gralloc_drm_bo_get_handle(bo, stride);
214 static int drm_mod_open_gpu0(struct drm_module_t *dmod, hw_device_t **dev)
216 struct alloc_device_t *alloc;
219 err = drm_init(dmod, 0);
223 alloc = calloc(1, sizeof(*alloc));
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;
232 alloc->alloc = drm_mod_alloc_gpu0;
233 alloc->free = drm_mod_free_gpu0;
235 *dev = &alloc->common;
240 static int drm_mod_close_fb0(struct hw_device_t *dev)
242 struct framebuffer_device_t *fb = (struct framebuffer_device_t *) dev;
249 static int drm_mod_set_swap_interval_fb0(struct framebuffer_device_t *fb,
252 if (interval < fb->minSwapInterval || interval > fb->maxSwapInterval)
257 static int drm_mod_post_fb0(struct framebuffer_device_t *fb,
258 buffer_handle_t handle)
260 struct drm_module_t *dmod = (struct drm_module_t *) fb->common.module;
261 struct gralloc_drm_bo_t *bo;
263 bo = gralloc_drm_bo_from_handle(handle);
267 return gralloc_drm_bo_post(bo);
271 static int drm_mod_composition_complete_fb0(struct framebuffer_device_t *fb)
273 struct drm_module_t *dmod = (struct drm_module_t *) fb->common.module;
275 if (gralloc_drm_is_kms_pipelined(dmod->drm))
283 static int drm_mod_open_fb0(struct drm_module_t *dmod, struct hw_device_t **dev)
285 struct framebuffer_device_t *fb;
288 err = drm_init(dmod, 1);
292 fb = calloc(1, sizeof(*fb));
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;
301 fb->setSwapInterval = drm_mod_set_swap_interval_fb0;
302 fb->post = drm_mod_post_fb0;
303 fb->compositionComplete = drm_mod_composition_complete_fb0;
305 gralloc_drm_get_kms_info(dmod->drm, fb);
309 ALOGI("mode.hdisplay %d\n"
324 static int drm_mod_open(const struct hw_module_t *mod,
325 const char *name, struct hw_device_t **dev)
327 struct drm_module_t *dmod = (struct drm_module_t *) mod;
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);
340 static struct hw_module_methods_t drm_mod_methods = {
344 struct drm_module_t HAL_MODULE_INFO_SYM = {
347 .tag = HARDWARE_MODULE_TAG,
350 .id = GRALLOC_HARDWARE_MODULE_ID,
351 .name = "DRM Memory Allocator",
352 .author = "Chia-I Wu",
353 .methods = &drm_mod_methods
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
361 .mutex = PTHREAD_MUTEX_INITIALIZER,