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 **************************************************************************/
32 #include "pipe/p_compiler.h"
33 #include "pipe/p_format.h"
34 #include "util/u_inlines.h"
35 #include "util/u_format.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
39 #include "state_tracker/sw_winsys.h"
40 #include "dri_sw_winsys.h"
43 struct dri_sw_displaytarget
45 enum pipe_format format
;
54 const void *front_private
;
59 struct sw_winsys base
;
61 struct drisw_loader_funcs
*lf
;
64 static inline struct dri_sw_displaytarget
*
65 dri_sw_displaytarget( struct sw_displaytarget
*dt
)
67 return (struct dri_sw_displaytarget
*)dt
;
70 static inline struct dri_sw_winsys
*
71 dri_sw_winsys( struct sw_winsys
*ws
)
73 return (struct dri_sw_winsys
*)ws
;
78 dri_sw_is_displaytarget_format_supported( struct sw_winsys
*ws
,
80 enum pipe_format format
)
82 /* TODO: check visuals or other sensible thing here */
87 alloc_shm(struct dri_sw_displaytarget
*dri_sw_dt
, unsigned size
)
91 dri_sw_dt
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
92 if (dri_sw_dt
->shmid
< 0)
95 addr
= (char *) shmat(dri_sw_dt
->shmid
, 0, 0);
96 /* mark the segment immediately for deletion to avoid leaks */
97 shmctl(dri_sw_dt
->shmid
, IPC_RMID
, 0);
99 if (addr
== (char *) -1)
105 static struct sw_displaytarget
*
106 dri_sw_displaytarget_create(struct sw_winsys
*winsys
,
108 enum pipe_format format
,
109 unsigned width
, unsigned height
,
111 const void *front_private
,
114 struct dri_sw_winsys
*ws
= dri_sw_winsys(winsys
);
115 struct dri_sw_displaytarget
*dri_sw_dt
;
116 unsigned nblocksy
, size
, format_stride
;
118 dri_sw_dt
= CALLOC_STRUCT(dri_sw_displaytarget
);
122 dri_sw_dt
->format
= format
;
123 dri_sw_dt
->width
= width
;
124 dri_sw_dt
->height
= height
;
125 dri_sw_dt
->front_private
= front_private
;
127 format_stride
= util_format_get_stride(format
, width
);
128 dri_sw_dt
->stride
= align(format_stride
, alignment
);
130 nblocksy
= util_format_get_nblocksy(format
, height
);
131 size
= dri_sw_dt
->stride
* nblocksy
;
133 dri_sw_dt
->shmid
= -1;
134 if (ws
->lf
->put_image_shm
)
135 dri_sw_dt
->data
= alloc_shm(dri_sw_dt
, size
);
138 dri_sw_dt
->data
= align_malloc(size
, alignment
);
143 *stride
= dri_sw_dt
->stride
;
144 return (struct sw_displaytarget
*)dri_sw_dt
;
153 dri_sw_displaytarget_destroy(struct sw_winsys
*ws
,
154 struct sw_displaytarget
*dt
)
156 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
158 if (dri_sw_dt
->shmid
>= 0) {
159 shmdt(dri_sw_dt
->data
);
160 shmctl(dri_sw_dt
->shmid
, IPC_RMID
, 0);
162 align_free(dri_sw_dt
->data
);
169 dri_sw_displaytarget_map(struct sw_winsys
*ws
,
170 struct sw_displaytarget
*dt
,
173 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
174 dri_sw_dt
->mapped
= dri_sw_dt
->data
;
176 if (dri_sw_dt
->front_private
&& (flags
& PIPE_TRANSFER_READ
)) {
177 struct dri_sw_winsys
*dri_sw_ws
= dri_sw_winsys(ws
);
178 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
);
180 dri_sw_dt
->map_flags
= flags
;
181 return dri_sw_dt
->mapped
;
185 dri_sw_displaytarget_unmap(struct sw_winsys
*ws
,
186 struct sw_displaytarget
*dt
)
188 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
189 if (dri_sw_dt
->front_private
&& (dri_sw_dt
->map_flags
& PIPE_TRANSFER_WRITE
)) {
190 struct dri_sw_winsys
*dri_sw_ws
= dri_sw_winsys(ws
);
191 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
);
193 dri_sw_dt
->map_flags
= 0;
194 dri_sw_dt
->mapped
= NULL
;
197 static struct sw_displaytarget
*
198 dri_sw_displaytarget_from_handle(struct sw_winsys
*winsys
,
199 const struct pipe_resource
*templ
,
200 struct winsys_handle
*whandle
,
208 dri_sw_displaytarget_get_handle(struct sw_winsys
*winsys
,
209 struct sw_displaytarget
*dt
,
210 struct winsys_handle
*whandle
)
212 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
214 if (whandle
->type
== WINSYS_HANDLE_TYPE_SHMID
) {
215 if (dri_sw_dt
->shmid
< 0)
217 whandle
->handle
= dri_sw_dt
->shmid
;
225 dri_sw_displaytarget_display(struct sw_winsys
*ws
,
226 struct sw_displaytarget
*dt
,
227 void *context_private
,
228 struct pipe_box
*box
)
230 struct dri_sw_winsys
*dri_sw_ws
= dri_sw_winsys(ws
);
231 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
232 struct dri_drawable
*dri_drawable
= (struct dri_drawable
*)context_private
;
233 unsigned width
, height
, x
= 0, y
= 0;
234 unsigned blsize
= util_format_get_blocksize(dri_sw_dt
->format
);
236 void *data
= dri_sw_dt
->data
;
238 /* Set the width to 'stride / cpp'.
240 * PutImage correctly clips to the width of the dst drawable.
243 offset
= (dri_sw_dt
->stride
* box
->y
) + box
->x
* blsize
;
248 height
= box
->height
;
250 width
= dri_sw_dt
->stride
/ blsize
;
251 height
= dri_sw_dt
->height
;
254 if (dri_sw_dt
->shmid
!= -1) {
255 dri_sw_ws
->lf
->put_image_shm(dri_drawable
, dri_sw_dt
->shmid
, dri_sw_dt
->data
, offset
,
256 x
, y
, width
, height
, dri_sw_dt
->stride
);
261 dri_sw_ws
->lf
->put_image2(dri_drawable
, data
,
262 x
, y
, width
, height
, dri_sw_dt
->stride
);
264 dri_sw_ws
->lf
->put_image(dri_drawable
, data
, width
, height
);
268 dri_destroy_sw_winsys(struct sw_winsys
*winsys
)
274 dri_create_sw_winsys(struct drisw_loader_funcs
*lf
)
276 struct dri_sw_winsys
*ws
;
278 ws
= CALLOC_STRUCT(dri_sw_winsys
);
283 ws
->base
.destroy
= dri_destroy_sw_winsys
;
285 ws
->base
.is_displaytarget_format_supported
= dri_sw_is_displaytarget_format_supported
;
287 /* screen texture functions */
288 ws
->base
.displaytarget_create
= dri_sw_displaytarget_create
;
289 ws
->base
.displaytarget_destroy
= dri_sw_displaytarget_destroy
;
290 ws
->base
.displaytarget_from_handle
= dri_sw_displaytarget_from_handle
;
291 ws
->base
.displaytarget_get_handle
= dri_sw_displaytarget_get_handle
;
293 /* texture functions */
294 ws
->base
.displaytarget_map
= dri_sw_displaytarget_map
;
295 ws
->base
.displaytarget_unmap
= dri_sw_displaytarget_unmap
;
297 ws
->base
.displaytarget_display
= dri_sw_displaytarget_display
;
302 /* vim: set sw=3 ts=8 sts=3 expandtab: */