1 /**************************************************************************
3 * Copyright 2009, VMware, Inc.
5 * Copyright 2010 George Sapountzis <gsapountzis@gmail.com>
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:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
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.
28 **************************************************************************/
37 #include <sys/types.h>
44 #include "pipe/p_compiler.h"
45 #include "pipe/p_format.h"
46 #include "util/u_inlines.h"
47 #include "util/u_format.h"
48 #include "util/u_math.h"
49 #include "util/u_memory.h"
50 #include "util/u_double_list.h"
52 #include "state_tracker/sw_winsys.h"
53 #include "state_tracker/drm_driver.h"
56 #define DEBUG(msg, ...) fprintf(stderr, msg, __VA_ARGS__)
58 #define DEBUG(msg, ...)
63 struct sw_winsys
*kms_dri_create_winsys(int fd
);
65 struct kms_sw_displaytarget
67 enum pipe_format format
;
77 struct list_head link
;
82 struct sw_winsys base
;
85 struct list_head bo_list
;
88 static INLINE
struct kms_sw_displaytarget
*
89 kms_sw_displaytarget( struct sw_displaytarget
*dt
)
91 return (struct kms_sw_displaytarget
*)dt
;
94 static INLINE
struct kms_sw_winsys
*
95 kms_sw_winsys( struct sw_winsys
*ws
)
97 return (struct kms_sw_winsys
*)ws
;
102 kms_sw_is_displaytarget_format_supported( struct sw_winsys
*ws
,
104 enum pipe_format format
)
106 /* TODO: check visuals or other sensible thing here */
110 static struct sw_displaytarget
*
111 kms_sw_displaytarget_create(struct sw_winsys
*ws
,
113 enum pipe_format format
,
114 unsigned width
, unsigned height
,
118 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
119 struct kms_sw_displaytarget
*kms_sw_dt
;
120 struct drm_mode_create_dumb create_req
;
121 struct drm_mode_destroy_dumb destroy_req
;
124 kms_sw_dt
= CALLOC_STRUCT(kms_sw_displaytarget
);
128 kms_sw_dt
->ref_count
= 1;
130 kms_sw_dt
->format
= format
;
131 kms_sw_dt
->width
= width
;
132 kms_sw_dt
->height
= height
;
135 create_req
.width
= width
;
136 create_req
.height
= height
;
137 create_req
.handle
= 0;
138 ret
= drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create_req
);
142 kms_sw_dt
->stride
= create_req
.pitch
;
143 kms_sw_dt
->size
= create_req
.size
;
144 kms_sw_dt
->handle
= create_req
.handle
;
146 list_add(&kms_sw_dt
->link
, &kms_sw
->bo_list
);
148 DEBUG("KMS-DEBUG: created buffer %u (size %u)\n", kms_sw_dt
->handle
, kms_sw_dt
->size
);
150 *stride
= kms_sw_dt
->stride
;
151 return (struct sw_displaytarget
*)kms_sw_dt
;
154 memset(&destroy_req
, 0, sizeof destroy_req
);
155 destroy_req
.handle
= create_req
.handle
;
156 drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_DESTROY_DUMB
, &destroy_req
);
163 kms_sw_displaytarget_destroy(struct sw_winsys
*ws
,
164 struct sw_displaytarget
*dt
)
166 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
167 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
168 struct drm_mode_destroy_dumb destroy_req
;
170 kms_sw_dt
->ref_count
--;
171 if (kms_sw_dt
->ref_count
> 0)
174 memset(&destroy_req
, 0, sizeof destroy_req
);
175 destroy_req
.handle
= kms_sw_dt
->handle
;
176 drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_DESTROY_DUMB
, &destroy_req
);
178 list_del(&kms_sw_dt
->link
);
180 DEBUG("KMS-DEBUG: destroyed buffer %u\n", kms_sw_dt
->handle
);
186 kms_sw_displaytarget_map(struct sw_winsys
*ws
,
187 struct sw_displaytarget
*dt
,
190 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
191 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
192 struct drm_mode_map_dumb map_req
;
195 memset(&map_req
, 0, sizeof map_req
);
196 map_req
.handle
= kms_sw_dt
->handle
;
197 ret
= drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_MAP_DUMB
, &map_req
);
201 prot
= (flags
== PIPE_TRANSFER_READ
) ? PROT_READ
: (PROT_READ
| PROT_WRITE
);
202 kms_sw_dt
->mapped
= mmap(0, kms_sw_dt
->size
, prot
, MAP_SHARED
,
203 kms_sw
->fd
, map_req
.offset
);
205 if (kms_sw_dt
->mapped
== MAP_FAILED
)
208 DEBUG("KMS-DEBUG: mapped buffer %u (size %u) at %p\n",
209 kms_sw_dt
->handle
, kms_sw_dt
->size
, kms_sw_dt
->mapped
);
211 return kms_sw_dt
->mapped
;
214 static struct kms_sw_displaytarget
*
215 kms_sw_displaytarget_add_from_prime(struct kms_sw_winsys
*kms_sw
, int fd
)
217 uint32_t handle
= -1;
218 struct kms_sw_displaytarget
* kms_sw_dt
;
221 ret
= drmPrimeFDToHandle(kms_sw
->fd
, fd
, &handle
);
226 kms_sw_dt
= CALLOC_STRUCT(kms_sw_displaytarget
);
230 kms_sw_dt
->ref_count
= 1;
231 kms_sw_dt
->handle
= handle
;
232 kms_sw_dt
->size
= lseek(fd
, 0, SEEK_END
);
234 if (kms_sw_dt
->size
== (off_t
)-1) {
239 lseek(fd
, 0, SEEK_SET
);
241 list_add(&kms_sw_dt
->link
, &kms_sw
->bo_list
);
247 kms_sw_displaytarget_unmap(struct sw_winsys
*ws
,
248 struct sw_displaytarget
*dt
)
250 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
252 DEBUG("KMS-DEBUG: unmapped buffer %u (was %p)\n", kms_sw_dt
->handle
, kms_sw_dt
->mapped
);
254 munmap(kms_sw_dt
->mapped
, kms_sw_dt
->size
);
255 kms_sw_dt
->mapped
= NULL
;
258 static struct sw_displaytarget
*
259 kms_sw_displaytarget_from_handle(struct sw_winsys
*ws
,
260 const struct pipe_resource
*templ
,
261 struct winsys_handle
*whandle
,
264 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
265 struct kms_sw_displaytarget
*kms_sw_dt
;
267 assert(whandle
->type
== DRM_API_HANDLE_TYPE_KMS
||
268 whandle
->type
== DRM_API_HANDLE_TYPE_FD
);
270 switch(whandle
->type
) {
271 case DRM_API_HANDLE_TYPE_FD
:
272 kms_sw_dt
= kms_sw_displaytarget_add_from_prime(kms_sw
, whandle
->handle
);
274 kms_sw_dt
->ref_count
++;
275 kms_sw_dt
->width
= templ
->width0
;
276 kms_sw_dt
->height
= templ
->height0
;
277 kms_sw_dt
->stride
= whandle
->stride
;
278 *stride
= kms_sw_dt
->stride
;
280 return (struct sw_displaytarget
*)kms_sw_dt
;
281 case DRM_API_HANDLE_TYPE_KMS
:
282 LIST_FOR_EACH_ENTRY(kms_sw_dt
, &kms_sw
->bo_list
, link
) {
283 if (kms_sw_dt
->handle
== whandle
->handle
) {
284 kms_sw_dt
->ref_count
++;
286 DEBUG("KMS-DEBUG: imported buffer %u (size %u)\n", kms_sw_dt
->handle
, kms_sw_dt
->size
);
288 *stride
= kms_sw_dt
->stride
;
289 return (struct sw_displaytarget
*)kms_sw_dt
;
302 kms_sw_displaytarget_get_handle(struct sw_winsys
*winsys
,
303 struct sw_displaytarget
*dt
,
304 struct winsys_handle
*whandle
)
306 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(winsys
);
307 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
309 switch(whandle
->type
) {
310 case DRM_API_HANDLE_TYPE_KMS
:
311 whandle
->handle
= kms_sw_dt
->handle
;
312 whandle
->stride
= kms_sw_dt
->stride
;
314 case DRM_API_HANDLE_TYPE_FD
:
315 if (!drmPrimeHandleToFD(kms_sw
->fd
, kms_sw_dt
->handle
,
316 DRM_CLOEXEC
, &whandle
->handle
)) {
317 whandle
->stride
= kms_sw_dt
->stride
;
329 kms_sw_displaytarget_display(struct sw_winsys
*ws
,
330 struct sw_displaytarget
*dt
,
331 void *context_private
,
332 struct pipe_box
*box
)
334 /* This function should not be called, instead the dri2 loader should
335 handle swap buffers internally.
342 kms_destroy_sw_winsys(struct sw_winsys
*winsys
)
348 kms_dri_create_winsys(int fd
)
350 struct kms_sw_winsys
*ws
;
352 ws
= CALLOC_STRUCT(kms_sw_winsys
);
357 list_inithead(&ws
->bo_list
);
359 ws
->base
.destroy
= kms_destroy_sw_winsys
;
361 ws
->base
.is_displaytarget_format_supported
= kms_sw_is_displaytarget_format_supported
;
363 /* screen texture functions */
364 ws
->base
.displaytarget_create
= kms_sw_displaytarget_create
;
365 ws
->base
.displaytarget_destroy
= kms_sw_displaytarget_destroy
;
366 ws
->base
.displaytarget_from_handle
= kms_sw_displaytarget_from_handle
;
367 ws
->base
.displaytarget_get_handle
= kms_sw_displaytarget_get_handle
;
369 /* texture functions */
370 ws
->base
.displaytarget_map
= kms_sw_displaytarget_map
;
371 ws
->base
.displaytarget_unmap
= kms_sw_displaytarget_unmap
;
373 ws
->base
.displaytarget_display
= kms_sw_displaytarget_display
;
378 /* vim: set sw=3 ts=8 sts=3 expandtab: */