src/video/SDL_renderer_sw.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 28 Aug 2006 03:17:39 +0000
changeset 1985 8055185ae4ed
parent 1972 a0e278364188
child 1989 5b5f5de5433f
permissions -rw-r--r--
Added source color and alpha modulation support.
Added perl script to generate optimized render copy functions.
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@1985
    29
#include "SDL_rendercopy.h"
slouken@1895
    30
slouken@1895
    31
slouken@1895
    32
/* SDL surface based renderer implementation */
slouken@1895
    33
slouken@1918
    34
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1970
    35
static int SW_ActivateRenderer(SDL_Renderer * renderer);
slouken@1970
    36
static int SW_DisplayModeChanged(SDL_Renderer * renderer);
slouken@1918
    37
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    38
static int SW_QueryTexturePixels(SDL_Renderer * renderer,
slouken@1918
    39
                                 SDL_Texture * texture, void **pixels,
slouken@1918
    40
                                 int *pitch);
slouken@1918
    41
static int SW_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    42
                                SDL_Texture * texture,
slouken@1918
    43
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    44
                                int ncolors);
slouken@1918
    45
static int SW_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    46
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    47
                                int firstcolor, int ncolors);
slouken@1985
    48
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    49
                                 SDL_Texture * texture);
slouken@1985
    50
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    51
                                 SDL_Texture * texture);
slouken@1985
    52
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@1985
    53
                                  SDL_Texture * texture);
slouken@1985
    54
static int SW_SetTextureScaleMode(SDL_Renderer * renderer,
slouken@1985
    55
                                  SDL_Texture * texture);
slouken@1985
    56
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    57
                            const SDL_Rect * rect, const void *pixels,
slouken@1985
    58
                            int pitch);
slouken@1918
    59
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    60
                          const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1985
    61
                          int *pitch);
slouken@1918
    62
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    63
static void SW_DirtyTexture(SDL_Renderer * renderer,
slouken@1918
    64
                            SDL_Texture * texture, int numrects,
slouken@1918
    65
                            const SDL_Rect * rects);
slouken@1985
    66
static int SW_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
slouken@1985
    67
                         Uint8 a, const SDL_Rect * rect);
slouken@1918
    68
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    69
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@1918
    70
static void SW_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    71
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    72
static void SW_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    73
slouken@1895
    74
slouken@1918
    75
SDL_RenderDriver SW_RenderDriver = {
slouken@1918
    76
    SW_CreateRenderer,
slouken@1895
    77
    {
slouken@1895
    78
     "software",
slouken@1965
    79
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
slouken@1965
    80
      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
slouken@1965
    81
      SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC),
slouken@1985
    82
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
    83
      SDL_TEXTUREMODULATE_ALPHA),
slouken@1965
    84
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1985
    85
      SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
slouken@1985
    86
      SDL_TEXTUREBLENDMODE_MOD),
slouken@1965
    87
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST),
slouken@1895
    88
     11,
slouken@1895
    89
     {
slouken@1965
    90
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    91
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    92
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    93
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    94
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    95
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    96
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
    97
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
    98
      SDL_PIXELFORMAT_BGRA8888,
slouken@1965
    99
      SDL_PIXELFORMAT_YUY2,
slouken@1965
   100
      SDL_PIXELFORMAT_UYVY},
slouken@1895
   101
     0,
slouken@1895
   102
     0}
slouken@1895
   103
};
slouken@1895
   104
slouken@1895
   105
typedef struct
slouken@1895
   106
{
slouken@1907
   107
    Uint32 format;
slouken@1972
   108
    SDL_bool updateSize;
slouken@1907
   109
    int current_texture;
slouken@1907
   110
    SDL_Texture *texture[3];
slouken@1907
   111
    SDL_Surface surface;
slouken@1895
   112
    SDL_Renderer *renderer;
slouken@1895
   113
    SDL_DirtyRectList dirty;
slouken@1918
   114
} SW_RenderData;
slouken@1895
   115
slouken@1907
   116
static SDL_Texture *
slouken@1907
   117
