src/render/software/SDL_render_sw.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 05 Feb 2011 12:01:11 -0800
changeset 5195 bb45ecd958d8
parent 5166 src/render/software/SDL_renderer_sw.c@4d39eeaad00b
child 5224 2178ffe17222
permissions -rw-r--r--
Renamed files for consistency
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@5154
    24
#include "../SDL_sysrender.h"
slouken@5154
    25
#include "../../video/SDL_pixels_c.h"
slouken@1895
    26
slouken@5163
    27
#include "SDL_draw.h"
slouken@5163
    28
#include "SDL_blendfillrect.h"
slouken@5163
    29
#include "SDL_blendline.h"
slouken@5163
    30
#include "SDL_blendpoint.h"
slouken@5163
    31
#include "SDL_drawline.h"
slouken@5163
    32
#include "SDL_drawpoint.h"
slouken@5163
    33
slouken@1895
    34
slouken@1895
    35
/* SDL surface based renderer implementation */
slouken@1895
    36
slouken@1918
    37
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5147
    38
static void SW_WindowEvent(SDL_Renderer * renderer,
slouken@5147
    39
                           const SDL_WindowEvent *event);
slouken@1918
    40
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1985
    41
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    42
                                 SDL_Texture * texture);
slouken@1985
    43
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    44
                                 SDL_Texture * texture);
slouken@5141
    45
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@5141
    46
                                  SDL_Texture * texture);
slouken@1985
    47
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    48
                            const SDL_Rect * rect, const void *pixels,
slouken@1985
    49
                            int pitch);
slouken@1918
    50
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
    51
                          const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1918
    52
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@3596
    53
static int SW_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3596
    54
                               const SDL_Point * points, int count);
slouken@3596
    55
static int SW_RenderDrawLines(SDL_Renderer * renderer,
slouken@3596
    56
                              const SDL_Point * points, int count);
slouken@3596
    57
static int SW_RenderFillRects(SDL_Renderer * renderer,
slouken@3596
    58
                              const SDL_Rect ** rects, int count);
slouken@1918
    59
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    60
                         const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3427
    61
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
    62
                               Uint32 format, void * pixels, int pitch);
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@5166
    72
     0,
slouken@5156
    73
     8,
slouken@1895
    74
     {
slouken@1965
    75
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    76
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    77
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    78
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    79
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    80
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
    81
      SDL_PIXELFORMAT_ABGR8888,
slouken@5156
    82
      SDL_PIXELFORMAT_BGRA8888
slouken@5156
    83
     },
slouken@1895
    84
     0,
slouken@1895
    85
     0}
slouken@1895
    86
};
slouken@1895
    87
slouken@1895
    88
typedef struct
slouken@1895
    89
{
slouken@1972
    90
    SDL_bool updateSize;
slouken@5166
    91
    SDL_Surface *surface;
slouken@1918
    92
} SW_RenderData;
slouken@1895
    93
slouken@1907
    94
slouken@1895
    95
SDL_Renderer *
slouken@5166
    96
