Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / gallium / winsys / gdi / gdi_softpipe_winsys.c
1 /**************************************************************************
2 *
3 * Copyright 2008 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 * Softpipe support.
32 *
33 * @author Keith Whitwell
34 * @author Brian Paul
35 * @author Jose Fonseca
36 */
37
38
39 #include <windows.h>
40
41 #include "pipe/internal/p_winsys_screen.h"
42 #include "pipe/p_format.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_inlines.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
47 #include "softpipe/sp_winsys.h"
48 #include "softpipe/sp_texture.h"
49 #include "stw_winsys.h"
50
51
52 struct gdi_softpipe_buffer
53 {
54 struct pipe_buffer base;
55 boolean userBuffer; /** Is this a user-space buffer? */
56 void *data;
57 void *mapped;
58 };
59
60
61 /** Cast wrapper */
62 static INLINE struct gdi_softpipe_buffer *
63 gdi_softpipe_buffer( struct pipe_buffer *buf )
64 {
65 return (struct gdi_softpipe_buffer *)buf;
66 }
67
68
69 static void *
70 gdi_softpipe_buffer_map(struct pipe_winsys *winsys,
71 struct pipe_buffer *buf,
72 unsigned flags)
73 {
74 struct gdi_softpipe_buffer *gdi_softpipe_buf = gdi_softpipe_buffer(buf);
75 gdi_softpipe_buf->mapped = gdi_softpipe_buf->data;
76 return gdi_softpipe_buf->mapped;
77 }
78
79
80 static void
81 gdi_softpipe_buffer_unmap(struct pipe_winsys *winsys,
82 struct pipe_buffer *buf)
83 {
84 struct gdi_softpipe_buffer *gdi_softpipe_buf = gdi_softpipe_buffer(buf);
85 gdi_softpipe_buf->mapped = NULL;
86 }
87
88
89 static void
90 gdi_softpipe_buffer_destroy(struct pipe_buffer *buf)
91 {
92 struct gdi_softpipe_buffer *oldBuf = gdi_softpipe_buffer(buf);
93
94 if (oldBuf->data) {
95 if (!oldBuf->userBuffer)
96 align_free(oldBuf->data);
97
98 oldBuf->data = NULL;
99 }
100
101 FREE(oldBuf);
102 }
103
104
105 static const char *
106 gdi_softpipe_get_name(struct pipe_winsys *winsys)
107 {
108 return "softpipe";
109 }
110
111
112 static struct pipe_buffer *
113 gdi_softpipe_buffer_create(struct pipe_winsys *winsys,
114 unsigned alignment,
115 unsigned usage,
116 unsigned size)
117 {
118 struct gdi_softpipe_buffer *buffer = CALLOC_STRUCT(gdi_softpipe_buffer);
119
120 pipe_reference_init(&buffer->base.reference, 1);
121 buffer->base.alignment = alignment;
122 buffer->base.usage = usage;
123 buffer->base.size = size;
124
125 buffer->data = align_malloc(size, alignment);
126
127 return &buffer->base;
128 }
129
130
131 /**
132 * Create buffer which wraps user-space data.
133 */
134 static struct pipe_buffer *
135 gdi_softpipe_user_buffer_create(struct pipe_winsys *winsys,
136 void *ptr,
137 unsigned bytes)
138 {
139 struct gdi_softpipe_buffer *buffer;
140
141 buffer = CALLOC_STRUCT(gdi_softpipe_buffer);
142 if(!buffer)
143 return NULL;
144
145 pipe_reference_init(&buffer->base.reference, 1);
146 buffer->base.size = bytes;
147 buffer->userBuffer = TRUE;
148 buffer->data = ptr;
149
150 return &buffer->base;
151 }
152
153
154 /**
155 * Round n up to next multiple.
156 */
157 static INLINE unsigned
158 round_up(unsigned n, unsigned multiple)
159 {
160 return (n + multiple - 1) & ~(multiple - 1);
161 }
162
163
164 static struct pipe_buffer *
165 gdi_softpipe_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 gdi_softpipe_dummy_flush_frontbuffer(struct pipe_winsys *winsys,
189 struct pipe_surface *surface,
190 void *context_private)
191 {
192 assert(0);
193 }
194
195
196 static void
197 gdi_softpipe_fence_reference(struct pipe_winsys *winsys,
198 struct pipe_fence_handle **ptr,
199 struct pipe_fence_handle *fence)
200 {
201 }
202
203
204 static int
205 gdi_softpipe_fence_signalled(struct pipe_winsys *winsys,
206 struct pipe_fence_handle *fence,
207 unsigned flag)
208 {
209 return 0;
210 }
211
212
213 static int
214 gdi_softpipe_fence_finish(struct pipe_winsys *winsys,
215 struct pipe_fence_handle *fence,
216 unsigned flag)
217 {
218 return 0;
219 }
220
221
222 static void
223 gdi_softpipe_destroy(struct pipe_winsys *winsys)
224 {
225 FREE(winsys);
226 }
227
228
229 static struct pipe_screen *
230 gdi_softpipe_screen_create(void)
231 {
232 static struct pipe_winsys *winsys;
233 struct pipe_screen *screen;
234
235 winsys = CALLOC_STRUCT(pipe_winsys);
236 if(!winsys)
237 return NULL;
238
239 winsys->destroy = gdi_softpipe_destroy;
240
241 winsys->buffer_create = gdi_softpipe_buffer_create;
242 winsys->user_buffer_create = gdi_softpipe_user_buffer_create;
243 winsys->buffer_map = gdi_softpipe_buffer_map;
244 winsys->buffer_unmap = gdi_softpipe_buffer_unmap;
245 winsys->buffer_destroy = gdi_softpipe_buffer_destroy;
246
247 winsys->surface_buffer_create = gdi_softpipe_surface_buffer_create;
248
249 winsys->fence_reference = gdi_softpipe_fence_reference;
250 winsys->fence_signalled = gdi_softpipe_fence_signalled;
251 winsys->fence_finish = gdi_softpipe_fence_finish;
252
253 winsys->flush_frontbuffer = gdi_softpipe_dummy_flush_frontbuffer;
254 winsys->get_name = gdi_softpipe_get_name;
255
256 screen = softpipe_create_screen(winsys);
257 if(!screen)
258 gdi_softpipe_destroy(winsys);
259
260 return screen;
261 }
262
263
264 static struct pipe_context *
265 gdi_softpipe_context_create(struct pipe_screen *screen)
266 {
267 return softpipe_create(screen);
268 }
269
270
271 static void
272 gdi_softpipe_present(struct pipe_screen *screen,
273 struct pipe_surface *surface,
274 HDC hDC)
275 {
276 struct softpipe_texture *texture;
277 struct gdi_softpipe_buffer *buffer;
278 BITMAPINFO bmi;
279
280 texture = softpipe_texture(surface->texture);
281
282 buffer = gdi_softpipe_buffer(texture->buffer);
283
284 memset(&bmi, 0, sizeof(BITMAPINFO));
285 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
286 bmi.bmiHeader.biWidth = texture->stride[surface->level] / pf_get_size(surface->format);
287 bmi.bmiHeader.biHeight= -(long)surface->height;
288 bmi.bmiHeader.biPlanes = 1;
289 bmi.bmiHeader.biBitCount = pf_get_bits(surface->format);
290 bmi.bmiHeader.biCompression = BI_RGB;
291 bmi.bmiHeader.biSizeImage = 0;
292 bmi.bmiHeader.biXPelsPerMeter = 0;
293 bmi.bmiHeader.biYPelsPerMeter = 0;
294 bmi.bmiHeader.biClrUsed = 0;
295 bmi.bmiHeader.biClrImportant = 0;
296
297 StretchDIBits(hDC,
298 0, 0, surface->width, surface->height,
299 0, 0, surface->width, surface->height,
300 buffer->data, &bmi, 0, SRCCOPY);
301 }
302
303
304 static const struct stw_winsys stw_winsys = {
305 &gdi_softpipe_screen_create,
306 &gdi_softpipe_context_create,
307 &gdi_softpipe_present,
308 NULL, /* get_adapter_luid */
309 NULL, /* shared_surface_open */
310 NULL, /* shared_surface_close */
311 NULL /* compose */
312 };
313
314
315 BOOL WINAPI
316 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
317 {
318 switch (fdwReason) {
319 case DLL_PROCESS_ATTACH:
320 if (!stw_init(&stw_winsys)) {
321 return FALSE;
322 }
323 return stw_init_thread();
324
325 case DLL_THREAD_ATTACH:
326 return stw_init_thread();
327
328 case DLL_THREAD_DETACH:
329 stw_cleanup_thread();
330 break;
331
332 case DLL_PROCESS_DETACH:
333 stw_cleanup_thread();
334 stw_cleanup();
335 break;
336 }
337 return TRUE;
338 }