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