src/video/SDL_renderer_sw.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 09 Dec 2009 15:56:56 +0000
changeset 3536 0267b8b1595c
parent 3435 9f62f47d989b
child 3596 f638ded38b8a
permissions -rw-r--r--
Added interfaces for batch drawing of points, lines and rects:
SDL_DrawPoints()
SDL_BlendPoints()
SDL_BlendLines()
SDL_DrawLines()
SDL_FillRects()
SDL_BlendRects()
SDL_RenderPoints()
SDL_RenderLines()
SDL_RenderRects()
Renamed SDL_RenderFill() to SDL_RenderRect()
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#include "SDL_video.h"
slouken@1895
    25
#include "SDL_sysvideo.h"
slouken@1907
    26
#include "SDL_pixels_c.h"
slouken@1895
    27
#include "SDL_rect_c.h"
slouken@1895
    28
#include "SDL_yuv_sw_c.h"
slouken@1895
    29
slouken@1895
    30
slouken@1895
    31
/* SDL surface based renderer implementation */
slouken@1895
    32
slouken@1918
    33
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1970
    34
static int SW_ActivateRenderer(SDL_Renderer * renderer);
slouken@1970
    35
static int SW_DisplayModeChanged(SDL_Renderer * renderer);
slouken@1918
    36
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    37
static int SW_QueryTexturePixels(SDL_Renderer * renderer,
slouken@1918
    38
                                 SDL_Texture * texture, void **pixels,
slouken@1918
    39
                                 int *pitch);
slouken@1918
    40
static int SW_SetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    41
                                SDL_Texture * texture,
slouken@1918
    42
                                const SDL_Color * colors, int firstcolor,
slouken@1918
    43
                                int ncolors);
slouken@1918
    44
static int SW_GetTexturePalette(SDL_Renderer * renderer,
slouken@1918
    45
                                SDL_Texture * texture, SDL_Color * colors,
slouken@1918
    46
                                int firstcolor, int ncolors);
slouken@1985
    47
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    48
                                 SDL_Texture * texture);
slouken@1985
    49
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    50
                                 SDL_Texture * texture);
slouken@1985
    51
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@1985
    52
                                  SDL_Texture * texture);
slouken@1985
    53
static int SW_SetTextureScaleMode(SDL_Renderer * renderer,
slouken@1985
    54
                                  SDL_Texture * texture);
slouken@1985
    55
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    56
                            const SDL_Rect * rect, const void *pixels,
slouken@1985
    57
                            int pitch);
slouken@1918
    58
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    59
                          const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1985
    60
                          int *pitch);
slouken@1918
    61
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@3536
    62
static int SW_RenderPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3536
    63
                           int count);
slouken@3536
    64
static int SW_RenderLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3536
    65
                          int count);
slouken@3536
    66
static int SW_RenderRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
slouken@3536
    67
                          int count);
slouken@1918
    68
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    69
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3427
    70
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    71
                               Uint32 format, void * pixels, int pitch);
slouken@3427
    72
static int SW_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    73
                                Uint32 format, const void * pixels, int pitch);
slouken@1918
    74
static void SW_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    75
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    76
static void SW_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    77
slouken@1895
    78
slouken@1918
    79
SDL_RenderDriver SW_RenderDriver = {
slouken@1918
    80
    SW_CreateRenderer,
slouken@1895
    81
    {
slouken@1895
    82
     "software",
slouken@1965
    83
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
slouken@1965
    84
      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
slouken@1965
    85
      SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC),
slouken@1985
    86
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
    87
      SDL_TEXTUREMODULATE_ALPHA),
slouken@2884
    88
     (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK |
slouken@2884
    89
      SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
slouken@1965
    90
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST),
slouken@2811
    91
     14,
slouken@1895
    92
     {
slouken@1965
    93
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    94
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    95
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    96
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    97
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    98
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    99
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
   100
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
   101
      SDL_PIXELFORMAT_BGRA8888,
slouken@2811
   102
      SDL_PIXELFORMAT_YV12,
slouken@2811
   103
      SDL_PIXELFORMAT_IYUV,
slouken@1965
   104
      SDL_PIXELFORMAT_YUY2,
slouken@2810
   105
      SDL_PIXELFORMAT_UYVY,
slouken@2810
   106
      SDL_PIXELFORMAT_YVYU},
slouken@1895
   107
     0,
slouken@1895
   108
     0}
slouken@1895
   109
};
slouken@1895
   110
slouken@1895
   111
typedef struct
slouken@1895
   112
{
slouken@1907
   113
    Uint32 format;
slouken@1972
   114
    SDL_bool updateSize;
slouken@1907
   115
    int current_texture;
slouken@1907
   116
    SDL_Texture *texture[3];
slouken@1907
   117
    SDL_Surface surface;
slouken@1895
   118
    SDL_Renderer *renderer;
slouken@1895
   119
    SDL_DirtyRectList dirty;
slouken@1918
   120
} SW_RenderData;
slouken@1895
   121
slouken@1907
   122
static SDL_Texture *
slouken@1907
   123