CreateTexture(SDL_Renderer * renderer, Uint32 format, int w, int h)
slouken@1907
   118
{
slouken@1907
   119
    SDL_Texture *texture;
slouken@1907
   120
slouken@1920
   121
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@1907
   122
    if (!texture) {
slouken@1907
   123
        SDL_OutOfMemory();
slouken@1907
   124
        return NULL;
slouken@1907
   125
    }
slouken@1907
   126
slouken@1907
   127
    texture->format = format;
slouken@1965
   128
    texture->access = SDL_TEXTUREACCESS_LOCAL;
slouken@1907
   129
    texture->w = w;
slouken@1907
   130
    texture->h = h;
slouken@1907
   131
    texture->renderer = renderer;
slouken@1907
   132
slouken@1907
   133
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@1907
   134
        SDL_free(texture);
slouken@1907
   135
        return NULL;
slouken@1907
   136
    }
slouken@1907
   137
    return texture;
slouken@1907
   138
}
slouken@1907
   139
slouken@1907
   140
static void
slouken@1907
   141
DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1907
   142
{
slouken@1907
   143
    renderer->DestroyTexture(renderer, texture);
slouken@1907
   144
    SDL_free(texture);
slouken@1907
   145
}
slouken@1907
   146
slouken@1907
   147
static int
slouken@1907
   148
DisplayPaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1907
   149
{
slouken@1918
   150
    SW_RenderData *data = (SW_RenderData *) userdata;
slouken@1907
   151
    int i;
slouken@1907
   152
slouken@1907
   153
    for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   154
        if (data->texture[i] && data->renderer->SetTexturePalette) {
slouken@1907
   155
            data->renderer->SetTexturePalette(data->renderer,
slouken@1907
   156
                                              data->texture[i],
slouken@1907
   157
                                              palette->colors, 0,
slouken@1907
   158
                                              palette->ncolors);
slouken@1907
   159
        }
slouken@1907
   160
    }
slouken@1907
   161
    return 0;
slouken@1907
   162
}
slouken@1907
   163
slouken@1895
   164
SDL_Renderer *
slouken@1918
   165
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   166
{
slouken@1895
   167
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   168
    SDL_DisplayMode *displayMode = &display->current_mode;
slouken@1895
   169
    SDL_Renderer *renderer;
slouken@1918
   170
    SW_RenderData *data;
slouken@1895
   171
    int i, n;
slouken@1895
   172
    int bpp;
slouken@1895
   173
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1907
   174
    Uint32 renderer_flags;
slouken@1929
   175
    const char *desired_driver;
slouken@1895
   176
slouken@1895
   177
    if (!SDL_PixelFormatEnumToMasks
slouken@1895
   178
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   179
        SDL_SetError("Unknown display format");
slouken@1895
   180
        return NULL;
slouken@1895
   181
    }
slouken@1895
   182
slouken@1895
   183
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   184
    if (!renderer) {
slouken@1895
   185
        SDL_OutOfMemory();
slouken@1895
   186
        return NULL;
slouken@1895
   187
    }
slouken@1895
   188
slouken@1920
   189
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   190
    if (!data) {
slouken@1918
   191
        SW_DestroyRenderer(renderer);
slouken@1895
   192
        SDL_OutOfMemory();
slouken@1895
   193
        return NULL;
slouken@1895
   194
    }
slouken@1970
   195
    renderer->ActivateRenderer = SW_ActivateRenderer;
slouken@1970
   196
    renderer->DisplayModeChanged = SW_DisplayModeChanged;
slouken@1918
   197
    renderer->CreateTexture = SW_CreateTexture;
slouken@1918
   198
    renderer->QueryTexturePixels = SW_QueryTexturePixels;
slouken@1918
   199
    renderer->SetTexturePalette = SW_SetTexturePalette;
slouken@1918
   200
    renderer->GetTexturePalette = SW_GetTexturePalette;
slouken@1985
   201
    renderer->SetTextureColorMod = SW_SetTextureColorMod;
slouken@1985
   202
    renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
slouken@1985
   203
    renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
slouken@1985
   204
    renderer->SetTextureScaleMode = SW_SetTextureScaleMode;
slouken@1918
   205
    renderer->UpdateTexture = SW_UpdateTexture;
slouken@1918
   206
    renderer->LockTexture = SW_LockTexture;
slouken@1918
   207
    renderer->UnlockTexture = SW_UnlockTexture;
slouken@1918
   208
    renderer->DirtyTexture = SW_DirtyTexture;
slouken@1918
   209
    renderer->RenderFill = SW_RenderFill;
slouken@1918
   210
    renderer->RenderCopy = SW_RenderCopy;
slouken@1918
   211
    renderer->RenderPresent = SW_RenderPresent;
slouken@1918
   212
    renderer->DestroyTexture = SW_DestroyTexture;
slouken@1918
   213
    renderer->DestroyRenderer = SW_DestroyRenderer;
slouken@1918
   214
    renderer->info = SW_RenderDriver.info;
slouken@1895
   215
    renderer->window = window->id;
slouken@1895
   216
    renderer->driverdata = data;
slouken@1895
   217
slouken@1907
   218
    renderer->info.flags = 0;
slouken@1895
   219
slouken@1965
   220
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1965
   221
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
slouken@1895
   222
        n = 2;
slouken@1965
   223
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1965
   224
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
slouken@1895
   225
        n = 3;
slouken@1895
   226
    } else {
slouken@1965
   227
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
slouken@1895
   228
        n = 1;
slouken@1895
   229
    }
