gallium: create target for gdi libgl
[mesa.git] / src / gallium / winsys / gdi / gdi_sw_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 * GDI software rasterizer 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 "util/u_inlines.h"
42 #include "util/u_format.h"
43 #include "util/u_math.h"
44 #include "util/u_memory.h"
45 #include "state_tracker/sw_winsys.h"
46 #include "gdi_sw_winsys.h"
47
48
49 struct gdi_sw_displaytarget
50 {
51 enum pipe_format format;
52 unsigned width;
53 unsigned height;
54 unsigned stride;
55
56 unsigned size;
57
58 void *data;
59
60 BITMAPINFO bmi;
61 };
62
63
64 /** Cast wrapper */
65 static INLINE struct gdi_sw_displaytarget *
66 gdi_sw_displaytarget( struct sw_displaytarget *buf )
67 {
68 return (struct gdi_sw_displaytarget *)buf;
69 }
70
71
72 static boolean
73 gdi_sw_is_displaytarget_format_supported( struct sw_winsys *ws,
74 enum pipe_format format )
75 {
76 switch(format) {
77 case PIPE_FORMAT_B8G8R8X8_UNORM:
78 case PIPE_FORMAT_B8G8R8A8_UNORM:
79 return TRUE;
80
81 /* TODO: Support other formats possible with BMPs, as described in
82 * http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx */
83
84 default:
85 return FALSE;
86 }
87 }
88
89
90 static void *
91 gdi_sw_displaytarget_map(struct sw_winsys *ws,
92 struct sw_displaytarget *dt,
93 unsigned flags )
94 {
95 struct gdi_sw_displaytarget *gdt = gdi_sw_displaytarget(dt);
96
97 return gdt->data;
98 }
99
100
101 static void
102 gdi_sw_displaytarget_unmap(struct sw_winsys *ws,
103 struct sw_displaytarget *dt )
104 {
105
106 }
107
108
109 static void
110 gdi_sw_displaytarget_destroy(struct sw_winsys *winsys,
111 struct sw_displaytarget *dt)
112 {
113 struct gdi_sw_displaytarget *gdt = gdi_sw_displaytarget(dt);
114
115 align_free(gdt->data);
116 FREE(gdt);
117 }
118
119
120 static struct sw_displaytarget *
121 gdi_sw_displaytarget_create(struct sw_winsys *winsys,
122 enum pipe_format format,
123 unsigned width, unsigned height,
124 unsigned alignment,
125 unsigned *stride)
126 {
127 struct gdi_sw_displaytarget *gdt;
128 unsigned cpp;
129 unsigned bpp;
130
131 gdt = CALLOC_STRUCT(gdi_sw_displaytarget);
132 if(!gdt)
133 goto no_gdt;
134
135 gdt->format = format;
136 gdt->width = width;
137 gdt->height = height;
138
139 bpp = util_format_get_blocksizebits(format);
140 cpp = util_format_get_blocksize(format);
141
142 gdt->stride = align(width * cpp, alignment);
143 gdt->size = gdt->stride * height;
144
145 gdt->data = align_malloc(gdt->size, alignment);
146 if(!gdt->data)
147 goto no_data;
148
149 gdt->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
150 gdt->bmi.bmiHeader.biWidth = gdt->stride / cpp;
151 gdt->bmi.bmiHeader.biHeight= -(long)height;
152 gdt->bmi.bmiHeader.biPlanes = 1;
153 gdt->bmi.bmiHeader.biBitCount = bpp;
154 gdt->bmi.bmiHeader.biCompression = BI_RGB;
155 gdt->bmi.bmiHeader.biSizeImage = 0;
156 gdt->bmi.bmiHeader.biXPelsPerMeter = 0;
157 gdt->bmi.bmiHeader.biYPelsPerMeter = 0;
158 gdt->bmi.bmiHeader.biClrUsed = 0;
159 gdt->bmi.bmiHeader.biClrImportant = 0;
160
161 *stride = gdt->stride;
162 return (struct sw_displaytarget *)gdt;
163
164 no_data:
165 FREE(gdt);
166 no_gdt:
167 return NULL;
168 }
169
170
171 void
172 gdi_sw_display( struct sw_winsys *winsys,
173 struct sw_displaytarget *dt,
174 HDC hDC )
175 {
176 struct gdi_sw_displaytarget *gdt = gdi_sw_displaytarget(dt);
177
178 StretchDIBits(hDC,
179 0, 0, gdt->width, gdt->height,
180 0, 0, gdt->width, gdt->height,
181 gdt->data, &gdt->bmi, 0, SRCCOPY);
182 }
183
184 static void
185 gdi_sw_displaytarget_display(struct sw_winsys *winsys,
186 struct sw_displaytarget *dt,
187 void *context_private)
188 {
189 /* nasty:
190 */
191 HDC hDC = (HDC)context_private;
192
193 gdi_sw_display(winsys, dt, hDC);
194 }
195
196
197 static void
198 gdi_sw_destroy(struct sw_winsys *winsys)
199 {
200 FREE(winsys);
201 }
202
203 struct sw_winsys *
204 gdi_create_sw_winsys(void)
205 {
206 static struct sw_winsys *winsys;
207
208 winsys = CALLOC_STRUCT(sw_winsys);
209 if(!winsys)
210 return NULL;
211
212 winsys->destroy = gdi_sw_destroy;
213 winsys->is_displaytarget_format_supported = gdi_sw_is_displaytarget_format_supported;
214 winsys->displaytarget_create = gdi_sw_displaytarget_create;
215 winsys->displaytarget_map = gdi_sw_displaytarget_map;
216 winsys->displaytarget_unmap = gdi_sw_displaytarget_unmap;
217 winsys->displaytarget_display = gdi_sw_displaytarget_display;
218 winsys->displaytarget_destroy = gdi_sw_displaytarget_destroy;
219
220 return winsys;
221 }
222