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 **************************************************************************/
29 #include "pipe/p_compiler.h"
30 #include "pipe/p_format.h"
31 #include "util/u_inlines.h"
32 #include "util/u_format.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
36 #include "state_tracker/sw_winsys.h"
37 #include "dri_sw_winsys.h"
40 struct dri_sw_displaytarget
42 enum pipe_format format
;
50 const void *front_private
;
55 struct sw_winsys base
;
57 struct drisw_loader_funcs
*lf
;
60 static inline struct dri_sw_displaytarget
*
61 dri_sw_displaytarget( struct sw_displaytarget
*dt
)
63 return (struct dri_sw_displaytarget
*)dt
;
66 static inline struct dri_sw_winsys
*
67 dri_sw_winsys( struct sw_winsys
*ws
)
69 return (struct dri_sw_winsys
*)ws
;
74 dri_sw_is_displaytarget_format_supported( struct sw_winsys
*ws
,
76 enum pipe_format format
)
78 /* TODO: check visuals or other sensible thing here */
82 static struct sw_displaytarget
*
83 dri_sw_displaytarget_create(struct sw_winsys
*winsys
,
85 enum pipe_format format
,
86 unsigned width
, unsigned height
,
88 const void *front_private
,
91 struct dri_sw_displaytarget
*dri_sw_dt
;
92 unsigned nblocksy
, size
, format_stride
;
94 dri_sw_dt
= CALLOC_STRUCT(dri_sw_displaytarget
);
98 dri_sw_dt
->format
= format
;
99 dri_sw_dt
->width
= width
;
100 dri_sw_dt
->height
= height
;
101 dri_sw_dt
->front_private
= front_private
;
103 format_stride
= util_format_get_stride(format
, width
);
104 dri_sw_dt
->stride
= align(format_stride
, alignment
);
106 nblocksy
= util_format_get_nblocksy(format
, height
);
107 size
= dri_sw_dt
->stride
* nblocksy
;
109 dri_sw_dt
->data
= align_malloc(size
, alignment
);
113 *stride
= dri_sw_dt
->stride
;
114 return (struct sw_displaytarget
*)dri_sw_dt
;
123 dri_sw_displaytarget_destroy(struct sw_winsys
*ws
,
124 struct sw_displaytarget
*dt
)
126 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
128 align_free(dri_sw_dt
->data
);
134 dri_sw_displaytarget_map(struct sw_winsys
*ws
,
135 struct sw_displaytarget
*dt
,
138 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
139 dri_sw_dt
->mapped
= dri_sw_dt
->data
;
141 if (dri_sw_dt
->front_private
&& (flags
& PIPE_TRANSFER_READ
)) {
142 struct dri_sw_winsys
*dri_sw_ws
= dri_sw_winsys(ws
);
143 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
);
145 dri_sw_dt
->map_flags
= flags
;
146 return dri_sw_dt
->mapped
;
150 dri_sw_displaytarget_unmap(struct sw_winsys
*ws
,
151 struct sw_displaytarget
*dt
)
153 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
154 if (dri_sw_dt
->front_private
&& (dri_sw_dt
->map_flags
& PIPE_TRANSFER_WRITE
)) {
155 struct dri_sw_winsys
*dri_sw_ws
= dri_sw_winsys(ws
);
156 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
);
158 dri_sw_dt
->map_flags
= 0;
159 dri_sw_dt
->mapped
= NULL
;
162 static struct sw_displaytarget
*
163 dri_sw_displaytarget_from_handle(struct sw_winsys
*winsys
,
164 const struct pipe_resource
*templ
,
165 struct winsys_handle
*whandle
,
173 dri_sw_displaytarget_get_handle(struct sw_winsys
*winsys
,
174 struct sw_displaytarget
*dt
,
175 struct winsys_handle
*whandle
)
182 dri_sw_displaytarget_display(struct sw_winsys
*ws
,
183 struct sw_displaytarget
*dt
,
184 void *context_private
,
185 struct pipe_box
*box
)
187 struct dri_sw_winsys
*dri_sw_ws
= dri_sw_winsys(ws
);
188 struct dri_sw_displaytarget
*dri_sw_dt
= dri_sw_displaytarget(dt
);
189 struct dri_drawable
*dri_drawable
= (struct dri_drawable
*)context_private
;
190 unsigned width
, height
;
191 unsigned blsize
= util_format_get_blocksize(dri_sw_dt
->format
);
193 /* Set the width to 'stride / cpp'.
195 * PutImage correctly clips to the width of the dst drawable.
197 width
= dri_sw_dt
->stride
/ blsize
;
199 height
= dri_sw_dt
->height
;
203 data
= (char *)dri_sw_dt
->data
+ (dri_sw_dt
->stride
* box
->y
) + box
->x
* blsize
;
204 dri_sw_ws
->lf
->put_image2(dri_drawable
, data
,
205 box
->x
, box
->y
, box
->width
, box
->height
, dri_sw_dt
->stride
);
207 dri_sw_ws
->lf
->put_image(dri_drawable
, dri_sw_dt
->data
, width
, height
);
212 dri_destroy_sw_winsys(struct sw_winsys
*winsys
)
218 dri_create_sw_winsys(struct drisw_loader_funcs
*lf
)
220 struct dri_sw_winsys
*ws
;
222 ws
= CALLOC_STRUCT(dri_sw_winsys
);
227 ws
->base
.destroy
= dri_destroy_sw_winsys
;
229 ws
->base
.is_displaytarget_format_supported
= dri_sw_is_displaytarget_format_supported
;
231 /* screen texture functions */
232 ws
->base
.displaytarget_create
= dri_sw_displaytarget_create
;
233 ws
->base
.displaytarget_destroy
= dri_sw_displaytarget_destroy
;
234 ws
->base
.displaytarget_from_handle
= dri_sw_displaytarget_from_handle
;
235 ws
->base
.displaytarget_get_handle
= dri_sw_displaytarget_get_handle
;
237 /* texture functions */
238 ws
->base
.displaytarget_map
= dri_sw_displaytarget_map
;
239 ws
->base
.displaytarget_unmap
= dri_sw_displaytarget_unmap
;
241 ws
->base
.displaytarget_display
= dri_sw_displaytarget_display
;
246 /* vim: set sw=3 ts=8 sts=3 expandtab: */