slouken@1907
   230
    data->format = displayMode->format;
slouken@1895
   231
slouken@1895
   232
    /* Find a render driver that we can use to display data */
slouken@1965
   233
    renderer_flags = (SDL_RENDERER_SINGLEBUFFER |
slouken@1965
   234
                      SDL_RENDERER_PRESENTDISCARD);
slouken@1965
   235
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   236
        renderer_flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   237
    }
slouken@1929
   238
    desired_driver = SDL_getenv("SDL_VIDEO_RENDERER_SWDRIVER");
slouken@1895
   239
    for (i = 0; i < display->num_render_drivers; ++i) {
slouken@1895
   240
        SDL_RenderDriver *driver = &display->render_drivers[i];
slouken@1929
   241
        if (driver->info.name == SW_RenderDriver.info.name) {
slouken@1929
   242
            continue;
slouken@1929
   243
        }
slouken@1929
   244
        if (desired_driver
slouken@1929
   245
            && SDL_strcasecmp(desired_driver, driver->info.name) != 0) {
slouken@1929
   246
            continue;
slouken@1929
   247
        }
slouken@1929
   248
        data->renderer = driver->CreateRenderer(window, renderer_flags);
slouken@1929
   249
        if (data->renderer) {
slouken@1929
   250
            break;
slouken@1895
   251
        }
slouken@1895
   252
    }
slouken@1895
   253
    if (i == display->num_render_drivers) {
slouken@1918
   254
        SW_DestroyRenderer(renderer);
slouken@1895
   255
        SDL_SetError("Couldn't find display render driver");
slouken@1895
   256
        return NULL;
slouken@1895
   257
    }
slouken@1965
   258
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   259
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   260
    }
slouken@1907
   261
slouken@1907
   262
    /* Create the textures we'll use for display */
slouken@1907
   263
    for (i = 0; i < n; ++i) {
slouken@1907
   264
        data->texture[i] =
slouken@1907
   265
            CreateTexture(data->renderer, data->format, window->w, window->h);
slouken@1907
   266
        if (!data->texture[i]) {
slouken@1918
   267
            SW_DestroyRenderer(renderer);
slouken@1907
   268
            return NULL;
slouken@1907
   269
        }
slouken@1907
   270
    }
slouken@1907
   271
    data->current_texture = 0;
slouken@1907
   272
slouken@1907
   273
    /* Create a surface we'll use for rendering */
slouken@1907
   274
    data->surface.flags = SDL_PREALLOC;
