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