src/video/SDL_renderer_sw.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
child 1897 c2a27da60b18
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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@1895
    26
#include "SDL_rect_c.h"
slouken@1895
    27
#include "SDL_yuv_sw_c.h"
slouken@1895
    28
slouken@1895
    29
slouken@1895
    30
/* SDL surface based renderer implementation */
slouken@1895
    31
slouken@1895
    32
static SDL_Renderer *SDL_SW_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1895
    33
static int SDL_SW_CreateTexture(SDL_Renderer * renderer,
slouken@1895
    34
                                SDL_Texture * texture);
slouken@1895
    35
static int SDL_SW_QueryTexturePixels(SDL_Renderer * renderer,
slouken@1895
    36
                                     SDL_Texture * texture, void **pixels,
slouken@1895
    37
                                     int *pitch);
slouken@1895
    38
static int SDL_SW_SetTexturePalette(SDL_Renderer * renderer,
slouken@1895
    39
                                    SDL_Texture * texture,
slouken@1895
    40
                                    const SDL_Color * colors, int firstcolor,
slouken@1895
    41
                                    int ncolors);
slouken@1895
    42
static int SDL_SW_GetTexturePalette(SDL_Renderer * renderer,
slouken@1895
    43
                                    SDL_Texture * texture, SDL_Color * colors,
slouken@1895
    44
                                    int firstcolor, int ncolors);
slouken@1895
    45
static int SDL_SW_UpdateTexture(SDL_Renderer * renderer,
slouken@1895
    46
                                SDL_Texture * texture, const SDL_Rect * rect,
slouken@1895
    47
                                const void *pixels, int pitch);
slouken@1895
    48
static int SDL_SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
    49
                              const SDL_Rect * rect, int markDirty,
slouken@1895
    50
                              void **pixels, int *pitch);
slouken@1895
    51
static void SDL_SW_UnlockTexture(SDL_Renderer * renderer,
slouken@1895
    52
                                 SDL_Texture * texture);
slouken@1895
    53
static void SDL_SW_DirtyTexture(SDL_Renderer * renderer,
slouken@1895
    54
                                SDL_Texture * texture, int numrects,
slouken@1895
    55
                                const SDL_Rect * rects);
slouken@1895
    56
static void SDL_SW_SelectRenderTexture(SDL_Renderer * renderer,
slouken@1895
    57
                                       SDL_Texture * texture);
slouken@1895
    58
static int SDL_SW_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
    59
                             Uint32 color);
slouken@1895
    60
static int SDL_SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
    61
                             const SDL_Rect * srcrect,
slouken@1895
    62
                             const SDL_Rect * dstrect, int blendMode,
slouken@1895
    63
                             int scaleMode);
slouken@1895
    64
static int SDL_SW_RenderReadPixels(SDL_Renderer * renderer,
slouken@1895
    65
                                   const SDL_Rect * rect, void *pixels,
slouken@1895
    66
                                   int pitch);
slouken@1895
    67
static int SDL_SW_RenderWritePixels(SDL_Renderer * renderer,
slouken@1895
    68
                                    const SDL_Rect * rect, const void *pixels,
slouken@1895
    69
                                    int pitch);
slouken@1895
    70
static void SDL_SW_RenderPresent(SDL_Renderer * renderer);
slouken@1895
    71
static void SDL_SW_DestroyTexture(SDL_Renderer * renderer,
slouken@1895
    72
                                  SDL_Texture * texture);
slouken@1895
    73
static void SDL_SW_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    74
slouken@1895
    75
slouken@1895
    76
