src/video/SDL_renderer_sw.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 05 Aug 2006 17:09:42 +0000
changeset 1965 a788656ca29a
parent 1930 9483df98e011
child 1970 db3ba6c0d0df
permissions -rw-r--r--
SDL constants are all uppercase.
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@1918
    34
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    35
static int SW_QueryTexturePixels(SDL_Renderer * renderer,
slouken@1918
    36
                                 SDL_Texture * texture, void **pixels,
slouken@1918
    37
                                 int *pitch);
slouken@1918
    38
static int SW_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    39
                                SDL_Texture * texture,
slouken@1918
    40
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    41
                                int ncolors);
slouken@1918
    42
static int SW_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    43
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    44
                                int firstcolor, int ncolors);
slouken@1918
    45
static int SW_UpdateTexture(SDL_Renderer * renderer,
slouken@1918
    46
                            SDL_Texture * texture, const SDL_Rect * rect,
slouken@1918
    47
                            const void *pixels, int pitch);
slouken@1918
    48
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    49
                          const SDL_Rect * rect, int markDirty,
slouken@1918
    50
                          void **pixels, int *pitch);
slouken@1918
    51
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    52
static void SW_DirtyTexture(SDL_Renderer * renderer,
slouken@1918
    53
                            SDL_Texture * texture, int numrects,
slouken@1918
    54
                            const SDL_Rect * rects);
slouken@1918
    55
static int SW_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1918
    56
                         Uint32 color);
slouken@1918
    57
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    58
                         const SDL_Rect * srcrect,
slouken@1918
    59
                         const SDL_Rect * dstrect, int blendMode,
slouken@1918
    60
                         int scaleMode);
slouken@1918
    61
static void SW_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    62
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    63
static void SW_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    64
slouken@1895
    65
slouken@1918
    66
SDL_RenderDriver SW_RenderDriver = {
slouken@1918
    67
    SW_CreateRenderer,
slouken@1895
    68
    {
slouken@1895
    69
     "software",
slouken@1965
    70
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
slouken@1965
    71
      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
slouken@1965
    72
      SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC),
slouken@1965
    73
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1965
    74
      SDL_TEXTUREBLENDMODE_BLEND),
slouken@1965
    75
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST),
slouken@1895
    76
     11,
slouken@1895
    77
     {
slouken@1965
    78
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    79
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    80
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    81
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    82
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    83
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    84
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
    85
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
    86
      SDL_PIXELFORMAT_BGRA8888,
slouken@1965
    87
      SDL_PIXELFORMAT_YUY2,
slouken@1965
    88
      SDL_PIXELFORMAT_UYVY},
slouken@1895
    89
     0,
slouken@1895
    90
     0}
slouken@1895
    91
};
slouken@1895
    92
slouken@1895
    93
typedef struct
slouken@1895
    94
{
slouken@1907
    95
    Uint32 format;
slouken@1907
    96
    int current_texture;
slouken@1907
    97
    SDL_Texture *texture[3];
slouken@1907
    98
    SDL_Surface surface;
slouken@1895
    99
    SDL_Renderer *renderer;
slouken@1895
   100
    SDL_DirtyRectList dirty;
slouken@1918
   101
} SW_RenderData;
slouken@1895
   102
slouken@1907
   103
static SDL_Texture *
slouken@1907
   104
CreateTexture(SDL_Renderer * renderer, Uint32 format, int w, int h)
slouken@1907
   105
{
slouken@1907
   106
    SDL_Texture *texture;
slouken@1907
   107
slouken@1920
   108
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@1907
   109
    if (!texture) {
slouken@1907
   110
        SDL_OutOfMemory();
slouken@1907
   111
        return NULL;
slouken@1907
   112
    }
slouken@1907
   113
slouken@1907
   114
    texture->format = format;
slouken@1965
   115
    texture->access = SDL_TEXTUREACCESS_LOCAL;
slouken@1907
   116
    texture->w = w;
slouken@1907
   117
    texture->h = h;
slouken@1907
   118
    texture->renderer = renderer;
slouken@1907
   119
slouken@1907
   120
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@1907
   121
        SDL_free(texture);
slouken@1907
   122
        return NULL;
slouken@1907
   123
    }
