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