slouken@1907
   275
    data->surface.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1907
   276
    if (!data->surface.format) {
slouken@1918
   277
        SW_DestroyRenderer(renderer);
slouken@1907
   278
        return NULL;
slouken@1907
   279
    }
slouken@1907
   280
    SDL_SetSurfacePalette(&data->surface, display->palette);
slouken@1907
   281
slouken@1907
   282
    /* Set up a palette watch on the display palette */
slouken@1907
   283
    if (display->palette) {
slouken@1907
   284
        SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
slouken@1907
   285
    }
slouken@1907
   286
slouken@1895
   287
    return renderer;
slouken@1895
   288
}
slouken@1895
   289
slouken@1895
   290
static int
slouken@1970
   291
SW_ActivateRenderer(SDL_Renderer * renderer)
slouken@1970
   292
{
slouken@1970
   293
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1972
   294
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1972
   295
    int i, n;
slouken@1970
   296
slouken@1970
   297
    if (data->renderer && data->renderer->ActivateRenderer) {
slouken@1970
   298
        if (data->renderer->ActivateRenderer(data->renderer) < 0) {
slouken@1970
   299
            return -1;
slouken@1970
   300
        }
slouken@1970
   301
    }
slouken@1972
   302
    if (data->updateSize) {
slouken@1972
   303
        /* Recreate the textures for the new window size */
slouken@1972
   304
        if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1972
   305
            n = 2;
slouken@1972
   306
        } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1972
   307
            n = 3;
slouken@1972
   308
        } else {
slouken@1972
   309
            n = 1;
slouken@1972
   310
        }
slouken@1972
   311
        for (i = 0; i < n; ++i) {
slouken@1972
   312
            if (data->texture[i]) {
slouken@1972
   313
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1972
   314
                data->texture[i] = 0;
slouken@1972
   315
            }
slouken@1972
   316
        }
slouken@1972
   317
        for (i = 0; i < n; ++i) {
slouken@1972
   318
            data->texture[i] =
slouken@1972
   319
                CreateTexture(data->renderer, data->format, window->w,
slouken@1972
   320
                              window->h);
slouken@1972
   321
            if (!data->texture[i]) {
slouken@1972
   322
                return -1;
slouken@1972
   323
            }
slouken@1972
   324
        }
slouken@1972
   325
        data->updateSize = SDL_FALSE;
slouken@1972
   326
    }
slouken@1970
   327
    return 0;
slouken@1970
   328
}
slouken@1970
   329
slouken@1970
   330
static int
slouken@1970
   331
SW_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   332
{
slouken@1970
   333
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
   334
slouken@1970
   335
    if (data->renderer && data->renderer->DisplayModeChanged) {
slouken@1970
   336
        if (data->renderer->DisplayModeChanged(data->renderer) < 0) {
slouken@1970
   337
            return -1;
slouken@1970
   338
        }
slouken@1970
   339
    }
slouken@1972
   340
    data->updateSize = SDL_TRUE;
slouken@1970
   341
    return 0;
slouken@1970
   342
}
slouken@1970
   343
slouken@1970
   344
static int
slouken@1918
   345
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   346
{
slouken@1895
   347
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   348
        texture->driverdata = SDL_SW_CreateYUVTexture(texture);
slouken@1895
   349
    } else {
slouken@1985
   350
        SDL_Surface *surface;
slouken@1895
   351
        int bpp;
slouken@1895
   352
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   353
slouken@1895
   354
        if (!SDL_PixelFormatEnumToMasks
slouken@1895
   355
            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   356
            SDL_SetError("Unknown texture format");
slouken@1895
   357
            return -1;
slouken@1895
   358
        }
slouken@1895
   359
slouken@1895
   360
        texture->driverdata =
slouken@1895
   361
            SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@1895
   362
                                 Bmask, Amask);
slouken@1895
   363
    }
slouken@1895
   364
slouken@1895
   365
    if (!texture->driverdata) {
slouken@1895
   366
        return -1;
slouken@1895
   367
    }
slouken@1895
   368
    return 0;
slouken@1895
   369
}
slouken@1895
   370
slouken@1895
   371
static int
slouken@1918
   372
