Header file clean-up:
[mesa.git] / src / mesa / drivers / allegro / amesa.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 3.0
4 * Copyright (C) 1995-1998 Brian Paul
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <allegro.h>
24 #include "context.h"
25 #include "imports.h"
26 #include "matrix.h"
27 #include "mtypes.h"
28 #include "GL/amesa.h"
29
30
31 struct amesa_visual
32 {
33 GLvisual *GLVisual; /* inherit from GLvisual */
34 GLboolean DBFlag; /* double buffered? */
35 GLuint Depth; /* bits per pixel ( >= 15 ) */
36 };
37
38
39 struct amesa_buffer
40 {
41 GLframebuffer *GLBuffer; /* inherit from GLframebuffer */
42 GLuint Width, Height;
43 BITMAP *Screen;
44 BITMAP *Background;
45 BITMAP *Active;
46 };
47
48
49 struct amesa_context
50 {
51 GLcontext *GLContext; /* inherit from GLcontext */
52 AMesaVisual Visual;
53 AMesaBuffer Buffer;
54 GLuint ClearColor;
55 GLuint CurrentColor;
56 };
57
58
59 static void setup_dd_pointers(GLcontext *ctx);
60
61
62 /**********************************************************************/
63 /***** drawing functions *****/
64 /**********************************************************************/
65
66 #define FLIP(context, y) (context->Buffer->Height - (y) - 1)
67
68 #include "allegro/generic.h"
69 #include "allegro/direct.h"
70
71
72 /**********************************************************************/
73 /***** 15-bit accelerated drawing funcs *****/
74 /**********************************************************************/
75
76 IMPLEMENT_WRITE_RGBA_SPAN(15, unsigned short)
77 IMPLEMENT_WRITE_RGB_SPAN(15, unsigned short)
78 IMPLEMENT_WRITE_MONO_RGBA_SPAN(15, unsigned short)
79 IMPLEMENT_READ_RGBA_SPAN(15, unsigned short)
80 IMPLEMENT_WRITE_RGBA_PIXELS(15, unsigned short)
81 IMPLEMENT_WRITE_MONO_RGBA_PIXELS(15, unsigned short)
82 IMPLEMENT_READ_RGBA_PIXELS(15, unsigned short)
83
84
85 /**********************************************************************/
86 /***** 16-bit accelerated drawing funcs *****/
87 /**********************************************************************/
88
89 IMPLEMENT_WRITE_RGBA_SPAN(16, unsigned short)
90 IMPLEMENT_WRITE_RGB_SPAN(16, unsigned short)
91 IMPLEMENT_WRITE_MONO_RGBA_SPAN(16, unsigned short)
92 IMPLEMENT_READ_RGBA_SPAN(16, unsigned short)
93 IMPLEMENT_WRITE_RGBA_PIXELS(16, unsigned short)
94 IMPLEMENT_WRITE_MONO_RGBA_PIXELS(16, unsigned short)
95 IMPLEMENT_READ_RGBA_PIXELS(16, unsigned short)
96
97
98 /**********************************************************************/
99 /***** 32-bit accelerated drawing funcs *****/
100 /**********************************************************************/
101
102 IMPLEMENT_WRITE_RGBA_SPAN(32, unsigned long)
103 IMPLEMENT_WRITE_RGB_SPAN(32, unsigned long)
104 IMPLEMENT_WRITE_MONO_RGBA_SPAN(32, unsigned long)
105 IMPLEMENT_READ_RGBA_SPAN(32, unsigned long)
106 IMPLEMENT_WRITE_RGBA_PIXELS(32, unsigned long)
107 IMPLEMENT_WRITE_MONO_RGBA_PIXELS(32, unsigned long)
108 IMPLEMENT_READ_RGBA_PIXELS(32, unsigned long)
109
110
111 /**********************************************************************/
112 /***** Miscellaneous device driver funcs *****/
113 /**********************************************************************/
114
115 static GLboolean set_buffer(GLcontext *ctx, GLframebuffer *buffer, GLuint bit)
116 {
117 AMesaContext context = (AMesaContext)(ctx->DriverCtx);
118 GLboolean ok = GL_TRUE;
119
120 if (bit == FRONT_LEFT_BIT)
121 context->Buffer->Active = context->Buffer->Screen;
122
123 else if (bit == BACK_LEFT)
124 {
125 if (context->Buffer->Background)
126 context->Buffer->Active = context->Buffer->Background;
127 else
128 ok = GL_FALSE;
129 }
130
131 else
132 ok = GL_FALSE;
133
134 return ok;
135 }
136
137
138 static void get_buffer_size(GLcontext *ctx, GLuint *width, GLuint *height)
139 {
140 AMesaContext context = (AMesaContext)(ctx->DriverCtx);
141
142 *width = context->Buffer->Width;
143 *height = context->Buffer->Height;
144 }
145
146
147 /**********************************************************************/
148 /**********************************************************************/
149
150 static void setup_dd_pointers(GLcontext *ctx)
151 {
152 AMesaContext context = (AMesaContext)(ctx->DriverCtx);
153
154 /* Initialize all the pointers in the driver struct. Do this whenever */
155 /* a new context is made current or we change buffers via set_buffer! */
156
157 ctx->Driver.UpdateState = setup_dd_pointers;
158 ctx->Driver.SetBuffer = set_buffer;
159 ctx->Driver.GetBufferSize = get_buffer_size;
160
161 ctx->Driver.Color = set_color_generic;
162 ctx->Driver.ClearColor = clear_color_generic;
163 ctx->Driver.Clear = clear_generic;
164 ctx->Driver.WriteRGBASpan = write_rgba_span_generic;
165 ctx->Driver.WriteRGBSpan = write_rgb_span_generic;
166 ctx->Driver.WriteMonoRGBASpan = write_mono_rgba_span_generic;
167 ctx->Driver.WriteRGBAPixels = write_rgba_pixels_generic;
168 ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_generic;
169 ctx->Driver.ReadRGBASpan = read_rgba_span_generic;
170 ctx->Driver.ReadRGBAPixels = read_rgba_pixels_generic;
171
172 if (context->Buffer->Active != screen)
173 {
174 switch (context->Visual->Depth)
175 {
176 case 15:
177 ctx->Driver.WriteRGBASpan = write_rgba_span_15;
178 ctx->Driver.WriteRGBSpan = write_rgb_span_15;
179 ctx->Driver.WriteMonoRGBASpan = write_mono_rgba_span_15;
180 ctx->Driver.WriteRGBAPixels = write_rgba_pixels_15;
181 ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_15;
182 ctx->Driver.ReadRGBASpan = read_rgba_span_15;
183 ctx->Driver.ReadRGBAPixels = read_rgba_pixels_15;
184 break;
185
186 case 16:
187 ctx->Driver.WriteRGBASpan = write_rgba_span_16;
188 ctx->Driver.WriteRGBSpan = write_rgb_span_16;
189 ctx->Driver.WriteMonoRGBASpan = write_mono_rgba_span_16;
190 ctx->Driver.WriteRGBAPixels = write_rgba_pixels_16;
191 ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_16;
192 ctx->Driver.ReadRGBASpan = read_rgba_span_16;
193 ctx->Driver.ReadRGBAPixels = read_rgba_pixels_16;
194 break;
195
196 case 32:
197 ctx->Driver.WriteRGBASpan = write_rgba_span_32;
198 ctx->Driver.WriteRGBSpan = write_rgb_span_32;
199 ctx->Driver.WriteMonoRGBASpan = write_mono_rgba_span_32;
200 ctx->Driver.WriteRGBAPixels = write_rgba_pixels_32;
201 ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_32;
202 ctx->Driver.ReadRGBASpan = read_rgba_span_32;
203 ctx->Driver.ReadRGBAPixels = read_rgba_pixels_32;
204 break;
205 }
206 }
207 }
208
209
210 /**********************************************************************/
211 /***** AMesa Public API Functions *****/
212 /**********************************************************************/
213
214
215 AMesaVisual AMesaCreateVisual(GLboolean dbFlag, GLint depth,
216 GLint depthSize, GLint stencilSize, GLint accumSize)
217 {
218 AMesaVisual visual;
219 GLbyte redBits, greenBits, blueBits;
220
221 visual = (AMesaVisual)calloc(1, sizeof(struct amesa_visual));
222 if (!visual)
223 return NULL;
224
225 switch (depth)
226 {
227 case 15:
228 redBits = 5;
229 greenBits = 5;
230 blueBits = 5;
231 break;
232
233 case 16:
234 redBits = 5;
235 greenBits = 6;
236 blueBits = 5;
237 break;
238
239 case 24: case 32:
240 redBits = 8;
241 greenBits = 8;
242 blueBits = 8;
243 break;
244
245 default:
246 free(visual);
247 return NULL;
248 }
249
250 visual->DBFlag = dbFlag;
251 visual->Depth = depth;
252 visual->GLVisual = _mesa_create_visual(GL_TRUE, /* rgb mode */
253 dbFlag, /* db_flag */
254 GL_FALSE, /* stereo */
255 redBits, greenBits, blueBits, 8,
256 0, /* index bits */
257 depthSize, /* depth bits */
258 stencilSize,/* stencil bits */
259 accumSize, /* accum bits */
260 accumSize, /* accum bits */
261 accumSize, /* accum bits */
262 accumSize, /* accum bits */
263 1 );
264 if (!visual->GLVisual)
265 {
266 free(visual);
267 return NULL;
268 }
269
270 return visual;
271 }
272
273
274 void AMesaDestroyVisual(AMesaVisual visual)
275 {
276 _mesa_destroy_visual(visual->GLVisual);
277 free(visual);
278 }
279
280
281 AMesaBuffer AMesaCreateBuffer(AMesaVisual visual,
282 GLint width, GLint height)
283 {
284 AMesaBuffer buffer;
285
286 buffer = (AMesaBuffer)calloc(1, sizeof(struct amesa_buffer));
287 if (!buffer)
288 return NULL;
289
290 buffer->Screen = NULL;
291 buffer->Background = NULL;
292 buffer->Active = NULL;
293 buffer->Width = width;
294 buffer->Height = height;
295
296 if (visual->DBFlag)
297 {
298 buffer->Background = create_bitmap_ex(visual->Depth, width, height);
299 if (!buffer->Background)
300 {
301 free(buffer);
302 return NULL;
303 }
304 }
305
306 buffer->GLBuffer = _mesa_create_framebuffer(visual->GLVisual);
307 if (!buffer->GLBuffer)
308 {
309 if (buffer->Background) destroy_bitmap(buffer->Background);
310 free(buffer);
311 return NULL;
312 }
313
314 return buffer;
315 }
316
317
318 void AMesaDestroyBuffer(AMesaBuffer buffer)
319 {
320 if (buffer->Screen) destroy_bitmap(buffer->Screen);
321 if (buffer->Background) destroy_bitmap(buffer->Background);
322 _mesa_destroy_framebuffer(buffer->GLBuffer);
323 free(buffer);
324 }
325
326
327 AMesaContext AMesaCreateContext(AMesaVisual visual,
328 AMesaContext share)
329 {
330 AMesaContext context;
331 GLboolean direct = GL_FALSE;
332
333 context = (AMesaContext)calloc(1, sizeof(struct amesa_context));
334 if (!context)
335 return NULL;
336
337 context->Visual = visual;
338 context->Buffer = NULL;
339 context->ClearColor = 0;
340 context->CurrentColor = 0;
341 context->GLContext = _mesa_create_context(visual->GLVisual,
342 share ? share->GLContext : NULL,
343 (void *) context, GL_FALSE );
344 if (!context->GLContext)
345 {
346 free(context);
347 return NULL;
348 }
349
350 return context;
351 }
352
353
354 void AMesaDestroyContext(AMesaContext context)
355 {
356 _mesa_destroy_context(context->GLContext);
357 free(context);
358 }
359
360
361 GLboolean AMesaMakeCurrent(AMesaContext context, AMesaBuffer buffer)
362 {
363 if (context && buffer) {
364 set_color_depth(context->Visual->Depth);
365 if (set_gfx_mode(GFX_AUTODETECT, buffer->Width, buffer->Height, 0, 0) != 0)
366 return GL_FALSE;
367
368 context->Buffer = buffer;
369 buffer->Screen = screen;
370 buffer->Active = buffer->Background ? buffer->Background : screen;
371
372 setup_dd_pointers(context->GLContext);
373 _mesa_make_current(context->GLContext, buffer->GLBuffer);
374 _mesa_set_viewport(context->GLContext, 0, 0, buffer->Width, buffer->Height);
375 }
376 else {
377 destroy_bitmap(context->Buffer->Screen);
378 context->Buffer->Screen = NULL;
379 context->Buffer->Active = NULL;
380 context->Buffer = NULL;
381 _mesa_make_current(NULL, NULL);
382 }
383
384 return GL_TRUE;
385 }
386
387
388 void AMesaSwapBuffers(AMesaBuffer buffer)
389 {
390 if (buffer->Background) {
391 blit(buffer->Background, buffer->Screen,
392 0, 0, 0, 0,
393 buffer->Width, buffer->Height);
394 }
395 }