src/video/SDL_renderer_sw.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 Aug 2007 05:39:09 +0000
changeset 2267 c785543d1843
parent 2262 bee005ace1bf
child 2698 e1da92da346c
permissions -rw-r--r--
Okay, still some bugs, but everything builds again...
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#include "SDL_video.h"
slouken@1895
    25
#include "SDL_sysvideo.h"
slouken@1907
    26
#include "SDL_pixels_c.h"
slouken@1895
    27
#include "SDL_rect_c.h"
slouken@1895
    28
#include "SDL_yuv_sw_c.h"
slouken@1895
    29
slouken@1895
    30
slouken@1895
    31
/* SDL surface based renderer implementation */
slouken@1895
    32
slouken@1918
    33
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1970
    34
static int SW_ActivateRenderer(SDL_Renderer * renderer);
slouken@1970
    35
static int SW_DisplayModeChanged(SDL_Renderer * renderer);
slouken@1918
    36
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    37
static int SW_QueryTexturePixels(SDL_Renderer * renderer,
slouken@1918
    38
                                 SDL_Texture * texture, void **pixels,
slouken@1918
    39
                                 int *pitch);
slouken@1918
    40
static int SW_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    41
                                SDL_Texture * texture,
slouken@1918
    42
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    43
                                int ncolors);
slouken@1918
    44
static int SW_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    45
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    46
                                int firstcolor, int ncolors);
slouken@1985
    47
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    48
                                 SDL_Texture * texture);
slouken@1985
    49
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    50
                                 SDL_Texture * texture);
slouken@1985
    51
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@1985
    52
                                  SDL_Texture * texture);
slouken@1985
    53
static int SW_SetTextureScaleMode(SDL_Renderer * renderer,
slouken@1985
    54
                                  SDL_Texture * texture);
slouken@1985
    55
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    56
                            const SDL_Rect * rect, const void *pixels,
slouken@1985
    57
                            int pitch);
slouken@1918
    58
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    59
                          const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1985
    60
                          int *pitch);
slouken@1918
    61
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    62
static void SW_DirtyTexture(SDL_Renderer * renderer,
slouken@1918
    63
                            SDL_Texture * texture, int numrects,
slouken@1918
    64
                            const SDL_Rect * rects);
slouken@1985
    65
static int SW_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
slouken@1985
    66
                         Uint8 a, const SDL_Rect * rect);
slouken@1918
    67
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    68
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@1918
    69
static void SW_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    70
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    71
static void SW_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    72
slouken@1895
    73
slouken@1918
    74
SDL_RenderDriver SW_RenderDriver = {
slouken@1918
    75
    SW_CreateRenderer,
slouken@1895
    76
    {
slouken@1895
    77
     "software",
slouken@1965
    78
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
slouken@1965
    79
      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
slouken@1965
    80
      SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC),
slouken@1985
    81
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
    82
      SDL_TEXTUREMODULATE_ALPHA),
slouken@1965
    83
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1985
    84
      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
slouken@1985
    85
      SDL_TEXTUREBLENDMODE_MOD),
slouken@1965
    86
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST),
slouken@1895
    87
     11,
slouken@1895
    88
     {
slouken@1965
    89
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    90
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    91
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    92
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    93
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    94
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    95
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
    96
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
    97
      SDL_PIXELFORMAT_BGRA8888,
slouken@1965
    98
      SDL_PIXELFORMAT_YUY2,
slouken@1965
    99
      SDL_PIXELFORMAT_UYVY},
slouken@1895
   100
     0,
slouken@1895
   101
     0}
slouken@1895
   102
};
slouken@1895
   103
slouken@1895
   104
typedef struct
slouken@1895
   105
{
slouken@1907
   106
    Uint32 format;
slouken@1972
   107
    SDL_bool updateSize;
slouken@1907
   108
    int current_texture;
slouken@1907
   109
    SDL_Texture *texture[3];
slouken@1907
   110
    SDL_Surface surface;
slouken@1895
   111
    SDL_Renderer *renderer;
slouken@1895
   112
    SDL_DirtyRectList dirty;
slouken@1918
   113
} SW_RenderData;
slouken@1895
   114
