src/video/SDL_yuv.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1643 51038e80ae59
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* This is the implementation of the YUV video surface support */
slouken@0
    25
slouken@0
    26
#include "SDL_video.h"
slouken@0
    27
#include "SDL_sysvideo.h"
slouken@0
    28
#include "SDL_yuvfuncs.h"
slouken@0
    29
#include "SDL_yuv_sw_c.h"
slouken@0
    30
slouken@0
    31
slouken@1662
    32
SDL_Overlay *
slouken@1662
    33
SDL_CreateYUVOverlay (int w, int h, Uint32 format, SDL_Surface * display)
slouken@0
    34
{
slouken@1662
    35
    SDL_VideoDevice *_this = SDL_GetVideoDevice ();
slouken@1662
    36
    SDL_Window *window;
slouken@1662
    37
    const char *yuv_hwaccel;
slouken@1662
    38
    SDL_Overlay *overlay;
slouken@0
    39
slouken@1662
    40
    window = SDL_GetWindowFromSurface (display);
slouken@1662
    41
    if (window && (window->flags & SDL_WINDOW_OPENGL)) {
slouken@1662
    42
        SDL_SetError ("YUV overlays are not supported in OpenGL mode");
slouken@1662
    43
        return NULL;
slouken@1662
    44
    }
slouken@0
    45
slouken@1662
    46
    /* Display directly on video surface, if possible */
slouken@1662
    47
    if (SDL_getenv ("SDL_VIDEO_YUV_DIRECT")) {
slouken@1662
    48
        if (window &&
slouken@1662
    49
            ((window->surface->format->BytesPerPixel == 2) ||
slouken@1662
    50
             (window->surface->format->BytesPerPixel == 4))) {
slouken@1662
    51
            display = window->surface;
slouken@1662
    52
        }
slouken@1662
    53
    }
slouken@1662
    54
    overlay = NULL;
slouken@1662
    55
    yuv_hwaccel = SDL_getenv ("SDL_VIDEO_YUV_HWACCEL");
slouken@1662
    56
    if (((display->flags & SDL_SCREEN_SURFACE) && _this->CreateYUVOverlay) &&
slouken@1662
    57
        (!yuv_hwaccel || (SDL_atoi (yuv_hwaccel) > 0))) {
slouken@1662
    58
        overlay = _this->CreateYUVOverlay (_this, w, h, format, display);
slouken@1662
    59
    }
slouken@1662
    60
    /* If hardware YUV overlay failed ... */
slouken@1662
    61
    if (overlay == NULL) {
slouken@1662
    62
        overlay = SDL_CreateYUV_SW (_this, w, h, format, display);
slouken@1662
    63
    }
slouken@1662
    64
    return overlay;
slouken@0
    65
}
slouken@0
    66
slouken@1662
    67
int
slouken@1662
    68
SDL_LockYUVOverlay (SDL_Overlay * overlay)
slouken@0
    69
{
slouken@1662
    70
    SDL_VideoDevice *_this = SDL_GetVideoDevice ();
slouken@1662
    71
    return overlay->hwfuncs->Lock (_this, overlay);
slouken@0
    72
}
slouken@0
    73
slouken@1662
    74
void
slouken@1662
    75
SDL_UnlockYUVOverlay (SDL_Overlay * overlay)
slouken@0
    76
{
slouken@1662
    77
    SDL_VideoDevice *_this = SDL_GetVideoDevice ();
slouken@1662
    78
    overlay->hwfuncs->Unlock (_this, overlay);
slouken@0
    79
}
slouken@0
    80
slouken@1662
    81
int
slouken@1662
    82
SDL_DisplayYUVOverlay (SDL_Overlay * overlay, SDL_Rect * dstrect)
slouken@0
    83
{
slouken@1662
    84
    SDL_VideoDevice *_this = SDL_GetVideoDevice ();
slouken@1662
    85
    SDL_Rect src, dst;
slouken@1662
    86
    int srcx, srcy, srcw, srch;
slouken@1662
    87
    int dstx, dsty, dstw, dsth;
slouken@1643
    88
slouken@1662
    89
    /* Clip the rectangle to the screen area */
slouken@1662
    90
    srcx = 0;
slouken@1662
    91
    srcy = 0;
slouken@1662
    92
    srcw = overlay->w;
slouken@1662
    93
    srch = overlay->h;
slouken@1662
    94
    dstx = dstrect->x;
slouken@1662
    95
    dsty = dstrect->y;
slouken@1662
    96
    dstw = dstrect->w;
slouken@1662
    97
    dsth = dstrect->h;
slouken@1662
    98
    if (dstx < 0) {
slouken@1662
    99
        srcw += (dstx * overlay->w) / dstrect->w;
slouken@1662
   100
        dstw += dstx;
slouken@1662
   101
        srcx -= (dstx * overlay->w) / dstrect->w;
slouken@1662
   102
        dstx = 0;
slouken@1662
   103
    }
slouken@1662
   104
    if ((dstx + dstw) > SDL_VideoSurface->w) {
slouken@1662
   105
        int extra = (dstx + dstw - SDL_VideoSurface->w);
slouken@1662
   106
        srcw -= (extra * overlay->w) / dstrect->w;
slouken@1662
   107
        dstw -= extra;
slouken@1662
   108
    }
slouken@1662
   109
    if (dsty < 0) {
slouken@1662
   110
        srch += (dsty * overlay->h) / dstrect->h;
slouken@1662
   111
        dsth += dsty;
slouken@1662
   112
        srcy -= (dsty * overlay->h) / dstrect->h;
slouken@1662
   113
        dsty = 0;
slouken@1662
   114
    }
slouken@1662
   115
    if ((dsty + dsth) > SDL_VideoSurface->h) {
slouken@1662
   116
        int extra = (dsty + dsth - SDL_VideoSurface->h);
slouken@1662
   117
        srch -= (extra * overlay->h) / dstrect->h;
slouken@1662
   118
        dsth -= extra;
slouken@1662
   119
    }
slouken@1662
   120
    if (srcw <= 0 || srch <= 0 || srch <= 0 || dsth <= 0) {
slouken@1662
   121
        return 0;
slouken@1662
   122
    }
slouken@1662
   123
    /* Ugh, I can't wait for SDL_Rect to be int values */
slouken@1662
   124
    src.x = srcx;
slouken@1662
   125
    src.y = srcy;
slouken@1662
   126
    src.w = srcw;
slouken@1662
   127
    src.h = srch;
slouken@1662
   128
    dst.x = dstx;
slouken@1662
   129
    dst.y = dsty;
slouken@1662
   130
    dst.w = dstw;
slouken@1662
   131
    dst.h = dsth;
slouken@1662
   132
    return overlay->hwfuncs->Display (_this, overlay, &src, &dst);
slouken@0
   133
}
slouken@0
   134
slouken@1662
   135
void
slouken@1662
   136
SDL_FreeYUVOverlay (SDL_Overlay * overlay)
slouken@0
   137
{
slouken@1662
   138
    SDL_VideoDevice *_this = SDL_GetVideoDevice ();
slouken@1662
   139
    if (overlay) {
slouken@1662
   140
        if (overlay->hwfuncs) {
slouken@1662
   141
            overlay->hwfuncs->FreeHW (_this, overlay);
slouken@1662
   142
        }
slouken@1662
   143
        SDL_free (overlay);
slouken@1662
   144
    }
slouken@0
   145
}
slouken@1662
   146
slouken@1662
   147
/* vi: set ts=4 sw=4 expandtab: */