mesa: prefix more #includes with "main/"
[mesa.git] / src / mesa / drivers / dos / dmesa.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * DOS/DJGPP device driver for Mesa
27 *
28 * Author: Daniel Borca
29 * Email : dborca@users.sourceforge.net
30 * Web : http://www.geocities.com/dborca
31 */
32
33
34 #include "main/context.h"
35 #include "main/imports.h"
36 #include "main/mtypes.h"
37
38 #include "video.h"
39
40 #include "GL/osmesa.h"
41 #include "GL/dmesa.h"
42
43
44 /*
45 * This has nothing to do with Mesa Visual structure.
46 * We keep this one around for backwards compatibility,
47 * and to store video mode data for DMesaCreateContext.
48 */
49 struct dmesa_visual {
50 GLenum format; /* OSMesa framebuffer format */
51 GLint depthBits;
52 GLint stencilBits;
53 GLint accumBits;
54 };
55
56 /*
57 * This has nothing to do with Mesa Buffer structure.
58 * We keep this one around for backwards compatibility,
59 * and to store various data.
60 */
61 struct dmesa_buffer {
62 int xpos, ypos; /* position */
63 int width, height; /* size in pixels */
64 GLenum type;
65 void *the_window; /* your window handle, etc */
66 };
67
68 /*
69 * This has nothing to do with Mesa Context structure.
70 * We keep this one around for backwards compatibility,
71 * and to store real off-screen context.
72 */
73 struct dmesa_context {
74 OSMesaContext osmesa;
75 DMesaBuffer buffer;
76 };
77
78
79 static DMesaContext ctx;
80
81
82 /****************************************************************************
83 * DMesa Public API Functions
84 ***************************************************************************/
85
86 /*
87 * The exact arguments to this function will depend on your window system
88 */
89 DMesaVisual
90 DMesaCreateVisual (GLint width,
91 GLint height,
92 GLint colDepth,
93 GLint refresh,
94 GLboolean dbFlag,
95 GLboolean rgbFlag,
96 GLint alphaSize,
97 GLint depthSize,
98 GLint stencilSize,
99 GLint accumSize)
100 {
101 DMesaVisual visual;
102 GLenum format;
103 int fbbits;
104
105 if (dbFlag) {
106 return NULL;
107 }
108
109 if (!rgbFlag) {
110 format = OSMESA_COLOR_INDEX;
111 fbbits = 8;
112 } else if (alphaSize) {
113 format = OSMESA_BGRA;
114 fbbits = 32;
115 } else if (colDepth == 15 || colDepth == 16) {
116 format = OSMESA_RGB_565;
117 fbbits = 16;
118 } else {
119 format = OSMESA_BGR;
120 fbbits = 24;
121 }
122
123 if ((visual = (DMesaVisual)CALLOC_STRUCT(dmesa_visual)) == NULL) {
124 return NULL;
125 }
126
127 if (vl_video_init(width, height, colDepth, rgbFlag, refresh, fbbits) <= 0) {
128 FREE(visual);
129 return NULL;
130 }
131
132 visual->format = format;
133 visual->depthBits = depthSize;
134 visual->stencilBits = stencilSize;
135 visual->accumBits = accumSize;
136 return visual;
137 }
138
139
140 void
141 DMesaDestroyVisual (DMesaVisual visual)
142 {
143 vl_video_exit();
144 FREE(visual);
145 }
146
147
148 DMesaBuffer
149 DMesaCreateBuffer (DMesaVisual visual,
150 GLint xpos, GLint ypos,
151 GLint width, GLint height)
152 {
153 DMesaBuffer buffer;
154 GLenum type;
155 int bytesPerPixel;
156
157 switch (visual->format) {
158 case OSMESA_COLOR_INDEX:
159 bytesPerPixel = 1;
160 type = CHAN_TYPE;
161 break;
162 case OSMESA_RGB_565:
163 bytesPerPixel = 2;
164 type = GL_UNSIGNED_SHORT_5_6_5;
165 break;
166 case OSMESA_BGR:
167 bytesPerPixel = 3;
168 type = CHAN_TYPE;
169 break;
170 default:
171 bytesPerPixel = 4;
172 type = CHAN_TYPE;
173 }
174
175 if ((buffer = (DMesaBuffer)CALLOC_STRUCT(dmesa_buffer)) != NULL) {
176 buffer->xpos = xpos;
177 buffer->ypos = ypos;
178 buffer->width = width;
179 buffer->height = height;
180 buffer->type = type;
181 buffer->the_window = MALLOC(width * height * bytesPerPixel + 1);
182 if (buffer->the_window == NULL) {
183 FREE(buffer);
184 buffer = NULL;
185 }
186 }
187
188 return buffer;
189 }
190
191
192 void
193 DMesaDestroyBuffer (DMesaBuffer buffer)
194 {
195 FREE(buffer->the_window);
196 FREE(buffer);
197 }
198
199
200 DMesaContext
201 DMesaCreateContext (DMesaVisual visual, DMesaContext share)
202 {
203 DMesaContext dmesa;
204 if ((dmesa = (DMesaContext)CALLOC_STRUCT(dmesa_context)) != NULL) {
205 dmesa->osmesa = OSMesaCreateContextExt(
206 visual->format,
207 visual->depthBits,
208 visual->stencilBits,
209 visual->accumBits,
210 (share != NULL) ? share->osmesa : NULL);
211 if (dmesa->osmesa == NULL) {
212 FREE(dmesa);
213 dmesa = NULL;
214 }
215 }
216 return dmesa;
217 }
218
219
220 void
221 DMesaDestroyContext (DMesaContext dmesa)
222 {
223 OSMesaDestroyContext(dmesa->osmesa);
224 FREE(dmesa);
225 }
226
227
228 GLboolean
229 DMesaMoveBuffer (GLint xpos, GLint ypos)
230 {
231 const DMesaContext dmesa = DMesaGetCurrentContext();
232 DMesaBuffer b = dmesa->buffer;
233
234 if (vl_sync_buffer(&b->the_window, xpos, ypos, b->width, b->height) == 0) {
235 b->xpos = xpos;
236 b->ypos = ypos;
237 return GL_TRUE;
238 }
239
240 return GL_FALSE;
241 }
242
243
244 GLboolean
245 DMesaResizeBuffer (GLint width, GLint height)
246 {
247 const DMesaContext dmesa = DMesaGetCurrentContext();
248 DMesaBuffer b = dmesa->buffer;
249
250 if (vl_sync_buffer(&b->the_window, b->xpos, b->ypos, width, height) == 0) {
251 b->width = width;
252 b->height = height;
253 return GL_TRUE;
254 }
255
256 return GL_FALSE;
257 }
258
259
260 GLboolean
261 DMesaMakeCurrent (DMesaContext dmesa, DMesaBuffer buffer)
262 {
263 if (dmesa == NULL || buffer == NULL) {
264 ctx = NULL;
265 return GL_TRUE;
266 }
267 if (OSMesaMakeCurrent(dmesa->osmesa, buffer->the_window,
268 buffer->type,
269 buffer->width, buffer->height) &&
270 vl_sync_buffer(&buffer->the_window, buffer->xpos, buffer->ypos, buffer->width, buffer->height) == 0) {
271 OSMesaPixelStore(OSMESA_Y_UP, GL_FALSE);
272 dmesa->buffer = buffer;
273 ctx = dmesa;
274 return GL_TRUE;
275 }
276 return GL_FALSE;
277 }
278
279
280 void
281 DMesaSwapBuffers (DMesaBuffer buffer)
282 {
283 /* copy/swap back buffer to front if applicable */
284 GET_CURRENT_CONTEXT(ctx);
285 _mesa_notifySwapBuffers(ctx);
286 vl_flip();
287 (void)buffer;
288 }
289
290
291 void
292 DMesaSetCI (int ndx, GLfloat red, GLfloat green, GLfloat blue)
293 {
294 vl_setCI(ndx, red, green, blue);
295 }
296
297
298 DMesaContext
299 DMesaGetCurrentContext (void)
300 {
301 return ctx;
302 }
303
304
305 DMesaBuffer
306 DMesaGetCurrentBuffer (void)
307 {
308 const DMesaContext dmesa = DMesaGetCurrentContext();
309
310 if (dmesa != NULL) {
311 return dmesa->buffer;
312 }
313
314 return NULL;
315 }
316
317
318 DMesaProc
319 DMesaGetProcAddress (const char *name)
320 {
321 DMesaProc p = (DMesaProc)_glapi_get_proc_address(name);
322
323 /* TODO: handle DMesa* namespace
324 if (p == NULL) {
325 }
326 */
327
328 return p;
329 }
330
331
332 int
333 DMesaGetIntegerv (GLenum pname, GLint *params)
334 {
335 switch (pname) {
336 case DMESA_GET_SCREEN_SIZE:
337 vl_get(VL_GET_SCREEN_SIZE, params);
338 break;
339 case DMESA_GET_DRIVER_CAPS:
340 params[0] = 0;
341 break;
342 case DMESA_GET_VIDEO_MODES:
343 return vl_get(VL_GET_VIDEO_MODES, params);
344 case DMESA_GET_BUFFER_ADDR: {
345 const DMesaContext dmesa = DMesaGetCurrentContext();
346 if (dmesa != NULL) {
347 DMesaBuffer b = dmesa->buffer;
348 if (b != NULL) {
349 params[0] = (GLint)b->the_window;
350 }
351 }
352 break;
353 }
354 default:
355 return -1;
356 }
357
358 return 0;
359 }