summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/allegro/amesa.c
blob: 23ce8885096d6e6eefca3843a19737c6c25640c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Mesa 3-D graphics library
 * Version:  3.0
 * Copyright (C) 1995-1998  Brian Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <allegro.h>
#include "context.h"
#include "matrix.h"
#include "mtypes.h"
#include "GL/amesa.h"


struct amesa_visual
    {
    GLvisual   *GLVisual;       /* inherit from GLvisual      */
    GLboolean   DBFlag;         /* double buffered?           */
    GLuint      Depth;          /* bits per pixel ( >= 15 )   */
    };


struct amesa_buffer
    {
    GLframebuffer *GLBuffer;    /* inherit from GLframebuffer */
    GLuint         Width, Height;
    BITMAP        *Screen;
	BITMAP        *Background;
	BITMAP        *Active;
    };


struct amesa_context
    {
    GLcontext   *GLContext;     /* inherit from GLcontext     */
    AMesaVisual  Visual;
	AMesaBuffer  Buffer;
    GLuint       ClearColor;
    GLuint       CurrentColor;
    };


static void setup_dd_pointers(GLcontext *ctx);


/**********************************************************************/
/*****                   drawing functions                        *****/
/**********************************************************************/

#define FLIP(context, y)  (context->Buffer->Height - (y) - 1)

#include "allegro/generic.h"
#include "allegro/direct.h"


/**********************************************************************/
/*****            15-bit accelerated drawing funcs                *****/
/**********************************************************************/

IMPLEMENT_WRITE_RGBA_SPAN(15, unsigned short)
IMPLEMENT_WRITE_RGB_SPAN(15, unsigned short)
IMPLEMENT_WRITE_MONO_RGBA_SPAN(15, unsigned short)
IMPLEMENT_READ_RGBA_SPAN(15, unsigned short)
IMPLEMENT_WRITE_RGBA_PIXELS(15, unsigned short)
IMPLEMENT_WRITE_MONO_RGBA_PIXELS(15, unsigned short)
IMPLEMENT_READ_RGBA_PIXELS(15, unsigned short)


/**********************************************************************/
/*****            16-bit accelerated drawing funcs                *****/
/**********************************************************************/

IMPLEMENT_WRITE_RGBA_SPAN(16, unsigned short)
IMPLEMENT_WRITE_RGB_SPAN(16, unsigned short)
IMPLEMENT_WRITE_MONO_RGBA_SPAN(16, unsigned short)
IMPLEMENT_READ_RGBA_SPAN(16, unsigned short)
IMPLEMENT_WRITE_RGBA_PIXELS(16, unsigned short)
IMPLEMENT_WRITE_MONO_RGBA_PIXELS(16, unsigned short)
IMPLEMENT_READ_RGBA_PIXELS(16, unsigned short)


/**********************************************************************/
/*****            32-bit accelerated drawing funcs                *****/
/**********************************************************************/

IMPLEMENT_WRITE_RGBA_SPAN(32, unsigned long)
IMPLEMENT_WRITE_RGB_SPAN(32, unsigned long)
IMPLEMENT_WRITE_MONO_RGBA_SPAN(32, unsigned long)
IMPLEMENT_READ_RGBA_SPAN(32, unsigned long)
IMPLEMENT_WRITE_RGBA_PIXELS(32, unsigned long)
IMPLEMENT_WRITE_MONO_RGBA_PIXELS(32, unsigned long)
IMPLEMENT_READ_RGBA_PIXELS(32, unsigned long)


/**********************************************************************/
/*****              Miscellaneous device driver funcs             *****/
/**********************************************************************/