SDL_RenderDriver SDL_SW_RenderDriver = {
slouken@1895
    77
    SDL_SW_CreateRenderer,
slouken@1895
    78
    {
slouken@1895
    79
     "software",
slouken@1895
    80
     (SDL_Renderer_PresentDiscard |
slouken@1895
    81
      SDL_Renderer_PresentCopy |
slouken@1895
    82
      SDL_Renderer_PresentFlip2 |
slouken@1895
    83
      SDL_Renderer_PresentFlip3 | SDL_Renderer_RenderTarget),
slouken@1895
    84
     (SDL_TextureBlendMode_None |
slouken@1895
    85
      SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
slouken@1895
    86
     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
slouken@1895
    87
     11,
slouken@1895
    88
     {
slouken@1895
    89
      SDL_PixelFormat_Index8,
slouken@1895
    90
      SDL_PixelFormat_RGB555,
slouken@1895
    91
      SDL_PixelFormat_RGB565,
slouken@1895
    92
      SDL_PixelFormat_RGB888,
slouken@1895
    93
      SDL_PixelFormat_BGR888,
slouken@1895
    94
      SDL_PixelFormat_ARGB8888,
slouken@1895
    95
      SDL_PixelFormat_RGBA8888,
slouken@1895
    96
      SDL_PixelFormat_ABGR8888,
slouken@1895
    97
      SDL_PixelFormat_BGRA8888,
slouken@1895
    98
      SDL_PixelFormat_YUY2,
slouken@1895
    99
      SDL_PixelFormat_UYVY},
slouken@1895
   100
     0,
slouken@1895
   101
     0}
slouken@1895
   102
};
slouken@1895
   103
slouken@1895
   104
typedef struct
slouken@1895
   105
{
slouken@1895
   106
    int current_screen;
slouken@1895
   107
    SDL_Surface *screens[3];
slouken@1895
   108
    SDL_Surface *target;
slouken@1895
   109
    SDL_Renderer *renderer;
slouken@1895
   110
    SDL_DirtyRectList dirty;
slouken@1895
   111
} SDL_SW_RenderData;
slouken@1895
   112
slouken@1895
   113
SDL_Renderer *
slouken@1895
   114
SDL_SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   115
{
slouken@1895
   116
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   117
    SDL_DisplayMode *displayMode = &display->current_mode;
slouken@1895
   118
    SDL_Renderer *renderer;
slouken@1895
   119
    SDL_SW_RenderData *data;
slouken@1895
   120
    int i, n;
slouken@1895
   121
    int bpp;
slouken@1895
   122
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   123
slouken@1895
   124
    if (!SDL_PixelFormatEnumToMasks
slouken@1895
   125
        (displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   126
        SDL_SetError("Unknown display format");
slouken@1895
   127
        return NULL;
slouken@1895
   128
    }
slouken@1895
   129
slouken@1895
   130
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   131
    if (!renderer) {
slouken@1895
   132
        SDL_OutOfMemory();
slouken@1895
   133
        return NULL;
slouken@1895
   134
    }
slouken@1895
   135
slouken@1895
   136
    data = (SDL_SW_RenderData *) SDL_malloc(sizeof(*data));
slouken@1895
   137
    if (!data) {
slouken@1895
   138
        SDL_SW_DestroyRenderer(renderer);
slouken@1895
   139
        SDL_OutOfMemory();
slouken@1895
   140
        return NULL;
slouken@1895
   141
    }
slouken@1895
   142
    SDL_zerop(data);
slouken@1895
   143
slouken@1895
   144
    renderer->CreateTexture = SDL_SW_CreateTexture;
slouken@1895
   145
    renderer->QueryTexturePixels = SDL_SW_QueryTexturePixels;
slouken@1895
   146
    renderer->SetTexturePalette = SDL_SW_SetTexturePalette;
slouken@1895
   147
    renderer->GetTexturePalette = SDL_SW_GetTexturePalette;
slouken@1895
   148
    renderer->UpdateTexture = SDL_SW_UpdateTexture;
slouken@1895
   149
    renderer->LockTexture = SDL_SW_LockTexture;
slouken@1895
   150
    renderer->UnlockTexture = SDL_SW_UnlockTexture;
slouken@1895
   151
    renderer->DirtyTexture = SDL_SW_DirtyTexture;
slouken@1895
   152
    renderer->SelectRenderTexture = SDL_SW_SelectRenderTexture;
slouken@1895
   153
    renderer->RenderFill = SDL_SW_RenderFill;
slouken@1895
   154
    renderer->RenderCopy = SDL_SW_RenderCopy;
slouken@1895
   155
    renderer->RenderReadPixels = SDL_SW_RenderReadPixels;
slouken@1895
   156
    renderer->RenderWritePixels = SDL_SW_RenderWritePixels;
slouken@1895
   157
    renderer->RenderPresent = SDL_SW_RenderPresent;
slouken@1895
   158
    renderer->DestroyTexture = SDL_SW_DestroyTexture;
slouken@1895
   159
    renderer->DestroyRenderer = SDL_SW_DestroyRenderer;
slouken@1895
   160
    renderer->info = SDL_SW_RenderDriver.info;
slouken@1895
   161
    renderer->window = window->id;
slouken@1895
   162
    renderer->driverdata = data;
slouken@1895
   163
slouken@1895
   164
    renderer->info.flags = SDL_Renderer_RenderTarget;
slouken@1895
   165
slouken@1895
   166
    if (flags & SDL_Renderer_PresentFlip2) {
slouken@1895
   167
        renderer->info.flags |= SDL_Renderer_PresentFlip2;
slouken@1895
   168
        n = 2;
slouken@1895
   169
    } else if (flags & SDL_Renderer_PresentFlip3) {
slouken@1895
   170
        renderer->info.flags |= SDL_Renderer_PresentFlip3;
slouken@1895
   171
        n = 3;
slouken@1895
   172
    } else {
slouken@1895
   173
        renderer->info.flags |= SDL_Renderer_PresentCopy;
slouken@1895
   174
        n = 1;
slouken@1895
   175
    }
slouken@1895
   176
    for (i = 0; i < n; ++i) {
slouken@1895
   177
        data->screens[i] =
slouken@1895
   178
            SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
slouken@1895
   179
                                 Bmask, Amask);
slouken@1895
   180
        if (!data->screens[i]) {
slouken@1895
   181
            SDL_SW_DestroyRenderer(renderer);
slouken@1895
   182
            return NULL;
slouken@1895
   183
        }
slouken@1895
   184
        SDL_SetSurfacePalette(data->screens[i], display->palette);
slouken@1895
   185
    }
slouken@1895
   186
    data->current_screen = 0;
slouken@1895
   187
    data->target = data->screens[0];
slouken@1895
   188
slouken@1895
   189
    /* Find a render driver that we can use to display data */
slouken@1895
   190
    for (i = 0; i < display->num_render_drivers; ++i) {
slouken@1895
   191
        SDL_RenderDriver *driver = &display->render_drivers[i];
slouken@1895
   192
        if (driver->info.name != SDL_SW_RenderDriver.info.name) {
slouken@1895
   193
            data->renderer =
slouken@1895
   194
                driver->CreateRenderer(window, SDL_Renderer_PresentDiscard);
slouken@1895
   195
            if (data->renderer) {
slouken@1895
   196
                break;
slouken@1895
   197
            }
slouken@1895
   198
        }
slouken@1895
   199
    }
slouken@1895
   200
    if (i == display->num_render_drivers) {
slouken@1895
   201
        SDL_SW_DestroyRenderer(renderer);
slouken@1895
   202
        SDL_SetError("Couldn't find display render driver");
slouken@1895
   203
        return NULL;
slouken@1895
   204
    }
slouken@1895
   205
    return renderer;
slouken@1895
   206
}
slouken@1895
   207
slouken@1895
   208
static int
slouken@1895
   209
SDL_SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   210
{
slouken@1895
   211
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   212
        if (texture->access == SDL_TextureAccess_Render) {
slouken@1895
   213
            SDL_SetError("Rendering to YUV format textures is not supported");
slouken@1895
   214
            return -1;
slouken@1895
   215
        }
slouken@1895
   216
        texture->driverdata = SDL_SW_CreateYUVTexture(texture);
slouken@1895
   217
    } else {
slouken@1895
   218
        int bpp;
slouken@1895
   219
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   220
slouken@1895
   221
        if (!SDL_PixelFormatEnumToMasks
slouken@1895
   222
            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1895
   223
            SDL_SetError("Unknown texture format");
slouken@1895
   224
            return -1;
slouken@1895
   225
        }
slouken@1895
   226
slouken@1895
   227
        texture->driverdata =
slouken@1895
   228
            SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@1895
   229
                                 Bmask, Amask);
slouken@1895
   230
    }
