src/video/SDL_renderer_sw.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 07 Dec 2008 22:37:40 +0000
changeset 2853 6258fa7cd300
parent 2811 7af2419ad5b0
child 2859 99210400e8b9
permissions -rw-r--r--
Fixed picking blit function when RLE fails
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@1985
    62
static int SW_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
slouken@1985
    63
                         Uint8 a, const SDL_Rect * rect);
slouken@1918
    64
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    65
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@1918
    66
static void SW_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    67
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    68
static void SW_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    69
slouken@1895
    70
slouken@1918
    71
SDL_RenderDriver SW_RenderDriver = {
slouken@1918
    72
    SW_CreateRenderer,
slouken@1895
    73
    {
slouken@1895
    74
     "software",
slouken@1965
    75
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
slouken@1965
    76
      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
slouken@1965
    77
      SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC),
slouken@1985
    78
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
    79
      SDL_TEXTUREMODULATE_ALPHA),
slouken@1965
    80
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1985
    81
      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
slouken@1985
    82
      SDL_TEXTUREBLENDMODE_MOD),
slouken@1965
    83
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST),
slouken@2811
    84
     14,
slouken@1895
    85
     {
slouken@1965
    86
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    87
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    88
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    89
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    90
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    91
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    92
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
    93
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
    94
      SDL_PIXELFORMAT_BGRA8888,
slouken@2811
    95
      SDL_PIXELFORMAT_YV12,
slouken@2811
    96
      SDL_PIXELFORMAT_IYUV,
slouken@1965
    97
      SDL_PIXELFORMAT_YUY2,
slouken@2810
    98
      SDL_PIXELFORMAT_UYVY,
slouken@2810
    99
      SDL_PIXELFORMAT_YVYU},
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->DestroyTexture = SW_DestroyTexture;
slouken@2227
   178
slouken@2227
   179
    renderer->info.mod_modes = SW_RenderDriver.info.mod_modes;
slouken@2227
   180
    renderer->info.blend_modes = SW_RenderDriver.info.blend_modes;
slouken@2227
   181
    renderer->info.scale_modes = SW_RenderDriver.info.scale_modes;
slouken@2227
   182
    renderer->info.num_texture_formats =
slouken@2227
   183
        SW_RenderDriver.info.num_texture_formats;
slouken@2227
   184
    SDL_memcpy(renderer->info.texture_formats,
slouken@2227
   185
               SW_RenderDriver.info.texture_formats,
slouken@2227
   186
               sizeof(renderer->info.texture_formats));;
slouken@2227
   187
    renderer->info.max_texture_width = SW_RenderDriver.info.max_texture_width;
slouken@2227
   188
    renderer->info.max_texture_height =
slouken@2227
   189
        SW_RenderDriver.info.max_texture_height;
slouken@2227
   190
}
slouken@2227
   191
slouken@1895
   192
