Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 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-DRM" |
| 25 | |
| 26 | #include <cutils/log.h> |
| 27 | #include <cutils/atomic.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <fcntl.h> |
| 33 | |
| 34 | #include "gralloc_drm.h" |
| 35 | #include "gralloc_drm_priv.h" |
| 36 | |
| 37 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 38 | |
| 39 | #define GRALLOC_DRM_DEVICE "/dev/dri/card0" |
| 40 | |
| 41 | static int32_t gralloc_drm_pid = 0; |
| 42 | |
| 43 | /* |
| 44 | * Return the pid of the process. |
| 45 | */ |
| 46 | static int gralloc_drm_get_pid(void) |
| 47 | { |
| 48 | if (unlikely(!gralloc_drm_pid)) |
| 49 | android_atomic_write((int32_t) getpid(), &gralloc_drm_pid); |
| 50 | |
| 51 | return gralloc_drm_pid; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Create the driver for a DRM fd. |
| 56 | */ |
| 57 | static struct gralloc_drm_drv_t * |
| 58 | init_drv_from_fd(int fd) |
| 59 | { |
| 60 | struct gralloc_drm_drv_t *drv = NULL; |
| 61 | drmVersionPtr version; |
| 62 | |
| 63 | /* get the kernel module name */ |
| 64 | version = drmGetVersion(fd); |
| 65 | if (!version) { |
| 66 | LOGE("invalid DRM fd"); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | if (version->name) { |
Chia-I Wu | a020bfa | 2011-06-13 08:48:44 +0800 | [diff] [blame] | 71 | #ifdef ENABLE_PIPE |
| 72 | drv = gralloc_drm_drv_create_for_pipe(fd, version->name); |
| 73 | #endif |
| 74 | |
Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 75 | #ifdef ENABLE_INTEL |
| 76 | if (!drv && !strcmp(version->name, "i915")) |
| 77 | drv = gralloc_drm_drv_create_for_intel(fd); |
| 78 | #endif |
| 79 | #ifdef ENABLE_RADEON |
| 80 | if (!drv && !strcmp(version->name, "radeon")) |
| 81 | drv = gralloc_drm_drv_create_for_radeon(fd); |
| 82 | #endif |
Chia-I Wu | 64345b4 | 2011-06-12 18:43:33 +0800 | [diff] [blame] | 83 | #ifdef ENABLE_NOUVEAU |
| 84 | if (!drv && !strcmp(version->name, "nouveau")) |
| 85 | drv = gralloc_drm_drv_create_for_nouveau(fd); |
| 86 | #endif |
Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (!drv) { |
| 90 | LOGE("unsupported driver: %s", (version->name) ? |
| 91 | version->name : "NULL"); |
| 92 | } |
| 93 | |
| 94 | drmFreeVersion(version); |
| 95 | |
| 96 | return drv; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Create a DRM device object. |
| 101 | */ |
| 102 | struct gralloc_drm_t *gralloc_drm_create(void) |
| 103 | { |
| 104 | struct gralloc_drm_t *drm; |
| 105 | int err; |
| 106 | |
| 107 | drm = calloc(1, sizeof(*drm)); |
| 108 | if (!drm) |
| 109 | return NULL; |
| 110 | |
| 111 | drm->fd = open(GRALLOC_DRM_DEVICE, O_RDWR); |
| 112 | if (drm->fd < 0) { |
| 113 | LOGE("failed to open %s", GRALLOC_DRM_DEVICE); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | drm->drv = init_drv_from_fd(drm->fd); |
| 118 | if (!drm->drv) { |
| 119 | close(drm->fd); |
| 120 | free(drm); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | return drm; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Destroy a DRM device object. |
| 129 | */ |
| 130 | void gralloc_drm_destroy(struct gralloc_drm_t *drm) |
| 131 | { |
| 132 | if (drm->drv) |
| 133 | drm->drv->destroy(drm->drv); |
| 134 | close(drm->fd); |
| 135 | free(drm); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Get the file descriptor of a DRM device object. |
| 140 | */ |
| 141 | int gralloc_drm_get_fd(struct gralloc_drm_t *drm) |
| 142 | { |
| 143 | return drm->fd; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Get the magic for authentication. |
| 148 | */ |
| 149 | int gralloc_drm_get_magic(struct gralloc_drm_t *drm, int32_t *magic) |
| 150 | { |
| 151 | return drmGetMagic(drm->fd, (drm_magic_t *) magic); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Authenticate a magic. |
| 156 | */ |
| 157 | int gralloc_drm_auth_magic(struct gralloc_drm_t *drm, int32_t magic) |
| 158 | { |
| 159 | return drmAuthMagic(drm->fd, (drm_magic_t) magic); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Set as the master of a DRM device. |
| 164 | */ |
| 165 | int gralloc_drm_set_master(struct gralloc_drm_t *drm) |
| 166 | { |
| 167 | LOGD("set master"); |
| 168 | drmSetMaster(drm->fd); |
| 169 | drm->first_post = 1; |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Drop from the master of a DRM device. |
| 176 | */ |
| 177 | void gralloc_drm_drop_master(struct gralloc_drm_t *drm) |
| 178 | { |
| 179 | drmDropMaster(drm->fd); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Create a buffer handle. |
| 184 | */ |
| 185 | static struct gralloc_drm_handle_t *create_bo_handle(int width, |
| 186 | int height, int format, int usage) |
| 187 | { |
| 188 | struct gralloc_drm_handle_t *handle; |
| 189 | |
| 190 | handle = calloc(1, sizeof(*handle)); |
| 191 | if (!handle) |
| 192 | return NULL; |
| 193 | |
| 194 | handle->base.version = sizeof(handle->base); |
| 195 | handle->base.numInts = GRALLOC_DRM_HANDLE_NUM_INTS; |
| 196 | handle->base.numFds = GRALLOC_DRM_HANDLE_NUM_FDS; |
| 197 | |
| 198 | handle->magic = GRALLOC_DRM_HANDLE_MAGIC; |
| 199 | handle->width = width; |
| 200 | handle->height = height; |
| 201 | handle->format = format; |
| 202 | handle->usage = usage; |
| 203 | |
| 204 | return handle; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Create a bo. |
| 209 | */ |
| 210 | struct gralloc_drm_bo_t *gralloc_drm_bo_create(struct gralloc_drm_t *drm, |
| 211 | int width, int height, int format, int usage) |
| 212 | { |
| 213 | struct gralloc_drm_bo_t *bo; |
| 214 | struct gralloc_drm_handle_t *handle; |
| 215 | |
| 216 | handle = create_bo_handle(width, height, format, usage); |
| 217 | if (!handle) |
| 218 | return NULL; |
| 219 | |
| 220 | bo = drm->drv->alloc(drm->drv, handle); |
| 221 | if (!bo) { |
| 222 | free(handle); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | bo->drm = drm; |
| 227 | bo->imported = 0; |
| 228 | bo->handle = handle; |
| 229 | |
| 230 | handle->data_owner = gralloc_drm_get_pid(); |
| 231 | handle->data = (int) bo; |
| 232 | |
| 233 | return bo; |
| 234 | } |
| 235 | |
| 236 | /* |
Chia-I Wu | 2fc5da4 | 2011-07-29 19:57:04 +0900 | [diff] [blame^] | 237 | * Destroy a bo. |
Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 238 | */ |
Chia-I Wu | 2fc5da4 | 2011-07-29 19:57:04 +0900 | [diff] [blame^] | 239 | void gralloc_drm_bo_destroy(struct gralloc_drm_bo_t *bo) |
| 240 | { |
| 241 | struct gralloc_drm_handle_t *handle = bo->handle; |
| 242 | int imported = bo->imported; |
| 243 | |
| 244 | bo->drm->drv->free(bo->drm->drv, bo); |
| 245 | if (imported) { |
| 246 | handle->data_owner = 0; |
| 247 | handle->data = 0; |
| 248 | } |
| 249 | else { |
| 250 | free(handle); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Register a buffer handle and return the associated bo. |
| 256 | */ |
| 257 | struct gralloc_drm_bo_t *gralloc_drm_bo_register(struct gralloc_drm_t *drm, |
| 258 | buffer_handle_t _handle, int create) |
Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 259 | { |
| 260 | struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle); |
| 261 | |
| 262 | /* the buffer handle is passed to a new process */ |
| 263 | if (handle && unlikely(handle->data_owner != gralloc_drm_pid)) { |
| 264 | struct gralloc_drm_bo_t *bo; |
| 265 | |
Chia-I Wu | 2fc5da4 | 2011-07-29 19:57:04 +0900 | [diff] [blame^] | 266 | if (!create) |
| 267 | return NULL; |
| 268 | |
Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 269 | /* create the struct gralloc_drm_bo_t locally */ |
Chia-I Wu | 68ea7da | 2011-07-15 10:46:06 +0800 | [diff] [blame] | 270 | if (handle->name) |
| 271 | bo = drm->drv->alloc(drm->drv, handle); |
| 272 | else /* an invalid handle */ |
| 273 | bo = NULL; |
Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 274 | if (bo) { |
| 275 | bo->drm = drm; |
| 276 | bo->imported = 1; |
| 277 | bo->handle = handle; |
| 278 | } |
| 279 | |
| 280 | handle->data_owner = gralloc_drm_get_pid(); |
| 281 | handle->data = (int) bo; |
| 282 | } |
| 283 | |
| 284 | return (struct gralloc_drm_bo_t *) handle->data; |
| 285 | } |
| 286 | |
| 287 | /* |
Chia-I Wu | 2fc5da4 | 2011-07-29 19:57:04 +0900 | [diff] [blame^] | 288 | * Unregister a bo. It is no-op for bo created locally. |
Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 289 | */ |
Chia-I Wu | 2fc5da4 | 2011-07-29 19:57:04 +0900 | [diff] [blame^] | 290 | void gralloc_drm_bo_unregister(struct gralloc_drm_bo_t *bo) |
Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 291 | { |
Chia-I Wu | 2fc5da4 | 2011-07-29 19:57:04 +0900 | [diff] [blame^] | 292 | if (bo->imported) |
| 293 | gralloc_drm_bo_destroy(bo); |
Chia-I Wu | 2ec32d4 | 2011-06-12 16:21:30 +0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Map a bo for CPU access. |
| 298 | */ |
| 299 | int gralloc_drm_bo_map(struct gralloc_drm_bo_t *bo, |
| 300 | int x, int y, int w, int h, |
| 301 | int enable_write, void **addr) |
| 302 | { |
| 303 | return bo->drm->drv->map(bo->drm->drv, bo, |
| 304 | x, y, w, h, enable_write, addr); |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Unmap a bo. |
| 309 | */ |
| 310 | void gralloc_drm_bo_unmap(struct gralloc_drm_bo_t *bo) |
| 311 | { |
| 312 | bo->drm->drv->unmap(bo->drm->drv, bo); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Get the buffer handle and stride of a bo. |
| 317 | */ |
| 318 | buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride) |
| 319 | { |
| 320 | if (stride) |
| 321 | *stride = bo->handle->stride; |
| 322 | return &bo->handle->base; |
| 323 | } |