src/video/SDL_renderer_sw.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 06 Aug 2006 04:39:13 +0000
changeset 1970 db3ba6c0d0df
parent 1965 a788656ca29a
child 1971 5432b63cc919
permissions -rw-r--r--
Allow the render context to do necessary work when the video mode changes.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 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@1918
    47
static int SW_UpdateTexture(SDL_Renderer * renderer,
slouken@1918
    48
                            SDL_Texture * texture, const SDL_Rect * rect,
slouken@1918
    49
                            const void *pixels, int pitch);
slouken@1918
    50
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    51
                          const SDL_Rect * rect, int markDirty,
slouken@1918
    52
                          void **pixels, int *pitch);
slouken@1918
    53
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    54
static void SW_DirtyTexture(SDL_Renderer * renderer,
slouken@1918
    55
                            SDL_Texture * texture, int numrects,
slouken@1918
    56
                            const SDL_Rect * rects);
slouken@1918
    57
static int SW_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1918
    58
                         Uint32 color);
slouken@1918
    59
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
    60
                         const SDL_Rect * srcrect,
slouken@1918
    61
                         const SDL_Rect * dstrect, int blendMode,
slouken@1918
    62
                         int scaleMode);
slouken@1918
    63
static void SW_RenderPresent(SDL_Renderer * renderer);
slouken@1918
    64
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1918
    65
static void SW_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    66
slouken@1895
    67
slouken@1918
    68
SDL_RenderDriver SW_RenderDriver = {
slouken@1918
    69
    SW_CreateRenderer,
slouken@1895
    70
    {
slouken@1895
    71
     "software",
slouken@1965
    72
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
slouken@1965
    73
      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
slouken@1965
    74
      SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC),
slouken@1965
    75
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1965
    76
      SDL_TEXTUREBLENDMODE_BLEND),
slouken@1965
    77
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST),
slouken@1895
    78
     11,
slouken@1895
    79
     {
slouken@1965
    80
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    81
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    82
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    83
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    84
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    85
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    86
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
    87
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
    88
      SDL_PIXELFORMAT_BGRA8888,
slouken@1965
    89
      SDL_PIXELFORMAT_YUY2,
slouken@1965
    90
      SDL_PIXELFORMAT_UYVY},
slouken@1895
    91
     0,
slouken@1895
    92
     0}
slouken@1895
    93
};
slouken@1895
    94
slouken@1895
    95
typedef struct
slouken@1895
    96
{
slouken@1907
    97
    Uint32 format;
slouken@1907
    98
    int current_texture;
slouken@1907
    99
    SDL_Texture *texture[3];
slouken@1907
   100
    SDL_Surface surface;
slouken@1895
   101
    SDL_Renderer *renderer;
slouken@1895
   102
    SDL_DirtyRectList dirty;
slouken@1918
   103
} SW_RenderData;
slouken@1895
   104
slouken@1907
   105
static SDL_Texture *
slouken@1907
   106
CreateTexture(SDL_Renderer * renderer, Uint32 format, int w, int h)
slouken@1907
   107
{
slouken@1907
   108
    SDL_Texture *texture;
slouken@1907
   109
slouken@1920
   110
    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
slouken@1907
   111
    if (!texture) {
slouken@1907
   112
        SDL_OutOfMemory();
slouken@1907
   113
        return NULL;
slouken@1907
   114
    }
slouken@1907
   115
slouken@1907
   116
    texture->format = format;
slouken@1965
   117
    texture->access = SDL_TEXTUREACCESS_LOCAL;
slouken@1907
   118
    texture->w = w;
slouken@1907
   119
    texture->h = h;
slouken@1907
   120
    texture->renderer = renderer;
slouken@1907
   121
slouken@1907
   122
    if (renderer->CreateTexture(renderer, texture) < 0) {
slouken@1907
   123
        SDL_free(texture);
slouken@1907
   124
        return NULL;
slouken@1907
   125
    }
slouken@1907
   126
    return texture;
slouken@1907
   127
}
slouken@1907
   128
slouken@1907
   129
static void
slouken@1907
   130
DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1907
   131
{
slouken@1907
   132
    renderer->DestroyTexture(renderer, texture);
slouken@1907
   133
    SDL_free(texture);
slouken@1907
   134
}
slouken@1907
   135
slouken@1907
   136
static int
slouken@1907
   137
DisplayPaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1907
   138
{
slouken@1918
   139
    SW_RenderData *data = (SW_RenderData *) userdata;
slouken@1907
   140
    int i;
slouken@1907
   141
slouken@1907
   142
    for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   143
        if (data->texture[i] && data->renderer->SetTexturePalette) {
slouken@1907
   144
            data->renderer->SetTexturePalette(data->renderer,
slouken@1907
   145
                                              data->texture[i],
slouken@1907
   146
                                              palette->colors, 0,
slouken@1907
   147
                                              palette->ncolors);
slouken@1907
   148
        }
slouken@1907
   149
    }
slouken@1907
   150
    return 0;
slouken@1907
   151
}
slouken@1907
   152
slouken@1895
   153
SDL_Renderer *
slouken@1918
   154
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   155
{
slouken@1895
   156
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   157
    SDL_DisplayMode *displayMode = &display->current_mode;
slouken@1895
   158
    SDL_Renderer *renderer;
slouken@1918
   159
    SW_RenderData *data;
slouken@1895
   160
    int i, n;
slouken@1895
   161
    int bpp;
slouken@1895
   162
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1907
   163
    Uint32 renderer_flags;
slouken@1929
   164
    const char *desired_driver;
slouken@1895
   165
slouken@1895
   166
    if (!SDL_PixelFormatEnumToMasks
slouken@1895
   167
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   168
        SDL_SetError("Unknown display format");
slouken@1895
   169
        return NULL;
slouken@1895
   170
    }
slouken@1895
   171
slouken@1895
   172
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   173
    if (!renderer) {
slouken@1895
   174
        SDL_OutOfMemory();
slouken@1895
   175
        return NULL;
slouken@1895
   176
    }
slouken@1895
   177
slouken@1920
   178
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   179
    if (!data) {
slouken@1918
   180
        SW_DestroyRenderer(renderer);
slouken@1895
   181
        SDL_OutOfMemory();
slouken@1895
   182
        return NULL;
slouken@1895
   183
    }
slouken@1970
   184
    renderer->ActivateRenderer = SW_ActivateRenderer;
slouken@1970
   185
    renderer->DisplayModeChanged = SW_DisplayModeChanged;
slouken@1918
   186
    renderer->CreateTexture = SW_CreateTexture;
slouken@1918
   187
    renderer->QueryTexturePixels = SW_QueryTexturePixels;
slouken@1918
   188
    renderer->SetTexturePalette = SW_SetTexturePalette;
slouken@1918
   189
    renderer->GetTexturePalette = SW_GetTexturePalette;
slouken@1918
   190
    renderer->UpdateTexture = SW_UpdateTexture;
slouken@1918
   191
    renderer->LockTexture = SW_LockTexture;
slouken@1918
   192
    renderer->UnlockTexture = SW_UnlockTexture;
slouken@1918
   193
    renderer->DirtyTexture = SW_DirtyTexture;
slouken@1918
   194
    renderer->RenderFill = SW_RenderFill;
slouken@1918
   195
    renderer->RenderCopy = SW_RenderCopy;
slouken@1918
   196
    renderer->RenderPresent = SW_RenderPresent;
slouken@1918
   197
    renderer->DestroyTexture = SW_DestroyTexture;
slouken@1918
   198
    renderer->DestroyRenderer = SW_DestroyRenderer;
slouken@1918
   199
    renderer->info = SW_RenderDriver.info;
slouken@1895
   200
    renderer->window = window->id;
slouken@1895
   201
    renderer->driverdata = data;
slouken@1895
   202
slouken@1907
   203
    renderer->info.flags = 0;
slouken@1895
   204
slouken@1965
   205
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1965
   206
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
slouken@1895
   207
        n = 2;
slouken@1965
   208
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1965
   209
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
slouken@1895
   210
        n = 3;
slouken@1895
   211
    } else {
slouken@1965
   212
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
slouken@1895
   213
        n = 1;
slouken@1895
   214
    }
slouken@1907
   215
    data->format = displayMode->format;
slouken@1895
   216
slouken@1895
   217
    /* Find a render driver that we can use to display data */
slouken@1965
   218
    renderer_flags = (SDL_RENDERER_SINGLEBUFFER |
slouken@1965
   219
                      SDL_RENDERER_PRESENTDISCARD);
slouken@1965
   220
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   221
        renderer_flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   222
    }
