sw/wrapper: Make sure targets are textures
[mesa.git] / src / gallium / winsys / sw / wrapper / wrapper_sw_winsys.c
1 /**********************************************************
2 * Copyright 2010 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26
27 #include "wrapper_sw_winsys.h"
28
29 #include "pipe/p_format.h"
30 #include "pipe/p_state.h"
31
32 #include "state_tracker/sw_winsys.h"
33
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36
37 /*
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.
41 *
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).
46 *
47 * We also need a pipe context for the transfers.
48 */
49
50 struct wrapper_sw_winsys
51 {
52 struct sw_winsys base;
53 struct pipe_screen *screen;
54 struct pipe_context *pipe;
55 };
56
57 struct wrapper_sw_displaytarget
58 {
59 struct wrapper_sw_winsys *winsys;
60 struct pipe_resource *tex;
61 struct pipe_transfer *transfer;
62
63 unsigned width;
64 unsigned height;
65 unsigned map_count;
66 unsigned stride; /**< because we give stride at create */
67 void *ptr;
68 };
69
70 static INLINE struct wrapper_sw_winsys *
71 wrapper_sw_winsys(struct sw_winsys *ws)
72 {
73 return (struct wrapper_sw_winsys *)ws;
74 }
75
76 static INLINE struct wrapper_sw_displaytarget *
77 wrapper_sw_displaytarget(struct sw_displaytarget *dt)
78 {
79 return (struct wrapper_sw_displaytarget *)dt;
80 }
81
82
83 /*
84 * Functions
85 */
86
87
88 static boolean
89 wsw_dt_get_stride(struct wrapper_sw_displaytarget *wdt, unsigned *stride)
90 {
91 struct pipe_context *pipe = wdt->winsys->pipe;
92 struct pipe_resource *tex = wdt->tex;
93 struct pipe_transfer *tr;
94
95 tr = pipe_get_transfer(pipe, tex, 0, 0, 0,
96 PIPE_TRANSFER_READ_WRITE,
97 0, 0, wdt->width, wdt->height);
98 if (!tr)
99 return FALSE;
100
101 *stride = tr->stride;
102 wdt->stride = tr->stride;
103
104 pipe->transfer_destroy(pipe, tr);
105
106 return TRUE;
107 }
108
109 static struct sw_displaytarget *
110 wsw_dt_wrap_texture(struct wrapper_sw_winsys *wsw,
111 struct pipe_resource *tex, unsigned *stride)
112 {
113 struct wrapper_sw_displaytarget *wdt = CALLOC_STRUCT(wrapper_sw_displaytarget);
114 if (!wdt)
115 goto err_unref;
116
117 wdt->tex = tex;
118 wdt->winsys = wsw;
119
120 if (!wsw_dt_get_stride(wdt, stride))
121 goto err_free;
122
123 return (struct sw_displaytarget *)wdt;
124
125 err_free:
126 FREE(wdt);
127 err_unref:
128 pipe_resource_reference(&tex, NULL);
129 return NULL;
130 }
131
132 static struct sw_displaytarget *
133 wsw_dt_create(struct sw_winsys *ws,
134 unsigned bind,
135 enum pipe_format format,
136 unsigned width, unsigned height,
137 unsigned alignment,
138 unsigned *stride)
139 {
140 struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
141 struct pipe_resource templ;
142 struct pipe_resource *tex;
143
144 /*
145 * XXX Why don't we just get the template.
146 */
147 memset(&templ, 0, sizeof(templ));
148 templ.target = PIPE_TEXTURE_2D;
149 templ.width0 = width;
150 templ.height0 = height;
151 templ.format = format;
152 templ.bind = bind;
153
154 /* XXX alignment: we can't do anything about this */
155
156 tex = wsw->screen->resource_create(wsw->screen, &templ);
157 if (!tex)
158 return NULL;
159
160 return wsw_dt_wrap_texture(wsw, tex, stride);
161 }
162
163 static struct sw_displaytarget *
164 wsw_dt_from_handle(struct sw_winsys *ws,
165 const struct pipe_resource *templ,
166 struct winsys_handle *whandle,
167 unsigned *stride)
168 {
169 struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
170 struct pipe_resource *tex;
171
172 tex = wsw->screen->resource_from_handle(wsw->screen, templ, whandle);
173 if (!tex)
174 return NULL;
175
176 return wsw_dt_wrap_texture(wsw, tex, stride);
177 }
178
179 static void *
180 wsw_dt_map(struct sw_winsys *ws,
181 struct sw_displaytarget *dt,
182 unsigned flags)
183 {
184 struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
185 struct pipe_context *pipe = wdt->winsys->pipe;
186 struct pipe_resource *tex = wdt->tex;
187 struct pipe_transfer *tr;
188 void *ptr;
189
190 if (!wdt->map_count) {
191
192 assert(!wdt->transfer);
193
194 tr = pipe_get_transfer(pipe, tex, 0, 0, 0,
195 PIPE_TRANSFER_READ_WRITE,
196 0, 0, wdt->width, wdt->height);
197 if (!tr)
198 return NULL;
199
200 ptr = pipe->transfer_map(pipe, tr);
201 if (!ptr)
202 goto err;
203
204 wdt->transfer = tr;
205 wdt->ptr = ptr;
206
207 /* XXX Handle this case */
208 assert(tr->stride == wdt->stride);
209 }
210
211 wdt->map_count++;
212
213 return wdt->ptr;
214
215 err:
216 pipe->transfer_destroy(pipe, tr);
217 return NULL;
218 }
219
220 static void
221 wsw_dt_unmap(struct sw_winsys *ws,
222 struct sw_displaytarget *dt)
223 {
224 struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
225 struct pipe_context *pipe = wdt->winsys->pipe;
226
227 assert(wdt->transfer);
228
229 wdt->map_count--;
230
231 if (wdt->map_count)
232 return;
233
234 pipe->transfer_unmap(pipe, wdt->transfer);
235 pipe->transfer_destroy(pipe, wdt->transfer);
236 wdt->transfer = NULL;
237 }
238
239 static void
240 wsw_dt_destroy(struct sw_winsys *ws,
241 struct sw_displaytarget *dt)
242 {
243 struct wrapper_sw_displaytarget *wdt = wrapper_sw_displaytarget(dt);
244
245 pipe_resource_reference(&wdt->tex, NULL);
246
247 FREE(wdt);
248 }
249
250 static void
251 wsw_destroy(struct sw_winsys *ws)
252 {
253 struct wrapper_sw_winsys *wsw = wrapper_sw_winsys(ws);
254
255 wsw->pipe->destroy(wsw->pipe);
256 wsw->screen->destroy(wsw->screen);
257
258 FREE(wsw);
259 }
260
261 struct sw_winsys *
262 wrapper_sw_winsys_warp_pipe_screen(struct pipe_screen *screen)
263 {
264 struct wrapper_sw_winsys *wsw = CALLOC_STRUCT(wrapper_sw_winsys);
265
266 if (!wsw)
267 goto err;
268
269 wsw->base.displaytarget_create = wsw_dt_create;
270 wsw->base.displaytarget_from_handle = wsw_dt_from_handle;
271 wsw->base.displaytarget_map = wsw_dt_map;
272 wsw->base.displaytarget_unmap = wsw_dt_unmap;
273 wsw->base.displaytarget_destroy = wsw_dt_destroy;
274 wsw->base.destroy = wsw_destroy;
275
276 wsw->screen = screen;
277 wsw->pipe = screen->context_create(screen, NULL);
278 if (!wsw->pipe)
279 goto err_free;
280
281 return &wsw->base;
282
283 err_free:
284 FREE(wsw);
285 err:
286 return NULL;
287 }