slouken@1895
   231
slouken@1895
   232
    if (!texture->driverdata) {
slouken@1895
   233
        return -1;
slouken@1895
   234
    }
slouken@1895
   235
    return 0;
slouken@1895
   236
}
slouken@1895
   237
slouken@1895
   238
static int
slouken@1895
   239
SDL_SW_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   240
                          void **pixels, int *pitch)
slouken@1895
   241
{
slouken@1895
   242
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   243
        return SDL_SW_QueryYUVTexturePixels((SDL_SW_YUVTexture *) texture->
slouken@1895
   244
                                            driverdata, pixels, pitch);
slouken@1895
   245
    } else {
slouken@1895
   246
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   247
slouken@1895
   248
        *pixels = surface->pixels;
slouken@1895
   249
        *pitch = surface->pitch;
slouken@1895
   250
        return 0;
slouken@1895
   251
    }
slouken@1895
   252
}
slouken@1895
   253
slouken@1895
   254
static int
slouken@1895
   255
SDL_SW_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   256
                         const SDL_Color * colors, int firstcolor,
slouken@1895
   257
                         int ncolors)
slouken@1895
   258
{
slouken@1895
   259
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   260
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   261
        return -1;
slouken@1895
   262
    } else {
slouken@1895
   263
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   264
slouken@1895
   265
        return SDL_SetPaletteColors(surface->format->palette, colors,
slouken@1895
   266
                                    firstcolor, ncolors);
slouken@1895
   267
    }
