Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
authorSam Lantinga <slouken@libsdl.org>
Sat, 22 Jul 2006 08:33:18 +0000
changeset 19208a162bfdc838
parent 1919 00816063b9c9
child 1921 f3399f779a1d
Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
OpenGL renderer in progress
include/SDL_video.h
src/video/SDL_pixels.c
src/video/SDL_renderer_gl.c
src/video/SDL_renderer_sw.c
src/video/SDL_surface.c
src/video/SDL_video.c
src/video/SDL_yuv_sw.c
src/video/win32/SDL_d3drender.c
src/video/win32/SDL_gdirender.c
     1.1 --- a/include/SDL_video.h	Wed Jul 19 08:46:57 2006 +0000
     1.2 +++ b/include/SDL_video.h	Sat Jul 22 08:33:18 2006 +0000
     1.3 @@ -191,7 +191,7 @@
     1.4      Uint32 blend_modes;         /**< A mask of supported blend modes */
     1.5      Uint32 scale_modes;         /**< A mask of supported scale modes */
     1.6      Uint32 num_texture_formats; /**< The number of available texture formats */
     1.7 -    Uint32 texture_formats[16]; /**< The available texture formats */
     1.8 +    Uint32 texture_formats[20]; /**< The available texture formats */
     1.9      int max_texture_width;      /**< The maximimum texture width */
    1.10      int max_texture_height;     /**< The maximimum texture height */
    1.11  } SDL_RendererInfo;
     2.1 --- a/src/video/SDL_pixels.c	Wed Jul 19 08:46:57 2006 +0000
     2.2 +++ b/src/video/SDL_pixels.c	Sat Jul 22 08:33:18 2006 +0000
     2.3 @@ -346,12 +346,11 @@
     2.4      Uint32 mask;
     2.5  
     2.6      /* Allocate an empty pixel format structure */
     2.7 -    format = SDL_malloc(sizeof(*format));
     2.8 +    format = SDL_calloc(1, sizeof(*format));
     2.9      if (format == NULL) {
    2.10          SDL_OutOfMemory();
    2.11          return (NULL);
    2.12      }
    2.13 -    SDL_memset(format, 0, sizeof(*format));
    2.14      format->alpha = SDL_ALPHA_OPAQUE;
    2.15  
    2.16      /* Set up the format */
    2.17 @@ -714,22 +713,20 @@
    2.18      SDL_BlitMap *map;
    2.19  
    2.20      /* Allocate the empty map */
    2.21 -    map = (SDL_BlitMap *) SDL_malloc(sizeof(*map));
    2.22 +    map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
    2.23      if (map == NULL) {
    2.24          SDL_OutOfMemory();
    2.25          return (NULL);
    2.26      }
    2.27 -    SDL_memset(map, 0, sizeof(*map));
    2.28  
    2.29      /* Allocate the software blit data */
    2.30      map->sw_data =
    2.31 -        (struct private_swaccel *) SDL_malloc(sizeof(*map->sw_data));
    2.32 +        (struct private_swaccel *) SDL_calloc(1, sizeof(*map->sw_data));
    2.33      if (map->sw_data == NULL) {
    2.34          SDL_FreeBlitMap(map);
    2.35          SDL_OutOfMemory();
    2.36          return (NULL);
    2.37      }
    2.38 -    SDL_memset(map->sw_data, 0, sizeof(*map->sw_data));
    2.39  
    2.40      /* It's ready to go */
    2.41      return (map);
     3.1 --- a/src/video/SDL_renderer_gl.c	Wed Jul 19 08:46:57 2006 +0000
     3.2 +++ b/src/video/SDL_renderer_gl.c	Sat Jul 22 08:33:18 2006 +0000
     3.3 @@ -22,8 +22,13 @@
     3.4  #include "SDL_config.h"
     3.5  
     3.6  #if SDL_VIDEO_OPENGL
     3.7 -#if 0
     3.8 -#include "SDL_win32video.h"
     3.9 +
    3.10 +#include "SDL_video.h"
    3.11 +#include "SDL_opengl.h"
    3.12 +#include "SDL_sysvideo.h"
    3.13 +#include "SDL_pixels_c.h"
    3.14 +#include "SDL_rect_c.h"
    3.15 +#include "SDL_yuv_sw_c.h"
    3.16  
    3.17  /* OpenGL renderer implementation */
    3.18  
    3.19 @@ -66,8 +71,10 @@
    3.20        SDL_TextureBlendMode_Mod),
    3.21       (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast |
    3.22        SDL_TextureScaleMode_Best),
    3.23 -     12,
    3.24 +     18,
    3.25       {
    3.26 +      SDL_PixelFormat_Index1LSB,
    3.27 +      SDL_PixelFormat_Index1MSB,
    3.28        SDL_PixelFormat_Index8,
    3.29        SDL_PixelFormat_RGB332,
    3.30        SDL_PixelFormat_RGB444,
    3.31 @@ -75,11 +82,15 @@
    3.32        SDL_PixelFormat_ARGB4444,
    3.33        SDL_PixelFormat_ARGB1555,
    3.34        SDL_PixelFormat_RGB565,
    3.35 +      SDL_PixelFormat_RGB24,
    3.36 +      SDL_PixelFormat_BGR24,
    3.37        SDL_PixelFormat_RGB888,
    3.38 +      SDL_PixelFormat_BGR888,
    3.39        SDL_PixelFormat_ARGB8888,
    3.40 -      SDL_PixelFormat_ARGB2101010,
    3.41 -      SDL_PixelFormat_UYVY,
    3.42 -      SDL_PixelFormat_YUY2},
    3.43 +      SDL_PixelFormat_RGBA8888,
    3.44 +      SDL_PixelFormat_ABGR8888,
    3.45 +      SDL_PixelFormat_BGRA8888,
    3.46 +      SDL_PixelFormat_ARGB2101010},     /* FIXME: YUV texture support */
    3.47       0,
    3.48       0}
    3.49  };
    3.50 @@ -87,57 +98,26 @@
    3.51  typedef struct
    3.52  {
    3.53      SDL_GLContext context;
    3.54 -    SDL_bool beginScene;
    3.55  } GL_RenderData;
    3.56  
    3.57  typedef struct
    3.58  {
    3.59      GLuint texture;
    3.60 +    GLenum type;
    3.61      GLfloat texw;
    3.62      GLfloat texh;
    3.63 +    GLenum format;
    3.64 +    GLenum formattype;
    3.65      void *pixels;
    3.66      int pitch;
    3.67 +    SDL_DirtyRectList dirty;
    3.68  } GL_TextureData;
    3.69  
    3.70 -static GLFORMAT
    3.71 -PixelFormatToOpenGL(Uint32 format,)
    3.72 -{
    3.73 -    switch (format) {
    3.74 -    case SDL_PixelFormat_Index8:
    3.75 -        return GLFMT_P8;
    3.76 -    case SDL_PixelFormat_RGB332:
    3.77 -        return GLFMT_R3G3B2;
    3.78 -    case SDL_PixelFormat_RGB444:
    3.79 -        return GLFMT_X4R4G4B4;
    3.80 -    case SDL_PixelFormat_RGB555:
    3.81 -        return GLFMT_X1R5G5B5;
    3.82 -    case SDL_PixelFormat_ARGB4444:
    3.83 -        return GLFMT_A4R4G4B4;
    3.84 -    case SDL_PixelFormat_ARGB1555:
    3.85 -        return GLFMT_A1R5G5B5;
    3.86 -    case SDL_PixelFormat_RGB565:
    3.87 -        return GLFMT_R5G6B5;
    3.88 -    case SDL_PixelFormat_RGB888:
    3.89 -        return GLFMT_X8R8G8B8;
    3.90 -    case SDL_PixelFormat_ARGB8888:
    3.91 -        return GLFMT_A8R8G8B8;
    3.92 -    case SDL_PixelFormat_ARGB2101010:
    3.93 -        return GLFMT_A2R10G10B10;
    3.94 -    case SDL_PixelFormat_UYVY:
    3.95 -        return GLFMT_UYVY;
    3.96 -    case SDL_PixelFormat_YUY2:
    3.97 -        return GLFMT_YUY2;
    3.98 -    default:
    3.99 -        return GLFMT_UNKNOWN;
   3.100 -    }
   3.101 -}
   3.102  
   3.103  void
   3.104  GL_AddRenderDriver(_THIS)
   3.105  {
   3.106 -    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   3.107 -
   3.108 -    if (data->d3d) {
   3.109 +    if (_this->GL_CreateContext) {
   3.110          SDL_AddRenderDriver(0, &GL_RenderDriver);
   3.111      }
   3.112  }
   3.113 @@ -145,14 +125,8 @@
   3.114  SDL_Renderer *
   3.115  GL_CreateRenderer(SDL_Window * window, Uint32 flags)
   3.116  {
   3.117 -    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   3.118 -    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
   3.119 -    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
   3.120      SDL_Renderer *renderer;
   3.121      GL_RenderData *data;
   3.122 -    HRESULT result;
   3.123 -    GLPRESENT_PARAMETERS pparams;
   3.124 -    IDirect3DSwapChain9 *chain;
   3.125  
   3.126      if (!(window->flags & SDL_WINDOW_OPENGL)) {
   3.127          SDL_SetError
   3.128 @@ -160,20 +134,18 @@
   3.129          return NULL;
   3.130      }
   3.131  
   3.132 -    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
   3.133 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   3.134      if (!renderer) {
   3.135          SDL_OutOfMemory();
   3.136          return NULL;
   3.137      }
   3.138 -    SDL_zerop(renderer);
   3.139  
   3.140 -    data = (GL_RenderData *) SDL_malloc(sizeof(*data));
   3.141 +    data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
   3.142      if (!data) {
   3.143          GL_DestroyRenderer(renderer);
   3.144          SDL_OutOfMemory();
   3.145          return NULL;
   3.146      }
   3.147 -    SDL_zerop(data);
   3.148  
   3.149      renderer->CreateTexture = GL_CreateTexture;
   3.150      renderer->SetTexturePalette = GL_SetTexturePalette;
   3.151 @@ -203,7 +175,6 @@
   3.152          GL_DestroyRenderer(renderer);
   3.153          return NULL;
   3.154      }
   3.155 -    data->beginScene = SDL_TRUE;
   3.156  
   3.157      if (flags & SDL_Renderer_PresentVSync) {
   3.158          SDL_GL_SetSwapInterval(1);
   3.159 @@ -214,67 +185,147 @@
   3.160          renderer->info.flags |= SDL_Renderer_PresentVSync;
   3.161      }
   3.162  
   3.163 +    /* FIXME: Add a function to make the rendering context current when selecting the renderer */
   3.164 +
   3.165 +    /* FIXME: Query maximum texture size */
   3.166 +
   3.167 +    /* FIXME: Check for GL_ARB_texture_rectangle and GL_EXT_texture_rectangle */
   3.168 +
   3.169      /* Set up parameters for rendering */
   3.170      glDisable(GL_DEPTH_TEST);
   3.171      glDisable(GL_CULL_FACE);
   3.172      glEnable(GL_TEXTURE_2D);
   3.173 +    glEnable(GL_TEXTURE_RECTANGLE_ARB);
   3.174 +    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   3.175      glMatrixMode(GL_PROJECTION);
   3.176      glLoadIdentity();
   3.177      glMatrixMode(GL_MODELVIEW);
   3.178      glLoadIdentity();
   3.179      glViewport(0, 0, window->w, window->h);
   3.180      glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0, 0.0, 1.0);
   3.181 -    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   3.182  
   3.183      return renderer;
   3.184  }
   3.185  
   3.186 -/* Quick utility function for texture creation */
   3.187 -static int
   3.188 -power_of_two(int input)
   3.189 -{
   3.190 -    int value = 1;
   3.191 -
   3.192 -    while (value < input) {
   3.193 -        value <<= 1;
   3.194 -    }
   3.195 -    return value;
   3.196 -}
   3.197 -
   3.198  static int
   3.199  GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   3.200  {
   3.201      GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   3.202      SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   3.203 -    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   3.204      GL_TextureData *data;
   3.205 -    GLPOOL pool;
   3.206 -    HRESULT result;
   3.207 +    GLint internalFormat;
   3.208 +    GLenum format, type;
   3.209  
   3.210 -    data = (GL_TextureData *) SDL_malloc(sizeof(*data));
   3.211 +    switch (texture->format) {
   3.212 +    case SDL_PixelFormat_Index1LSB:
   3.213 +    case SDL_PixelFormat_Index1MSB:
   3.214 +        internalFormat = GL_RGB;
   3.215 +        format = GL_COLOR_INDEX;
   3.216 +        type = GL_BITMAP;
   3.217 +        break;
   3.218 +    case SDL_PixelFormat_Index8:
   3.219 +        internalFormat = GL_RGB;
   3.220 +        format = GL_COLOR_INDEX;
   3.221 +        type = GL_UNSIGNED_BYTE;
   3.222 +        break;
   3.223 +    case SDL_PixelFormat_RGB332:
   3.224 +        internalFormat = GL_R3_G3_B2;
   3.225 +        format = GL_RGB;
   3.226 +        type = GL_UNSIGNED_BYTE_3_3_2;
   3.227 +        break;
   3.228 +    case SDL_PixelFormat_RGB444:
   3.229 +        internalFormat = GL_RGB4;
   3.230 +        format = GL_RGB;
   3.231 +        type = GL_UNSIGNED_SHORT_4_4_4_4;
   3.232 +        break;
   3.233 +    case SDL_PixelFormat_RGB555:
   3.234 +        internalFormat = GL_RGB5;
   3.235 +        format = GL_RGB;
   3.236 +        type = GL_UNSIGNED_SHORT_5_5_5_1;
   3.237 +        break;
   3.238 +    case SDL_PixelFormat_ARGB4444:
   3.239 +        internalFormat = GL_RGBA4;
   3.240 +        format = GL_BGRA;
   3.241 +        type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
   3.242 +        break;
   3.243 +    case SDL_PixelFormat_ARGB1555:
   3.244 +        internalFormat = GL_RGB5_A1;
   3.245 +        format = GL_BGRA;
   3.246 +        type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
   3.247 +        break;
   3.248 +    case SDL_PixelFormat_RGB565:
   3.249 +        internalFormat = GL_RGB8;
   3.250 +        format = GL_RGB;
   3.251 +        type = GL_UNSIGNED_SHORT_5_6_5;
   3.252 +        break;
   3.253 +    case SDL_PixelFormat_RGB24:
   3.254 +        internalFormat = GL_RGB8;
   3.255 +        format = GL_RGB;
   3.256 +        type = GL_UNSIGNED_BYTE;
   3.257 +        break;
   3.258 +    case SDL_PixelFormat_RGB888:
   3.259 +        internalFormat = GL_RGB8;
   3.260 +        format = GL_RGB;
   3.261 +        type = GL_UNSIGNED_INT_8_8_8_8;
   3.262 +        break;
   3.263 +    case SDL_PixelFormat_BGR24:
   3.264 +        internalFormat = GL_RGB8;
   3.265 +        format = GL_BGR;
   3.266 +        type = GL_UNSIGNED_BYTE;
   3.267 +        break;
   3.268 +    case SDL_PixelFormat_BGR888:
   3.269 +        internalFormat = GL_RGB8;
   3.270 +        format = GL_BGR;
   3.271 +        type = GL_UNSIGNED_INT_8_8_8_8;
   3.272 +        break;
   3.273 +    case SDL_PixelFormat_ARGB8888:
   3.274 +        internalFormat = GL_RGBA8;
   3.275 +        format = GL_BGRA;
   3.276 +        type = GL_UNSIGNED_INT_8_8_8_8_REV;
   3.277 +        break;
   3.278 +    case SDL_PixelFormat_RGBA8888:
   3.279 +        internalFormat = GL_RGBA8;
   3.280 +        format = GL_BGRA;
   3.281 +        type = GL_UNSIGNED_INT_8_8_8_8;
   3.282 +        break;
   3.283 +    case SDL_PixelFormat_ABGR8888:
   3.284 +        internalFormat = GL_RGBA8;
   3.285 +        format = GL_RGBA;
   3.286 +        type = GL_UNSIGNED_INT_8_8_8_8_REV;
   3.287 +        break;
   3.288 +    case SDL_PixelFormat_BGRA8888:
   3.289 +        internalFormat = GL_RGBA8;
   3.290 +        format = GL_BGRA;
   3.291 +        type = GL_UNSIGNED_INT_8_8_8_8;
   3.292 +        break;
   3.293 +    case SDL_PixelFormat_ARGB2101010:
   3.294 +        internalFormat = GL_RGB10_A2;
   3.295 +        format = GL_BGRA;
   3.296 +        type = GL_UNSIGNED_INT_2_10_10_10_REV;
   3.297 +        break;
   3.298 +    default:
   3.299 +        SDL_SetError("Unsupported texture format");
   3.300 +        return -1;
   3.301 +    }
   3.302 +
   3.303 +    data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
   3.304      if (!data) {
   3.305          SDL_OutOfMemory();
   3.306          return -1;
   3.307      }
   3.308 -    SDL_zerop(data);
   3.309  
   3.310      texture->driverdata = data;
   3.311  
   3.312 -    if (texture->access == SDL_TextureAccess_Local) {
   3.313 -        pool = GLPOOL_MANAGED;
   3.314 -    } else {
   3.315 -        pool = GLPOOL_DEFAULT;
   3.316 -    }
   3.317 -    result =
   3.318 -        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   3.319 -                                       texture->h, 1, 0,
   3.320 -                                       PixelFormatToGLFMT(texture->format),
   3.321 -                                       pool, &data->texture, NULL);
   3.322 -    if (FAILED(result)) {
   3.323 -        SDL_free(data);
   3.324 -        GL_SetError("CreateTexture()", result);
   3.325 -        return -1;
   3.326 -    }
   3.327 +    /* FIXME: Check for GL_ARB_texture_rectangle and GL_EXT_texture_rectangle */
   3.328 +    glGenTextures(1, &data->texture);
   3.329 +    data->type = GL_TEXTURE_RECTANGLE_ARB;
   3.330 +    data->texw = (GLfloat) texture->w;
   3.331 +    data->texh = (GLfloat) texture->h;
   3.332 +    data->format = format;
   3.333 +    data->formattype = type;
   3.334 +    glBindTexture(data->type, data->texture);
   3.335 +    glTexImage2D(data->type, 0, internalFormat, texture->w, texture->h, 0,
   3.336 +                 format, type, NULL);
   3.337  
   3.338      return 0;
   3.339  }
   3.340 @@ -303,57 +354,14 @@
   3.341                   const SDL_Rect * rect, const void *pixels, int pitch)
   3.342  {
   3.343      GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   3.344 -    GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
   3.345 -    IDirect3DTexture9 *temp;
   3.346 -    RECT d3drect;
   3.347 -    GLLOCKED_RECT locked;
   3.348 -    const Uint8 *src;
   3.349 -    Uint8 *dst;
   3.350 -    int row, length;
   3.351 -    HRESULT result;
   3.352  
   3.353 -    result =
   3.354 -        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   3.355 -                                       texture->h, 1, 0,
   3.356 -                                       PixelFormatToGLFMT(texture->format),
   3.357 -                                       GLPOOL_SYSTEMMEM, &temp, NULL);
   3.358 -    if (FAILED(result)) {
   3.359 -        GL_SetError("CreateTexture()", result);
   3.360 -        return -1;
   3.361 -    }
   3.362 -
   3.363 -    d3drect.left = rect->x;
   3.364 -    d3drect.right = rect->x + rect->w;
   3.365 -    d3drect.top = rect->y;
   3.366 -    d3drect.bottom = rect->y + rect->h;
   3.367 -
   3.368 -    result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
   3.369 -    if (FAILED(result)) {
   3.370 -        IDirect3DTexture9_Release(temp);
   3.371 -        GL_SetError("LockRect()", result);
   3.372 -        return -1;
   3.373 -    }
   3.374 -
   3.375 -    src = pixels;
   3.376 -    dst = locked.pBits;
   3.377 -    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   3.378 -    for (row = 0; row < rect->h; ++row) {
   3.379 -        SDL_memcpy(dst, src, length);
   3.380 -        src += pitch;
   3.381 -        dst += locked.Pitch;
   3.382 -    }
   3.383 -    IDirect3DTexture9_UnlockRect(temp, 0);
   3.384 -
   3.385 -    result =
   3.386 -        IDirect3DDevice9_UpdateTexture(renderdata->device,
   3.387 -                                       (IDirect3DBaseTexture9 *) temp,
   3.388 -                                       (IDirect3DBaseTexture9 *) data->
   3.389 -                                       texture);
   3.390 -    IDirect3DTexture9_Release(temp);
   3.391 -    if (FAILED(result)) {
   3.392 -        GL_SetError("UpdateTexture()", result);
   3.393 -        return -1;
   3.394 -    }
   3.395 +    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);      /* FIXME, what to use for RGB 4 byte formats? */
   3.396 +    glPixelStorei(GL_UNPACK_ROW_LENGTH,
   3.397 +                  pitch / SDL_BYTESPERPIXEL(texture->format));
   3.398 +    glBindTexture(data->type, data->texture);
   3.399 +    glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, rect->h,
   3.400 +                    data->format, data->formattype, pixels);
   3.401 +    /* FIXME: check for errors */
   3.402      return 0;
   3.403  }
   3.404  
   3.405 @@ -363,38 +371,30 @@
   3.406                 int *pitch)
   3.407  {
   3.408      GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   3.409 -    RECT d3drect;
   3.410 -    GLLOCKED_RECT locked;
   3.411 -    HRESULT result;
   3.412  
   3.413 -    if (texture->access != SDL_TextureAccess_Local) {
   3.414 -        SDL_SetError("Can't lock remote video memory");
   3.415 -        return -1;
   3.416 +    if (!data->pixels) {
   3.417 +        data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
   3.418 +        data->pixels = SDL_malloc(texture->h * data->pitch);
   3.419 +        if (!data->pixels) {
   3.420 +            SDL_OutOfMemory();
   3.421 +            return -1;
   3.422 +        }
   3.423      }
   3.424  
   3.425 -    d3drect.left = rect->x;
   3.426 -    d3drect.right = rect->x + rect->w;
   3.427 -    d3drect.top = rect->y;
   3.428 -    d3drect.bottom = rect->y + rect->h;
   3.429 +    if (markDirty) {
   3.430 +        SDL_AddDirtyRect(&data->dirty, rect);
   3.431 +    }
   3.432  
   3.433 -    result =
   3.434 -        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
   3.435 -                                   markDirty ? 0 : GLLOCK_NO_DIRTY_UPDATE);
   3.436 -    if (FAILED(result)) {
   3.437 -        GL_SetError("LockRect()", result);
   3.438 -        return -1;
   3.439 -    }
   3.440 -    *pixels = locked.pBits;
   3.441 -    *pitch = locked.Pitch;
   3.442 +    *pixels =
   3.443 +        (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
   3.444 +                  rect->x * SDL_BYTESPERPIXEL(texture->format));
   3.445 +    *pitch = data->pitch;
   3.446      return 0;
   3.447  }
   3.448  
   3.449  static void
   3.450  GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   3.451  {
   3.452 -    GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   3.453 -
   3.454 -    IDirect3DTexture9_UnlockRect(data->texture, 0);
   3.455  }
   3.456  
   3.457  static void
   3.458 @@ -402,18 +402,10 @@
   3.459                  const SDL_Rect * rects)
   3.460  {
   3.461      GL_TextureData *data = (GL_TextureData *) texture->driverdata;
   3.462 -    RECT d3drect;
   3.463      int i;
   3.464  
   3.465      for (i = 0; i < numrects; ++i) {
   3.466 -        const SDL_Rect *rect = &rects[i];
   3.467 -
   3.468 -        d3drect.left = rect->x;
   3.469 -        d3drect.right = rect->x + rect->w;
   3.470 -        d3drect.top = rect->y;
   3.471 -        d3drect.bottom = rect->y + rect->h;
   3.472 -
   3.473 -        IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
   3.474 +        SDL_AddDirtyRect(&data->dirty, &rects[i]);
   3.475      }
   3.476  }
   3.477  
   3.478 @@ -446,17 +438,43 @@
   3.479      int minx, miny, maxx, maxy;
   3.480      GLfloat minu, maxu, minv, maxv;
   3.481  
   3.482 +    if (texturedata->dirty.count > 0) {
   3.483 +        SDL_DirtyRect *dirty;
   3.484 +        void *pixels;
   3.485 +        int bpp = SDL_BYTESPERPIXEL(texture->format);
   3.486 +        int pitch = texturedata->pitch;
   3.487 +
   3.488 +        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);  /* FIXME, what to use for RGB 4 byte formats? */
   3.489 +        glPixelStorei(GL_UNPACK_ROW_LENGTH,
   3.490 +                      pitch / SDL_BYTESPERPIXEL(texture->format));
   3.491 +        glBindTexture(texturedata->type, texturedata->texture);
   3.492 +        for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
   3.493 +            SDL_Rect *rect = &dirty->rect;
   3.494 +            pixels =
   3.495 +                (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
   3.496 +                          rect->x * bpp);
   3.497 +            glTexSubImage2D(texturedata->type, 0, rect->x, rect->y, rect->w,
   3.498 +                            rect->h, texturedata->format,
   3.499 +                            texturedata->formattype, pixels);
   3.500 +        }
   3.501 +        SDL_ClearDirtyRects(&texturedata->dirty);
   3.502 +    }
   3.503 +
   3.504      minx = dstrect->x;
   3.505      miny = dstrect->y;
   3.506      maxx = dstrect->x + dstrect->w;
   3.507      maxy = dstrect->y + dstrect->h;
   3.508  
   3.509      minu = (GLfloat) srcrect->x / texture->w;
   3.510 +    minu *= texturedata->texw;
   3.511      maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
   3.512 +    maxu *= texturedata->texw;
   3.513      minv = (GLfloat) srcrect->y / texture->h;
   3.514 +    minv *= texturedata->texh;
   3.515      maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
   3.516 +    maxv *= texturedata->texh;
   3.517  
   3.518 -    glBindTexture(GL_TEXTURE_2D, texturedata->texture);
   3.519 +    glBindTexture(texturedata->type, texturedata->texture);
   3.520  
   3.521      switch (blendMode) {
   3.522      case SDL_TextureBlendMode_None:
   3.523 @@ -480,13 +498,13 @@
   3.524      switch (scaleMode) {
   3.525      case SDL_TextureScaleMode_None:
   3.526      case SDL_TextureScaleMode_Fast:
   3.527 -        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   3.528 -        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   3.529 +        glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   3.530 +        glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   3.531          break;
   3.532      case SDL_TextureScaleMode_Slow:
   3.533      case SDL_TextureScaleMode_Best:
   3.534 -        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   3.535 -        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   3.536 +        glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   3.537 +        glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   3.538          break;
   3.539      }
   3.540  
   3.541 @@ -519,8 +537,12 @@
   3.542          return;
   3.543      }
   3.544      if (data->texture) {
   3.545 -        IDirect3DTexture9_Release(data->texture);
   3.546 +        glDeleteTextures(1, &data->texture);
   3.547      }
   3.548 +    if (data->pixels) {
   3.549 +        SDL_free(data->pixels);
   3.550 +    }
   3.551 +    SDL_FreeDirtyRects(&data->dirty);
   3.552      SDL_free(data);
   3.553      texture->driverdata = NULL;
   3.554  }
   3.555 @@ -531,15 +553,15 @@
   3.556      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
   3.557  
   3.558      if (data) {
   3.559 -        if (data->device) {
   3.560 -            IDirect3DDevice9_Release(data->device);
   3.561 +        if (data->context) {
   3.562 +            SDL_GL_MakeCurrent(0, NULL);
   3.563 +            SDL_GL_DeleteContext(data->context);
   3.564          }
   3.565          SDL_free(data);
   3.566      }
   3.567      SDL_free(renderer);
   3.568  }
   3.569  
   3.570 -#endif /* 0 */
   3.571  #endif /* SDL_VIDEO_OPENGL */
   3.572  
   3.573  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/video/SDL_renderer_sw.c	Wed Jul 19 08:46:57 2006 +0000
     4.2 +++ b/src/video/SDL_renderer_sw.c	Sat Jul 22 08:33:18 2006 +0000
     4.3 @@ -105,13 +105,12 @@
     4.4  {
     4.5      SDL_Texture *texture;
     4.6  
     4.7 -    texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
     4.8 +    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
     4.9      if (!texture) {
    4.10          SDL_OutOfMemory();
    4.11          return NULL;
    4.12      }
    4.13  
    4.14 -    SDL_zerop(texture);
    4.15      texture->format = format;
    4.16      texture->access = SDL_TextureAccess_Local;
    4.17      texture->w = w;
    4.18 @@ -173,13 +172,12 @@
    4.19          return NULL;
    4.20      }
    4.21  
    4.22 -    data = (SW_RenderData *) SDL_malloc(sizeof(*data));
    4.23 +    data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
    4.24      if (!data) {
    4.25          SW_DestroyRenderer(renderer);
    4.26          SDL_OutOfMemory();
    4.27          return NULL;
    4.28      }
    4.29 -    SDL_zerop(data);
    4.30  
    4.31      renderer->CreateTexture = SW_CreateTexture;
    4.32      renderer->QueryTexturePixels = SW_QueryTexturePixels;
     5.1 --- a/src/video/SDL_surface.c	Wed Jul 19 08:46:57 2006 +0000
     5.2 +++ b/src/video/SDL_surface.c	Sat Jul 22 08:33:18 2006 +0000
     5.3 @@ -42,12 +42,11 @@
     5.4      SDL_Surface *surface;
     5.5  
     5.6      /* Allocate the surface */
     5.7 -    surface = (SDL_Surface *) SDL_malloc(sizeof(*surface));
     5.8 +    surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
     5.9      if (surface == NULL) {
    5.10          SDL_OutOfMemory();
    5.11          return NULL;
    5.12      }
    5.13 -    SDL_zerop(surface);
    5.14  
    5.15      surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
    5.16      if (!surface->format) {
     6.1 --- a/src/video/SDL_video.c	Wed Jul 19 08:46:57 2006 +0000
     6.2 +++ b/src/video/SDL_video.c	Sat Jul 22 08:33:18 2006 +0000
     6.3 @@ -279,10 +279,10 @@
     6.4  
     6.5      /* The software renderer is always available */
     6.6      for (i = 0; i < _this->num_displays; ++i) {
     6.7 +#if SDL_VIDEO_OPENGL
     6.8 +        SDL_AddRenderDriver(i, &GL_RenderDriver);
     6.9 +#endif
    6.10          if (_this->displays[i].num_render_drivers > 0) {
    6.11 -#if 0 //SDL_VIDEO_OPENGL
    6.12 -            SDL_AddRenderDriver(i, &GL_RenderDriver);
    6.13 -#endif
    6.14              SDL_AddRenderDriver(i, &SW_RenderDriver);
    6.15          }
    6.16      }
    6.17 @@ -1403,13 +1403,12 @@
    6.18          return 0;
    6.19      }
    6.20  
    6.21 -    texture = (SDL_Texture *) SDL_malloc(sizeof(*texture));
    6.22 +    texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
    6.23      if (!texture) {
    6.24          SDL_OutOfMemory();
    6.25          return 0;
    6.26      }
    6.27  
    6.28 -    SDL_zerop(texture);
    6.29      texture->id = _this->next_object_id++;
    6.30      texture->format = format;
    6.31      texture->access = access;
     7.1 --- a/src/video/SDL_yuv_sw.c	Wed Jul 19 08:46:57 2006 +0000
     7.2 +++ b/src/video/SDL_yuv_sw.c	Sat Jul 22 08:33:18 2006 +0000
     7.3 @@ -1054,12 +1054,11 @@
     7.4      int i;
     7.5      int CR, CB;
     7.6  
     7.7 -    swdata = (SDL_SW_YUVTexture *) SDL_malloc(sizeof(*swdata));
     7.8 +    swdata = (SDL_SW_YUVTexture *) SDL_calloc(1, sizeof(*swdata));
     7.9      if (!swdata) {
    7.10          SDL_OutOfMemory();
    7.11          return NULL;
    7.12      }
    7.13 -    SDL_zerop(swdata);
    7.14  
    7.15      switch (texture->format) {
    7.16      case SDL_PixelFormat_YV12:
     8.1 --- a/src/video/win32/SDL_d3drender.c	Wed Jul 19 08:46:57 2006 +0000
     8.2 +++ b/src/video/win32/SDL_d3drender.c	Sat Jul 22 08:33:18 2006 +0000
     8.3 @@ -239,20 +239,18 @@
     8.4      D3DPRESENT_PARAMETERS pparams;
     8.5      IDirect3DSwapChain9 *chain;
     8.6  
     8.7 -    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
     8.8 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
     8.9      if (!renderer) {
    8.10          SDL_OutOfMemory();
    8.11          return NULL;
    8.12      }
    8.13 -    SDL_zerop(renderer);
    8.14  
    8.15 -    data = (D3D_RenderData *) SDL_malloc(sizeof(*data));
    8.16 +    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
    8.17      if (!data) {
    8.18          D3D_DestroyRenderer(renderer);
    8.19          SDL_OutOfMemory();
    8.20          return NULL;
    8.21      }
    8.22 -    SDL_zerop(data);
    8.23  
    8.24      renderer->CreateTexture = D3D_CreateTexture;
    8.25      renderer->SetTexturePalette = D3D_SetTexturePalette;
    8.26 @@ -379,12 +377,11 @@
    8.27      D3DPOOL pool;
    8.28      HRESULT result;
    8.29  
    8.30 -    data = (D3D_TextureData *) SDL_malloc(sizeof(*data));
    8.31 +    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
    8.32      if (!data) {
    8.33          SDL_OutOfMemory();
    8.34          return -1;
    8.35      }
    8.36 -    SDL_zerop(data);
    8.37  
    8.38      texture->driverdata = data;
    8.39  
     9.1 --- a/src/video/win32/SDL_gdirender.c	Wed Jul 19 08:46:57 2006 +0000
     9.2 +++ b/src/video/win32/SDL_gdirender.c	Sat Jul 22 08:33:18 2006 +0000
     9.3 @@ -142,20 +142,18 @@
     9.4      HBITMAP hbm;
     9.5      int i, n;
     9.6  
     9.7 -    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
     9.8 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
     9.9      if (!renderer) {
    9.10          SDL_OutOfMemory();
    9.11          return NULL;
    9.12      }
    9.13 -    SDL_zerop(renderer);
    9.14  
    9.15 -    data = (GDI_RenderData *) SDL_malloc(sizeof(*data));
    9.16 +    data = (GDI_RenderData *) SDL_calloc(1, sizeof(*data));
    9.17      if (!data) {
    9.18          GDI_DestroyRenderer(renderer);
    9.19          SDL_OutOfMemory();
    9.20          return NULL;
    9.21      }
    9.22 -    SDL_zerop(data);
    9.23  
    9.24      renderer->CreateTexture = GDI_CreateTexture;
    9.25      renderer->QueryTexturePixels = GDI_QueryTexturePixels;
    9.26 @@ -183,13 +181,12 @@
    9.27  
    9.28      /* Fill in the compatible bitmap info */
    9.29      bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
    9.30 -    data->bmi = (LPBITMAPINFO) SDL_malloc(bmi_size);
    9.31 +    data->bmi = (LPBITMAPINFO) SDL_calloc(1, bmi_size);
    9.32      if (!data->bmi) {
    9.33          GDI_DestroyRenderer(renderer);
    9.34          SDL_OutOfMemory();
    9.35          return NULL;
    9.36      }
    9.37 -    SDL_memset(data->bmi, 0, bmi_size);
    9.38      data->bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    9.39  
    9.40      hbm = CreateCompatibleBitmap(data->window_hdc, 1, 1);
    9.41 @@ -241,12 +238,11 @@
    9.42      SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
    9.43      GDI_TextureData *data;
    9.44  
    9.45 -    data = (GDI_TextureData *) SDL_malloc(sizeof(*data));
    9.46 +    data = (GDI_TextureData *) SDL_calloc(1, sizeof(*data));
    9.47      if (!data) {
    9.48          SDL_OutOfMemory();
    9.49          return -1;
    9.50      }
    9.51 -    SDL_zerop(data);
    9.52  
    9.53      texture->driverdata = data;
    9.54  
    9.55 @@ -268,13 +264,12 @@
    9.56          LPBITMAPINFO bmi;
    9.57  
    9.58          bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
    9.59 -        bmi = (LPBITMAPINFO) SDL_malloc(bmi_size);
    9.60 +        bmi = (LPBITMAPINFO) SDL_calloc(1, bmi_size);
    9.61          if (!bmi) {
    9.62              GDI_DestroyTexture(renderer, texture);
    9.63              SDL_OutOfMemory();
    9.64              return -1;
    9.65          }
    9.66 -        SDL_memset(bmi, 0, bmi_size);
    9.67          bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    9.68          bmi->bmiHeader.biWidth = texture->w;
    9.69          bmi->bmiHeader.biHeight = -texture->h;  /* topdown bitmap */