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
;
55 enum pipe_texture_target target
;
58 struct wrapper_sw_displaytarget
60 struct wrapper_sw_winsys
*winsys
;
61 struct pipe_resource
*tex
;
62 struct pipe_transfer
*transfer
;
67 unsigned stride
; /**< because we give stride at create */
71 static INLINE
struct wrapper_sw_winsys
*
72 wrapper_sw_winsys(struct sw_winsys
*ws
)
74 return (struct wrapper_sw_winsys
*)ws
;
77 static INLINE
struct wrapper_sw_displaytarget
*
78 wrapper_sw_displaytarget(struct sw_displaytarget
*dt
)
80 return (struct wrapper_sw_displaytarget
*)dt
;
90 wsw_dt_get_stride(struct wrapper_sw_displaytarget
*wdt
, unsigned *stride
)
92 struct pipe_context
*pipe
= wdt
->winsys
->pipe
;
93 struct pipe_resource
*tex
= wdt
->tex
;
94 struct pipe_transfer
*tr
;
96 tr
= pipe_get_transfer(pipe
, tex
, 0, 0, 0,
97 PIPE_TRANSFER_READ_WRITE
,
98 0, 0, wdt
->width
, wdt
->height
);
102 *stride
= tr
->stride
;
103 wdt
->stride
= tr
->stride
;
105 pipe
->transfer_destroy(pipe
, tr
);
110 static struct sw_displaytarget
*
111 wsw_dt_wrap_texture(struct wrapper_sw_winsys
*wsw
,
112 struct pipe_resource
*tex
, unsigned *stride
)
114 struct wrapper_sw_displaytarget
*wdt
= CALLOC_STRUCT(wrapper_sw_displaytarget
);
121 if (!wsw_dt_get_stride(wdt
, stride
))
124 return (struct sw_displaytarget
*)wdt
;
129 pipe_resource_reference(&tex
, NULL
);
133 static struct sw_displaytarget
*
134 wsw_dt_create(struct sw_winsys
*ws
,
136 enum pipe_format format
,
137 unsigned width
, unsigned height
,
141 struct wrapper_sw_winsys
*wsw
= wrapper_sw_winsys(ws
);
142 struct pipe_resource templ
;
143 struct pipe_resource
*tex
;
146 * XXX Why don't we just get the template.
148 memset(&templ
, 0, sizeof(templ
));
149 templ
.target
= wsw
->target
;
150 templ
.width0
= width
;
151 templ
.height0
= height
;
152 templ
.format
= format
;
155 /* XXX alignment: we can't do anything about this */
157 tex
= wsw
->screen
->resource_create(wsw
->screen
, &templ
);
161 return wsw_dt_wrap_texture(wsw
, tex
, stride
);
164 static struct sw_displaytarget
*
165 wsw_dt_from_handle(struct sw_winsys
*ws
,
166 const struct pipe_resource
*templ
,
167 struct winsys_handle
*whandle
,
170 struct wrapper_sw_winsys
*wsw
= wrapper_sw_winsys(ws
);
171 struct pipe_resource
*tex
;
173 tex
= wsw
->screen
->resource_from_handle(wsw
->screen
, templ
, whandle
);
177 return wsw_dt_wrap_texture(wsw
, tex
, stride
);
181 wsw_dt_get_handle(struct sw_winsys
*ws
,
182 struct sw_displaytarget
*dt
,
183 struct winsys_handle
*whandle
)
185 struct wrapper_sw_winsys
*wsw
= wrapper_sw_winsys(ws
);
186 struct wrapper_sw_displaytarget
*wdt
= wrapper_sw_displaytarget(dt
);
187 struct pipe_resource
*tex
= wdt
->tex
;
189 return wsw
->screen
->resource_get_handle(wsw
->screen
, tex
, whandle
);
193 wsw_dt_map(struct sw_winsys
*ws
,
194 struct sw_displaytarget
*dt
,
197 struct wrapper_sw_displaytarget
*wdt
= wrapper_sw_displaytarget(dt
);
198 struct pipe_context
*pipe
= wdt
->winsys
->pipe
;
199 struct pipe_resource
*tex
= wdt
->tex
;
200 struct pipe_transfer
*tr
;
203 if (!wdt
->map_count
) {
205 assert(!wdt
->transfer
);
207 tr
= pipe_get_transfer(pipe
, tex
, 0, 0, 0,
208 PIPE_TRANSFER_READ_WRITE
,
209 0, 0, wdt
->width
, wdt
->height
);
213 ptr
= pipe
->transfer_map(pipe
, tr
);
220 /* XXX Handle this case */
221 assert(tr
->stride
== wdt
->stride
);
229 pipe
->transfer_destroy(pipe
, tr
);
234 wsw_dt_unmap(struct sw_winsys
*ws
,
235 struct sw_displaytarget
*dt
)
237 struct wrapper_sw_displaytarget
*wdt
= wrapper_sw_displaytarget(dt
);
238 struct pipe_context
*pipe
= wdt
->winsys
->pipe
;
240 assert(wdt
->transfer
);
247 pipe
->transfer_unmap(pipe
, wdt
->transfer
);
248 pipe
->transfer_destroy(pipe
, wdt
->transfer
);
249 wdt
->transfer
= NULL
;
253 wsw_dt_destroy(struct sw_winsys
*ws
,
254 struct sw_displaytarget
*dt
)
256 struct wrapper_sw_displaytarget
*wdt
= wrapper_sw_displaytarget(dt
);
258 pipe_resource_reference(&wdt
->tex
, NULL
);
264 wsw_destroy(struct sw_winsys
*ws
)
266 struct wrapper_sw_winsys
*wsw
= wrapper_sw_winsys(ws
);
268 wsw
->pipe
->destroy(wsw
->pipe
);
269 wsw
->screen
->destroy(wsw
->screen
);
275 wrapper_sw_winsys_warp_pipe_screen(struct pipe_screen
*screen
)
277 struct wrapper_sw_winsys
*wsw
= CALLOC_STRUCT(wrapper_sw_winsys
);
282 wsw
->base
.displaytarget_create
= wsw_dt_create
;
283 wsw
->base
.displaytarget_from_handle
= wsw_dt_from_handle
;
284 wsw
->base
.displaytarget_get_handle
= wsw_dt_get_handle
;
285 wsw
->base
.displaytarget_map
= wsw_dt_map
;
286 wsw
->base
.displaytarget_unmap
= wsw_dt_unmap
;
287 wsw
->base
.displaytarget_destroy
= wsw_dt_destroy
;
288 wsw
->base
.destroy
= wsw_destroy
;
290 wsw
->screen
= screen
;
291 wsw
->pipe
= screen
->context_create(screen
, NULL
);
295 if(screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
))
296 wsw
->target
= PIPE_TEXTURE_2D
;
298 wsw
->target
= PIPE_TEXTURE_RECT
;