OSDN Git Service

Change all occurances of LOGE to ALOGE, LOGW to ALOGW and LOGI to ALOGI due to change...
[android-x86/external-drm_gralloc.git] / gralloc_drm_pipe.c
1 /*
2  * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
3  * Copyright (C) 2010-2011 LunarG Inc.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
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.
22  */
23
24 #define LOG_TAG "GRALLOC-PIPE"
25
26 #include <cutils/log.h>
27 #include <errno.h>
28
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>
34
35 #include "gralloc_drm.h"
36 #include "gralloc_drm_priv.h"
37
38 struct pipe_manager {
39         struct gralloc_drm_drv_t base;
40
41         int fd;
42         char driver[16];
43         pthread_mutex_t mutex;
44         struct pipe_screen *screen;
45         struct pipe_context *context;
46 };
47
48 struct pipe_buffer {
49         struct gralloc_drm_bo_t base;
50
51         struct pipe_resource *resource;
52         struct winsys_handle winsys;
53
54         struct pipe_transfer *transfer;
55 };
56
57 static enum pipe_format get_pipe_format(int format)
58 {
59         enum pipe_format fmt;
60
61         switch (format) {
62         case HAL_PIXEL_FORMAT_RGBA_8888:
63                 fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
64                 break;
65         case HAL_PIXEL_FORMAT_RGBX_8888:
66                 fmt = PIPE_FORMAT_R8G8B8X8_UNORM;
67                 break;
68         case HAL_PIXEL_FORMAT_RGB_888:
69                 fmt = PIPE_FORMAT_R8G8B8_UNORM;
70                 break;
71         case HAL_PIXEL_FORMAT_RGB_565:
72                 fmt = PIPE_FORMAT_B5G6R5_UNORM;
73                 break;
74         case HAL_PIXEL_FORMAT_BGRA_8888:
75                 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
76                 break;
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:
82         default:
83                 fmt = PIPE_FORMAT_NONE;
84                 break;
85         }
86
87         return fmt;
88 }
89
90 static unsigned get_pipe_bind(int usage)
91 {
92         unsigned bind = PIPE_BIND_SHARED;
93
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;
98
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;
106         }
107
108         return bind;
109 }
110
111 static struct pipe_buffer *get_pipe_buffer_locked(struct pipe_manager *pm,
112                 const struct gralloc_drm_handle_t *handle)
113 {
114         struct pipe_buffer *buf;
115         struct pipe_resource templ;
116
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;
121
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);
126                 return NULL;
127         }
128
129         buf = CALLOC(1, sizeof(*buf));
130         if (!buf) {
131                 ALOGE("failed to allocate pipe buffer");
132                 return NULL;
133         }
134
135         templ.width0 = handle->width;
136         templ.height0 = handle->height;
137         templ.depth0 = 1;
138         templ.array_size = 1;
139
140         if (handle->name) {
141                 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
142                 buf->winsys.handle = handle->name;
143                 buf->winsys.stride = handle->stride;
144
145                 buf->resource = pm->screen->resource_from_handle(pm->screen,
146                                 &templ, &buf->winsys);
147                 if (!buf->resource)
148                         goto fail;
149         }
150         else {
151                 buf->resource =
152                         pm->screen->resource_create(pm->screen, &templ);
153                 if (!buf->resource)
154                         goto fail;
155
156                 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
157                 if (!pm->screen->resource_get_handle(pm->screen,
158                                         buf->resource, &buf->winsys))
159                         goto fail;
160         }
161
162         /* need the gem handle for fb */
163         if (handle->usage & GRALLOC_USAGE_HW_FB) {
164                 struct winsys_handle tmp;
165
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))
170                         goto fail;
171
172                 buf->base.fb_handle = tmp.handle;
173         }
174
175         return buf;
176
177 fail:
178         ALOGE("failed to allocate pipe buffer");
179         if (buf->resource)
180                 pipe_resource_reference(&buf->resource, NULL);
181         FREE(buf);
182
183         return NULL;
184 }
185
186 static struct gralloc_drm_bo_t *pipe_alloc(struct gralloc_drm_drv_t *drv,
187                 struct gralloc_drm_handle_t *handle)
188 {
189         struct pipe_manager *pm = (struct pipe_manager *) drv;
190         struct pipe_buffer *buf;
191
192         pthread_mutex_lock(&pm->mutex);
193         buf = get_pipe_buffer_locked(pm, handle);
194         pthread_mutex_unlock(&pm->mutex);
195
196         if (buf) {
197                 handle->name = (int) buf->winsys.handle;
198                 handle->stride = (int) buf->winsys.stride;
199
200                 buf->base.handle = handle;
201         }
202
203         return &buf->base;
204 }
205
206 static void pipe_free(struct gralloc_drm_drv_t *drv, struct gralloc_drm_bo_t *bo)
207 {
208         struct pipe_manager *pm = (struct pipe_manager *) drv;
209         struct pipe_buffer *buf = (struct pipe_buffer *) bo;
210
211         pthread_mutex_lock(&pm->mutex);
212
213         if (buf->transfer)
214                 pipe_transfer_destroy(pm->context, buf->transfer);
215         pipe_resource_reference(&buf->resource, NULL);
216
217         pthread_mutex_unlock(&pm->mutex);
218
219         FREE(buf);
220 }
221
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)
225 {
226         struct pipe_manager *pm = (struct pipe_manager *) drv;
227         struct pipe_buffer *buf = (struct pipe_buffer *) bo;
228         int err = 0;
229
230         pthread_mutex_lock(&pm->mutex);
231
232         /* need a context to get transfer */
233         if (!pm->context) {
234                 pm->context = pm->screen->context_create(pm->screen, NULL);
235                 if (!pm->context) {
236                         ALOGE("failed to create pipe context");
237                         err = -ENOMEM;
238                 }
239         }
240
241         if (!err) {
242                 enum pipe_transfer_usage usage;
243
244                 usage = PIPE_TRANSFER_READ;
245                 if (enable_write)
246                         usage |= PIPE_TRANSFER_WRITE;
247
248                 assert(!buf->transfer);
249
250                 /*
251                  * ignore x, y, w and h so that returned addr points at the
252                  * start of the buffer
253                  */
254                 buf->transfer = pipe_get_transfer(pm->context, buf->resource,
255                                 0, 0, usage, 0, 0,
256                                 buf->resource->width0, buf->resource->height0);
257                 if (buf->transfer)
258                         *addr = pipe_transfer_map(pm->context, buf->transfer);
259                 else
260                         err = -ENOMEM;
261         }
262
263         pthread_mutex_unlock(&pm->mutex);
264
265         return err;
266 }
267
268 static void pipe_unmap(struct gralloc_drm_drv_t *drv,
269                 struct gralloc_drm_bo_t *bo)
270 {
271         struct pipe_manager *pm = (struct pipe_manager *) drv;
272         struct pipe_buffer *buf = (struct pipe_buffer *) bo;
273
274         pthread_mutex_lock(&pm->mutex);
275
276         assert(buf && buf->transfer);
277
278         pipe_transfer_unmap(pm->context, buf->transfer);
279         pipe_transfer_destroy(pm->context, buf->transfer);
280         buf->transfer = NULL;
281
282         pm->context->flush(pm->context, NULL);
283
284         pthread_mutex_unlock(&pm->mutex);
285 }
286
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)
291 {
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;
296
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");
302                 return;
303         }
304
305         if (x1 < 0)
306                 x1 = 0;
307         if (y1 < 0)
308                 y1 = 0;
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;
313
314         if (x2 <= x1 || y2 <= y1)
315                 return;
316
317         u_box_2d(x1, y1, x2 - x1, y2 - y1, &src_box);
318
319         pthread_mutex_lock(&pm->mutex);
320
321         /* need a context for copying */
322         if (!pm->context) {
323                 pm->context = pm->screen->context_create(pm->screen, NULL);
324                 if (!pm->context) {
325                         ALOGE("failed to create pipe context");
326                         pthread_mutex_unlock(&pm->mutex);
327                         return;
328                 }
329         }
330
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);
335
336         pthread_mutex_unlock(&pm->mutex);
337 }
338
339 static void pipe_init_kms_features(struct gralloc_drm_drv_t *drv, struct gralloc_drm_t *drm)
340 {
341         struct pipe_manager *pm = (struct pipe_manager *) drv;
342
343         switch (drm->fb_format) {
344         case HAL_PIXEL_FORMAT_BGRA_8888:
345         case HAL_PIXEL_FORMAT_RGB_565:
346                 break;
347         default:
348                 drm->fb_format = HAL_PIXEL_FORMAT_BGRA_8888;
349                 break;
350         }
351
352         if (strcmp(pm->driver, "vmwgfx") == 0) {
353                 drm->mode_quirk_vmwgfx = 1;
354                 drm->swap_mode = DRM_SWAP_COPY;
355         }
356         else {
357                 drm->mode_quirk_vmwgfx = 0;
358                 drm->swap_mode = DRM_SWAP_FLIP;
359         }
360         drm->mode_sync_flip = 1;
361         drm->swap_interval = 1;
362         drm->vblank_secondary = 0;
363 }
364
365 static void pipe_destroy(struct gralloc_drm_drv_t *drv)
366 {
367         struct pipe_manager *pm = (struct pipe_manager *) drv;
368
369         if (pm->context)
370                 pm->context->destroy(pm->context);
371         pm->screen->destroy(pm->screen);
372         FREE(pm);
373 }
374
375 /* for nouveau */
376 #include "nouveau/drm/nouveau_drm_public.h"
377 /* for r300 */
378 #include "radeon/drm/radeon_drm_public.h"
379 #include "r300/r300_public.h"
380 /* for r600 */
381 #include "r600/drm/r600_drm_public.h"
382 #include "r600/r600_public.h"
383 /* for vmwgfx */
384 #include "svga/drm/svga_drm_public.h"
385 #include "svga/svga_winsys.h"
386 #include "svga/svga_public.h"
387 /* for debug */
388 #include "target-helpers/inline_debug_helper.h"
389
390 static int pipe_init_screen(struct pipe_manager *pm)
391 {
392         struct pipe_screen *screen = NULL;
393
394 #ifdef ENABLE_PIPE_NOUVEAU
395         if (strcmp(pm->driver, "nouveau") == 0)
396                 screen = nouveau_drm_screen_create(pm->fd);
397 #endif
398 #ifdef ENABLE_PIPE_R300
399         if (strcmp(pm->driver, "r300") == 0) {
400                 struct radeon_winsys *sws = radeon_drm_winsys_create(pm->fd);
401
402                 if (sws) {
403                         screen = r300_screen_create(sws);
404                         if (!screen)
405                                 sws->destroy(sws);
406                 }
407         }
408 #endif
409 #ifdef ENABLE_PIPE_R600
410         if (strcmp(pm->driver, "r600") == 0) {
411                 struct radeon *rw = r600_drm_winsys_create(pm->fd);
412
413                 if (rw) {
414                         screen = r600_screen_create(rw);
415                         if (!screen)
416                                 FREE(rw);
417                 }
418         }
419 #endif
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);
424
425                 if (sws) {
426                         screen = svga_screen_create(sws);
427                         if (!screen)
428                                 sws->destroy(sws);
429                 }
430         }
431 #endif
432
433         if (!screen) {
434                 ALOGW("failed to create screen for %s", pm->driver);
435                 return -EINVAL;
436         }
437
438         pm->screen = debug_screen_wrap(screen);
439
440         return 0;
441 }
442
443 #include <xf86drm.h>
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)
448 {
449         int err = -EINVAL;
450
451         if (strcmp(name, "i915") == 0) {
452                 struct drm_i915_getparam gp;
453
454                 *vendor = 0x8086;
455
456                 memset(&gp, 0, sizeof(gp));
457                 gp.param = I915_PARAM_CHIPSET_ID;
458                 gp.value = device;
459                 err = drmCommandWriteRead(pm->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
460         }
461         else if (strcmp(name, "radeon") == 0) {
462                 struct drm_radeon_info info;
463
464                 *vendor = 0x1002;
465
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));
470         }
471         else if (strcmp(name, "nouveau") == 0) {
472                 *vendor = 0x10de;
473                 *device = 0;
474                 err = 0;
475         }
476         else if (strcmp(name, "vmwgfx") == 0) {
477                 *vendor = 0x15ad;
478                 /* assume SVGA II */
479                 *device = 0x0405;
480                 err = 0;
481         }
482         else {
483                 err = -EINVAL;
484         }
485
486         return err;
487 }
488
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)
492 {
493         int vendor, device;
494         int err;
495         const char *driver;
496
497         err = pipe_get_pci_id(pm, name, &vendor, &device);
498         if (!err) {
499                 int idx;
500
501                 /* look up in the driver map */
502                 for (idx = 0; driver_map[idx].driver; idx++) {
503                         int i;
504
505                         if (vendor != driver_map[idx].vendor_id)
506                                 continue;
507
508                         if (driver_map[idx].num_chips_ids == -1)
509                                 break;
510
511                         for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
512                                 if (driver_map[idx].chip_ids[i] == device)
513                                         break;
514                         }
515                         if (i < driver_map[idx].num_chips_ids)
516                                 break;
517                 }
518
519                 driver = driver_map[idx].driver;
520                 err = (driver) ? 0 : -ENODEV;
521         }
522         else {
523                 if (strcmp(name, "vmwgfx") == 0) {
524                         driver = "vmwgfx";
525                         err = 0;
526                 }
527         }
528
529         if (!err)
530                 strncpy(pm->driver, driver, sizeof(pm->driver) - 1);
531
532         return err;
533 }
534
535 struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_pipe(int fd, const char *name)
536 {
537         struct pipe_manager *pm;
538
539         pm = CALLOC(1, sizeof(*pm));
540         if (!pm) {
541                 ALOGE("failed to allocate pipe manager for %s", name);
542                 return NULL;
543         }
544
545         pm->fd = fd;
546         pthread_mutex_init(&pm->mutex, NULL);
547
548         if (pipe_find_driver(pm, name)) {
549                 FREE(pm);
550                 return NULL;
551         }
552
553         if (pipe_init_screen(pm)) {
554                 FREE(pm);
555                 return NULL;
556         }
557
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;
565
566         return &pm->base;
567 }