SW_CreateRendererForSurface(SDL_Surface * surface)
slouken@1895
    97
{
slouken@1895
    98
    SDL_Renderer *renderer;
slouken@1918
    99
    SW_RenderData *data;
slouken@1895
   100
slouken@5166
   101
    if (!surface) {
slouken@5166
   102
        SDL_SetError("Can't create renderer for NULL surface");
slouken@1895
   103
        return NULL;
slouken@1895
   104
    }
slouken@1895
   105
slouken@1895
   106
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   107
    if (!renderer) {
slouken@1895
   108
        SDL_OutOfMemory();
slouken@1895
   109
        return NULL;
slouken@1895
   110
    }
slouken@1895
   111
slouken@1920
   112
    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   113
    if (!data) {
slouken@1918
   114
        SW_DestroyRenderer(renderer);
slouken@1895
   115
        SDL_OutOfMemory();
slouken@1895
   116
        return NULL;
slouken@1895
   117
    }
slouken@5166
   118
    data->surface = surface;
slouken@5166
   119
slouken@5147
   120
    renderer->WindowEvent = SW_WindowEvent;
slouken@5154
   121
    renderer->CreateTexture = SW_CreateTexture;
slouken@5154
   122
    renderer->SetTextureColorMod = SW_SetTextureColorMod;
slouken@5154
   123
    renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
slouken@5154
   124
    renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
slouken@5154
   125
    renderer->UpdateTexture = SW_UpdateTexture;
slouken@5154
   126
    renderer->LockTexture = SW_LockTexture;
slouken@5154
   127
    renderer->UnlockTexture = SW_UnlockTexture;
slouken@5154
   128
    renderer->DestroyTexture = SW_DestroyTexture;
slouken@3596
   129
    renderer->RenderDrawPoints = SW_RenderDrawPoints;
slouken@3596
   130
    renderer->RenderDrawLines = SW_RenderDrawLines;
slouken@3596
   131
    renderer->RenderFillRects = SW_RenderFillRects;
slouken@1918
   132
    renderer->RenderCopy = SW_RenderCopy;
slouken@3427
   133
    renderer->RenderReadPixels = SW_RenderReadPixels;
slouken@1918
   134
    renderer->RenderPresent = SW_RenderPresent;
slouken@1918
   135
    renderer->DestroyRenderer = SW_DestroyRenderer;
slouken@5154
   136
    renderer->info = SW_RenderDriver.info;
slouken@1895
   137
    renderer->driverdata = data;
slouken@1895
   138
slouken@1895
   139
    return renderer;
slouken@1895
   140
}
slouken@1895
   141
slouken@5166
   142
SDL_Renderer *
slouken@5166
   143
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@5166
   144
{
slouken@5166
   145
    SDL_Surface *surface;
slouken@5166
   146
slouken@5166
   147
    surface = SDL_GetWindowSurface(window);
slouken@5166
   148
    if (!surface) {
slouken@5166
   149
        return NULL;
slouken@5166
   150
    }
slouken@5166
   151
    return SW_CreateRendererForSurface(surface);
slouken@5166
   152
}
slouken@5166
   153
slouken@5166
   154
static SDL_Surface *
slouken@1970
   155
SW_ActivateRenderer(SDL_Renderer * renderer)
slouken@1970
   156
{
slouken@1970
   157
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@3685
   158
    SDL_Window *window = renderer->window;
slouken@1970
   159
slouken@1972
   160
    if (data->updateSize) {
slouken@5166
   161
        data->surface = SDL_GetWindowSurface(window);
slouken@5166
   162
        data->updateSize = SDL_FALSE;
slouken@1972
   163
    }
slouken@5166
   164
    return data->surface;
slouken@1970
   165
}
slouken@1970
   166
slouken@5147
   167
static void
slouken@5147
   168
SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1970
   169
{
slouken@1970
   170
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1970
   171
slouken@5147
   172
    if (event->event == SDL_WINDOWEVENT_RESIZED) {
slouken@5147
   173
        data->updateSize = SDL_TRUE;
slouken@1970
   174
    }
slouken@1970
   175
}
slouken@1970
   176
slouken@1970
   177
static int
slouken@1918
   178
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   179
{
slouken@5156
   180
    int bpp;
slouken@5156
   181
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   182
slouken@5156
   183
    if (!SDL_PixelFormatEnumToMasks
slouken@5156
   184
        (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5156
   185
        SDL_SetError("Unknown texture format");
slouken@5156
   186
        return -1;
slouken@5156
   187
    }
slouken@1895
   188
slouken@5156
   189
    texture->driverdata =
slouken@5156
   190
        SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
slouken@5156
   191
                             Bmask, Amask);
slouken@5156
   192
    SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
slouken@5156
   193
                           texture->b);
slouken@5156
   194
    SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
slouken@5156
   195
    SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
slouken@3053
   196
slouken@5156
   197
    if (texture->access == SDL_TEXTUREACCESS_STATIC) {
slouken@5156
   198
        SDL_SetSurfaceRLE(texture->driverdata, 1);
slouken@1895
   199
    }
slouken@1895
   200
slouken@1895
   201
    if (!texture->driverdata) {
slouken@1895
   202
        return -1;
slouken@1895
   203
    }
slouken@1895
   204
    return 0;
slouken@1895
   205
}
slouken@1895
   206
slouken@1895
   207
static int
slouken@1985
   208
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   209
{
slouken@2267
   210
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   211
    return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
slouken@2267
   212
                                  texture->b);
slouken@1985
   213
}
slouken@1985
   214
