1 /**************************************************************************
3 * Copyright 2009, VMware, Inc.
5 * Copyright 2010 George Sapountzis <gsapountzis@gmail.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
33 /* sys/ipc.h -> sys/_types.h -> machine/param.h
34 * - defines ALIGN which clashes with our ALIGN
40 #include "pipe/p_compiler.h"
41 #include "pipe/p_format.h"
42 #include "pipe/p_state.h"
43 #include "util/u_inlines.h"
44 #include "util/format/u_format.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
48 #include "frontend/sw_winsys.h"
49 #include "dri_sw_winsys.h"
52 struct dri_sw_displaytarget
54 enum pipe_format format
;
63 const void *front_private
;
68 struct sw_winsys base
;
70 const struct drisw_loader_funcs
*lf
;
73 static inline struct dri_sw_displaytarget
*
74 dri_sw_displaytarget( struct sw_displaytarget
*dt
)
76 return (struct dri_sw_displaytarget
*)dt
;
79 static inline struct dri_sw_winsys
*
80 dri_sw_winsys( struct sw_winsys
*ws
)
82 return (struct dri_sw_winsys
*)ws
;
87 dri_sw_is_displaytarget_format_supported( struct sw_winsys
*ws
,
89 enum pipe_format format
)
91 /* TODO: check visuals or other sensible thing here */
97 alloc_shm(struct dri_sw_displaytarget
*dri_sw_dt
, unsigned size
)
101 /* 0600 = user read+write */
102 dri_sw_dt
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
| 0600);
103 if (dri_sw_dt
->shmid
< 0)
106 addr
= (char *) shmat(dri_sw_dt
->shmid
, 0, 0);
107 /* mark the segment immediately for deletion to avoid leaks */
108 shmctl(dri_sw_dt
->shmid
, IPC_RMID
, 0);
110 if (addr
== (char *) -1)
117 static struct sw_displaytarget
*
118 dri_sw_displaytarget_create(struct sw_winsys
*winsys
,
120 enum pipe_format format
,
121 unsigned width
, unsigned height
,
123 const void *front_private
,
126 struct dri_sw_winsys
*ws
= dri_sw_winsys(winsys
);
127 struct dri_sw_displaytarget
*dri_sw_dt
;
128 unsigned nblocksy
, size
, format_stride
;
130 dri_sw_dt
= CALLOC_STRUCT(dri_sw_displaytarget
);
134 dri_sw_dt
->format
= format
;
135 dri_sw_dt
->width
= width
;
136 dri_sw_dt
->height
= height
;
137 dri_sw_dt
->front_private
= front_private
;
139 format_stride
= util_format_get_stride(format
, width
);
140 dri_sw_dt
->stride
= align(format_stride
, alignment
);
142 nblocksy
= util_format_get_nblocksy(format
, height
);
143 size
= dri_sw_dt
->stride
* nblocksy
;
145 dri_sw_dt
->shmid
= -1;
147 #ifdef HAVE_SYS_SHM_H
148 if (ws
->lf
->put_image_shm
)
149 dri_sw_dt
->data
= alloc_shm(dri_sw_dt
, size
);
153 dri_sw_dt
->data
= align_malloc(size
, alignment
);
158 *stride
= dri_sw_dt
->stride
;
159 return (struct sw_displaytarget
*)dri_sw_dt
;
168 dri_sw_displaytarget_destroy(struct sw_winsys
*ws
,
169 struct sw_displaytarget
*dt
)
171 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
173 if (dri_sw_dt
->shmid
>= 0) {
174 #ifdef HAVE_SYS_SHM_H
175 shmdt(dri_sw_dt
->data
);
176 shmctl(dri_sw_dt
->shmid
, IPC_RMID
, 0);
179 align_free(dri_sw_dt
->data
);
186 dri_sw_displaytarget_map(struct sw_winsys
*ws
,
187 struct sw_displaytarget
*dt
,
190 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
191 dri_sw_dt
->mapped
= dri_sw_dt
->data
;
193 if (dri_sw_dt
->front_private
&& (flags
& PIPE_TRANSFER_READ
)) {
194 struct dri_sw_winsys
*dri_sw_ws
= dri_sw_winsys(ws
);
195 dri_sw_ws
->lf
->get_image((void *)dri_sw_dt
->front_private
, 0, 0, dri_sw_dt
->width
, dri_sw_dt
->height
, dri_sw_dt
->stride
, dri_sw_dt
->data
);
197 dri_sw_dt
->map_flags
= flags
;
198 return dri_sw_dt
->mapped
;
202 dri_sw_displaytarget_unmap(struct sw_winsys
*ws
,
203 struct sw_displaytarget
*dt
)
205 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
206 if (dri_sw_dt
->front_private
&& (dri_sw_dt
->map_flags
& PIPE_TRANSFER_WRITE
)) {
207 struct dri_sw_winsys
*dri_sw_ws
= dri_sw_winsys(ws
);
208 dri_sw_ws
->lf
->put_image2((void *)dri_sw_dt
->front_private
, dri_sw_dt
->data
, 0, 0, dri_sw_dt
->width
, dri_sw_dt
->height
, dri_sw_dt
->stride
);
210 dri_sw_dt
->map_flags
= 0;
211 dri_sw_dt
->mapped
= NULL
;
214 static struct sw_displaytarget
*
215 dri_sw_displaytarget_from_handle(struct sw_winsys
*winsys
,
216 const struct pipe_resource
*templ
,
217 struct winsys_handle
*whandle
,
225 dri_sw_displaytarget_get_handle(struct sw_winsys
*winsys
,
226 struct sw_displaytarget
*dt
,
227 struct winsys_handle
*whandle
)
229 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
231 if (whandle
->type
== WINSYS_HANDLE_TYPE_SHMID
) {
232 if (dri_sw_dt
->shmid
< 0)
234 whandle
->handle
= dri_sw_dt
->shmid
;
242 dri_sw_displaytarget_display(struct sw_winsys
*ws
,
243 struct sw_displaytarget
*dt
,
244 void *context_private
,
245 struct pipe_box
*box
)
247 struct dri_sw_winsys
*dri_sw_ws
= dri_sw_winsys(ws
);
248 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
249 struct dri_drawable
*dri_drawable
= (struct dri_drawable
*)context_private
;
250 unsigned width
, height
, x
= 0, y
= 0;
251 unsigned blsize
= util_format_get_blocksize(dri_sw_dt
->format
);
253 unsigned offset_x
= 0;
254 char *data
= dri_sw_dt
->data
;
255 bool is_shm
= dri_sw_dt
->shmid
!= -1;
256 /* Set the width to 'stride / cpp'.
258 * PutImage correctly clips to the width of the dst drawable.
261 offset
= dri_sw_dt
->stride
* box
->y
;
262 offset_x
= box
->x
* blsize
;
264 /* don't add x offset for shm, the put_image_shm will deal with it */
270 height
= box
->height
;
272 width
= dri_sw_dt
->stride
/ blsize
;
273 height
= dri_sw_dt
->height
;
277 dri_sw_ws
->lf
->put_image_shm(dri_drawable
, dri_sw_dt
->shmid
, dri_sw_dt
->data
, offset
, offset_x
,
278 x
, y
, width
, height
, dri_sw_dt
->stride
);
283 dri_sw_ws
->lf
->put_image2(dri_drawable
, data
,
284 x
, y
, width
, height
, dri_sw_dt
->stride
);
286 dri_sw_ws
->lf
->put_image(dri_drawable
, data
, width
, height
);
290 dri_destroy_sw_winsys(struct sw_winsys
*winsys
)
296 dri_create_sw_winsys(const struct drisw_loader_funcs
*lf
)
298 struct dri_sw_winsys
*ws
;
300 ws
= CALLOC_STRUCT(dri_sw_winsys
);
305 ws
->base
.destroy
= dri_destroy_sw_winsys
;
307 ws
->base
.is_displaytarget_format_supported
= dri_sw_is_displaytarget_format_supported
;
309 /* screen texture functions */
310 ws
->base
.displaytarget_create
= dri_sw_displaytarget_create
;
311 ws
->base
.displaytarget_destroy
= dri_sw_displaytarget_destroy
;
312 ws
->base
.displaytarget_from_handle
= dri_sw_displaytarget_from_handle
;
313 ws
->base
.displaytarget_get_handle
= dri_sw_displaytarget_get_handle
;
315 /* texture functions */
316 ws
->base
.displaytarget_map
= dri_sw_displaytarget_map
;
317 ws
->base
.displaytarget_unmap
= dri_sw_displaytarget_unmap
;
319 ws
->base
.displaytarget_display
= dri_sw_displaytarget_display
;
324 /* vim: set sw=3 ts=8 sts=3 expandtab: */