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:
80 fmt = PIPE_FORMAT_NONE;
87 static unsigned get_pipe_bind(int usage)
89 unsigned bind = PIPE_BIND_SHARED;
91 if (usage & GRALLOC_USAGE_SW_READ_MASK)
92 bind |= PIPE_BIND_TRANSFER_READ;
93 if (usage & GRALLOC_USAGE_SW_WRITE_MASK)
94 bind |= PIPE_BIND_TRANSFER_WRITE;
96 if (usage & GRALLOC_USAGE_HW_TEXTURE)
97 bind |= PIPE_BIND_SAMPLER_VIEW;
98 if (usage & GRALLOC_USAGE_HW_RENDER)
99 bind |= PIPE_BIND_RENDER_TARGET;
100 if (usage & GRALLOC_USAGE_HW_FB) {
101 bind |= PIPE_BIND_RENDER_TARGET;
102 bind |= PIPE_BIND_SCANOUT;
108 static struct pipe_buffer *get_pipe_buffer_locked(struct pipe_manager *pm,
109 const struct gralloc_drm_handle_t *handle)
111 struct pipe_buffer *buf;
112 struct pipe_resource templ;
114 memset(&templ, 0, sizeof(templ));
115 templ.format = get_pipe_format(handle->format);
116 templ.bind = get_pipe_bind(handle->usage);
117 templ.target = PIPE_TEXTURE_2D;
119 if (templ.format == PIPE_FORMAT_NONE ||
120 !pm->screen->is_format_supported(pm->screen, templ.format,
121 templ.target, 0, templ.bind)) {
122 LOGE("unsupported format 0x%x", handle->format);
126 buf = CALLOC(1, sizeof(*buf));
128 LOGE("failed to allocate pipe buffer");
132 templ.width0 = handle->width;
133 templ.height0 = handle->height;
135 templ.array_size = 1;
138 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
139 buf->winsys.handle = handle->name;
140 buf->winsys.stride = handle->stride;
142 buf->resource = pm->screen->resource_from_handle(pm->screen,
143 &templ, &buf->winsys);
149 pm->screen->resource_create(pm->screen, &templ);
153 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
154 if (!pm->screen->resource_get_handle(pm->screen,
155 buf->resource, &buf->winsys))
159 /* need the gem handle for fb */
160 if (handle->usage & GRALLOC_USAGE_HW_FB) {
161 struct winsys_handle tmp;
163 memset(&tmp, 0, sizeof(tmp));
164 tmp.type = DRM_API_HANDLE_TYPE_KMS;
165 if (!pm->screen->resource_get_handle(pm->screen,
166 buf->resource, &tmp))
169 buf->base.fb_handle = tmp.handle;
175 LOGE("failed to allocate pipe buffer");
177 pipe_resource_reference(&buf->resource, NULL);
183 static struct gralloc_drm_bo_t *pipe_alloc(struct gralloc_drm_drv_t *drv,
184 struct gralloc_drm_handle_t *handle)
186 struct pipe_manager *pm = (struct pipe_manager *) drv;
187 struct pipe_buffer *buf;
189 pthread_mutex_lock(&pm->mutex);
190 buf = get_pipe_buffer_locked(pm, handle);
191 pthread_mutex_unlock(&pm->mutex);
194 handle->name = (int) buf->winsys.handle;
195 handle->stride = (int) buf->winsys.stride;
197 buf->base.handle = handle;
203 static void pipe_free(struct gralloc_drm_drv_t *drv, struct gralloc_drm_bo_t *bo)
205 struct pipe_manager *pm = (struct pipe_manager *) drv;
206 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
208 pthread_mutex_lock(&pm->mutex);
211 pipe_transfer_destroy(pm->context, buf->transfer);
212 pipe_resource_reference(&buf->resource, NULL);
214 pthread_mutex_unlock(&pm->mutex);
219 static int pipe_map(struct gralloc_drm_drv_t *drv,
220 struct gralloc_drm_bo_t *bo, int x, int y, int w, int h,
221 int enable_write, void **addr)
223 struct pipe_manager *pm = (struct pipe_manager *) drv;
224 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
227 pthread_mutex_lock(&pm->mutex);
229 /* need a context to get transfer */
231 pm->context = pm->screen->context_create(pm->screen, NULL);
233 LOGE("failed to create pipe context");
239 enum pipe_transfer_usage usage;
241 usage = PIPE_TRANSFER_READ;
243 usage |= PIPE_TRANSFER_WRITE;
245 assert(!buf->transfer);
248 * ignore x, y, w and h so that returned addr points at the
249 * start of the buffer
251 buf->transfer = pipe_get_transfer(pm->context, buf->resource,
253 buf->resource->width0, buf->resource->height0);
255 *addr = pipe_transfer_map(pm->context, buf->transfer);
260 pthread_mutex_unlock(&pm->mutex);
265 static void pipe_unmap(struct gralloc_drm_drv_t *drv,
266 struct gralloc_drm_bo_t *bo)
268 struct pipe_manager *pm = (struct pipe_manager *) drv;
269 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
271 pthread_mutex_lock(&pm->mutex);
273 assert(buf && buf->transfer);
275 pipe_transfer_unmap(pm->context, buf->transfer);
276 pipe_transfer_destroy(pm->context, buf->transfer);
277 buf->transfer = NULL;
279 pm->context->flush(pm->context, NULL);
281 pthread_mutex_unlock(&pm->mutex);
284 static void pipe_copy(struct gralloc_drm_drv_t *drv,
285 struct gralloc_drm_bo_t *dst_bo,
286 struct gralloc_drm_bo_t *src_bo,
287 short x1, short y1, short x2, short y2)
289 struct pipe_manager *pm = (struct pipe_manager *) drv;
290 struct pipe_buffer *dst = (struct pipe_buffer *) dst_bo;
291 struct pipe_buffer *src = (struct pipe_buffer *) src_bo;
292 struct pipe_box src_box;
294 if (dst_bo->handle->width != src_bo->handle->width ||
295 dst_bo->handle->height != src_bo->handle->height ||
296 dst_bo->handle->stride != src_bo->handle->stride ||
297 dst_bo->handle->format != src_bo->handle->format) {
298 LOGE("copy between incompatible buffers");
306 if (x2 > dst_bo->handle->width)
307 x2 = dst_bo->handle->width;
308 if (y2 > dst_bo->handle->height)
309 y2 = dst_bo->handle->height;
311 if (x2 <= x1 || y2 <= y1)
314 u_box_2d(x1, y1, x2 - x1, y2 - y1, &src_box);
316 pthread_mutex_lock(&pm->mutex);
318 /* need a context for copying */
320 pm->context = pm->screen->context_create(pm->screen, NULL);
322 LOGE("failed to create pipe context");
323 pthread_mutex_unlock(&pm->mutex);
328 pm->context->resource_copy_region(pm->context,
329 dst->resource, 0, x1, y1, 0,
330 src->resource, 0, &src_box);
331 pm->context->flush(pm->context, NULL);
333 pthread_mutex_unlock(&pm->mutex);
336 static void pipe_init_kms_features(struct gralloc_drm_drv_t *drv, struct gralloc_drm_t *drm)
338 struct pipe_manager *pm = (struct pipe_manager *) drv;
340 drm->fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
341 drm->mode_dirty_fb = (strcmp(pm->driver, "vmwgfx") == 0);
342 drm->swap_mode = DRM_SWAP_FLIP;
343 drm->mode_sync_flip = 1;
344 drm->swap_interval = 1;
345 drm->vblank_secondary = 0;
348 static void pipe_destroy(struct gralloc_drm_drv_t *drv)
350 struct pipe_manager *pm = (struct pipe_manager *) drv;
353 pm->context->destroy(pm->context);
354 pm->screen->destroy(pm->screen);
359 #include "nouveau/drm/nouveau_drm_public.h"
361 #include "radeon/drm/radeon_drm_public.h"
362 #include "r300/r300_public.h"
364 #include "r600/drm/r600_drm_public.h"
365 #include "r600/r600_public.h"
367 #include "svga/drm/svga_drm_public.h"
368 #include "svga/svga_public.h"
370 #include "target-helpers/inline_debug_helper.h"
372 static int pipe_init_screen(struct pipe_manager *pm)
374 struct pipe_screen *screen = NULL;
376 #ifdef ENABLE_PIPE_NOUVEAU
377 if (strcmp(pm->driver, "nouveau") == 0)
378 screen = nouveau_drm_screen_create(pm->fd);
380 #ifdef ENABLE_PIPE_R300
381 if (strcmp(pm->driver, "r300") == 0) {
382 struct radeon_winsys *sws = radeon_drm_winsys_create(pm->fd);
385 screen = r300_screen_create(sws);
391 #ifdef ENABLE_PIPE_R600
392 if (strcmp(pm->driver, "r600") == 0) {
393 struct radeon *rw = r600_drm_winsys_create(pm->fd);
396 screen = r600_screen_create(rw);
402 #ifdef ENABLE_PIPE_VMWGFX
403 if (strcmp(pm->driver, "vmwgfx") == 0) {
404 struct svga_winsys_screen *sws =
405 svga_drm_winsys_screen_create(pm->fd);
408 screen = svga_screen_create(sws);
416 LOGW("failed to create screen for %s", pm->driver);
420 pm->screen = debug_screen_wrap(screen);
426 #include <i915_drm.h>
427 #include <radeon_drm.h>
428 static int pipe_get_pci_id(struct pipe_manager *pm,
429 const char *name, int *vendor, int *device)
433 if (strcmp(name, "i915") == 0) {
434 struct drm_i915_getparam gp;
438 memset(&gp, 0, sizeof(gp));
439 gp.param = I915_PARAM_CHIPSET_ID;
441 err = drmCommandWriteRead(pm->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
443 else if (strcmp(name, "radeon") == 0) {
444 struct drm_radeon_info info;
448 memset(&info, 0, sizeof(info));
449 info.request = RADEON_INFO_DEVICE_ID;
450 info.value = (long) device;
451 err = drmCommandWriteRead(pm->fd, DRM_RADEON_INFO, &info, sizeof(info));
453 else if (strcmp(name, "nouveau") == 0) {
465 #define DRIVER_MAP_GALLIUM_ONLY
466 #include "pci_ids/pci_id_driver_map.h"
467 static int pipe_find_driver(struct pipe_manager *pm, const char *name)
473 err = pipe_get_pci_id(pm, name, &vendor, &device);
477 /* look up in the driver map */
478 for (idx = 0; driver_map[idx].driver; idx++) {
481 if (vendor != driver_map[idx].vendor_id)
484 if (driver_map[idx].num_chips_ids == -1)
487 for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
488 if (driver_map[idx].chip_ids[i] == device)
491 if (i < driver_map[idx].num_chips_ids)
495 driver = driver_map[idx].driver;
496 err = (driver) ? 0 : -ENODEV;
499 if (strcmp(name, "vmwgfx") == 0) {
506 strncpy(pm->driver, driver, sizeof(pm->driver) - 1);
511 struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_pipe(int fd, const char *name)
513 struct pipe_manager *pm;
515 pm = CALLOC(1, sizeof(*pm));
517 LOGE("failed to allocate pipe manager for %s", name);
522 pthread_mutex_init(&pm->mutex, NULL);
524 if (pipe_find_driver(pm, name)) {
529 if (pipe_init_screen(pm)) {
534 pm->base.destroy = pipe_destroy;
535 pm->base.init_kms_features = pipe_init_kms_features;
536 pm->base.alloc = pipe_alloc;
537 pm->base.free = pipe_free;
538 pm->base.map = pipe_map;
539 pm->base.unmap = pipe_unmap;
540 pm->base.copy = pipe_copy;