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