slouken@1929
   223
    desired_driver = SDL_getenv("SDL_VIDEO_RENDERER_SWDRIVER");
slouken@1895
   224
    for (i = 0; i < display->num_render_drivers; ++i) {
slouken@1895
   225
        SDL_RenderDriver *driver = &display->render_drivers[i];
slouken@1929
   226
        if (driver->info.name == SW_RenderDriver.info.name) {
slouken@1929
   227
            continue;
slouken@1929
   228
        }
slouken@1929
   229
        if (desired_driver
slouken@1929
   230
            && SDL_strcasecmp(desired_driver, driver->info.name) != 0) {
slouken@1929
   231
            continue;
slouken@1929
   232
        }
slouken@1929
   233
        data->renderer = driver->CreateRenderer(window, renderer_flags);
slouken@1929
   234
        if (data->renderer) {
slouken@1929
   235
            break;
slouken@1895
   236
        }
slouken@1895
   237
    }
slouken@1895
   238
    if (i == display->num_render_drivers) {
slouken@1918
   239
        SW_DestroyRenderer(renderer);
slouken@1895
   240
        SDL_SetError("Couldn't find display render driver");
slouken@1895
   241
        return NULL;
slouken@1895
   242
    }
slouken@1965
   243
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1965
   244
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   245
    }
slouken@1907
   246
slouken@1907
   247
    /* Create the textures we'll use for display */
slouken@1907
   248
    for (i = 0; i < n; ++i) {
slouken@1907
   249
        data->texture[i] =
slouken@1907
   250
            CreateTexture(data->renderer, data->format, window->w, window->h);
slouken@1907
   251
        if (!data->texture[i]) {
slouken@1918
   252
            SW_DestroyRenderer(renderer);
slouken@1907
   253
            return NULL;
slouken@1907
   254
        }
slouken@1907
   255
    }
slouken@1907
   256
    data->current_texture = 0;
slouken@1907
   257
slouken@1907
   258
    /* Create a surface we'll use for rendering */
slouken@1907
   259
    data->surface.flags = SDL_PREALLOC;
slouken@1907
   260
    data->surface.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@1907
   261
    if (!data->surface.format) {
slouken@1918
   262
        SW_DestroyRenderer(renderer);
slouken@1907
   263
        return NULL;
slouken@1907
   264
    }
slouken@1907
   265
    SDL_SetSurfacePalette(&data->surface, display->palette);
slouken@1907
   266
slouken@1907
   267
    /* Set up a palette watch on the display palette */
slouken@1907
   268
    if (display->palette) {
slouken@1907
   269
        SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
slouken@1907
   270
    }
slouken@1907
   271
slouken@1895
   272
    return renderer;
slouken@1895
   273
}
slouken@1895
   274
slouken@1895
   275
static int
slouken@1970
   276
SW_ActivateRenderer(SDL_Renderer * renderer)
slouken@1970
   277
{
slouken@1970
   278
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
   279
slouken@1970
   280
    if (data->renderer && data->renderer->ActivateRenderer) {
slouken@1970
   281
        if (data->renderer->ActivateRenderer(data->renderer) < 0) {
slouken@1970
   282
            return -1;
slouken@1970
   283
        }
slouken@1970
   284
    }
slouken@1970
   285
    return 0;
slouken@1970
   286
}
slouken@1970
   287
slouken@1970
   288
static int
slouken@1970
   289
SW_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1970
   290
{
slouken@1970
   291
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
   292
slouken@1970
   293
    if (data->renderer && data->renderer->DisplayModeChanged) {
slouken@1970
   294
        if (data->renderer->DisplayModeChanged(data->renderer) < 0) {
slouken@1970
   295
            return -1;
slouken@1970
   296
        }
slouken@1970
   297
    }
slouken@1970
   298
    return 0;
slouken@1970
   299
}
slouken@1970
   300
slouken@1970
   301
static int
slouken@1918
   302
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   303
{
slouken@1895
   304
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   305
        texture->driverdata = SDL_SW_CreateYUVTexture(texture);
slouken@1895
   306
    } else {
slouken@1895
   307
        int bpp;
slouken@1895
   308
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   309
slouken@1895
   310
        if (!SDL_PixelFormatEnumToMasks
slouken@1895
   311
            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   312
            SDL_SetError("Unknown texture format");
slouken@1895
   313
            return -1;
slouken@1895
   314
        }
slouken@1895
   315
slouken@1895
   316
        texture->driverdata =
slouken@1895
   317
            SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@1895
   318
                                 Bmask, Amask);
