1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include <vl_winsys.h>
29 #include <X11/Xutil.h>
30 #include <pipe/internal/p_winsys_screen.h>
31 #include <pipe/p_state.h>
32 #include <pipe/p_inlines.h>
33 #include <util/u_format.h>
34 #include <util/u_memory.h>
35 #include <util/u_math.h>
36 #include <softpipe/sp_winsys.h>
37 #include <softpipe/sp_video_context.h>
38 #include <softpipe/sp_texture.h>
40 /* pipe_winsys implementation */
42 struct xsp_pipe_winsys
44 struct pipe_winsys base
;
54 void (*pipe_destroy
)(struct pipe_video_context
*vpipe
);
59 struct pipe_buffer base
;
60 boolean is_user_buffer
;
65 static struct pipe_buffer
* xsp_buffer_create(struct pipe_winsys
*pws
, unsigned alignment
, unsigned usage
, unsigned size
)
67 struct xsp_buffer
*buffer
;
71 buffer
= calloc(1, sizeof(struct xsp_buffer
));
72 pipe_reference_init(&buffer
->base
.reference
, 1);
73 buffer
->base
.alignment
= alignment
;
74 buffer
->base
.usage
= usage
;
75 buffer
->base
.size
= size
;
76 buffer
->data
= align_malloc(size
, alignment
);
78 return (struct pipe_buffer
*)buffer
;
81 static struct pipe_buffer
* xsp_user_buffer_create(struct pipe_winsys
*pws
, void *data
, unsigned size
)
83 struct xsp_buffer
*buffer
;
87 buffer
= calloc(1, sizeof(struct xsp_buffer
));
88 pipe_reference_init(&buffer
->base
.reference
, 1);
89 buffer
->base
.size
= size
;
90 buffer
->is_user_buffer
= TRUE
;
93 return (struct pipe_buffer
*)buffer
;
96 static void* xsp_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buffer
, unsigned flags
)
98 struct xsp_buffer
*xsp_buf
= (struct xsp_buffer
*)buffer
;
103 xsp_buf
->mapped_data
= xsp_buf
->data
;
105 return xsp_buf
->mapped_data
;
108 static void xsp_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buffer
)
110 struct xsp_buffer
*xsp_buf
= (struct xsp_buffer
*)buffer
;
115 xsp_buf
->mapped_data
= NULL
;
118 static void xsp_buffer_destroy(struct pipe_buffer
*buffer
)
120 struct xsp_buffer
*xsp_buf
= (struct xsp_buffer
*)buffer
;
124 if (!xsp_buf
->is_user_buffer
)
125 align_free(xsp_buf
->data
);
130 static struct pipe_buffer
* xsp_surface_buffer_create
132 struct pipe_winsys
*pws
,
135 enum pipe_format format
,
141 const unsigned int ALIGNMENT
= 1;
144 nblocksy
= util_format_get_nblocksy(format
, height
);
145 *stride
= align(util_format_get_stride(format
, width
), ALIGNMENT
);
147 return pws
->buffer_create(pws
, ALIGNMENT
, usage
,
151 static void xsp_fence_reference(struct pipe_winsys
*pws
, struct pipe_fence_handle
**ptr
, struct pipe_fence_handle
*fence
)
158 static int xsp_fence_signalled(struct pipe_winsys
*pws
, struct pipe_fence_handle
*fence
, unsigned flag
)
166 static int xsp_fence_finish(struct pipe_winsys
*pws
, struct pipe_fence_handle
*fence
, unsigned flag
)
174 static void xsp_flush_frontbuffer(struct pipe_winsys
*pws
, struct pipe_surface
*surface
, void *context_private
)
176 struct xsp_pipe_winsys
*xsp_winsys
;
177 struct xsp_context
*xsp_context
;
181 assert(context_private
);
183 xsp_winsys
= (struct xsp_pipe_winsys
*)pws
;
184 xsp_context
= (struct xsp_context
*)context_private
;
185 xsp_winsys
->fbimage
->width
= surface
->width
;
186 xsp_winsys
->fbimage
->height
= surface
->height
;
187 xsp_winsys
->fbimage
->bytes_per_line
= surface
->width
* (xsp_winsys
->fbimage
->bits_per_pixel
>> 3);
188 xsp_winsys
->fbimage
->data
= (char*)((struct xsp_buffer
*)softpipe_texture(surface
->texture
)->buffer
)->data
+ surface
->offset
;
192 xsp_winsys
->display
, xsp_context
->drawable
,
193 XDefaultGC(xsp_winsys
->display
, xsp_winsys
->screen
),
194 xsp_winsys
->fbimage
, 0, 0, 0, 0,
195 surface
->width
, surface
->height
197 XFlush(xsp_winsys
->display
);
200 static const char* xsp_get_name(struct pipe_winsys
*pws
)
203 return "X11 SoftPipe";
206 static void xsp_destroy(struct pipe_winsys
*pws
)
208 struct xsp_pipe_winsys
*xsp_winsys
= (struct xsp_pipe_winsys
*)pws
;
212 /* XDestroyImage() wants to free the data as well */
213 xsp_winsys
->fbimage
->data
= NULL
;
215 XDestroyImage(xsp_winsys
->fbimage
);
219 /* Called through pipe_video_context::destroy() */
220 static void xsp_pipe_destroy(struct pipe_video_context
*vpipe
)
222 struct xsp_context
*xsp_context
;
226 xsp_context
= vpipe
->priv
;
228 /* Call the original destroy */
229 xsp_context
->pipe_destroy(vpipe
);
234 /* Show starts here */
237 vl_video_bind_drawable(struct pipe_video_context
*vpipe
, Drawable drawable
)
239 struct xsp_context
*xsp_context
;
240 Drawable old_drawable
;
244 xsp_context
= vpipe
->priv
;
245 old_drawable
= xsp_context
->drawable
;
246 xsp_context
->drawable
= drawable
;
252 vl_screen_create(Display
*display
, int screen
)
254 struct xsp_pipe_winsys
*xsp_winsys
;
258 xsp_winsys
= CALLOC_STRUCT(xsp_pipe_winsys
);
262 xsp_winsys
->base
.buffer_create
= xsp_buffer_create
;
263 xsp_winsys
->base
.user_buffer_create
= xsp_user_buffer_create
;
264 xsp_winsys
->base
.buffer_map
= xsp_buffer_map
;
265 xsp_winsys
->base
.buffer_unmap
= xsp_buffer_unmap
;
266 xsp_winsys
->base
.buffer_destroy
= xsp_buffer_destroy
;
267 xsp_winsys
->base
.surface_buffer_create
= xsp_surface_buffer_create
;
268 xsp_winsys
->base
.fence_reference
= xsp_fence_reference
;
269 xsp_winsys
->base
.fence_signalled
= xsp_fence_signalled
;
270 xsp_winsys
->base
.fence_finish
= xsp_fence_finish
;
271 xsp_winsys
->base
.flush_frontbuffer
= xsp_flush_frontbuffer
;
272 xsp_winsys
->base
.get_name
= xsp_get_name
;
273 xsp_winsys
->base
.destroy
= xsp_destroy
;
274 xsp_winsys
->display
= display
;
275 xsp_winsys
->screen
= screen
;
276 xsp_winsys
->fbimage
= XCreateImage
279 XDefaultVisual(display
, screen
),
280 XDefaultDepth(display
, screen
),
284 0, /* Don't know the width and height until flush_frontbuffer */
290 if (!xsp_winsys
->fbimage
) {
295 XInitImage(xsp_winsys
->fbimage
);
297 return softpipe_create_screen(&xsp_winsys
->base
);
300 struct pipe_video_context
*
301 vl_video_create(Display
*display
, int screen
,
302 struct pipe_screen
*p_screen
,
303 enum pipe_video_profile profile
,
304 enum pipe_video_chroma_format chroma_format
,
305 unsigned width
, unsigned height
)
307 struct pipe_video_context
*vpipe
;
308 struct xsp_context
*xsp_context
;
311 assert(width
&& height
);
313 vpipe
= sp_video_create(p_screen
, profile
, chroma_format
, width
, height
);
317 xsp_context
= CALLOC_STRUCT(xsp_context
);
319 vpipe
->destroy(vpipe
);
323 /* Override this so we can free our xsp_context when the pipe is freed */
324 xsp_context
->pipe_destroy
= vpipe
->destroy
;
325 vpipe
->destroy
= xsp_pipe_destroy
;
327 vpipe
->priv
= xsp_context
;