CreateTexture(SDL_Renderer * renderer, Uint32 format, int w, int h)
slouken@1907
   124
{
slouken@1907
   125
    SDL_Texture *texture;
slouken@1907
   126
slouken@1920
   127
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@1907
   128
    if (!texture) {
slouken@1907
   129
        SDL_OutOfMemory();
slouken@1907
   130
        return NULL;
slouken@1907
   131
    }
slouken@1907
   132
slouken@1907
   133
    texture->format = format;
slouken@2222
   134
    texture->access = SDL_TEXTUREACCESS_STREAMING;
slouken@1907
   135
    texture->w = w;
slouken@1907
   136
    texture->h = h;
slouken@1907
   137
    texture->renderer = renderer;
slouken@1907
   138
slouken@1907
   139
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@1907
   140
        SDL_free(texture);
slouken@1907
   141
        return NULL;
slouken@1907
   142
    }
slouken@1907
   143
    return texture;
slouken@1907
   144
}
slouken@1907
   145
slouken@1907
   146
static void
slouken@1907
   147
DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1907
   148
{
slouken@1907
   149
    renderer->DestroyTexture(renderer, texture);
slouken@1907
   150
    SDL_free(texture);
slouken@1907
   151
}
slouken@1907
   152
slouken@1907
   153
static int
slouken@1907
   154
DisplayPaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1907
   155
{
slouken@1918
   156
    SW_RenderData *data = (SW_RenderData *) userdata;
slouken@1907
   157
    int i;
slouken@1907
   158
slouken@1907
   159
    for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   160
        if (data->texture[i] && data->renderer->SetTexturePalette) {
slouken@1907
   161
            data->renderer->SetTexturePalette(data->renderer,
slouken@1907
   162
                                              data->texture[i],
slouken@1907
   163
                                              palette->colors, 0,
slouken@1907
   164
                                              palette->ncolors);
slouken@1907
   165
        }
slouken@1907
   166
    }
slouken@1907
   167
    return 0;
slouken@1907
   168
}
slouken@1907
   169
slouken@2227
   170
void
slouken@2227
   171
Setup_SoftwareRenderer(SDL_Renderer * renderer)
slouken@2227
   172
{
slouken@2227
   173
    renderer->CreateTexture = SW_CreateTexture;
slouken@2227
   174
    renderer->QueryTexturePixels = SW_QueryTexturePixels;
slouken@2227
   175
    renderer->SetTexturePalette = SW_SetTexturePalette;
slouken@2227
   176
    renderer->GetTexturePalette = SW_GetTexturePalette;
slouken@2227
   177
    renderer->SetTextureColorMod = SW_SetTextureColorMod;
slouken@2227
   178
    renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
slouken@2227
   179
    renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
slouken@2227
   180
    renderer->SetTextureScaleMode = SW_SetTextureScaleMode;
slouken@2227
   181
    renderer->UpdateTexture = SW_UpdateTexture;
slouken@2227
   182
    renderer->LockTexture = SW_LockTexture;
slouken@2227
   183
    renderer->UnlockTexture = SW_UnlockTexture;
slouken@2227
   184
    renderer->DestroyTexture = SW_DestroyTexture;
slouken@2227
   185
slouken@2227
   186
    renderer->info.mod_modes = SW_RenderDriver.info.mod_modes;
slouken@2227
   187
    renderer->info.blend_modes = SW_RenderDriver.info.blend_modes;
slouken@2227
   188
    renderer->info.scale_modes = SW_RenderDriver.info.scale_modes;
slouken@2227
   189
    renderer->info.num_texture_formats =
slouken@2227
   190
        SW_RenderDriver.info.num_texture_formats;
slouken@2227
   191
    SDL_memcpy(renderer->info.texture_formats,
slouken@2227
   192
               SW_RenderDriver.info.texture_formats,
slouken@2227
   193
               sizeof(renderer->info.texture_formats));;
slouken@2227
   194
    renderer->info.max_texture_width = SW_RenderDriver.info.max_texture_width;
slouken@2227
   195
    renderer->info.max_texture_height =
slouken@2227
   196
        SW_RenderDriver.info.max_texture_height;
slouken@2227
   197
}
slouken@2227
   198
slouken@1895
   199
