1 /**********************************************************
2 * Copyright 2010 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
27 #include "wrapper_sw_winsys.h"
29 #include "pipe/p_format.h"
30 #include "pipe/p_state.h"
32 #include "state_tracker/sw_winsys.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
38 * This code wraps a pipe_screen and exposes a sw_winsys interface for use
39 * with software resterizers. This code is used by the DRM based winsys to
40 * allow access to the drm driver.
42 * We must borrow the whole stack because only the pipe screen knows how
43 * to decode the content of a buffer. Or how to create a buffer that
44 * can still be used by drivers using real hardware (as the case is
45 * with software st/xorg but hw st/dri).
47 * We also need a pipe context for the transfers.
50 struct wrapper_sw_winsys
52 struct sw_winsys base
;
53 struct pipe_screen
*screen
;
54 struct pipe_context
*pipe
;
57 struct wrapper_sw_displaytarget
59 struct wrapper_sw_winsys
*winsys
;
60 struct pipe_texture
*tex
;
61 struct pipe_transfer
*transfer
;
66 unsigned stride
; /**< because we give stride at create */
70 static INLINE
struct wrapper_sw_winsys
*
71 wrapper_sw_winsys(struct sw_winsys
*ws
)
73 return (struct wrapper_sw_winsys
*)ws
;
76 static INLINE
struct wrapper_sw_displaytarget
*
77 wrapper_sw_displaytarget(struct sw_displaytarget
*dt
)
79 return (struct wrapper_sw_displaytarget
*)dt
;
89 wsw_dt_get_stride(struct wrapper_sw_displaytarget
*wdt
, unsigned *stride
)
91 struct pipe_context
*pipe
= wdt
->winsys
->pipe
;
92 struct pipe_texture
*tex
= wdt
->tex
;
93 struct pipe_transfer
*tr
;
95 tr
= pipe
->get_tex_transfer(pipe
, tex
, 0, 0, 0,
96 PIPE_TRANSFER_READ_WRITE
,
97 0, 0, wdt
->width
, wdt
->height
);
101 *stride
= tr
->stride
;
102 wdt
->stride
= tr
->stride
;
104 pipe
->tex_transfer_destroy(pipe
, tr
);
109 static struct sw_displaytarget
*
110 wsw_dt_wrap_texture(struct wrapper_sw_winsys
*wsw
,
111 struct pipe_texture
*tex
, unsigned *stride
)
113 struct wrapper_sw_displaytarget
*wdt
= CALLOC_STRUCT(wrapper_sw_displaytarget
);
120 if (!wsw_dt_get_stride(wdt
, stride
))
123 return (struct sw_displaytarget
*)wdt
;
128 pipe_texture_reference(&tex
, NULL
);
132 static struct sw_displaytarget
*
133 wsw_dt_create(struct sw_winsys
*ws
,
135 enum pipe_format format
,
136 unsigned width
, unsigned height
,
140 struct wrapper_sw_winsys
*wsw
= wrapper_sw_winsys(ws
);
141 struct pipe_texture templ
;
142 struct pipe_texture
*tex
;
145 * XXX Why don't we just get the template.
147 memset(&templ
, 0, sizeof(templ
));
148 templ
.width0
= width
;
149 templ
.height0
= height
;
150 templ
.format
= format
;
151 templ
.tex_usage
= tex_usage
;
153 /* XXX alignment: we can't do anything about this */
155 tex
= wsw
->screen
->texture_create(wsw
->screen
, &templ
);
159 return wsw_dt_wrap_texture(wsw
, tex
, stride
);
162 static struct sw_displaytarget
*
163 wsw_dt_from_handle(struct sw_winsys
*ws
,
164 const struct pipe_texture
*templ
,
165 struct winsys_handle
*whandle
,
168 struct wrapper_sw_winsys
*wsw
= wrapper_sw_winsys(ws
);
169 struct pipe_texture
*tex
;
171 tex
= wsw
->screen
->texture_from_handle(wsw
->screen
, templ
, whandle
);
175 return wsw_dt_wrap_texture(wsw
, tex
, stride
);
179 wsw_dt_map(struct sw_winsys
*ws
,
180 struct sw_displaytarget
*dt
,
183 struct wrapper_sw_displaytarget
*wdt
= wrapper_sw_displaytarget(dt
);
184 struct pipe_context
*pipe
= wdt
->winsys
->pipe
;
185 struct pipe_texture
*tex
= wdt
->tex
;
186 struct pipe_transfer
*tr
;
189 if (!wdt
->map_count
) {
191 assert(!wdt
->transfer
);
193 tr
= pipe
->get_tex_transfer(pipe
, tex
, 0, 0, 0,
194 PIPE_TRANSFER_READ_WRITE
,
195 0, 0, wdt
->width
, wdt
->height
);
199 ptr
= pipe
->transfer_map(pipe
, tr
);
206 /* XXX Handle this case */
207 assert(tr
->stride
== wdt
->stride
);
215 pipe
->tex_transfer_destroy(pipe
, tr
);
220 wsw_dt_unmap(struct sw_winsys
*ws
,
221 struct sw_displaytarget
*dt
)
223 struct wrapper_sw_displaytarget
*wdt
= wrapper_sw_displaytarget(dt
);
224 struct pipe_context
*pipe
= wdt
->winsys
->pipe
;
226 assert(wdt
->transfer
);
233 pipe
->transfer_unmap(pipe
, wdt
->transfer
);
234 pipe
->tex_transfer_destroy(pipe
, wdt
->transfer
);
235 wdt
->transfer
= NULL
;
239 wsw_dt_destroy(struct sw_winsys
*ws
,
240 struct sw_displaytarget
*dt
)
242 struct wrapper_sw_displaytarget
*wdt
= wrapper_sw_displaytarget(dt
);
244 pipe_texture_reference(&wdt
->tex
, NULL
);
250 wsw_destroy(struct sw_winsys
*ws
)
252 struct wrapper_sw_winsys
*wsw
= wrapper_sw_winsys(ws
);
254 wsw
->pipe
->destroy(wsw
->pipe
);
255 wsw
->screen
->destroy(wsw
->screen
);
261 wrapper_sw_winsys_warp_pipe_screen(struct pipe_screen
*screen
)
263 struct wrapper_sw_winsys
*wsw
= CALLOC_STRUCT(wrapper_sw_winsys
);
265 wsw
->base
.displaytarget_create
= wsw_dt_create
;
266 wsw
->base
.displaytarget_from_handle
= wsw_dt_from_handle
;
267 wsw
->base
.displaytarget_map
= wsw_dt_map
;
268 wsw
->base
.displaytarget_unmap
= wsw_dt_unmap
;
269 wsw
->base
.displaytarget_destroy
= wsw_dt_destroy
;
270 wsw
->base
.destroy
= wsw_destroy
;
272 wsw
->screen
= screen
;
273 wsw
->pipe
= screen
->context_create(screen
, NULL
);