Debian Codegraph @

src/gallium/winsys/sw/kms-dri/kms_dri_sw_winsys.c

raw
1 /**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 * Copyright 2010 George Sapountzis <[email protected]>
6 * 2013 Red Hat, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stddef.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <errno.h>
37
38 #include <sys/types.h>
39 #include <sys/mman.h>
40 #include <unistd.h>
41 #include <dlfcn.h>
42 #include <fcntl.h>
43 #include <xf86drm.h>
44
45 #include "drm-uapi/drm_fourcc.h"
46 #include "util/compiler.h"
47 #include "util/format/u_formats.h"
48 #include "pipe/p_state.h"
49 #include "util/u_inlines.h"
50 #include "util/format/u_format.h"
51 #include "util/u_math.h"
52 #include "util/u_memory.h"
53 #include "util/list.h"
54
55 #include "frontend/sw_winsys.h"
56 #include "frontend/drm_driver.h"
57 #include "kms_dri_sw_winsys.h"
58
59 #include "util/simple_mtx.h"
60
61 #if MESA_DEBUG
62 #define DEBUG_PRINT(msg, ...) fprintf(stderr, msg, __VA_ARGS__)
63 #else
64 #define DEBUG_PRINT(msg, ...)
65 #endif
66
67 struct kms_sw_displaytarget;
68
69 struct kms_sw_plane
70 {
71 unsigned width;
72 unsigned height;
73 unsigned stride;
74 unsigned offset;
75 struct kms_sw_displaytarget *dt;
76 struct list_head link;
77 };
78
79 struct kms_sw_displaytarget
80 {
81 enum pipe_format format;
82 unsigned size;
83
84 uint32_t handle;
85 void *mapped;
86 void *ro_mapped;
87
88 int ref_count;
89 int map_count;
90 struct list_head link;
91 struct list_head planes;
92 mtx_t map_lock;
93 };
94
95 struct kms_sw_winsys
96 {
97 struct sw_winsys base;
98
99 int fd;
100 struct list_head bo_list;
101 };
102
103 static inline struct kms_sw_plane *
104 kms_sw_plane( struct sw_displaytarget *dt )
105 {
106 return (struct kms_sw_plane *)dt;
107 }
108
109 static inline struct sw_displaytarget *
110 sw_displaytarget( struct kms_sw_plane *pl)
111 {
112 return (struct sw_displaytarget *)pl;
113 }
114
115 static inline struct kms_sw_winsys *
116 kms_sw_winsys( struct sw_winsys *ws )
117 {
118 return (struct kms_sw_winsys *)ws;
119 }
120
121
122 static bool
123 kms_sw_is_displaytarget_format_supported( struct sw_winsys *ws,
124 unsigned tex_usage,
125 enum pipe_format format )
126 {
127 /* TODO: check visuals or other sensible thing here */
128 return true;
129 }
130
131 static struct kms_sw_plane *get_plane(struct kms_sw_displaytarget *kms_sw_dt,
132 enum pipe_format format,
133 unsigned width, unsigned height,
134 unsigned stride, unsigned offset)
135 {
136 struct kms_sw_plane *plane = NULL;
137
138 if (offset + util_format_get_2d_size(format, stride, height) >
139 kms_sw_dt->size) {
140 DEBUG_PRINT("KMS-DEBUG: plane too big. format: %d stride: %d height: %d "
141 "offset: %d size:%d\n", format, stride, height, offset,
142 kms_sw_dt->size);
143 return NULL;
144 }
145
146 LIST_FOR_EACH_ENTRY(plane, &kms_sw_dt->planes, link) {
147 if (plane->offset == offset)
148 return plane;
149 }
150
151 plane = CALLOC_STRUCT(kms_sw_plane);
152 if (!plane)
153 return NULL;
154
155 plane->width = width;
156 plane->height = height;
157 plane->stride = stride;
158 plane->offset = offset;
159 plane->dt = kms_sw_dt;
160 list_add(&plane->link, &kms_sw_dt->planes);
161 return plane;
162 }
163
164 static struct sw_displaytarget *
165 kms_sw_displaytarget_create(struct sw_winsys *ws,
166 unsigned tex_usage,
167 enum pipe_format format,
168 unsigned width, unsigned height,
169 unsigned alignment,
170 const void *front_private,
171 unsigned *stride)
172 {
173 struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);
174 struct kms_sw_displaytarget *kms_sw_dt;
175 struct drm_mode_create_dumb create_req;
176 struct drm_mode_destroy_dumb destroy_req;
177 int ret;
178
179 kms_sw_dt = CALLOC_STRUCT(kms_sw_displaytarget);
180 if (!kms_sw_dt)
181 goto no_dt;
182
183 list_inithead(&kms_sw_dt->planes);
184 kms_sw_dt->ref_count = 1;
185 kms_sw_dt->mapped = MAP_FAILED;
186 kms_sw_dt->ro_mapped = MAP_FAILED;
187
188 kms_sw_dt->format = format;
189
190 mtx_init(&kms_sw_dt->map_lock, mtx_plain);
191
192 memset(&create_req, 0, sizeof(create_req));
193 create_req.bpp = util_format_get_blocksizebits(format);
194 create_req.width = width;
195 create_req.height = height;
196 ret = drmIoctl(kms_sw->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_req);
197 if (ret) {
198 fprintf(stderr, "KMS: DRM_IOCTL_MODE_CREATE_DUMB failed: %s\n", strerror(errno));
199 goto free_bo;
200 }
201
202 kms_sw_dt->size = create_req.size;
203 kms_sw_dt->handle = create_req.handle;
204 struct kms_sw_plane *plane = get_plane(kms_sw_dt, format, width, height,
205 create_req.pitch, 0);
206 if (!plane)
207 goto free_bo;
208
209 list_add(&kms_sw_dt->link, &kms_sw->bo_list);
210
211 DEBUG_PRINT("KMS-DEBUG: created buffer %u (size %u)\n", kms_sw_dt->handle, kms_sw_dt->size);
212
213 *stride = create_req.pitch;
214 return sw_displaytarget(plane);
215
216 free_bo:
217 memset(&destroy_req, 0, sizeof destroy_req);
218 destroy_req.handle = create_req.handle;
219 drmIoctl(kms_sw->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_req);
220 FREE(kms_sw_dt);
221 no_dt:
222 return NULL;
223 }
224
225 static struct sw_displaytarget *
226 kms_sw_displaytarget_create_mapped(struct sw_winsys *ws,
227 unsigned tex_usage,
228 enum pipe_format format,
229 unsigned width, unsigned height,
230 unsigned stride,
231 void *data,
232 struct winsys_handle *whandle)
233 {
234 struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);
235 struct kms_sw_displaytarget *kms_sw_dt;
236 unsigned nblocksy, size;
237
238 kms_sw_dt = CALLOC_STRUCT(kms_sw_displaytarget);
239 if (!kms_sw_dt)
240 return NULL;
241
242 list_inithead(&kms_sw_dt->planes);
243 kms_sw_dt->ref_count = 1;
244 kms_sw_dt->mapped = data;
245 kms_sw_dt->ro_mapped = MAP_FAILED;
246
247 kms_sw_dt->format = format;
248
249 mtx_init(&kms_sw_dt->map_lock, mtx_plain);
250
251 nblocksy = util_format_get_nblocksy(format, height);
252 size = stride * nblocksy;
253 kms_sw_dt->size = size;
254 kms_sw_dt->handle = -1;
255 struct kms_sw_plane *plane = get_plane(kms_sw_dt, format, width, height,
256 stride, 0);
257 if (!plane) {
258 FREE(kms_sw_dt);
259 return NULL;
260 }
261
262 list_add(&kms_sw_dt->link, &kms_sw->bo_list);
263
264 if (whandle) {
265 uint32_t handle = -1;
266
267 drmPrimeFDToHandle(kms_sw->fd, whandle->handle, &handle);
268 kms_sw_dt->handle = handle;
269 }
270
271 return sw_displaytarget(plane);
272 }
273
274 static void
275 kms_sw_displaytarget_destroy(struct sw_winsys *ws,
276 struct sw_displaytarget *dt)
277 {
278 struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);
279 struct kms_sw_plane *plane = kms_sw_plane(dt);
280 struct kms_sw_displaytarget *kms_sw_dt = plane->dt;
281
282 kms_sw_dt->ref_count --;
283 if (kms_sw_dt->ref_count > 0)
284 return;
285
286 if (kms_sw_dt->map_count > 0) {
287 DEBUG_PRINT("KMS-DEBUG: leaked map buffer %u\n", kms_sw_dt->handle);
288 }
289
290 if (kms_sw_dt->handle != -1) {
291 struct drm_mode_destroy_dumb destroy_req;
292
293 memset(&destroy_req, 0, sizeof destroy_req);
294 destroy_req.handle = kms_sw_dt->handle;
295 drmIoctl(kms_sw->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_req);
296 }
297
298 list_del(&kms_sw_dt->link);
299
300 mtx_destroy(&kms_sw_dt->map_lock);
301 DEBUG_PRINT("KMS-DEBUG: destroyed buffer %u\n", kms_sw_dt->handle);
302
303 struct kms_sw_plane *tmp;
304 LIST_FOR_EACH_ENTRY_SAFE(plane, tmp, &kms_sw_dt->planes, link) {
305 FREE(plane);
306 }
307
308 FREE(kms_sw_dt);
309 }
310
311 static void *
312 kms_sw_displaytarget_map(struct sw_winsys *ws,
313 struct sw_displaytarget *dt,
314 unsigned flags)
315 {
316 struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);
317 struct kms_sw_plane *plane = kms_sw_plane(dt);
318 struct kms_sw_displaytarget *kms_sw_dt = plane->dt;
319 struct drm_mode_map_dumb map_req;
320 int prot, ret;
321
322 mtx_lock(&kms_sw_dt->map_lock);
323 memset(&map_req, 0, sizeof map_req);
324 map_req.handle = kms_sw_dt->handle;
325 ret = drmIoctl(kms_sw->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_req);
326 if (ret)
327 goto fail_locked;
328
329 prot = (flags == PIPE_MAP_READ) ? PROT_READ : (PROT_READ | PROT_WRITE);
330 void **ptr = (flags == PIPE_MAP_READ) ? &kms_sw_dt->ro_mapped : &kms_sw_dt->mapped;
331 if (*ptr == MAP_FAILED) {
332 void *tmp = mmap(NULL, kms_sw_dt->size, prot, MAP_SHARED,
333 kms_sw->fd, map_req.offset);
334 if (tmp == MAP_FAILED)
335 goto fail_locked;
336 *ptr = tmp;
337 }
338
339 DEBUG_PRINT("KMS-DEBUG: mapped buffer %u (size %u) at %p\n",
340 kms_sw_dt->handle, kms_sw_dt->size, *ptr);
341
342 kms_sw_dt->map_count++;
343
344 mtx_unlock(&kms_sw_dt->map_lock);
345
346 return *ptr + plane->offset;
347 fail_locked:
348 mtx_unlock(&kms_sw_dt->map_lock);
349 return NULL;
350 }
351
352 static struct kms_sw_displaytarget *
353 kms_sw_displaytarget_find_and_ref(struct kms_sw_winsys *kms_sw,
354 unsigned int kms_handle)
355 {
356 struct kms_sw_displaytarget *kms_sw_dt;
357
358 LIST_FOR_EACH_ENTRY(kms_sw_dt, &kms_sw->bo_list, link) {
359 if (kms_sw_dt->handle == kms_handle) {
360 kms_sw_dt->ref_count++;
361
362 DEBUG_PRINT("KMS-DEBUG: imported buffer %u (size %u)\n",
363 kms_sw_dt->handle, kms_sw_dt->size);
364
365 return kms_sw_dt;
366 }
367 }
368
369 return NULL;
370 }
371
372 static struct kms_sw_plane *
373 kms_sw_displaytarget_add_from_prime(struct kms_sw_winsys *kms_sw, int fd,
374 enum pipe_format format,
375 unsigned width, unsigned height,
376 unsigned stride, unsigned offset)
377 {
378 uint32_t handle = -1;
379 struct kms_sw_displaytarget * kms_sw_dt;
380 int ret;
381
382 ret = drmPrimeFDToHandle(kms_sw->fd, fd, &handle);
383
384 if (ret)
385 return NULL;
386
387 kms_sw_dt = kms_sw_displaytarget_find_and_ref(kms_sw, handle);
388 struct kms_sw_plane *plane = NULL;
389 if (kms_sw_dt) {
390 plane = get_plane(kms_sw_dt, format, width, height, stride, offset);
391 if (!plane)
392 kms_sw_dt->ref_count --;
393 return plane;
394 }
395
396 kms_sw_dt = CALLOC_STRUCT(kms_sw_displaytarget);
397 if (!kms_sw_dt)
398 return NULL;
399
400 list_inithead(&kms_sw_dt->planes);
401 off_t lseek_ret = lseek(fd, 0, SEEK_END);
402 if (lseek_ret == -1) {
403 FREE(kms_sw_dt);
404 return NULL;
405 }
406 kms_sw_dt->mapped = MAP_FAILED;
407 kms_sw_dt->ro_mapped = MAP_FAILED;
408 kms_sw_dt->size = lseek_ret;
409 kms_sw_dt->ref_count = 1;
410 kms_sw_dt->handle = handle;
411
412 lseek(fd, 0, SEEK_SET);
413 plane = get_plane(kms_sw_dt, format, width, height, stride, offset);
414 if (!plane) {
415 FREE(kms_sw_dt);
416 return NULL;
417 }
418
419 list_add(&kms_sw_dt->link, &kms_sw->bo_list);
420
421 return plane;
422 }
423
424 static void
425 kms_sw_displaytarget_unmap(struct sw_winsys *ws,
426 struct sw_displaytarget *dt)
427 {
428 struct kms_sw_plane *plane = kms_sw_plane(dt);
429 struct kms_sw_displaytarget *kms_sw_dt = plane->dt;
430
431 mtx_lock(&kms_sw_dt->map_lock);
432 if (!kms_sw_dt->map_count) {
433 DEBUG_PRINT("KMS-DEBUG: ignore duplicated unmap %u", kms_sw_dt->handle);
434 mtx_unlock(&kms_sw_dt->map_lock);
435 return;
436 }
437 kms_sw_dt->map_count--;
438 if (kms_sw_dt->map_count) {
439 DEBUG_PRINT("KMS-DEBUG: ignore unmap for busy buffer %u", kms_sw_dt->handle);
440 mtx_unlock(&kms_sw_dt->map_lock);
441 return;
442 }
443
444 DEBUG_PRINT("KMS-DEBUG: unmapped buffer %u (was %p)\n", kms_sw_dt->handle, kms_sw_dt->mapped);
445 DEBUG_PRINT("KMS-DEBUG: unmapped buffer %u (was %p)\n", kms_sw_dt->handle, kms_sw_dt->ro_mapped);
446
447 if (kms_sw_dt->mapped != MAP_FAILED) {
448 munmap(kms_sw_dt->mapped, kms_sw_dt->size);
449 kms_sw_dt->mapped = MAP_FAILED;
450 }
451 if (kms_sw_dt->ro_mapped != MAP_FAILED) {
452 munmap(kms_sw_dt->ro_mapped, kms_sw_dt->size);
453 kms_sw_dt->ro_mapped = MAP_FAILED;
454 }
455 mtx_unlock(&kms_sw_dt->map_lock);
456 }
457
458 static struct sw_displaytarget *
459 kms_sw_displaytarget_from_handle(struct sw_winsys *ws,
460 const struct pipe_resource *templ,
461 struct winsys_handle *whandle,
462 unsigned *stride)
463 {
464 struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);
465 struct kms_sw_displaytarget *kms_sw_dt;
466 struct kms_sw_plane *kms_sw_pl;
467
468
469 assert(whandle->type == WINSYS_HANDLE_TYPE_KMS ||
470 whandle->type == WINSYS_HANDLE_TYPE_FD);
471
472 switch(whandle->type) {
473 case WINSYS_HANDLE_TYPE_FD:
474 kms_sw_pl = kms_sw_displaytarget_add_from_prime(kms_sw, whandle->handle,
475 templ->format,
476 templ->width0,
477 templ->height0,
478 whandle->stride,
479 whandle->offset);
480 if (kms_sw_pl)
481 *stride = kms_sw_pl->stride;
482 return sw_displaytarget(kms_sw_pl);
483 case WINSYS_HANDLE_TYPE_KMS:
484 kms_sw_dt = kms_sw_displaytarget_find_and_ref(kms_sw, whandle->handle);
485 if (kms_sw_dt) {
486 struct kms_sw_plane *plane;
487 LIST_FOR_EACH_ENTRY(plane, &kms_sw_dt->planes, link) {
488 if (whandle->offset == plane->offset) {
489 *stride = plane->stride;
490 return sw_displaytarget(plane);
491 }
492 }
493 kms_sw_dt->ref_count --;
494 }
495 FALLTHROUGH;
496 default:
497 break;
498 }
499
500 assert(0);
501 return NULL;
502 }
503
504 static bool
505 kms_sw_displaytarget_get_handle(struct sw_winsys *winsys,
506 struct sw_displaytarget *dt,
507 struct winsys_handle *whandle)
508 {
509 struct kms_sw_winsys *kms_sw = kms_sw_winsys(winsys);
510 struct kms_sw_plane *plane = kms_sw_plane(dt);
511 struct kms_sw_displaytarget *kms_sw_dt = plane->dt;
512
513 switch(whandle->type) {
514 case WINSYS_HANDLE_TYPE_KMS:
515 whandle->handle = kms_sw_dt->handle;
516 whandle->stride = plane->stride;
517 whandle->offset = plane->offset;
518 whandle->modifier = DRM_FORMAT_MOD_LINEAR;
519 return true;
520 case WINSYS_HANDLE_TYPE_FD:
521 if (!drmPrimeHandleToFD(kms_sw->fd, kms_sw_dt->handle,
522 DRM_CLOEXEC | DRM_RDWR, (int*)&whandle->handle)) {
523 whandle->stride = plane->stride;
524 whandle->offset = plane->offset;
525 whandle->modifier = DRM_FORMAT_MOD_LINEAR;
526 return true;
527 }
528 FALLTHROUGH;
529 default:
530 whandle->handle = 0;
531 whandle->stride = 0;
532 whandle->offset = 0;
533 return false;
534 }
535 }
536
537 static void
538 kms_sw_displaytarget_display(struct sw_winsys *ws,
539 struct sw_displaytarget *dt,
540 void *context_private,
541 unsigned nboxes,
542 struct pipe_box *box)
543 {
544 /* This function should not be called, instead the dri2 loader should
545 handle swap buffers internally.
546 */
547 assert(0);
548 }
549
550 static int
551 kms_sw_winsys_get_fd(struct sw_winsys *ws)
552 {
553 struct kms_sw_winsys *kms_sw = kms_sw_winsys(ws);
554
555 return kms_sw->fd;
556 }
557
558 static void
559 kms_destroy_sw_winsys(struct sw_winsys *winsys)
560 {
561 FREE(winsys);
562 }
563
564 struct sw_winsys *
565 kms_dri_create_winsys(int fd)
566 {
567 struct kms_sw_winsys *ws;
568
569 ws = CALLOC_STRUCT(kms_sw_winsys);
570 if (!ws)
571 return NULL;
572
573 ws->fd = fd;
574 list_inithead(&ws->bo_list);
575
576 ws->base.destroy = kms_destroy_sw_winsys;
577
578 ws->base.get_fd = kms_sw_winsys_get_fd;
579
580 ws->base.is_displaytarget_format_supported = kms_sw_is_displaytarget_format_supported;
581
582 /* screen texture functions */
583 ws->base.displaytarget_create = kms_sw_displaytarget_create;
584 ws->base.displaytarget_create_mapped = kms_sw_displaytarget_create_mapped;
585 ws->base.displaytarget_destroy = kms_sw_displaytarget_destroy;
586 ws->base.displaytarget_from_handle = kms_sw_displaytarget_from_handle;
587 ws->base.displaytarget_get_handle = kms_sw_displaytarget_get_handle;
588
589 /* texture functions */
590 ws->base.displaytarget_map = kms_sw_displaytarget_map;
591 ws->base.displaytarget_unmap = kms_sw_displaytarget_unmap;
592
593 ws->base.displaytarget_display = kms_sw_displaytarget_display;
594
595 return &ws->base;
596 }
597
598 /* vim: set sw=3 ts=8 sts=3 expandtab: */