SDL_Renderer *
slouken@1918
   200
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   201
{
slouken@1895
   202
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   203
    SDL_DisplayMode *displayMode = &display->current_mode;
slouken@1895
   204
    SDL_Renderer *renderer;
slouken@1918
   205
    SW_RenderData *data;
slouken@1895
   206
    int i, n;
slouken@1895
   207
    int bpp;
slouken@1895
   208
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1907
   209
    Uint32 renderer_flags;
slouken@1929
   210
    const char *desired_driver;
slouken@1895
   211
slouken@1895
   212
    if (!SDL_PixelFormatEnumToMasks
slouken@1895
   213
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   214
        SDL_SetError("Unknown display format");
slouken@1895
   215
        return NULL;
slouken@1895
   216
    }
slouken@1895
   217
slouken@1895
   218
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   219
    if (!renderer) {
slouken@1895
   220
        SDL_OutOfMemory();
slouken@1895
   221
        return NULL;
slouken@1895
   222
    }
slouken@1895
   223
slouken@1920
   224
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   225
    if (!data) {
slouken@1918
   226
        SW_DestroyRenderer(renderer);
slouken@1895
   227
        SDL_OutOfMemory();
slouken@1895
   228
        return NULL;
slouken@1895
   229
    }
slouken@1970
   230
    renderer->ActivateRenderer = SW_ActivateRenderer;
slouken@1970
   231
    renderer->DisplayModeChanged = SW_DisplayModeChanged;
slouken@2884
   232
slouken@3536
   233
    renderer->RenderPoints = SW_RenderPoints;
slouken@3536
   234
    renderer->RenderLines = SW_RenderLines;
slouken@3536
   235
    renderer->RenderRects = SW_RenderRects;
slouken@1918
   236
    renderer->RenderCopy = SW_RenderCopy;
slouken@3427
   237
    renderer->RenderReadPixels = SW_RenderReadPixels;
slouken@3427
   238
    renderer->RenderWritePixels = SW_RenderWritePixels;
slouken@1918
   239
    renderer->RenderPresent = SW_RenderPresent;
slouken@1918
   240
    renderer->DestroyRenderer = SW_DestroyRenderer;
slouken@2227
   241
    renderer->info.name = SW_RenderDriver.info.name;
slouken@2227
   242
    renderer->info.flags = 0;
slouken@1895
   243
    renderer->window = window->id;
slouken@1895
   244
    renderer->driverdata = data;
slouken@2227
   245
    Setup_SoftwareRenderer(renderer);
slouken@1895
   246
slouken@1965
   247
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1965
   248
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
slouken@1895
   249
        n = 2;
slouken@1965
   250
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1965
   251
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
slouken@1895
   252
        n = 3;
slouken@1895
   253
    } else {
slouken@1965
   254
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
slouken@1895
   255
        n = 1;
slouken@1895
   256
    }
slouken@1907
   257
    data->format = displayMode->format;
slouken@1895
   258
slouken@1895
   259
    /* Find a render driver that we can use to display data */
slouken@1965
   260
    renderer_flags = (SDL_RENDERER_SINGLEBUFFER |
slouken@1965
   261
                      SDL_RENDERER_PRESENTDISCARD);
slouken@1965
   262
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   263
        renderer_flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   264
    }
slouken@1929
   265
    desired_driver = SDL_getenv("SDL_VIDEO_RENDERER_SWDRIVER");
slouken@1895
   266
    for (i = 0; i < display->num_render_drivers; ++i) {
slouken@1895
   267
        SDL_RenderDriver *driver = &display->render_drivers[i];
slouken@1929
   268
        if (driver->info.name == SW_RenderDriver.info.name) {
slouken@1929
   269
            continue;
slouken@1929
   270
        }
slouken@1929
   271
        if (desired_driver
slouken@1929
   272
            && SDL_strcasecmp(desired_driver, driver->info.name) != 0) {
slouken@1929
   273
            continue;
slouken@1929
   274
        }
slouken@1929
   275
        data->renderer = driver->CreateRenderer(window, renderer_flags);
slouken@1929
   276
        if (data->renderer) {
slouken@1929
   277
            break;
slouken@1895
   278
        }
slouken@1895
   279
    }
slouken@1895
   280
    if (i == display->num_render_drivers) {
slouken@1918
   281
        SW_DestroyRenderer(renderer);
slouken@1895
   282
        SDL_SetError("Couldn't find display render driver");
slouken@1895
   283
        return NULL;
slouken@1895
   284
    }
slouken@1965
   285
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   286
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   287
    }
slouken@1907
   288
slouken@1907
   289
    /* Create the textures we'll use for display */
slouken@1907
   290
    for (i = 0; i < n; ++i) {
slouken@1907
   291
        data->texture[i] =
slouken@1907
   292
            CreateTexture(data->renderer, data->format, window->w, window->h);
slouken@1907
   293
        if (!data->texture[i]) {
slouken@1918
   294
            SW_DestroyRenderer(renderer);
slouken@1907
   295
            return NULL;
slouken@1907
   296
        }
slouken@1907
   297
    }
slouken@1907
   298
    data->current_texture = 0;
slouken@1907
   299
slouken@1907
   300
    /* Create a surface we'll use for rendering */
slouken@1907
   301
    data->surface.flags = SDL_PREALLOC;
slouken@1907
   302
    data->surface.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1907
   303
    if (!data->surface.format) {
slouken@1918
   304
        SW_DestroyRenderer(renderer);
slouken@1907
   305
        return NULL;
slouken@1907
   306
    }
slouken@1907
   307
    SDL_SetSurfacePalette(&data->surface, display->palette);
slouken@1907
   308
slouken@1907
   309
    /* Set up a palette watch on the display palette */
slouken@1907
   310
    if (display->palette) {
slouken@1907
   311
        SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
slouken@1907
   312
    }
slouken@1907
   313
slouken@1895
   314
    return renderer;
slouken@1895
   315
}
slouken@1895
   316
slouken@1895
   317
static int
slouken@1970
   318