slouken@1907
   115
static SDL_Texture *
slouken@1907
   116
CreateTexture(SDL_Renderer * renderer, Uint32 format, int w, int h)
slouken@1907
   117
{
slouken@1907
   118
    SDL_Texture *texture;
slouken@1907
   119
slouken@1920
   120
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@1907
   121
    if (!texture) {
slouken@1907
   122
        SDL_OutOfMemory();
slouken@1907
   123
        return NULL;
slouken@1907
   124
    }
slouken@1907
   125
slouken@1907
   126
    texture->format = format;
slouken@2222
   127
    texture->access = SDL_TEXTUREACCESS_STREAMING;
slouken@1907
   128
    texture->w = w;
slouken@1907
   129
    texture->h = h;
slouken@1907
   130
    texture->renderer = renderer;
slouken@1907
   131
slouken@1907
   132
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@1907
   133
        SDL_free(texture);
slouken@1907
   134
        return NULL;
slouken@1907
   135
    }
slouken@1907
   136
    return texture;
slouken@1907
   137
}
slouken@1907
   138
slouken@1907
   139
static void
slouken@1907
   140
DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1907
   141
{
slouken@1907
   142
    renderer->DestroyTexture(renderer, texture);
slouken@1907
   143
    SDL_free(texture);
slouken@1907
   144
}
slouken@1907
   145
slouken@1907
   146
static int
slouken@1907
   147
DisplayPaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1907
   148
{
slouken@1918
   149
    SW_RenderData *data = (SW_RenderData *) userdata;
slouken@1907
   150
    int i;
slouken@1907
   151
slouken@1907
   152
    for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   153
        if (data->texture[i] && data->renderer->SetTexturePalette) {
slouken@1907
   154
            data->renderer->SetTexturePalette(data->renderer,
slouken@1907
   155
                                              data->texture[i],
slouken@1907
   156
                                              palette->colors, 0,
slouken@1907
   157
                                              palette->ncolors);
slouken@1907
   158
        }
slouken@1907
   159
    }
slouken@1907
   160
    return 0;
slouken@1907
   161
}
slouken@1907
   162
slouken@2227
   163
void
slouken@2227
   164
Setup_SoftwareRenderer(SDL_Renderer * renderer)
slouken@2227
   165
{
slouken@2227
   166
    renderer->CreateTexture = SW_CreateTexture;
slouken@2227
   167
    renderer->QueryTexturePixels = SW_QueryTexturePixels;
slouken@2227
   168
    renderer->SetTexturePalette = SW_SetTexturePalette;
slouken@2227
   169
    renderer->GetTexturePalette = SW_GetTexturePalette;
slouken@2227
   170
    renderer->SetTextureColorMod = SW_SetTextureColorMod;
slouken@2227
   171
    renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
slouken@2227
   172
    renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
slouken@2227
   173
    renderer->SetTextureScaleMode = SW_SetTextureScaleMode;
slouken@2227
   174
    renderer->UpdateTexture = SW_UpdateTexture;
slouken@2227
   175
    renderer->LockTexture = SW_LockTexture;
slouken@2227
   176
    renderer->UnlockTexture = SW_UnlockTexture;
slouken@2227
   177
    renderer->DirtyTexture = SW_DirtyTexture;
slouken@2227
   178
    renderer->DestroyTexture = SW_DestroyTexture;
slouken@2227
   179
slouken@2227
   180
    renderer->info.mod_modes = SW_RenderDriver.info.mod_modes;
slouken@2227
   181
    renderer->info.blend_modes = SW_RenderDriver.info.blend_modes;
slouken@2227
   182
    renderer->info.scale_modes = SW_RenderDriver.info.scale_modes;
slouken@2227
   183
    renderer->info.num_texture_formats =
slouken@2227
   184
        SW_RenderDriver.info.num_texture_formats;
slouken@2227
   185
    SDL_memcpy(renderer->info.texture_formats,
slouken@2227
   186
               SW_RenderDriver.info.texture_formats,
slouken@2227
   187
               sizeof(renderer->info.texture_formats));;
slouken@2227
   188
    renderer->info.max_texture_width = SW_RenderDriver.info.max_texture_width;
slouken@2227
   189
    renderer->info.max_texture_height =
slouken@2227
   190
        SW_RenderDriver.info.max_texture_height;
slouken@2227
   191
}
slouken@2227
   192
