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