SDL_Renderer *
slouken@1918
   193
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   194
{
slouken@1895
   195
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   196
    SDL_DisplayMode *displayMode = &display->current_mode;
slouken@1895
   197
    SDL_Renderer *renderer;
slouken@1918
   198
    SW_RenderData *data;
slouken@1895
   199
    int i, n;
slouken@1895
   200
    int bpp;
slouken@1895
   201
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1907
   202
    Uint32 renderer_flags;
slouken@1929
   203
    const char *desired_driver;
slouken@1895
   204
slouken@1895
   205
    if (!SDL_PixelFormatEnumToMasks
slouken@1895
   206
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   207
        SDL_SetError("Unknown display format");
slouken@1895
   208
        return NULL;
slouken@1895
   209
    }
slouken@1895
   210
slouken@1895
   211
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   212
    if (!renderer) {
slouken@1895
   213
        SDL_OutOfMemory();
slouken@1895
   214
        return NULL;
slouken@1895
   215
    }
slouken@1895
   216
slouken@1920
   217
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   218
    if (!data) {
slouken@1918
   219
        SW_DestroyRenderer(renderer);
slouken@1895
   220
        SDL_OutOfMemory();
slouken@1895
   221
        return NULL;
slouken@1895
   222
    }
slouken@1970
   223
    renderer->ActivateRenderer = SW_ActivateRenderer;
slouken@1970
   224
    renderer->DisplayModeChanged = SW_DisplayModeChanged;
slouken@1918
   225
    renderer->RenderFill = SW_RenderFill;
slouken@1918
   226
    renderer->RenderCopy = SW_RenderCopy;
slouken@1918
   227
    renderer->RenderPresent = SW_RenderPresent;
slouken@1918
   228
    renderer->DestroyRenderer = SW_DestroyRenderer;
slouken@2227
   229
    renderer->info.name = SW_RenderDriver.info.name;
slouken@2227
   230
    renderer->info.flags = 0;
slouken@1895
   231
    renderer->window = window->id;
slouken@1895
   232
    renderer->driverdata = data;
slouken@2227
   233
    Setup_SoftwareRenderer(renderer);
slouken@1895
   234
slouken@1965
   235
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1965
   236
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
slouken@1895
   237
        n = 2;
slouken@1965
   238
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1965
   239
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
slouken@1895
   240
        n = 3;
slouken@1895
   241
    } else {
slouken@1965
   242
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
slouken@1895
   243
        n = 1;
slouken@1895
   244
    }
slouken@1907
   245
    data->format = displayMode->format;
slouken@1895
   246
slouken@1895
   247
    /* Find a render driver that we can use to display data */
slouken@1965
   248
    renderer_flags = (SDL_RENDERER_SINGLEBUFFER |
slouken@1965
   249
                      SDL_RENDERER_PRESENTDISCARD);
slouken@1965
   250
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   251
        renderer_flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   252
    }
slouken@1929
   253
    desired_driver = SDL_getenv("SDL_VIDEO_RENDERER_SWDRIVER");
slouken@1895
   254
    for (i = 0; i < display->num_render_drivers; ++i) {
slouken@1895
   255
        SDL_RenderDriver *driver = &display->render_drivers[i];
slouken@1929
   256
        if (driver->info.name == SW_RenderDriver.info.name) {
slouken@1929
   257
            continue;
slouken@1929
   258
        }
slouken@1929
   259
        if (desired_driver
slouken@1929
   260
            && SDL_strcasecmp(desired_driver, driver->info.name) != 0) {
slouken@1929
   261
            continue;
slouken@1929
   262
        }
slouken@1929
   263
        data->renderer = driver->CreateRenderer(window, renderer_flags);
slouken@1929
   264
        if (data->renderer) {
slouken@1929
   265
            break;
slouken@1895
   266
        }
slouken@1895
   267
    }
slouken@1895
   268
    if (i == display->num_render_drivers) {
slouken@1918
   269
        SW_DestroyRenderer(renderer);
slouken@1895
   270
        SDL_SetError("Couldn't find display render driver");
slouken@1895
   271
        return NULL;
slouken@1895
   272
    }
slouken@1965
   273
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   274
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   275
    }
slouken@1907
   276
slouken@1907
   277
    /* Create the textures we'll use for display */
slouken@1907
   278
    for (i = 0; i < n; ++i) {
slouken@1907
   279
        data->texture[i] =
slouken@1907
   280
            CreateTexture(data->renderer, data->format, window->w, window->h);
slouken@1907
   281
        if (!data->texture[i]) {
slouken@1918
   282
            SW_DestroyRenderer(renderer);
slouken@1907
   283
            return NULL;
slouken@1907
   284
        }
slouken@1907
   285
    }
slouken@1907
   286
    data->current_texture = 0;
slouken@1907
   287
slouken@1907
   288
    /* Create a surface we'll use for rendering */
slouken@1907
   289
    data->surface.flags = SDL_PREALLOC;
