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 static int32_t gralloc_drm_pid = 0;
42 * Return the pid of the process.
44 static int gralloc_drm_get_pid(void)
46 if (unlikely(!gralloc_drm_pid))
47 android_atomic_write((int32_t) getpid(), &gralloc_drm_pid);
49 return gralloc_drm_pid;
53 * Create the driver for a DRM fd.
55 static struct gralloc_drm_drv_t *
56 init_drv_from_fd(int fd)
58 struct gralloc_drm_drv_t *drv = NULL;
59 drmVersionPtr version;
61 /* get the kernel module name */
62 version = drmGetVersion(fd);
64 ALOGE("invalid DRM fd");
69 #ifdef ENABLE_FREEDRENO
70 if (!strcmp(version->name, "msm")) {
71 drv = gralloc_drm_drv_create_for_freedreno(fd);
72 ALOGI_IF(drv, "create freedreno for driver msm");
76 if (!strcmp(version->name, "i915")) {
77 drv = gralloc_drm_drv_create_for_intel(fd);
78 ALOGI_IF(drv, "create intel for driver i915");
82 if (!strcmp(version->name, "radeon")) {
83 drv = gralloc_drm_drv_create_for_radeon(fd);
84 ALOGI_IF(drv, "create radeon for driver radeon");
88 if (!strcmp(version->name, "nouveau")) {
89 drv = gralloc_drm_drv_create_for_nouveau(fd);
90 ALOGI_IF(drv, "create nouveau for driver nouveau");
95 drv = gralloc_drm_drv_create_for_pipe(fd, version->name);
96 ALOGI_IF(drv, "create pipe for driver %s", version->name);
100 ALOGE("unsupported driver: %s", (version->name) ?
101 version->name : "NULL");
105 drmFreeVersion(version);
110 static const char *fbdrv_map[][2] = {
111 { "amdgpudrmfb", "amdgpu" },
112 { "inteldrmfb", "i915" },
113 { "nouveaufb", "nouveau" },
114 { "radeondrmfb", "radeon" },
115 { "svgadrmfb", "vmwgfx" },
116 { "virtiodrmfb", "virtio_gpu" },
120 * Create a DRM device object.
122 struct gralloc_drm_t *gralloc_drm_create(void)
124 struct gralloc_drm_t *drm;
126 unsigned card, ret = 0;
129 if ((fb = fopen("/proc/fb", "r"))) {
130 ret = fscanf(fb, "%u %s", &card, buf);
134 ALOGE("failed to open /proc/fb");
138 drm = calloc(1, sizeof(*drm));
143 for (card = 0; card < sizeof(fbdrv_map) / sizeof(const char *) / 2; ++card) {
144 if (!strcmp(buf, fbdrv_map[card][0])) {
145 drm->fd = drmOpen(fbdrv_map[card][1], NULL);
146 ALOGD("drmOpen %s: %d", fbdrv_map[card][1], drm->fd);
152 ALOGE("failed to open driver for %s", buf);
154 drm->drv = init_drv_from_fd(drm->fd);
167 * Destroy a DRM device object.
169 void gralloc_drm_destroy(struct gralloc_drm_t *drm)
172 drm->drv->destroy(drm->drv);
178 * Get the file descriptor of a DRM device object.
180 int gralloc_drm_get_fd(struct gralloc_drm_t *drm)
186 * Get the magic for authentication.
188 int gralloc_drm_get_magic(struct gralloc_drm_t *drm, int32_t *magic)
190 return drmGetMagic(drm->fd, (drm_magic_t *) magic);
194 * Authenticate a magic.
196 int gralloc_drm_auth_magic(struct gralloc_drm_t *drm, int32_t magic)
198 return drmAuthMagic(drm->fd, (drm_magic_t) magic);
202 * Set as the master of a DRM device.
204 int gralloc_drm_set_master(struct gralloc_drm_t *drm)
207 drmSetMaster(drm->fd);
214 * Drop from the master of a DRM device.
216 void gralloc_drm_drop_master(struct gralloc_drm_t *drm)
218 drmDropMaster(drm->fd);
222 * Validate a buffer handle and return the associated bo.
224 static struct gralloc_drm_bo_t *validate_handle(buffer_handle_t _handle,
225 struct gralloc_drm_t *drm)
227 struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
232 /* the buffer handle is passed to a new process */
233 if (unlikely(handle->data_owner != gralloc_drm_get_pid())) {
234 struct gralloc_drm_bo_t *bo;
240 /* create the struct gralloc_drm_bo_t locally */
242 bo = drm->drv->alloc(drm->drv, handle);
243 else /* an invalid handle */
252 handle->data_owner = gralloc_drm_get_pid();
260 * Register a buffer handle.
262 int gralloc_drm_handle_register(buffer_handle_t handle, struct gralloc_drm_t *drm)
264 struct gralloc_drm_bo_t *bo;
266 bo = validate_handle(handle, drm);
276 * Unregister a buffer handle. It is no-op for handles created locally.
278 int gralloc_drm_handle_unregister(buffer_handle_t handle)
280 struct gralloc_drm_bo_t *bo;
282 bo = validate_handle(handle, NULL);
286 gralloc_drm_bo_decref(bo);
288 gralloc_drm_bo_decref(bo);
294 * Create a buffer handle.
296 static struct gralloc_drm_handle_t *create_bo_handle(int width,
297 int height, int format, int usage)
299 struct gralloc_drm_handle_t *handle;
301 handle = calloc(1, sizeof(*handle));
305 handle->base.version = sizeof(handle->base);
306 handle->base.numInts = GRALLOC_DRM_HANDLE_NUM_INTS;
307 handle->base.numFds = GRALLOC_DRM_HANDLE_NUM_FDS;
309 handle->magic = GRALLOC_DRM_HANDLE_MAGIC;
310 handle->width = width;
311 handle->height = height;
312 handle->format = format;
313 handle->usage = usage;
314 handle->plane_mask = 0;
315 handle->prime_fd = -1;
323 struct gralloc_drm_bo_t *gralloc_drm_bo_create(struct gralloc_drm_t *drm,
324 int width, int height, int format, int usage)
326 struct gralloc_drm_bo_t *bo;
327 struct gralloc_drm_handle_t *handle;
329 handle = create_bo_handle(width, height, format, usage);
333 handle->plane_mask = planes_for_format(drm, format);
335 bo = drm->drv->alloc(drm->drv, handle);
347 handle->data_owner = gralloc_drm_get_pid();
356 static void gralloc_drm_bo_destroy(struct gralloc_drm_bo_t *bo)
358 struct gralloc_drm_handle_t *handle = bo->handle;
359 int imported = bo->imported;
361 /* gralloc still has a reference */
365 gralloc_drm_bo_rm_fb(bo);
367 bo->drm->drv->free(bo->drm->drv, bo);
369 handle->data_owner = 0;
378 * Decrease refcount, if no refs anymore then destroy.
380 void gralloc_drm_bo_decref(struct gralloc_drm_bo_t *bo)
383 gralloc_drm_bo_destroy(bo);
387 * Return the bo of a registered handle.
389 struct gralloc_drm_bo_t *gralloc_drm_bo_from_handle(buffer_handle_t handle)
391 return validate_handle(handle, NULL);
395 * Get the buffer handle and stride of a bo.
397 buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride)
400 *stride = bo->handle->stride;
401 return &bo->handle->base;
404 int gralloc_drm_get_gem_handle(buffer_handle_t _handle)
406 struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
407 return (handle) ? handle->name : 0;
410 int gralloc_drm_get_prime_fd(buffer_handle_t _handle)
412 struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
413 return (handle) ? handle->prime_fd : -1;
417 * Query YUV component offsets for a buffer handle
419 void gralloc_drm_resolve_format(buffer_handle_t _handle,
420 uint32_t *pitches, uint32_t *offsets, uint32_t *handles)
422 struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
423 struct gralloc_drm_bo_t *bo = handle->data;
424 struct gralloc_drm_t *drm = bo->drm;
426 /* if handle exists and driver implements resolve_format */
427 if (handle && drm->drv->resolve_format)
428 drm->drv->resolve_format(drm->drv, bo,
429 pitches, offsets, handles);
433 * Lock a bo. XXX thread-safety?
435 int gralloc_drm_bo_lock(struct gralloc_drm_bo_t *bo,
436 int usage, int x, int y, int w, int h,
439 if ((bo->handle->usage & usage) != usage) {
440 /* make FB special for testing software renderer with */
441 if (!(bo->handle->usage & (
442 GRALLOC_USAGE_SW_READ_OFTEN |
443 GRALLOC_USAGE_HW_FB |
444 GRALLOC_USAGE_HW_TEXTURE |
445 GRALLOC_USAGE_HW_VIDEO_ENCODER))) {
446 ALOGE("bo.usage:x%X/usage:x%X is not GRALLOC_USAGE_HW_{FB,TEXTURE,VIDEO_ENCODER}",
447 bo->handle->usage, usage);
452 /* allow multiple locks with compatible usages */
453 if (bo->lock_count && (bo->locked_for & usage) != usage)
456 usage |= bo->locked_for;
458 if (usage & (GRALLOC_USAGE_SW_WRITE_MASK |
459 GRALLOC_USAGE_SW_READ_MASK)) {
460 /* the driver is supposed to wait for the bo */
461 int write = !!(usage & GRALLOC_USAGE_SW_WRITE_MASK);
462 int err = bo->drm->drv->map(bo->drm->drv, bo,
463 x, y, w, h, write, addr);
468 /* kernel handles the synchronization here */
472 bo->locked_for |= usage;
480 void gralloc_drm_bo_unlock(struct gralloc_drm_bo_t *bo)
482 int mapped = bo->locked_for &
483 (GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_SW_READ_MASK);
489 bo->drm->drv->unmap(bo->drm->drv, bo);