Implemented fully featured dummy renderer
authorSam Lantinga <slouken@libsdl.org>
Sat, 15 Jul 2006 19:29:42 +0000
changeset 1908e079dafea2bf
parent 1907 06c27a737b7a
child 1909 8f1ab2f7c722
Implemented fully featured dummy renderer
src/video/dummy/SDL_nullrender.c
     1.1 --- a/src/video/dummy/SDL_nullrender.c	Sat Jul 15 09:46:36 2006 +0000
     1.2 +++ b/src/video/dummy/SDL_nullrender.c	Sat Jul 15 19:29:42 2006 +0000
     1.3 @@ -23,15 +23,52 @@
     1.4  
     1.5  #include "SDL_video.h"
     1.6  #include "../SDL_sysvideo.h"
     1.7 +#include "../SDL_yuv_sw_c.h"
     1.8  
     1.9  
    1.10  /* SDL surface based renderer implementation */
    1.11  
    1.12 -static SDL_Renderer *SDL_DUMMY_CreateRenderer(SDL_Window * window,
    1.13 -                                              Uint32 flags);
    1.14 +static SDL_Renderer *SDL_DUMMY_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.15  static int SDL_DUMMY_CreateTexture(SDL_Renderer * renderer,
    1.16 -                                   SDL_Texture * texture);
    1.17 +                                SDL_Texture * texture);
    1.18 +static int SDL_DUMMY_QueryTexturePixels(SDL_Renderer * renderer,
    1.19 +                                     SDL_Texture * texture, void **pixels,
    1.20 +                                     int *pitch);
    1.21 +static int SDL_DUMMY_SetTexturePalette(SDL_Renderer * renderer,
    1.22 +                                    SDL_Texture * texture,
    1.23 +                                    const SDL_Color * colors, int firstcolor,
    1.24 +                                    int ncolors);
    1.25 +static int SDL_DUMMY_GetTexturePalette(SDL_Renderer * renderer,
    1.26 +                                    SDL_Texture * texture, SDL_Color * colors,
    1.27 +                                    int firstcolor, int ncolors);
    1.28 +static int SDL_DUMMY_UpdateTexture(SDL_Renderer * renderer,
    1.29 +                                SDL_Texture * texture, const SDL_Rect * rect,
    1.30 +                                const void *pixels, int pitch);
    1.31 +static int SDL_DUMMY_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.32 +                              const SDL_Rect * rect, int markDirty,
    1.33 +                              void **pixels, int *pitch);
    1.34 +static void SDL_DUMMY_UnlockTexture(SDL_Renderer * renderer,
    1.35 +                                 SDL_Texture * texture);
    1.36 +static void SDL_DUMMY_DirtyTexture(SDL_Renderer * renderer,
    1.37 +                                SDL_Texture * texture, int numrects,
    1.38 +                                const SDL_Rect * rects);
    1.39 +static void SDL_DUMMY_SelectRenderTexture(SDL_Renderer * renderer,
    1.40 +                                       SDL_Texture * texture);
    1.41 +static int SDL_DUMMY_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
    1.42 +                             Uint32 color);
    1.43 +static int SDL_DUMMY_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    1.44 +                             const SDL_Rect * srcrect,
    1.45 +                             const SDL_Rect * dstrect, int blendMode,
    1.46 +                             int scaleMode);
    1.47 +static int SDL_DUMMY_RenderReadPixels(SDL_Renderer * renderer,
    1.48 +                                   const SDL_Rect * rect, void *pixels,
    1.49 +                                   int pitch);
    1.50 +static int SDL_DUMMY_RenderWritePixels(SDL_Renderer * renderer,
    1.51 +                                    const SDL_Rect * rect, const void *pixels,
    1.52 +                                    int pitch);
    1.53  static void SDL_DUMMY_RenderPresent(SDL_Renderer * renderer);
    1.54 +static void SDL_DUMMY_DestroyTexture(SDL_Renderer * renderer,
    1.55 +                                  SDL_Texture * texture);
    1.56  static void SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer);
    1.57  
    1.58  
    1.59 @@ -39,18 +76,33 @@
    1.60      SDL_DUMMY_CreateRenderer,
    1.61      {
    1.62       "dummy",
    1.63 -     (SDL_Renderer_PresentDiscard | SDL_Renderer_PresentCopy),
    1.64 -     SDL_TextureBlendMode_None,
    1.65 -     SDL_TextureScaleMode_None,
    1.66 -     0,
    1.67 -     {0},
    1.68 +     (SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
    1.69 +      SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    1.70 +      SDL_Renderer_PresentDiscard),
    1.71 +     (SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask |
    1.72 +      SDL_TextureBlendMode_Blend),
    1.73 +     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
    1.74 +     11,
    1.75 +     {
    1.76 +      SDL_PixelFormat_Index8,
    1.77 +      SDL_PixelFormat_RGB555,
    1.78 +      SDL_PixelFormat_RGB565,
    1.79 +      SDL_PixelFormat_RGB888,
    1.80 +      SDL_PixelFormat_BGR888,
    1.81 +      SDL_PixelFormat_ARGB8888,
    1.82 +      SDL_PixelFormat_RGBA8888,
    1.83 +      SDL_PixelFormat_ABGR8888,
    1.84 +      SDL_PixelFormat_BGRA8888,
    1.85 +      SDL_PixelFormat_YUY2,
    1.86 +      SDL_PixelFormat_UYVY},
    1.87       0,
    1.88       0}
    1.89  };
    1.90  
    1.91  typedef struct
    1.92  {
    1.93 -    SDL_Surface *surface;
    1.94 +    int current_screen;
    1.95 +    SDL_Surface *screens[3];
    1.96  } SDL_DUMMY_RenderData;
    1.97  
    1.98  SDL_Renderer *
    1.99 @@ -60,6 +112,7 @@
   1.100      SDL_DisplayMode *displayMode = &display->current_mode;
   1.101      SDL_Renderer *renderer;
   1.102      SDL_DUMMY_RenderData *data;
   1.103 +    int i, n;
   1.104      int bpp;
   1.105      Uint32 Rmask, Gmask, Bmask, Amask;
   1.106  
   1.107 @@ -69,12 +122,11 @@
   1.108          return NULL;
   1.109      }
   1.110  
   1.111 -    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
   1.112 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   1.113      if (!renderer) {
   1.114          SDL_OutOfMemory();
   1.115          return NULL;
   1.116      }
   1.117 -    SDL_zerop(renderer);
   1.118  
   1.119      data = (SDL_DUMMY_RenderData *) SDL_malloc(sizeof(*data));
   1.120      if (!data) {
   1.121 @@ -84,50 +136,284 @@
   1.122      }
   1.123      SDL_zerop(data);
   1.124  
   1.125 +    renderer->CreateTexture = SDL_DUMMY_CreateTexture;
   1.126 +    renderer->QueryTexturePixels = SDL_DUMMY_QueryTexturePixels;
   1.127 +    renderer->SetTexturePalette = SDL_DUMMY_SetTexturePalette;
   1.128 +    renderer->GetTexturePalette = SDL_DUMMY_GetTexturePalette;
   1.129 +    renderer->UpdateTexture = SDL_DUMMY_UpdateTexture;
   1.130 +    renderer->LockTexture = SDL_DUMMY_LockTexture;
   1.131 +    renderer->UnlockTexture = SDL_DUMMY_UnlockTexture;
   1.132 +    renderer->DirtyTexture = SDL_DUMMY_DirtyTexture;
   1.133 +    renderer->RenderFill = SDL_DUMMY_RenderFill;
   1.134 +    renderer->RenderCopy = SDL_DUMMY_RenderCopy;
   1.135      renderer->RenderPresent = SDL_DUMMY_RenderPresent;
   1.136 +    renderer->DestroyTexture = SDL_DUMMY_DestroyTexture;
   1.137      renderer->DestroyRenderer = SDL_DUMMY_DestroyRenderer;
   1.138      renderer->info = SDL_DUMMY_RenderDriver.info;
   1.139      renderer->window = window->id;
   1.140      renderer->driverdata = data;
   1.141  
   1.142 -    data->surface =
   1.143 -        SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
   1.144 -                             Bmask, Amask);
   1.145 -    if (!data->surface) {
   1.146 -        SDL_DUMMY_DestroyRenderer(renderer);
   1.147 -        return NULL;
   1.148 +    renderer->info.flags = 0;
   1.149 +
   1.150 +    if (flags & SDL_Renderer_PresentFlip2) {
   1.151 +        renderer->info.flags |= SDL_Renderer_PresentFlip2;
   1.152 +        n = 2;
   1.153 +    } else if (flags & SDL_Renderer_PresentFlip3) {
   1.154 +        renderer->info.flags |= SDL_Renderer_PresentFlip3;
   1.155 +        n = 3;
   1.156 +    } else {
   1.157 +        renderer->info.flags |= SDL_Renderer_PresentCopy;
   1.158 +        n = 1;
   1.159      }
   1.160 -    SDL_SetSurfacePalette(data->surface, display->palette);
   1.161 +    for (i = 0; i < n; ++i) {
   1.162 +        data->screens[i] =
   1.163 +            SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
   1.164 +                                 Bmask, Amask);
   1.165 +        if (!data->screens[i]) {
   1.166 +            SDL_DUMMY_DestroyRenderer(renderer);
   1.167 +            return NULL;
   1.168 +        }
   1.169 +        SDL_SetSurfacePalette(data->screens[i], display->palette);
   1.170 +    }
   1.171 +    data->current_screen = 0;
   1.172  
   1.173      return renderer;
   1.174  }
   1.175  
   1.176 -void
   1.177 +static int
   1.178 +SDL_DUMMY_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.179 +{
   1.180 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.181 +        texture->driverdata = SDL_SW_CreateYUVTexture(texture);
   1.182 +    } else {
   1.183 +        int bpp;
   1.184 +        Uint32 Rmask, Gmask, Bmask, Amask;
   1.185 +
   1.186 +        if (!SDL_PixelFormatEnumToMasks
   1.187 +            (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   1.188 +            SDL_SetError("Unknown texture format");
   1.189 +            return -1;
   1.190 +        }
   1.191 +
   1.192 +        texture->driverdata = SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask, Bmask, Amask);
   1.193 +    }
   1.194 +
   1.195 +    if (!texture->driverdata) {
   1.196 +        return -1;
   1.197 +    }
   1.198 +    return 0;
   1.199 +}
   1.200 +
   1.201 +static int
   1.202 +SDL_DUMMY_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   1.203 +                          void **pixels, int *pitch)
   1.204 +{
   1.205 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.206 +        return SDL_SW_QueryYUVTexturePixels((SDL_SW_YUVTexture *) texture->driverdata, pixels, pitch);
   1.207 +    } else {
   1.208 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.209 +
   1.210 +        *pixels = surface->pixels;
   1.211 +        *pitch = surface->pitch;
   1.212 +        return 0;
   1.213 +    }
   1.214 +}
   1.215 +
   1.216 +static int
   1.217 +SDL_DUMMY_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.218 +                         const SDL_Color * colors, int firstcolor,
   1.219 +                         int ncolors)
   1.220 +{
   1.221 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.222 +        SDL_SetError("YUV textures don't have a palette");
   1.223 +        return -1;
   1.224 +    } else {
   1.225 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.226 +
   1.227 +        return SDL_SetPaletteColors(surface->format->palette, colors,
   1.228 +                                    firstcolor, ncolors);
   1.229 +    }
   1.230 +}
   1.231 +
   1.232 +static int
   1.233 +SDL_DUMMY_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.234 +                         SDL_Color * colors, int firstcolor, int ncolors)
   1.235 +{
   1.236 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.237 +        SDL_SetError("YUV textures don't have a palette");
   1.238 +        return -1;
   1.239 +    } else {
   1.240 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.241 +
   1.242 +        SDL_memcpy(colors, &surface->format->palette->colors[firstcolor],
   1.243 +                   ncolors * sizeof(*colors));
   1.244 +        return 0;
   1.245 +    }
   1.246 +}
   1.247 +
   1.248 +static int
   1.249 +SDL_DUMMY_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.250 +                     const SDL_Rect * rect, const void *pixels, int pitch)
   1.251 +{
   1.252 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.253 +        return SDL_SW_UpdateYUVTexture((SDL_SW_YUVTexture *) texture->
   1.254 +                                       driverdata, rect, pixels, pitch);
   1.255 +    } else {
   1.256 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.257 +        Uint8 *src, *dst;
   1.258 +        int row;
   1.259 +        size_t length;
   1.260 +
   1.261 +        src = (Uint8 *) pixels;
   1.262 +        dst =
   1.263 +            (Uint8 *) surface->pixels + rect->y * surface->pitch +
   1.264 +            rect->x * surface->format->BytesPerPixel;
   1.265 +        length = rect->w * surface->format->BytesPerPixel;
   1.266 +        for (row = 0; row < rect->h; ++row) {
   1.267 +            SDL_memcpy(dst, src, length);
   1.268 +            src += pitch;
   1.269 +            dst += surface->pitch;
   1.270 +        }
   1.271 +        return 0;
   1.272 +    }
   1.273 +}
   1.274 +
   1.275 +static int
   1.276 +SDL_DUMMY_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.277 +                   const SDL_Rect * rect, int markDirty, void **pixels,
   1.278 +                   int *pitch)
   1.279 +{
   1.280 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.281 +        return SDL_SW_LockYUVTexture((SDL_SW_YUVTexture *) texture->
   1.282 +                                     driverdata, rect, markDirty, pixels,
   1.283 +                                     pitch);
   1.284 +    } else {
   1.285 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.286 +
   1.287 +        *pixels =
   1.288 +            (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
   1.289 +                      rect->x * surface->format->BytesPerPixel);
   1.290 +        *pitch = surface->pitch;
   1.291 +        return 0;
   1.292 +    }
   1.293 +}
   1.294 +
   1.295 +static void
   1.296 +SDL_DUMMY_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.297 +{
   1.298 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.299 +        SDL_SW_UnlockYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
   1.300 +    }
   1.301 +}
   1.302 +
   1.303 +static void
   1.304 +SDL_DUMMY_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.305 +                    int numrects, const SDL_Rect * rects)
   1.306 +{
   1.307 +}
   1.308 +
   1.309 +static int
   1.310 +SDL_DUMMY_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
   1.311 +                  Uint32 color)
   1.312 +{
   1.313 +    SDL_DUMMY_RenderData *data = (SDL_DUMMY_RenderData *) renderer->driverdata;
   1.314 +    SDL_Surface *target = data->screens[data->current_screen];
   1.315 +    SDL_Rect real_rect = *rect;
   1.316 +    Uint8 r, g, b, a;
   1.317 +
   1.318 +    a = (Uint8) ((color >> 24) & 0xFF);
   1.319 +    r = (Uint8) ((color >> 16) & 0xFF);
   1.320 +    g = (Uint8) ((color >> 8) & 0xFF);
   1.321 +    b = (Uint8) (color & 0xFF);
   1.322 +    color = SDL_MapRGBA(target->format, r, g, b, a);
   1.323 +
   1.324 +    return SDL_FillRect(target, &real_rect, color);
   1.325 +}
   1.326 +
   1.327 +static int
   1.328 +SDL_DUMMY_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.329 +                  const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   1.330 +                  int blendMode, int scaleMode)
   1.331 +{
   1.332 +    SDL_DUMMY_RenderData *data = (SDL_DUMMY_RenderData *) renderer->driverdata;
   1.333 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.334 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.335 +
   1.336 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.337 +        SDL_Surface *target = data->screens[data->current_screen];
   1.338 +        void *pixels =
   1.339 +            (Uint8 *) target->pixels + dstrect->y * target->pitch +
   1.340 +            dstrect->x * target->format->BytesPerPixel;
   1.341 +        return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
   1.342 +                                   srcrect, display->current_mode.format,
   1.343 +                                   dstrect->w, dstrect->h, pixels,
   1.344 +                                   target->pitch);
   1.345 +    } else {
   1.346 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.347 +        SDL_Surface *target = data->screens[data->current_screen];
   1.348 +        SDL_Rect real_srcrect = *srcrect;
   1.349 +        SDL_Rect real_dstrect = *dstrect;
   1.350 +
   1.351 +        if (blendMode &
   1.352 +            (SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend)) {
   1.353 +            SDL_SetAlpha(surface, SDL_SRCALPHA, 0);
   1.354 +        } else {
   1.355 +            SDL_SetAlpha(surface, 0, 0);
   1.356 +        }
   1.357 +        if (scaleMode != SDL_TextureScaleMode_None &&
   1.358 +            (srcrect->w != dstrect->w || srcrect->h != dstrect->h)) {
   1.359 +            return SDL_SoftStretch(surface, &real_srcrect, target, &real_dstrect);
   1.360 +        } else {
   1.361 +            return SDL_LowerBlit(surface, &real_srcrect, target, &real_dstrect);
   1.362 +        }
   1.363 +    }
   1.364 +}
   1.365 +
   1.366 +static void
   1.367  SDL_DUMMY_RenderPresent(SDL_Renderer * renderer)
   1.368  {
   1.369      static int frame_number;
   1.370 -    SDL_DUMMY_RenderData *data =
   1.371 -        (SDL_DUMMY_RenderData *) renderer->driverdata;
   1.372 -    SDL_Surface *surface = data->surface;
   1.373 +    SDL_DUMMY_RenderData *data = (SDL_DUMMY_RenderData *) renderer->driverdata;
   1.374  
   1.375 +    /* Send the data to the display */
   1.376      if (SDL_getenv("SDL_VIDEO_DUMMY_SAVE_FRAMES")) {
   1.377          char file[128];
   1.378          SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
   1.379                       renderer->window, ++frame_number);
   1.380 -        SDL_SaveBMP(surface, file);
   1.381 +        SDL_SaveBMP(data->screens[data->current_screen], file);
   1.382 +    }
   1.383 +
   1.384 +    /* Update the flipping chain, if any */
   1.385 +    if (renderer->info.flags & SDL_Renderer_PresentFlip2) {
   1.386 +        data->current_screen = (data->current_screen + 1) % 2;
   1.387 +    } else if (renderer->info.flags & SDL_Renderer_PresentFlip3) {
   1.388 +        data->current_screen = (data->current_screen + 1) % 3;
   1.389      }
   1.390  }
   1.391  
   1.392 -void
   1.393 +static void
   1.394 +SDL_DUMMY_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.395 +{
   1.396 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
   1.397 +        SDL_SW_DestroyYUVTexture((SDL_SW_YUVTexture *) texture->driverdata);
   1.398 +    } else {
   1.399 +        SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
   1.400 +
   1.401 +        SDL_FreeSurface(surface);
   1.402 +    }
   1.403 +}
   1.404 +
   1.405 +static void
   1.406  SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer)
   1.407  {
   1.408 -    SDL_DUMMY_RenderData *data =
   1.409 -        (SDL_DUMMY_RenderData *) renderer->driverdata;
   1.410 +    SDL_DUMMY_RenderData *data = (SDL_DUMMY_RenderData *) renderer->driverdata;
   1.411 +    int i;
   1.412  
   1.413      if (data) {
   1.414 -        if (data->surface) {
   1.415 -            data->surface->format->palette = NULL;
   1.416 -            SDL_FreeSurface(data->surface);
   1.417 +        for (i = 0; i < SDL_arraysize(data->screens); ++i) {
   1.418 +            if (data->screens[i]) {
   1.419 +                SDL_FreeSurface(data->screens[i]);
   1.420 +            }
   1.421          }
   1.422          SDL_free(data);
   1.423      }