slouken@1907
   290
    data->surface.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1907
   291
    if (!data->surface.format) {
slouken@1918
   292
        SW_DestroyRenderer(renderer);
slouken@1907
   293
        return NULL;
slouken@1907
   294
    }
slouken@1907
   295
    SDL_SetSurfacePalette(&data->surface, display->palette);
slouken@1907
   296
slouken@1907
   297
    /* Set up a palette watch on the display palette */
slouken@1907
   298
    if (display->palette) {
slouken@1907
   299
        SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
slouken@1907
   300
    }
slouken@1907
   301
slouken@1895
   302
    return renderer;
slouken@1895
   303
}
slouken@1895
   304
slouken@1895
   305
static int
slouken@1970
   306
SW_ActivateRenderer(SDL_Renderer * renderer)
slouken@1970
   307
{
slouken@1970
   308
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1972
   309
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1972
   310
    int i, n;
slouken@1970
   311
slouken@1970
   312
    if (data->renderer && data->renderer->ActivateRenderer) {
slouken@1970
   313
        if (data->renderer->ActivateRenderer(data->renderer) < 0) {
slouken@1970
   314
            return -1;
slouken@1970
   315
        }
slouken@1970
   316
    }
slouken@1972
   317
    if (data->updateSize) {
slouken@1972
   318
        /* Recreate the textures for the new window size */
slouken@1972
   319
        if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1972
   320
            n = 2;
slouken@1972
   321
        } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1972
   322
            n = 3;
slouken@1972
   323
        } else {
slouken@1972
   324
            n = 1;
slouken@1972
   325
        }
slouken@1972
   326
        for (i = 0; i < n; ++i) {
slouken@1972
   327
            if (data->texture[i]) {
slouken@1972
   328
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1972
   329
                data->texture[i] = 0;
slouken@1972
   330
            }
slouken@1972
   331
        }
slouken@1972
   332
        for (i = 0; i < n; ++i) {
slouken@1972
   333
            data->texture[i] =
slouken@1972
   334
                CreateTexture(data->renderer, data->format, window->w,
slouken@1972
   335
                              window->h);
slouken@1972
   336
            if (!data->texture[i]) {
slouken@1972
   337
                return -1;
slouken@1972
   338
            }
slouken@1972
   339
        }
slouken@1972
   340
        data->updateSize = SDL_FALSE;
slouken@1972
   341
    }
slouken@1970
   342
    return 0;
slouken@1970
   343
}
slouken@1970
   344
slouken@1970
   345
static int
slouken@1970
   346
SW_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   347
{
slouken@1970
   348
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
   349
slouken@1970
   350
    if (data->renderer && data->renderer->DisplayModeChanged) {
slouken@1970
   351
        if (data->renderer->DisplayModeChanged(data->renderer) < 0) {
slouken@1970
   352
            return -1;
slouken@1970
   353
        }
slouken@1970
   354
    }
slouken@1972
   355
    data->updateSize = SDL_TRUE;
slouken@1970
   356
    return 0;
slouken@1970
   357
}
slouken@1970
   358
slouken@1970
   359
static int
slouken@1918
   360
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   361
{
slouken@1895
   362
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@2786
   363
        texture->driverdata =
slouken@2786
   364
            SDL_SW_CreateYUVTexture(texture->format, texture->w, texture->h);
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@2802
   378
        if (texture->access == SDL_TEXTUREACCESS_STATIC) {
slouken@2802
   379
            SDL_SetSurfaceRLE(texture->driverdata, 1);
slouken@2802
   380
        }
slouken@1895
   381
    }
slouken@1895
   382
slouken@1895
   383
    if (!texture->driverdata) {
slouken@1895
   384
        return -1;
slouken@1895
   385
    }
slouken@1895
   386
    return 0;
slouken@1895
   387
}
slouken@1895
   388
slouken@1895
   389
