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