slouken@1895
   193
SDL_Renderer *
slouken@1918
   194
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   195
{
slouken@1895
   196
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   197
    SDL_DisplayMode *displayMode = &display->current_mode;
slouken@1895
   198
    SDL_Renderer *renderer;
slouken@1918
   199
    SW_RenderData *data;
slouken@1895
   200
    int i, n;
slouken@1895
   201
    int bpp;
slouken@1895
   202
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1907
   203
    Uint32 renderer_flags;
slouken@1929
   204
    const char *desired_driver;
slouken@1895
   205
slouken@1895
   206
    if (!SDL_PixelFormatEnumToMasks
slouken@1895
   207
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   208
        SDL_SetError("Unknown display format");
slouken@1895
   209
        return NULL;
slouken@1895
   210
    }
slouken@1895
   211
slouken@1895
   212
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   213
    if (!renderer) {
slouken@1895
   214
        SDL_OutOfMemory();
slouken@1895
   215
        return NULL;
slouken@1895
   216
    }
slouken@1895
   217
slouken@1920
   218
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   219
    if (!data) {
slouken@1918
   220
        SW_DestroyRenderer(renderer);
slouken@1895
   221
        SDL_OutOfMemory();
slouken@1895
   222
        return NULL;
slouken@1895
   223
    }
slouken@1970
   224
    renderer->ActivateRenderer = SW_ActivateRenderer;
slouken@1970
   225
    renderer->DisplayModeChanged = SW_DisplayModeChanged;
slouken@1918
   226
    renderer->RenderFill = SW_RenderFill;
slouken@1918
   227
    renderer->RenderCopy = SW_RenderCopy;
slouken@1918
   228
    renderer->RenderPresent = SW_RenderPresent;
slouken@1918
   229
    renderer->DestroyRenderer = SW_DestroyRenderer;
slouken@2227
   230
    renderer->info.name = SW_RenderDriver.info.name;
slouken@2227
   231
    renderer->info.flags = 0;
slouken@1895
   232
    renderer->window = window->id;
slouken@1895
   233
    renderer->driverdata = data;
slouken@2227
   234
    Setup_SoftwareRenderer(renderer);
slouken@1895
   235
slouken@1965
   236
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1965
   237
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
slouken@1895
   238
        n = 2;
slouken@1965
   239
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1965
   240
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
slouken@1895
   241
        n = 3;
slouken@1895
   242
    } else {
slouken@1965
   243
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
slouken@1895
   244
        n = 1;
slouken@1895
   245
    }
slouken@1907
   246
    data->format = displayMode->format;
slouken@1895
   247
slouken@1895
   248
    /* Find a render driver that we can use to display data */
slouken@1965
   249
    renderer_flags = (SDL_RENDERER_SINGLEBUFFER |
slouken@1965
   250
                      SDL_RENDERER_PRESENTDISCARD);
slouken@1965
   251
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   252
        renderer_flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   253
    }
slouken@1929
   254
    desired_driver = SDL_getenv("SDL_VIDEO_RENDERER_SWDRIVER");
slouken@1895
   255
    for (i = 0; i < display->num_render_drivers; ++i) {
slouken@1895
   256
        SDL_RenderDriver *driver = &display->render_drivers[i];
slouken@1929
   257
        if (driver->info.name == SW_RenderDriver.info.name) {
slouken@1929
   258
            continue;
slouken@1929
   259
        }
slouken@1929
   260
        if (desired_driver
slouken@1929
   261
            && SDL_strcasecmp(desired_driver, driver->info.name) != 0) {
slouken@1929
   262
            continue;
slouken@1929
   263
        }
slouken@1929
   264
        data->renderer = driver->CreateRenderer(window, renderer_flags);
slouken@1929
   265
        if (data->renderer) {
slouken@1929
   266
            break;
slouken@1895
   267
        }
slouken@1895
   268
    }
