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-PIPE"
26 #include <cutils/log.h>
30 #include <pipe/p_screen.h>
31 #include <pipe/p_context.h>
32 #include <state_tracker/drm_driver.h>
33 #include <util/u_inlines.h>
34 #include <util/u_memory.h>
36 #include "gralloc_drm.h"
37 #include "gralloc_drm_priv.h"
40 #define DRI_LIBRARY_PATH "/system/lib64/dri"
42 #define DRI_LIBRARY_PATH "/system/lib/dri"
46 struct gralloc_drm_drv_t base;
50 pthread_mutex_t mutex;
51 struct pipe_loader_device *dev;
52 struct pipe_screen *screen;
53 struct pipe_context *context;
57 struct gralloc_drm_bo_t base;
59 struct pipe_resource *resource;
60 struct winsys_handle winsys;
62 struct pipe_transfer *transfer;
65 static enum pipe_format get_pipe_format(int format)
70 case HAL_PIXEL_FORMAT_RGBA_8888:
71 fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
73 case HAL_PIXEL_FORMAT_RGBX_8888:
74 fmt = PIPE_FORMAT_R8G8B8X8_UNORM;
76 case HAL_PIXEL_FORMAT_RGB_888:
77 fmt = PIPE_FORMAT_R8G8B8_UNORM;
79 case HAL_PIXEL_FORMAT_RGB_565:
80 fmt = PIPE_FORMAT_B5G6R5_UNORM;
82 case HAL_PIXEL_FORMAT_BGRA_8888:
83 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
85 case HAL_PIXEL_FORMAT_YV12:
86 case HAL_PIXEL_FORMAT_DRM_NV12:
87 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
88 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
90 fmt = PIPE_FORMAT_NONE;
97 static unsigned get_pipe_bind(int usage)
99 unsigned bind = PIPE_BIND_SHARED;
101 if (usage & GRALLOC_USAGE_SW_READ_MASK)
102 bind |= PIPE_BIND_TRANSFER_READ;
103 if (usage & GRALLOC_USAGE_SW_WRITE_MASK)
104 bind |= PIPE_BIND_TRANSFER_WRITE;
106 if (usage & GRALLOC_USAGE_HW_TEXTURE)
107 bind |= PIPE_BIND_SAMPLER_VIEW;
108 if (usage & GRALLOC_USAGE_HW_RENDER)
109 bind |= PIPE_BIND_RENDER_TARGET;
110 if (usage & GRALLOC_USAGE_HW_FB) {
111 bind |= PIPE_BIND_RENDER_TARGET;
112 bind |= PIPE_BIND_SCANOUT;
118 static struct pipe_buffer *get_pipe_buffer_locked(struct pipe_manager *pm,
119 const struct gralloc_drm_handle_t *handle)
121 struct pipe_buffer *buf;
122 struct pipe_resource templ;
124 memset(&templ, 0, sizeof(templ));
125 templ.format = get_pipe_format(handle->format);
126 templ.bind = get_pipe_bind(handle->usage);
127 templ.target = PIPE_TEXTURE_2D;
129 if (templ.format == PIPE_FORMAT_NONE ||
130 !pm->screen->is_format_supported(pm->screen, templ.format,
131 templ.target, 0, templ.bind)) {
132 ALOGE("unsupported format 0x%x", handle->format);
136 buf = CALLOC(1, sizeof(*buf));
138 ALOGE("failed to allocate pipe buffer");
142 templ.width0 = handle->width;
143 templ.height0 = handle->height;
145 templ.array_size = 1;
148 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
149 buf->winsys.handle = handle->name;
150 buf->winsys.stride = handle->stride;
152 buf->resource = pm->screen->resource_from_handle(pm->screen,
153 &templ, &buf->winsys);
159 pm->screen->resource_create(pm->screen, &templ);
163 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
164 if (!pm->screen->resource_get_handle(pm->screen,
165 buf->resource, &buf->winsys))
169 /* need the gem handle for fb */
170 if (handle->usage & GRALLOC_USAGE_HW_FB) {
171 struct winsys_handle tmp;
173 memset(&tmp, 0, sizeof(tmp));
174 tmp.type = DRM_API_HANDLE_TYPE_KMS;
175 if (!pm->screen->resource_get_handle(pm->screen,
176 buf->resource, &tmp))
179 buf->base.fb_handle = tmp.handle;
185 ALOGE("failed to allocate pipe buffer");
187 pipe_resource_reference(&buf->resource, NULL);
193 static struct gralloc_drm_bo_t *pipe_alloc(struct gralloc_drm_drv_t *drv,
194 struct gralloc_drm_handle_t *handle)
196 struct pipe_manager *pm = (struct pipe_manager *) drv;
197 struct pipe_buffer *buf;
199 pthread_mutex_lock(&pm->mutex);
200 buf = get_pipe_buffer_locked(pm, handle);
201 pthread_mutex_unlock(&pm->mutex);
204 handle->name = (int) buf->winsys.handle;
205 handle->stride = (int) buf->winsys.stride;
207 buf->base.handle = handle;
213 static void pipe_free(struct gralloc_drm_drv_t *drv, struct gralloc_drm_bo_t *bo)
215 struct pipe_manager *pm = (struct pipe_manager *) drv;
216 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
218 pthread_mutex_lock(&pm->mutex);
221 pipe_transfer_unmap(pm->context, buf->transfer);
222 pipe_resource_reference(&buf->resource, NULL);
224 pthread_mutex_unlock(&pm->mutex);
229 static int pipe_map(struct gralloc_drm_drv_t *drv,
230 struct gralloc_drm_bo_t *bo, int x, int y, int w, int h,
231 int enable_write, void **addr)
233 struct pipe_manager *pm = (struct pipe_manager *) drv;
234 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
237 pthread_mutex_lock(&pm->mutex);
239 /* need a context to get transfer */
241 pm->context = pm->screen->context_create(pm->screen, NULL, 0);
243 ALOGE("failed to create pipe context");
249 enum pipe_transfer_usage usage;
251 usage = PIPE_TRANSFER_READ;
253 usage |= PIPE_TRANSFER_WRITE;
255 assert(!buf->transfer);
258 * ignore x, y, w and h so that returned addr points at the
259 * start of the buffer
261 *addr = pipe_transfer_map(pm->context, buf->resource,
263 buf->resource->width0, buf->resource->height0,
269 pthread_mutex_unlock(&pm->mutex);
274 static void pipe_unmap(struct gralloc_drm_drv_t *drv,
275 struct gralloc_drm_bo_t *bo)
277 struct pipe_manager *pm = (struct pipe_manager *) drv;
278 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
280 pthread_mutex_lock(&pm->mutex);
282 assert(buf && buf->transfer);
284 pipe_transfer_unmap(pm->context, buf->transfer);
285 buf->transfer = NULL;
287 pm->context->flush(pm->context, NULL, 0);
289 pthread_mutex_unlock(&pm->mutex);
292 static void pipe_blit(struct gralloc_drm_drv_t *drv,
293 struct gralloc_drm_bo_t *dst_bo,
294 struct gralloc_drm_bo_t *src_bo,
295 uint16_t dst_x1, uint16_t dst_y1,
296 uint16_t dst_x2, uint16_t dst_y2,
297 uint16_t src_x1, uint16_t src_y1,
298 uint16_t src_x2, uint16_t src_y2)
300 struct pipe_manager *pm = (struct pipe_manager *) drv;
301 struct pipe_buffer *dst = (struct pipe_buffer *) dst_bo;
302 struct pipe_buffer *src = (struct pipe_buffer *) src_bo;
303 struct pipe_box src_box;
305 if (dst_bo->handle->width != src_bo->handle->width ||
306 dst_bo->handle->height != src_bo->handle->height ||
307 dst_bo->handle->stride != src_bo->handle->stride ||
308 dst_bo->handle->format != src_bo->handle->format) {
309 ALOGE("copy between incompatible buffers");
313 if (dst_x2 > dst_bo->handle->width)
314 dst_x2 = dst_bo->handle->width;
315 if (dst_y2 > dst_bo->handle->height)
316 dst_y2 = dst_bo->handle->height;
318 if (src_x2 <= src_x1 || src_y2 <= src_y1)
321 u_box_2d(src_x1, src_y1, src_x2 - src_x1, src_y2 - src_y1, &src_box);
323 pthread_mutex_lock(&pm->mutex);
325 /* need a context for copying */
327 pm->context = pm->screen->context_create(pm->screen, NULL, 0);
329 ALOGE("failed to create pipe context");
330 pthread_mutex_unlock(&pm->mutex);
335 pm->context->resource_copy_region(pm->context,
336 dst->resource, 0, dst_x1, dst_y1, 0,
337 src->resource, 0, &src_box);
338 pm->context->flush(pm->context, NULL, 0);
340 pthread_mutex_unlock(&pm->mutex);
343 static void pipe_init_kms_features(struct gralloc_drm_drv_t *drv, struct gralloc_drm_t *drm)
345 struct pipe_manager *pm = (struct pipe_manager *) drv;
347 switch (drm->primary.fb_format) {
348 case HAL_PIXEL_FORMAT_BGRA_8888:
349 case HAL_PIXEL_FORMAT_RGB_565:
352 drm->primary.fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
356 drm->mode_quirk_vmwgfx = 0;
357 drm->swap_mode = DRM_SWAP_FLIP;
358 drm->mode_sync_flip = 1;
359 drm->swap_interval = 1;
360 drm->vblank_secondary = 0;
363 static void pipe_destroy(struct gralloc_drm_drv_t *drv)
365 struct pipe_manager *pm = (struct pipe_manager *) drv;
368 pm->context->destroy(pm->context);
369 pm->screen->destroy(pm->screen);
370 dlclose(pm->gallium);
374 struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_pipe(int fd, const char *name)
376 struct pipe_manager *pm;
377 struct pipe_screen *(*load_pipe_screen)(struct pipe_loader_device **dev, int fd);
379 pm = CALLOC(1, sizeof(*pm));
381 ALOGE("failed to allocate pipe manager for %s", name);
386 pthread_mutex_init(&pm->mutex, NULL);
388 pm->gallium = dlopen(DRI_LIBRARY_PATH"/gallium_dri.so", RTLD_NOW | RTLD_GLOBAL);
392 load_pipe_screen = dlsym(pm->gallium, "load_pipe_screen");
393 if (!load_pipe_screen)
396 pm->screen = load_pipe_screen(&pm->dev, fd);
400 pm->base.destroy = pipe_destroy;
401 pm->base.init_kms_features = pipe_init_kms_features;
402 pm->base.alloc = pipe_alloc;
403 pm->base.free = pipe_free;
404 pm->base.map = pipe_map;
405 pm->base.unmap = pipe_unmap;
406 pm->base.blit = pipe_blit;
411 dlclose(pm->gallium);