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"
33 #include "gralloc_drm_priv.h"
36 * Initialize the DRM device object, optionally with KMS.
38 static int drm_init(struct drm_module_t *dmod, int kms)
42 pthread_mutex_lock(&dmod->mutex);
44 dmod->drm = gralloc_drm_create();
49 err = gralloc_drm_init_kms(dmod->drm);
50 pthread_mutex_unlock(&dmod->mutex);
55 static int drm_mod_perform(const struct gralloc_module_t *mod, int op, ...)
57 struct drm_module_t *dmod = (struct drm_module_t *) mod;
61 err = drm_init(dmod, 0);
67 case GRALLOC_MODULE_PERFORM_GET_DRM_FD:
69 int *fd = va_arg(args, int *);
70 *fd = gralloc_drm_get_fd(dmod->drm);
74 /* should we remove this and next ops, and make it transparent? */
75 case GRALLOC_MODULE_PERFORM_GET_DRM_MAGIC:
77 int32_t *magic = va_arg(args, int32_t *);
78 err = gralloc_drm_get_magic(dmod->drm, magic);
81 case GRALLOC_MODULE_PERFORM_AUTH_DRM_MAGIC:
83 int32_t magic = va_arg(args, int32_t);
84 err = gralloc_drm_auth_magic(dmod->drm, magic);
87 case GRALLOC_MODULE_PERFORM_ENTER_VT:
89 err = gralloc_drm_set_master(dmod->drm);
92 case GRALLOC_MODULE_PERFORM_LEAVE_VT:
94 gralloc_drm_drop_master(dmod->drm);
107 static int drm_mod_register_buffer(const gralloc_module_t *mod,
108 buffer_handle_t handle)
110 struct drm_module_t *dmod = (struct drm_module_t *) mod;
113 err = drm_init(dmod, 0);
117 return gralloc_drm_handle_register(handle, dmod->drm);
120 static int drm_mod_unregister_buffer(const gralloc_module_t *mod,
121 buffer_handle_t handle)
123 return gralloc_drm_handle_unregister(handle);
126 static int drm_mod_lock(const gralloc_module_t *mod, buffer_handle_t handle,
127 int usage, int x, int y, int w, int h, void **ptr)
129 struct gralloc_drm_bo_t *bo;
132 bo = gralloc_drm_bo_from_handle(handle);
136 return gralloc_drm_bo_lock(bo, usage, x, y, w, h, ptr);
139 static int drm_mod_unlock(const gralloc_module_t *mod, buffer_handle_t handle)
141 struct drm_module_t *dmod = (struct drm_module_t *) mod;
142 struct gralloc_drm_bo_t *bo;
144 bo = gralloc_drm_bo_from_handle(handle);
148 gralloc_drm_bo_unlock(bo);
153 static int drm_mod_close_gpu0(struct hw_device_t *dev)
155 struct alloc_device_t *alloc = (struct alloc_device_t *) dev;
162 static int drm_mod_free_gpu0(alloc_device_t *dev, buffer_handle_t handle)
164 struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
165 struct gralloc_drm_bo_t *bo;
167 bo = gralloc_drm_bo_from_handle(handle);
171 gralloc_drm_bo_decref(bo);
176 static int drm_mod_alloc_gpu0(alloc_device_t *dev,
177 int w, int h, int format, int usage,
178 buffer_handle_t *handle, int *stride)
180 struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
181 struct gralloc_drm_bo_t *bo;
184 bpp = gralloc_drm_get_bpp(format);
188 bo = gralloc_drm_bo_create(dmod->drm, w, h, format, usage);
192 if (gralloc_drm_bo_need_fb(bo)) {
193 err = gralloc_drm_bo_add_fb(bo);
195 ALOGE("failed to add fb");
196 gralloc_drm_bo_decref(bo);
201 *handle = gralloc_drm_bo_get_handle(bo, stride);
208 static int drm_mod_open_gpu0(struct drm_module_t *dmod, hw_device_t **dev)
210 struct alloc_device_t *alloc;
213 err = drm_init(dmod, 0);
217 alloc = calloc(1, sizeof(*alloc));
221 alloc->common.tag = HARDWARE_DEVICE_TAG;
222 alloc->common.version = 0;
223 alloc->common.module = &dmod->base.common;
224 alloc->common.close = drm_mod_close_gpu0;
226 alloc->alloc = drm_mod_alloc_gpu0;
227 alloc->free = drm_mod_free_gpu0;
229 *dev = &alloc->common;
234 static int drm_mod_close_fb0(struct hw_device_t *dev)
236 struct framebuffer_device_t *fb = (struct framebuffer_device_t *) dev;
243 static int drm_mod_set_swap_interval_fb0(struct framebuffer_device_t *fb,
246 if (interval < fb->minSwapInterval || interval > fb->maxSwapInterval)
251 static int drm_mod_post_fb0(struct framebuffer_device_t *fb,
252 buffer_handle_t handle)
254 struct drm_module_t *dmod = (struct drm_module_t *) fb->common.module;
255 struct gralloc_drm_bo_t *bo;
257 bo = gralloc_drm_bo_from_handle(handle);
261 return gralloc_drm_bo_post(bo);
265 static int drm_mod_composition_complete_fb0(struct framebuffer_device_t *fb)
267 struct drm_module_t *dmod = (struct drm_module_t *) fb->common.module;
269 if (gralloc_drm_is_kms_pipelined(dmod->drm))
277 static int drm_mod_open_fb0(struct drm_module_t *dmod, struct hw_device_t **dev)
279 struct framebuffer_device_t *fb;
282 err = drm_init(dmod, 1);
286 fb = calloc(1, sizeof(*fb));
290 fb->common.tag = HARDWARE_DEVICE_TAG;
291 fb->common.version = 0;
292 fb->common.module = &dmod->base.common;
293 fb->common.close = drm_mod_close_fb0;
295 fb->setSwapInterval = drm_mod_set_swap_interval_fb0;
296 fb->post = drm_mod_post_fb0;
297 fb->compositionComplete = drm_mod_composition_complete_fb0;
299 gralloc_drm_get_kms_info(dmod->drm, fb);
303 ALOGI("mode.hdisplay %d\n"
318 static int drm_mod_open(const struct hw_module_t *mod,
319 const char *name, struct hw_device_t **dev)
321 struct drm_module_t *dmod = (struct drm_module_t *) mod;
324 if (strcmp(name, GRALLOC_HARDWARE_GPU0) == 0)
325 err = drm_mod_open_gpu0(dmod, dev);
326 else if (strcmp(name, GRALLOC_HARDWARE_FB0) == 0)
327 err = drm_mod_open_fb0(dmod, dev);
334 static struct hw_module_methods_t drm_mod_methods = {
338 struct drm_module_t HAL_MODULE_INFO_SYM = {
341 .tag = HARDWARE_MODULE_TAG,
344 .id = GRALLOC_HARDWARE_MODULE_ID,
345 .name = "DRM Memory Allocator",
346 .author = "Chia-I Wu",
347 .methods = &drm_mod_methods
349 .registerBuffer = drm_mod_register_buffer,
350 .unregisterBuffer = drm_mod_unregister_buffer,
351 .lock = drm_mod_lock,
352 .unlock = drm_mod_unlock,
353 .perform = drm_mod_perform
355 .hwc_reserve_plane = gralloc_drm_reserve_plane,
356 .hwc_disable_planes = gralloc_drm_disable_planes,
357 .hwc_set_plane_handle = gralloc_drm_set_plane_handle,
359 .mutex = PTHREAD_MUTEX_INITIALIZER,