slouken@1895
   269
    if (i == display->num_render_drivers) {
slouken@1918
   270
        SW_DestroyRenderer(renderer);
slouken@1895
   271
        SDL_SetError("Couldn't find display render driver");
slouken@1895
   272
        return NULL;
slouken@1895
   273
    }
slouken@1965
   274
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   275
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   276
    }
slouken@1907
   277
slouken@1907
   278
    /* Create the textures we'll use for display */
slouken@1907
   279
    for (i = 0; i < n; ++i) {
slouken@1907
   280
        data->texture[i] =
slouken@1907
   281
            CreateTexture(data->renderer, data->format, window->w, window->h);
slouken@1907
   282
        if (!data->texture[i]) {
slouken@1918
   283
            SW_DestroyRenderer(renderer);
slouken@1907
   284
            return NULL;
slouken@1907
   285
        }
slouken@1907
   286
    }
slouken@1907
   287
    data->current_texture = 0;
slouken@1907
   288
slouken@1907
   289
    /* Create a surface we'll use for rendering */
slouken@1907
   290
    data->surface.flags = SDL_PREALLOC;
slouken@1907
   291
    data->surface.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1907
   292
    if (!data->surface.format) {
slouken@1918
   293
        SW_DestroyRenderer(renderer);
slouken@1907
   294
        return NULL;
slouken@1907
   295
    }
slouken@1907
   296
    SDL_SetSurfacePalette(&data->surface, display->palette);
slouken@1907
   297
slouken@1907
   298
    /* Set up a palette watch on the display palette */
slouken@1907
   299
    if (display->palette) {
slouken@1907
   300
        SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
slouken@1907
   301
    }
slouken@1907
   302
slouken@1895
   303
    return renderer;
slouken@1895
   304
}
slouken@1895
   305
slouken@1895
   306
static int
slouken@1970
   307
SW_ActivateRenderer(SDL_Renderer * renderer)
slouken@1970
   308
{
slouken@1970
   309
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1972
   310
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1972
   311
    int i, n;
slouken@1970
   312
slouken@1970
   313
    if (data->renderer && data->renderer->ActivateRenderer) {
slouken@1970
   314
        if (data->renderer->ActivateRenderer(data->renderer) < 0) {
slouken@1970
   315
            return -1;
slouken@1970
   316
        }
slouken@1970
   317
    }
slouken@1972
   318
    if (data->updateSize) {
slouken@1972
   319
        /* Recreate the textures for the new window size */
slouken@1972
   320
        if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1972
   321
            n = 2;
slouken@1972
   322
        } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1972
   323
            n = 3;
slouken@1972
   324
        } else {
slouken@1972
   325
            n = 1;
slouken@1972
   326
        }
slouken@1972
   327
        for (i = 0; i < n; ++i) {
slouken@1972
   328
            if (data->texture[i]) {
slouken@1972
   329
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1972
   330
                data->texture[i] = 0;
slouken@1972
   331
            }
slouken@1972
   332
        }
slouken@1972
   333
        for (i = 0; i < n; ++i) {
slouken@1972
   334
            data->texture[i] =
slouken@1972
   335
                CreateTexture(data->renderer, data->format, window->w,
slouken@1972
   336
                              window->h);
slouken@1972
   337
            if (!data->texture[i]) {
slouken@1972
   338
                return -1;
slouken@1972
   339
            }
slouken@1972
   340
        }
slouken@1972
   341
        data->updateSize = SDL_FALSE;
slouken@1972
   342
    }
slouken@1970
   343
    return 0;
slouken@1970
   344
}
slouken@1970
   345
slouken@1970
   346
static int
slouken@1970
   347
SW_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   348
{
slouken@1970
   349
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
   350
slouken@1970
   351
    if (data->renderer && data->renderer->DisplayModeChanged) {
slouken@1970
   352
        if (data->renderer->DisplayModeChanged(data->renderer) < 0) {
slouken@1970
   353
            return -1;
slouken@1970
   354
        }
slouken@1970
   355
    }
slouken@1972
   356
    data->updateSize = SDL_TRUE;
slouken@1970
   357
    return 0;
slouken@1970
   358
}
slouken@1970
   359
slouken@1970
   360
