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-DRM"
26 #include <cutils/log.h>
27 #include <cutils/atomic.h>
30 #include <sys/types.h>
34 #include "gralloc_drm.h"
35 #include "gralloc_drm_priv.h"
37 #define unlikely(x) __builtin_expect(!!(x), 0)
39 #define GRALLOC_DRM_DEVICE "/dev/dri/card0"
41 static int32_t gralloc_drm_pid = 0;
44 * Return the pid of the process.
46 static int gralloc_drm_get_pid(void)
48 if (unlikely(!gralloc_drm_pid))
49 android_atomic_write((int32_t) getpid(), &gralloc_drm_pid);
51 return gralloc_drm_pid;
55 * Create the driver for a DRM fd.
57 static struct gralloc_drm_drv_t *
58 init_drv_from_fd(int fd)
60 struct gralloc_drm_drv_t *drv = NULL;
61 drmVersionPtr version;
63 /* get the kernel module name */
64 version = drmGetVersion(fd);
66 ALOGE("invalid DRM fd");
72 drv = gralloc_drm_drv_create_for_pipe(fd, version->name);
76 if (!drv && !strcmp(version->name, "i915"))
77 drv = gralloc_drm_drv_create_for_intel(fd);
80 if (!drv && !strcmp(version->name, "radeon"))
81 drv = gralloc_drm_drv_create_for_radeon(fd);
84 if (!drv && !strcmp(version->name, "nouveau"))
85 drv = gralloc_drm_drv_create_for_nouveau(fd);
90 ALOGE("unsupported driver: %s", (version->name) ?
91 version->name : "NULL");
94 drmFreeVersion(version);
100 * Create a DRM device object.
102 struct gralloc_drm_t *gralloc_drm_create(void)
104 struct gralloc_drm_t *drm;
107 drm = calloc(1, sizeof(*drm));
111 drm->fd = open(GRALLOC_DRM_DEVICE, O_RDWR);
113 ALOGE("failed to open %s", GRALLOC_DRM_DEVICE);
117 drm->drv = init_drv_from_fd(drm->fd);
128 * Destroy a DRM device object.
130 void gralloc_drm_destroy(struct gralloc_drm_t *drm)
133 drm->drv->destroy(drm->drv);
139 * Get the file descriptor of a DRM device object.
141 int gralloc_drm_get_fd(struct gralloc_drm_t *drm)
147 * Get the magic for authentication.
149 int gralloc_drm_get_magic(struct gralloc_drm_t *drm, int32_t *magic)
151 return drmGetMagic(drm->fd, (drm_magic_t *) magic);
155 * Authenticate a magic.
157 int gralloc_drm_auth_magic(struct gralloc_drm_t *drm, int32_t magic)
159 return drmAuthMagic(drm->fd, (drm_magic_t) magic);
163 * Set as the master of a DRM device.
165 int gralloc_drm_set_master(struct gralloc_drm_t *drm)
168 drmSetMaster(drm->fd);
175 * Drop from the master of a DRM device.
177 void gralloc_drm_drop_master(struct gralloc_drm_t *drm)
179 drmDropMaster(drm->fd);
183 * Validate a buffer handle and return the associated bo.
185 static struct gralloc_drm_bo_t *validate_handle(buffer_handle_t _handle,
186 struct gralloc_drm_t *drm)
188 struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
193 /* the buffer handle is passed to a new process */
194 if (unlikely(handle->data_owner != gralloc_drm_pid)) {
195 struct gralloc_drm_bo_t *bo;
201 /* create the struct gralloc_drm_bo_t locally */
203 bo = drm->drv->alloc(drm->drv, handle);
204 else /* an invalid handle */
212 handle->data_owner = gralloc_drm_get_pid();
213 handle->data = (int) bo;
216 return (struct gralloc_drm_bo_t *) handle->data;
220 * Register a buffer handle.
222 int gralloc_drm_handle_register(buffer_handle_t handle, struct gralloc_drm_t *drm)
224 return (validate_handle(handle, drm)) ? 0 : -EINVAL;
228 * Unregister a buffer handle. It is no-op for handles created locally.
230 int gralloc_drm_handle_unregister(buffer_handle_t handle)
232 struct gralloc_drm_bo_t *bo;
234 bo = validate_handle(handle, NULL);
239 gralloc_drm_bo_decref(bo);
245 * Create a buffer handle.
247 static struct gralloc_drm_handle_t *create_bo_handle(int width,
248 int height, int format, int usage)
250 struct gralloc_drm_handle_t *handle;
252 handle = calloc(1, sizeof(*handle));
256 handle->base.version = sizeof(handle->base);
257 handle->base.numInts = GRALLOC_DRM_HANDLE_NUM_INTS;
258 handle->base.numFds = GRALLOC_DRM_HANDLE_NUM_FDS;
260 handle->magic = GRALLOC_DRM_HANDLE_MAGIC;
261 handle->width = width;
262 handle->height = height;
263 handle->format = format;
264 handle->usage = usage;
265 handle->plane_mask = 0;
273 struct gralloc_drm_bo_t *gralloc_drm_bo_create(struct gralloc_drm_t *drm,
274 int width, int height, int format, int usage)
276 struct gralloc_drm_bo_t *bo;
277 struct gralloc_drm_handle_t *handle;
279 handle = create_bo_handle(width, height, format, usage);
283 handle->plane_mask = planes_for_format(drm, format);
285 bo = drm->drv->alloc(drm->drv, handle);
297 handle->data_owner = gralloc_drm_get_pid();
298 handle->data = (int) bo;
306 static void gralloc_drm_bo_destroy(struct gralloc_drm_bo_t *bo)
308 struct gralloc_drm_handle_t *handle = bo->handle;
309 int imported = bo->imported;
311 /* gralloc still has a reference */
315 gralloc_drm_bo_rm_fb(bo);
317 bo->drm->drv->free(bo->drm->drv, bo);
319 handle->data_owner = 0;
328 * Decrease refcount, if no refs anymore then destroy.
330 void gralloc_drm_bo_decref(struct gralloc_drm_bo_t *bo)
333 gralloc_drm_bo_destroy(bo);
337 * Return the bo of a registered handle.
339 struct gralloc_drm_bo_t *gralloc_drm_bo_from_handle(buffer_handle_t handle)
341 return validate_handle(handle, NULL);
345 * Get the buffer handle and stride of a bo.
347 buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride)
350 *stride = bo->handle->stride;
351 return &bo->handle->base;
354 int gralloc_drm_get_gem_handle(buffer_handle_t _handle)
356 struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
357 return (handle) ? handle->name : 0;
361 * Query YUV component offsets for a buffer handle
363 void gralloc_drm_resolve_format(buffer_handle_t _handle,
364 uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
366 struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
367 struct gralloc_drm_bo_t *bo = (struct gralloc_drm_bo_t *) handle->data;
368 struct gralloc_drm_t *drm = bo->drm;
370 /* if handle exists and driver implements resolve_format */
371 if (handle && drm->drv->resolve_format)
372 drm->drv->resolve_format(drm->drv, bo,
373 pitches, offsets, handles);
377 * Lock a bo. XXX thread-safety?
379 int gralloc_drm_bo_lock(struct gralloc_drm_bo_t *bo,
380 int usage, int x, int y, int w, int h,
383 if ((bo->handle->usage & usage) != usage) {
384 /* make FB special for testing software renderer with */
385 if (!(bo->handle->usage & GRALLOC_USAGE_HW_FB))
389 /* allow multiple locks with compatible usages */
390 if (bo->lock_count && (bo->locked_for & usage) != usage)
393 usage |= bo->locked_for;
395 if (usage & (GRALLOC_USAGE_SW_WRITE_MASK |
396 GRALLOC_USAGE_SW_READ_MASK)) {
397 /* the driver is supposed to wait for the bo */
398 int write = !!(usage & GRALLOC_USAGE_SW_WRITE_MASK);
399 int err = bo->drm->drv->map(bo->drm->drv, bo,
400 x, y, w, h, write, addr);
405 /* kernel handles the synchronization here */
409 bo->locked_for |= usage;
417 void gralloc_drm_bo_unlock(struct gralloc_drm_bo_t *bo)
419 int mapped = bo->locked_for &
420 (GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_SW_READ_MASK);
426 bo->drm->drv->unmap(bo->drm->drv, bo);