static int
slouken@1918
   390
SW_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   391
                      void **pixels, int *pitch)
slouken@1895
   392
{
slouken@1895
   393
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@2735
   394
        return SDL_SW_QueryYUVTexturePixels((SDL_SW_YUVTexture *)
slouken@2735
   395
                                            texture->driverdata, pixels,
slouken@2735
   396
                                            pitch);
slouken@1895
   397
    } else {
slouken@1895
   398
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   399
slouken@1895
   400
        *pixels = surface->pixels;
slouken@1895
   401
        *pitch = surface->pitch;
slouken@1895
   402
        return 0;
slouken@1895
   403
    }
slouken@1895
   404
}
slouken@1895
   405
slouken@1895
   406
static int
slouken@1918
   407
SW_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   408
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   409
{
slouken@1895
   410
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   411
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   412
        return -1;
slouken@1895
   413
    } else {
slouken@1895
   414
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   415
slouken@1895
   416
        return SDL_SetPaletteColors(surface->format->palette, colors,
slouken@1895
   417
                                    firstcolor, ncolors);
slouken@1895
   418
    }
slouken@1895
   419
}
slouken@1895
   420
slouken@1895
   421
static int
slouken@1918
   422
SW_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   423
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   424
{
slouken@1895
   425
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   426
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   427
        return -1;
slouken@1895
   428
    } else {
slouken@1895
   429
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   430
slouken@1895
   431
        SDL_memcpy(colors, &surface->format->palette->colors[firstcolor],
slouken@1895
   432
                   ncolors * sizeof(*colors));
slouken@1895
   433
        return 0;
slouken@1895
   434
    }
slouken@1895
   435
}
slouken@1895
   436
slouken@1985
   437
static int
slouken@1985
   438
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   439
{
slouken@2267
   440
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   441
    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
slouken@2267
   442
                                  texture->b);
slouken@1985
   443
}
slouken@1985
   444
slouken@1985
   445
static int
slouken@1985
   446
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   447
{
slouken@2267
   448
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   449
    return SDL_SetSurfaceAlphaMod(surface, texture->a);
slouken@1985
   450
}
slouken@1985
   451
slouken@1985
   452
static int
slouken@1985
   453
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   454
{
slouken@2267
   455
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   456
    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
slouken@1985
   457
}
slouken@1985
   458
slouken@1985
   459
static int
slouken@1985
   460
SW_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   461
{
slouken@2267
   462
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2802
   463
    return SDL_SetSurfaceScaleMode(surface, texture->scaleMode);
slouken@1985
   464
}
slouken@1985
   465
slouken@1895
   466
static int
slouken@1918
   467
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   468
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   469
{
slouken@1895
   470
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@2735
   471
        return SDL_SW_UpdateYUVTexture((SDL_SW_YUVTexture *)
slouken@2735
   472
                                       texture->driverdata, rect, pixels,
slouken@2735
   473
                                       pitch);
slouken@1895
   474
    } else {
slouken@1895
   475
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   476
        Uint8 *src, *dst;
slouken@1895
   477
        int row;
slouken@1895
   478
        size_t length;
slouken@1895
   479
slouken@1895
   480
        src = (Uint8 *) pixels;
slouken@1895
   481
        dst =
slouken@1895
   482
            (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   483
            rect->x * surface->format->BytesPerPixel;
slouken@1895
   484
        length = rect->w * surface->format->BytesPerPixel;
slouken@1895
   485
        for (row = 0; row < rect->h; ++row) {
slouken@1895
   486
            SDL_memcpy(dst, src, length);
slouken@1895
   487
            src += pitch;
slouken@1895
   488
            dst += surface->pitch;
slouken@1895
   489
        }
slouken@1895
   490
        return 0;
slouken@1895
   491
    }
slouken@1895
   492
}
slouken@1895
   493
slouken@1895
   494
static int
slouken@1918
   495
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   496
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   497
               int *pitch)