SW_ActivateRenderer(SDL_Renderer * renderer)
slouken@1970
   319
{
slouken@1970
   320
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1972
   321
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1972
   322
    int i, n;
slouken@1970
   323
slouken@1970
   324
    if (data->renderer && data->renderer->ActivateRenderer) {
slouken@1970
   325
        if (data->renderer->ActivateRenderer(data->renderer) < 0) {
slouken@1970
   326
            return -1;
slouken@1970
   327
        }
slouken@1970
   328
    }
slouken@1972
   329
    if (data->updateSize) {
slouken@1972
   330
        /* Recreate the textures for the new window size */
slouken@1972
   331
        if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1972
   332
            n = 2;
slouken@1972
   333
        } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1972
   334
            n = 3;
slouken@1972
   335
        } else {
slouken@1972
   336
            n = 1;
slouken@1972
   337
        }
slouken@1972
   338
        for (i = 0; i < n; ++i) {
slouken@1972
   339
            if (data->texture[i]) {
slouken@1972
   340
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1972
   341
                data->texture[i] = 0;
slouken@1972
   342
            }
slouken@1972
   343
        }
slouken@1972
   344
        for (i = 0; i < n; ++i) {
slouken@1972
   345
            data->texture[i] =
slouken@1972
   346
                CreateTexture(data->renderer, data->format, window->w,
slouken@1972
   347
                              window->h);
slouken@1972
   348
            if (!data->texture[i]) {
slouken@1972
   349
                return -1;
slouken@1972
   350
            }
slouken@1972
   351
        }
slouken@1972
   352
        data->updateSize = SDL_FALSE;
slouken@1972
   353
    }
slouken@1970
   354
    return 0;
slouken@1970
   355
}
slouken@1970
   356
slouken@1970
   357
static int
slouken@1970
   358
SW_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   359
{
slouken@1970
   360
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
   361
slouken@1970
   362
    if (data->renderer && data->renderer->DisplayModeChanged) {
slouken@1970
   363
        if (data->renderer->DisplayModeChanged(data->renderer) < 0) {
slouken@1970
   364
            return -1;
slouken@1970
   365
        }
slouken@1970
   366
    }
slouken@2905
   367
    /* Rebind the context to the window area */
slouken@1972
   368
    data->updateSize = SDL_TRUE;
slouken@2905
   369
    return SW_ActivateRenderer(renderer);
slouken@1970
   370
}
slouken@1970
   371
slouken@1970
   372
static int
slouken@1918
   373
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   374
{
slouken@1895
   375
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@2786
   376
        texture->driverdata =
slouken@2786
   377
            SDL_SW_CreateYUVTexture(texture->format, texture->w, texture->h);
slouken@1895
   378
    } else {
slouken@1895
   379
        int bpp;
slouken@1895
   380
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   381
slouken@1895
   382
        if (!SDL_PixelFormatEnumToMasks
slouken@1895
   383
            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   384
            SDL_SetError("Unknown texture format");
slouken@1895
   385
            return -1;
slouken@1895
   386
        }
slouken@1895
   387
slouken@1895
   388
        texture->driverdata =
slouken@1895
   389
            SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@1895
   390
                                 Bmask, Amask);
slouken@3053
   391
        SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
slouken@3053
   392
                               texture->b);
slouken@3053
   393
        SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
slouken@3053
   394
        SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
slouken@3053
   395
        SDL_SetSurfaceScaleMode(texture->driverdata, texture->scaleMode);
slouken@3053
   396
slouken@2802
   397
        if (texture->access == SDL_TEXTUREACCESS_STATIC) {
slouken@2802
   398
            SDL_SetSurfaceRLE(texture->driverdata, 1);
slouken@2802
   399
        }
slouken@1895
   400
    }
slouken@1895
   401
slouken@1895
   402
    if (!texture->driverdata) {
slouken@1895
   403
        return -1;
slouken@1895
   404
    }
slouken@1895
   405
    return 0;
slouken@1895
   406
}
slouken@1895
   407
slouken@1895
   408
static int
slouken@1918
   409
SW_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   410
                      void **pixels, int *pitch)
slouken@1895
   411
{
slouken@1895
   412
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@2735
   413
        return SDL_SW_QueryYUVTexturePixels((SDL_SW_YUVTexture *)
slouken@2735
   414
                                            texture->driverdata, pixels,
slouken@2735
   415
                                            pitch);
slouken@1895
   416
    } else {
slouken@1895
   417
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   418
slouken@1895
   419
        *pixels = surface->pixels;
slouken@1895
   420
        *pitch = surface->pitch;
slouken@1895
   421
        return 0;
slouken@1895
   422
    }
slouken@1895
   423
}
slouken@1895
   424
slouken@1895
   425
static int
slouken@1918
   426
SW_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   427
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   428
{
slouken@1895
   429
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   430
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   431
        return -1;
slouken@1895
   432
    } else {
slouken@1895
   433
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   434
slouken@1895
   435
        return SDL_SetPaletteColors(surface->format->palette, colors,
slouken@1895
   436
                                    firstcolor, ncolors);
slouken@1895
   437
    }
slouken@1895
   438
}
slouken@1895
   439
slouken@1895
   440
static int
slouken@1918
   441
SW_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   442
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   443
{
slouken@1895
   444
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   445
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   446
        return -1;
slouken@1895
   447
    } else {
slouken@1895
   448
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   449
slouken@1895
   450
        SDL_memcpy(colors, &surface->format->palette->colors[firstcolor],
slouken@1895
   451
                   ncolors * sizeof(*colors));
slouken@1895
   452
        return 0;
slouken@1895
   453
    }
