2 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3 * Copyright (C) 2010-2011 LunarG Inc.
4 * Copyright (C) 2016 Linaro, Ltd., Rob Herring <robh@kernel.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 #define LOG_TAG "GRALLOC-GBM"
27 #include <cutils/log.h>
28 #include <cutils/atomic.h>
29 #include <cutils/properties.h>
32 #include <sys/types.h>
37 #include <hardware/gralloc.h>
38 #include <system/graphics.h>
42 #include "gralloc_gbm_priv.h"
43 #include "gralloc_drm_handle.h"
45 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
47 #define unlikely(x) __builtin_expect(!!(x), 0)
49 struct gralloc_gbm_bo_t {
53 struct gralloc_gbm_handle_t *handle;
55 int imported; /* the handle is from a remote proces when true */
60 unsigned int refcount;
63 static int32_t gralloc_gbm_pid = 0;
65 static uint32_t get_gbm_format(int format)
70 case HAL_PIXEL_FORMAT_RGBA_8888:
71 fmt = GBM_FORMAT_ABGR8888;
73 case HAL_PIXEL_FORMAT_RGBX_8888:
74 fmt = GBM_FORMAT_XBGR8888;
76 case HAL_PIXEL_FORMAT_RGB_888:
77 fmt = GBM_FORMAT_RGB888;
79 case HAL_PIXEL_FORMAT_RGB_565:
80 fmt = GBM_FORMAT_RGB565;
82 case HAL_PIXEL_FORMAT_BGRA_8888:
83 fmt = GBM_FORMAT_ARGB8888;
85 case HAL_PIXEL_FORMAT_YV12:
86 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
87 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
96 static unsigned int get_pipe_bind(int usage)
98 unsigned int bind = 0;
100 if (usage & (GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN))
101 bind |= GBM_BO_USE_LINEAR;
102 if (usage & GRALLOC_USAGE_CURSOR)
103 ;//bind |= GBM_BO_USE_CURSOR;
104 if (usage & (GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
105 bind |= GBM_BO_USE_RENDERING;
106 if (usage & GRALLOC_USAGE_HW_FB)
107 bind |= GBM_BO_USE_SCANOUT;
112 static struct gralloc_gbm_bo_t *gbm_import(struct gbm_device *gbm,
113 struct gralloc_gbm_handle_t *handle)
115 struct gralloc_gbm_bo_t *buf;
116 struct gbm_import_fd_data data;
117 int format = get_gbm_format(handle->format);
119 if (handle->prime_fd < 0)
122 buf = new struct gralloc_gbm_bo_t();
124 ALOGE("failed to allocate pipe buffer");
128 data.fd = handle->prime_fd;
129 data.width = handle->width;
130 data.height = handle->height;
131 data.stride = handle->stride;
132 data.format = format;
134 buf->bo = gbm_bo_import(gbm, GBM_BO_IMPORT_FD, &data, 0);
142 static struct gralloc_gbm_bo_t *gbm_alloc(struct gbm_device *gbm,
143 struct gralloc_gbm_handle_t *handle)
145 struct gralloc_gbm_bo_t *buf;
146 int format = get_gbm_format(handle->format);
147 int usage = get_pipe_bind(handle->usage);
150 buf = new struct gralloc_gbm_bo_t();
152 ALOGE("failed to allocate pipe buffer");
156 width = handle->width;
157 height = handle->height;
158 if (usage & GBM_BO_USE_CURSOR) {
159 if (handle->width < 64)
161 if (handle->height < 64)
165 ALOGD("create BO, size=%dx%d, fmt=%d, usage=%x",
166 handle->width, handle->height, handle->format, usage);
167 buf->bo = gbm_bo_create(gbm, width, height, format, usage);
169 ALOGE("failed to create BO, size=%dx%d, fmt=%d, usage=%x",
170 handle->width, handle->height, handle->format, usage);
175 handle->prime_fd = gbm_bo_get_fd(buf->bo);
176 handle->stride = gbm_bo_get_stride(buf->bo);
181 static void gbm_free(struct gralloc_gbm_bo_t *bo)
183 struct gralloc_gbm_handle_t *handle = bo->handle;
185 close(handle->prime_fd);
186 handle->prime_fd = -1;
188 gbm_bo_destroy(bo->bo);
192 static int gbm_map(struct gralloc_gbm_bo_t *bo, int x, int y, int w, int h,
193 int enable_write, void **addr)
196 int flags = GBM_BO_TRANSFER_READ;
203 flags |= GBM_BO_TRANSFER_WRITE;
205 *addr = gbm_bo_map(bo->bo, x, y, w, h, flags, &stride, &bo->map_data);
206 ALOGE("mapped bo %p at %p", bo, *addr);
210 assert(stride == gbm_bo_get_stride(bo));
215 static void gbm_unmap(struct gralloc_gbm_bo_t *bo)
217 gbm_bo_unmap(bo->bo, bo->map_data);
221 void gbm_dev_destroy(struct gbm_device *gbm)
223 int fd = gbm_device_get_fd(gbm);
225 gbm_device_destroy(gbm);
229 struct gbm_device *gbm_dev_create(void)
231 struct gbm_device *gbm;
232 char path[PROPERTY_VALUE_MAX];
235 property_get("gralloc.gbm.device", path, "/dev/dri/renderD128");
236 fd = open(path, O_RDWR | O_CLOEXEC);
238 ALOGE("failed to open %s", path);
242 gbm = gbm_create_device(fd);
244 ALOGE("failed to create gbm device");
252 * Return the pid of the process.
254 static int gralloc_gbm_get_pid(void)
256 if (unlikely(!gralloc_gbm_pid))
257 android_atomic_write((int32_t) getpid(), &gralloc_gbm_pid);
259 return gralloc_gbm_pid;
263 * Validate a buffer handle and return the associated bo.
265 static struct gralloc_gbm_bo_t *validate_handle(buffer_handle_t _handle,
266 struct gbm_device *gbm)
268 struct gralloc_gbm_bo_t *bo;
269 struct gralloc_gbm_handle_t *handle = gralloc_gbm_handle(_handle);
274 /* the buffer handle is passed to a new process */
275 //ALOGE("data_owner=%d gralloc_pid=%d data=%p\n", handle->data_owner, gralloc_gbm_get_pid(), handle->data);
276 if (handle->data_owner == gralloc_gbm_get_pid())
277 return (struct gralloc_gbm_bo_t *)handle->data;
283 ALOGE("handle: name=%d pfd=%d\n", handle->name,
285 /* create the struct gralloc_gbm_bo_t locally */
286 if (handle->name || handle->prime_fd >= 0)
287 bo = gbm_import(gbm, handle);
288 else /* an invalid handle */
296 handle->data_owner = gralloc_gbm_get_pid();
303 * Register a buffer handle.
305 int gralloc_gbm_handle_register(buffer_handle_t handle, struct gbm_device *gbm)
307 return (validate_handle(handle, gbm)) ? 0 : -EINVAL;
311 * Unregister a buffer handle. It is no-op for handles created locally.
313 int gralloc_gbm_handle_unregister(buffer_handle_t handle)
315 struct gralloc_gbm_bo_t *bo;
317 bo = validate_handle(handle, NULL);
322 gralloc_gbm_bo_decref(bo);
328 * Create a buffer handle.
330 static struct gralloc_gbm_handle_t *create_bo_handle(int width,
331 int height, int format, int usage)
333 struct gralloc_gbm_handle_t *handle;
335 handle = new gralloc_gbm_handle_t();
339 handle->base.version = sizeof(handle->base);
340 handle->base.numInts = GRALLOC_GBM_HANDLE_NUM_INTS;
341 handle->base.numFds = GRALLOC_GBM_HANDLE_NUM_FDS;
343 handle->magic = GRALLOC_GBM_HANDLE_MAGIC;
344 handle->width = width;
345 handle->height = height;
346 handle->format = format;
347 handle->usage = usage;
348 handle->prime_fd = -1;
356 struct gralloc_gbm_bo_t *gralloc_gbm_bo_create(struct gbm_device *gbm,
357 int width, int height, int format, int usage)
359 struct gralloc_gbm_bo_t *bo;
360 struct gralloc_gbm_handle_t *handle;
362 handle = create_bo_handle(width, height, format, usage);
366 bo = gbm_alloc(gbm, handle);
376 handle->data_owner = gralloc_gbm_get_pid();
385 static void gralloc_gbm_bo_destroy(struct gralloc_gbm_bo_t *bo)
387 struct gralloc_gbm_handle_t *handle = bo->handle;
388 int imported = bo->imported;
390 /* gralloc still has a reference */
396 handle->data_owner = 0;
405 * Decrease refcount, if no refs anymore then destroy.
407 void gralloc_gbm_bo_decref(struct gralloc_gbm_bo_t *bo)
410 gralloc_gbm_bo_destroy(bo);
414 * Return the bo of a registered handle.
416 struct gralloc_gbm_bo_t *gralloc_gbm_bo_from_handle(buffer_handle_t handle)
418 return validate_handle(handle, NULL);
422 * Get the buffer handle and stride of a bo.
424 buffer_handle_t gralloc_gbm_bo_get_handle(struct gralloc_gbm_bo_t *bo)
426 return &bo->handle->base;
430 * Get the buffer handle and stride of a bo.
432 struct gbm_bo *gralloc_gbm_bo_to_gbm_bo(struct gralloc_gbm_bo_t *_bo)
438 * Lock a bo. XXX thread-safety?
440 int gralloc_gbm_bo_lock(struct gralloc_gbm_bo_t *bo,
441 int usage, int x, int y, int w, int h,
444 if ((bo->handle->usage & usage) != usage) {
445 /* make FB special for testing software renderer with */
447 if (!(bo->handle->usage & GRALLOC_USAGE_SW_READ_OFTEN) &&
448 !(bo->handle->usage & GRALLOC_USAGE_HW_FB) &&
449 !(bo->handle->usage & GRALLOC_USAGE_HW_TEXTURE)) {
450 ALOGE("bo.usage:x%X/usage:x%X is not GRALLOC_USAGE_HW_FB or GRALLOC_USAGE_HW_TEXTURE",
451 bo->handle->usage, usage);
456 /* allow multiple locks with compatible usages */
457 if (bo->lock_count && (bo->locked_for & usage) != usage)
460 usage |= bo->locked_for;
462 if (usage & (GRALLOC_USAGE_SW_WRITE_MASK |
463 GRALLOC_USAGE_SW_READ_MASK)) {
464 /* the driver is supposed to wait for the bo */
465 int write = !!(usage & GRALLOC_USAGE_SW_WRITE_MASK);
466 int err = gbm_map(bo, x, y, w, h, write, addr);
471 /* kernel handles the synchronization here */
475 bo->locked_for |= usage;
483 void gralloc_gbm_bo_unlock(struct gralloc_gbm_bo_t *bo)
485 int mapped = bo->locked_for &
486 (GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_SW_READ_MASK);