i965: Fix ENDLOOP to only patch up this loop's BREAK and CONT.
[mesa.git] / src / gallium / drivers / softpipe / sp_winsys.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Bismarck, ND., USA
4 * All Rights Reserved.
5 *
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:
13 *
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.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28
29 /**
30 * @file
31 * Malloc softpipe winsys. Uses malloc for all memory allocations.
32 *
33 * @author Keith Whitwell
34 * @author Brian Paul
35 * @author Jose Fonseca
36 */
37
38
39 #include "util/u_simple_screen.h"/* port to just p_screen */
40 #include "util/u_format.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/u_inlines.h"
44 #include "pipe/p_format.h"
45 #include "pipe/p_context.h"
46 #include "sp_winsys.h"
47
48
49 struct st_softpipe_buffer
50 {
51 struct pipe_buffer base;
52 boolean userBuffer; /** Is this a user-space buffer? */
53 void *data;
54 void *mapped;
55 };
56
57
58 /** Cast wrapper */
59 static INLINE struct st_softpipe_buffer *
60 st_softpipe_buffer( struct pipe_buffer *buf )
61 {
62 return (struct st_softpipe_buffer *)buf;
63 }
64
65
66 static void *
67 st_softpipe_buffer_map(struct pipe_winsys *winsys,
68 struct pipe_buffer *buf,
69 unsigned flags)
70 {
71 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
72 st_softpipe_buf->mapped = st_softpipe_buf->data;
73 return st_softpipe_buf->mapped;
74 }
75
76
77 static void
78 st_softpipe_buffer_unmap(struct pipe_winsys *winsys,
79 struct pipe_buffer *buf)
80 {
81 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
82 st_softpipe_buf->mapped = NULL;
83 }
84
85
86 static void
87 st_softpipe_buffer_destroy(struct pipe_buffer *buf)
88 {
89 struct st_softpipe_buffer *oldBuf = st_softpipe_buffer(buf);
90
91 if (oldBuf->data) {
92 if (!oldBuf->userBuffer)
93 align_free(oldBuf->data);
94
95 oldBuf->data = NULL;
96 }
97
98 FREE(oldBuf);
99 }
100
101
102 static void
103 st_softpipe_flush_frontbuffer(struct pipe_winsys *winsys,
104 struct pipe_surface *surf,
105 void *context_private)
106 {
107 }
108
109
110
111 static const char *
112 st_softpipe_get_name(struct pipe_winsys *winsys)
113 {
114 return "softpipe";
115 }
116
117
118 static struct pipe_buffer *
119 st_softpipe_buffer_create(struct pipe_winsys *winsys,
120 unsigned alignment,
121 unsigned usage,
122 unsigned size)
123 {
124 struct st_softpipe_buffer *buffer = CALLOC_STRUCT(st_softpipe_buffer);
125
126 pipe_reference_init(&buffer->base.reference, 1);
127 buffer->base.alignment = alignment;
128 buffer->base.usage = usage;
129 buffer->base.size = size;
130
131 buffer->data = align_malloc(size, alignment);
132
133 return &buffer->base;
134 }
135
136
137 /**
138 * Create buffer which wraps user-space data.
139 */
140 static struct pipe_buffer *
141 st_softpipe_user_buffer_create(struct pipe_winsys *winsys,
142 void *ptr,
143 unsigned bytes)
144 {
145 struct st_softpipe_buffer *buffer;
146
147 buffer = CALLOC_STRUCT(st_softpipe_buffer);
148 if(!buffer)
149 return NULL;
150
151 pipe_reference_init(&buffer->base.reference, 1);
152 buffer->base.size = bytes;
153 buffer->userBuffer = TRUE;
154 buffer->data = ptr;
155
156 return &buffer->base;
157 }
158
159
160 static struct pipe_buffer *
161 st_softpipe_surface_buffer_create(struct pipe_winsys *winsys,
162 unsigned width, unsigned height,
163 enum pipe_format format,
164 unsigned usage,
165 unsigned tex_usage,
166 unsigned *stride)
167 {
168 const unsigned alignment = 64;
169 unsigned nblocksy;
170
171 nblocksy = util_format_get_nblocksy(format, height);
172 *stride = align(util_format_get_stride(format, width), alignment);
173
174 return winsys->buffer_create(winsys, alignment,
175 usage,
176 *stride * nblocksy);
177 }
178
179
180 static void
181 st_softpipe_fence_reference(struct pipe_winsys *winsys,
182 struct pipe_fence_handle **ptr,
183 struct pipe_fence_handle *fence)
184 {
185 }
186
187
188 static int
189 st_softpipe_fence_signalled(struct pipe_winsys *winsys,
190 struct pipe_fence_handle *fence,
191 unsigned flag)
192 {
193 return 0;
194 }
195
196
197 static int
198 st_softpipe_fence_finish(struct pipe_winsys *winsys,
199 struct pipe_fence_handle *fence,
200 unsigned flag)
201 {
202 return 0;
203 }
204
205
206 static void
207 st_softpipe_destroy(struct pipe_winsys *winsys)
208 {
209 FREE(winsys);
210 }
211
212
213 struct pipe_screen *
214 softpipe_create_screen_malloc(void)
215 {
216 static struct pipe_winsys *winsys;
217 struct pipe_screen *screen;
218
219 winsys = CALLOC_STRUCT(pipe_winsys);
220 if(!winsys)
221 return NULL;
222
223 winsys->destroy = st_softpipe_destroy;
224
225 winsys->buffer_create = st_softpipe_buffer_create;
226 winsys->user_buffer_create = st_softpipe_user_buffer_create;
227 winsys->buffer_map = st_softpipe_buffer_map;
228 winsys->buffer_unmap = st_softpipe_buffer_unmap;
229 winsys->buffer_destroy = st_softpipe_buffer_destroy;
230
231 winsys->surface_buffer_create = st_softpipe_surface_buffer_create;
232
233 winsys->fence_reference = st_softpipe_fence_reference;
234 winsys->fence_signalled = st_softpipe_fence_signalled;
235 winsys->fence_finish = st_softpipe_fence_finish;
236
237 winsys->flush_frontbuffer = st_softpipe_flush_frontbuffer;
238 winsys->get_name = st_softpipe_get_name;
239
240 screen = softpipe_create_screen(winsys);
241 if(!screen)
242 st_softpipe_destroy(winsys);
243
244 return screen;
245 }