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