static int
slouken@1918
   361
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   362
{
slouken@1895
   363
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   364
        texture->driverdata = SDL_SW_CreateYUVTexture(texture);
slouken@1895
   365
    } else {
slouken@1895
   366
        int bpp;
slouken@1895
   367
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   368
slouken@1895
   369
        if (!SDL_PixelFormatEnumToMasks
slouken@1895
   370
            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   371
            SDL_SetError("Unknown texture format");
slouken@1895
   372
            return -1;
slouken@1895
   373
        }
slouken@1895
   374
slouken@1895
   375
        texture->driverdata =
slouken@1895
   376
            SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@1895
   377
                                 Bmask, Amask);
slouken@1895
   378
    }
slouken@1895
   379
slouken@1895
   380
    if (!texture->driverdata) {
slouken@1895
   381
        return -1;
slouken@1895
   382
    }
slouken@1895
   383
    return 0;
slouken@1895
   384
}
slouken@1895
   385
slouken@1895
   386
static int
slouken@1918
   387
SW_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   388
                      void **pixels, int *pitch)
slouken@1895
   389
{
slouken@1895
   390
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   391
        return SDL_SW_QueryYUVTexturePixels((SDL_SW_YUVTexture *) texture->
slouken@1895
   392
                                            driverdata, pixels, pitch);
slouken@1895
   393
    } else {
slouken@1895
   394
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   395
slouken@1895
   396
        *pixels = surface->pixels;
slouken@1895
   397
        *pitch = surface->pitch;
slouken@1895
   398
        return 0;
slouken@1895
   399
    }
slouken@1895
   400
}
slouken@1895
   401
slouken@1895
   402
static int
slouken@1918
   403
SW_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   404
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   405
{
slouken@1895
   406
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   407
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   408
        return -1;
slouken@1895
   409
    } else {
slouken@1895
   410
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   411
slouken@1895
   412
        return SDL_SetPaletteColors(surface->format->palette, colors,
slouken@1895
   413
                                    firstcolor, ncolors);
slouken@1895
   414
    }
slouken@1895
   415
}
slouken@1895
   416
slouken@1895
   417
static int
slouken@1918
   418
SW_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   419
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   420
{
slouken@1895
   421
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   422
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   423
        return -1;
slouken@1895
   424
    } else {
slouken@1895
   425
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   426
slouken@1895
   427
        SDL_memcpy(colors, &surface->format->palette->colors[firstcolor],
slouken@1895
   428
                   ncolors * sizeof(*colors));
slouken@1895
   429
        return 0;
slouken@1895
   430
    }
slouken@1895
   431
}
slouken@1895
   432
slouken@1985
   433
static int
slouken@1985
   434
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   435
{
slouken@2267
   436
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   437
    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
slouken@2267
   438
                                  texture->b);
slouken@1985
   439
}
slouken@1985
   440
slouken@1985
   441
static int
slouken@1985
   442
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   443
{
slouken@2267
   444
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   445
    return SDL_SetSurfaceAlphaMod(surface, texture->a);
slouken@1985
   446
}
slouken@1985
   447
slouken@1985
   448
static int
slouken@1985
   449
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   450
{
slouken@2267
   451
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   452
    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
slouken@1985
   453
}
slouken@1985
   454
slouken@1985
   455
static int
slouken@1985
   456
SW_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   457
{
slouken@2267
   458
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   459
    return SDL_SetSurfaceBlendMode(surface, texture->scaleMode);
slouken@1985
   460
}
slouken@1985
   461
slouken@1895
   462
static int
slouken@1918
   463
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   464
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   465
{
slouken@1895
   466
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   467
        return SDL_SW_UpdateYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   468
                                       driverdata, rect, pixels, pitch);
slouken@1895
   469
    } else {
slouken@1895
   470
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   471
        Uint8 *src, *dst;
slouken@1895
   472
        int row;
slouken@1895
   473
        size_t length;
slouken@1895
   474
slouken@1895
   475
        src = (Uint8 *) pixels;
slouken@1895
   476
        dst =
slouken@1895
   477
            (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   478
            rect->x * surface->format->BytesPerPixel;
slouken@1895
   479
        length = rect->w * surface->format->BytesPerPixel;
slouken@1895
   480
        for (row = 0; row < rect->h; ++row) {
slouken@1895
   481
            SDL_memcpy(dst, src, length);
slouken@1895
   482
            src += pitch;
slouken@1895
   483
            dst += surface->pitch;
slouken@1895
   484
        }
slouken@1895
   485
        return 0;
slouken@1895
   486
    }
slouken@1895
   487
}
slouken@1895
   488