SW_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   373
                      void **pixels, int *pitch)
slouken@1895
   374
{
slouken@1895
   375
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   376
        return SDL_SW_QueryYUVTexturePixels((SDL_SW_YUVTexture *) texture->
slouken@1895
   377
                                            driverdata, pixels, pitch);
slouken@1895
   378
    } else {
slouken@1895
   379
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   380
slouken@1895
   381
        *pixels = surface->pixels;
slouken@1895
   382
        *pitch = surface->pitch;
slouken@1895
   383
        return 0;
slouken@1895
   384
    }
slouken@1895
   385
}
slouken@1895
   386
slouken@1895
   387
static int
slouken@1918
   388
SW_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   389
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   390
{
slouken@1895
   391
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   392
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   393
        return -1;
slouken@1895
   394
    } else {
slouken@1895
   395
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   396
slouken@1895
   397
        return SDL_SetPaletteColors(surface->format->palette, colors,
slouken@1895
   398
                                    firstcolor, ncolors);
slouken@1895
   399
    }
slouken@1895
   400
}
slouken@1895
   401
slouken@1895
   402
static int
slouken@1918
   403
SW_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   404
                     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
        SDL_memcpy(colors, &surface->format->palette->colors[firstcolor],
slouken@1895
   413
                   ncolors * sizeof(*colors));
slouken@1895
   414
        return 0;
slouken@1895
   415
    }
slouken@1895
   416
}
slouken@1895
   417
slouken@1985
   418
static void
slouken@1985
   419
SW_UpdateRenderCopyFunc(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   420
{
slouken@1985
   421
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1985
   422
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1985
   423
slouken@1985
   424
    surface->userdata =
slouken@1985
   425
        SDL_GetRenderCopyFunc(texture->format, data->format, texture->modMode,
slouken@1985
   426
                              texture->blendMode, texture->scaleMode);
slouken@1985
   427
}
slouken@1985
   428
slouken@1985
   429
static int
slouken@1985
   430
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   431
{
slouken@1985
   432
    SW_UpdateRenderCopyFunc(renderer, texture);
slouken@1985
   433
    return 0;
slouken@1985
   434
}
slouken@1985
   435
slouken@1985
   436
static int
slouken@1985
   437
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   438
{
slouken@1985
   439
    SW_UpdateRenderCopyFunc(renderer, texture);
slouken@1985
   440
    return 0;
slouken@1985
   441
}
slouken@1985
   442
slouken@1985
   443
static int
slouken@1985
   444
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   445
{
slouken@1985
   446
    switch (texture->blendMode) {
slouken@1985
   447
    case SDL_TEXTUREBLENDMODE_NONE:
slouken@1985
   448
    case SDL_TEXTUREBLENDMODE_MASK:
slouken@1985
   449
    case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1985
   450
    case SDL_TEXTUREBLENDMODE_ADD:
slouken@1985
   451
    case SDL_TEXTUREBLENDMODE_MOD:
slouken@1985
   452
        SW_UpdateRenderCopyFunc(renderer, texture);
slouken@1985
   453
        return 0;
slouken@1985
   454
    default:
slouken@1985
   455
        SDL_Unsupported();
slouken@1985
   456
        texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
slouken@1985
   457
        SW_UpdateRenderCopyFunc(renderer, texture);
slouken@1985
   458
        return -1;
slouken@1985
   459
    }
slouken@1985
   460
}
slouken@1985
   461
slouken@1985
   462
static int
slouken@1985
   463
SW_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   464
{
slouken@1985
   465
    switch (texture->scaleMode) {
slouken@1985
   466
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1985
   467
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1985
   468
        SW_UpdateRenderCopyFunc(renderer, texture);
slouken@1985
   469
        return 0;
slouken@1985
   470
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1985
   471
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1985
   472
        SDL_Unsupported();
slouken@1985
   473
        texture->scaleMode = SDL_TEXTURESCALEMODE_FAST;
slouken@1985
   474
        SW_UpdateRenderCopyFunc(renderer, texture);
slouken@1985
   475
        return -1;
slouken@1985
   476
    default:
slouken@1985
   477
        SDL_Unsupported();
slouken@1985
   478
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1985
   479
        SW_UpdateRenderCopyFunc(renderer, texture);
slouken@1985
   480
        return -1;
slouken@1985
   481
    }
slouken@1985
   482
}
slouken@1985
   483
slouken@1895
   484
static int
slouken@1918
   485
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   486
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   487
{
slouken@1895
   488
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   489
        return SDL_SW_UpdateYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   490
                                       driverdata, rect, pixels, pitch);
slouken@1895
   491
    } else {
slouken@1895
   492
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   493
        Uint8 *src, *dst;
slouken@1895
   494
        int row;
slouken@1895
   495
        size_t length;
slouken@1895
   496
slouken@1895
   497
        src = (Uint8 *) pixels;
slouken@1895
   498
        dst =
slouken@1895
   499
            (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   500
            rect->x * surface->format->BytesPerPixel;
slouken@1895
   501
        length = rect->w * surface->format->BytesPerPixel;
slouken@1895
   502
        for (row = 0; row < rect->h; ++row) {
slouken@1895
   503
            SDL_memcpy(dst, src, length);
slouken@1895
   504
            src += pitch;
slouken@1895
   505
            dst += surface->pitch;
slouken@1895
   506
        }
slouken@1895
   507
        return 0;
slouken@1895
   508
    }
slouken@1895
   509
}
slouken@1895
   510
