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