slouken@1907
   124
    return texture;
slouken@1907
   125
}
slouken@1907
   126
slouken@1907
   127
static void
slouken@1907
   128
DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1907
   129
{
slouken@1907
   130
    renderer->DestroyTexture(renderer, texture);
slouken@1907
   131
    SDL_free(texture);
slouken@1907
   132
}
slouken@1907
   133
slouken@1907
   134
static int
slouken@1907
   135
DisplayPaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1907
   136
{
slouken@1918
   137
    SW_RenderData *data = (SW_RenderData *) userdata;
slouken@1907
   138
    int i;
slouken@1907
   139
slouken@1907
   140
    for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   141
        if (data->texture[i] && data->renderer->SetTexturePalette) {
slouken@1907
   142
            data->renderer->SetTexturePalette(data->renderer,
slouken@1907
   143
                                              data->texture[i],
slouken@1907
   144
                                              palette->colors, 0,
slouken@1907
   145
                                              palette->ncolors);
slouken@1907
   146
        }
slouken@1907
   147
    }
slouken@1907
   148
    return 0;
slouken@1907
   149
}
slouken@1907
   150
slouken@1895
   151
SDL_Renderer *
slouken@1918
   152
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   153
{
slouken@1895
   154
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   155
    SDL_DisplayMode *displayMode = &display->current_mode;
slouken@1895
   156
    SDL_Renderer *renderer;
slouken@1918
   157
    SW_RenderData *data;
slouken@1895
   158
    int i, n;
slouken@1895
   159
    int bpp;
slouken@1895
   160
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1907
   161
    Uint32 renderer_flags;
slouken@1929
   162
    const char *desired_driver;
slouken@1895
   163
slouken@1895
   164
    if (!SDL_PixelFormatEnumToMasks
slouken@1895
   165
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   166
        SDL_SetError("Unknown display format");
slouken@1895
   167
        return NULL;
slouken@1895
   168
    }
slouken@1895
   169
slouken@1895
   170
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   171
    if (!renderer) {
slouken@1895
   172
        SDL_OutOfMemory();
slouken@1895
   173
        return NULL;
slouken@1895
   174
    }
slouken@1895
   175
slouken@1920
   176
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   177
    if (!data) {
slouken@1918
   178
        SW_DestroyRenderer(renderer);
slouken@1895
   179
        SDL_OutOfMemory();
slouken@1895
   180
        return NULL;
slouken@1895
   181
    }
slouken@1895
   182
slouken@1918
   183
    renderer->CreateTexture = SW_CreateTexture;
slouken@1918
   184
    renderer->QueryTexturePixels = SW_QueryTexturePixels;
slouken@1918
   185
    renderer->SetTexturePalette = SW_SetTexturePalette;
slouken@1918
   186
    renderer->GetTexturePalette = SW_GetTexturePalette;
slouken@1918
   187
    renderer->UpdateTexture = SW_UpdateTexture;
slouken@1918
   188
    renderer->LockTexture = SW_LockTexture;
slouken@1918
   189
    renderer->UnlockTexture = SW_UnlockTexture;
slouken@1918
   190
    renderer->DirtyTexture = SW_DirtyTexture;
slouken@1918
   191
    renderer->RenderFill = SW_RenderFill;
slouken@1918
   192
    renderer->RenderCopy = SW_RenderCopy;
slouken@1918
   193
    renderer->RenderPresent = SW_RenderPresent;
slouken@1918
   194
    renderer->DestroyTexture = SW_DestroyTexture;
slouken@1918
   195
    renderer->DestroyRenderer = SW_DestroyRenderer;
slouken@1918
   196
    renderer->info = SW_RenderDriver.info;
slouken@1895
   197
    renderer->window = window->id;
slouken@1895
   198
    renderer->driverdata = data;
slouken@1895
   199
slouken@1907
   200
    renderer->info.flags = 0;
slouken@1895
   201
slouken@1965
   202
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1965
   203
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
slouken@1895
   204
        n = 2;
slouken@1965
   205
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1965
   206
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
slouken@1895
   207
        n = 3;
slouken@1895
   208
    } else {
slouken@1965
   209
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
slouken@1895
   210
        n = 1;
slouken@1895
   211
    }