slouken@1985
   215
static int
slouken@1985
   216
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   217
{
slouken@2267
   218
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   219
    return SDL_SetSurfaceAlphaMod(surface, texture->a);
slouken@1985
   220
}
slouken@1985
   221
slouken@1985
   222
static int
slouken@1985
   223
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   224
{
slouken@2267
   225
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@2267
   226
    return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
slouken@1985
   227
}
slouken@1985
   228
slouken@1985
   229
static int
slouken@1918
   230
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1918
   231
                 const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   232
{
slouken@5156
   233
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@5156
   234
    Uint8 *src, *dst;
slouken@5156
   235
    int row;
slouken@5156
   236
    size_t length;
slouken@1895
   237
slouken@5156
   238
    src = (Uint8 *) pixels;
slouken@5156
   239
    dst = (Uint8 *) surface->pixels +
slouken@5156
   240
                        rect->y * surface->pitch +
slouken@5156
   241
                        rect->x * surface->format->BytesPerPixel;
slouken@5156
   242
    length = rect->w * surface->format->BytesPerPixel;
slouken@5156
   243
    for (row = 0; row < rect->h; ++row) {
slouken@5156
   244
        SDL_memcpy(dst, src, length);
slouken@5156
   245
        src += pitch;
slouken@5156
   246
        dst += surface->pitch;
slouken@1895
   247
    }
slouken@5156
   248
    return 0;
slouken@1895
   249
}
slouken@1895
   250
slouken@1895
   251
static int
slouken@1918
   252
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   253
               const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   254
{
slouken@5156
   255
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   256
slouken@5156
   257
    *pixels =
slouken@5156
   258
        (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
slouken@5156
   259
                  rect->x * surface->format->BytesPerPixel);
slouken@5156
   260
    *pitch = surface->pitch;
slouken@5156
   261
    return 0;
slouken@1895
   262
}
slouken@1895
   263
slouken@1895
   264
static void
slouken@1918
   265
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   266
{
slouken@1895
   267
}
slouken@1895
   268
slouken@1895
   269
static int
slouken@3596
   270
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   271
                    int count)
slouken@2901
   272
{
slouken@5166
   273
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@2901
   274
slouken@5166
   275
    if (!surface) {
slouken@5147
   276
        return -1;
slouken@5147
   277
    }
slouken@5147
   278
slouken@3536
   279
    /* Draw the points! */
slouken@5140
   280
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   281
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   282
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   283
                                   renderer->a);
slouken@2888
   284
slouken@5166
   285
        return SDL_DrawPoints(surface, points, count, color);
slouken@2888
   286
    } else {
slouken@5166
   287
        return SDL_BlendPoints(surface, points, count,
slouken@5166
   288
                               renderer->blendMode,
slouken@5166
   289
                               renderer->r, renderer->g, renderer->b,
slouken@5166
   290
                               renderer->a);
slouken@2888
   291
    }
slouken@2888
   292
}
slouken@2888
   293
slouken@2888
   294
static int
slouken@3596
   295
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3596
   296
                   int count)
slouken@1895
   297
{
slouken@5166
   298
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@3536
   299
slouken@5166
   300
    if (!surface) {
slouken@5147
   301
        return -1;
slouken@5147
   302
    }
slouken@5147
   303
slouken@5166
   304
    /* Draw the lines! */
slouken@5140
   305
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   306
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@3536
   307
                                   renderer->r, renderer->g, renderer->b,
slouken@3536
   308
                                   renderer->a);
slouken@2888
   309
slouken@5166
   310
        return SDL_DrawLines(surface, points, count, color);
slouken@2888
   311
    } else {
slouken@5166
   312
        return SDL_BlendLines(surface, points, count,
slouken@5166
   313
                              renderer->blendMode,
slouken@5166
   314
                              renderer->r, renderer->g, renderer->b,
slouken@5166
   315
                              renderer->a);
slouken@2888
   316
    }
slouken@3536
   317
}
slouken@3536
   318
slouken@3536
   319
static int
slouken@3596
   320
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
slouken@3596
   321
                   int count)
