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/list.h"
52 #include "state_tracker/sw_winsys.h"
53 #include "state_tracker/drm_driver.h"
54 #include "kms_dri_sw_winsys.h"
57 #define DEBUG_PRINT(msg, ...) fprintf(stderr, msg, __VA_ARGS__)
59 #define DEBUG_PRINT(msg, ...)
63 struct kms_sw_displaytarget
65 enum pipe_format format
;
75 struct list_head link
;
80 struct sw_winsys base
;
83 struct list_head bo_list
;
86 static inline struct kms_sw_displaytarget
*
87 kms_sw_displaytarget( struct sw_displaytarget
*dt
)
89 return (struct kms_sw_displaytarget
*)dt
;
92 static inline struct kms_sw_winsys
*
93 kms_sw_winsys( struct sw_winsys
*ws
)
95 return (struct kms_sw_winsys
*)ws
;
100 kms_sw_is_displaytarget_format_supported( struct sw_winsys
*ws
,
102 enum pipe_format format
)
104 /* TODO: check visuals or other sensible thing here */
108 static struct sw_displaytarget
*
109 kms_sw_displaytarget_create(struct sw_winsys
*ws
,
111 enum pipe_format format
,
112 unsigned width
, unsigned height
,
116 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
117 struct kms_sw_displaytarget
*kms_sw_dt
;
118 struct drm_mode_create_dumb create_req
;
119 struct drm_mode_destroy_dumb destroy_req
;
122 kms_sw_dt
= CALLOC_STRUCT(kms_sw_displaytarget
);
126 kms_sw_dt
->ref_count
= 1;
128 kms_sw_dt
->format
= format
;
129 kms_sw_dt
->width
= width
;
130 kms_sw_dt
->height
= height
;
132 memset(&create_req
, 0, sizeof(create_req
));
134 create_req
.width
= width
;
135 create_req
.height
= height
;
136 ret
= drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create_req
);
140 kms_sw_dt
->stride
= create_req
.pitch
;
141 kms_sw_dt
->size
= create_req
.size
;
142 kms_sw_dt
->handle
= create_req
.handle
;
144 list_add(&kms_sw_dt
->link
, &kms_sw
->bo_list
);
146 DEBUG_PRINT("KMS-DEBUG: created buffer %u (size %u)\n", kms_sw_dt
->handle
, kms_sw_dt
->size
);
148 *stride
= kms_sw_dt
->stride
;
149 return (struct sw_displaytarget
*)kms_sw_dt
;
152 memset(&destroy_req
, 0, sizeof destroy_req
);
153 destroy_req
.handle
= create_req
.handle
;
154 drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_DESTROY_DUMB
, &destroy_req
);
161 kms_sw_displaytarget_destroy(struct sw_winsys
*ws
,
162 struct sw_displaytarget
*dt
)
164 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
165 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
166 struct drm_mode_destroy_dumb destroy_req
;
168 kms_sw_dt
->ref_count
--;
169 if (kms_sw_dt
->ref_count
> 0)
172 memset(&destroy_req
, 0, sizeof destroy_req
);
173 destroy_req
.handle
= kms_sw_dt
->handle
;
174 drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_DESTROY_DUMB
, &destroy_req
);
176 list_del(&kms_sw_dt
->link
);
178 DEBUG_PRINT("KMS-DEBUG: destroyed buffer %u\n", kms_sw_dt
->handle
);
184 kms_sw_displaytarget_map(struct sw_winsys
*ws
,
185 struct sw_displaytarget
*dt
,
188 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
189 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
190 struct drm_mode_map_dumb map_req
;
193 memset(&map_req
, 0, sizeof map_req
);
194 map_req
.handle
= kms_sw_dt
->handle
;
195 ret
= drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_MAP_DUMB
, &map_req
);
199 prot
= (flags
== PIPE_TRANSFER_READ
) ? PROT_READ
: (PROT_READ
| PROT_WRITE
);
200 kms_sw_dt
->mapped
= mmap(0, kms_sw_dt
->size
, prot
, MAP_SHARED
,
201 kms_sw
->fd
, map_req
.offset
);
203 if (kms_sw_dt
->mapped
== MAP_FAILED
)
206 DEBUG_PRINT("KMS-DEBUG: mapped buffer %u (size %u) at %p\n",
207 kms_sw_dt
->handle
, kms_sw_dt
->size
, kms_sw_dt
->mapped
);
209 return kms_sw_dt
->mapped
;
212 static struct kms_sw_displaytarget
*
213 kms_sw_displaytarget_add_from_prime(struct kms_sw_winsys
*kms_sw
, int fd
)
215 uint32_t handle
= -1;
216 struct kms_sw_displaytarget
* kms_sw_dt
;
219 ret
= drmPrimeFDToHandle(kms_sw
->fd
, fd
, &handle
);
224 kms_sw_dt
= CALLOC_STRUCT(kms_sw_displaytarget
);
228 kms_sw_dt
->ref_count
= 1;
229 kms_sw_dt
->handle
= handle
;
230 kms_sw_dt
->size
= lseek(fd
, 0, SEEK_END
);
232 if (kms_sw_dt
->size
== (off_t
)-1) {
237 lseek(fd
, 0, SEEK_SET
);
239 list_add(&kms_sw_dt
->link
, &kms_sw
->bo_list
);
245 kms_sw_displaytarget_unmap(struct sw_winsys
*ws
,
246 struct sw_displaytarget
*dt
)
248 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
250 DEBUG_PRINT("KMS-DEBUG: unmapped buffer %u (was %p)\n", kms_sw_dt
->handle
, kms_sw_dt
->mapped
);
252 munmap(kms_sw_dt
->mapped
, kms_sw_dt
->size
);
253 kms_sw_dt
->mapped
= NULL
;
256 static struct sw_displaytarget
*
257 kms_sw_displaytarget_from_handle(struct sw_winsys
*ws
,
258 const struct pipe_resource
*templ
,
259 struct winsys_handle
*whandle
,
262 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
263 struct kms_sw_displaytarget
*kms_sw_dt
;
265 assert(whandle
->type
== DRM_API_HANDLE_TYPE_KMS
||
266 whandle
->type
== DRM_API_HANDLE_TYPE_FD
);
268 switch(whandle
->type
) {
269 case DRM_API_HANDLE_TYPE_FD
:
270 kms_sw_dt
= kms_sw_displaytarget_add_from_prime(kms_sw
, whandle
->handle
);
272 kms_sw_dt
->ref_count
++;
273 kms_sw_dt
->width
= templ
->width0
;
274 kms_sw_dt
->height
= templ
->height0
;
275 kms_sw_dt
->stride
= whandle
->stride
;
276 *stride
= kms_sw_dt
->stride
;
278 return (struct sw_displaytarget
*)kms_sw_dt
;
279 case DRM_API_HANDLE_TYPE_KMS
:
280 LIST_FOR_EACH_ENTRY(kms_sw_dt
, &kms_sw
->bo_list
, link
) {
281 if (kms_sw_dt
->handle
== whandle
->handle
) {
282 kms_sw_dt
->ref_count
++;
284 DEBUG_PRINT("KMS-DEBUG: imported buffer %u (size %u)\n", kms_sw_dt
->handle
, kms_sw_dt
->size
);
286 *stride
= kms_sw_dt
->stride
;
287 return (struct sw_displaytarget
*)kms_sw_dt
;
300 kms_sw_displaytarget_get_handle(struct sw_winsys
*winsys
,
301 struct sw_displaytarget
*dt
,
302 struct winsys_handle
*whandle
)
304 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(winsys
);
305 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
307 switch(whandle
->type
) {
308 case DRM_API_HANDLE_TYPE_KMS
:
309 whandle
->handle
= kms_sw_dt
->handle
;
310 whandle
->stride
= kms_sw_dt
->stride
;
312 case DRM_API_HANDLE_TYPE_FD
:
313 if (!drmPrimeHandleToFD(kms_sw
->fd
, kms_sw_dt
->handle
,
314 DRM_CLOEXEC
, (int*)&whandle
->handle
)) {
315 whandle
->stride
= kms_sw_dt
->stride
;
327 kms_sw_displaytarget_display(struct sw_winsys
*ws
,
328 struct sw_displaytarget
*dt
,
329 void *context_private
,
330 struct pipe_box
*box
)
332 /* This function should not be called, instead the dri2 loader should
333 handle swap buffers internally.
340 kms_destroy_sw_winsys(struct sw_winsys
*winsys
)
346 kms_dri_create_winsys(int fd
)
348 struct kms_sw_winsys
*ws
;
350 ws
= CALLOC_STRUCT(kms_sw_winsys
);
355 list_inithead(&ws
->bo_list
);
357 ws
->base
.destroy
= kms_destroy_sw_winsys
;
359 ws
->base
.is_displaytarget_format_supported
= kms_sw_is_displaytarget_format_supported
;
361 /* screen texture functions */
362 ws
->base
.displaytarget_create
= kms_sw_displaytarget_create
;
363 ws
->base
.displaytarget_destroy
= kms_sw_displaytarget_destroy
;
364 ws
->base
.displaytarget_from_handle
= kms_sw_displaytarget_from_handle
;
365 ws
->base
.displaytarget_get_handle
= kms_sw_displaytarget_get_handle
;
367 /* texture functions */
368 ws
->base
.displaytarget_map
= kms_sw_displaytarget_map
;
369 ws
->base
.displaytarget_unmap
= kms_sw_displaytarget_unmap
;
371 ws
->base
.displaytarget_display
= kms_sw_displaytarget_display
;
376 /* vim: set sw=3 ts=8 sts=3 expandtab: */