mesa: Don't bind DRAW/READ_FRAMEBUFFER separately without FBO blit support
[mesa.git] / src / gallium / winsys / egl_xlib / sw_winsys.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * Totally software-based winsys layer.
30 * Note that the one winsys function that we can't implement here
31 * is flush_frontbuffer().
32 * Whoever uses this code will have to provide that.
33 *
34 * Authors: Brian Paul
35 */
36
37
38 #include "pipe/internal/p_winsys_screen.h"
39 #include "pipe/p_state.h"
40 #include "pipe/p_inlines.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43
44 #include "sw_winsys.h"
45
46
47
48 /** Subclass of pipe_winsys */
49 struct sw_pipe_winsys
50 {
51 struct pipe_winsys Base;
52 /* no extra fields for now */
53 };
54
55
56 /** subclass of pipe_buffer */
57 struct sw_pipe_buffer
58 {
59 struct pipe_buffer Base;
60 boolean UserBuffer; /** Is this a user-space buffer? */
61 void *Data;
62 void *Mapped;
63 };
64
65
66 /** cast wrapper */
67 static INLINE struct sw_pipe_buffer *
68 sw_pipe_buffer(struct pipe_buffer *b)
69 {
70 return (struct sw_pipe_buffer *) b;
71 }
72
73
74 /**
75 * Round n up to next multiple.
76 */
77 static INLINE unsigned
78 round_up(unsigned n, unsigned multiple)
79 {
80 return (n + multiple - 1) & ~(multiple - 1);
81 }
82
83
84 static const char *
85 get_name(struct pipe_winsys *pws)
86 {
87 return "software";
88 }
89
90
91 /** Create new pipe_buffer and allocate storage of given size */
92 static struct pipe_buffer *
93 buffer_create(struct pipe_winsys *pws,
94 unsigned alignment,
95 unsigned usage,
96 unsigned size)
97 {
98 struct sw_pipe_buffer *buffer = CALLOC_STRUCT(sw_pipe_buffer);
99 if (!buffer)
100 return NULL;
101
102 pipe_reference_init(&buffer->Base.reference, 1);
103 buffer->Base.alignment = alignment;
104 buffer->Base.usage = usage;
105 buffer->Base.size = size;
106
107 /* align to 16-byte multiple for Cell */
108 buffer->Data = align_malloc(size, MAX2(alignment, 16));
109
110 return &buffer->Base;
111 }
112
113
114 /**
115 * Create buffer which wraps user-space data.
116 */
117 static struct pipe_buffer *
118 user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
119 {
120 struct sw_pipe_buffer *buffer = CALLOC_STRUCT(sw_pipe_buffer);
121 if (!buffer)
122 return NULL;
123
124 pipe_reference_init(&buffer->Base.reference, 1);
125 buffer->Base.size = bytes;
126 buffer->UserBuffer = TRUE;
127 buffer->Data = ptr;
128
129 return &buffer->Base;
130 }
131
132
133 static void *
134 buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buf, unsigned flags)
135 {
136 struct sw_pipe_buffer *buffer = sw_pipe_buffer(buf);
137 buffer->Mapped = buffer->Data;
138 return buffer->Mapped;
139 }
140
141
142 static void
143 buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buf)
144 {
145 struct sw_pipe_buffer *buffer = sw_pipe_buffer(buf);
146 buffer->Mapped = NULL;
147 }
148
149
150 static void
151 buffer_destroy(struct pipe_buffer *buf)
152 {
153 struct sw_pipe_buffer *buffer = sw_pipe_buffer(buf);
154
155 if (buffer->Data && !buffer->UserBuffer) {
156 align_free(buffer->Data);
157 buffer->Data = NULL;
158 }
159
160 free(buffer);
161 }
162
163
164 static struct pipe_buffer *
165 surface_buffer_create(struct pipe_winsys *winsys,
166 unsigned width, unsigned height,
167 enum pipe_format format,
168 unsigned usage,
169 unsigned tex_usage,
170 unsigned *stride)
171 {
172 const unsigned alignment = 64;
173 struct pipe_format_block block;
174 unsigned nblocksx, nblocksy;
175
176 pf_get_block(format, &block);
177 nblocksx = pf_get_nblocksx(&block, width);
178 nblocksy = pf_get_nblocksy(&block, height);
179 *stride = round_up(nblocksx * block.size, alignment);
180
181 return winsys->buffer_create(winsys, alignment,
182 usage,
183 *stride * nblocksy);
184 }
185
186
187 static void
188 fence_reference(struct pipe_winsys *sws, struct pipe_fence_handle **ptr,
189 struct pipe_fence_handle *fence)
190 {
191 /* no-op */
192 }
193
194
195 static int
196 fence_signalled(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
197 unsigned flag)
198 {
199 /* no-op */
200 return 0;
201 }
202
203
204 static int
205 fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
206 unsigned flag)
207 {
208 /* no-op */
209 return 0;
210 }
211
212
213 /**
214 * Create/return a new pipe_winsys object.
215 */
216 struct pipe_winsys *
217 create_sw_winsys(void)
218 {
219 struct sw_pipe_winsys *ws = CALLOC_STRUCT(sw_pipe_winsys);
220 if (!ws)
221 return NULL;
222
223 /* Fill in this struct with callbacks that pipe will need to
224 * communicate with the window system, buffer manager, etc.
225 */
226 ws->Base.buffer_create = buffer_create;
227 ws->Base.user_buffer_create = user_buffer_create;
228 ws->Base.buffer_map = buffer_map;
229 ws->Base.buffer_unmap = buffer_unmap;
230 ws->Base.buffer_destroy = buffer_destroy;
231
232 ws->Base.surface_buffer_create = surface_buffer_create;
233
234 ws->Base.fence_reference = fence_reference;
235 ws->Base.fence_signalled = fence_signalled;
236 ws->Base.fence_finish = fence_finish;
237
238 ws->Base.flush_frontbuffer = NULL; /* not implemented here! */
239
240 ws->Base.get_name = get_name;
241
242 return &ws->Base;
243 }