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_DRM_NV12:
81 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
82 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
84 fmt = PIPE_FORMAT_NONE;
91 static unsigned get_pipe_bind(int usage)
93 unsigned bind = PIPE_BIND_SHARED;
95 if (usage & GRALLOC_USAGE_SW_READ_MASK)
96 bind |= PIPE_BIND_TRANSFER_READ;
97 if (usage & GRALLOC_USAGE_SW_WRITE_MASK)
98 bind |= PIPE_BIND_TRANSFER_WRITE;
100 if (usage & GRALLOC_USAGE_HW_TEXTURE)
101 bind |= PIPE_BIND_SAMPLER_VIEW;
102 if (usage & GRALLOC_USAGE_HW_RENDER)
103 bind |= PIPE_BIND_RENDER_TARGET;
104 if (usage & GRALLOC_USAGE_HW_FB) {
105 bind |= PIPE_BIND_RENDER_TARGET;
106 bind |= PIPE_BIND_SCANOUT;
112 static struct pipe_buffer *get_pipe_buffer_locked(struct pipe_manager *pm,
113 const struct gralloc_drm_handle_t *handle)
115 struct pipe_buffer *buf;
116 struct pipe_resource templ;
118 memset(&templ, 0, sizeof(templ));
119 templ.format = get_pipe_format(handle->format);
120 templ.bind = get_pipe_bind(handle->usage);
121 templ.target = PIPE_TEXTURE_2D;
123 if (templ.format == PIPE_FORMAT_NONE ||
124 !pm->screen->is_format_supported(pm->screen, templ.format,
125 templ.target, 0, templ.bind)) {
126 ALOGE("unsupported format 0x%x", handle->format);
130 buf = CALLOC(1, sizeof(*buf));
132 ALOGE("failed to allocate pipe buffer");
136 templ.width0 = handle->width;
137 templ.height0 = handle->height;
139 templ.array_size = 1;
142 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
143 buf->winsys.handle = handle->name;
144 buf->winsys.stride = handle->stride;
146 buf->resource = pm->screen->resource_from_handle(pm->screen,
147 &templ, &buf->winsys);
153 pm->screen->resource_create(pm->screen, &templ);
157 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
158 if (!pm->screen->resource_get_handle(pm->screen,
159 buf->resource, &buf->winsys))
163 /* need the gem handle for fb */
164 if (handle->usage & GRALLOC_USAGE_HW_FB) {
165 struct winsys_handle tmp;
167 memset(&tmp, 0, sizeof(tmp));
168 tmp.type = DRM_API_HANDLE_TYPE_KMS;
169 if (!pm->screen->resource_get_handle(pm->screen,
170 buf->resource, &tmp))
173 buf->base.fb_handle = tmp.handle;
179 ALOGE("failed to allocate pipe buffer");
181 pipe_resource_reference(&buf->resource, NULL);
187 static struct gralloc_drm_bo_t *pipe_alloc(struct gralloc_drm_drv_t *drv,
188 struct gralloc_drm_handle_t *handle)
190 struct pipe_manager *pm = (struct pipe_manager *) drv;
191 struct pipe_buffer *buf;
193 pthread_mutex_lock(&pm->mutex);
194 buf = get_pipe_buffer_locked(pm, handle);
195 pthread_mutex_unlock(&pm->mutex);
198 handle->name = (int) buf->winsys.handle;
199 handle->stride = (int) buf->winsys.stride;
201 buf->base.handle = handle;
207 static void pipe_free(struct gralloc_drm_drv_t *drv, struct gralloc_drm_bo_t *bo)
209 struct pipe_manager *pm = (struct pipe_manager *) drv;
210 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
212 pthread_mutex_lock(&pm->mutex);
215 pipe_transfer_destroy(pm->context, buf->transfer);
216 pipe_resource_reference(&buf->resource, NULL);
218 pthread_mutex_unlock(&pm->mutex);
223 static int pipe_map(struct gralloc_drm_drv_t *drv,
224 struct gralloc_drm_bo_t *bo, int x, int y, int w, int h,
225 int enable_write, void **addr)
227 struct pipe_manager *pm = (struct pipe_manager *) drv;
228 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
231 pthread_mutex_lock(&pm->mutex);
233 /* need a context to get transfer */
235 pm->context = pm->screen->context_create(pm->screen, NULL);
237 ALOGE("failed to create pipe context");
243 enum pipe_transfer_usage usage;
245 usage = PIPE_TRANSFER_READ;
247 usage |= PIPE_TRANSFER_WRITE;
249 assert(!buf->transfer);
252 * ignore x, y, w and h so that returned addr points at the
253 * start of the buffer
255 buf->transfer = pipe_get_transfer(pm->context, buf->resource,
257 buf->resource->width0, buf->resource->height0);
259 *addr = pipe_transfer_map(pm->context, buf->transfer);
264 pthread_mutex_unlock(&pm->mutex);
269 static void pipe_unmap(struct gralloc_drm_drv_t *drv,
270 struct gralloc_drm_bo_t *bo)
272 struct pipe_manager *pm = (struct pipe_manager *) drv;
273 struct pipe_buffer *buf = (struct pipe_buffer *) bo;
275 pthread_mutex_lock(&pm->mutex);
277 assert(buf && buf->transfer);
279 pipe_transfer_unmap(pm->context, buf->transfer);
280 pipe_transfer_destroy(pm->context, buf->transfer);
281 buf->transfer = NULL;
283 pm->context->flush(pm->context, NULL);
285 pthread_mutex_unlock(&pm->mutex);
288 static void pipe_copy(struct gralloc_drm_drv_t *drv,
289 struct gralloc_drm_bo_t *dst_bo,
290 struct gralloc_drm_bo_t *src_bo,
291 short x1, short y1, short x2, short y2)
293 struct pipe_manager *pm = (struct pipe_manager *) drv;
294 struct pipe_buffer *dst = (struct pipe_buffer *) dst_bo;
295 struct pipe_buffer *src = (struct pipe_buffer *) src_bo;
296 struct pipe_box src_box;
298 if (dst_bo->handle->width != src_bo->handle->width ||
299 dst_bo->handle->height != src_bo->handle->height ||
300 dst_bo->handle->stride != src_bo->handle->stride ||
301 dst_bo->handle->format != src_bo->handle->format) {
302 ALOGE("copy between incompatible buffers");
310 if (x2 > dst_bo->handle->width)
311 x2 = dst_bo->handle->width;
312 if (y2 > dst_bo->handle->height)
313 y2 = dst_bo->handle->height;
315 if (x2 <= x1 || y2 <= y1)
318 u_box_2d(x1, y1, x2 - x1, y2 - y1, &src_box);
320 pthread_mutex_lock(&pm->mutex);
322 /* need a context for copying */
324 pm->context = pm->screen->context_create(pm->screen, NULL);
326 ALOGE("failed to create pipe context");
327 pthread_mutex_unlock(&pm->mutex);
332 pm->context->resource_copy_region(pm->context,
333 dst->resource, 0, x1, y1, 0,
334 src->resource, 0, &src_box);
335 pm->context->flush(pm->context, NULL);
337 pthread_mutex_unlock(&pm->mutex);
340 static void pipe_init_kms_features(struct gralloc_drm_drv_t *drv, struct gralloc_drm_t *drm)
342 struct pipe_manager *pm = (struct pipe_manager *) drv;
344 switch (drm->fb_format) {
345 case HAL_PIXEL_FORMAT_BGRA_8888:
346 case HAL_PIXEL_FORMAT_RGB_565:
349 drm->fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
353 if (strcmp(pm->driver, "vmwgfx") == 0) {
354 drm->mode_quirk_vmwgfx = 1;
355 drm->swap_mode = DRM_SWAP_COPY;
358 drm->mode_quirk_vmwgfx = 0;
359 drm->swap_mode = DRM_SWAP_FLIP;
361 drm->mode_sync_flip = 1;
362 drm->swap_interval = 1;
363 drm->vblank_secondary = 0;
366 static void pipe_destroy(struct gralloc_drm_drv_t *drv)
368 struct pipe_manager *pm = (struct pipe_manager *) drv;
371 pm->context->destroy(pm->context);
372 pm->screen->destroy(pm->screen);
377 #include "nouveau/drm/nouveau_drm_public.h"
379 #include "radeon/drm/radeon_drm_public.h"
380 #include "r300/r300_public.h"
382 #include "r600/drm/r600_drm_public.h"
383 #include "r600/r600_public.h"
385 #include "svga/drm/svga_drm_public.h"
386 #include "svga/svga_winsys.h"
387 #include "svga/svga_public.h"
389 #include "target-helpers/inline_debug_helper.h"
391 static int pipe_init_screen(struct pipe_manager *pm)
393 struct pipe_screen *screen = NULL;
395 #ifdef ENABLE_PIPE_NOUVEAU
396 if (strcmp(pm->driver, "nouveau") == 0)
397 screen = nouveau_drm_screen_create(pm->fd);
399 #ifdef ENABLE_PIPE_R300
400 if (strcmp(pm->driver, "r300") == 0) {
401 struct radeon_winsys *sws = radeon_drm_winsys_create(pm->fd);
404 screen = r300_screen_create(sws);
410 #ifdef ENABLE_PIPE_R600
411 if (strcmp(pm->driver, "r600") == 0) {
412 struct radeon *rw = r600_drm_winsys_create(pm->fd);
415 screen = r600_screen_create(rw);
421 #ifdef ENABLE_PIPE_VMWGFX
422 if (strcmp(pm->driver, "vmwgfx") == 0) {
423 struct svga_winsys_screen *sws =
424 svga_drm_winsys_screen_create(pm->fd);
427 screen = svga_screen_create(sws);
435 ALOGW("failed to create screen for %s", pm->driver);
439 pm->screen = debug_screen_wrap(screen);
445 #include <i915_drm.h>
446 #include <radeon_drm.h>
447 static int pipe_get_pci_id(struct pipe_manager *pm,
448 const char *name, int *vendor, int *device)
452 if (strcmp(name, "i915") == 0) {
453 struct drm_i915_getparam gp;
457 memset(&gp, 0, sizeof(gp));
458 gp.param = I915_PARAM_CHIPSET_ID;
460 err = drmCommandWriteRead(pm->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
462 else if (strcmp(name, "radeon") == 0) {
463 struct drm_radeon_info info;
467 memset(&info, 0, sizeof(info));
468 info.request = RADEON_INFO_DEVICE_ID;
469 info.value = (long) device;
470 err = drmCommandWriteRead(pm->fd, DRM_RADEON_INFO, &info, sizeof(info));
472 else if (strcmp(name, "nouveau") == 0) {
477 else if (strcmp(name, "vmwgfx") == 0) {
490 #define DRIVER_MAP_GALLIUM_ONLY
491 #include "pci_ids/pci_id_driver_map.h"
492 static int pipe_find_driver(struct pipe_manager *pm, const char *name)
498 err = pipe_get_pci_id(pm, name, &vendor, &device);
502 /* look up in the driver map */
503 for (idx = 0; driver_map[idx].driver; idx++) {
506 if (vendor != driver_map[idx].vendor_id)
509 if (driver_map[idx].num_chips_ids == -1)
512 for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
513 if (driver_map[idx].chip_ids[i] == device)
516 if (i < driver_map[idx].num_chips_ids)
520 driver = driver_map[idx].driver;
521 err = (driver) ? 0 : -ENODEV;
524 if (strcmp(name, "vmwgfx") == 0) {
531 strncpy(pm->driver, driver, sizeof(pm->driver) - 1);
536 struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_pipe(int fd, const char *name)
538 struct pipe_manager *pm;
540 pm = CALLOC(1, sizeof(*pm));
542 ALOGE("failed to allocate pipe manager for %s", name);
547 pthread_mutex_init(&pm->mutex, NULL);
549 if (pipe_find_driver(pm, name)) {
554 if (pipe_init_screen(pm)) {
559 pm->base.destroy = pipe_destroy;
560 pm->base.init_kms_features = pipe_init_kms_features;
561 pm->base.alloc = pipe_alloc;
562 pm->base.free = pipe_free;
563 pm->base.map = pipe_map;
564 pm->base.unmap = pipe_unmap;
565 pm->base.copy = pipe_copy;