slouken@3536
   322
{
slouken@5166
   323
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@3536
   324
slouken@5166
   325
    if (!surface) {
slouken@5147
   326
        return -1;
slouken@5147
   327
    }
slouken@5147
   328
slouken@5140
   329
    if (renderer->blendMode == SDL_BLENDMODE_NONE) {
slouken@5166
   330
        Uint32 color = SDL_MapRGBA(surface->format,
slouken@5166
   331
                                   renderer->r, renderer->g, renderer->b,
slouken@5166
   332
                                   renderer->a);
slouken@5166
   333
        return SDL_FillRects(surface, rects, count, color);
slouken@5166
   334
    } else {
slouken@5166
   335
        return SDL_BlendFillRects(surface, rects, count,
slouken@5166
   336
                                     renderer->blendMode,
slouken@5166
   337
                                     renderer->r, renderer->g, renderer->b,
slouken@5166
   338
                                     renderer->a);
slouken@3536
   339
    }
slouken@1895
   340
}
slouken@1895
   341
slouken@1895
   342
static int
slouken@1918
   343
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   344
              const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   345
{
slouken@5166
   346
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   347
    SDL_Surface *src = (SDL_Surface *) texture->driverdata;
slouken@5166
   348
    SDL_Rect final_rect = *dstrect;
slouken@1895
   349
slouken@5166
   350
    if (!surface) {
slouken@5147
   351
        return -1;
slouken@5147
   352
    }
slouken@5166
   353
    return SDL_BlitSurface(src, srcrect, surface, &final_rect);
slouken@1895
   354
}
slouken@1895
   355
slouken@3427
   356
static int
slouken@3427
   357
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3435
   358
                    Uint32 format, void * pixels, int pitch)
slouken@3427
   359
{
slouken@5166
   360
    SDL_Surface *surface = SW_ActivateRenderer(renderer);
slouken@5166
   361
    Uint32 src_format;
slouken@5166
   362
    void *src_pixels;
slouken@3427
   363
slouken@5166
   364
    if (!surface) {
slouken@5147
   365
        return -1;
slouken@5147
   366
    }
slouken@5147
   367
slouken@5166
   368
    if (rect->x < 0 || rect->x+rect->w > surface->w ||
slouken@5166
   369
        rect->y < 0 || rect->y+rect->h > surface->h) {
slouken@5166
   370
        SDL_SetError("Tried to read outside of surface bounds");
slouken@3427
   371
        return -1;
slouken@3427
   372
    }
slouken@3427
   373
slouken@5166
   374
    src_format = SDL_MasksToPixelFormatEnum(
slouken@5166
   375
                    surface->format->BitsPerPixel,
slouken@5166
   376
                    surface->format->Rmask, surface->format->Gmask,
slouken@5166
   377
                    surface->format->Bmask, surface->format->Amask);
slouken@3427
   378
slouken@5166
   379
    src_pixels = (void*)((Uint8 *) surface->pixels +
slouken@5166
   380
                    rect->y * surface->pitch +
slouken@5166
   381
                    rect->x * surface->format->BytesPerPixel);
slouken@5166
   382
slouken@5166
   383
    return SDL_ConvertPixels(rect->w, rect->h,
slouken@5166
   384
                             src_format, src_pixels, surface->pitch,
slouken@5166
   385
                             format, pixels, pitch);
slouken@3427
   386
}
slouken@3427
   387
slouken@1895
   388
static void
slouken@1918
   389
SW_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   390
{
slouken@1918
   391
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@5166
   392
    SDL_Window *window = renderer->window;
slouken@1895
   393
slouken@5166
   394
    if (window) {
slouken@5166
   395
        SDL_UpdateWindowSurface(window);
slouken@5147
   396
    }
slouken@1895
   397
}
slouken@1895
   398
slouken@1895
   399
static void
slouken@1918
   400
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   401
{
slouken@5156
   402
    SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
slouken@1895
   403
slouken@5156
   404
    SDL_FreeSurface(surface);
slouken@1895
   405
}
slouken@1895
   406
slouken@1895
   407
static void
slouken@1918
   408
SW_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   409
{
slouken@1918
   410
    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
slouken@1895
   411
slouken@1895
   412
    if (data) {
slouken@1895
   413
        SDL_free(data);
slouken@1895
   414
    }
slouken@1895
   415
    SDL_free(renderer);
slouken@1895
   416
}
slouken@1895
   417
slouken@1895
   418
/* vi: set ts=4 sw=4 expandtab: */