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