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_memory.h>
34 #include <util/u_math.h>
35 #include <softpipe/sp_winsys.h>
36 #include <softpipe/sp_video_context.h>
37 #include <softpipe/sp_texture.h>
39 /* pipe_winsys implementation */
41 struct xsp_pipe_winsys
43 struct pipe_winsys base
;
53 void (*pipe_destroy
)(struct pipe_video_context
*vpipe
);
58 struct pipe_buffer base
;
59 boolean is_user_buffer
;
64 static struct pipe_buffer
* xsp_buffer_create(struct pipe_winsys
*pws
, unsigned alignment
, unsigned usage
, unsigned size
)
66 struct xsp_buffer
*buffer
;
70 buffer
= calloc(1, sizeof(struct xsp_buffer
));
71 pipe_reference_init(&buffer
->base
.reference
, 1);
72 buffer
->base
.alignment
= alignment
;
73 buffer
->base
.usage
= usage
;
74 buffer
->base
.size
= size
;
75 buffer
->data
= align_malloc(size
, alignment
);
77 return (struct pipe_buffer
*)buffer
;
80 static struct pipe_buffer
* xsp_user_buffer_create(struct pipe_winsys
*pws
, void *data
, unsigned size
)
82 struct xsp_buffer
*buffer
;
86 buffer
= calloc(1, sizeof(struct xsp_buffer
));
87 pipe_reference_init(&buffer
->base
.reference
, 1);
88 buffer
->base
.size
= size
;
89 buffer
->is_user_buffer
= TRUE
;
92 return (struct pipe_buffer
*)buffer
;
95 static void* xsp_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buffer
, unsigned flags
)
97 struct xsp_buffer
*xsp_buf
= (struct xsp_buffer
*)buffer
;
102 xsp_buf
->mapped_data
= xsp_buf
->data
;
104 return xsp_buf
->mapped_data
;
107 static void xsp_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buffer
)
109 struct xsp_buffer
*xsp_buf
= (struct xsp_buffer
*)buffer
;
114 xsp_buf
->mapped_data
= NULL
;
117 static void xsp_buffer_destroy(struct pipe_buffer
*buffer
)
119 struct xsp_buffer
*xsp_buf
= (struct xsp_buffer
*)buffer
;
123 if (!xsp_buf
->is_user_buffer
)
124 align_free(xsp_buf
->data
);
129 static struct pipe_buffer
* xsp_surface_buffer_create
131 struct pipe_winsys
*pws
,
134 enum pipe_format format
,
140 const unsigned int ALIGNMENT
= 1;
141 struct pipe_format_block block
;
142 unsigned nblocksx
, nblocksy
;
144 pf_get_block(format
, &block
);
145 nblocksx
= pf_get_nblocksx(&block
, width
);
146 nblocksy
= pf_get_nblocksy(&block
, height
);
147 *stride
= align(nblocksx
* block
.size
, ALIGNMENT
);
149 return pws
->buffer_create(pws
, ALIGNMENT
, usage
,
153 static void xsp_fence_reference(struct pipe_winsys
*pws
, struct pipe_fence_handle
**ptr
, struct pipe_fence_handle
*fence
)
160 static int xsp_fence_signalled(struct pipe_winsys
*pws
, struct pipe_fence_handle
*fence
, unsigned flag
)
168 static int xsp_fence_finish(struct pipe_winsys
*pws
, struct pipe_fence_handle
*fence
, unsigned flag
)
176 static void xsp_flush_frontbuffer(struct pipe_winsys
*pws
, struct pipe_surface
*surface
, void *context_private
)
178 struct xsp_pipe_winsys
*xsp_winsys
;
179 struct xsp_context
*xsp_context
;
183 assert(context_private
);
185 xsp_winsys
= (struct xsp_pipe_winsys
*)pws
;
186 xsp_context
= (struct xsp_context
*)context_private
;
187 xsp_winsys
->fbimage
->width
= surface
->width
;
188 xsp_winsys
->fbimage
->height
= surface
->height
;
189 xsp_winsys
->fbimage
->bytes_per_line
= surface
->width
* (xsp_winsys
->fbimage
->bits_per_pixel
>> 3);
190 xsp_winsys
->fbimage
->data
= (char*)((struct xsp_buffer
*)softpipe_texture(surface
->texture
)->buffer
)->data
+ surface
->offset
;
194 xsp_winsys
->display
, xsp_context
->drawable
,
195 XDefaultGC(xsp_winsys
->display
, xsp_winsys
->screen
),
196 xsp_winsys
->fbimage
, 0, 0, 0, 0,
197 surface
->width
, surface
->height
199 XFlush(xsp_winsys
->display
);
202 static const char* xsp_get_name(struct pipe_winsys
*pws
)
205 return "X11 SoftPipe";
208 static void xsp_destroy(struct pipe_winsys
*pws
)
210 struct xsp_pipe_winsys
*xsp_winsys
= (struct xsp_pipe_winsys
*)pws
;
214 /* XDestroyImage() wants to free the data as well */
215 xsp_winsys
->fbimage
->data
= NULL
;
217 XDestroyImage(xsp_winsys
->fbimage
);
221 /* Called through pipe_video_context::destroy() */
222 static void xsp_pipe_destroy(struct pipe_video_context
*vpipe
)
224 struct xsp_context
*xsp_context
;
228 xsp_context
= vpipe
->priv
;
230 /* Call the original destroy */
231 xsp_context
->pipe_destroy(vpipe
);
236 /* Show starts here */
239 vl_video_bind_drawable(struct pipe_video_context
*vpipe
, Drawable drawable
)
241 struct xsp_context
*xsp_context
;
242 Drawable old_drawable
;
246 xsp_context
= vpipe
->priv
;
247 old_drawable
= xsp_context
->drawable
;
248 xsp_context
->drawable
= drawable
;
254 vl_screen_create(Display
*display
, int screen
)
256 struct xsp_pipe_winsys
*xsp_winsys
;
260 xsp_winsys
= CALLOC_STRUCT(xsp_pipe_winsys
);
264 xsp_winsys
->base
.buffer_create
= xsp_buffer_create
;
265 xsp_winsys
->base
.user_buffer_create
= xsp_user_buffer_create
;
266 xsp_winsys
->base
.buffer_map
= xsp_buffer_map
;
267 xsp_winsys
->base
.buffer_unmap
= xsp_buffer_unmap
;
268 xsp_winsys
->base
.buffer_destroy
= xsp_buffer_destroy
;
269 xsp_winsys
->base
.surface_buffer_create
= xsp_surface_buffer_create
;
270 xsp_winsys
->base
.fence_reference
= xsp_fence_reference
;
271 xsp_winsys
->base
.fence_signalled
= xsp_fence_signalled
;
272 xsp_winsys
->base
.fence_finish
= xsp_fence_finish
;
273 xsp_winsys
->base
.flush_frontbuffer
= xsp_flush_frontbuffer
;
274 xsp_winsys
->base
.get_name
= xsp_get_name
;
275 xsp_winsys
->base
.destroy
= xsp_destroy
;
276 xsp_winsys
->display
= display
;
277 xsp_winsys
->screen
= screen
;
278 xsp_winsys
->fbimage
= XCreateImage
281 XDefaultVisual(display
, screen
),
282 XDefaultDepth(display
, screen
),
286 0, /* Don't know the width and height until flush_frontbuffer */
292 if (!xsp_winsys
->fbimage
) {
297 XInitImage(xsp_winsys
->fbimage
);
299 return softpipe_create_screen(&xsp_winsys
->base
);
302 struct pipe_video_context
*
303 vl_video_create(Display
*display
, int screen
,
304 struct pipe_screen
*p_screen
,
305 enum pipe_video_profile profile
,
306 enum pipe_video_chroma_format chroma_format
,
307 unsigned width
, unsigned height
)
309 struct pipe_video_context
*vpipe
;
310 struct xsp_context
*xsp_context
;
313 assert(width
&& height
);
315 vpipe
= sp_video_create(p_screen
, profile
, chroma_format
, width
, height
);
319 xsp_context
= CALLOC_STRUCT(xsp_context
);
321 vpipe
->destroy(vpipe
);
325 /* Override this so we can free our xsp_context when the pipe is freed */
326 xsp_context
->pipe_destroy
= vpipe
->destroy
;
327 vpipe
->destroy
= xsp_pipe_destroy
;
329 vpipe
->priv
= xsp_context
;