slouken@1895
   498
{
slouken@1895
   499
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@2735
   500
        return SDL_SW_LockYUVTexture((SDL_SW_YUVTexture *)
slouken@2735
   501
                                     texture->driverdata, rect, markDirty,
slouken@2735
   502
                                     pixels, pitch);
slouken@1895
   503
    } else {
slouken@1895
   504
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   505
slouken@1895
   506
        *pixels =
slouken@1895
   507
            (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   508
                      rect->x * surface->format->BytesPerPixel);
slouken@1895
   509
        *pitch = surface->pitch;
slouken@1895
   510
        return 0;
slouken@1895
   511
    }
slouken@1895
   512
}
slouken@1895
   513
slouken@1895
   514
static void
slouken@1918
   515
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   516
{
slouken@1895
   517
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   518
        SDL_SW_UnlockYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   519
    }
slouken@1895
   520
}
slouken@1895
   521
slouken@1895
   522
static int
slouken@1985
   523
SW_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
slouken@1985
   524
              const SDL_Rect * rect)
slouken@1895
   525
{
slouken@1918
   526
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1985
   527
    Uint32 color;
slouken@1907
   528
    SDL_Rect real_rect;
slouken@1907
   529
    int status;
slouken@1895
   530
slouken@1965
   531
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   532
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1897
   533
    }
slouken@1895
   534
slouken@1907
   535
    color = SDL_MapRGBA(data->surface.format, r, g, b, a);
slouken@1895
   536
slouken@2735
   537
    if (data->renderer->LockTexture(data->renderer,
slouken@2735
   538
                                    data->texture[data->current_texture],
slouken@2735
   539
                                    rect, 1, &data->surface.pixels,
slouken@2735
   540
                                    &data->surface.pitch) < 0) {
slouken@1907
   541
        return -1;
slouken@1907
   542
    }
slouken@1907
   543
    data->surface.w = rect->w;
slouken@1907
   544
    data->surface.h = rect->h;
slouken@1907
   545
    data->surface.clip_rect.w = rect->w;
slouken@1907
   546
    data->surface.clip_rect.h = rect->h;
slouken@1907
   547
    real_rect = data->surface.clip_rect;
slouken@1907
   548
slouken@1907
   549
    status = SDL_FillRect(&data->surface, &real_rect, color);
slouken@1907
   550
slouken@1907
   551
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   552
                                  data->texture[data->current_texture]);
slouken@1907
   553
    return status;
slouken@1895
   554
}
slouken@1895
   555
slouken@1895
   556
static int
slouken@1918
   557
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   558
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   559
{
slouken@1918
   560
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   561
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   562
    int status;
slouken@1895
   563
slouken@1965
   564
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   565
        SDL_AddDirtyRect(&data->dirty, dstrect);
slouken@1897
   566
    }
slouken@1895
   567
slouken@2735
   568
    if (data->renderer->LockTexture(data->renderer,
slouken@2735
   569
                                    data->texture[data->current_texture],
slouken@2735
   570
                                    dstrect, 1, &data->surface.pixels,
slouken@2735
   571
                                    &data->surface.pitch) < 0) {
slouken@1907
   572
        return -1;
slouken@1907
   573
    }
slouken@1907
   574
slouken@1895
   575
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1907
   576
        status =
slouken@1907
   577
            SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
slouken@1907
   578
                                srcrect, data->format, dstrect->w, dstrect->h,
slouken@1907
   579
                                data->surface.pixels, data->surface.pitch);