slouken@1895
   319
    }
slouken@1895
   320
slouken@1895
   321
    if (!texture->driverdata) {
slouken@1895
   322
        return -1;
slouken@1895
   323
    }
slouken@1895
   324
    return 0;
slouken@1895
   325
}
slouken@1895
   326
slouken@1895
   327
static int
slouken@1918
   328
SW_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   329
                      void **pixels, int *pitch)
slouken@1895
   330
{
slouken@1895
   331
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   332
        return SDL_SW_QueryYUVTexturePixels((SDL_SW_YUVTexture *) texture->
slouken@1895
   333
                                            driverdata, pixels, pitch);
slouken@1895
   334
    } else {
slouken@1895
   335
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   336
slouken@1895
   337
        *pixels = surface->pixels;
slouken@1895
   338
        *pitch = surface->pitch;
slouken@1895
   339
        return 0;
slouken@1895
   340
    }
slouken@1895
   341
}
slouken@1895
   342
slouken@1895
   343
static int
slouken@1918
   344
SW_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   345
                     const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   346
{
slouken@1895
   347
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   348
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   349
        return -1;
slouken@1895
   350
    } else {
slouken@1895
   351
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   352
slouken@1895
   353
        return SDL_SetPaletteColors(surface->format->palette, colors,
slouken@1895
   354
                                    firstcolor, ncolors);
slouken@1895
   355
    }
slouken@1895
   356
}
slouken@1895
   357
slouken@1895
   358
static int
slouken@1918
   359
SW_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   360
                     SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   361
{
slouken@1895
   362
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   363
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   364
        return -1;
slouken@1895
   365
    } else {
slouken@1895
   366
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   367
slouken@1895
   368
        SDL_memcpy(colors, &surface->format->palette->colors[firstcolor],
slouken@1895
   369
                   ncolors * sizeof(*colors));
slouken@1895
   370
        return 0;
slouken@1895
   371
    }
slouken@1895
   372
}
slouken@1895
   373
slouken@1895
   374
static int
slouken@1918
   375
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   376
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   377
{
slouken@1895
   378
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   379
        return SDL_SW_UpdateYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   380
                                       driverdata, rect, pixels, pitch);
slouken@1895
   381
    } else {
slouken@1895
   382
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   383
        Uint8 *src, *dst;
slouken@1895
   384
        int row;
slouken@1895
   385
        size_t length;
slouken@1895
   386
slouken@1895
   387
        src = (Uint8 *) pixels;
slouken@1895
   388
        dst =
slouken@1895
   389
            (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   390
            rect->x * surface->format->BytesPerPixel;
slouken@1895
   391
        length = rect->w * surface->format->BytesPerPixel;
slouken@1895
   392
        for (row = 0; row < rect->h; ++row) {
slouken@1895
   393
            SDL_memcpy(dst, src, length);
slouken@1895
   394
            src += pitch;
slouken@1895
   395
            dst += surface->pitch;
slouken@1895
   396
        }
slouken@1895
   397
        return 0;
slouken@1895
   398
    }
slouken@1895
   399
}
slouken@1895
   400
slouken@1895
   401
static int
slouken@1918
   402
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   403
               const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1918
   404
               int *pitch)
slouken@1895
   405
{
slouken@1895
   406
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   407
        return SDL_SW_LockYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   408
                                     driverdata, rect, markDirty, pixels,
slouken@1895
   409
                                     pitch);
slouken@1895
   410
    } else {
slouken@1895
   411
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   412
slouken@1895
   413
        *pixels =
slouken@1895
   414
            (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   415
                      rect->x * surface->format->BytesPerPixel);
slouken@1895
   416
        *pitch = surface->pitch;
slouken@1895
   417
        return 0;
slouken@1895
   418
    }
slouken@1895
   419
}
slouken@1895
   420
slouken@1895
   421
static void
slouken@1918
   422
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   423
{
slouken@1895
   424
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   425
        SDL_SW_UnlockYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   426
    }
slouken@1895
   427
}
slouken@1895
   428
slouken@1895
   429
static void
slouken@1918
   430
SW_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   431
                int numrects, const SDL_Rect * rects)
slouken@1895
   432
{
slouken@1895
   433
}
slouken@1895
   434
