OSDN Git Service

support other drivers in pipe
[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         default:
80                 fmt = PIPE_FORMAT_NONE;
81                 break;
82         }
83
84         return fmt;
85 }
86
87 static unsigned get_pipe_bind(int usage)
88 {
89         unsigned bind = PIPE_BIND_SHARED;
90
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;
95
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;
103         }
104
105         return bind;
106 }
107
108 static struct pipe_buffer *get_pipe_buffer_locked(struct pipe_manager *pm,
109                 const struct gralloc_drm_handle_t *handle)
110 {
111         struct pipe_buffer *buf;
112         struct pipe_resource templ;
113
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;
118
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);
123                 return NULL;
124         }
125
126         buf = CALLOC(1, sizeof(*buf));
127         if (!buf) {
128                 LOGE("failed to allocate pipe buffer");
129                 return NULL;
130         }
131
132         templ.width0 = handle->width;
133         templ.height0 = handle->height;
134         templ.depth0 = 1;
135         templ.array_size = 1;
136
137         if (handle->name) {
138                 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
139                 buf->winsys.handle = handle->name;
140                 buf->winsys.stride = handle->stride;
141
142                 buf->resource = pm->screen->resource_from_handle(pm->screen,
143                                 &templ, &buf->winsys);
144                 if (!buf->resource)
145                         goto fail;
146         }
147         else {
148                 buf->resource =
149                         pm->screen->resource_create(pm->screen, &templ);
150                 if (!buf->resource)
151                         goto fail;
152
153                 buf->winsys.type = DRM_API_HANDLE_TYPE_SHARED;
154                 if (!pm->screen->resource_get_handle(pm->screen,
155                                         buf->resource, &buf->winsys))
156                         goto fail;
157         }
158
159         /* need the gem handle for fb */
160         if (handle->usage & GRALLOC_USAGE_HW_FB) {
161                 struct winsys_handle tmp;
162
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))
167                         goto fail;
168
169                 buf->base.fb_handle = tmp.handle;
170         }
171
172         return buf;
173
174 fail:
175         LOGE("failed to allocate pipe buffer");
176         if (buf->resource)
177                 pipe_resource_reference(&buf->resource, NULL);
178         FREE(buf);
179
180         return NULL;
181 }
182
183 static struct gralloc_drm_bo_t *pipe_alloc(struct gralloc_drm_drv_t *drv,
184                 struct gralloc_drm_handle_t *handle)
185 {
186         struct pipe_manager *pm = (struct pipe_manager *) drv;
187         struct pipe_buffer *buf;
188
189         pthread_mutex_lock(&pm->mutex);
190         buf = get_pipe_buffer_locked(pm, handle);
191         pthread_mutex_unlock(&pm->mutex);
192
193         if (buf) {
194                 handle->name = (int) buf->winsys.handle;
195                 handle->stride = (int) buf->winsys.stride;
196
197                 buf->base.handle = handle;
198         }
199
200         return &buf->base;
201 }
202
203 static void pipe_free(struct gralloc_drm_drv_t *drv, struct gralloc_drm_bo_t *bo)
204 {
205         struct pipe_manager *pm = (struct pipe_manager *) drv;
206         struct pipe_buffer *buf = (struct pipe_buffer *) bo;
207
208         pthread_mutex_lock(&pm->mutex);
209
210         if (buf->transfer)
211                 pipe_transfer_destroy(pm->context, buf->transfer);
212         pipe_resource_reference(&buf->resource, NULL);
213
214         pthread_mutex_unlock(&pm->mutex);
215
216         FREE(buf);
217 }
218
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)
222 {
223         struct pipe_manager *pm = (struct pipe_manager *) drv;
224         struct pipe_buffer *buf = (struct pipe_buffer *) bo;
225         int err = 0;
226
227         pthread_mutex_lock(&pm->mutex);
228
229         /* need a context to get transfer */
230         if (!pm->context) {
231                 pm->context = pm->screen->context_create(pm->screen, NULL);
232                 if (!pm->context) {
233                         LOGE("failed to create pipe context");
234                         err = -ENOMEM;
235                 }
236         }
237
238         if (!err) {
239                 enum pipe_transfer_usage usage;
240
241                 usage = PIPE_TRANSFER_READ;
242                 if (enable_write)
243                         usage |= PIPE_TRANSFER_WRITE;
244
245                 assert(!buf->transfer);
246
247                 /*
248                  * ignore x, y, w and h so that returned addr points at the
249                  * start of the buffer
250                  */
251                 buf->transfer = pipe_get_transfer(pm->context, buf->resource,
252                                 0, 0, usage, 0, 0,
253                                 buf->resource->width0, buf->resource->height0);
254                 if (buf->transfer)
255                         *addr = pipe_transfer_map(pm->context, buf->transfer);
256                 else
257                         err = -ENOMEM;
258         }
259
260         pthread_mutex_unlock(&pm->mutex);
261
262         return err;
263 }
264
265 static void pipe_unmap(struct gralloc_drm_drv_t *drv,
266                 struct gralloc_drm_bo_t *bo)
267 {
268         struct pipe_manager *pm = (struct pipe_manager *) drv;
269         struct pipe_buffer *buf = (struct pipe_buffer *) bo;
270
271         pthread_mutex_lock(&pm->mutex);
272
273         assert(buf && buf->transfer);
274
275         pipe_transfer_unmap(pm->context, buf->transfer);
276         pipe_transfer_destroy(pm->context, buf->transfer);
277         buf->transfer = NULL;
278
279         pm->context->flush(pm->context, NULL);
280
281         pthread_mutex_unlock(&pm->mutex);
282 }
283
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)
288 {
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;
293
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");
299                 return;
300         }
301
302         if (x1 < 0)
303                 x1 = 0;
304         if (y1 < 0)
305                 y1 = 0;
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;
310
311         if (x2 <= x1 || y2 <= y1)
312                 return;
313
314         u_box_2d(x1, y1, x2 - x1, y2 - y1, &src_box);
315
316         pthread_mutex_lock(&pm->mutex);
317
318         /* need a context for copying */
319         if (!pm->context) {
320                 pm->context = pm->screen->context_create(pm->screen, NULL);
321                 if (!pm->context) {
322                         LOGE("failed to create pipe context");
323                         pthread_mutex_unlock(&pm->mutex);
324                         return;
325                 }
326         }
327
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);
332
333         pthread_mutex_unlock(&pm->mutex);
334 }
335
336 static void pipe_init_kms_features(struct gralloc_drm_drv_t *drv, struct gralloc_drm_t *drm)
337 {
338         struct pipe_manager *pm = (struct pipe_manager *) drv;
339
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;
346 }
347
348 static void pipe_destroy(struct gralloc_drm_drv_t *drv)
349 {
350         struct pipe_manager *pm = (struct pipe_manager *) drv;
351
352         if (pm->context)
353                 pm->context->destroy(pm->context);
354         pm->screen->destroy(pm->screen);
355         FREE(pm);
356 }
357
358 /* for nouveau */
359 #include "nouveau/drm/nouveau_drm_public.h"
360 /* for r300 */
361 #include "radeon/drm/radeon_drm_public.h"
362 #include "r300/r300_public.h"
363 /* for r600 */
364 #include "r600/drm/r600_drm_public.h"
365 #include "r600/r600_public.h"
366 /* for vmwgfx */
367 #include "svga/drm/svga_drm_public.h"
368 #include "svga/svga_public.h"
369 /* for debug */
370 #include "target-helpers/inline_debug_helper.h"
371
372 static int pipe_init_screen(struct pipe_manager *pm)
373 {
374         struct pipe_screen *screen = NULL;
375
376 #ifdef ENABLE_PIPE_NOUVEAU
377         if (strcmp(pm->driver, "nouveau") == 0)
378                 screen = nouveau_drm_screen_create(pm->fd);
379 #endif
380 #ifdef ENABLE_PIPE_R300
381         if (strcmp(pm->driver, "r300") == 0) {
382                 struct radeon_winsys *sws = radeon_drm_winsys_create(pm->fd);
383
384                 if (sws) {
385                         screen = r300_screen_create(sws);
386                         if (!screen)
387                                 sws->destroy(sws);
388                 }
389         }
390 #endif
391 #ifdef ENABLE_PIPE_R600
392         if (strcmp(pm->driver, "r600") == 0) {
393                 struct radeon *rw = r600_drm_winsys_create(pm->fd);
394
395                 if (rw) {
396                         screen = r600_screen_create(rw);
397                         if (!screen)
398                                 FREE(rw);
399                 }
400         }
401 #endif
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);
406
407                 if (sws) {
408                         screen = svga_screen_create(sws);
409                         if (!screen)
410                                 sws->destroy(sws);
411                 }
412         }
413 #endif
414
415         if (!screen) {
416                 LOGW("failed to create screen for %s", pm->driver);
417                 return -EINVAL;
418         }
419
420         pm->screen = debug_screen_wrap(screen);
421
422         return 0;
423 }
424
425 #include <xf86drm.h>
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)
430 {
431         int err = -EINVAL;
432
433         if (strcmp(name, "i915") == 0) {
434                 struct drm_i915_getparam gp;
435
436                 *vendor = 0x8086;
437
438                 memset(&gp, 0, sizeof(gp));
439                 gp.param = I915_PARAM_CHIPSET_ID;
440                 gp.value = device;
441                 err = drmCommandWriteRead(pm->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
442         }
443         else if (strcmp(name, "radeon") == 0) {
444                 struct drm_radeon_info info;
445
446                 *vendor = 0x1002;
447
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));
452         }
453         else if (strcmp(name, "nouveau") == 0) {
454                 *vendor = 0x10de;
455                 *device = 0;
456                 err = 0;
457         }
458         else {
459                 err = -EINVAL;
460         }
461
462         return err;
463 }
464
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)
468 {
469         int vendor, device;
470         int err;
471         const char *driver;
472
473         err = pipe_get_pci_id(pm, name, &vendor, &device);
474         if (!err) {
475                 int idx;
476
477                 /* look up in the driver map */
478                 for (idx = 0; driver_map[idx].driver; idx++) {
479                         int i;
480
481                         if (vendor != driver_map[idx].vendor_id)
482                                 continue;
483
484                         if (driver_map[idx].num_chips_ids == -1)
485                                 break;
486
487                         for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
488                                 if (driver_map[idx].chip_ids[i] == device)
489                                         break;
490                         }
491                         if (i < driver_map[idx].num_chips_ids)
492                                 break;
493                 }
494
495                 driver = driver_map[idx].driver;
496                 err = (driver) ? 0 : -ENODEV;
497         }
498         else {
499                 if (strcmp(name, "vmwgfx") == 0) {
500                         driver = "vmwgfx";
501                         err = 0;
502                 }
503         }
504
505         if (!err)
506                 strncpy(pm->driver, driver, sizeof(pm->driver) - 1);
507
508         return err;
509 }
510
511 struct gralloc_drm_drv_t *gralloc_drm_drv_create_for_pipe(int fd, const char *name)
512 {
513         struct pipe_manager *pm;
514
515         pm = CALLOC(1, sizeof(*pm));
516         if (!pm) {
517                 LOGE("failed to allocate pipe manager for %s", name);
518                 return NULL;
519         }
520
521         pm->fd = fd;
522         pthread_mutex_init(&pm->mutex, NULL);
523
524         if (pipe_find_driver(pm, name)) {
525                 FREE(pm);
526                 return NULL;
527         }
528
529         if (pipe_init_screen(pm)) {
530                 FREE(pm);
531                 return NULL;
532         }
533
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;
541
542         return &pm->base;
543 }