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 <util/u_simple_screen.h>
31 #include <pipe/p_state.h>
32 #include <util/u_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 /* TODO: Find a good way to calculate this */
41 static enum pipe_format
VisualToPipe(Visual
*visual
)
44 return PIPE_FORMAT_B8G8R8X8_UNORM
;
47 struct xsp_pipe_winsys
49 struct pipe_winsys base
;
57 struct vl_context base
;
64 struct pipe_buffer base
;
65 boolean is_user_buffer
;
70 static struct pipe_buffer
* xsp_buffer_create(struct pipe_winsys
*pws
, unsigned alignment
, unsigned usage
, unsigned size
)
72 struct xsp_buffer
*buffer
;
76 buffer
= calloc(1, sizeof(struct xsp_buffer
));
77 pipe_reference_init(&buffer
->base
.reference
, 1);
78 buffer
->base
.alignment
= alignment
;
79 buffer
->base
.usage
= usage
;
80 buffer
->base
.size
= size
;
81 buffer
->data
= align_malloc(size
, alignment
);
83 return (struct pipe_buffer
*)buffer
;
86 static struct pipe_buffer
* xsp_user_buffer_create(struct pipe_winsys
*pws
, void *data
, unsigned size
)
88 struct xsp_buffer
*buffer
;
92 buffer
= calloc(1, sizeof(struct xsp_buffer
));
93 pipe_reference_init(&buffer
->base
.reference
, 1);
94 buffer
->base
.size
= size
;
95 buffer
->is_user_buffer
= TRUE
;
98 return (struct pipe_buffer
*)buffer
;
101 static void* xsp_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buffer
, unsigned flags
)
103 struct xsp_buffer
*xsp_buf
= (struct xsp_buffer
*)buffer
;
108 xsp_buf
->mapped_data
= xsp_buf
->data
;
110 return xsp_buf
->mapped_data
;
113 static void xsp_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buffer
)
115 struct xsp_buffer
*xsp_buf
= (struct xsp_buffer
*)buffer
;
120 xsp_buf
->mapped_data
= NULL
;
123 static void xsp_buffer_destroy(struct pipe_buffer
*buffer
)
125 struct xsp_buffer
*xsp_buf
= (struct xsp_buffer
*)buffer
;
129 if (!xsp_buf
->is_user_buffer
)
130 align_free(xsp_buf
->data
);
135 static struct pipe_buffer
* xsp_surface_buffer_create
137 struct pipe_winsys
*pws
,
140 enum pipe_format format
,
146 const unsigned int ALIGNMENT
= 1;
149 nblocksy
= util_format_get_nblocksy(format
, height
);
150 *stride
= align(util_format_get_stride(format
, width
), ALIGNMENT
);
152 return pws
->buffer_create(pws
, ALIGNMENT
, usage
,
156 static void xsp_fence_reference(struct pipe_winsys
*pws
, struct pipe_fence_handle
**ptr
, struct pipe_fence_handle
*fence
)
163 static int xsp_fence_signalled(struct pipe_winsys
*pws
, struct pipe_fence_handle
*fence
, unsigned flag
)
171 static int xsp_fence_finish(struct pipe_winsys
*pws
, struct pipe_fence_handle
*fence
, unsigned flag
)
179 static void xsp_flush_frontbuffer(struct pipe_winsys
*pws
, struct pipe_surface
*surface
, void *context_private
)
181 struct xsp_pipe_winsys
*xsp_winsys
;
182 struct xsp_context
*xsp_context
;
186 assert(context_private
);
188 xsp_winsys
= (struct xsp_pipe_winsys
*)pws
;
189 xsp_context
= (struct xsp_context
*)context_private
;
190 xsp_winsys
->fbimage
->width
= surface
->width
;
191 xsp_winsys
->fbimage
->height
= surface
->height
;
192 xsp_winsys
->fbimage
->bytes_per_line
= surface
->width
* (xsp_winsys
->fbimage
->bits_per_pixel
>> 3);
193 xsp_winsys
->fbimage
->data
= (char*)((struct xsp_buffer
*)softpipe_texture(surface
->texture
)->buffer
)->data
+ surface
->offset
;
197 xsp_winsys
->display
, xsp_context
->drawable
,
198 XDefaultGC(xsp_winsys
->display
, xsp_winsys
->screen
),
199 xsp_winsys
->fbimage
, 0, 0, 0, 0,
200 surface
->width
, surface
->height
202 XFlush(xsp_winsys
->display
);
205 static const char* xsp_get_name(struct pipe_winsys
*pws
)
208 return "X11 SoftPipe";
211 static void xsp_destroy(struct pipe_winsys
*pws
)
213 struct xsp_pipe_winsys
*xsp_winsys
= (struct xsp_pipe_winsys
*)pws
;
217 /* XDestroyImage() wants to free the data as well */
218 xsp_winsys
->fbimage
->data
= NULL
;
220 XDestroyImage(xsp_winsys
->fbimage
);
225 vl_video_bind_drawable(struct vl_context
*vctx
, Drawable drawable
)
227 struct xsp_context
*xsp_context
= (struct xsp_context
*)vctx
;
228 Drawable old_drawable
;
232 old_drawable
= xsp_context
->drawable
;
233 xsp_context
->drawable
= drawable
;
239 vl_screen_create(Display
*display
, int screen
)
241 struct vl_screen
*vscreen
;
242 struct xsp_pipe_winsys
*xsp_winsys
;
246 vscreen
= CALLOC_STRUCT(vl_screen
);
250 xsp_winsys
= CALLOC_STRUCT(xsp_pipe_winsys
);
256 xsp_winsys
->base
.buffer_create
= xsp_buffer_create
;
257 xsp_winsys
->base
.user_buffer_create
= xsp_user_buffer_create
;
258 xsp_winsys
->base
.buffer_map
= xsp_buffer_map
;
259 xsp_winsys
->base
.buffer_unmap
= xsp_buffer_unmap
;
260 xsp_winsys
->base
.buffer_destroy
= xsp_buffer_destroy
;
261 xsp_winsys
->base
.surface_buffer_create
= xsp_surface_buffer_create
;
262 xsp_winsys
->base
.fence_reference
= xsp_fence_reference
;
263 xsp_winsys
->base
.fence_signalled
= xsp_fence_signalled
;
264 xsp_winsys
->base
.fence_finish
= xsp_fence_finish
;
265 xsp_winsys
->base
.flush_frontbuffer
= xsp_flush_frontbuffer
;
266 xsp_winsys
->base
.get_name
= xsp_get_name
;
267 xsp_winsys
->base
.destroy
= xsp_destroy
;
268 xsp_winsys
->display
= display
;
269 xsp_winsys
->screen
= screen
;
270 xsp_winsys
->fbimage
= XCreateImage
273 XDefaultVisual(display
, screen
),
274 XDefaultDepth(display
, screen
),
278 0, /* Don't know the width and height until flush_frontbuffer */
284 if (!xsp_winsys
->fbimage
) {
290 XInitImage(xsp_winsys
->fbimage
);
292 vscreen
->pscreen
= softpipe_create_screen(&xsp_winsys
->base
);
294 if (!vscreen
->pscreen
) {
296 XDestroyImage(xsp_winsys
->fbimage
);
301 vscreen
->format
= VisualToPipe(XDefaultVisual(display
, screen
));
306 void vl_screen_destroy(struct vl_screen
*vscreen
)
310 vscreen
->pscreen
->destroy(vscreen
->pscreen
);
315 vl_video_create(struct vl_screen
*vscreen
,
316 enum pipe_video_profile profile
,
317 enum pipe_video_chroma_format chroma_format
,
318 unsigned width
, unsigned height
)
320 struct pipe_video_context
*vpipe
;
321 struct xsp_context
*xsp_context
;
324 assert(width
&& height
);
326 vpipe
= sp_video_create(vscreen
->pscreen
, profile
, chroma_format
, width
, height
);
330 xsp_context
= CALLOC_STRUCT(xsp_context
);
332 vpipe
->destroy(vpipe
);
336 vpipe
->priv
= xsp_context
;
337 xsp_context
->base
.vpipe
= vpipe
;
338 xsp_context
->base
.vscreen
= vscreen
;
340 return &xsp_context
->base
;
343 void vl_video_destroy(struct vl_context
*vctx
)
347 vctx
->vpipe
->destroy(vctx
->vpipe
);