slouken@1907
   212
    data->format = displayMode->format;
slouken@1895
   213
slouken@1895
   214
    /* Find a render driver that we can use to display data */
slouken@1965
   215
    renderer_flags = (SDL_RENDERER_SINGLEBUFFER |
slouken@1965
   216
                      SDL_RENDERER_PRESENTDISCARD);
slouken@1965
   217
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   218
        renderer_flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   219
    }
slouken@1929
   220
    desired_driver = SDL_getenv("SDL_VIDEO_RENDERER_SWDRIVER");
slouken@1895
   221
    for (i = 0; i < display->num_render_drivers; ++i) {
slouken@1895
   222
        SDL_RenderDriver *driver = &display->render_drivers[i];
slouken@1929
   223
        if (driver->info.name == SW_RenderDriver.info.name) {
slouken@1929
   224
            continue;
slouken@1929
   225
        }
slouken@1929
   226
        if (desired_driver
slouken@1929
   227
            && SDL_strcasecmp(desired_driver, driver->info.name) != 0) {
slouken@1929
   228
            continue;
slouken@1929
   229
        }
slouken@1929
   230
        data->renderer = driver->CreateRenderer(window, renderer_flags);
slouken@1929
   231
        if (data->renderer) {
slouken@1929
   232
            break;
slouken@1895
   233
        }
slouken@1895
   234
    }
slouken@1895
   235
    if (i == display->num_render_drivers) {
slouken@1918
   236
        SW_DestroyRenderer(renderer);
slouken@1895
   237
        SDL_SetError("Couldn't find display render driver");
slouken@1895
   238
        return NULL;
slouken@1895
   239
    }
slouken@1965
   240
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   241
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   242
    }
slouken@1907
   243
slouken@1907
   244
    /* Create the textures we'll use for display */
slouken@1907
   245
    for (i = 0; i < n; ++i) {
slouken@1907
   246
        data->texture[i] =
slouken@1907
   247
            CreateTexture(data->renderer, data->format, window->w, window->h);
slouken@1907
   248
        if (!data->texture[i]) {
slouken@1918
   249
            SW_DestroyRenderer(renderer);
slouken@1907
   250
            return NULL;
slouken@1907
   251
        }
slouken@1907
   252
    }
slouken@1907
   253
    data->current_texture = 0;
slouken@1907
   254
slouken@1907
   255
    /* Create a surface we'll use for rendering */
slouken@1907
   256
    data->surface.flags = SDL_PREALLOC;
slouken@1907
   257
    data->surface.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1907
   258
    if (!data->surface.format) {
slouken@1918
   259
        SW_DestroyRenderer(renderer);
slouken@1907
   260
        return NULL;
slouken@1907
   261
    }
slouken@1907
   262
    SDL_SetSurfacePalette(&data->surface, display->palette);
slouken@1907
   263
slouken@1907
   264
    /* Set up a palette watch on the display palette */
slouken@1907
   265
    if (display->palette) {
slouken@1907
   266
        SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
slouken@1907
   267
    }
slouken@1907
   268
slouken@1895
   269
    return renderer;
slouken@1895
   270
}
slouken@1895
   271
slouken@1895
   272
static int
slouken@1918
   273
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   274
{
slouken@1895
   275
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   276
        texture->driverdata = SDL_SW_CreateYUVTexture(texture);
slouken@1895
   277
    } else {
slouken@1895
   278
        int bpp;
slouken@1895
   279
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   280
slouken@1895
   281
        if (!SDL_PixelFormatEnumToMasks
slouken@1895
   282
            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   283
            SDL_SetError("Unknown texture format");
slouken@1895
   284
            return -1;
slouken@1895
   285
        }
slouken@1895
   286
slouken@1895
   287
        texture->driverdata =
slouken@1895
   288
            SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@1895
   289
                                 Bmask, Amask);
slouken@1895
   290
    }
slouken@1895
   291
slouken@1895
   292
    if (!texture->driverdata) {
slouken@1895
   293
        return -1;
slouken@1895
   294
    }
