Merge commit 'origin/gallium-0.2' into gallium-xlib-rework
[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/p_winsys.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 "stw_winsys.h"
49
50
51 struct gdi_softpipe_buffer
52 {
53 struct pipe_buffer base;
54 boolean userBuffer; /** Is this a user-space buffer? */
55 void *data;
56 void *mapped;
57 };
58
59
60 /** Cast wrapper */
61 static INLINE struct gdi_softpipe_buffer *
62 gdi_softpipe_buffer( struct pipe_buffer *buf )
63 {
64 return (struct gdi_softpipe_buffer *)buf;
65 }
66
67
68 static void *
69 gdi_softpipe_buffer_map(struct pipe_winsys *winsys,
70 struct pipe_buffer *buf,
71 unsigned flags)
72 {
73 struct gdi_softpipe_buffer *gdi_softpipe_buf = gdi_softpipe_buffer(buf);
74 gdi_softpipe_buf->mapped = gdi_softpipe_buf->data;
75 return gdi_softpipe_buf->mapped;
76 }
77
78
79 static void
80 gdi_softpipe_buffer_unmap(struct pipe_winsys *winsys,
81 struct pipe_buffer *buf)
82 {
83 struct gdi_softpipe_buffer *gdi_softpipe_buf = gdi_softpipe_buffer(buf);
84 gdi_softpipe_buf->mapped = NULL;
85 }
86
87
88 static void
89 gdi_softpipe_buffer_destroy(struct pipe_winsys *winsys,
90 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 buffer->base.refcount = 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 buffer->base.refcount = 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 int
165 gdi_softpipe_surface_alloc_storage(struct pipe_winsys *winsys,
166 struct pipe_surface *surf,
167 unsigned width, unsigned height,
168 enum pipe_format format,
169 unsigned flags,
170 unsigned tex_usage)
171 {
172 const unsigned alignment = 64;
173
174 surf->width = width;
175 surf->height = height;
176 surf->format = format;
177 pf_get_block(format, &surf->block);
178 surf->nblocksx = pf_get_nblocksx(&surf->block, width);
179 surf->nblocksy = pf_get_nblocksy(&surf->block, height);
180 surf->stride = round_up(surf->nblocksx * surf->block.size, alignment);
181 surf->usage = flags;
182
183 assert(!surf->buffer);
184 surf->buffer = winsys->buffer_create(winsys, alignment,
185 PIPE_BUFFER_USAGE_PIXEL,
186 surf->stride * surf->nblocksy);
187 if(!surf->buffer)
188 return -1;
189
190 return 0;
191 }
192
193
194 static struct pipe_surface *
195 gdi_softpipe_surface_alloc(struct pipe_winsys *winsys)
196 {
197 struct pipe_surface *surface = CALLOC_STRUCT(pipe_surface);
198
199 assert(winsys);
200
201 surface->refcount = 1;
202 surface->winsys = winsys;
203
204 return surface;
205 }
206
207
208 static void
209 gdi_softpipe_surface_release(struct pipe_winsys *winsys,
210 struct pipe_surface **s)
211 {
212 struct pipe_surface *surf = *s;
213 assert(!surf->texture);
214 surf->refcount--;
215 if (surf->refcount == 0) {
216 if (surf->buffer)
217 winsys_buffer_reference(winsys, &surf->buffer, NULL);
218 free(surf);
219 }
220 *s = NULL;
221 }
222
223
224 static void
225 gdi_softpipe_dummy_flush_frontbuffer(struct pipe_winsys *winsys,
226 struct pipe_surface *surface,
227 void *context_private)
228 {
229 assert(0);
230 }
231
232
233 static void
234 gdi_softpipe_fence_reference(struct pipe_winsys *winsys,
235 struct pipe_fence_handle **ptr,
236 struct pipe_fence_handle *fence)
237 {
238 }
239
240
241 static int
242 gdi_softpipe_fence_signalled(struct pipe_winsys *winsys,
243 struct pipe_fence_handle *fence,
244 unsigned flag)
245 {
246 return 0;
247 }
248
249
250 static int
251 gdi_softpipe_fence_finish(struct pipe_winsys *winsys,
252 struct pipe_fence_handle *fence,
253 unsigned flag)
254 {
255 return 0;
256 }
257
258
259 static void
260 gdi_softpipe_destroy(struct pipe_winsys *winsys)
261 {
262 FREE(winsys);
263 }
264
265
266 static struct pipe_screen *
267 gdi_softpipe_screen_create(void)
268 {
269 static struct pipe_winsys *winsys;
270 struct pipe_screen *screen;
271
272 winsys = CALLOC_STRUCT(pipe_winsys);
273 if(!winsys)
274 return NULL;
275
276 winsys->destroy = gdi_softpipe_destroy;
277
278 winsys->buffer_create = gdi_softpipe_buffer_create;
279 winsys->user_buffer_create = gdi_softpipe_user_buffer_create;
280 winsys->buffer_map = gdi_softpipe_buffer_map;
281 winsys->buffer_unmap = gdi_softpipe_buffer_unmap;
282 winsys->buffer_destroy = gdi_softpipe_buffer_destroy;
283
284 winsys->surface_alloc = gdi_softpipe_surface_alloc;
285 winsys->surface_alloc_storage = gdi_softpipe_surface_alloc_storage;
286 winsys->surface_release = gdi_softpipe_surface_release;
287
288 winsys->fence_reference = gdi_softpipe_fence_reference;
289 winsys->fence_signalled = gdi_softpipe_fence_signalled;
290 winsys->fence_finish = gdi_softpipe_fence_finish;
291
292 winsys->flush_frontbuffer = gdi_softpipe_dummy_flush_frontbuffer;
293 winsys->get_name = gdi_softpipe_get_name;
294
295 screen = softpipe_create_screen(winsys);
296 if(!screen)
297 gdi_softpipe_destroy(winsys);
298
299 return screen;
300 }
301
302
303 static struct pipe_context *
304 gdi_softpipe_context_create(struct pipe_screen *screen)
305 {
306 return softpipe_create(screen, screen->winsys, NULL);
307 }
308
309
310 static void
311 gdi_softpipe_flush_frontbuffer(struct pipe_winsys *winsys,
312 struct pipe_surface *surface,
313 HDC hDC)
314 {
315 struct gdi_softpipe_buffer *buffer;
316 BITMAPINFO bmi;
317
318 buffer = gdi_softpipe_buffer(surface->buffer);
319
320 memset(&bmi, 0, sizeof(BITMAPINFO));
321 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
322 bmi.bmiHeader.biWidth = surface->stride / pf_get_size(surface->format);
323 bmi.bmiHeader.biHeight= -(long)surface->height;
324 bmi.bmiHeader.biPlanes = 1;
325 bmi.bmiHeader.biBitCount = pf_get_bits(surface->format);
326 bmi.bmiHeader.biCompression = BI_RGB;
327 bmi.bmiHeader.biSizeImage = 0;
328 bmi.bmiHeader.biXPelsPerMeter = 0;
329 bmi.bmiHeader.biYPelsPerMeter = 0;
330 bmi.bmiHeader.biClrUsed = 0;
331 bmi.bmiHeader.biClrImportant = 0;
332
333 StretchDIBits(hDC,
334 0, 0, surface->width, surface->height,
335 0, 0, surface->width, surface->height,
336 buffer->data, &bmi, 0, SRCCOPY);
337 }
338
339
340 static const struct stw_winsys stw_winsys = {
341 &gdi_softpipe_screen_create,
342 &gdi_softpipe_context_create,
343 &gdi_softpipe_flush_frontbuffer
344 };
345
346
347 BOOL WINAPI
348 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
349 {
350 switch (fdwReason) {
351 case DLL_PROCESS_ATTACH:
352 return st_init(&stw_winsys);
353
354 case DLL_PROCESS_DETACH:
355 st_cleanup();
356 break;
357 }
358 return TRUE;
359 }