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