slouken@1895
   435
static int
slouken@1918
   436
SW_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1895
   437
{
slouken@1918
   438
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   439
    Uint8 r, g, b, a;
slouken@1907
   440
    SDL_Rect real_rect;
slouken@1907
   441
    int status;
slouken@1895
   442
slouken@1965
   443
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   444
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1897
   445
    }
slouken@1895
   446
slouken@1895
   447
    a = (Uint8) ((color >> 24) & 0xFF);
slouken@1895
   448
    r = (Uint8) ((color >> 16) & 0xFF);
slouken@1895
   449
    g = (Uint8) ((color >> 8) & 0xFF);
slouken@1895
   450
    b = (Uint8) (color & 0xFF);
slouken@1907
   451
    color = SDL_MapRGBA(data->surface.format, r, g, b, a);
slouken@1895
   452
slouken@1907
   453
    if (data->renderer->
slouken@1907
   454
        LockTexture(data->renderer, data->texture[data->current_texture],
slouken@1907
   455
                    rect, 1, &data->surface.pixels,
slouken@1907
   456
                    &data->surface.pitch) < 0) {
slouken@1907
   457
        return -1;
slouken@1907
   458
    }
slouken@1907
   459
    data->surface.w = rect->w;
slouken@1907
   460
    data->surface.h = rect->h;
slouken@1907
   461
    data->surface.clip_rect.w = rect->w;
slouken@1907
   462
    data->surface.clip_rect.h = rect->h;
slouken@1907
   463
    real_rect = data->surface.clip_rect;
slouken@1907
   464
slouken@1907
   465
    status = SDL_FillRect(&data->surface, &real_rect, color);
slouken@1907
   466
slouken@1907
   467
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   468
                                  data->texture[data->current_texture]);
slouken@1907
   469
    return status;
slouken@1895
   470
}
slouken@1895
   471
slouken@1895
   472
static int
slouken@1918
   473
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   474
              const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1918
   475
              int blendMode, int scaleMode)
slouken@1895
   476
{
slouken@1918
   477
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   478
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   479
    int status;
slouken@1895
   480
slouken@1965
   481
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1897
   482
        SDL_AddDirtyRect(&data->dirty, dstrect);
slouken@1897
   483
    }
slouken@1895
   484
slouken@1907
   485
    if (data->renderer->
slouken@1907
   486
        LockTexture(data->renderer, data->texture[data->current_texture],
slouken@1907
   487
                    dstrect, 1, &data->surface.pixels,
slouken@1907
   488
                    &data->surface.pitch) < 0) {
slouken@1907
   489
        return -1;
slouken@1907
   490
    }
slouken@1907
   491
slouken@1895
   492
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1907
   493
        status =
slouken@1907
   494
            SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
slouken@1907
   495
                                srcrect, data->format, dstrect->w, dstrect->h,
slouken@1907
   496
                                data->surface.pixels, data->surface.pitch);
slouken@1895
   497
    } else {
slouken@1895
   498
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   499
        SDL_Rect real_srcrect = *srcrect;
slouken@1907
   500
        SDL_Rect real_dstrect;
slouken@1907
   501
slouken@1907
   502
        data->surface.w = dstrect->w;
slouken@1907
   503
        data->surface.h = dstrect->h;
slouken@1907
   504
        data->surface.clip_rect.w = dstrect->w;
slouken@1907
   505
        data->surface.clip_rect.h = dstrect->h;
slouken@1907
   506
        real_dstrect = data->surface.clip_rect;
slouken@1895
   507
slouken@1895
   508
        if (blendMode &
slouken@1965
   509
            (SDL_TEXTUREBLENDMODE_MASK | SDL_TEXTUREBLENDMODE_BLEND)) {
slouken@1895
   510
            SDL_SetAlpha(surface, SDL_SRCALPHA, 0);
slouken@1895
   511
        } else {
slouken@1895
   512
            SDL_SetAlpha(surface, 0, 0);
slouken@1895
   513
        }
slouken@1965
   514
        if (scaleMode != SDL_TEXTURESCALEMODE_NONE &&
slouken@1895
   515
            (srcrect->w != dstrect->w || srcrect->h != dstrect->h)) {
slouken@1907
   516
            status =
slouken@1907
   517
                SDL_SoftStretch(surface, &real_srcrect, &data->surface,
slouken@1907
   518
                                &real_dstrect);
slouken@1895
   519
        } else {
slouken@1907
   520
            status =
slouken@1907
   521
                SDL_LowerBlit(surface, &real_srcrect, &data->surface,
slouken@1907
   522
                              &real_dstrect);
slouken@1895
   523
        }
slouken@1895
   524
    }