slouken@1895
   454
}
slouken@1895
   455
slouken@1985
   456
static int
slouken@1985
   457
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   458
{
slouken@2267
   459
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   460
    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
slouken@2267
   461
                                  texture->b);
slouken@1985
   462
}
slouken@1985
   463
slouken@1985
   464
static int
slouken@1985
   465
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   466
{
slouken@2267
   467
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   468
    return SDL_SetSurfaceAlphaMod(surface, texture->a);
slouken@1985
   469
}
slouken@1985
   470
slouken@1985
   471
static int
slouken@1985
   472
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   473
{
slouken@2267
   474
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   475
    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
slouken@1985
   476
}
slouken@1985
   477
slouken@1985
   478
static int
slouken@1985
   479
SW_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   480
{
slouken@2267
   481
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2802
   482
    return SDL_SetSurfaceScaleMode(surface, texture->scaleMode);
slouken@1985
   483
}
slouken@1985
   484
slouken@1895
   485
static int
slouken@1918
   486
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   487
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   488
{
slouken@1895
   489
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@2735
   490
        return SDL_SW_UpdateYUVTexture((SDL_SW_YUVTexture *)
slouken@2735
   491
                                       texture->driverdata, rect, pixels,
slouken@2735
   492
                                       pitch);
slouken@1895
   493
    } else {
slouken@1895
   494
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   495
        Uint8 *src, *dst;
slouken@1895
   496
        int row;
slouken@1895
   497
        size_t length;
slouken@1895
   498
slouken@1895
   499
        src = (Uint8 *) pixels;
slouken@1895
   500
        dst =
slouken@1895
   501
            (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   502
            rect->x * surface->format->BytesPerPixel;
slouken@1895
   503
        length = rect->w * surface->format->BytesPerPixel;
slouken@1895
   504
        for (row = 0; row < rect->h; ++row) {
slouken@1895
   505
            SDL_memcpy(dst, src, length);
slouken@1895
   506
            src += pitch;
slouken@1895
   507
            dst += surface->pitch;
slouken@1895
   508
        }
slouken@1895
   509
        return 0;
slouken@1895
   510
    }
slouken@1895
   511
}
slouken@1895
   512
slouken@1895
   513
static int
slouken@1918
   514
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   515
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   516
               int *pitch)
slouken@1895
   517
{
slouken@1895
   518
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@2735
   519
        return SDL_SW_LockYUVTexture((SDL_SW_YUVTexture *)
slouken@2735
   520
                                     texture->driverdata, rect, markDirty,
slouken@2735
   521
                                     pixels, pitch);
slouken@1895
   522
    } else {
slouken@1895
   523
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   524
slouken@1895
   525
        *pixels =
slouken@1895
   526
            (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   527
                      rect->x * surface->format->BytesPerPixel);
slouken@1895
   528
        *pitch = surface->pitch;
slouken@1895
   529
        return 0;
slouken@1895
   530
    }
slouken@1895
   531
}
slouken@1895
   532
slouken@1895
   533
static void
slouken@1918
   534
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   535
{
slouken@1895
   536
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   537
        SDL_SW_UnlockYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   538
    }
slouken@1895
   539
}
slouken@1895
   540
slouken@1895
   541
static int
slouken@3536
   542
SW_RenderPoints(SDL_Renderer * renderer, const SDL_Point * points, int count)
slouken@2901
   543
{
slouken@2901
   544
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@3536
   545
    SDL_Texture *texture = data->texture[data->current_texture];
slouken@2904
   546
    SDL_Rect rect;
slouken@3536
   547
    int i;
slouken@3536
   548
    int x, y;
slouken@3536
   549
    int status = 0;
slouken@2901
   550
slouken@3536
   551
    /* Get the smallest rectangle that contains everything */
slouken@3536
   552
    rect.x = 0;
slouken@3536
   553
    rect.y = 0;
slouken@3536
   554
    rect.w = texture->w;
slouken@3536
   555
    rect.h = texture->h;
slouken@3536
   556
    if (!SDL_EnclosePoints(points, count, &rect, &rect)) {
slouken@3536
   557
        /* Nothing to draw */
slouken@3536
   558
        return 0;
slouken@2904
   559
    }
slouken@2904
   560
slouken@2888
   561
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@2888
   562
        SDL_AddDirtyRect(&data->dirty, &rect);
slouken@2888
   563
    }
slouken@2888
   564
slouken@3536
   565
    if (data->renderer->LockTexture(data->renderer, texture, &rect, 1,
slouken@2891
   566
                                    &data->surface.pixels,
slouken@2888
   567
                                    &data->surface.pitch) < 0) {
slouken@2888
   568
        return -1;
slouken@2888
   569
    }
slouken@2888
   570
slouken@3536
   571
    data->surface.clip_rect.w = data->surface.w = rect.w;
slouken@3536
   572
    data->surface.clip_rect.h = data->surface.h = rect.h;
slouken@2904
   573
slouken@3536
   574
    /* Draw the points! */
