src/video/ataricommon/SDL_atarigl.c
author Edgar Simo <bobbens@gmail.com>
Sun, 06 Jul 2008 17:06:37 +0000
branchgsoc2008_force_feedback
changeset 2498 ab567bd667bf
parent 2043 adf732f1f016
child 2698 e1da92da346c
permissions -rw-r--r--
Fixed various mistakes in the doxygen.
patmandin@989
     1
/*
patmandin@989
     2
    SDL - Simple DirectMedia Layer
patmandin@989
     3
    Copyright (C) 1997-2004 Sam Lantinga
patmandin@989
     4
patmandin@989
     5
    This library is free software; you can redistribute it and/or
patmandin@989
     6
    modify it under the terms of the GNU Library General Public
patmandin@989
     7
    License as published by the Free Software Foundation; either
patmandin@989
     8
    version 2 of the License, or (at your option) any later version.
patmandin@989
     9
patmandin@989
    10
    This library is distributed in the hope that it will be useful,
patmandin@989
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
patmandin@989
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
patmandin@989
    13
    Library General Public License for more details.
patmandin@989
    14
patmandin@989
    15
    You should have received a copy of the GNU Library General Public
patmandin@989
    16
    License along with this library; if not, write to the Free
patmandin@989
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
patmandin@989
    18
patmandin@989
    19
    Sam Lantinga
patmandin@989
    20
    slouken@libsdl.org
patmandin@989
    21
*/
slouken@1402
    22
#include "SDL_config.h"
patmandin@989
    23
patmandin@989
    24
/* Atari OSMesa.ldg implementation of SDL OpenGL support */
patmandin@989
    25
patmandin@989
    26
/*--- Includes ---*/
patmandin@989
    27
slouken@1361
    28
#if SDL_VIDEO_OPENGL
patmandin@989
    29
#include <GL/osmesa.h>
patmandin@989
    30
#endif
patmandin@989
    31
patmandin@991
    32
#include <mint/osbind.h>
patmandin@991
    33
slouken@1358
    34
#include "SDL_endian.h"
patmandin@989
    35
#include "SDL_video.h"
patmandin@989
    36
#include "SDL_atarigl_c.h"
slouken@1361
    37
#if SDL_VIDEO_OPENGL_OSMESA_DYNAMIC
patmandin@991
    38
#include "SDL_loadso.h"
patmandin@991
    39
#endif
patmandin@989
    40
patmandin@991
    41
/*--- Defines ---*/
patmandin@991
    42
patmandin@991
    43
#define PATH_OSMESA_LDG	"osmesa.ldg"
patmandin@991
    44
#define PATH_MESAGL_LDG	"mesa_gl.ldg"
patmandin@991
    45
#define PATH_TINYGL_LDG	"tiny_gl.ldg"
patmandin@989
    46
patmandin@1000
    47
#define VDI_RGB	0xf
patmandin@1000
    48
patmandin@989
    49
/*--- Functions prototypes ---*/
patmandin@989
    50
patmandin@991
    51
static void SDL_AtariGL_UnloadLibrary(_THIS);
patmandin@991
    52
slouken@1361
    53
#if SDL_VIDEO_OPENGL
slouken@1895
    54
static void CopyShadowNull(_THIS, SDL_Surface * surface);
slouken@1895
    55
static void CopyShadowDirect(_THIS, SDL_Surface * surface);
slouken@1895
    56
static void CopyShadowRGBTo555(_THIS, SDL_Surface * surface);
slouken@1895
    57
static void CopyShadowRGBTo565(_THIS, SDL_Surface * surface);
slouken@1895
    58
static void CopyShadowRGBSwap(_THIS, SDL_Surface * surface);
slouken@1895
    59
static void CopyShadowRGBToARGB(_THIS, SDL_Surface * surface);
slouken@1895
    60
static void CopyShadowRGBToABGR(_THIS, SDL_Surface * surface);
slouken@1895
    61
static void CopyShadowRGBToBGRA(_THIS, SDL_Surface * surface);
slouken@1895
    62
static void CopyShadowRGBToRGBA(_THIS, SDL_Surface * surface);
slouken@1895
    63
static void CopyShadow8888To555(_THIS, SDL_Surface * surface);
slouken@1895
    64
static void CopyShadow8888To565(_THIS, SDL_Surface * surface);
patmandin@991
    65
slouken@1895
    66
static void ConvertNull(_THIS, SDL_Surface * surface);
slouken@1895
    67
static void Convert565To555be(_THIS, SDL_Surface * surface);
slouken@1895
    68
static void Convert565To555le(_THIS, SDL_Surface * surface);
slouken@1895
    69
static void Convert565le(_THIS, SDL_Surface * surface);
slouken@1895
    70
static void ConvertBGRAToABGR(_THIS, SDL_Surface * surface);
patmandin@991
    71
slouken@1895
    72
static int InitNew(_THIS, SDL_Surface * current);
slouken@1895
    73
static int InitOld(_THIS, SDL_Surface * current);
patmandin@1000
    74
#endif
patmandin@989
    75
patmandin@989
    76
/*--- Public functions ---*/
patmandin@989
    77
slouken@1895
    78
int
slouken@1895
    79
SDL_AtariGL_Init(_THIS, SDL_Surface * current)
patmandin@989
    80
{
slouken@1361
    81
#if SDL_VIDEO_OPENGL
slouken@1895
    82
    if (gl_oldmesa) {
patmandin@2033
    83
        gl_active = InitOld(_this, current);
slouken@1895
    84
    } else {
patmandin@2033
    85
        gl_active = InitNew(_this, current);
slouken@1895
    86
    }
patmandin@991
    87
#endif
patmandin@989
    88
slouken@1895
    89
    return (gl_active);
patmandin@989
    90
}
patmandin@989
    91
slouken@1895
    92
void
slouken@1895
    93
SDL_AtariGL_Quit(_THIS, SDL_bool unload)
patmandin@989
    94
{
slouken@1361
    95
#if SDL_VIDEO_OPENGL
slouken@1895
    96
    if (gl_oldmesa) {
slouken@1895
    97
        /* Old mesa implementations */
patmandin@2033
    98
        if (_this->gl_data->OSMesaDestroyLDG) {
patmandin@2033
    99
            _this->gl_data->OSMesaDestroyLDG();
slouken@1895
   100
        }
slouken@1895
   101
        if (gl_shadow) {
slouken@1895
   102
            Mfree(gl_shadow);
slouken@1895
   103
            gl_shadow = NULL;
slouken@1895
   104
        }
slouken@1895
   105
    } else {
slouken@1895
   106
        /* New mesa implementation */
slouken@1895
   107
        if (gl_ctx) {
patmandin@2033
   108
            if (_this->gl_data->OSMesaDestroyContext) {
patmandin@2033
   109
                _this->gl_data->OSMesaDestroyContext(gl_ctx);
slouken@1895
   110
            }
slouken@1895
   111
            gl_ctx = NULL;
slouken@1895
   112
        }
slouken@1895
   113
    }
patmandin@991
   114
slouken@1895
   115
    if (unload) {
patmandin@2033
   116
        SDL_AtariGL_UnloadLibrary(_this);
slouken@1895
   117
    }
slouken@1361
   118
#endif /* SDL_VIDEO_OPENGL */
slouken@1895
   119
    gl_active = 0;
patmandin@989
   120
}
patmandin@989
   121
slouken@1895
   122
int
slouken@1895
   123
