Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / winsys / gdi / gdi_llvmpipe_winsys.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
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 * LLVMpipe support.
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 */
35
36
37 #include <windows.h>
38
39 #include "pipe/p_format.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_inlines.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
44 #include "llvmpipe/lp_winsys.h"
45 #include "llvmpipe/lp_texture.h"
46 #include "shared/stw_winsys.h"
47
48
49 struct gdi_llvmpipe_displaytarget
50 {
51 enum pipe_format format;
52 struct pipe_format_block block;
53 unsigned width;
54 unsigned height;
55 unsigned stride;
56
57 unsigned size;
58
59 void *data;
60
61 BITMAPINFO bmi;
62 };
63
64
65 /** Cast wrapper */
66 static INLINE struct gdi_llvmpipe_displaytarget *
67 gdi_llvmpipe_displaytarget( struct llvmpipe_displaytarget *buf )
68 {
69 return (struct gdi_llvmpipe_displaytarget *)buf;
70 }
71
72
73 static boolean
74 gdi_llvmpipe_is_displaytarget_format_supported( struct llvmpipe_winsys *ws,
75 enum pipe_format format )
76 {
77 switch(format) {
78 case PIPE_FORMAT_X8R8G8B8_UNORM:
79 case PIPE_FORMAT_A8R8G8B8_UNORM:
80 return TRUE;
81
82 /* TODO: Support other formats possible with BMPs, as described in
83 * http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx */
84
85 default:
86 return FALSE;
87 }
88 }
89
90
91 static void *
92 gdi_llvmpipe_displaytarget_map(struct llvmpipe_winsys *ws,
93 struct llvmpipe_displaytarget *dt,
94 unsigned flags )
95 {
96 struct gdi_llvmpipe_displaytarget *gdt = gdi_llvmpipe_displaytarget(dt);
97
98 return gdt->data;
99 }
100
101
102 static void
103 gdi_llvmpipe_displaytarget_unmap(struct llvmpipe_winsys *ws,
104 struct llvmpipe_displaytarget *dt )
105 {
106
107 }
108
109
110 static void
111 gdi_llvmpipe_displaytarget_destroy(struct llvmpipe_winsys *winsys,
112 struct llvmpipe_displaytarget *dt)
113 {
114 struct gdi_llvmpipe_displaytarget *gdt = gdi_llvmpipe_displaytarget(dt);
115
116 align_free(gdt->data);
117 FREE(gdt);
118 }
119
120
121 /**
122 * Round n up to next multiple.
123 */
124 static INLINE unsigned
125 round_up(unsigned n, unsigned multiple)
126 {
127 return (n + multiple - 1) & ~(multiple - 1);
128 }
129
130
131 static struct llvmpipe_displaytarget *
132 gdi_llvmpipe_displaytarget_create(struct llvmpipe_winsys *winsys,
133 enum pipe_format format,
134 unsigned width, unsigned height,
135 unsigned alignment,
136 unsigned *stride)
137 {
138 struct gdi_llvmpipe_displaytarget *gdt;
139 unsigned cpp;
140 unsigned bpp;
141
142 gdt = CALLOC_STRUCT(gdi_llvmpipe_displaytarget);
143 if(!gdt)
144 goto no_gdt;
145
146 gdt->format = format;
147 gdt->width = width;
148 gdt->height = height;
149
150 bpp = pf_get_bits(format);
151 cpp = pf_get_size(format);
152
153 gdt->stride = round_up(width * cpp, alignment);
154 gdt->size = gdt->stride * height;
155
156 gdt->data = align_malloc(gdt->size, alignment);
157 if(!gdt->data)
158 goto no_data;
159
160 gdt->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
161 gdt->bmi.bmiHeader.biWidth = gdt->stride / cpp;
162 gdt->bmi.bmiHeader.biHeight= -(long)height;
163 gdt->bmi.bmiHeader.biPlanes = 1;
164 gdt->bmi.bmiHeader.biBitCount = bpp;
165 gdt->bmi.bmiHeader.biCompression = BI_RGB;
166 gdt->bmi.bmiHeader.biSizeImage = 0;
167 gdt->bmi.bmiHeader.biXPelsPerMeter = 0;
168 gdt->bmi.bmiHeader.biYPelsPerMeter = 0;
169 gdt->bmi.bmiHeader.biClrUsed = 0;
170 gdt->bmi.bmiHeader.biClrImportant = 0;
171
172 *stride = gdt->stride;
173 return (struct llvmpipe_displaytarget *)gdt;
174
175 no_data:
176 FREE(gdt);
177 no_gdt:
178 return NULL;
179 }
180
181
182 static void
183 gdi_llvmpipe_displaytarget_display(struct llvmpipe_winsys *winsys,
184 struct llvmpipe_displaytarget *dt,
185 void *context_private)
186 {
187 assert(0);
188 }
189
190
191 static void
192 gdi_llvmpipe_destroy(struct llvmpipe_winsys *winsys)
193 {
194 FREE(winsys);
195 }
196
197
198 static struct pipe_screen *
199 gdi_llvmpipe_screen_create(void)
200 {
201 static struct llvmpipe_winsys *winsys;
202 struct pipe_screen *screen;
203
204 winsys = CALLOC_STRUCT(llvmpipe_winsys);
205 if(!winsys)
206 goto no_winsys;
207
208 winsys->destroy = gdi_llvmpipe_destroy;
209 winsys->is_displaytarget_format_supported = gdi_llvmpipe_is_displaytarget_format_supported;
210 winsys->displaytarget_create = gdi_llvmpipe_displaytarget_create;
211 winsys->displaytarget_map = gdi_llvmpipe_displaytarget_map;
212 winsys->displaytarget_unmap = gdi_llvmpipe_displaytarget_unmap;
213 winsys->displaytarget_display = gdi_llvmpipe_displaytarget_display;
214 winsys->displaytarget_destroy = gdi_llvmpipe_displaytarget_destroy;
215
216 screen = llvmpipe_create_screen(winsys);
217 if(!screen)
218 goto no_screen;
219
220 return screen;
221
222 no_screen:
223 FREE(winsys);
224 no_winsys:
225 return NULL;
226 }
227
228
229 static struct pipe_context *
230 gdi_llvmpipe_context_create(struct pipe_screen *screen)
231 {
232 return llvmpipe_create(screen);
233 }
234
235
236 static void
237 gdi_llvmpipe_flush_frontbuffer(struct pipe_screen *screen,
238 struct pipe_surface *surface,
239 HDC hDC)
240 {
241 struct llvmpipe_texture *texture;
242 struct gdi_llvmpipe_displaytarget *gdt;
243
244 texture = llvmpipe_texture(surface->texture);
245 gdt = gdi_llvmpipe_displaytarget(texture->dt);
246
247 StretchDIBits(hDC,
248 0, 0, gdt->width, gdt->height,
249 0, 0, gdt->width, gdt->height,
250 gdt->data, &gdt->bmi, 0, SRCCOPY);
251 }
252
253
254 static const struct stw_winsys stw_winsys = {
255 &gdi_llvmpipe_screen_create,
256 &gdi_llvmpipe_context_create,
257 &gdi_llvmpipe_flush_frontbuffer
258 };
259
260
261 BOOL WINAPI
262 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
263 {
264 switch (fdwReason) {
265 case DLL_PROCESS_ATTACH:
266 if (!stw_init(&stw_winsys)) {
267 return FALSE;
268 }
269 return stw_init_thread();
270
271 case DLL_THREAD_ATTACH:
272 return stw_init_thread();
273
274 case DLL_THREAD_DETACH:
275 stw_cleanup_thread();
276 break;
277
278 case DLL_PROCESS_DETACH:
279 stw_cleanup_thread();
280 stw_cleanup();
281 break;
282 }
283 return TRUE;
284 }