slouken@2901
   575
    if (renderer->blendMode == SDL_BLENDMODE_NONE ||
slouken@2901
   576
        renderer->blendMode == SDL_BLENDMODE_MASK) {
slouken@3536
   577
        Uint32 color = SDL_MapRGBA(data->surface.format,
slouken@3536
   578
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   579
                                   renderer->a);
slouken@2888
   580
slouken@3536
   581
        for (i = 0; i < count; ++i) {
slouken@3536
   582
            x = points[i].x - rect.x;
slouken@3536
   583
            y = points[i].y - rect.y;
slouken@3536
   584
slouken@3536
   585
            status = SDL_DrawPoint(&data->surface, x, y, color);
slouken@3536
   586
        }
slouken@2888
   587
    } else {
slouken@3536
   588
        for (i = 0; i < count; ++i) {
slouken@3536
   589
            x = points[i].x - rect.x;
slouken@3536
   590
            y = points[i].y - rect.y;
slouken@3536
   591
slouken@3536
   592
            status = SDL_BlendPoint(&data->surface, x, y,
slouken@3536
   593
                                    renderer->blendMode,
slouken@3536
   594
                                    renderer->r, renderer->g, renderer->b,
slouken@3536
   595
                                    renderer->a);
slouken@3536
   596
        }
slouken@2888
   597
    }
slouken@2888
   598
slouken@3536
   599
    data->renderer->UnlockTexture(data->renderer, texture);
slouken@3536
   600
slouken@2888
   601
    return status;
slouken@2888
   602
}
slouken@2888
   603
slouken@2888
   604
static int
slouken@3536
   605
SW_RenderLines(SDL_Renderer * renderer, const SDL_Point * points, int count)
slouken@1895
   606
{
slouken@1918
   607
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@3536
   608
    SDL_Texture *texture = data->texture[data->current_texture];
slouken@3536
   609
    SDL_Rect clip, rect;
slouken@3536
   610
    int i;
slouken@3536
   611
    int x1, y1, x2, y2;
slouken@3536
   612
    int status = 0;
slouken@3536
   613
slouken@3536
   614
    /* Get the smallest rectangle that contains everything */
slouken@3536
   615
    clip.x = 0;
slouken@3536
   616
    clip.y = 0;
slouken@3536
   617
    clip.w = texture->w;
slouken@3536
   618
    clip.h = texture->h;
slouken@3536
   619
    SDL_EnclosePoints(points, count, NULL, &rect);
slouken@3536
   620
    if (!SDL_IntersectRect(&rect, &clip, &rect)) {
slouken@3536
   621
        /* Nothing to draw */
slouken@3536
   622
        return 0;
slouken@3536
   623
    }
slouken@1895
   624
slouken@1965
   625
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@3536
   626
        SDL_AddDirtyRect(&data->dirty, &rect);
slouken@1897
   627
    }
slouken@1895
   628
slouken@3536
   629
    if (data->renderer->LockTexture(data->renderer, texture, &rect, 1,
slouken@3536
   630
                                    &data->surface.pixels,
slouken@2735
   631
                                    &data->surface.pitch) < 0) {
slouken@1907
   632
        return -1;
slouken@1907
   633
    }
slouken@2888
   634
slouken@3536
   635
    data->surface.clip_rect.w = data->surface.w = rect.w;
slouken@3536
   636
    data->surface.clip_rect.h = data->surface.h = rect.h;
slouken@1907
   637
slouken@3536
   638
    /* Draw the points! */
slouken@3536
   639
    if (renderer->blendMode == SDL_BLENDMODE_NONE ||
slouken@3536
   640
        renderer->blendMode == SDL_BLENDMODE_MASK) {
slouken@3536
   641
        Uint32 color = SDL_MapRGBA(data->surface.format,
slouken@3536
   642
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   643
                                   renderer->a);
slouken@2888
   644
slouken@3536
   645
        for (i = 1; i < count; ++i) {
slouken@3536
   646
            x1 = points[i-1].x - rect.x;
slouken@3536
   647
            y1 = points[i-1].y - rect.y;
slouken@3536
   648
            x2 = points[i].x - rect.x;
slouken@3536
   649
            y2 = points[i].y - rect.y;
slouken@3536
   650
slouken@3536
   651
            status = SDL_DrawLine(&data->surface, x1, y1, x2, y2, color);
slouken@3536
   652
        }
slouken@2888
   653
    } else {
slouken@3536
   654
        for (i = 1; i < count; ++i) {
slouken@3536
   655
            x1 = points[i-1].x - rect.x;
slouken@3536
   656
            y1 = points[i-1].y - rect.y;
slouken@3536
   657
            x2 = points[i].x - rect.x;
slouken@3536
   658
            y2 = points[i].y - rect.y;
slouken@3536
   659
slouken@3536
   660
            status = SDL_BlendLine(&data->surface, x1, y1, x2, y2,
slouken@3536
   661
                                   renderer->blendMode,
slouken@3536
   662
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   663
                                   renderer->a);
slouken@3536
   664
        }
slouken@2888
   665
    }
slouken@1907
   666
slouken@3536
   667
    data->renderer->UnlockTexture(data->renderer, texture);
slouken@3536
   668
slouken@3536
   669
    return status;
slouken@3536
   670
}
slouken@3536
   671
slouken@3536
   672
static int
slouken@3536
   673