slouken@1895
   511
static int
slouken@1918
   512
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   513
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   514
               int *pitch)
slouken@1895
   515
{
slouken@1895
   516
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   517
        return SDL_SW_LockYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   518
                                     driverdata, rect, markDirty, pixels,
slouken@1895
   519
                                     pitch);
slouken@1895
   520
    } else {
slouken@1895
   521
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   522
slouken@1895
   523
        *pixels =
slouken@1895
   524
            (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   525
                      rect->x * surface->format->BytesPerPixel);
slouken@1895
   526
        *pitch = surface->pitch;
slouken@1895
   527
        return 0;
slouken@1895
   528
    }
slouken@1895
   529
}
slouken@1895
   530
slouken@1895
   531
static void
slouken@1918
   532
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   533
{
slouken@1895
   534
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   535
        SDL_SW_UnlockYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   536
    }
slouken@1895
   537
}
slouken@1895
   538
slouken@1895
   539
static void
slouken@1918
   540
SW_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   541
                int numrects, const SDL_Rect * rects)
slouken@1895
   542
{
slouken@1895
   543
}
slouken@1895
   544
slouken@1895
   545
static int
slouken@1985
   546
SW_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
slouken@1985
   547
              const SDL_Rect * rect)
slouken@1895
   548
{
slouken@1918
   549
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1985
   550
    Uint32 color;
slouken@1907
   551
    SDL_Rect real_rect;
slouken@1907
   552
    int status;
slouken@1895
   553
slouken@1965
   554
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   555
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1897
   556
    }
slouken@1895
   557
slouken@1907
   558
    color = SDL_MapRGBA(data->surface.format, r, g, b, a);
slouken@1895
   559
slouken@1907
   560
    if (data->renderer->
slouken@1907
   561
        LockTexture(data->renderer, data->texture[data->current_texture],
slouken@1907
   562
                    rect, 1, &data->surface.pixels,
slouken@1907
   563
                    &data->surface.pitch) < 0) {
slouken@1907
   564
        return -1;
slouken@1907
   565
    }
slouken@1907
   566
    data->surface.w = rect->w;
slouken@1907
   567
    data->surface.h = rect->h;
slouken@1907
   568
    data->surface.clip_rect.w = rect->w;
slouken@1907
   569
    data->surface.clip_rect.h = rect->h;
slouken@1907
   570
    real_rect = data->surface.clip_rect;
slouken@1907
   571
slouken@1907
   572
    status = SDL_FillRect(&data->surface, &real_rect, color);
slouken@1907
   573
slouken@1907
   574
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   575
                                  data->texture[data->current_texture]);
slouken@1907
   576
    return status;