SDL_AtariGL_LoadLibrary(_THIS, const char *path)
patmandin@989
   124
{
slouken@1361
   125
#if SDL_VIDEO_OPENGL
patmandin@991
   126
slouken@1361
   127
#if SDL_VIDEO_OPENGL_OSMESA_DYNAMIC
slouken@1895
   128
    void *handle;
slouken@1895
   129
    SDL_bool cancel_load;
patmandin@991
   130
slouken@1895
   131
    if (gl_active) {
slouken@1895
   132
        SDL_SetError("OpenGL context already created");
slouken@1895
   133
        return -1;
slouken@1895
   134
    }
patmandin@991
   135
slouken@1895
   136
    /* Unload previous driver */
patmandin@2033
   137
    SDL_AtariGL_UnloadLibrary(_this);
patmandin@991
   138
slouken@1895
   139
    /* Load library given by path */
slouken@1895
   140
    handle = SDL_LoadObject(path);
slouken@1895
   141
    if (handle == NULL) {
slouken@1895
   142
        /* Try to load another one */
slouken@1895
   143
        path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
slouken@1895
   144
        if (path != NULL) {
slouken@1895
   145
            handle = SDL_LoadObject(path);
slouken@1895
   146
        }
patmandin@991
   147
slouken@1895
   148
        /* If it does not work, try some other */
slouken@1895
   149
        if (handle == NULL) {
slouken@1895
   150
            path = PATH_OSMESA_LDG;
slouken@1895
   151
            handle = SDL_LoadObject(path);
slouken@1895
   152
        }
patmandin@991
   153
slouken@1895
   154
        if (handle == NULL) {
slouken@1895
   155
            path = PATH_MESAGL_LDG;
slouken@1895
   156
            handle = SDL_LoadObject(path);
slouken@1895
   157
        }
patmandin@991
   158
slouken@1895
   159
        if (handle == NULL) {
slouken@1895
   160
            path = PATH_TINYGL_LDG;
slouken@1895
   161
            handle = SDL_LoadObject(path);
slouken@1895
   162
        }
slouken@1895
   163
    }
patmandin@991
   164
slouken@1895
   165
    if (handle == NULL) {
slouken@1895
   166
        SDL_SetError("Could not load OpenGL library");
slouken@1895
   167
        return -1;
slouken@1895
   168
    }
patmandin@991
   169
patmandin@2033
   170
    _this->gl_data->glGetIntegerv = SDL_LoadFunction(handle, "glGetIntegerv");
patmandin@2033
   171
    _this->gl_data->glFinish = SDL_LoadFunction(handle, "glFinish");
patmandin@2033
   172
    _this->gl_data->glFlush = SDL_LoadFunction(handle, "glFlush");
patmandin@993
   173
slouken@1895
   174
    cancel_load = SDL_FALSE;
patmandin@2033
   175
    if (_this->gl_data->glGetIntegerv == NULL) {
slouken@1895
   176
        cancel_load = SDL_TRUE;
slouken@1895
   177
    } else {
slouken@1895
   178
        /* We need either glFinish (OSMesa) or glFlush (TinyGL) */
patmandin@2033
   179
        if ((_this->gl_data->glFinish == NULL) &&
patmandin@2033
   180
            (_this->gl_data->glFlush == NULL)) {
slouken@1895
   181
            cancel_load = SDL_TRUE;
slouken@1895
   182
        }
slouken@1895
   183
    }
slouken@1895
   184
    if (cancel_load) {
slouken@1895
   185
        SDL_SetError("Could not retrieve OpenGL functions");
slouken@1895
   186
        SDL_UnloadObject(handle);
slouken@1895
   187
        /* Restore pointers to static library */
slouken@1895
   188
        SDL_AtariGL_InitPointers(this);
slouken@1895
   189
        return -1;
slouken@1895
   190
    }
patmandin@993
   191
slouken@1895
   192
    /* Load functions pointers (osmesa.ldg) */
patmandin@2033
   193
    _this->gl_data->OSMesaCreateContextExt =
slouken@1895
   194
        SDL_LoadFunction(handle, "OSMesaCreateContextExt");
patmandin@2033
   195
    _this->gl_data->OSMesaDestroyContext =
slouken@1895
   196
        SDL_LoadFunction(handle, "OSMesaDestroyContext");
patmandin@2033
   197
    _this->gl_data->OSMesaMakeCurrent =
slouken@1895
   198
        SDL_LoadFunction(handle, "OSMesaMakeCurrent");
patmandin@2033
   199
    _this->gl_data->OSMesaPixelStore =
slouken@1895
   200
        SDL_LoadFunction(handle, "OSMesaPixelStore");
patmandin@2033
   201
    _this->gl_data->OSMesaGetProcAddress =
slouken@1895
   202
        SDL_LoadFunction(handle, "OSMesaGetProcAddress");
patmandin@991
   203
slouken@1895
   204
    /* Load old functions pointers (mesa_gl.ldg, tiny_gl.ldg) */
patmandin@2033
   205
    _this->gl_data->OSMesaCreateLDG =
slouken@1895
   206
        SDL_LoadFunction(handle, "OSMesaCreateLDG");
patmandin@2033
   207
    _this->gl_data->OSMesaDestroyLDG =
slouken@1895
   208
        SDL_LoadFunction(handle, "OSMesaDestroyLDG");
patmandin@991
   209
slouken@1895
   210
    gl_oldmesa = 0;
patmandin@991
   211
patmandin@2033
   212
    if ((_this->gl_data->OSMesaCreateContextExt == NULL) ||
patmandin@2033
   213
        (_this->gl_data->OSMesaDestroyContext == NULL) ||
patmandin@2033
   214
        (_this->gl_data->OSMesaMakeCurrent == NULL) ||
patmandin@2033
   215
        (_this->gl_data->OSMesaPixelStore == NULL) ||
patmandin@2033
   216
        (_this->gl_data->OSMesaGetProcAddress == NULL)) {
slouken@1895
   217
        /* Hum, maybe old library ? */
patmandin@2033
   218
        if ((_this->gl_data->OSMesaCreateLDG == NULL) ||
patmandin@2033
   219
            (_this->gl_data->OSMesaDestroyLDG == NULL)) {
slouken@1895
   220
            SDL_SetError("Could not retrieve OSMesa functions");
slouken@1895
   221
            SDL_UnloadObject(handle);
slouken@1895
   222
            /* Restore pointers to static library */
patmandin@2033
   223
            SDL_AtariGL_InitPointers(_this);
slouken@1895
   224
            return -1;
slouken@1895
   225
        } else {
slouken@1895
   226
            gl_oldmesa = 1;
slouken@1895
   227
        }
slouken@1895
   228
    }
patmandin@991
   229
patmandin@2033
   230
    _this->gl_config.dll_handle = handle;
slouken@1895
   231
    if (path) {
patmandin@2033
   232
        SDL_strlcpy(_this->gl_config.driver_path, path,
patmandin@2033
   233
                    SDL_arraysize(_this->gl_config.driver_path));
slouken@1895
   234
    } else {
slouken@1895
   235
        *this->gl_config.driver_path = '\0';
slouken@1895
   236
    }
patmandin@991
   237
patmandin@991
   238
#endif
patmandin@2033
   239
    _this->gl_config.driver_loaded = 1;
patmandin@991
   240
slouken@1895
   241
    return 0;
patmandin@991
   242
#else
slouken@1895
   243
    return -1;
patmandin@989
   244
#endif
patmandin@989
   245
}
patmandin@989
   246
slouken@1895
   247
void *
slouken@1895
   248
SDL_AtariGL_GetProcAddress(_THIS, const char *proc)
patmandin@989
   249
{
slouken@1895
   250
    void *func = NULL;
slouken@1361
   251
#if SDL_VIDEO_OPENGL
patmandin@991
   252
patmandin@2033
   253
    if (_this->gl_config.dll_handle) {
slouken@1895
   254
        func = SDL_LoadFunction(this->gl_config.dll_handle, (void *) proc);
patmandin@2033
   255
    } else if (_this->gl_data->OSMesaGetProcAddress) {
patmandin@2033
   256
        func = _this->gl_data->OSMesaGetProcAddress(proc);
slouken@1895
   257
    }
patmandin@989
   258
#endif
slouken@1895
   259
    return func;
patmandin@989
   260
}
patmandin@989
   261