SW_RenderRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count)
slouken@3536
   674
{
slouken@3536
   675
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@3536
   676
    SDL_Texture *texture = data->texture[data->current_texture];
slouken@3536
   677
    SDL_Rect clip, rect;
slouken@3536
   678
    Uint32 color = 0;
slouken@3536
   679
    int i;
slouken@3536
   680
    int status = 0;
slouken@3536
   681
slouken@3536
   682
    clip.x = 0;
slouken@3536
   683
    clip.y = 0;
slouken@3536
   684
    clip.w = texture->w;
slouken@3536
   685
    clip.h = texture->h;
slouken@3536
   686
slouken@3536
   687
    if (renderer->blendMode == SDL_BLENDMODE_NONE ||
slouken@3536
   688
        renderer->blendMode == SDL_BLENDMODE_MASK) {
slouken@3536
   689
        color = SDL_MapRGBA(data->surface.format,
slouken@3536
   690
                            renderer->r, renderer->g, renderer->b,
slouken@3536
   691
                            renderer->a);
slouken@3536
   692
    }
slouken@3536
   693
slouken@3536
   694
    for (i = 0; i < count; ++i) {
slouken@3536
   695
        if (!SDL_IntersectRect(rects[i], &clip, &rect)) {
slouken@3536
   696
            /* Nothing to draw */
slouken@3536
   697
            continue;
slouken@3536
   698
        }
slouken@3536
   699
slouken@3536
   700
        if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@3536
   701
            SDL_AddDirtyRect(&data->dirty, &rect);
slouken@3536
   702
        }
slouken@3536
   703
slouken@3536
   704
        if (data->renderer->LockTexture(data->renderer, texture, &rect, 1,
slouken@3536
   705
                                        &data->surface.pixels,
slouken@3536
   706
                                        &data->surface.pitch) < 0) {
slouken@3536
   707
            return -1;
slouken@3536
   708
        }
slouken@3536
   709
slouken@3536
   710
        data->surface.clip_rect.w = data->surface.w = rect.w;
slouken@3536
   711
        data->surface.clip_rect.h = data->surface.h = rect.h;
slouken@3536
   712
slouken@3536
   713
        if (renderer->blendMode == SDL_BLENDMODE_NONE ||
slouken@3536
   714
            renderer->blendMode == SDL_BLENDMODE_MASK) {
slouken@3536
   715
            status = SDL_FillRect(&data->surface, NULL, color);
slouken@3536
   716
        } else {
slouken@3536
   717
            status = SDL_BlendRect(&data->surface, NULL,
slouken@3536
   718
                                   renderer->blendMode,
slouken@3536
   719
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   720
                                   renderer->a);
slouken@3536
   721
        }
slouken@3536
   722
slouken@3536
   723
        data->renderer->UnlockTexture(data->renderer, texture);
slouken@3536
   724
    }
slouken@1907
   725
    return status;
slouken@1895
   726
}
slouken@1895
   727
slouken@1895
   728
static int
slouken@1918
   729
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   730
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   731
{
slouken@1918
   732
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   733
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   734
    int status;
slouken@1895
   735
slouken@1965
   736
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   737
        SDL_AddDirtyRect(&data->dirty, dstrect);
slouken@1897
   738
    }
slouken@1895
   739
slouken@2735
   740
    if (data->renderer->LockTexture(data->renderer,
slouken@2735
   741
                                    data->texture[data->current_texture],
slouken@2735
   742
                                    dstrect, 1, &data->surface.pixels,
slouken@2735
   743
                                    &data->surface.pitch) < 0) {
slouken@1907
   744
        return -1;
slouken@1907
   745
    }
slouken@1907
   746
slouken@1895
   747
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1907
   748
        status =
slouken@1907
   749
            SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
slouken@1907
   750
                                srcrect, data->format, dstrect->w, dstrect->h,
slouken@1907
   751
                                data->surface.pixels, data->surface.pitch);
slouken@1895
   752
    } else {
slouken@1895
   753
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   754
        SDL_Rect real_srcrect = *srcrect;
slouken@2267
   755
        SDL_Rect real_dstrect;
slouken@1907
   756
slouken@2267
   757
        data->surface.w = dstrect->w;
slouken@2267
   758
        data->surface.h = dstrect->h;
slouken@2267
   759
        data->surface.clip_rect.w = dstrect->w;
slouken@2267
   760
        data->surface.clip_rect.h = dstrect->h;
slouken@2267
   761
        real_dstrect = data->surface.clip_rect;
slouken@1895
   762
slouken@2267
   763
        status =
slouken@2267
   764
            SDL_LowerBlit(surface, &real_srcrect, &data->surface,
slouken@2267
   765
                          &real_dstrect);
slouken@1895
   766
    }
slouken@1907
   767
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   768
                                  data->texture[data->current_texture]);
slouken@1907
   769
    return status;
slouken@1895
   770
}
slouken@1895
   771
slouken@3427
   772
static int
slouken@3427
   773
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   774
                    Uint32 format, void * pixels, int pitch)