slouken@1895
   268
}
slouken@1895
   269
slouken@1895
   270
static int
slouken@1895
   271
SDL_SW_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   272
                         SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   273
{
slouken@1895
   274
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   275
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   276
        return -1;
slouken@1895
   277
    } else {
slouken@1895
   278
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   279
slouken@1895
   280
        SDL_memcpy(colors, &surface->format->palette->colors[firstcolor],
slouken@1895
   281
                   ncolors * sizeof(*colors));
slouken@1895
   282
        return 0;
slouken@1895
   283
    }
slouken@1895
   284
}
slouken@1895
   285
slouken@1895
   286
static int
slouken@1895
   287
SDL_SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   288
                     const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   289
{
slouken@1895
   290
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   291
        return SDL_SW_UpdateYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   292
                                       driverdata, rect, pixels, pitch);
slouken@1895
   293
    } else {
slouken@1895
   294
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   295
        Uint8 *src, *dst;
slouken@1895
   296
        int row;
slouken@1895
   297
        size_t length;
slouken@1895
   298
slouken@1895
   299
        src = (Uint8 *) pixels;
slouken@1895
   300
        dst =
slouken@1895
   301
            (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   302
            rect->x * surface->format->BytesPerPixel;
slouken@1895
   303
        length = rect->w * surface->format->BytesPerPixel;
slouken@1895
   304
        for (row = 0; row < rect->h; ++row) {
slouken@1895
   305
            SDL_memcpy(dst, src, length);
slouken@1895
   306
            src += pitch;
slouken@1895
   307
            dst += surface->pitch;
slouken@1895
   308
        }
slouken@1895
   309
        return 0;
slouken@1895
   310
    }
slouken@1895
   311
}
slouken@1895
   312
slouken@1895
   313
static int
slouken@1895
   314
SDL_SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   315
                   const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1895
   316
                   int *pitch)
slouken@1895
   317
{
slouken@1895
   318
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   319
        return SDL_SW_LockYUVTexture((SDL_SW_YUVTexture *) texture->
slouken@1895
   320
                                     driverdata, rect, markDirty, pixels,
slouken@1895
   321
                                     pitch);
slouken@1895
   322
    } else {
slouken@1895
   323
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   324
slouken@1895
   325
        *pixels =
slouken@1895
   326
            (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   327
                      rect->x * surface->format->BytesPerPixel);
slouken@1895
   328
        *pitch = surface->pitch;
slouken@1895
   329
        return 0;
slouken@1895
   330
    }
slouken@1895
   331
}
slouken@1895
   332
slouken@1895
   333
static void
slouken@1895
   334
SDL_SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   335
{
slouken@1895
   336
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   337
        SDL_SW_UnlockYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   338
    }
slouken@1895
   339
}
slouken@1895
   340
slouken@1895
   341
static void
slouken@1895
   342
SDL_SW_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   343
                    int numrects, const SDL_Rect * rects)
slouken@1895
   344
{
slouken@1895
   345
}
slouken@1895
   346
slouken@1895
   347
static void
slouken@1895
   348
SDL_SW_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   349
{
slouken@1895
   350
    SDL_SW_RenderData *data = (SDL_SW_RenderData *) renderer->driverdata;
slouken@1895
   351
slouken@1895
   352
    if (texture) {
slouken@1895
   353
        data->target = (SDL_Surface *) texture->driverdata;
slouken@1895
   354
    } else {
slouken@1895
   355
        data->target = data->screens[data->current_screen];
slouken@1895
   356
    }
slouken@1895
   357
}
slouken@1895
   358
slouken@1895
   359
static int
slouken@1895
   360
SDL_SW_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
   361
                  Uint32 color)
