2 * Mesa 3-D graphics library
5 * Copyright (C) 2011 Benjamin Franzke <benjaminfranzke@googlemail.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
27 #include <sys/types.h>
31 #include "pipe/p_compiler.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_state.h"
34 #include "util/u_format.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
37 #include "state_tracker/sw_winsys.h"
39 #include <wayland-client.h>
40 #include "wayland_sw_winsys.h"
42 struct wayland_sw_displaytarget
51 enum pipe_format format
;
57 struct wayland_sw_winsys
59 struct sw_winsys base
;
61 struct wl_display
*display
;
64 static INLINE
struct wayland_sw_displaytarget
*
65 wayland_sw_displaytarget(struct sw_displaytarget
*dt
)
67 return (struct wayland_sw_displaytarget
*) dt
;
70 static INLINE
struct wayland_sw_winsys
*
71 wayland_sw_winsys(struct sw_winsys
*ws
)
73 return (struct wayland_sw_winsys
*) ws
;
77 wayland_displaytarget_display(struct sw_winsys
*ws
,
78 struct sw_displaytarget
*dt
,
79 void *context_private
)
84 wayland_displaytarget_unmap(struct sw_winsys
*ws
,
85 struct sw_displaytarget
*dt
)
87 struct wayland_sw_displaytarget
*wldt
= wayland_sw_displaytarget(dt
);
90 if (wldt
->map_count
> 0)
93 munmap(wldt
->map
, wldt
->size
);
98 wayland_displaytarget_map(struct sw_winsys
*ws
,
99 struct sw_displaytarget
*dt
,
102 struct wayland_sw_displaytarget
*wldt
= wayland_sw_displaytarget(dt
);
110 if (flags
& PIPE_TRANSFER_READ
)
111 mmap_flags
|= PROT_READ
;
112 if (flags
& PIPE_TRANSFER_WRITE
)
113 mmap_flags
|= PROT_WRITE
;
115 wldt
->map
= mmap(NULL
, wldt
->size
, mmap_flags
,
116 MAP_SHARED
, wldt
->fd
, 0);
118 if (wldt
->map
== MAP_FAILED
)
127 wayland_displaytarget_destroy(struct sw_winsys
*ws
,
128 struct sw_displaytarget
*dt
)
130 struct wayland_sw_displaytarget
*wldt
= wayland_sw_displaytarget(dt
);
133 wayland_displaytarget_unmap(ws
, dt
);
139 wayland_is_displaytarget_format_supported(struct sw_winsys
*ws
,
141 enum pipe_format format
)
144 case PIPE_FORMAT_B8G8R8X8_UNORM
:
145 case PIPE_FORMAT_B8G8R8A8_UNORM
:
152 static struct sw_displaytarget
*
153 wayland_displaytarget_create(struct sw_winsys
*ws
,
155 enum pipe_format format
,
156 unsigned width
, unsigned height
,
160 struct wayland_sw_displaytarget
*wldt
;
161 unsigned nblocksy
, format_stride
;
162 char filename
[] = "/tmp/wayland-shm-XXXXXX";
164 if (!wayland_is_displaytarget_format_supported(ws
, tex_usage
, format
))
167 wldt
= CALLOC_STRUCT(wayland_sw_displaytarget
);
173 wldt
->format
= format
;
175 wldt
->height
= height
;
177 format_stride
= util_format_get_stride(format
, width
);
178 wldt
->stride
= align(format_stride
, alignment
);
180 nblocksy
= util_format_get_nblocksy(format
, height
);
181 wldt
->size
= wldt
->stride
* nblocksy
;
183 wldt
->fd
= mkstemp(filename
);
189 if (ftruncate(wldt
->fd
, wldt
->size
) < 0) {
198 *stride
= wldt
->stride
;
200 return (struct sw_displaytarget
*) wldt
;
203 static struct sw_displaytarget
*
204 wayland_displaytarget_from_handle(struct sw_winsys
*ws
,
205 const struct pipe_resource
*templet
,
206 struct winsys_handle
*whandle
,
209 struct wayland_sw_displaytarget
*wldt
;
212 if (!wayland_is_displaytarget_format_supported(ws
, 0, templet
->format
))
215 wldt
= CALLOC_STRUCT(wayland_sw_displaytarget
);
219 wldt
->fd
= whandle
->fd
;
220 wldt
->stride
= whandle
->stride
;
221 wldt
->width
= templet
->width0
;
222 wldt
->height
= templet
->height0
;
223 wldt
->format
= templet
->format
;
225 nblocksy
= util_format_get_nblocksy(wldt
->format
, wldt
->height
);
227 wldt
->size
= wldt
->stride
* nblocksy
;
231 *stride
= wldt
->stride
;
233 return (struct sw_displaytarget
*) wldt
;
238 wayland_displaytarget_get_handle(struct sw_winsys
*ws
,
239 struct sw_displaytarget
*dt
,
240 struct winsys_handle
*whandle
)
242 struct wayland_sw_displaytarget
*wldt
= wayland_sw_displaytarget(dt
);
244 whandle
->fd
= wldt
->fd
;
245 whandle
->stride
= wldt
->stride
;
246 whandle
->size
= wldt
->size
;
252 wayland_destroy(struct sw_winsys
*ws
)
254 struct wayland_sw_winsys
*wayland
= wayland_sw_winsys(ws
);
260 wayland_create_sw_winsys(struct wl_display
*display
)
262 struct wayland_sw_winsys
*wlws
;
264 wlws
= CALLOC_STRUCT(wayland_sw_winsys
);
268 wlws
->display
= display
;
270 wlws
->base
.destroy
= wayland_destroy
;
271 wlws
->base
.is_displaytarget_format_supported
=
272 wayland_is_displaytarget_format_supported
;
274 wlws
->base
.displaytarget_create
= wayland_displaytarget_create
;
275 wlws
->base
.displaytarget_from_handle
= wayland_displaytarget_from_handle
;
276 wlws
->base
.displaytarget_get_handle
= wayland_displaytarget_get_handle
;
277 wlws
->base
.displaytarget_destroy
= wayland_displaytarget_destroy
;
278 wlws
->base
.displaytarget_map
= wayland_displaytarget_map
;
279 wlws
->base
.displaytarget_unmap
= wayland_displaytarget_unmap
;
281 wlws
->base
.displaytarget_display
= wayland_displaytarget_display
;
286 /* vim: set sw=3 ts=8 sts=3 expandtab: */