slouken@3427
   775
{
slouken@3427
   776
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@3427
   777
slouken@3427
   778
    if (data->renderer->LockTexture(data->renderer,
slouken@3427
   779
                                    data->texture[data->current_texture],
slouken@3427
   780
                                    rect, 0, &data->surface.pixels,
slouken@3427
   781
                                    &data->surface.pitch) < 0) {
slouken@3427
   782
        return -1;
slouken@3427
   783
    }
slouken@3427
   784
slouken@3435
   785
    SDL_ConvertPixels(rect->w, rect->h,
slouken@3435
   786
                      data->format, data->surface.pixels, data->surface.pitch,
slouken@3435
   787
                      format, pixels, pitch);
slouken@3427
   788
slouken@3427
   789
    data->renderer->UnlockTexture(data->renderer,
slouken@3427
   790
                                  data->texture[data->current_texture]);
slouken@3427
   791
    return 0;
slouken@3427
   792
}
slouken@3427
   793
slouken@3427
   794
static int
slouken@3427
   795
SW_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   796
                     Uint32 format, const void * pixels, int pitch)
slouken@3427
   797
{
slouken@3427
   798
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@3427
   799
slouken@3427
   800
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@3427
   801
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@3427
   802
    }
slouken@3427
   803
slouken@3427
   804
    if (data->renderer->LockTexture(data->renderer,
slouken@3427
   805
                                    data->texture[data->current_texture],
slouken@3427
   806
                                    rect, 1, &data->surface.pixels,
slouken@3427
   807
                                    &data->surface.pitch) < 0) {
slouken@3427
   808
        return -1;
slouken@3427
   809
    }
slouken@3427
   810
slouken@3435
   811
    SDL_ConvertPixels(rect->w, rect->h, format, pixels, pitch,
slouken@3435
   812
                      data->format, data->surface.pixels, data->surface.pitch);
slouken@3427
   813
slouken@3427
   814
    data->renderer->UnlockTexture(data->renderer,
slouken@3427
   815
                                  data->texture[data->current_texture]);
slouken@3427
   816
    return 0;
slouken@3427
   817
}
slouken@3427
   818
slouken@1895
   819
static void
slouken@1918
   820
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   821
{
slouken@1918
   822
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   823
    SDL_Texture *texture = data->texture[data->current_texture];
slouken@1895
   824
slouken@1895
   825
    /* Send the data to the display */
slouken@1965
   826
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1907
   827
        SDL_DirtyRect *dirty;
slouken@1907
   828
        for (dirty = data->dirty.list; dirty; dirty = dirty->next) {
slouken@1907
   829
            data->renderer->RenderCopy(data->renderer, texture, &dirty->rect,
slouken@1985
   830
                                       &dirty->rect);
slouken@1907
   831
        }
slouken@1907
   832
        SDL_ClearDirtyRects(&data->dirty);
slouken@1907
   833
    } else {
slouken@1907
   834
        SDL_Rect rect;
slouken@1907
   835
        rect.x = 0;
slouken@1907
   836
        rect.y = 0;
slouken@1907
   837
        rect.w = texture->w;
slouken@1907
   838
        rect.h = texture->h;
slouken@1985
   839
        data->renderer->RenderCopy(data->renderer, texture, &rect, &rect);
slouken@1895
   840
    }
slouken@1895
   841
    data->renderer->RenderPresent(data->renderer);
slouken@1895
   842
slouken@1895
   843
    /* Update the flipping chain, if any */
slouken@1965
   844
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1907
   845
        data->current_texture = (data->current_texture + 1) % 2;
slouken@1965
   846
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1907
   847
        data->current_texture = (data->current_texture + 1) % 3;
slouken@1895
   848
    }
slouken@1895
   849
}
slouken@1895
   850
slouken@1895
   851
static void
slouken@1918
   852
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   853
{
slouken@1895
   854
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   855
        SDL_SW_DestroyYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   856
    } else {
slouken@1895
   857
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   858
slouken@1895
   859
        SDL_FreeSurface(surface);
slouken@1895
   860
    }
slouken@1895
   861
}
slouken@1895
   862
slouken@1895
   863
static void
slouken@1918
   864
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   865
{
slouken@1918
   866
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   867
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   868
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   869
    int i;
slouken@1895
   870
slouken@1895
   871
    if (data) {
slouken@1907
   872
        for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   873
            if (data->texture[i]) {
slouken@1907
   874
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1895
   875
            }
slouken@1895
   876
        }
slouken@1907
   877
        if (data->surface.format) {
slouken@1907
   878
            SDL_SetSurfacePalette(&data->surface, NULL);
slouken@1907
   879
            SDL_FreeFormat(data->surface.format);
slouken@1907
   880
        }
slouken@1907
   881
        if (display->palette) {
slouken@1907
   882
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
slouken@1907
   883
                                data);
slouken@1907
   884
        }
slouken@1907
   885
        if (data->renderer) {
slouken@1907
   886
            data->renderer->DestroyRenderer(data->renderer);
slouken@1907
   887
        }
slouken@1895
   888
        SDL_FreeDirtyRects(&data->dirty);
slouken@1895
   889
        SDL_free(data);
slouken@1895
   890
    }
slouken@1895
   891
    SDL_free(renderer);
slouken@1895
   892
}
slouken@1895
   893
slouken@1895
   894
/* vi: set ts=4 sw=4 expandtab: */