slouken@1895
   489
static int
slouken@1918
   490
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   491
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   492
               int *pitch)
slouken@1895
   493
{
slouken@1895
   494
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   495
        return SDL_SW_LockYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   496
                                     driverdata, rect, markDirty, pixels,
slouken@1895
   497
                                     pitch);
slouken@1895
   498
    } else {
slouken@1895
   499
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   500
slouken@1895
   501
        *pixels =
slouken@1895
   502
            (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   503
                      rect->x * surface->format->BytesPerPixel);
slouken@1895
   504
        *pitch = surface->pitch;
slouken@1895
   505
        return 0;
slouken@1895
   506
    }
slouken@1895
   507
}
slouken@1895
   508
slouken@1895
   509
static void
slouken@1918
   510
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   511
{
slouken@1895
   512
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   513
        SDL_SW_UnlockYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   514
    }
slouken@1895
   515
}
slouken@1895
   516
slouken@1895
   517
static void
slouken@1918
   518
SW_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   519
                int numrects, const SDL_Rect * rects)
slouken@1895
   520
{
slouken@1895
   521
}
slouken@1895
   522
slouken@1895
   523
static int
slouken@1985
   524
SW_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
slouken@1985
   525
              const SDL_Rect * rect)
slouken@1895
   526
{
slouken@1918
   527
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1985
   528
    Uint32 color;
slouken@1907
   529
    SDL_Rect real_rect;
slouken@1907
   530
    int status;
slouken@1895
   531
slouken@1965
   532
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   533
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1897
   534
    }
slouken@1895
   535
slouken@1907
   536
    color = SDL_MapRGBA(data->surface.format, r, g, b, a);
slouken@1895
   537
slouken@1907
   538
    if (data->renderer->
slouken@1907
   539
        LockTexture(data->renderer, data->texture[data->current_texture],
slouken@1907
   540
                    rect, 1, &data->surface.pixels,
slouken@1907
   541
                    &data->surface.pitch) < 0) {
slouken@1907
   542
        return -1;
slouken@1907
   543
    }
slouken@1907
   544
    data->surface.w = rect->w;
slouken@1907
   545
    data->surface.h = rect->h;
slouken@1907
   546
    data->surface.clip_rect.w = rect->w;
slouken@1907
   547
    data->surface.clip_rect.h = rect->h;
slouken@1907
   548
    real_rect = data->surface.clip_rect;
slouken@1907
   549
slouken@1907
   550
    status = SDL_FillRect(&data->surface, &real_rect, color);
slouken@1907
   551
slouken@1907
   552
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   553
                                  data->texture[data->current_texture]);
slouken@1907
   554
    return status;
slouken@1895
   555
}
slouken@1895
   556
slouken@1895
   557
static int
slouken@1918
   558
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   559
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   560
{
slouken@1918
   561
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   562
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   563
    int status;
slouken@1895
   564
slouken@1965
   565
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   566
        SDL_AddDirtyRect(&data->dirty, dstrect);
slouken@1897
   567
    }
slouken@1895
   568
slouken@1907
   569
    if (data->renderer->
slouken@1907
   570
        LockTexture(data->renderer, data->texture[data->current_texture],
slouken@1907
   571
                    dstrect, 1, &data->surface.pixels,
slouken@1907
   572
                    &data->surface.pitch) < 0) {
slouken@1907
   573
        return -1;
slouken@1907
   574
    }
slouken@1907
   575
slouken@1895
   576
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1907
   577
        status =
slouken@1907
   578
            SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
slouken@1907
   579
                                srcrect, data->format, dstrect->w, dstrect->h,
slouken@1907
   580
                                data->surface.pixels, data->surface.pitch);