slouken@1895
   577
}
slouken@1895
   578
slouken@1895
   579
static int
slouken@1918
   580
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   581
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   582
{
slouken@1918
   583
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   584
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   585
    int status;
slouken@1895
   586
slouken@1965
   587
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   588
        SDL_AddDirtyRect(&data->dirty, dstrect);
slouken@1897
   589
    }
slouken@1895
   590
slouken@1907
   591
    if (data->renderer->
slouken@1907
   592
        LockTexture(data->renderer, data->texture[data->current_texture],
slouken@1907
   593
                    dstrect, 1, &data->surface.pixels,
slouken@1907
   594
                    &data->surface.pitch) < 0) {
slouken@1907
   595
        return -1;
slouken@1907
   596
    }
slouken@1907
   597
slouken@1895
   598
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1907
   599
        status =
slouken@1907
   600
            SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
slouken@1907
   601
                                srcrect, data->format, dstrect->w, dstrect->h,
slouken@1907
   602
                                data->surface.pixels, data->surface.pitch);
slouken@1895
   603
    } else {
slouken@1895
   604
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1985
   605
        SDL_RenderCopyFunc copyfunc = (SDL_RenderCopyFunc) surface->userdata;
slouken@1907
   606
slouken@1985
   607
        if (copyfunc) {
slouken@1985
   608
            SDL_RenderCopyData copydata;
slouken@1895
   609
slouken@1985
   610
            copydata.src =
slouken@1985
   611
                (Uint8 *) surface->pixels + srcrect->y * surface->pitch +
slouken@1985
   612
                srcrect->x * surface->format->BytesPerPixel;
slouken@1985
   613
            copydata.src_w = srcrect->w;
slouken@1985
   614
            copydata.src_h = srcrect->h;
slouken@1985
   615
            copydata.src_pitch = surface->pitch;
slouken@1985
   616
            copydata.dst = (Uint8 *) data->surface.pixels;
slouken@1985
   617
            copydata.dst_w = dstrect->w;
slouken@1985
   618
            copydata.dst_h = dstrect->h;
slouken@1985
   619
            copydata.dst_pitch = data->surface.pitch;
slouken@1985
   620
            copydata.flags = 0;
slouken@1985
   621
            if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
slouken@1985
   622
                copydata.flags |= SDL_RENDERCOPY_MODULATE_COLOR;
slouken@1985
   623
                copydata.r = texture->r;
slouken@1985
   624
                copydata.g = texture->g;
slouken@1985
   625
                copydata.b = texture->b;
slouken@1985
   626
            }
slouken@1985
   627
            if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
slouken@1985
   628
                copydata.flags |= SDL_RENDERCOPY_MODULATE_ALPHA;
slouken@1985
   629
                copydata.a = texture->a;
slouken@1985
   630
            }
slouken@1985
   631
            if (texture->
slouken@1985
   632
                blendMode & (SDL_TEXTUREBLENDMODE_MASK |
slouken@1985
   633
                             SDL_TEXTUREBLENDMODE_BLEND)) {
slouken@1985
   634
                copydata.flags |= SDL_RENDERCOPY_BLEND;
slouken@1985
   635
            } else if (texture->blendMode & SDL_TEXTUREBLENDMODE_ADD) {
slouken@1985
   636
                copydata.flags |= SDL_RENDERCOPY_ADD;
slouken@1985
   637
            } else if (texture->blendMode & SDL_TEXTUREBLENDMODE_MOD) {
slouken@1985
   638
                copydata.flags |= SDL_RENDERCOPY_MOD;
slouken@1985
   639
            }
slouken@1985
   640
            if (texture->scaleMode) {
slouken@1985
   641
                copydata.flags |= SDL_RENDERCOPY_NEAREST;
slouken@1985
   642
            }
slouken@1985
   643
            status = copyfunc(&copydata);