slouken@1895
   262
int
slouken@1895
   263
SDL_AtariGL_GetAttribute(_THIS, SDL_GLattr attrib, int *value)
patmandin@989
   264
{
slouken@1361
   265
#if SDL_VIDEO_OPENGL
slouken@1895
   266
    GLenum mesa_attrib;
slouken@1895
   267
    SDL_Surface *surface;
patmandin@989
   268
slouken@1895
   269
    if (!gl_active) {
slouken@1895
   270
        return -1;
slouken@1895
   271
    }
patmandin@989
   272
slouken@1895
   273
    switch (attrib) {
slouken@1895
   274
    case SDL_GL_RED_SIZE:
slouken@1895
   275
        mesa_attrib = GL_RED_BITS;
slouken@1895
   276
        break;
slouken@1895
   277
    case SDL_GL_GREEN_SIZE:
slouken@1895
   278
        mesa_attrib = GL_GREEN_BITS;
slouken@1895
   279
        break;
slouken@1895
   280
    case SDL_GL_BLUE_SIZE:
slouken@1895
   281
        mesa_attrib = GL_BLUE_BITS;
slouken@1895
   282
        break;
slouken@1895
   283
    case SDL_GL_ALPHA_SIZE:
slouken@1895
   284
        mesa_attrib = GL_ALPHA_BITS;
slouken@1895
   285
        break;
slouken@1895
   286
    case SDL_GL_DOUBLEBUFFER:
patmandin@2033
   287
        surface = _this->screen;
slouken@1895
   288
        *value = ((surface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF);
slouken@1895
   289
        return 0;
slouken@1895
   290
    case SDL_GL_DEPTH_SIZE:
slouken@1895
   291
        mesa_attrib = GL_DEPTH_BITS;
slouken@1895
   292
        break;
slouken@1895
   293
    case SDL_GL_STENCIL_SIZE:
slouken@1895
   294
        mesa_attrib = GL_STENCIL_BITS;
slouken@1895
   295
        break;
slouken@1895
   296
    case SDL_GL_ACCUM_RED_SIZE:
slouken@1895
   297
        mesa_attrib = GL_ACCUM_RED_BITS;
slouken@1895
   298
        break;
slouken@1895
   299
    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@1895
   300
        mesa_attrib = GL_ACCUM_GREEN_BITS;
slouken@1895
   301
        break;
slouken@1895
   302
    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@1895
   303
        mesa_attrib = GL_ACCUM_BLUE_BITS;
slouken@1895
   304
        break;
slouken@1895
   305
    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@1895
   306
        mesa_attrib = GL_ACCUM_ALPHA_BITS;
slouken@1895
   307
        break;
slouken@1895
   308
    default:
slouken@1895
   309
        return -1;
slouken@1895
   310
    }
patmandin@989
   311
patmandin@2033
   312
    _this->gl_data->glGetIntegerv(mesa_attrib, value);
slouken@1895
   313
    return 0;
patmandin@989
   314
#else
slouken@1895
   315
    return -1;
patmandin@989
   316
#endif
patmandin@989
   317
}
patmandin@989
   318
slouken@1895
   319
int
slouken@1895
   320
SDL_AtariGL_MakeCurrent(_THIS)
patmandin@989
   321
{
slouken@1361
   322
#if SDL_VIDEO_OPENGL
slouken@1895
   323
    SDL_Surface *surface;
slouken@1895
   324
    GLenum type;
patmandin@989
   325
slouken@1895
   326
    if (gl_oldmesa && gl_active) {
slouken@1895
   327
        return 0;
slouken@1895
   328
    }
patmandin@991
   329
patmandin@2033
   330
    if (_this->gl_config.dll_handle) {
patmandin@2033
   331
        if ((_this->gl_data->OSMesaMakeCurrent == NULL) ||
patmandin@2033
   332
            (_this->gl_data->OSMesaPixelStore == NULL)) {
slouken@1895
   333
            return -1;
slouken@1895
   334
        }
slouken@1895
   335
    }
patmandin@991
   336
slouken@1895
   337
    if (!gl_active) {
slouken@1895
   338
        SDL_SetError("Invalid OpenGL context");
slouken@1895
   339
        return -1;
slouken@1895
   340
    }
slouken@1895
   341
patmandin@2033
   342
    surface = _this->screen;
patmandin@989
   343
slouken@1895
   344
    if ((surface->format->BitsPerPixel == 15)
slouken@1895
   345
        || (surface->format->BitsPerPixel == 16)) {
slouken@1895
   346
        type = GL_UNSIGNED_SHORT_5_6_5;
slouken@1895
   347
    } else {
slouken@1895
   348
        type = GL_UNSIGNED_BYTE;
slouken@1895
   349
    }
patmandin@989
   350
slouken@1895
   351
    if (!
patmandin@2033
   352
        (_this->gl_data->
slouken@1895
   353
         OSMesaMakeCurrent(gl_ctx, surface->pixels, type, surface->w,
slouken@1895
   354
                           surface->h))) {
slouken@1895
   355
        SDL_SetError("Can not make OpenGL context current");
slouken@1895
   356
        return -1;
slouken@1895
   357
    }
patmandin@989
   358
slouken@1895
   359
    /* OSMesa draws upside down */
patmandin@2033
   360
    _this->gl_data->OSMesaPixelStore(OSMESA_Y_UP, 0);
patmandin@989
   361
slouken@1895
   362
    return 0;
patmandin@989
   363
#else
slouken@1895
   364
    return -1;
patmandin@989
   365
#endif
patmandin@989
   366
}
patmandin@989
   367
slouken@1895
   368
void
slouken@1895
   369
SDL_AtariGL_SwapBuffers(_THIS)
patmandin@989
   370
{
slouken@1361
   371
#if SDL_VIDEO_OPENGL
slouken@1895
   372
    if (gl_active) {
patmandin@2033
   373
        if (_this->gl_config.dll_handle) {
patmandin@2033
   374
            if (_this->gl_data->glFinish) {
patmandin@2033
   375
                _this->gl_data->glFinish();
patmandin@2033
   376
            } else if (_this->gl_data->glFlush) {
patmandin@2033
   377
                _this->gl_data->glFlush();
slouken@1895
   378
            }
slouken@1895
   379
        } else {
patmandin@2033
   380
            _this->gl_data->glFinish();
slouken@1895
   381
        }
patmandin@2033
   382
        gl_copyshadow(_this, _this->screen);
patmandin@2033
   383
        gl_convert(_this, _this->screen);
slouken@1895
   384
    }
patmandin@991
   385
#endif
patmandin@991
   386
}
patmandin@989
   387
slouken@1895
   388
void
slouken@1895
   389
SDL_AtariGL_InitPointers(_THIS)
patmandin@991
   390
{
slouken@1361
   391
#if SDL_VIDEO_OPENGL
patmandin@2033
   392
    _this->gl_data->OSMesaCreateContextExt = OSMesaCreateContextExt;
patmandin@2033
   393
    _this->gl_data->OSMesaDestroyContext = OSMesaDestroyContext;
patmandin@2033
   394
    _this->gl_data->OSMesaMakeCurrent = OSMesaMakeCurrent;
patmandin@2033
   395
    _this->gl_data->OSMesaPixelStore = OSMesaPixelStore;
patmandin@2033
   396
    _this->gl_data->OSMesaGetProcAddress = OSMesaGetProcAddress;
patmandin@993
   397
patmandin@2033
   398
    _this->gl_data->glGetIntegerv = glGetIntegerv;
patmandin@2033
   399
    _this->gl_data->glFinish = glFinish;
patmandin@2033
   400
    _this->gl_data->glFlush = glFlush;
patmandin@993
   401
patmandin@2033
   402
    _this->gl_data->OSMesaCreateLDG = NULL;
patmandin@2033
   403
    _this->gl_data->OSMesaDestroyLDG = NULL;
patmandin@989
   404
#endif
patmandin@989
   405
}
patmandin@989
   406
patmandin@989
   407
/*--- Private functions ---*/
patmandin@989
   408
slouken@1895
   409
static void
slouken@1895
   410
SDL_AtariGL_UnloadLibrary(_THIS)
patmandin@991
   411
{
slouken@1361
   412
#if SDL_VIDEO_OPENGL
patmandin@2033
   413
    if (_this->gl_config.dll_handle) {
patmandin@2033
   414
        SDL_UnloadObject(_this->gl_config.dll_handle);
patmandin@2033
   415
        _this->gl_config.dll_handle = NULL;
patmandin@991
   416
slouken@1895
   417
        /* Restore pointers to static library */
patmandin@2033
   418
        SDL_AtariGL_InitPointers(_this);
slouken@1895
   419
    }
patmandin@991
   420
#endif
patmandin@991
   421
}
patmandin@991
   422
patmandin@991
   423
/*--- Creation of an OpenGL context using new/old functions ---*/
patmandin@991
   424
slouken@1361
   425
#if SDL_VIDEO_OPENGL
slouken@1895
   426
static int
slouken@1895
   427
InitNew(_THIS, SDL_Surface * current)
patmandin@991
   428
{
slouken@1895
   429
    GLenum osmesa_format;
slouken@1895
   430
    SDL_PixelFormat *pixel_format;
slouken@1895
   431
    Uint32 redmask;
slouken@1895
   432
    int recreatecontext;
slouken@1895
   433
    GLint newaccumsize;
patmandin@991
   434
patmandin@2033
   435
    if (_this->gl_config.dll_handle) {
patmandin@2033
   436
        if (_this->gl_data->OSMesaCreateContextExt == NULL) {
slouken@1895
   437
            return 0;
slouken@1895
   438
        }
slouken@1895
   439
    }
patmandin@991
   440
slouken@1895
   441
    /* Init OpenGL context using OSMesa */
slouken@1895
   442
    gl_convert = ConvertNull;
slouken@1895
   443
    gl_copyshadow = CopyShadowNull;
slouken@1895
   444
    gl_upsidedown = SDL_FALSE;
patmandin@991
   445
slouken@1895
   446
    pixel_format = current->format;
slouken@1895
   447
    redmask = pixel_format->Rmask;
slouken@1895
   448
    switch (pixel_format->BitsPerPixel) {
slouken@1895
   449
    case 15:
slouken@1895
   450
        /* 1555, big and little endian, unsupported */
slouken@1895
   451
        gl_pixelsize = 2;
slouken@1895
   452
        osmesa_format = OSMESA_RGB_565;
slouken@1895
   453
        if (redmask == 31 << 10) {
slouken@1895
   454
            gl_convert = Convert565To555be;
slouken@1895
   455
        } else {
slouken@1895
   456
            gl_convert = Convert565To555le;
slouken@1895
   457
        }
slouken@1895
   458
        break;
slouken@1895
   459
    case 16:
slouken@1895
   460
        gl_pixelsize = 2;
slouken@1895
   461
        if (redmask == 31 << 11) {
slouken@1895
   462
            osmesa_format = OSMESA_RGB_565;
slouken@1895
   463
        } else {
slouken@1895
   464
            /* 565, little endian, unsupported */
slouken@1895
   465
            osmesa_format = OSMESA_RGB_565;
slouken@1895
   466
            gl_convert = Convert565le;
slouken@1895
   467
        }
slouken@1895
   468
        break;
slouken@1895
   469
    case 24:
slouken@1895
   470
        gl_pixelsize = 3;
slouken@1895
   471
        if (redmask == 255 << 16) {
slouken@1895
   472
            osmesa_format = OSMESA_RGB;
slouken@1895
   473
        } else {
slouken@1895
   474
            osmesa_format = OSMESA_BGR;
slouken@1895
   475
        }
slouken@1895
   476
        break;
slouken@1895
   477
    case 32:
slouken@1895
   478
        gl_pixelsize = 4;
slouken@1895
   479
        if (redmask == 255 << 16) {
slouken@1895
   480
            osmesa_format = OSMESA_ARGB;
slouken@1895
   481
        } else if (redmask == 255 << 8) {
slouken@1895
   482
            osmesa_format = OSMESA_BGRA;
slouken@1895
   483
        } else if (redmask == 255 << 24) {
slouken@1895
   484
            osmesa_format = OSMESA_RGBA;
slouken@1895
   485
        } else {
slouken@1895
   486
            /* ABGR format unsupported */
slouken@1895
   487
            osmesa_format = OSMESA_BGRA;
slouken@1895
   488
            gl_convert = ConvertBGRAToABGR;
slouken@1895
   489
        }
slouken@1895
   490
        break;
slouken@1895
   491
    default:
slouken@1895
   492
        gl_pixelsize = 1;
slouken@1895
   493
        osmesa_format = OSMESA_COLOR_INDEX;
slouken@1895
   494
        break;
slouken@1895
   495
    }
patmandin@991
   496
slouken@1895
   497
    /* Try to keep current context if possible */
slouken@1895
   498
    newaccumsize =
patmandin@2033
   499
        _this->gl_config.accum_red_size +
patmandin@2033
   500
        _this->gl_config.accum_green_size +
patmandin@2033
   501
        _this->gl_config.accum_blue_size + _this->gl_config.accum_alpha_size;
slouken@1895
   502
    recreatecontext = 1;
slouken@1895
   503
    if (gl_ctx &&
slouken@1895
   504
        (gl_curformat == osmesa_format) &&
patmandin@2033
   505
        (gl_curdepth == _this->gl_config.depth_size) &&
patmandin@2033
   506
        (gl_curstencil == _this->gl_config.stencil_size) &&
slouken@1895
   507
        (gl_curaccum == newaccumsize)) {
slouken@1895
   508
        recreatecontext = 0;
slouken@1895
   509
    }
slouken@1895
   510
    if (recreatecontext) {
slouken@1895
   511
        SDL_AtariGL_Quit(this, SDL_FALSE);
patmandin@992
   512
slouken@1895
   513
        gl_ctx =
patmandin@2033
   514
            _this->gl_data->OSMesaCreateContextExt(osmesa_format,
slouken@2043
   515
                                                   _this->gl_config.
slouken@2043
   516
                                                   depth_size,
slouken@2043
   517
                                                   _this->gl_config.
slouken@2043
   518
                                                   stencil_size,
slouken@2043
   519
                                                   newaccumsize, NULL);
patmandin@992
   520
slouken@1895
   521
        if (gl_ctx) {
slouken@1895
   522
            gl_curformat = osmesa_format;
patmandin@2033
   523
            gl_curdepth = _this->gl_config.depth_size;
patmandin@2033
   524
            gl_curstencil = _this->gl_config.stencil_size;
slouken@1895
   525
            gl_curaccum = newaccumsize;
slouken@1895
   526
        } else {
slouken@1895
   527
            gl_curformat = 0;
slouken@1895
   528
            gl_curdepth = 0;
slouken@1895
   529
            gl_curstencil = 0;
slouken@1895
   530
            gl_curaccum = 0;
slouken@1895
   531
        }
slouken@1895
   532
    }
patmandin@991
   533
slouken@1895
   534
    return (gl_ctx != NULL);
patmandin@991
   535
}
patmandin@991
   536
patmandin@1000
   537
slouken@1895
   538
static int
slouken@1895
   539
InitOld(_THIS, SDL_Surface * current)
patmandin@991
   540
{
slouken@1895
   541
    GLenum osmesa_format;
slouken@1895
   542
    SDL_PixelFormat *pixel_format;
slouken@1895
   543
    Uint32 redmask;
slouken@1895
   544
    int recreatecontext, tinygl_present;
patmandin@991
   545
patmandin@2033
   546
    if (_this->gl_config.dll_handle) {
patmandin@2033
   547
        if (_this->gl_data->OSMesaCreateLDG == NULL) {
slouken@1895
   548
            return 0;
slouken@1895
   549
        }
slouken@1895
   550
    }
patmandin@1000
   551
slouken@1895
   552
    /* TinyGL only supports VDI_RGB (OSMESA_RGB) */
slouken@1895
   553
    tinygl_present = 0;
patmandin@2033
   554
    if (_this->gl_config.dll_handle) {
patmandin@2033
   555
        if (_this->gl_data->glFinish == NULL) {
slouken@1895
   556
            tinygl_present = 1;
slouken@1895
   557
        }
slouken@1895
   558
    }
slouken@1895
   559
slouken@1895
   560
    /* Init OpenGL context using OSMesa */
slouken@1895
   561
    gl_convert = ConvertNull;
slouken@1895
   562
    gl_copyshadow = CopyShadowNull;
slouken@1895
   563
    gl_upsidedown = SDL_FALSE;
patmandin@991
   564
slouken@1895
   565
    pixel_format = current->format;
slouken@1895
   566
    redmask = pixel_format->Rmask;
slouken@1895
   567
    switch (pixel_format->BitsPerPixel) {
slouken@1895
   568
    case 15:
slouken@1895
   569
        /* 15 bits unsupported */
slouken@1895
   570
        if (tinygl_present) {
slouken@1895
   571
            gl_pixelsize = 3;
slouken@1895
   572
            osmesa_format = VDI_RGB;
slouken@1895
   573
            if (redmask == 31 << 10) {
slouken@1895
   574
                gl_copyshadow = CopyShadowRGBTo555;
slouken@1895
   575
            } else {
slouken@1895
   576
                gl_copyshadow = CopyShadowRGBTo565;
slouken@1895
   577
                gl_convert = Convert565To555le;
slouken@1895
   578
            }
slouken@1895
   579
        } else {
slouken@1895
   580
            gl_pixelsize = 4;
slouken@1895
   581
            gl_upsidedown = SDL_TRUE;
slouken@1895
   582
            osmesa_format = OSMESA_ARGB;
slouken@1895
   583
            if (redmask == 31 << 10) {
slouken@1895
   584
                gl_copyshadow = CopyShadow8888To555;
slouken@1895
   585
            } else {
slouken@1895
   586
                gl_copyshadow = CopyShadow8888To565;
slouken@1895
   587
                gl_convert = Convert565To555le;
slouken@1895
   588
            }
slouken@1895
   589
        }
slouken@1895
   590
        break;
slouken@1895
   591
    case 16:
slouken@1895
   592
        /* 16 bits unsupported */
slouken@1895
   593
        if (tinygl_present) {
slouken@1895
   594
            gl_pixelsize = 3;
slouken@1895
   595
            osmesa_format = VDI_RGB;
slouken@1895
   596
            gl_copyshadow = CopyShadowRGBTo565;
slouken@1895
   597
            if (redmask != 31 << 11) {
slouken@1895
   598
                /* 565, little endian, unsupported */
slouken@1895
   599
                gl_convert = Convert565le;
slouken@1895
   600
            }
slouken@1895
   601
        } else {
slouken@1895
   602
            gl_pixelsize = 4;
slouken@1895
   603
            gl_upsidedown = SDL_TRUE;
slouken@1895
   604
            osmesa_format = OSMESA_ARGB;
slouken@1895
   605
            gl_copyshadow = CopyShadow8888To565;
slouken@1895
   606
            if (redmask != 31 << 11) {
slouken@1895
   607
                /* 565, little endian, unsupported */
slouken@1895
   608
                gl_convert = Convert565le;
slouken@1895
   609
            }
slouken@1895
   610
        }
slouken@1895
   611
        break;
slouken@1895
   612
    case 24:
slouken@1895
   613
        gl_pixelsize = 3;
slouken@1895
   614
        if (tinygl_present) {
slouken@1895
   615
            osmesa_format = VDI_RGB;
slouken@1895
   616
            gl_copyshadow = CopyShadowDirect;
slouken@1895
   617
            if (redmask != 255 << 16) {
slouken@1895
   618
                gl_copyshadow = CopyShadowRGBSwap;
slouken@1895
   619
            }
slouken@1895
   620
        } else {
slouken@1895
   621
            gl_copyshadow = CopyShadowDirect;
slouken@1895
   622
            gl_upsidedown = SDL_TRUE;
slouken@1895
   623
            if (redmask == 255 << 16) {
slouken@1895
   624
                osmesa_format = OSMESA_RGB;
slouken@1895
   625
            } else {
slouken@1895
   626
                osmesa_format = OSMESA_BGR;
slouken@1895
   627
            }
slouken@1895
   628
        }
slouken@1895
   629
        break;
slouken@1895
   630
    case 32:
slouken@1895
   631
        if (tinygl_present) {
slouken@1895
   632
            gl_pixelsize = 3;
slouken@1895
   633
            osmesa_format = VDI_RGB;
slouken@1895
   634
            gl_copyshadow = CopyShadowRGBToARGB;
slouken@1895
   635
            if (redmask == 255) {
slouken@1895
   636
                gl_convert = CopyShadowRGBToABGR;
slouken@1895
   637
            } else if (redmask == 255 << 8) {
slouken@1895
   638
                gl_convert = CopyShadowRGBToBGRA;
slouken@1895
   639
            } else if (redmask == 255 << 24) {
slouken@1895
   640
                gl_convert = CopyShadowRGBToRGBA;
slouken@1895
   641
            }
slouken@1895
   642
        } else {
slouken@1895
   643
            gl_pixelsize = 4;
slouken@1895
   644
            gl_upsidedown = SDL_TRUE;
slouken@1895
   645
            gl_copyshadow = CopyShadowDirect;
slouken@1895
   646
            if (redmask == 255 << 16) {
slouken@1895
   647
                osmesa_format = OSMESA_ARGB;
slouken@1895
   648
            } else if (redmask == 255 << 8) {
slouken@1895
   649
                osmesa_format = OSMESA_BGRA;
slouken@1895
   650
            } else if (redmask == 255 << 24) {
slouken@1895
   651
                osmesa_format = OSMESA_RGBA;
slouken@1895
   652
            } else {
slouken@1895
   653
                /* ABGR format unsupported */
slouken@1895
   654
                osmesa_format = OSMESA_BGRA;
slouken@1895
   655
                gl_convert = ConvertBGRAToABGR;
slouken@1895
   656
            }
slouken@1895
   657
        }
slouken@1895
   658
        break;
slouken@1895
   659
    default:
slouken@1895
   660
        if (tinygl_present) {
patmandin@2033
   661
            SDL_AtariGL_Quit(_this, SDL_FALSE);
slouken@1895
   662
            return 0;
slouken@1895
   663
        }
slouken@1895
   664
        gl_pixelsize = 1;
slouken@1895
   665
        gl_copyshadow = CopyShadowDirect;
slouken@1895
   666
        osmesa_format = OSMESA_COLOR_INDEX;
slouken@1895
   667
        break;
slouken@1895
   668
    }
patmandin@991
   669
slouken@1895
   670
    /* Try to keep current context if possible */
slouken@1895
   671
    recreatecontext = 1;
slouken@1895
   672
    if (gl_shadow &&
slouken@1895
   673
        (gl_curformat == osmesa_format) &&
slouken@1895
   674
        (gl_curwidth == current->w) && (gl_curheight == current->h)) {
slouken@1895
   675
        recreatecontext = 0;
slouken@1895
   676
    }
slouken@1895
   677
    if (recreatecontext) {
patmandin@2033
   678
        SDL_AtariGL_Quit(_this, SDL_FALSE);
patmandin@992
   679
slouken@1895
   680
        gl_shadow =
patmandin@2033
   681
            _this->gl_data->OSMesaCreateLDG(osmesa_format, GL_UNSIGNED_BYTE,
slouken@2043
   682
                                            current->w, current->h);
patmandin@992
   683
slouken@1895
   684
        if (gl_shadow) {
slouken@1895
   685
            gl_curformat = osmesa_format;
slouken@1895
   686
            gl_curwidth = current->w;
slouken@1895
   687
            gl_curheight = current->h;
slouken@1895
   688
        } else {
slouken@1895
   689
            gl_curformat = 0;
slouken@1895
   690
            gl_curwidth = 0;
slouken@1895
   691
            gl_curheight = 0;
slouken@1895
   692
        }
slouken@1895
   693
    }
patmandin@991
   694
slouken@1895
   695
    return (gl_shadow != NULL);
patmandin@991
   696
}
patmandin@991
   697
patmandin@991
   698
/*--- Conversions routines from shadow buffer to the screen ---*/
patmandin@991
   699
slouken@1895
   700
static void
slouken@1895
   701
CopyShadowNull(_THIS, SDL_Surface * surface)
patmandin@989
   702
{
patmandin@989
   703
}
patmandin@989
   704
slouken@1895
   705
static void
slouken@1895
   706
CopyShadowDirect(_THIS, SDL_Surface * surface)
patmandin@991
   707
{
slouken@1895
   708
    int y, srcpitch, dstpitch;
slouken@1895
   709
    Uint8 *srcline, *dstline;
patmandin@991
   710
slouken@1895
   711
    srcline = gl_shadow;
slouken@1895
   712
    srcpitch = surface->w * gl_pixelsize;
slouken@1895
   713
    dstline = surface->pixels;
slouken@1895
   714
    dstpitch = surface->pitch;
slouken@1895
   715
    if (gl_upsidedown) {
slouken@1895
   716
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
   717
        srcpitch = -srcpitch;
slouken@1895
   718
    }
patmandin@991
   719
slouken@1895
   720
    for (y = 0; y < surface->h; y++) {
slouken@1895
   721
        SDL_memcpy(dstline, srcline, srcpitch);
patmandin@991
   722
slouken@1895
   723
        srcline += srcpitch;
slouken@1895
   724
        dstline += dstpitch;
slouken@1895
   725
    }
patmandin@991
   726
}
patmandin@991
   727
slouken@1895
   728
static void
slouken@1895
   729
CopyShadowRGBTo555(_THIS, SDL_Surface * surface)
patmandin@1000
   730
{
slouken@1895
   731
    int x, y, srcpitch, dstpitch;
slouken@1895
   732
    Uint16 *dstline, *dstcol;
slouken@1895
   733
    Uint8 *srcline, *srccol;
patmandin@1000
   734
slouken@1895
   735
    srcline = (Uint8 *) gl_shadow;
slouken@1895
   736
    srcpitch = surface->w * gl_pixelsize;
slouken@1895
   737
    dstline = surface->pixels;
slouken@1895
   738
    dstpitch = surface->pitch >> 1;
slouken@1895
   739
    if (gl_upsidedown) {
slouken@1895
   740
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
   741
        srcpitch = -srcpitch;
slouken@1895
   742
    }
patmandin@1000
   743
slouken@1895
   744
    for (y = 0; y < surface->h; y++) {
slouken@1895
   745
        srccol = srcline;
slouken@1895
   746
        dstcol = dstline;
slouken@1895
   747
        for (x = 0; x < surface->w; x++) {
slouken@1895
   748
            Uint16 dstcolor;
patmandin@1000
   749
slouken@1895
   750
            dstcolor = ((*srccol++) << 7) & (31 << 10);
slouken@1895
   751
            dstcolor |= ((*srccol++) << 2) & (31 << 5);
slouken@1895
   752
            dstcolor |= ((*srccol++) >> 3) & 31;
slouken@1895
   753
            *dstcol++ = dstcolor;
slouken@1895
   754
        }
slouken@1895
   755
slouken@1895
   756
        srcline += srcpitch;
slouken@1895
   757
        dstline += dstpitch;
slouken@1895
   758
    }
patmandin@1000
   759
}
patmandin@1000
   760
slouken@1895
   761
static void
slouken@1895
   762
CopyShadowRGBTo565(_THIS, SDL_Surface * surface)
patmandin@1000
   763
{
slouken@1895
   764
    int x, y, srcpitch, dstpitch;
slouken@1895
   765
    Uint16 *dstline, *dstcol;
slouken@1895
   766
    Uint8 *srcline, *srccol;
patmandin@1000
   767
slouken@1895
   768
    srcline = (Uint8 *) gl_shadow;
slouken@1895
   769
    srcpitch = surface->w * gl_pixelsize;
slouken@1895
   770
    dstline = surface->pixels;
slouken@1895
   771
    dstpitch = surface->pitch >> 1;
slouken@1895
   772
    if (gl_upsidedown) {
slouken@1895
   773
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
   774
        srcpitch = -srcpitch;
slouken@1895
   775
    }
patmandin@1000
   776
slouken@1895
   777
    for (y = 0; y < surface->h; y++) {
slouken@1895
   778
        srccol = srcline;
slouken@1895
   779
        dstcol = dstline;
slouken@1895
   780
slouken@1895
   781
        for (x = 0; x < surface->w; x++) {
slouken@1895
   782
            Uint16 dstcolor;
patmandin@1000
   783
slouken@1895
   784
            dstcolor = ((*srccol++) << 8) & (31 << 11);
slouken@1895
   785
            dstcolor |= ((*srccol++) << 3) & (63 << 5);
slouken@1895
   786
            dstcolor |= ((*srccol++) >> 3) & 31;
slouken@1895
   787
            *dstcol++ = dstcolor;
slouken@1895
   788
        }
patmandin@1000
   789
slouken@1895
   790
        srcline += srcpitch;
slouken@1895
   791
        dstline += dstpitch;
slouken@1895
   792
    }
patmandin@1000
   793
}
patmandin@1000
   794
slouken@1895
   795
static void
slouken@1895
   796
CopyShadowRGBSwap(_THIS, SDL_Surface * surface)
patmandin@1000
   797
{
slouken@1895
   798
    int x, y, srcpitch, dstpitch;
slouken@1895
   799
    Uint8 *dstline, *dstcol;
slouken@1895
   800
    Uint8 *srcline, *srccol;
patmandin@1000
   801
slouken@1895
   802
    srcline = (Uint8 *) gl_shadow;
slouken@1895
   803
    srcpitch = surface->w * gl_pixelsize;
slouken@1895
   804
    dstline = surface->pixels;
slouken@1895
   805
    dstpitch = surface->pitch;
slouken@1895
   806
    if (gl_upsidedown) {
slouken@1895
   807
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
   808
        srcpitch = -srcpitch;
slouken@1895
   809
    }
patmandin@1000
   810
slouken@1895
   811
    for (y = 0; y < surface->h; y++) {
slouken@1895
   812
        srccol = srcline;
slouken@1895
   813
        dstcol = dstline;
patmandin@1000
   814
slouken@1895
   815
        for (x = 0; x < surface->w; x++) {
slouken@1895
   816
            *dstcol++ = srccol[2];
slouken@1895
   817
            *dstcol++ = srccol[1];
slouken@1895
   818
            *dstcol++ = srccol[0];
slouken@1895
   819
            srccol += 3;
slouken@1895
   820
        }
slouken@1895
   821
slouken@1895
   822
        srcline += srcpitch;
slouken@1895
   823
        dstline += dstpitch;
slouken@1895
   824
    }
patmandin@1000
   825
}
patmandin@1000
   826
slouken@1895
   827
static void
slouken@1895
   828
CopyShadowRGBToARGB(_THIS, SDL_Surface * surface)
patmandin@1000
   829
{
slouken@1895
   830
    int x, y, srcpitch, dstpitch;
slouken@1895
   831
    Uint32 *dstline, *dstcol;
slouken@1895
   832
    Uint8 *srcline, *srccol;
patmandin@1000
   833
slouken@1895
   834
    srcline = (Uint8 *) gl_shadow;
slouken@1895
   835
    srcpitch = surface->w * gl_pixelsize;
slouken@1895
   836
    dstline = surface->pixels;
slouken@1895
   837
    dstpitch = surface->pitch >> 2;
slouken@1895
   838
    if (gl_upsidedown) {
slouken@1895
   839
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
   840
        srcpitch = -srcpitch;
slouken@1895
   841
    }
patmandin@1000
   842
slouken@1895
   843
    for (y = 0; y < surface->h; y++) {
slouken@1895
   844
        srccol = srcline;
slouken@1895
   845
        dstcol = dstline;
patmandin@1000
   846
slouken@1895
   847
        for (x = 0; x < surface->w; x++) {
slouken@1895
   848
            Uint32 dstcolor;
patmandin@1000
   849
slouken@1895
   850
            dstcolor = (*srccol++) << 16;
slouken@1895
   851
            dstcolor |= (*srccol++) << 8;
slouken@1895
   852
            dstcolor |= *srccol++;
patmandin@1000
   853
slouken@1895
   854
            *dstcol++ = dstcolor;
slouken@1895
   855
        }
patmandin@1000
   856
slouken@1895
   857
        srcline += srcpitch;
slouken@1895
   858
        dstline += dstpitch;
slouken@1895
   859
    }
patmandin@1000
   860
}
patmandin@1000
   861
slouken@1895
   862
static void
slouken@1895
   863
CopyShadowRGBToABGR(_THIS, SDL_Surface * surface)
patmandin@1000
   864
{
slouken@1895
   865
    int x, y, srcpitch, dstpitch;
slouken@1895
   866
    Uint32 *dstline, *dstcol;
slouken@1895
   867
    Uint8 *srcline, *srccol;
patmandin@1000
   868
slouken@1895
   869
    srcline = (Uint8 *) gl_shadow;
slouken@1895
   870
    srcpitch = surface->w * gl_pixelsize;
slouken@1895
   871
    dstline = surface->pixels;
slouken@1895
   872
    dstpitch = surface->pitch >> 2;
slouken@1895
   873
    if (gl_upsidedown) {
slouken@1895
   874
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
   875
        srcpitch = -srcpitch;
slouken@1895
   876
    }
patmandin@1000
   877
slouken@1895
   878
    for (y = 0; y < surface->h; y++) {
slouken@1895
   879
        srccol = srcline;
slouken@1895
   880
        dstcol = dstline;
patmandin@1000
   881
slouken@1895
   882
        for (x = 0; x < surface->w; x++) {
slouken@1895
   883
            Uint32 dstcolor;
patmandin@1000
   884
slouken@1895
   885
            dstcolor = *srccol++;
slouken@1895
   886
            dstcolor |= (*srccol++) << 8;
slouken@1895
   887
            dstcolor |= (*srccol++) << 16;
patmandin@1000
   888
slouken@1895
   889
            *dstcol++ = dstcolor;
slouken@1895
   890
        }
patmandin@1000
   891
slouken@1895
   892
        srcline += srcpitch;
slouken@1895
   893
        dstline += dstpitch;
slouken@1895
   894
    }
patmandin@1000
   895
}
patmandin@1000
   896
slouken@1895
   897
static void
slouken@1895
   898
CopyShadowRGBToBGRA(_THIS, SDL_Surface * surface)
patmandin@1000
   899
{
slouken@1895
   900
    int x, y, srcpitch, dstpitch;
slouken@1895
   901
    Uint32 *dstline, *dstcol;
slouken@1895
   902
    Uint8 *srcline, *srccol;
patmandin@1000
   903
slouken@1895
   904
    srcline = (Uint8 *) gl_shadow;
slouken@1895
   905
    srcpitch = surface->w * gl_pixelsize;
slouken@1895
   906
    dstline = surface->pixels;
slouken@1895
   907
    dstpitch = surface->pitch >> 2;
slouken@1895
   908
    if (gl_upsidedown) {
slouken@1895
   909
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
   910
        srcpitch = -srcpitch;
slouken@1895
   911
    }
patmandin@1000
   912
slouken@1895
   913
    for (y = 0; y < surface->h; y++) {
slouken@1895
   914
        srccol = srcline;
slouken@1895
   915
        dstcol = dstline;
patmandin@1000
   916
slouken@1895
   917
        for (x = 0; x < surface->w; x++) {
slouken@1895
   918
            Uint32 dstcolor;
patmandin@1000
   919
slouken@1895
   920
            dstcolor = (*srccol++) << 8;
slouken@1895
   921
            dstcolor |= (*srccol++) << 16;
slouken@1895
   922
            dstcolor |= (*srccol++) << 24;
patmandin@1000
   923
slouken@1895
   924
            *dstcol++ = dstcolor;
slouken@1895
   925
        }
patmandin@1000
   926
slouken@1895
   927
        srcline += srcpitch;
slouken@1895
   928
        dstline += dstpitch;
slouken@1895
   929
    }
patmandin@1000
   930
}
patmandin@1000
   931
slouken@1895
   932
static void
slouken@1895
   933
CopyShadowRGBToRGBA(_THIS, SDL_Surface * surface)
patmandin@1000
   934
{
slouken@1895
   935
    int x, y, srcpitch, dstpitch;
slouken@1895
   936
    Uint32 *dstline, *dstcol;
slouken@1895
   937
    Uint8 *srcline, *srccol;
patmandin@1000
   938
slouken@1895
   939
    srcline = (Uint8 *) gl_shadow;
slouken@1895
   940
    srcpitch = surface->w * gl_pixelsize;
slouken@1895
   941
    dstline = surface->pixels;
slouken@1895
   942
    dstpitch = surface->pitch >> 2;
slouken@1895
   943
    if (gl_upsidedown) {
slouken@1895
   944
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
   945
        srcpitch = -srcpitch;
slouken@1895
   946
    }
patmandin@1000
   947
slouken@1895
   948
    for (y = 0; y < surface->h; y++) {
slouken@1895
   949
        srccol = srcline;
slouken@1895
   950
        dstcol = dstline;
patmandin@1000
   951
slouken@1895
   952
        for (x = 0; x < surface->w; x++) {
slouken@1895
   953
            Uint32 dstcolor;
patmandin@1000
   954
slouken@1895
   955
            dstcolor = (*srccol++) << 24;
slouken@1895
   956
            dstcolor |= (*srccol++) << 16;
slouken@1895
   957
            dstcolor |= (*srccol++) << 8;
patmandin@1000
   958
slouken@1895
   959
            *dstcol++ = dstcolor;
slouken@1895
   960
        }
patmandin@1000
   961
slouken@1895
   962
        srcline += srcpitch;
slouken@1895
   963
        dstline += dstpitch;
slouken@1895
   964
    }
patmandin@1000
   965
}
patmandin@1000
   966
slouken@1895
   967
static void
slouken@1895
   968
CopyShadow8888To555(_THIS, SDL_Surface * surface)
patmandin@991
   969
{
slouken@1895
   970
    int x, y, srcpitch, dstpitch;
slouken@1895
   971
    Uint16 *dstline, *dstcol;
slouken@1895
   972
    Uint32 *srcline, *srccol;
patmandin@991
   973
slouken@1895
   974
    srcline = (Uint32 *) gl_shadow;
slouken@1895
   975
    srcpitch = (surface->w * gl_pixelsize) >> 2;
slouken@1895
   976
    dstline = surface->pixels;
slouken@1895
   977
    dstpitch = surface->pitch >> 1;
slouken@1895
   978
    if (gl_upsidedown) {
slouken@1895
   979
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
   980
        srcpitch = -srcpitch;
slouken@1895
   981
    }
patmandin@991
   982
slouken@1895
   983
    for (y = 0; y < surface->h; y++) {
slouken@1895
   984
        srccol = srcline;
slouken@1895
   985
        dstcol = dstline;
slouken@1895
   986
        for (x = 0; x < surface->w; x++) {
slouken@1895
   987
            Uint32 srccolor;
slouken@1895
   988
            Uint16 dstcolor;
patmandin@991
   989
slouken@1895
   990
            srccolor = *srccol++;
slouken@1895
   991
            dstcolor = (srccolor >> 9) & (31 << 10);
slouken@1895
   992
            dstcolor |= (srccolor >> 6) & (31 << 5);
slouken@1895
   993
            dstcolor |= (srccolor >> 3) & 31;
slouken@1895
   994
            *dstcol++ = dstcolor;
slouken@1895
   995
        }
slouken@1895
   996
slouken@1895
   997
        srcline += srcpitch;
slouken@1895
   998
        dstline += dstpitch;
slouken@1895
   999
    }
patmandin@991
  1000
}
patmandin@991
  1001
slouken@1895
  1002
static void
slouken@1895
  1003
CopyShadow8888To565(_THIS, SDL_Surface * surface)
patmandin@991
  1004
{
slouken@1895
  1005
    int x, y, srcpitch, dstpitch;
slouken@1895
  1006
    Uint16 *dstline, *dstcol;
slouken@1895
  1007
    Uint32 *srcline, *srccol;
patmandin@991
  1008
slouken@1895
  1009
    srcline = (Uint32 *) gl_shadow;
slouken@1895
  1010
    srcpitch = (surface->w * gl_pixelsize) >> 2;
slouken@1895
  1011
    dstline = surface->pixels;
slouken@1895
  1012
    dstpitch = surface->pitch >> 1;
slouken@1895
  1013
    if (gl_upsidedown) {
slouken@1895
  1014
        srcline += (surface->h - 1) * srcpitch;
slouken@1895
  1015
        srcpitch = -srcpitch;
slouken@1895
  1016
    }
patmandin@991
  1017
slouken@1895
  1018
    for (y = 0; y < surface->h; y++) {
slouken@1895
  1019
        srccol = srcline;
slouken@1895
  1020
        dstcol = dstline;
slouken@1895
  1021
slouken@1895
  1022
        for (x = 0; x < surface->w; x++) {
slouken@1895
  1023
            Uint32 srccolor;
slouken@1895
  1024
            Uint16 dstcolor;
patmandin@991
  1025
slouken@1895
  1026
            srccolor = *srccol++;
slouken@1895
  1027
            dstcolor = (srccolor >> 8) & (31 << 11);
slouken@1895
  1028
            dstcolor |= (srccolor >> 5) & (63 << 5);
slouken@1895
  1029
            dstcolor |= (srccolor >> 3) & 31;
slouken@1895
  1030
            *dstcol++ = dstcolor;
slouken@1895
  1031
        }
patmandin@991
  1032
slouken@1895
  1033
        srcline += srcpitch;
slouken@1895
  1034
        dstline += dstpitch;
slouken@1895
  1035
    }
patmandin@991
  1036
}
patmandin@991
  1037
patmandin@991
  1038
/*--- Conversions routines in the screen ---*/
patmandin@991
  1039
slouken@1895
  1040
static void
slouken@1895
  1041
ConvertNull(_THIS, SDL_Surface * surface)
patmandin@991
  1042
{
patmandin@991
  1043
}
patmandin@991
  1044
slouken@1895
  1045
static void
slouken@1895
  1046
Convert565To555be(_THIS, SDL_Surface * surface)
patmandin@989
  1047
{
slouken@1895
  1048
    int x, y, pitch;
slouken@1895
  1049
    unsigned short *line, *pixel;
patmandin@989
  1050
slouken@1895
  1051
    line = surface->pixels;
slouken@1895
  1052
    pitch = surface->pitch >> 1;
slouken@1895
  1053
    for (y = 0; y < surface->h; y++) {
slouken@1895
  1054
        pixel = line;
slouken@1895
  1055
        for (x = 0; x < surface->w; x++) {
slouken@1895
  1056
            unsigned short color = *pixel;
patmandin@989
  1057
slouken@1895
  1058
            *pixel++ = (color & 0x1f) | ((color >> 1) & 0xffe0);
slouken@1895
  1059
        }
patmandin@989
  1060
slouken@1895
  1061
        line += pitch;
slouken@1895
  1062
    }
patmandin@989
  1063
}
patmandin@989
  1064
slouken@1895
  1065
static void
slouken@1895
  1066
Convert565To555le(_THIS, SDL_Surface * surface)
patmandin@989
  1067
{
slouken@1895
  1068
    int x, y, pitch;
slouken@1895
  1069
    unsigned short *line, *pixel;
patmandin@989
  1070
slouken@1895
  1071
    line = surface->pixels;
slouken@1895
  1072
    pitch = surface->pitch >> 1;
slouken@1895
  1073
    for (y = 0; y < surface->h; y++) {
slouken@1895
  1074
        pixel = line;
slouken@1895
  1075
        for (x = 0; x < surface->w; x++) {
slouken@1895
  1076
            unsigned short color = *pixel;
patmandin@989
  1077
slouken@1895
  1078
            color = (color & 0x1f) | ((color >> 1) & 0xffe0);
slouken@1895
  1079
            *pixel++ = SDL_Swap16(color);
slouken@1895
  1080
        }
patmandin@989
  1081
slouken@1895
  1082
        line += pitch;
slouken@1895
  1083
    }
patmandin@989
  1084
}
patmandin@989
  1085
slouken@1895
  1086
static void
slouken@1895
  1087
Convert565le(_THIS, SDL_Surface * surface)
patmandin@989
  1088
{
slouken@1895
  1089
    int x, y, pitch;
slouken@1895
  1090
    unsigned short *line, *pixel;
patmandin@989
  1091
slouken@1895
  1092
    line = surface->pixels;
slouken@1895
  1093
    pitch = surface->pitch >> 1;
slouken@1895
  1094
    for (y = 0; y < surface->h; y++) {
slouken@1895
  1095
        pixel = line;
slouken@1895
  1096
        for (x = 0; x < surface->w; x++) {
slouken@1895
  1097
            unsigned short color = *pixel;
patmandin@989
  1098
slouken@1895
  1099
            *pixel++ = SDL_Swap16(color);
slouken@1895
  1100
        }
patmandin@989
  1101
slouken@1895
  1102
        line += pitch;
slouken@1895
  1103
    }
patmandin@989
  1104
}
patmandin@989
  1105
slouken@1895
  1106
static void
slouken@1895
  1107
ConvertBGRAToABGR(_THIS, SDL_Surface * surface)
patmandin@989
  1108
{
slouken@1895
  1109
    int x, y, pitch;
slouken@1895
  1110
    unsigned long *line, *pixel;
patmandin@989
  1111
slouken@1895
  1112
    line = surface->pixels;
slouken@1895
  1113
    pitch = surface->pitch >> 2;
slouken@1895
  1114
    for (y = 0; y < surface->h; y++) {
slouken@1895
  1115
        pixel = line;
slouken@1895
  1116
        for (x = 0; x < surface->w; x++) {
slouken@1895
  1117
            unsigned long color = *pixel;
patmandin@989
  1118
slouken@1895
  1119
            *pixel++ = (color << 24) | (color >> 8);
slouken@1895
  1120
        }
patmandin@989
  1121
slouken@1895
  1122
        line += pitch;
slouken@1895
  1123
    }
patmandin@989
  1124
}
patmandin@1000
  1125
slouken@1361
  1126
#endif /* SDL_VIDEO_OPENGL */
slouken@1895
  1127
/* vi: set ts=4 sw=4 expandtab: */