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>
33 #include <sys/types.h>
39 #include <hardware/gralloc.h>
40 #include <system/graphics.h>
44 #include "gralloc_gbm_priv.h"
45 #include <android/gralloc_handle.h>
47 #include <unordered_map>
49 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
51 #define unlikely(x) __builtin_expect(!!(x), 0)
53 static std::unordered_map<buffer_handle_t, struct gbm_bo *> gbm_bo_handle_map;
61 void gralloc_gbm_destroy_user_data(struct gbm_bo *bo, void *data)
63 struct bo_data_t *bo_data = (struct bo_data_t *)data;
69 static struct bo_data_t *gbm_bo_data(struct gbm_bo *bo) {
70 return (struct bo_data_t *)gbm_bo_get_user_data(bo);
74 static uint32_t get_gbm_format(int format)
79 case HAL_PIXEL_FORMAT_RGBA_8888:
80 fmt = GBM_FORMAT_ABGR8888;
82 case HAL_PIXEL_FORMAT_RGBX_8888:
83 fmt = GBM_FORMAT_XBGR8888;
85 case HAL_PIXEL_FORMAT_RGB_888:
86 fmt = GBM_FORMAT_RGB888;
88 case HAL_PIXEL_FORMAT_RGB_565:
89 fmt = GBM_FORMAT_RGB565;
91 case HAL_PIXEL_FORMAT_BGRA_8888:
92 fmt = GBM_FORMAT_ARGB8888;
94 case HAL_PIXEL_FORMAT_YV12:
95 /* YV12 is planar, but must be a single buffer so ask for GR88 */
96 fmt = GBM_FORMAT_GR88;
98 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
99 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
108 static int gralloc_gbm_get_bpp(int format)
113 case HAL_PIXEL_FORMAT_RGBA_8888:
114 case HAL_PIXEL_FORMAT_RGBX_8888:
115 case HAL_PIXEL_FORMAT_BGRA_8888:
118 case HAL_PIXEL_FORMAT_RGB_888:
121 case HAL_PIXEL_FORMAT_RGB_565:
122 case HAL_PIXEL_FORMAT_YCbCr_422_I:
125 /* planar; only Y is considered */
126 case HAL_PIXEL_FORMAT_YV12:
127 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
128 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
139 static unsigned int get_pipe_bind(int usage)
141 unsigned int bind = 0;
143 if (usage & (GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN))
144 bind |= GBM_BO_USE_LINEAR;
145 if (usage & GRALLOC_USAGE_CURSOR)
146 ;//bind |= GBM_BO_USE_CURSOR;
147 if (usage & (GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE))
148 bind |= GBM_BO_USE_RENDERING;
149 if (usage & GRALLOC_USAGE_HW_FB)
150 bind |= GBM_BO_USE_SCANOUT;
151 if (usage & GRALLOC_USAGE_HW_COMPOSER)
152 bind |= GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
157 static struct gbm_bo *gbm_import(struct gbm_device *gbm,
158 buffer_handle_t _handle)
161 struct gralloc_handle_t *handle = gralloc_handle(_handle);
162 #ifdef GBM_BO_IMPORT_FD_MODIFIER
163 struct gbm_import_fd_modifier_data data;
165 struct gbm_import_fd_data data;
168 int format = get_gbm_format(handle->format);
169 if (handle->prime_fd < 0)
172 memset(&data, 0, sizeof(data));
173 data.width = handle->width;
174 data.height = handle->height;
175 data.format = format;
176 /* Adjust the width and height for a GBM GR88 buffer */
177 if (handle->format == HAL_PIXEL_FORMAT_YV12) {
179 data.height += handle->height / 2;
182 #ifdef GBM_BO_IMPORT_FD_MODIFIER
184 data.fds[0] = handle->prime_fd;
185 data.strides[0] = handle->stride;
186 data.modifier = handle->modifier;
187 bo = gbm_bo_import(gbm, GBM_BO_IMPORT_FD_MODIFIER, &data, 0);
189 data.fd = handle->prime_fd;
190 data.stride = handle->stride;
191 bo = gbm_bo_import(gbm, GBM_BO_IMPORT_FD, &data, 0);
197 static struct gbm_bo *gbm_alloc(struct gbm_device *gbm,
198 buffer_handle_t _handle)
201 struct gralloc_handle_t *handle = gralloc_handle(_handle);
202 int format = get_gbm_format(handle->format);
203 int usage = get_pipe_bind(handle->usage);
206 width = handle->width;
207 height = handle->height;
208 if (usage & GBM_BO_USE_CURSOR) {
209 if (handle->width < 64)
211 if (handle->height < 64)
216 * For YV12, we request GR88, so halve the width since we're getting
217 * 16bpp. Then increase the height by 1.5 for the U and V planes.
219 if (handle->format == HAL_PIXEL_FORMAT_YV12) {
221 height += handle->height / 2;
224 ALOGV("create BO, size=%dx%d, fmt=%d, usage=%x",
225 handle->width, handle->height, handle->format, usage);
226 bo = gbm_bo_create(gbm, width, height, format, usage);
228 ALOGE("failed to create BO, size=%dx%d, fmt=%d, usage=%x",
229 handle->width, handle->height, handle->format, usage);
233 handle->prime_fd = gbm_bo_get_fd(bo);
234 handle->stride = gbm_bo_get_stride(bo);
235 #ifdef GBM_BO_IMPORT_FD_MODIFIER
236 handle->modifier = gbm_bo_get_modifier(bo);
242 int gbm_free(buffer_handle_t handle)
244 struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
249 gbm_bo_handle_map.erase(handle);
255 * Return the bo of a registered handle.
257 struct gbm_bo *gralloc_gbm_bo_from_handle(buffer_handle_t handle)
259 return gbm_bo_handle_map[handle];
262 static int gbm_map(buffer_handle_t handle, int x, int y, int w, int h,
263 int enable_write, void **addr)
266 int flags = GBM_BO_TRANSFER_READ;
267 struct gralloc_gbm_handle_t *gbm_handle = gralloc_handle(handle);
268 struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
269 struct bo_data_t *bo_data = gbm_bo_data(bo);
272 if (bo_data->map_data)
275 if (gbm_handle->format == HAL_PIXEL_FORMAT_YV12) {
277 ALOGE("can't map with offset for planar %p", bo);
283 flags |= GBM_BO_TRANSFER_WRITE;
285 *addr = gbm_bo_map(bo, 0, 0, x + w, y + h, flags, &stride, &bo_data->map_data);
286 ALOGV("mapped bo %p (%d, %d)-(%d, %d) at %p", bo, x, y, w, h, *addr);
290 assert(stride == gbm_bo_get_stride(bo));
295 static void gbm_unmap(struct gbm_bo *bo)
297 struct bo_data_t *bo_data = gbm_bo_data(bo);
299 gbm_bo_unmap(bo, bo_data->map_data);
300 bo_data->map_data = NULL;
303 void gbm_dev_destroy(struct gbm_device *gbm)
305 int fd = gbm_device_get_fd(gbm);
307 gbm_device_destroy(gbm);
311 struct gbm_device *gbm_dev_create(bool master)
313 struct gbm_device *gbm;
314 char path[PROPERTY_VALUE_MAX];
317 if (property_get("gralloc.gbm.device", path, NULL) > 0)
318 fd = open(path, O_RDWR | O_CLOEXEC);
320 fd = drmOpenByFB(0, master ? DRM_NODE_PRIMARY : DRM_NODE_RENDER);
323 ALOGE("failed to open %s", path);
327 gbm = gbm_create_device(fd);
329 ALOGE("failed to create gbm device");
337 * Register a buffer handle.
339 int gralloc_gbm_handle_register(buffer_handle_t _handle, struct gbm_device *gbm)
346 if (gbm_bo_handle_map.count(_handle))
349 bo = gbm_import(gbm, _handle);
353 gbm_bo_handle_map.emplace(_handle, bo);
359 * Unregister a buffer handle. It is no-op for handles created locally.
361 int gralloc_gbm_handle_unregister(buffer_handle_t handle)
363 return gbm_free(handle);
369 buffer_handle_t gralloc_gbm_bo_create(struct gbm_device *gbm,
370 int width, int height, int format, int usage, int *stride)
373 native_handle_t *handle;
375 handle = gralloc_handle_create(width, height, format, usage);
379 bo = gbm_alloc(gbm, handle);
381 native_handle_delete(handle);
385 gbm_bo_handle_map.emplace(handle, bo);
388 *stride = gralloc_handle(handle)->stride / gralloc_gbm_get_bpp(format);
394 * Lock a bo. XXX thread-safety?
396 int gralloc_gbm_bo_lock(buffer_handle_t handle,
397 int usage, int x, int y, int w, int h,
400 struct gralloc_handle_t *gbm_handle = gralloc_handle(handle);
401 struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
402 struct bo_data_t *bo_data;
407 if ((gbm_handle->usage & usage) != (uint32_t)usage) {
408 /* make FB special for testing software renderer with */
410 if (!(gbm_handle->usage & GRALLOC_USAGE_SW_READ_OFTEN) &&
411 !(gbm_handle->usage & GRALLOC_USAGE_HW_FB) &&
412 !(gbm_handle->usage & GRALLOC_USAGE_HW_TEXTURE)) {
413 ALOGE("bo.usage:x%X/usage:x%X is not GRALLOC_USAGE_HW_FB or GRALLOC_USAGE_HW_TEXTURE",
414 gbm_handle->usage, usage);
419 bo_data = gbm_bo_data(bo);
421 bo_data = new struct bo_data_t();
422 gbm_bo_set_user_data(bo, bo_data, gralloc_gbm_destroy_user_data);
425 ALOGI("lock bo %p, cnt=%d, usage=%x", bo, bo_data->lock_count, usage);
427 /* allow multiple locks with compatible usages */
428 if (bo_data->lock_count && (bo_data->locked_for & usage) != usage)
431 usage |= bo_data->locked_for;
433 if (usage & (GRALLOC_USAGE_SW_WRITE_MASK |
434 GRALLOC_USAGE_SW_READ_MASK)) {
435 /* the driver is supposed to wait for the bo */
436 int write = !!(usage & GRALLOC_USAGE_SW_WRITE_MASK);
437 int err = gbm_map(handle, x, y, w, h, write, addr);
442 /* kernel handles the synchronization here */
445 bo_data->lock_count++;
446 bo_data->locked_for |= usage;
454 int gralloc_gbm_bo_unlock(buffer_handle_t handle)
456 struct gbm_bo *bo = gralloc_gbm_bo_from_handle(handle);
457 struct bo_data_t *bo_data;
461 bo_data = gbm_bo_data(bo);
463 int mapped = bo_data->locked_for &
464 (GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_SW_READ_MASK);
466 if (!bo_data->lock_count)
472 bo_data->lock_count--;
473 if (!bo_data->lock_count)
474 bo_data->locked_for = 0;