slouken@1895
   362
{
slouken@1895
   363
    SDL_SW_RenderData *data = (SDL_SW_RenderData *) renderer->driverdata;
slouken@1895
   364
    SDL_Rect real_rect = *rect;
slouken@1895
   365
    Uint8 r, g, b, a;
slouken@1895
   366
slouken@1895
   367
    SDL_AddDirtyRect(&data->dirty, rect);
slouken@1895
   368
slouken@1895
   369
    a = (Uint8) ((color >> 24) & 0xFF);
slouken@1895
   370
    r = (Uint8) ((color >> 16) & 0xFF);
slouken@1895
   371
    g = (Uint8) ((color >> 8) & 0xFF);
slouken@1895
   372
    b = (Uint8) (color & 0xFF);
slouken@1895
   373
    color = SDL_MapRGBA(data->target->format, r, g, b, a);
slouken@1895
   374
slouken@1895
   375
    return SDL_FillRect(data->target, &real_rect, color);
slouken@1895
   376
}
slouken@1895
   377
slouken@1895
   378
static int
slouken@1895
   379
SDL_SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   380
                  const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1895
   381
                  int blendMode, int scaleMode)
slouken@1895
   382
{
slouken@1895
   383
    SDL_SW_RenderData *data = (SDL_SW_RenderData *) renderer->driverdata;
slouken@1895
   384
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1895
   385
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   386
slouken@1895
   387
    SDL_AddDirtyRect(&data->dirty, dstrect);
slouken@1895
   388
slouken@1895
   389
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   390
        SDL_Surface *target = data->target;
slouken@1895
   391
        void *pixels =
slouken@1895
   392
            (Uint8 *) target->pixels + dstrect->y * target->pitch +
slouken@1895
   393
            dstrect->x * target->format->BytesPerPixel;
slouken@1895
   394
        return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
slouken@1895
   395
                                   srcrect, display->current_mode.format,
slouken@1895
   396
                                   dstrect->w, dstrect->h, pixels,
slouken@1895
   397
                                   target->pitch);
slouken@1895
   398
    } else {
slouken@1895
   399
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   400
        SDL_Rect real_srcrect = *srcrect;
slouken@1895
   401
        SDL_Rect real_dstrect = *dstrect;
slouken@1895
   402
slouken@1895
   403
        if (blendMode &
slouken@1895
   404
            (SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend)) {
slouken@1895
   405
            SDL_SetAlpha(surface, SDL_SRCALPHA, 0);
slouken@1895
   406
        } else {
slouken@1895
   407
            SDL_SetAlpha(surface, 0, 0);
slouken@1895
   408
        }
slouken@1895
   409
        if (scaleMode != SDL_TextureScaleMode_None &&
slouken@1895
   410
            (srcrect->w != dstrect->w || srcrect->h != dstrect->h)) {
slouken@1895
   411
            return SDL_SoftStretch(surface, &real_srcrect, data->target,
slouken@1895
   412
                                   &real_dstrect);
slouken@1895
   413
        } else {
slouken@1895
   414
            return SDL_LowerBlit(surface, &real_srcrect, data->target,
slouken@1895
   415
                                 &real_dstrect);
slouken@1895
   416
        }
slouken@1895
   417
    }
slouken@1895
   418
}
slouken@1895
   419
slouken@1895
   420
static int
slouken@1895
   421
SDL_SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
   422
                        void *pixels, int pitch)
slouken@1895
   423
{
slouken@1895
   424
    SDL_SW_RenderData *data = (SDL_SW_RenderData *) renderer->driverdata;
slouken@1895
   425
    SDL_Surface *surface = data->target;
slouken@1895
   426
    Uint8 *src, *dst;
slouken@1895
   427
    int row;
slouken@1895
   428
    size_t length;
slouken@1895
   429
slouken@1895
   430
    src =
slouken@1895
   431
        (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   432
        rect->x * surface->format->BytesPerPixel;
slouken@1895
   433
    dst = (Uint8 *) pixels;
slouken@1895
   434
    length = rect->w * surface->format->BytesPerPixel;
slouken@1895
   435
    for (row = 0; row < rect->h; ++row) {
slouken@1895
   436
        SDL_memcpy(dst, src, length);
slouken@1895
   437
        src += surface->pitch;
slouken@1895
   438
        dst += pitch;
slouken@1895
   439
    }
slouken@1895
   440
    return 0;
slouken@1895
   441
}
slouken@1895
   442
slouken@1895
   443
static int
slouken@1895
   444
SDL_SW_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
   445
                         const void *pixels, int pitch)
