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