slouken@1895
   295
    return 0;
slouken@1895
   296
}
slouken@1895
   297
slouken@1895
   298
static int
slouken@1918
   299
SW_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   300
                      void **pixels, int *pitch)
slouken@1895
   301
{
slouken@1895
   302
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   303
        return SDL_SW_QueryYUVTexturePixels((SDL_SW_YUVTexture *) texture->
slouken@1895
   304
                                            driverdata, pixels, pitch);
slouken@1895
   305
    } else {
slouken@1895
   306
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   307
slouken@1895
   308
        *pixels = surface->pixels;
slouken@1895
   309
        *pitch = surface->pitch;
slouken@1895
   310
        return 0;
slouken@1895
   311
    }
slouken@1895
   312
}
slouken@1895
   313
slouken@1895
   314
static int
slouken@1918
   315
SW_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   316
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   317
{
slouken@1895
   318
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   319
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   320
        return -1;
slouken@1895
   321
    } else {
slouken@1895
   322
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   323
slouken@1895
   324
        return SDL_SetPaletteColors(surface->format->palette, colors,
slouken@1895
   325
                                    firstcolor, ncolors);
slouken@1895
   326
    }
slouken@1895
   327
}
slouken@1895
   328
slouken@1895
   329
static int
slouken@1918
   330
SW_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   331
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   332
{
slouken@1895
   333
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   334
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   335
        return -1;
slouken@1895
   336
    } else {
slouken@1895
   337
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   338
slouken@1895
   339
        SDL_memcpy(colors, &surface->format->palette->colors[firstcolor],
slouken@1895
   340
                   ncolors * sizeof(*colors));
slouken@1895
   341
        return 0;
slouken@1895
   342
    }
slouken@1895
   343
}
slouken@1895
   344
slouken@1895
   345
static int
slouken@1918
   346
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   347
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   348
{
slouken@1895
   349
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   350
        return SDL_SW_UpdateYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   351
                                       driverdata, rect, pixels, pitch);
slouken@1895
   352
    } else {
slouken@1895
   353
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   354
        Uint8 *src, *dst;
slouken@1895
   355
        int row;
slouken@1895
   356
        size_t length;
slouken@1895
   357
slouken@1895
   358
        src = (Uint8 *) pixels;
slouken@1895
   359
        dst =
slouken@1895
   360
            (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   361
            rect->x * surface->format->BytesPerPixel;
slouken@1895
   362
        length = rect->w * surface->format->BytesPerPixel;
slouken@1895
   363
        for (row = 0; row < rect->h; ++row) {
slouken@1895
   364
            SDL_memcpy(dst, src, length);
slouken@1895
   365
            src += pitch;
slouken@1895
   366
            dst += surface->pitch;
slouken@1895
   367
        }
slouken@1895
   368
        return 0;
slouken@1895
   369
    }
slouken@1895
   370
}
slouken@1895
   371
slouken@1895
   372
static int
slouken@1918
   373
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   374
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   375
               int *pitch)
slouken@1895
   376
{
slouken@1895
   377
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   378
        return SDL_SW_LockYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   379
                                     driverdata, rect, markDirty, pixels,
slouken@1895
   380
                                     pitch);
slouken@1895
   381
    } else {
slouken@1895
   382
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   383
slouken@1895
   384
        *pixels =
slouken@1895
   385
            (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   386
                      rect->x * surface->format->BytesPerPixel);
slouken@1895
   387
        *pitch = surface->pitch;
slouken@1895
   388
        return 0;
slouken@1895
   389
    }
slouken@1895
   390
}
slouken@1895
   391
slouken@1895
   392
static void
slouken@1918
   393
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   394
{
slouken@1895
   395
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   396
        SDL_SW_UnlockYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   397
    }
slouken@1895
   398
}
slouken@1895
   399
slouken@1895
   400
static void
slouken@1918
   401
SW_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   402
                int numrects, const SDL_Rect * rects)
slouken@1895
   403
{
slouken@1895
   404
}
slouken@1895
   405
slouken@1895
   406
static int
slouken@1918
   407