slouken@1895
   644
        } else {
slouken@1985
   645
            SDL_Rect real_srcrect = *srcrect;
slouken@1985
   646
            SDL_Rect real_dstrect;
slouken@1985
   647
slouken@1985
   648
            data->surface.w = dstrect->w;
slouken@1985
   649
            data->surface.h = dstrect->h;
slouken@1985
   650
            data->surface.clip_rect.w = dstrect->w;
slouken@1985
   651
            data->surface.clip_rect.h = dstrect->h;
slouken@1985
   652
            real_dstrect = data->surface.clip_rect;
slouken@1985
   653
slouken@1907
   654
            status =
slouken@1907
   655
                SDL_LowerBlit(surface, &real_srcrect, &data->surface,
slouken@1907
   656
                              &real_dstrect);
slouken@1895
   657
        }
slouken@1895
   658
    }
slouken@1907
   659
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   660
                                  data->texture[data->current_texture]);
slouken@1907
   661
    return status;
slouken@1895
   662
}
slouken@1895
   663
slouken@1895
   664
static void
slouken@1918
   665
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   666
{
slouken@1918
   667
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   668
    SDL_Texture *texture = data->texture[data->current_texture];
slouken@1895
   669
slouken@1895
   670
    /* Send the data to the display */
slouken@1965
   671
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1907
   672
        SDL_DirtyRect *dirty;
slouken@1907
   673
        for (dirty = data->dirty.list; dirty; dirty = dirty->next) {
slouken@1907
   674
            data->renderer->RenderCopy(data->renderer, texture, &dirty->rect,
slouken@1985
   675
                                       &dirty->rect);
slouken@1907
   676
        }
slouken@1907
   677
        SDL_ClearDirtyRects(&data->dirty);
slouken@1907
   678
    } else {
slouken@1907
   679
        SDL_Rect rect;
slouken@1907
   680
        rect.x = 0;
slouken@1907
   681
        rect.y = 0;
slouken@1907
   682
        rect.w = texture->w;
slouken@1907
   683
        rect.h = texture->h;
slouken@1985
   684
        data->renderer->RenderCopy(data->renderer, texture, &rect, &rect);
slouken@1895
   685
    }
slouken@1895
   686
    data->renderer->RenderPresent(data->renderer);
slouken@1895
   687
slouken@1895
   688
    /* Update the flipping chain, if any */
slouken@1965
   689
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1907
   690
        data->current_texture = (data->current_texture + 1) % 2;
slouken@1965
   691
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1907
   692
        data->current_texture = (data->current_texture + 1) % 3;
slouken@1895
   693
    }
slouken@1895
   694
}
slouken@1895
   695
slouken@1895
   696
static void
slouken@1918
   697
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   698
{
slouken@1895
   699
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   700
        SDL_SW_DestroyYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   701
    } else {
slouken@1895
   702
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   703
slouken@1895
   704
        SDL_FreeSurface(surface);
slouken@1895
   705
    }
slouken@1895
   706
}
slouken@1895
   707
slouken@1895
   708
static void
slouken@1918
   709
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   710
{
slouken@1918
   711
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   712
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   713
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   714
    int i;
slouken@1895
   715
slouken@1895
   716
    if (data) {
slouken@1907
   717
        for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   718
            if (data->texture[i]) {
slouken@1907
   719
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1895
   720
            }
slouken@1895
   721
        }
slouken@1907
   722
        if (data->surface.format) {
slouken@1907
   723
            SDL_SetSurfacePalette(&data->surface, NULL);
slouken@1907
   724
            SDL_FreeFormat(data->surface.format);
slouken@1907
   725
        }
slouken@1907
   726
        if (display->palette) {
slouken@1907
   727
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
slouken@1907
   728
                                data);
slouken@1907
   729
        }
slouken@1907
   730
        if (data->renderer) {
slouken@1907
   731
            data->renderer->DestroyRenderer(data->renderer);
slouken@1907
   732
        }
slouken@1895
   733
        SDL_FreeDirtyRects(&data->dirty);
slouken@1895
   734
        SDL_free(data);
slouken@1895
   735
    }
slouken@1895
   736
    SDL_free(renderer);
slouken@1895
   737
}
slouken@1895
   738
slouken@1895
   739
/* vi: set ts=4 sw=4 expandtab: */