slouken@1895
   581
    } else {
slouken@1895
   582
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   583
        SDL_Rect real_srcrect = *srcrect;
slouken@2267
   584
        SDL_Rect real_dstrect;
slouken@1907
   585
slouken@2267
   586
        data->surface.w = dstrect->w;
slouken@2267
   587
        data->surface.h = dstrect->h;
slouken@2267
   588
        data->surface.clip_rect.w = dstrect->w;
slouken@2267
   589
        data->surface.clip_rect.h = dstrect->h;
slouken@2267
   590
        real_dstrect = data->surface.clip_rect;
slouken@1895
   591
slouken@2267
   592
        status =
slouken@2267
   593
            SDL_LowerBlit(surface, &real_srcrect, &data->surface,
slouken@2267
   594
                          &real_dstrect);
slouken@1895
   595
    }
slouken@1907
   596
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   597
                                  data->texture[data->current_texture]);
slouken@1907
   598
    return status;
slouken@1895
   599
}
slouken@1895
   600
slouken@1895
   601
static void
slouken@1918
   602
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   603
{
slouken@1918
   604
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   605
    SDL_Texture *texture = data->texture[data->current_texture];
slouken@1895
   606
slouken@1895
   607
    /* Send the data to the display */
slouken@1965
   608
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1907
   609
        SDL_DirtyRect *dirty;
slouken@1907
   610
        for (dirty = data->dirty.list; dirty; dirty = dirty->next) {
slouken@1907
   611
            data->renderer->RenderCopy(data->renderer, texture, &dirty->rect,
slouken@1985
   612
                                       &dirty->rect);
slouken@1907
   613
        }
slouken@1907
   614
        SDL_ClearDirtyRects(&data->dirty);
slouken@1907
   615
    } else {
slouken@1907
   616
        SDL_Rect rect;
slouken@1907
   617
        rect.x = 0;
slouken@1907
   618
        rect.y = 0;
slouken@1907
   619
        rect.w = texture->w;
slouken@1907
   620
        rect.h = texture->h;
slouken@1985
   621
        data->renderer->RenderCopy(data->renderer, texture, &rect, &rect);
slouken@1895
   622
    }
slouken@1895
   623
    data->renderer->RenderPresent(data->renderer);
slouken@1895
   624
slouken@1895
   625
    /* Update the flipping chain, if any */
slouken@1965
   626
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1907
   627
        data->current_texture = (data->current_texture + 1) % 2;
slouken@1965
   628
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1907
   629
        data->current_texture = (data->current_texture + 1) % 3;
slouken@1895
   630
    }
slouken@1895
   631
}
slouken@1895
   632
slouken@1895
   633
static void
slouken@1918
   634
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   635
{
slouken@1895
   636
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   637
        SDL_SW_DestroyYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   638
    } else {
slouken@1895
   639
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   640
slouken@1895
   641
        SDL_FreeSurface(surface);
slouken@1895
   642
    }
slouken@1895
   643
}
slouken@1895
   644
slouken@1895
   645
static void
slouken@1918
   646
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   647
{
slouken@1918
   648
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   649
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   650
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   651
    int i;
slouken@1895
   652
slouken@1895
   653
    if (data) {
slouken@1907
   654
        for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   655
            if (data->texture[i]) {
slouken@1907
   656
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1895
   657
            }
slouken@1895
   658
        }
slouken@1907
   659
        if (data->surface.format) {
slouken@1907
   660
            SDL_SetSurfacePalette(&data->surface, NULL);
slouken@1907
   661
            SDL_FreeFormat(data->surface.format);
slouken@1907
   662
        }
slouken@1907
   663
        if (display->palette) {
slouken@1907
   664
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
slouken@1907
   665
                                data);
slouken@1907
   666
        }
slouken@1907
   667
        if (data->renderer) {
slouken@1907
   668
            data->renderer->DestroyRenderer(data->renderer);
slouken@1907
   669
        }
slouken@1895
   670
        SDL_FreeDirtyRects(&data->dirty);
slouken@1895
   671
        SDL_free(data);
slouken@1895
   672
    }
slouken@1895
   673
    SDL_free(renderer);
slouken@1895
   674
}
slouken@1895
   675
slouken@1895
   676
/* vi: set ts=4 sw=4 expandtab: */