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;
65 unsigned stride; /**< because we get stride at create */
69 static INLINE struct wrapper_sw_winsys *
70 wrapper_sw_winsys(struct sw_winsys *ws)
72 return (struct wrapper_sw_winsys *)ws;
75 static INLINE struct wrapper_sw_displaytarget *
76 wrapper_sw_displaytarget(struct sw_displaytarget *dt)
78 return (struct wrapper_sw_displaytarget *)dt;
88 wsw_dt_get_stride(struct wrapper_sw_displaytarget *wdt, unsigned *stride)
90 struct pipe_context *pipe = wdt->winsys->pipe;
91 struct pipe_resource *tex = wdt->tex;
92 struct pipe_transfer *tr;
94 tr = pipe_get_transfer(pipe, tex, 0, 0,
95 PIPE_TRANSFER_READ_WRITE,
96 0, 0, wdt->tex->width0, wdt->tex->height0);
100 *stride = tr->stride;
101 wdt->stride = tr->stride;
103 pipe->transfer_destroy(pipe, tr);
108 static struct sw_displaytarget *
109 wsw_dt_wrap_texture(struct wrapper_sw_winsys *wsw,
110 struct pipe_resource *tex, unsigned *stride)
112 struct wrapper_sw_displaytarget *wdt = CALLOC_STRUCT(wrapper_sw_displaytarget);
119 if (!wsw_dt_get_stride(wdt, stride))
122 return (struct sw_displaytarget *)wdt;
127 pipe_resource_reference(&tex, NULL);
131 static struct sw_displaytarget *
132 wsw_dt_create(struct sw_winsys *ws,
134 enum pipe_format format,
135 unsigned width, unsigned height,
139 struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
140 struct pipe_resource templ;
141 struct pipe_resource *tex;
144 * XXX Why don't we just get the template.
146 memset(&templ, 0, sizeof(templ));
147 templ.target = wsw->target;
148 templ.width0 = width;
149 templ.height0 = height;
151 templ.array_size = 1;
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,
208 PIPE_TRANSFER_READ_WRITE,
209 0, 0, wdt->tex->width0, wdt->tex->height0);
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 pipe->flush(pipe, NULL);
250 wdt->transfer = NULL;
254 wsw_dt_destroy(struct sw_winsys *ws,
255 struct sw_displaytarget *dt)
257 struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
259 pipe_resource_reference(&wdt->tex, NULL);
265 wsw_destroy(struct sw_winsys *ws)
267 struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
269 wsw->pipe->destroy(wsw->pipe);
270 wsw->screen->destroy(wsw->screen);
276 wrapper_sw_winsys_wrap_pipe_screen(struct pipe_screen *screen)
278 struct wrapper_sw_winsys *wsw = CALLOC_STRUCT(wrapper_sw_winsys);
283 wsw->base.displaytarget_create = wsw_dt_create;
284 wsw->base.displaytarget_from_handle = wsw_dt_from_handle;
285 wsw->base.displaytarget_get_handle = wsw_dt_get_handle;
286 wsw->base.displaytarget_map = wsw_dt_map;
287 wsw->base.displaytarget_unmap = wsw_dt_unmap;
288 wsw->base.displaytarget_destroy = wsw_dt_destroy;
289 wsw->base.destroy = wsw_destroy;
291 wsw->screen = screen;
292 wsw->pipe = screen->context_create(screen, NULL);
296 if(screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES))
297 wsw->target = PIPE_TEXTURE_2D;
299 wsw->target = PIPE_TEXTURE_RECT;
310 wrapper_sw_winsys_dewrap_pipe_screen(struct sw_winsys *ws)
312 struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
313 struct pipe_screen *screen = wsw->screen;
315 wsw->pipe->destroy(wsw->pipe);
316 /* don't destroy the screen its needed later on */