slouken@1907
   525
    data->renderer->UnlockTexture(data->renderer,
slouken@1907
   526
                                  data->texture[data->current_texture]);
slouken@1907
   527
    return status;
slouken@1895
   528
}
slouken@1895
   529
slouken@1895
   530
static void
slouken@1918
   531
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   532
{
slouken@1918
   533
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   534
    SDL_Texture *texture = data->texture[data->current_texture];
slouken@1895
   535
slouken@1895
   536
    /* Send the data to the display */
slouken@1965
   537
    if (data->renderer->info.flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1907
   538
        SDL_DirtyRect *dirty;
slouken@1907
   539
        for (dirty = data->dirty.list; dirty; dirty = dirty->next) {
slouken@1907
   540
            data->renderer->RenderCopy(data->renderer, texture, &dirty->rect,
slouken@1907
   541
                                       &dirty->rect,
slouken@1965
   542
                                       SDL_TEXTUREBLENDMODE_NONE,
slouken@1965
   543
                                       SDL_TEXTURESCALEMODE_NONE);
slouken@1907
   544
        }
slouken@1907
   545
        SDL_ClearDirtyRects(&data->dirty);
slouken@1907
   546
    } else {
slouken@1907
   547
        SDL_Rect rect;
slouken@1907
   548
        rect.x = 0;
slouken@1907
   549
        rect.y = 0;
slouken@1907
   550
        rect.w = texture->w;
slouken@1907
   551
        rect.h = texture->h;
slouken@1907
   552
        data->renderer->RenderCopy(data->renderer, texture, &rect, &rect,
slouken@1965
   553
                                   SDL_TEXTUREBLENDMODE_NONE,
slouken@1965
   554
                                   SDL_TEXTURESCALEMODE_NONE);
slouken@1895
   555
    }
slouken@1895
   556
    data->renderer->RenderPresent(data->renderer);
slouken@1895
   557
slouken@1895
   558
    /* Update the flipping chain, if any */
slouken@1965
   559
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1907
   560
        data->current_texture = (data->current_texture + 1) % 2;
slouken@1965
   561
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1907
   562
        data->current_texture = (data->current_texture + 1) % 3;
slouken@1895
   563
    }
slouken@1895
   564
}
slouken@1895
   565
slouken@1895
   566
static void
slouken@1918
   567
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   568
{
slouken@1895
   569
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   570
        SDL_SW_DestroyYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   571
    } else {
slouken@1895
   572
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   573
slouken@1895
   574
        SDL_FreeSurface(surface);
slouken@1895
   575
    }
slouken@1895
   576
}
slouken@1895
   577
slouken@1895
   578
static void
slouken@1918
   579
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   580
{
slouken@1918
   581
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1907
   582
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1907
   583
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   584
    int i;
slouken@1895
   585
slouken@1895
   586
    if (data) {
slouken@1907
   587
        for (i = 0; i < SDL_arraysize(data->texture); ++i) {
slouken@1907
   588
            if (data->texture[i]) {
slouken@1907
   589
                DestroyTexture(data->renderer, data->texture[i]);
slouken@1895
   590
            }
slouken@1895
   591
        }
slouken@1907
   592
        if (data->surface.format) {
slouken@1907
   593
            SDL_SetSurfacePalette(&data->surface, NULL);
slouken@1907
   594
            SDL_FreeFormat(data->surface.format);
slouken@1907
   595
        }
slouken@1907
   596
        if (display->palette) {
slouken@1907
   597
            SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged,
slouken@1907
   598
                                data);
slouken@1907
   599
        }
slouken@1907
   600
        if (data->renderer) {
slouken@1907
   601
            data->renderer->DestroyRenderer(data->renderer);
slouken@1907
   602
        }
slouken@1895
   603
        SDL_FreeDirtyRects(&data->dirty);
slouken@1895
   604
        SDL_free(data);
slouken@1895
   605
    }
slouken@1895
   606
    SDL_free(renderer);
slouken@1895
   607
}
slouken@1895
   608
slouken@1895
   609
/* vi: set ts=4 sw=4 expandtab: */