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>
29 #include <pipe/p_screen.h>
30 #include <pipe/p_context.h>
31 #include <state_tracker/drm_driver.h>
32 #include <util/u_inlines.h>
33 #include <util/u_memory.h>
35 #include "gralloc_drm.h"
36 #include "gralloc_drm_priv.h"
39 struct gralloc_drm_drv_t base;
43 pthread_mutex_t mutex;
44 struct pipe_screen *screen;
45 struct pipe_context *context;
49 struct gralloc_drm_bo_t base;
51 struct pipe_resource *resource;
52 struct winsys_handle winsys;
54 struct pipe_transfer *transfer;
57 static enum pipe_format get_pipe_format(int format)
62 case HAL_PIXEL_FORMAT_RGBA_8888:
63 fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
65 case HAL_PIXEL_FORMAT_RGBX_8888:
66 fmt = PIPE_FORMAT_R8G8B8X8_UNORM;
68 case HAL_PIXEL_FORMAT_RGB_888:
69 fmt = PIPE_FORMAT_R8G8B8_UNORM;
71 case HAL_PIXEL_FORMAT_RGB_565:
72 fmt = PIPE_FORMAT_B5G6R5_UNORM;
74 case HAL_PIXEL_FORMAT_BGRA_8888:
75 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
77 case HAL_PIXEL_FORMAT_RGBA_5551:
78 case HAL_PIXEL_FORMAT_RGBA_4444:
79 case HAL_PIXEL_FORMAT_YV12:
80 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
81 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
83 fmt = PIPE_FORMAT_NONE;
90 static unsigned get_pipe_bind(int usage)
92 unsigned bind = PIPE_BIND_SHARED;
94 if (usage & GRALLOC_USAGE_SW_READ_MASK)
95 bind |= PIPE_BIND_TRANSFER_READ;
96 if (usage & GRALLOC_USAGE_SW_WRITE_MASK)
97 bind |= PIPE_BIND_TRANSFER_WRITE;
99 if (usage & GRALLOC_USAGE_HW_TEXTURE)
100 bind |= PIPE_BIND_SAMPLER_VIEW;
101 if (usage & GRALLOC_USAGE_HW_RENDER)
102 bind |= PIPE_BIND_RENDER_TARGET;
103 if (usage & GRALLOC_USAGE_HW_FB) {
104 bind |= PIPE_BIND_RENDER_TARGET;
105 bind |= PIPE_BIND_SCANOUT;
111 static struct pipe_buffer *get_pipe_buffer_locked(struct pipe_manager *pm,
112 const struct gralloc_drm_handle_t *handle)
114 struct pipe_buffer *buf;
115 struct pipe_resource templ;
117 memset(&templ, 0, sizeof(templ));
118 templ.format = get_pipe_format(handle->format);
119 templ.bind = get_pipe_bind(handle->usage);
120 templ.target = PIPE_TEXTURE_2D;
122 if (templ.format == PIPE_FORMAT_NONE ||
123 !pm->screen->is_format_supported(pm->screen, templ.format,
124 templ.target, 0, templ.bind)) {
125 ALOGE("unsupported format 0x%x", handle->format);
129 buf = CALLOC(1, sizeof(*buf));
131 ALOGE("failed to allocate pipe buffer");
135 templ.width0 = handle->width;
136 templ.height0 = handle->height;
138 templ.array_size = 1;
141 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
142 buf->winsys.handle = handle->name;
143 buf->winsys.stride = handle->stride;
145 buf->resource = pm->screen->resource_from_handle(pm->screen,
146 &templ, &buf->winsys);
152 pm->screen->resource_create(pm->screen, &templ);
156 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
157 if (!pm->screen->resource_get_handle(pm->screen,
158 buf->resource, &buf->winsys))
162 /* need the gem handle for fb */
163 if (handle->usage & GRALLOC_USAGE_HW_FB) {
164 struct winsys_handle tmp;
166 memset(&tmp, 0, sizeof(tmp));
167 tmp.type = DRM_API_HANDLE_TYPE_KMS;
168 if (!pm->screen->resource_get_handle(pm->screen,
169 buf->resource, &tmp))
172 buf->base.fb_handle = tmp.handle;
178 ALOGE("failed to allocate pipe buffer");
180 pipe_resource_reference(&buf->resource, NULL);
186 static struct gralloc_drm_bo_t *pipe_alloc(struct gralloc_drm_drv_t *drv,
187 struct gralloc_drm_handle_t *handle)
189 struct pipe_manager *pm = (struct pipe_manager *) drv;
190 struct pipe_buffer *buf;
192 pthread_mutex_lock(&pm->mutex);
193 buf = get_pipe_buffer_locked(pm, handle);
194 pthread_mutex_unlock(&pm->mutex);
197 handle->name = (int) buf->winsys.handle;
198 handle->stride = (int) buf->winsys.stride;
200 buf->base.handle = handle;
206 static void pipe_free(struct gralloc_drm_drv_t *drv, struct gralloc_drm_bo_t *bo)
208 struct pipe_manager *pm = (struct pipe_manager *) drv;
209 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
211 pthread_mutex_lock(&pm->mutex);
214 pipe_transfer_destroy(pm->context, buf->transfer);
215 pipe_resource_reference(&buf->resource, NULL);
217 pthread_mutex_unlock(&pm->mutex);
222 static int pipe_map(struct gralloc_drm_drv_t *drv,
223 struct gralloc_drm_bo_t *bo, int x, int y, int w, int h,
224 int enable_write, void **addr)
226 struct pipe_manager *pm = (struct pipe_manager *) drv;
227 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
230 pthread_mutex_lock(&pm->mutex);
232 /* need a context to get transfer */
234 pm->context = pm->screen->context_create(pm->screen, NULL);
236 ALOGE("failed to create pipe context");
242 enum pipe_transfer_usage usage;
244 usage = PIPE_TRANSFER_READ;
246 usage |= PIPE_TRANSFER_WRITE;
248 assert(!buf->transfer);
251 * ignore x, y, w and h so that returned addr points at the
252 * start of the buffer
254 buf->transfer = pipe_get_transfer(pm->context, buf->resource,
256 buf->resource->width0, buf->resource->height0);
258 *addr = pipe_transfer_map(pm->context, buf->transfer);
263 pthread_mutex_unlock(&pm->mutex);
268 static void pipe_unmap(struct gralloc_drm_drv_t *drv,
269 struct gralloc_drm_bo_t *bo)
271 struct pipe_manager *pm = (struct pipe_manager *) drv;
272 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
274 pthread_mutex_lock(&pm->mutex);
276 assert(buf && buf->transfer);
278 pipe_transfer_unmap(pm->context, buf->transfer);
279 pipe_transfer_destroy(pm->context, buf->transfer);
280 buf->transfer = NULL;
282 pm->context->flush(pm->context, NULL);
284 pthread_mutex_unlock(&pm->mutex);
287 static void pipe_copy(struct gralloc_drm_drv_t *drv,
288 struct gralloc_drm_bo_t *dst_bo,
289 struct gralloc_drm_bo_t *src_bo,
290 short x1, short y1, short x2, short y2)
292 struct pipe_manager *pm = (struct pipe_manager *) drv;
293 struct pipe_buffer *dst = (struct pipe_buffer *) dst_bo;
294 struct pipe_buffer *src = (struct pipe_buffer *) src_bo;
295 struct pipe_box src_box;
297 if (dst_bo->handle->width != src_bo->handle->width ||
298 dst_bo->handle->height != src_bo->handle->height ||
299 dst_bo->handle->stride != src_bo->handle->stride ||
300 dst_bo->handle->format != src_bo->handle->format) {
301 ALOGE("copy between incompatible buffers");
309 if (x2 > dst_bo->handle->width)
310 x2 = dst_bo->handle->width;
311 if (y2 > dst_bo->handle->height)
312 y2 = dst_bo->handle->height;
314 if (x2 <= x1 || y2 <= y1)
317 u_box_2d(x1, y1, x2 - x1, y2 - y1, &src_box);
319 pthread_mutex_lock(&pm->mutex);
321 /* need a context for copying */
323 pm->context = pm->screen->context_create(pm->screen, NULL);
325 ALOGE("failed to create pipe context");
326 pthread_mutex_unlock(&pm->mutex);
331 pm->context->resource_copy_region(pm->context,
332 dst->resource, 0, x1, y1, 0,
333 src->resource, 0, &src_box);
334 pm->context->flush(pm->context, NULL);
336 pthread_mutex_unlock(&pm->mutex);
339 static void pipe_init_kms_features(struct gralloc_drm_drv_t *drv, struct gralloc_drm_t *drm)
341 struct pipe_manager *pm = (struct pipe_manager *) drv;
343 switch (drm->fb_format) {
344 case HAL_PIXEL_FORMAT_BGRA_8888:
345 case HAL_PIXEL_FORMAT_RGB_565:
348 drm->fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
352 if (strcmp(pm->driver, "vmwgfx") == 0) {
353 drm->mode_quirk_vmwgfx = 1;
354 drm->swap_mode = DRM_SWAP_COPY;
357 drm->mode_quirk_vmwgfx = 0;
358 drm->swap_mode = DRM_SWAP_FLIP;
360 drm->mode_sync_flip = 1;
361 drm->swap_interval = 1;
362 drm->vblank_secondary = 0;
365 static void pipe_destroy(struct gralloc_drm_drv_t *drv)
367 struct pipe_manager *pm = (struct pipe_manager *) drv;
370 pm->context->destroy(pm->context);
371 pm->screen->destroy(pm->screen);
376 #include "nouveau/drm/nouveau_drm_public.h"
378 #include "radeon/drm/radeon_drm_public.h"
379 #include "r300/r300_public.h"
381 #include "r600/drm/r600_drm_public.h"
382 #include "r600/r600_public.h"
384 #include "svga/drm/svga_drm_public.h"
385 #include "svga/svga_winsys.h"
386 #include "svga/svga_public.h"
388 #include "target-helpers/inline_debug_helper.h"
390 static int pipe_init_screen(struct pipe_manager *pm)
392 struct pipe_screen *screen = NULL;
394 #ifdef ENABLE_PIPE_NOUVEAU
395 if (strcmp(pm->driver, "nouveau") == 0)
396 screen = nouveau_drm_screen_create(pm->fd);
398 #ifdef ENABLE_PIPE_R300
399 if (strcmp(pm->driver, "r300") == 0) {
400 struct radeon_winsys *sws = radeon_drm_winsys_create(pm->fd);
403 screen = r300_screen_create(sws);
409 #ifdef ENABLE_PIPE_R600
410 if (strcmp(pm->driver, "r600") == 0) {
411 struct radeon *rw = r600_drm_winsys_create(pm->fd);
414 screen = r600_screen_create(rw);
420 #ifdef ENABLE_PIPE_VMWGFX
421 if (strcmp(pm->driver, "vmwgfx") == 0) {
422 struct svga_winsys_screen *sws =
423 svga_drm_winsys_screen_create(pm->fd);
426 screen = svga_screen_create(sws);
434 ALOGW("failed to create screen for %s", pm->driver);
438 pm->screen = debug_screen_wrap(screen);
444 #include <i915_drm.h>
445 #include <radeon_drm.h>
446 static int pipe_get_pci_id(struct pipe_manager *pm,
447 const char *name, int *vendor, int *device)
451 if (strcmp(name, "i915") == 0) {
452 struct drm_i915_getparam gp;
456 memset(&gp, 0, sizeof(gp));
457 gp.param = I915_PARAM_CHIPSET_ID;
459 err = drmCommandWriteRead(pm->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
461 else if (strcmp(name, "radeon") == 0) {
462 struct drm_radeon_info info;
466 memset(&info, 0, sizeof(info));
467 info.request = RADEON_INFO_DEVICE_ID;
468 info.value = (long) device;
469 err = drmCommandWriteRead(pm->fd, DRM_RADEON_INFO, &info, sizeof(info));
471 else if (strcmp(name, "nouveau") == 0) {
476 else if (strcmp(name, "vmwgfx") == 0) {
489 #define DRIVER_MAP_GALLIUM_ONLY
490 #include "pci_ids/pci_id_driver_map.h"
491 static int pipe_find_driver(struct pipe_manager *pm, const char *name)
497 err = pipe_get_pci_id(pm, name, &vendor, &device);
501 /* look up in the driver map */
502 for (idx = 0; driver_map[idx].driver; idx++) {
505 if (vendor != driver_map[idx].vendor_id)
508 if (driver_map[idx].num_chips_ids == -1)
511 for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
512 if (driver_map[idx].chip_ids[i] == device)
515 if (i < driver_map[idx].num_chips_ids)
519 driver = driver_map[idx].driver;
520 err = (driver) ? 0 : -ENODEV;
523 if (strcmp(name, "vmwgfx") == 0) {
530 strncpy(pm->driver, driver, sizeof(pm->driver) - 1);
535 struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_pipe(int fd, const char *name)
537 struct pipe_manager *pm;
539 pm = CALLOC(1, sizeof(*pm));
541 ALOGE("failed to allocate pipe manager for %s", name);
546 pthread_mutex_init(&pm->mutex, NULL);
548 if (pipe_find_driver(pm, name)) {
553 if (pipe_init_screen(pm)) {
558 pm->base.destroy = pipe_destroy;
559 pm->base.init_kms_features = pipe_init_kms_features;
560 pm->base.alloc = pipe_alloc;
561 pm->base.free = pipe_free;
562 pm->base.map = pipe_map;
563 pm->base.unmap = pipe_unmap;
564 pm->base.copy = pipe_copy;