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>
43 #include "pipe/p_compiler.h"
44 #include "pipe/p_format.h"
45 #include "util/u_inlines.h"
46 #include "util/u_format.h"
47 #include "util/u_math.h"
48 #include "util/u_memory.h"
49 #include "util/u_double_list.h"
51 #include "state_tracker/sw_winsys.h"
52 #include "state_tracker/drm_driver.h"
55 #define DEBUG(msg, ...) fprintf(stderr, msg, __VA_ARGS__)
57 #define DEBUG(msg, ...)
62 struct sw_winsys
*kms_dri_create_winsys(int fd
);
64 struct kms_sw_displaytarget
66 enum pipe_format format
;
76 struct list_head link
;
81 struct sw_winsys base
;
84 struct list_head bo_list
;
87 static INLINE
struct kms_sw_displaytarget
*
88 kms_sw_displaytarget( struct sw_displaytarget
*dt
)
90 return (struct kms_sw_displaytarget
*)dt
;
93 static INLINE
struct kms_sw_winsys
*
94 kms_sw_winsys( struct sw_winsys
*ws
)
96 return (struct kms_sw_winsys
*)ws
;
101 kms_sw_is_displaytarget_format_supported( struct sw_winsys
*ws
,
103 enum pipe_format format
)
105 /* TODO: check visuals or other sensible thing here */
109 static struct sw_displaytarget
*
110 kms_sw_displaytarget_create(struct sw_winsys
*ws
,
112 enum pipe_format format
,
113 unsigned width
, unsigned height
,
117 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
118 struct kms_sw_displaytarget
*kms_sw_dt
;
119 struct drm_mode_create_dumb create_req
;
120 struct drm_mode_destroy_dumb destroy_req
;
123 kms_sw_dt
= CALLOC_STRUCT(kms_sw_displaytarget
);
127 kms_sw_dt
->ref_count
= 1;
129 kms_sw_dt
->format
= format
;
130 kms_sw_dt
->width
= width
;
131 kms_sw_dt
->height
= height
;
134 create_req
.width
= width
;
135 create_req
.height
= height
;
136 create_req
.handle
= 0;
137 ret
= drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create_req
);
141 kms_sw_dt
->stride
= create_req
.pitch
;
142 kms_sw_dt
->size
= create_req
.size
;
143 kms_sw_dt
->handle
= create_req
.handle
;
145 list_add(&kms_sw_dt
->link
, &kms_sw
->bo_list
);
147 DEBUG("KMS-DEBUG: created buffer %u (size %u)\n", kms_sw_dt
->handle
, kms_sw_dt
->size
);
149 *stride
= kms_sw_dt
->stride
;
150 return (struct sw_displaytarget
*)kms_sw_dt
;
153 memset(&destroy_req
, 0, sizeof destroy_req
);
154 destroy_req
.handle
= create_req
.handle
;
155 drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_DESTROY_DUMB
, &destroy_req
);
162 kms_sw_displaytarget_destroy(struct sw_winsys
*ws
,
163 struct sw_displaytarget
*dt
)
165 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
166 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
167 struct drm_mode_destroy_dumb destroy_req
;
169 kms_sw_dt
->ref_count
--;
170 if (kms_sw_dt
->ref_count
> 0)
173 memset(&destroy_req
, 0, sizeof destroy_req
);
174 destroy_req
.handle
= kms_sw_dt
->handle
;
175 drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_DESTROY_DUMB
, &destroy_req
);
177 list_del(&kms_sw_dt
->link
);
179 DEBUG("KMS-DEBUG: destroyed buffer %u\n", kms_sw_dt
->handle
);
185 kms_sw_displaytarget_map(struct sw_winsys
*ws
,
186 struct sw_displaytarget
*dt
,
189 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
190 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
191 struct drm_mode_map_dumb map_req
;
194 memset(&map_req
, 0, sizeof map_req
);
195 map_req
.handle
= kms_sw_dt
->handle
;
196 ret
= drmIoctl(kms_sw
->fd
, DRM_IOCTL_MODE_MAP_DUMB
, &map_req
);
200 prot
= (flags
== PIPE_TRANSFER_READ
) ? PROT_READ
: (PROT_READ
| PROT_WRITE
);
201 kms_sw_dt
->mapped
= mmap(0, kms_sw_dt
->size
, prot
, MAP_SHARED
,
202 kms_sw
->fd
, map_req
.offset
);
204 if (kms_sw_dt
->mapped
== MAP_FAILED
)
207 DEBUG("KMS-DEBUG: mapped buffer %u (size %u) at %p\n",
208 kms_sw_dt
->handle
, kms_sw_dt
->size
, kms_sw_dt
->mapped
);
210 return kms_sw_dt
->mapped
;
214 kms_sw_displaytarget_unmap(struct sw_winsys
*ws
,
215 struct sw_displaytarget
*dt
)
217 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
219 DEBUG("KMS-DEBUG: unmapped buffer %u (was %p)\n", kms_sw_dt
->handle
, kms_sw_dt
->mapped
);
221 munmap(kms_sw_dt
->mapped
, kms_sw_dt
->size
);
222 kms_sw_dt
->mapped
= NULL
;
225 static struct sw_displaytarget
*
226 kms_sw_displaytarget_from_handle(struct sw_winsys
*ws
,
227 const struct pipe_resource
*templ
,
228 struct winsys_handle
*whandle
,
231 struct kms_sw_winsys
*kms_sw
= kms_sw_winsys(ws
);
232 struct kms_sw_displaytarget
*kms_sw_dt
;
234 LIST_FOR_EACH_ENTRY(kms_sw_dt
, &kms_sw
->bo_list
, link
) {
235 if (kms_sw_dt
->handle
== whandle
->handle
) {
236 kms_sw_dt
->ref_count
++;
238 DEBUG("KMS-DEBUG: imported buffer %u (size %u)\n", kms_sw_dt
->handle
, kms_sw_dt
->size
);
240 *stride
= kms_sw_dt
->stride
;
241 return (struct sw_displaytarget
*)kms_sw_dt
;
250 kms_sw_displaytarget_get_handle(struct sw_winsys
*winsys
,
251 struct sw_displaytarget
*dt
,
252 struct winsys_handle
*whandle
)
254 struct kms_sw_displaytarget
*kms_sw_dt
= kms_sw_displaytarget(dt
);
256 assert(whandle
->type
== DRM_API_HANDLE_TYPE_SHARED
);
257 whandle
->handle
= kms_sw_dt
->handle
;
258 whandle
->stride
= kms_sw_dt
->stride
;
263 kms_sw_displaytarget_display(struct sw_winsys
*ws
,
264 struct sw_displaytarget
*dt
,
265 void *context_private
,
266 struct pipe_box
*box
)
268 /* This function should not be called, instead the dri2 loader should
269 handle swap buffers internally.
276 kms_destroy_sw_winsys(struct sw_winsys
*winsys
)
282 kms_dri_create_winsys(int fd
)
284 struct kms_sw_winsys
*ws
;
286 ws
= CALLOC_STRUCT(kms_sw_winsys
);
291 list_inithead(&ws
->bo_list
);
293 ws
->base
.destroy
= kms_destroy_sw_winsys
;
295 ws
->base
.is_displaytarget_format_supported
= kms_sw_is_displaytarget_format_supported
;
297 /* screen texture functions */
298 ws
->base
.displaytarget_create
= kms_sw_displaytarget_create
;
299 ws
->base
.displaytarget_destroy
= kms_sw_displaytarget_destroy
;
300 ws
->base
.displaytarget_from_handle
= kms_sw_displaytarget_from_handle
;
301 ws
->base
.displaytarget_get_handle
= kms_sw_displaytarget_get_handle
;
303 /* texture functions */
304 ws
->base
.displaytarget_map
= kms_sw_displaytarget_map
;
305 ws
->base
.displaytarget_unmap
= kms_sw_displaytarget_unmap
;
307 ws
->base
.displaytarget_display
= kms_sw_displaytarget_display
;
312 /* vim: set sw=3 ts=8 sts=3 expandtab: */