slouken@1895
   446
{
slouken@1895
   447
    SDL_SW_RenderData *data = (SDL_SW_RenderData *) renderer->driverdata;
slouken@1895
   448
    SDL_Surface *surface = data->target;
slouken@1895
   449
    Uint8 *src, *dst;
slouken@1895
   450
    int row;
slouken@1895
   451
    size_t length;
slouken@1895
   452
slouken@1895
   453
    SDL_AddDirtyRect(&data->dirty, rect);
slouken@1895
   454
slouken@1895
   455
    src = (Uint8 *) pixels;
slouken@1895
   456
    dst =
slouken@1895
   457
        (Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@1895
   458
        rect->x * surface->format->BytesPerPixel;
slouken@1895
   459
    length = rect->w * surface->format->BytesPerPixel;
slouken@1895
   460
    for (row = 0; row < rect->h; ++row) {
slouken@1895
   461
        SDL_memcpy(dst, src, length);
slouken@1895
   462
        src += pitch;
slouken@1895
   463
        dst += surface->pitch;
slouken@1895
   464
    }
slouken@1895
   465
    return 0;
slouken@1895
   466
}
slouken@1895
   467
slouken@1895
   468
static void
slouken@1895
   469
SDL_SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   470
{
slouken@1895
   471
    SDL_SW_RenderData *data = (SDL_SW_RenderData *) renderer->driverdata;
slouken@1895
   472
    SDL_Surface *surface = data->screens[data->current_screen];
slouken@1895
   473
    SDL_DirtyRect *dirty;
slouken@1895
   474
    int new_screen;
slouken@1895
   475
slouken@1895
   476
    /* Send the data to the display */
slouken@1895
   477
    for (dirty = data->dirty.list; dirty; dirty = dirty->next) {
slouken@1895
   478
        void *pixels =
slouken@1895
   479
            (void *) ((Uint8 *) surface->pixels +
slouken@1895
   480
                      dirty->rect.y * surface->pitch +
slouken@1895
   481
                      dirty->rect.x * surface->format->BytesPerPixel);
slouken@1895
   482
        data->renderer->RenderWritePixels(data->renderer, &dirty->rect,
slouken@1895
   483
                                          pixels, surface->pitch);
slouken@1895
   484
    }
slouken@1895
   485
    SDL_ClearDirtyRects(&data->dirty);
slouken@1895
   486
    data->renderer->RenderPresent(data->renderer);
slouken@1895
   487
slouken@1895
   488
slouken@1895
   489
    /* Update the flipping chain, if any */
slouken@1895
   490
    if (renderer->info.flags & SDL_Renderer_PresentFlip2) {
slouken@1895
   491
        new_screen = (data->current_screen + 1) % 2;
slouken@1895
   492
    } else if (renderer->info.flags & SDL_Renderer_PresentFlip3) {
slouken@1895
   493
        new_screen = (data->current_screen + 1) % 3;
slouken@1895
   494
    } else {
slouken@1895
   495
        new_screen = 0;
slouken@1895
   496
    }
slouken@1895
   497
    if (data->target == data->screens[data->current_screen]) {
slouken@1895
   498
        data->target = data->screens[new_screen];
slouken@1895
   499
    }
slouken@1895
   500
    data->current_screen = new_screen;
slouken@1895
   501
}
slouken@1895
   502
slouken@1895
   503
static void
slouken@1895
   504
SDL_SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   505
{
slouken@1895
   506
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   507
        SDL_SW_DestroyYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
slouken@1895
   508
    } else {
slouken@1895
   509
        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   510
slouken@1895
   511
        SDL_FreeSurface(surface);
slouken@1895
   512
    }
slouken@1895
   513
}
slouken@1895
   514
slouken@1895
   515
static void
slouken@1895
   516
SDL_SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   517
{
slouken@1895
   518
    SDL_SW_RenderData *data = (SDL_SW_RenderData *) renderer->driverdata;
slouken@1895
   519
    int i;
slouken@1895
   520
slouken@1895
   521
    if (data) {
slouken@1895
   522
        for (i = 0; i < SDL_arraysize(data->screens); ++i) {
slouken@1895
   523
            if (data->screens[i]) {
slouken@1895
   524
                SDL_FreeSurface(data->screens[i]);
slouken@1895
   525
            }
slouken@1895
   526
        }
slouken@1895
   527
        SDL_FreeDirtyRects(&data->dirty);
slouken@1895
   528
        SDL_free(data);
slouken@1895
   529
    }
slouken@1895
   530
    SDL_free(renderer);
slouken@1895
   531
}
slouken@1895
   532
slouken@1895
   533
/* vi: set ts=4 sw=4 expandtab: */