static GLboolean set_buffer(GLcontext *ctx, GLenum mode)
    {
    AMesaContext context = (AMesaContext)(ctx->DriverCtx);
    GLboolean    ok      = GL_TRUE;

    if (mode == GL_FRONT_LEFT)
        context->Buffer->Active = context->Buffer->Screen;

    else if (mode == GL_BACK_LEFT)
        {
        if (context->Buffer->Background)
            context->Buffer->Active = context->Buffer->Background;
        else
            ok = GL_FALSE;
        }

    else
        ok = GL_FALSE;

    return ok;
    }


static void get_buffer_size(GLcontext *ctx, GLuint *width, GLuint *height)
    {
	AMesaContext context = (AMesaContext)(ctx->DriverCtx);

    *width  = context->Buffer->Width;
    *height = context->Buffer->Height;
    }


/**********************************************************************/
/**********************************************************************/

static void setup_dd_pointers(GLcontext *ctx)
	{
	AMesaContext context = (AMesaContext)(ctx->DriverCtx);

   	/* Initialize all the pointers in the driver struct. Do this whenever */
   	/* a new context is made current or we change buffers via set_buffer! */

    ctx->Driver.UpdateState   = setup_dd_pointers;
   	ctx->Driver.SetBuffer     = set_buffer;
   	ctx->Driver.GetBufferSize = get_buffer_size;

    ctx->Driver.Color               = set_color_generic;
    ctx->Driver.ClearColor          = clear_color_generic;
    ctx->Driver.Clear               = clear_generic;
    ctx->Driver.WriteRGBASpan       = write_rgba_span_generic;
    ctx->Driver.WriteRGBSpan        = write_rgb_span_generic;
    ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_generic;
    ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_generic;
    ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_generic;
    ctx->Driver.ReadRGBASpan        = read_rgba_span_generic;
    ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_generic;

    if (context->Buffer->Active != screen)
        {
        switch (context->Visual->Depth)
            {
            case 15:
                ctx->Driver.WriteRGBASpan       = write_rgba_span_15;
                ctx->Driver.WriteRGBSpan        = write_rgb_span_15;
                ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_15;
                ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_15;
                ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_15;
                ctx->Driver.ReadRGBASpan        = read_rgba_span_15;
                ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_15;
                break;

            case 16:
                ctx->Driver.WriteRGBASpan       = write_rgba_span_16;
                ctx->Driver.WriteRGBSpan        = write_rgb_span_16;
                ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_16;
                ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_16;
                ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_16;
                ctx->Driver.ReadRGBASpan        = read_rgba_span_16;
                ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_16;
                break;

            case 32:
                ctx->Driver.WriteRGBASpan       = write_rgba_span_32;
                ctx->Driver.WriteRGBSpan        = write_rgb_span_32;
                ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_32;
                ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_32;
                ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_32;
                ctx->Driver.ReadRGBASpan        = read_rgba_span_32;
                ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_32;
                break;
            }
        }
	}


/**********************************************************************/
/*****                AMesa Public API Functions                  *****/
/**********************************************************************/


AMesaVisual AMesaCreateVisual(GLboolean dbFlag, GLint depth,
                              GLint depthSize, GLint stencilSize, GLint accumSize)
	{
   	AMesaVisual visual;
    GLbyte      redBits, greenBits, blueBits;

    visual = (AMesaVisual)calloc(1, sizeof(struct amesa_visual));
   	if (!visual)
    	return NULL;

    switch (depth)
        {
        case 15:
            redBits   = 5;
            greenBits = 5;
            blueBits  = 5;
            break;

        case 16:
            redBits   = 5;
            greenBits = 6;
            blueBits  = 5;
            break;

        case 24: case 32:
            redBits   = 8;
            greenBits = 8;
            blueBits  = 8;
            break;

        default:
            free(visual);
            return NULL;
        }

    visual->DBFlag   = dbFlag;
    visual->Depth    = depth;
    visual->GLVisual = _mesa_create_visual(GL_TRUE,    /* rgb mode       */
                                           dbFlag,     /* db_flag        */
                                           GL_FALSE,   /* stereo         */
                                           redBits, greenBits, blueBits, 8,
                                           0,          /* index bits     */
                                           depthSize,  /* depth bits     */
                                           stencilSize,/* stencil bits   */
                                           accumSize,  /* accum bits     */
                                           accumSize,  /* accum bits     */
                                           accumSize,  /* accum bits     */
                                           accumSize,  /* accum bits     */
                                           1 );
    if (!visual->GLVisual)
        {
        free(visual);
        return NULL;
        }

  	return visual;
	}