SW_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1895
   408
{
slouken@1918
   409
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   410
    Uint8 r, g, b, a;
slouken@1907
   411
    SDL_Rect real_rect;
slouken@1907
   412
    int status;
slouken@1895
   413
slouken@1965
   414
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   415
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1897
   416
    }
slouken@1895
   417
slouken@1895
   418
    a = (Uint8) ((color >> 24) & 0xFF);
slouken@1895
   419
    r = (Uint8) ((color >> 16) & 0xFF);
slouken@1895
   420
    g = (Uint8) ((color >> 8) & 0xFF);
slouken@1895
   421
    b = (Uint8) (color & 0xFF);
slouken@1907
   422
    color = SDL_MapRGBA(data->surface.format, r, g, b, a);
slouken@1895
   423
slouken@1907
   424
    if (data->renderer->
slouken@1907
   425
        LockTexture(data->renderer, data->texture[data->current_texture],
slouken@1907
   426
                    rect, 1, &data->surface.pixels,
slouken@1907
   427
                    &data->surface.pitch) < 0) {
slouken@1907
   428
        return -1;
slouken@1907
   429
    }
slouken@1907
   430
    data->surface.w = rect->w;
slouken@1907
   431
    data->surface.h = rect->h;
slouken@1907
   432
    data->surface.clip_rect.w = rect->w;
slouken@1907
   433
    data->surface.clip_rect.h = rect->h;
slouken@1907
   434
    real_rect = data->surface.clip_rect;
slouken@1907
   435
slouken@1907
   436
    status = SDL_FillRect(&data->surface, &real_rect, color);
slouken@1907
   437
slouken@1907
   438
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   439
                                  data->texture[data->current_texture]);
slouken@1907
   440
    return status;
slouken@1895
   441
}
slouken@1895
   442
slouken@1895
   443
static int
slouken@1918
   444
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   445
              const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
   446
              int blendMode, int scaleMode)
slouken@1895
   447
{
slouken@1918
   448
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   449
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   450
    int status;
slouken@1895
   451
slouken@1965
   452
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   453
        SDL_AddDirtyRect(&data->dirty, dstrect);
slouken@1897
   454
    }
slouken@1895
   455
slouken@1907
   456
    if (data->renderer->
slouken@1907
   457
        LockTexture(data->renderer, data->texture[data->current_texture],
slouken@1907
   458
                    dstrect, 1, &data->surface.pixels,
slouken@1907
   459
                    &data->surface.pitch) < 0) {
slouken@1907
   460
        return -1;
slouken@1907
   461
    }
slouken@1907
   462
slouken@1895
   463
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1907
   464
        status =
slouken@1907
   465
            SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
slouken@1907
   466
                                srcrect, data->format, dstrect->w, dstrect->h,
slouken@1907
   467
                                data->surface.pixels, data->surface.pitch);
slouken@1895
   468
    } else {
slouken@1895
   469
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   470
        SDL_Rect real_srcrect = *srcrect;
slouken@1907
   471
        SDL_Rect real_dstrect;
slouken@1907
   472
slouken@1907
   473
        data->surface.w = dstrect->w;
slouken@1907
   474
        data->surface.h = dstrect->h;
slouken@1907
   475
        data->surface.clip_rect.w = dstrect->w;
slouken@1907
   476
        data->surface.clip_rect.h = dstrect->h;
slouken@1907
   477
        real_dstrect = data->surface.clip_rect;
slouken@1895
   478
slouken@1895
   479
        if (blendMode &
slouken@1965
   480
            (SDL_TEXTUREBLENDMODE_MASK | SDL_TEXTUREBLENDMODE_BLEND)) {
slouken@1895
   481
            SDL_SetAlpha(surface, SDL_SRCALPHA, 0);
slouken@1895
   482
        } else {
slouken@1895
   483
            SDL_SetAlpha(surface, 0, 0);
slouken@1895
   484
        }
slouken@1965
   485
        if (scaleMode != SDL_TEXTURESCALEMODE_NONE &&
slouken@1895
   486
            (srcrect->w != dstrect->w || srcrect->h != dstrect->h)) {
slouken@1907
   487
            status =
slouken@1907
   488
                SDL_SoftStretch(surface, &real_srcrect, &data->surface,
slouken@1907
   489
                                &real_dstrect);
slouken@1895
   490
        } else {
slouken@1907
   491
            status =
slouken@1907
   492
                SDL_LowerBlit(surface, &real_srcrect, &data->surface,
slouken@1907
   493
                              &real_dstrect);
slouken@1895
   494
        }
slouken@1895
   495
    }