slouken@1895
   580
    } else {
slouken@1895
   581
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   582
        SDL_Rect real_srcrect = *srcrect;
slouken@2267
   583
        SDL_Rect real_dstrect;
slouken@1907
   584
slouken@2267
   585
        data->surface.w = dstrect->w;
slouken@2267
   586
        data->surface.h = dstrect->h;
slouken@2267
   587
        data->surface.clip_rect.w = dstrect->w;
slouken@2267
   588
        data->surface.clip_rect.h = dstrect->h;
slouken@2267
   589
        real_dstrect = data->surface.clip_rect;
slouken@1895
   590
slouken@2267
   591
        status =
slouken@2267
   592
            SDL_LowerBlit(surface, &real_srcrect, &data->surface,
slouken@2267
   593
                          &real_dstrect);
slouken@1895
   594
    }
slouken@1907
   595
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   596
                                  data->texture[data->current_texture]);
slouken@1907
   597
    return status;
slouken@1895
   598
}
slouken@1895
   599
slouken@1895
   600
static void
slouken@1918
   601
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   602
{
slouken@1918
   603
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   604
    SDL_Texture *texture = data->texture[data->current_texture];
slouken@1895
   605
slouken@1895
   606
    /* Send the data to the display */
slouken@1965
   607
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1907
   608
        SDL_DirtyRect *dirty;
slouken@1907
   609
        for (dirty = data->dirty.list; dirty; dirty = dirty->next) {
slouken@1907
   610
            data->renderer->RenderCopy(data->renderer, texture, &dirty->rect,
slouken@1985
   611
                                       &dirty->rect);
slouken@1907
   612
        }
slouken@1907
   613
        SDL_ClearDirtyRects(&data->dirty);
slouken@1907
   614
    } else {
slouken@1907
   615
        SDL_Rect rect;
slouken@1907
   616
        rect.x = 0;
slouken@1907
   617
        rect.y = 0;
slouken@1907
   618
        rect.w = texture->w;
slouken@1907
   619
        rect.h = texture->h;
slouken@1985
   620
        data->renderer->RenderCopy(data->renderer, texture, &rect, &rect);
slouken@1895
   621
    }
slouken@1895
   622
    data->renderer->RenderPresent(data->renderer);
slouken@1895
   623
slouken@1895
   624
    /* Update the flipping chain, if any */
slouken@1965
   625
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1907
   626
        data->current_texture = (data->current_texture + 1) % 2;
slouken@1965
   627
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1907
   628
        data->current_texture = (data->current_texture + 1) % 3;
slouken@1895
   629
    }
slouken@1895
   630
}
slouken@1895
   631
slouken@1895
   632
static void
slouken@1918
   633
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   634
{
slouken@1895
   635
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   636
        SDL_SW_DestroyYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   637
    } else {
slouken@1895
   638
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   639
slouken@1895
   640
        SDL_FreeSurface(surface);
slouken@1895
   641
    }
slouken@1895
   642
}
slouken@1895
   643
slouken@1895
   644
static void
slouken@1918
   645
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   646
{
slouken@1918
   647
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   648
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   649
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   650
    int i;
slouken@1895
   651
slouken@1895
   652
    if (data) {
slouken@1907
   653
        for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   654
            if (data->texture[i]) {
slouken@1907
   655
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1895
   656
            }
slouken@1895
   657
        }
slouken@1907
   658
        if (data->surface.format) {
slouken@1907
   659
            SDL_SetSurfacePalette(&data->surface, NULL);
slouken@1907
   660
            SDL_FreeFormat(data->surface.format);
slouken@1907
   661
        }
slouken@1907
   662
        if (display->palette) {
slouken@1907
   663
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
slouken@1907
   664
                                data);
slouken@1907
   665
        }
slouken@1907
   666
        if (data->renderer) {
slouken@1907
   667
            data->renderer->DestroyRenderer(data->renderer);
slouken@1907
   668
        }
slouken@1895
   669
        SDL_FreeDirtyRects(&data->dirty);
slouken@1895
   670
        SDL_free(data);
slouken@1895
   671
    }
slouken@1895
   672
    SDL_free(renderer);
slouken@1895
   673
}
slouken@1895
   674
slouken@1895
   675
/* vi: set ts=4 sw=4 expandtab: */