void AMesaDestroyVisual(AMesaVisual visual)
	{
           _mesa_destroy_visual(visual->GLVisual);
           free(visual);
	}


AMesaBuffer AMesaCreateBuffer(AMesaVisual visual,
							  GLint width, GLint height)
	{
   	AMesaBuffer buffer;

    buffer = (AMesaBuffer)calloc(1, sizeof(struct amesa_buffer));
   	if (!buffer)
     	return NULL;

	buffer->Screen     = NULL;
	buffer->Background = NULL;
	buffer->Active     = NULL;
	buffer->Width      = width;
	buffer->Height     = height;

	if (visual->DBFlag)
		{
		buffer->Background = create_bitmap_ex(visual->Depth, width, height);
		if (!buffer->Background)
			{
			free(buffer);
			return NULL;
			}
		}

    buffer->GLBuffer = _mesa_create_framebuffer(visual->GLVisual);
    if (!buffer->GLBuffer)
        {
        if (buffer->Background) destroy_bitmap(buffer->Background);
        free(buffer);
        return NULL;
        }

   	return buffer;
	}


void AMesaDestroyBuffer(AMesaBuffer buffer)
{
   if (buffer->Screen)     destroy_bitmap(buffer->Screen);
   if (buffer->Background) destroy_bitmap(buffer->Background);
   _mesa_destroy_framebuffer(buffer->GLBuffer);
   free(buffer);
}


AMesaContext AMesaCreateContext(AMesaVisual visual,
                                AMesaContext share)
	{
   	AMesaContext context;
   	GLboolean    direct = GL_FALSE;

    context = (AMesaContext)calloc(1, sizeof(struct amesa_context));
	if (!context)
		return NULL;

	context->Visual       = visual;
	context->Buffer		  = NULL;
	context->ClearColor   = 0;
	context->CurrentColor = 0;
    context->GLContext    = _mesa_create_context(visual->GLVisual,
                                              share ? share->GLContext : NULL,
                                  		      (void*)context,
                                              direct);
    if (!context->GLContext)
        {
        free(context);
        return NULL;
        }

   	return context;
	}


void AMesaDestroyContext(AMesaContext context)
{
   _mesa_destroy_context(context->GLContext);
   free(context);
}


GLboolean AMesaMakeCurrent(AMesaContext context, AMesaBuffer buffer)
{
   if (context && buffer) {
      set_color_depth(context->Visual->Depth);
      if (set_gfx_mode(GFX_AUTODETECT, buffer->Width, buffer->Height, 0, 0) != 0)
         return GL_FALSE;

      context->Buffer = buffer;
      buffer->Screen  = screen;
      buffer->Active  = buffer->Background ? buffer->Background : screen;
        
      setup_dd_pointers(context->GLContext);
      _mesa_make_current(context->GLContext, buffer->GLBuffer);
      gl_Viewport(context->GLContext, 0, 0, buffer->Width, buffer->Height);
   }
   else {
      destroy_bitmap(context->Buffer->Screen);
      context->Buffer->Screen = NULL;
      context->Buffer->Active = NULL;
      context->Buffer         = NULL;
      _mesa_make_current(NULL, NULL);
   }

   return GL_TRUE;
}


void AMesaSwapBuffers(AMesaBuffer buffer)
{
   if (buffer->Background) {
      blit(buffer->Background, buffer->Screen,
           0, 0, 0, 0,
           buffer->Width, buffer->Height);
   }
}