slouken@1907
   496
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   497
                                  data->texture[data->current_texture]);
slouken@1907
   498
    return status;
slouken@1895
   499
}
slouken@1895
   500
slouken@1895
   501
static void
slouken@1918
   502
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   503
{
slouken@1918
   504
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   505
    SDL_Texture *texture = data->texture[data->current_texture];
slouken@1895
   506
slouken@1895
   507
    /* Send the data to the display */
slouken@1965
   508
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1907
   509
        SDL_DirtyRect *dirty;
slouken@1907
   510
        for (dirty = data->dirty.list; dirty; dirty = dirty->next) {
slouken@1907
   511
            data->renderer->RenderCopy(data->renderer, texture, &dirty->rect,
slouken@1907
   512
                                       &dirty->rect,
slouken@1965
   513
                                       SDL_TEXTUREBLENDMODE_NONE,
slouken@1965
   514
                                       SDL_TEXTURESCALEMODE_NONE);
slouken@1907
   515
        }
slouken@1907
   516
        SDL_ClearDirtyRects(&data->dirty);
slouken@1907
   517
    } else {
slouken@1907
   518
        SDL_Rect rect;
slouken@1907
   519
        rect.x = 0;
slouken@1907
   520
        rect.y = 0;
slouken@1907
   521
        rect.w = texture->w;
slouken@1907
   522
        rect.h = texture->h;
slouken@1907
   523
        data->renderer->RenderCopy(data->renderer, texture, &rect, &rect,
slouken@1965
   524
                                   SDL_TEXTUREBLENDMODE_NONE,
slouken@1965
   525
                                   SDL_TEXTURESCALEMODE_NONE);
slouken@1895
   526
    }
slouken@1895
   527
    data->renderer->RenderPresent(data->renderer);
slouken@1895
   528
slouken@1895
   529
    /* Update the flipping chain, if any */
slouken@1965
   530
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1907
   531
        data->current_texture = (data->current_texture + 1) % 2;
slouken@1965
   532
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1907
   533
        data->current_texture = (data->current_texture + 1) % 3;
slouken@1895
   534
    }
slouken@1895
   535
}
slouken@1895
   536
slouken@1895
   537
static void
slouken@1918
   538
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   539
{
slouken@1895
   540
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   541
        SDL_SW_DestroyYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   542
    } else {
slouken@1895
   543
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   544
slouken@1895
   545
        SDL_FreeSurface(surface);
slouken@1895
   546
    }
slouken@1895
   547
}
slouken@1895
   548
slouken@1895
   549
static void
slouken@1918
   550
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   551
{
slouken@1918
   552
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   553
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   554
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   555
    int i;
slouken@1895
   556
slouken@1895
   557
    if (data) {
slouken@1907
   558
        for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   559
            if (data->texture[i]) {
slouken@1907
   560
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1895
   561
            }
slouken@1895
   562
        }
slouken@1907
   563
        if (data->surface.format) {
slouken@1907
   564
            SDL_SetSurfacePalette(&data->surface, NULL);
slouken@1907
   565
            SDL_FreeFormat(data->surface.format);
slouken@1907
   566
        }
slouken@1907
   567
        if (display->palette) {
slouken@1907
   568
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
slouken@1907
   569
                                data);
slouken@1907
   570
        }
slouken@1907
   571
        if (data->renderer) {
slouken@1907
   572
            data->renderer->DestroyRenderer(data->renderer);
slouken@1907
   573
        }
slouken@1895
   574
        SDL_FreeDirtyRects(&data->dirty);
slouken@1895
   575
        SDL_free(data);
slouken@1895
   576
    }
slouken@1895
   577
    SDL_free(renderer);
slouken@1895
   578
}
slouken@1895
   579
slouken@1895
   580
/* vi: set ts=4 sw=4 expandtab: */