1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Bismarck, ND., USA
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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
27 **************************************************************************/
33 * @author Keith Whitwell
35 * @author Jose Fonseca
39 #include "pipe/p_winsys.h"
40 #include "pipe/p_format.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_util.h"
43 #include "pipe/p_inlines.h"
44 #include "softpipe/sp_winsys.h"
45 #include "st_winsys.h"
48 struct st_softpipe_buffer
50 struct pipe_buffer base
;
51 boolean userBuffer
; /** Is this a user-space buffer? */
58 static INLINE
struct st_softpipe_buffer
*
59 st_softpipe_buffer( struct pipe_buffer
*buf
)
61 return (struct st_softpipe_buffer
*)buf
;
66 st_softpipe_buffer_map(struct pipe_winsys
*winsys
,
67 struct pipe_buffer
*buf
,
70 struct st_softpipe_buffer
*st_softpipe_buf
= st_softpipe_buffer(buf
);
71 st_softpipe_buf
->mapped
= st_softpipe_buf
->data
;
72 return st_softpipe_buf
->mapped
;
77 st_softpipe_buffer_unmap(struct pipe_winsys
*winsys
,
78 struct pipe_buffer
*buf
)
80 struct st_softpipe_buffer
*st_softpipe_buf
= st_softpipe_buffer(buf
);
81 st_softpipe_buf
->mapped
= NULL
;
86 st_softpipe_buffer_destroy(struct pipe_winsys
*winsys
,
87 struct pipe_buffer
*buf
)
89 struct st_softpipe_buffer
*oldBuf
= st_softpipe_buffer(buf
);
92 if (!oldBuf
->userBuffer
)
93 align_free(oldBuf
->data
);
103 st_softpipe_flush_frontbuffer(struct pipe_winsys
*winsys
,
104 struct pipe_surface
*surf
,
105 void *context_private
)
112 st_softpipe_get_name(struct pipe_winsys
*winsys
)
118 static struct pipe_buffer
*
119 st_softpipe_buffer_create(struct pipe_winsys
*winsys
,
124 struct st_softpipe_buffer
*buffer
= CALLOC_STRUCT(st_softpipe_buffer
);
126 buffer
->base
.refcount
= 1;
127 buffer
->base
.alignment
= alignment
;
128 buffer
->base
.usage
= usage
;
129 buffer
->base
.size
= size
;
131 buffer
->data
= align_malloc(size
, alignment
);
133 return &buffer
->base
;
138 * Create buffer which wraps user-space data.
140 static struct pipe_buffer
*
141 st_softpipe_user_buffer_create(struct pipe_winsys
*winsys
,
145 struct st_softpipe_buffer
*buffer
;
147 buffer
= CALLOC_STRUCT(st_softpipe_buffer
);
151 buffer
->base
.refcount
= 1;
152 buffer
->base
.size
= bytes
;
153 buffer
->userBuffer
= TRUE
;
156 return &buffer
->base
;
161 * Round n up to next multiple.
163 static INLINE
unsigned
164 round_up(unsigned n
, unsigned multiple
)
166 return (n
+ multiple
- 1) & ~(multiple
- 1);
171 st_softpipe_surface_alloc_storage(struct pipe_winsys
*winsys
,
172 struct pipe_surface
*surf
,
173 unsigned width
, unsigned height
,
174 enum pipe_format format
,
178 const unsigned alignment
= 64;
181 surf
->height
= height
;
182 surf
->format
= format
;
183 pf_get_block(format
, &surf
->block
);
184 surf
->nblocksx
= pf_get_nblocksx(&surf
->block
, width
);
185 surf
->nblocksy
= pf_get_nblocksy(&surf
->block
, height
);
186 surf
->stride
= round_up(surf
->nblocksx
* surf
->block
.size
, alignment
);
189 assert(!surf
->buffer
);
190 surf
->buffer
= winsys
->buffer_create(winsys
, alignment
,
191 PIPE_BUFFER_USAGE_PIXEL
,
192 surf
->stride
* surf
->nblocksy
);
200 static struct pipe_surface
*
201 st_softpipe_surface_alloc(struct pipe_winsys
*winsys
)
203 struct pipe_surface
*surface
= CALLOC_STRUCT(pipe_surface
);
207 surface
->refcount
= 1;
208 surface
->winsys
= winsys
;
215 st_softpipe_surface_release(struct pipe_winsys
*winsys
,
216 struct pipe_surface
**s
)
218 struct pipe_surface
*surf
= *s
;
219 assert(!surf
->texture
);
221 if (surf
->refcount
== 0) {
223 pipe_buffer_reference(winsys
, &surf
->buffer
, NULL
);
231 st_softpipe_fence_reference(struct pipe_winsys
*winsys
,
232 struct pipe_fence_handle
**ptr
,
233 struct pipe_fence_handle
*fence
)
239 st_softpipe_fence_signalled(struct pipe_winsys
*winsys
,
240 struct pipe_fence_handle
*fence
,
248 st_softpipe_fence_finish(struct pipe_winsys
*winsys
,
249 struct pipe_fence_handle
*fence
,
257 st_softpipe_destroy(struct pipe_winsys
*winsys
)
263 static struct pipe_screen
*
264 st_softpipe_screen_create(void)
266 static struct pipe_winsys
*winsys
;
267 struct pipe_screen
*screen
;
269 winsys
= CALLOC_STRUCT(pipe_winsys
);
273 winsys
->destroy
= st_softpipe_destroy
;
275 winsys
->buffer_create
= st_softpipe_buffer_create
;
276 winsys
->user_buffer_create
= st_softpipe_user_buffer_create
;
277 winsys
->buffer_map
= st_softpipe_buffer_map
;
278 winsys
->buffer_unmap
= st_softpipe_buffer_unmap
;
279 winsys
->buffer_destroy
= st_softpipe_buffer_destroy
;
281 winsys
->surface_alloc
= st_softpipe_surface_alloc
;
282 winsys
->surface_alloc_storage
= st_softpipe_surface_alloc_storage
;
283 winsys
->surface_release
= st_softpipe_surface_release
;
285 winsys
->fence_reference
= st_softpipe_fence_reference
;
286 winsys
->fence_signalled
= st_softpipe_fence_signalled
;
287 winsys
->fence_finish
= st_softpipe_fence_finish
;
289 winsys
->flush_frontbuffer
= st_softpipe_flush_frontbuffer
;
290 winsys
->get_name
= st_softpipe_get_name
;
292 screen
= softpipe_create_screen(winsys
);
294 st_softpipe_destroy(winsys
);
300 static struct pipe_context
*
301 st_softpipe_context_create(struct pipe_screen
*screen
)
303 return softpipe_create(screen
, screen
->winsys
, NULL
);
307 const struct st_winsys st_softpipe_winsys
= {
308 &st_softpipe_screen_create
,
309 &st_softpipe_context_create
,